Files
homelab-infra/ansible/ci/deploy-machine.sh
T
karpenokandCursor a0507719b0 refactor: drop the finished secrets migration from the deploy path
Product tags now come from the role directory, and edits to the CI scripts no longer redeploy every machine.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-11 00:09:23 +07:00

106 lines
4.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Вход в Infisical, SSH и ansible-playbook для одной машины.
# Репозиторий уже должен быть склонирован: скрипт лежит внутри него.
set -euo pipefail
machine=${1:?machine}
event=${2:-}
before=${3:-}
after=${4:-}
case "$machine" in
core-net | core-secrets | rus-central) ;;
*)
echo "Неизвестная машина: $machine" >&2
exit 2
;;
esac
if [ -z "${INFISICAL_CLIENT_ID:-}" ] || [ -z "${INFISICAL_CLIENT_SECRET:-}" ]; then
echo "Нет INFISICAL_CLIENT_ID или INFISICAL_CLIENT_SECRET" >&2
exit 1
fi
export INFISICAL_PROJECT_ID="806e7f09-5838-492a-946b-2d69b96876ab"
export INFISICAL_DOMAIN="http://192.168.1.102:8080/api"
TOKEN=$(infisical login \
--domain="${INFISICAL_DOMAIN}" \
--method=universal-auth \
--client-id="${INFISICAL_CLIENT_ID}" \
--client-secret="${INFISICAL_CLIENT_SECRET}" \
--plain)
eval "$(infisical export --domain="${INFISICAL_DOMAIN}" --token="${TOKEN}" --env=prod --projectId="${INFISICAL_PROJECT_ID}" --path="/ci-cd" --format=dotenv-export --silent)"
if [ -z "${RUNNER_SSH_KEY:-}" ]; then
echo "В Infisical (/ci-cd) отсутствует RUNNER_SSH_KEY" >&2
exit 1
fi
install -d -m 700 "${HOME}/.ssh"
printf '%s\n' "${RUNNER_SSH_KEY}" > "${HOME}/.ssh/id_ed25519"
chmod 600 "${HOME}/.ssh/id_ed25519"
trap 'rm -f "${HOME}/.ssh/id_ed25519"' EXIT
extra=()
case "$machine" in
core-net)
if ! infisical export --domain="${INFISICAL_DOMAIN}" --token="${TOKEN}" --env=prod --projectId="${INFISICAL_PROJECT_ID}" --path="/services/authentik" --format=dotenv-export --silent 2>/dev/null | grep -q 'AUTHENTIK_SECRET_KEY'; then
infisical secrets-manager folders create authentik --path /services --env prod --projectId "${INFISICAL_PROJECT_ID}" --domain "${INFISICAL_DOMAIN}" --token "${TOKEN}" --silent 2>/dev/null || true
GEN_SECRET_KEY=$(openssl rand -base64 48 | tr -dc 'a-zA-Z0-9' | head -c 50)
GEN_PG_PASS=$(openssl rand -hex 20)
infisical secrets set --domain="${INFISICAL_DOMAIN}" --token="${TOKEN}" --env=prod --projectId="${INFISICAL_PROJECT_ID}" --path="/services/authentik" \
"AUTHENTIK_SECRET_KEY=${GEN_SECRET_KEY}" \
"AUTHENTIK_POSTGRES_PASSWORD=${GEN_PG_PASS}" \
--silent
unset GEN_SECRET_KEY GEN_PG_PASS
fi
eval "$(infisical export --domain="${INFISICAL_DOMAIN}" --token="${TOKEN}" --env=prod --projectId="${INFISICAL_PROJECT_ID}" --path="/services/authentik" --format=dotenv-export --silent)"
export AUTHENTIK_SECRET_KEY="${AUTHENTIK_SECRET_KEY:-}"
export AUTHENTIK_POSTGRES_PASSWORD="${AUTHENTIK_POSTGRES_PASSWORD:-}"
;;
core-secrets)
eval "$(infisical export --domain="${INFISICAL_DOMAIN}" --token="${TOKEN}" --env=prod --projectId="${INFISICAL_PROJECT_ID}" --path="/services/infisical" --format=dotenv-export --silent)"
export INFISICAL_POSTGRES_PASSWORD="${POSTGRES_PASSWORD}"
export INFISICAL_ENCRYPTION_KEY="${ENCRYPTION_KEY}"
export INFISICAL_AUTH_SECRET="${AUTH_SECRET}"
;;
rus-central)
input_email="${INPUT_LETSENCRYPT_EMAIL:-}"
eval "$(infisical export --domain="${INFISICAL_DOMAIN}" --token="${TOKEN}" --env=prod --projectId="${INFISICAL_PROJECT_ID}" --path="/services/netbird" --format=dotenv-export --silent)"
export NETBIRD_AUTH_SECRET="${NETBIRD_AUTH_SECRET:-}"
export NETBIRD_DATASTORE_KEY="${NETBIRD_DATASTORE_KEY:-}"
export NETBIRD_SESSION_KEY="${NETBIRD_SESSION_KEY:-}"
if [ -n "$input_email" ]; then
NETBIRD_LETSENCRYPT_EMAIL="$input_email"
fi
if [ -n "${NETBIRD_LETSENCRYPT_EMAIL:-}" ]; then
extra+=(-e "netbird_letsencrypt_email=${NETBIRD_LETSENCRYPT_EMAIL}")
fi
;;
esac
root=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
cd "$root"
target_file=$(mktemp)
bash ansible/ci/ansible-target.sh "$machine" "$event" "$before" "$after" > "$target_file"
mapfile -t target < "$target_file"
rm -f "$target_file"
if [ "${#target[@]}" -eq 0 ]; then
echo "Для ${machine} нет применимых изменений."
exit 0
fi
export ANSIBLE_HOST_KEY_CHECKING=False
cd "$root/ansible"
key="${HOME}/.ssh/id_ed25519"
ansible-playbook "${target[@]}" \
-i inventories/prod/hosts.yml \
--private-key "$key" \
"${extra[@]}"