provision_ssh.j2.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/bin/bash
  2. set -euo pipefail
  3. if [[ -n "{{ openshift_gcp_ssh_private_key }}" ]]; then
  4. # Create SSH key for GCE
  5. if [ ! -f "{{ openshift_gcp_ssh_private_key }}" ]; then
  6. ssh-keygen -t rsa -f "{{ openshift_gcp_ssh_private_key }}" -C gce-provision-cloud-user -N ''
  7. ssh-add "{{ openshift_gcp_ssh_private_key }}" || true
  8. fi
  9. # Check if the public key is in the project metadata, and if not, add it there
  10. if [ -f "{{ openshift_gcp_ssh_private_key }}.pub" ]; then
  11. pub_file="{{ openshift_gcp_ssh_private_key }}.pub"
  12. pub_key=$(cut -d ' ' -f 2 < "{{ openshift_gcp_ssh_private_key }}.pub")
  13. else
  14. keyfile="${HOME}/.ssh/google_compute_engine"
  15. pub_file="${keyfile}.pub"
  16. mkdir -p "${HOME}/.ssh"
  17. cp "{{ openshift_gcp_ssh_private_key }}" "${keyfile}"
  18. chmod 0600 "${keyfile}"
  19. ssh-keygen -y -f "${keyfile}" > "${pub_file}"
  20. pub_key=$(cut -d ' ' -f 2 < "${pub_file}")
  21. fi
  22. key_tmp_file='/tmp/ocp-gce-keys'
  23. if ! gcloud --project "{{ openshift_gcp_project }}" compute project-info describe | grep -q "$pub_key"; then
  24. if gcloud --project "{{ openshift_gcp_project }}" compute project-info describe | grep -q ssh-rsa; then
  25. gcloud --project "{{ openshift_gcp_project }}" compute project-info describe | grep ssh-rsa | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/value: //' > "$key_tmp_file"
  26. fi
  27. echo -n 'cloud-user:' >> "$key_tmp_file"
  28. cat "${pub_file}" >> "$key_tmp_file"
  29. gcloud --project "{{ openshift_gcp_project }}" compute project-info add-metadata --metadata-from-file "sshKeys=${key_tmp_file}"
  30. rm -f "$key_tmp_file"
  31. fi
  32. fi