entrypoint-provider 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # Provide a "files_dir" variable that points to inventory/dynamic/injected
  23. echo "files_dir: \"${FILES}\"" > "${WORK}/inventory/dynamic/${TYPE}/group_vars/all/00_default_files_dir.yml"
  24. # Add any injected variable files into the group vars directory
  25. find "${FILES}" \( -name '*.yml' -or -name '*.yaml' -or -name vars \) -print0 | xargs -0 -L1 -I {} ln -fs {} "${WORK}/inventory/dynamic/${TYPE}/group_vars/all"
  26. # Avoid sudo when running locally - nothing in the image requires it.
  27. mkdir -p "${WORK}/inventory/dynamic/${TYPE}/host_vars/localhost"
  28. echo "ansible_become: no" > "${WORK}/inventory/dynamic/${TYPE}/host_vars/localhost/00_skip_root.yaml"
  29. if [[ -z "${ANSIBLE_CONFIG-}" ]]; then
  30. export ANSIBLE_CONFIG="${WORK}/inventory/dynamic/${TYPE}/ansible.cfg"
  31. fi
  32. # SSH requires the file to be owned by the current user, but Docker copies
  33. # files in as root. Put the file into the ssh dir with the right permissions
  34. if [[ -f "${FILES}/ssh-privatekey" ]]; then
  35. if [[ "$TYPE" == 'gcp' ]]; then
  36. keyfile="${HOME}/.ssh/google_compute_engine"
  37. else
  38. keyfile="${HOME}/.ssh/id_rsa"
  39. fi
  40. mkdir "${HOME}/.ssh"
  41. rm -f "${keyfile}"
  42. cat "${FILES}/ssh-privatekey" > "${keyfile}"
  43. chmod 0600 "${keyfile}"
  44. ssh-keygen -y -f "${keyfile}" > "${keyfile}.pub"
  45. fi
  46. if [[ "$TYPE" == 'gcp' ]]; then
  47. if [[ -f "${FILES}/gce.json" ]]; then
  48. gcloud auth activate-service-account --quiet --key-file="${FILES}/gce.json"
  49. else
  50. echo "No service account file found at ${FILES}/gce.json, bypassing login"
  51. fi
  52. fi
  53. if [[ "$TYPE" == 'azure' ]]; then
  54. if [[ -f "${FILES}/credentials" ]]; then
  55. set -a
  56. . "${FILES}/credentials"
  57. set +a
  58. az login --service-principal --username "$AZURE_CLIENT_ID" --password "$AZURE_SECRET" --tenant "$AZURE_TENANT" >/dev/null
  59. else
  60. echo "No service account file found at ${FILES}/credentials, bypassing login"
  61. fi
  62. fi
  63. exec "$@"