Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

146 line
6.9 KiB

  1. # SPDX-FileCopyrightText: 2023 - 2024 Nikita Chernyi
  2. # SPDX-FileCopyrightText: 2023 - 2024 Slavi Pantaleev
  3. # SPDX-FileCopyrightText: 2024 Suguru Hirahara
  4. #
  5. # SPDX-License-Identifier: AGPL-3.0-or-later
  6. # mise (dev tool version manager)
  7. mise_data_dir := env("MISE_DATA_DIR", justfile_directory() / "var/mise")
  8. mise_trusted_config_paths := justfile_directory() / "mise.toml"
  9. prek_home := env("PREK_HOME", justfile_directory() / "var/prek")
  10. # Shows help
  11. default:
  12. @{{ just_executable() }} --list --justfile "{{ justfile() }}"
  13. # Adds a new host to the inventory, creating the inventory files if necessary (e.g. `just add-inventory-host example.com 1.2.3.4`)
  14. add-inventory-host domain server_address:
  15. @{{ justfile_directory() }}/bin/add-inventory-host.sh {{ quote(domain) }} {{ quote(server_address) }}
  16. # Pulls external Ansible roles
  17. roles:
  18. #!/usr/bin/env sh
  19. echo "[NOTE] This command just updates the roles, but if you want to update everything at once (playbook, roles, etc.) - use 'just update'"
  20. if [ -x "$(command -v agru)" ]; then
  21. agru -no-tui
  22. else
  23. rm -rf roles/galaxy
  24. ansible-galaxy install -r requirements.yml -p roles/galaxy/ --force
  25. fi
  26. # Updates the playbook and installs the necessary Ansible roles pinned in requirements.yml. If a -u flag is passed, also updates the requirements.yml file with new role versions (if available)
  27. update *flags: update-playbook-only
  28. #!/usr/bin/env sh
  29. if [ -x "$(command -v agru)" ]; then
  30. echo {{ if flags == "" { "Installing roles pinned in requirements.yml…" } else { if flags == "-u" { "Updating roles and pinning new versions in requirements.yml…" } else { "Unknown flags passed" } } }}
  31. agru -no-tui {{ flags }}
  32. else
  33. echo "[NOTE] You are using the standard ansible-galaxy tool to install roles, which is slow and lacks other features. We recommend installing the 'agru' tool to speed up the process: https://github.com/etkecc/agru#where-to-get"
  34. echo "Installing roles…"
  35. rm -rf roles/galaxy
  36. ansible-galaxy install -r requirements.yml -p roles/galaxy/ --force
  37. fi
  38. # Updates the playbook without installing/updating Ansible roles
  39. update-playbook-only:
  40. @echo "Updating playbook…"
  41. @git stash -q
  42. @git pull -q
  43. @-git stash pop -q
  44. # Invokes mise with the project-local data directory
  45. mise *args: _ensure_mise_data_directory
  46. #!/bin/sh
  47. export MISE_DATA_DIR="{{ mise_data_dir }}"
  48. export MISE_TRUSTED_CONFIG_PATHS="{{ mise_trusted_config_paths }}"
  49. export MISE_YES=1
  50. export PREK_HOME="{{ prek_home }}"
  51. mise {{ args }}
  52. # Runs prek (pre-commit hooks manager) with the given arguments
  53. prek *args: _ensure_mise_tools_installed
  54. @{{ just_executable() }} --justfile "{{ justfile() }}" mise exec -- prek {{ args }}
  55. # Runs pre-commit hooks on staged files
  56. prek-run-on-staged *args: _ensure_mise_tools_installed
  57. @{{ just_executable() }} --justfile "{{ justfile() }}" prek run {{ args }}
  58. # Runs pre-commit hooks on all files
  59. prek-run-on-all *args: _ensure_mise_tools_installed
  60. @{{ just_executable() }} --justfile "{{ justfile() }}" prek run --all-files {{ args }}
  61. # Installs the git pre-commit hook
  62. prek-install-git-pre-commit-hook: _ensure_mise_tools_installed
  63. #!/usr/bin/env sh
  64. set -eu
  65. {{ just_executable() }} --justfile "{{ justfile() }}" mise exec -- prek install
  66. # The installed git hooks run later under Git, outside this just/mise environment,
  67. # so they need to be told how to find their tooling:
  68. #
  69. # - PREK_HOME keeps prek's cache under var/prek instead of a global home dir.
  70. # - MISE_DATA_DIR / MISE_TRUSTED_CONFIG_PATHS make mise resolve against this project's
  71. # own data directory. Without them mise falls back to the global one and silently
  72. # installs a second copy of the tool there.
  73. # - prek bakes the full path of the currently installed version into the hook
  74. # (var/mise/installs/prek/<version>/...), which stops working as soon as the pinned
  75. # version changes or old versions are pruned. Pointing at mise's shim instead makes
  76. # the hook resolve whatever mise.toml pins, at the time it runs.
  77. #
  78. # Which hook files prek installs depends on `default_install_hook_types` in
  79. # .pre-commit-config.yaml, so patch every hook file that prek generated.
  80. for hook in "{{ justfile_directory() }}"/.git/hooks/*; do
  81. [ -f "$hook" ] || continue
  82. grep -q 'generated by prek' "$hook" || continue
  83. grep -q '^export PREK_HOME=' "$hook" || sed -i '2iexport PREK_HOME="{{ prek_home }}"' "$hook"
  84. grep -q '^export MISE_DATA_DIR=' "$hook" || sed -i '3iexport MISE_DATA_DIR="{{ mise_data_dir }}"' "$hook"
  85. grep -q '^export MISE_TRUSTED_CONFIG_PATHS=' "$hook" || sed -i '4iexport MISE_TRUSTED_CONFIG_PATHS="{{ mise_trusted_config_paths }}"' "$hook"
  86. sed -i 's#^PREK=".*"$#PREK="{{ mise_data_dir }}/shims/prek"#' "$hook"
  87. done
  88. # Runs the playbook with --tags=install-all,ensure-matrix-users-created,start and optional arguments
  89. install-all *extra_args: (run-tags "install-all,ensure-matrix-users-created,start" extra_args)
  90. # Runs installation tasks for a single service
  91. install-service service *extra_args:
  92. {{ just_executable() }} --justfile "{{ justfile() }}" run \
  93. --tags=install-{{ service }},start-group \
  94. --extra-vars=group={{ service }} {{ extra_args }}
  95. # Runs the playbook with --tags=setup-all,ensure-matrix-users-created,start and optional arguments
  96. setup-all *extra_args: (run-tags "setup-all,ensure-matrix-users-created,start" extra_args)
  97. # Runs the playbook with the given list of arguments
  98. run +extra_args:
  99. ansible-playbook -i inventory/hosts setup.yml {{ extra_args }}
  100. # Runs the playbook with the given list of comma-separated tags and optional arguments
  101. run-tags tags *extra_args:
  102. {{ just_executable() }} --justfile "{{ justfile() }}" run --tags={{ tags }} {{ extra_args }}
  103. # Runs the playbook in user-registration mode
  104. register-user username password admin_yes_or_no *extra_args:
  105. ansible-playbook -i inventory/hosts setup.yml --tags=register-user --extra-vars="username={{ username }} password={{ password }} admin={{ admin_yes_or_no }}" {{ extra_args }}
  106. # Starts all services
  107. start-all *extra_args: (run-tags "start-all" extra_args)
  108. # Starts a specific service group
  109. start-group group *extra_args:
  110. @{{ just_executable() }} --justfile "{{ justfile() }}" run-tags start-group --extra-vars="group={{ group }}" {{ extra_args }}
  111. # Stops all services
  112. stop-all *extra_args: (run-tags "stop-all" extra_args)
  113. # Stops a specific service group
  114. stop-group group *extra_args:
  115. @{{ just_executable() }} --justfile "{{ justfile() }}" run-tags stop-group --extra-vars="group={{ group }}" {{ extra_args }}
  116. # Internal - ensures var/mise and var/prek directories exist
  117. _ensure_mise_data_directory:
  118. @mkdir -p "{{ mise_data_dir }}"
  119. @mkdir -p "{{ prek_home }}"
  120. # Internal - ensures mise tools are installed
  121. _ensure_mise_tools_installed: _ensure_mise_data_directory
  122. @{{ just_executable() }} --justfile "{{ justfile() }}" mise install --quiet