From 54d5e15b90ded5f5aca661960b5e2147a4797158 Mon Sep 17 00:00:00 2001 From: devops Date: Sat, 18 Jul 2026 20:37:21 +0000 Subject: [PATCH] Upload files to "root" --- root/recover-stable-startup.sh | 58 +++++++++++++++++++++++++++ root/retire-headlamp-from-gitea.sh | 63 ++++++++++++++++++++++++++++++ root/verify-stable-startup.sh | 62 +++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 root/recover-stable-startup.sh create mode 100644 root/retire-headlamp-from-gitea.sh create mode 100644 root/verify-stable-startup.sh diff --git a/root/recover-stable-startup.sh b/root/recover-stable-startup.sh new file mode 100644 index 0000000..6a4789a --- /dev/null +++ b/root/recover-stable-startup.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +set -Eeuo pipefail +IFS=$'\n\t' + +CLUSTER_NAME="${CLUSTER_NAME:-lab-cluster}" +RUNNER_NAME="${GITEA_RUNNER_CONTAINER:-gitea-runner}" +KUBECONFIG_PATH="${KUBECONFIG_PATH:-/DATA/.kube/config}" + +echo "== Desactivando watchdog anterior ==" +sudo systemctl disable --now k3d-lab-ensure.timer 2>/dev/null || true +sudo systemctl stop k3d-lab-ensure.service 2>/dev/null || true +sudo systemctl reset-failed k3d-lab-ensure.service 2>/dev/null || true + +mapfile -t cluster_containers < <( + docker ps -aq --filter "label=k3d.cluster=${CLUSTER_NAME}" +) + +if (( ${#cluster_containers[@]} == 0 )); then + echo "ERROR: no se encontraron contenedores del clúster ${CLUSTER_NAME}." >&2 + exit 1 +fi + +echo "== Restaurando políticas confirmadas ==" +docker update --restart=unless-stopped "${cluster_containers[@]}" >/dev/null + +if docker inspect "$RUNNER_NAME" >/dev/null 2>&1; then + docker update --restart=unless-stopped "$RUNNER_NAME" >/dev/null + if [[ "$(docker inspect -f '{{.State.Running}}' "$RUNNER_NAME")" != "true" ]]; then + docker start "$RUNNER_NAME" >/dev/null + fi +fi + +echo "== Recuperando k3d sin detener nodos activos ==" +if ! k3d cluster start "$CLUSTER_NAME" --wait --timeout 360s; then + echo "AVISO: k3d devolvió error; se esperará la API porque K3s puede continuar iniciando." >&2 +fi + +export KUBECONFIG="$KUBECONFIG_PATH" + +deadline=$((SECONDS + 360)) +while (( SECONDS < deadline )); do + if kubectl get --raw=/readyz --request-timeout=5s >/dev/null 2>&1; then + break + fi + sleep 5 +done + +kubectl get --raw=/readyz --request-timeout=5s >/dev/null +kubectl wait --for=condition=Ready nodes --all --timeout=300s + +echo +echo "== Estado final ==" +k3d cluster list +kubectl get nodes +docker ps --filter "name=${RUNNER_NAME}" + +echo +echo "OK: políticas estables y runtime recuperados." diff --git a/root/retire-headlamp-from-gitea.sh b/root/retire-headlamp-from-gitea.sh new file mode 100644 index 0000000..7fc041f --- /dev/null +++ b/root/retire-headlamp-from-gitea.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +set -Eeuo pipefail +IFS=$'\n\t' + +APPS_REPO="${APPS_REPO:-/DATA/AppData/k3d-lab/git/apps-registry}" +PLATFORM_REPO="${PLATFORM_REPO:-/DATA/AppData/k3d-lab/git/platform-infra}" +CONFIRM_RETIRE_HEADLAMP="${CONFIRM_RETIRE_HEADLAMP:-false}" + +find_application_files() { + local repo="$1" + local app_name="$2" + + grep -RIl \ + --exclude-dir=.git \ + --include='*.yaml' \ + --include='*.yml' \ + -E "name:[[:space:]]*${app_name}([[:space:]]|$)" \ + "$repo" 2>/dev/null || true +} + +APP_FILES="$(find_application_files "$APPS_REPO" headlamp-app)" +GOV_FILES="$(find_application_files "$PLATFORM_REPO" headlamp-governance)" + +printf 'apps-registry:\n' +printf ' workload: %s\n' "${APPS_REPO}/workloads/headlamp" +printf '%s\n' "$APP_FILES" | sed '/^$/d;s/^/ application: /' + +printf 'platform-infra:\n' +printf '%s\n' "$GOV_FILES" | sed '/^$/d;s/^/ application: /' +find "$PLATFORM_REPO" \ + -path '*/.git' -prune -o \ + -type d -name headlamp-governance -print 2>/dev/null | + sed 's/^/ governance: /' + +if [[ "$CONFIRM_RETIRE_HEADLAMP" != "true" ]]; then + printf '\nDRY-RUN. Usa CONFIRM_RETIRE_HEADLAMP=true para retirar las rutas detectadas.\n' + exit 0 +fi + +if [[ -d "${APPS_REPO}/workloads/headlamp" ]]; then + git -C "$APPS_REPO" rm -r workloads/headlamp +fi + +while IFS= read -r file; do + [[ -n "$file" ]] || continue + git -C "$APPS_REPO" rm "${file#${APPS_REPO}/}" +done <<< "$APP_FILES" + +while IFS= read -r file; do + [[ -n "$file" ]] || continue + git -C "$PLATFORM_REPO" rm "${file#${PLATFORM_REPO}/}" +done <<< "$GOV_FILES" + +while IFS= read -r directory; do + [[ -n "$directory" ]] || continue + git -C "$PLATFORM_REPO" rm -r "${directory#${PLATFORM_REPO}/}" +done < <( + find "$PLATFORM_REPO" \ + -path '*/.git' -prune -o \ + -type d -name headlamp-governance -print 2>/dev/null +) + +printf '\nCambios preparados. Revisa git diff, haz commit y push en ambos repositorios.\n' diff --git a/root/verify-stable-startup.sh b/root/verify-stable-startup.sh new file mode 100644 index 0000000..4b8ac22 --- /dev/null +++ b/root/verify-stable-startup.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +set -Eeuo pipefail +IFS=$'\n\t' + +CLUSTER_NAME="${CLUSTER_NAME:-lab-cluster}" +RUNNER_NAME="${GITEA_RUNNER_CONTAINER:-gitea-runner}" +KUBECONFIG_PATH="${KUBECONFIG_PATH:-/DATA/.kube/config}" + +export KUBECONFIG="$KUBECONFIG_PATH" + +echo "== Políticas k3d ==" +docker inspect \ + "k3d-${CLUSTER_NAME}-server-0" \ + "k3d-${CLUSTER_NAME}-agent-0" \ + "k3d-${CLUSTER_NAME}-agent-1" \ + "k3d-${CLUSTER_NAME}-serverlb" \ + --format '{{.Name}} restart={{.HostConfig.RestartPolicy.Name}} status={{.State.Status}}' + +bad="$( + docker inspect \ + "k3d-${CLUSTER_NAME}-server-0" \ + "k3d-${CLUSTER_NAME}-agent-0" \ + "k3d-${CLUSTER_NAME}-agent-1" \ + "k3d-${CLUSTER_NAME}-serverlb" \ + --format '{{.HostConfig.RestartPolicy.Name}}' | + grep -v '^unless-stopped$' || true +)" +[[ -z "$bad" ]] || { echo "ERROR: k3d debe usar unless-stopped." >&2; exit 1; } + +echo +echo "== Runner ==" +docker inspect "$RUNNER_NAME" \ + --format '{{.Name}} restart={{.HostConfig.RestartPolicy.Name}} status={{.State.Status}}' + +[[ "$(docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' "$RUNNER_NAME")" == "unless-stopped" ]] +[[ "$(docker inspect -f '{{.State.Running}}' "$RUNNER_NAME")" == "true" ]] + +echo +echo "== Docker independiente ==" +test ! -f /etc/systemd/system/docker.service.d/10-k3d-storage.conf +echo "OK" + +echo +echo "== Timer ==" +systemctl is-enabled k3d-lab-ensure.timer +systemctl is-active k3d-lab-ensure.timer +systemctl cat k3d-lab-ensure.timer | grep -E 'OnBootSec=5min|Persistent=false' + +echo +echo "== Kubernetes ==" +k3d cluster list +kubectl get nodes + +ready="$( + kubectl get nodes \ + -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}' | + grep -c '^True$' || true +)" +[[ "$ready" -eq 3 ]] || { echo "ERROR: nodos Ready=${ready}, esperado=3." >&2; exit 1; } + +echo +echo "OK: listo para reinicio con orden estable."