Matrix Docker Ansible eploy
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

99 řádky
3.3 KiB

  1. #!/bin/bash
  2. # Runs a role's Molecule scenario locally.
  3. #
  4. # Called through `just molecule [role] [args...]`. With no role, lists the roles
  5. # that have a scenario.
  6. #
  7. # The same scenarios run in CI (.github/workflows/molecule.yml), but running one
  8. # here is the faster loop while writing or fixing a role: CI only tells you after
  9. # a push, and only about the roles that push touched.
  10. #
  11. # Deliberately NOT wired into prek. A run takes minutes, pulls container images
  12. # and needs a working Docker - which is fine when you ask for it, and not fine on
  13. # every commit.
  14. #
  15. # Usage:
  16. # just molecule # list roles that have a scenario
  17. # just molecule matrix-alertmanager-receiver
  18. # just molecule matrix-alertmanager-receiver converge # any molecule subcommand
  19. #
  20. # Environment:
  21. # MOLECULE_DISTRO base image to test on (default: ubuntu2604)
  22. set -euo pipefail
  23. repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  24. roles_dir="${repo_dir}/roles/custom"
  25. venv_dir="${repo_dir}/var/molecule-venv"
  26. list_roles() {
  27. find "${roles_dir}" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' \
  28. | while read -r candidate; do
  29. if [ -f "${roles_dir}/${candidate}/molecule/default/molecule.yml" ]; then
  30. echo "${candidate}"
  31. fi
  32. done \
  33. | sort
  34. }
  35. role="${1:-}"
  36. if [ -z "${role}" ]; then
  37. echo "Roles with a Molecule scenario:"
  38. found="$(list_roles)"
  39. if [ -z "${found}" ]; then
  40. echo " (none yet)"
  41. else
  42. printf ' %s\n' ${found}
  43. fi
  44. echo
  45. echo "Run one with: just molecule <role>"
  46. exit 0
  47. fi
  48. shift || true
  49. if [ ! -f "${roles_dir}/${role}/molecule/default/molecule.yml" ]; then
  50. echo "No Molecule scenario at roles/custom/${role}/molecule/default" >&2
  51. echo >&2
  52. echo "Roles that have one:" >&2
  53. list_roles | sed 's/^/ /' >&2
  54. exit 1
  55. fi
  56. # The virtualenv lives under var/, which is gitignored, and is shared by every
  57. # role - the dependencies are the same for all of them.
  58. if [ ! -x "${venv_dir}/bin/molecule" ]; then
  59. echo "Creating the Molecule virtualenv in ${venv_dir/#$HOME/\~} ..."
  60. python3 -m venv "${venv_dir}"
  61. "${venv_dir}/bin/pip" install --quiet --upgrade pip
  62. "${venv_dir}/bin/pip" install --quiet -r "${repo_dir}/molecule-shared/requirements.txt"
  63. fi
  64. # Galaxy content is installed with `force: true` on every run, so two scenarios
  65. # running at once will re-extract collections and roles into the same directory
  66. # and pull them out from under each other mid-play. It shows up as a collection
  67. # that was working moments earlier going missing:
  68. #
  69. # the connection plugin 'community.docker.docker' was not found
  70. #
  71. # ANSIBLE_HOME relocates both `collections/` and `roles/`, so one variable is
  72. # enough to give each role its own. The scenarios' ANSIBLE_ROLES_PATH follows it.
  73. #
  74. # Unset in CI, where it falls back to ~/.ansible - each role runs in its own job
  75. # there, so there is nothing to collide with and nothing to gain from isolation.
  76. export ANSIBLE_HOME="${ANSIBLE_HOME:-${repo_dir}/var/molecule-ansible-home/${role}}"
  77. export MOLECULE_DISTRO="${MOLECULE_DISTRO:-ubuntu2604}"
  78. export PY_COLORS="${PY_COLORS:-1}"
  79. export ANSIBLE_FORCE_COLOR="${ANSIBLE_FORCE_COLOR:-1}"
  80. echo "Running Molecule for ${role} on ${MOLECULE_DISTRO} ..."
  81. cd "${roles_dir}/${role}"
  82. if [ $# -eq 0 ]; then
  83. exec "${venv_dir}/bin/molecule" test --scenario-name default
  84. fi
  85. exec "${venv_dir}/bin/molecule" "$@" --scenario-name default