entrypoint-provider 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. #
  3. # This file sets up the user to run in a cloud environment.
  4. # It provides dynamic inventory that works well when run in
  5. # a container environment by setting up a default inventory.
  6. # It assumes the user has provided a service account token
  7. # and ssh-privatekey file at "$(pwd)/inventory/dynamic/injected"
  8. # and automatically links any YAML files found into the group
  9. # vars directory, which allows the playbook to more easily be
  10. # run in containerized contexts.
  11. #
  12. # Currently GCP and Azure are supported.
  13. set -euo pipefail
  14. WORK=$(pwd)
  15. FILES="${WORK}/inventory/dynamic/injected"
  16. # Patch /etc/passwd file with the current user info.
  17. # The current user's entry must be correctly defined in this file in order for
  18. # the `ssh` command to work within the created container.
  19. if ! whoami &>/dev/null; then
  20. echo "${USER:-default}:x:$(id -u):$(id -g):Default User:$HOME:/sbin/nologin" >> /etc/passwd
  21. fi
  22. mkdir -p "${WORK}/inventory/dynamic/${TYPE}/group_vars/all"
  23. # Override cluster version check when running in CI
  24. echo "ci_version_override: true" > "${WORK}/inventory/dynamic/${TYPE}/group_vars/all/ci_version_override.yml"
  25. # Add any injected variable files into the group vars directory
  26. find "${FILES}" \( -name '*.yml' -or -name '*.yaml' -or -name vars \) -print0 | xargs -0 -L1 -I {} ln -fs {} "${WORK}/inventory/dynamic/${TYPE}/group_vars/all"
  27. # Avoid sudo when running locally - nothing in the image requires it.
  28. mkdir -p "${WORK}/inventory/dynamic/${TYPE}/host_vars/localhost"
  29. echo "ansible_become: no" > "${WORK}/inventory/dynamic/${TYPE}/host_vars/localhost/00_skip_root.yaml"
  30. if [[ -z "${ANSIBLE_CONFIG-}" ]]; then
  31. export ANSIBLE_CONFIG="${WORK}/inventory/dynamic/${TYPE}/ansible.cfg"
  32. fi
  33. # SSH requires the file to be owned by the current user, but Docker copies
  34. # files in as root. Put the file into the ssh dir with the right permissions
  35. if [[ -f "${FILES}/ssh-privatekey" ]]; then
  36. if [[ "$TYPE" == 'gcp' ]]; then
  37. keyfile="${HOME}/.ssh/google_compute_engine"
  38. else
  39. keyfile="${HOME}/.ssh/id_rsa"
  40. fi
  41. mkdir "${HOME}/.ssh"
  42. rm -f "${keyfile}"
  43. cat "${FILES}/ssh-privatekey" > "${keyfile}"
  44. chmod 0600 "${keyfile}"
  45. ssh-keygen -y -f "${keyfile}" > "${keyfile}.pub"
  46. fi
  47. if [[ "$TYPE" == 'gcp' ]]; then
  48. if [[ -f "${FILES}/gce.json" ]]; then
  49. gcloud auth activate-service-account --quiet --key-file="${FILES}/gce.json"
  50. else
  51. echo "No service account file found at ${FILES}/gce.json, bypassing login"
  52. fi
  53. fi
  54. if [[ "$TYPE" == 'azure' ]]; then
  55. if [[ -f "${FILES}/credentials" ]]; then
  56. set -a
  57. . "${FILES}/credentials"
  58. set +a
  59. az login --service-principal --username "$AZURE_CLIENT_ID" --password "$AZURE_SECRET" --tenant "$AZURE_TENANT" >/dev/null
  60. else
  61. echo "No service account file found at ${FILES}/credentials, bypassing login"
  62. fi
  63. fi
  64. exec "$@"