refactor: split machine deploys from one-time secrets migration
ansible-core-net / deploy (push) Failing after 39s
ansible-core-secrets / deploy (push) Successful in 30s
ansible-rus-central / deploy (push) Failing after 15s

Roles stay under the machine that runs them, CI applies only a known product, and the /opt/secrets move is a one-shot playbook instead of part of every core-secrets run.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
karpenokandCursor committed 2026-11-10 23:55:34 +07:00
1 parent 341538fba9
commit 9c692522fb
85 files changed
+537 -492

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
#!/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"
if [ "$machine" = "core-secrets" ] && [ -f playbooks/migrate-core-secrets.yml ]; then
ansible-playbook playbooks/migrate-core-secrets.yml \
-i inventories/prod/hosts.yml \
--private-key "$key"
fi
ansible-playbook "${target[@]}" \
-i inventories/prod/hosts.yml \
--private-key "$key" \
"${extra[@]}"