#!/usr/bin/env bash # Run as the existing deploy user on workstation. Never resets the working tree. set -euo pipefail here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo="${HOMELAB_REPO:-/srv/homelab}" for tool in python3 git kubectl helm docker flock timeout; do command -v "$tool" >/dev/null || { echo "Install missing dependency: $tool" >&2; exit 1; } done [ -d "$repo/.git" ] || { echo "Missing deploy checkout: $repo" >&2; exit 1; } [[ "$repo" =~ ^/[A-Za-z0-9_./-]+$ ]] || { echo 'Deploy path must be absolute and contain no whitespace' >&2; exit 1; } if [ "$(loginctl show-user "$USER" -p Linger --value)" != yes ]; then echo "Run once: sudo loginctl enable-linger $USER" >&2 exit 1 fi config="${XDG_CONFIG_HOME:-$HOME/.config}/homelab-deploy" mkdir -p "$config" "$HOME/.local/lib/homelab-deploy" "$HOME/.config/systemd/user" chmod 700 "$config" if [ ! -f "$config/environment" ]; then context="$(kubectl config current-context)" cluster_uid="$(kubectl get namespace kube-system -o jsonpath='{.metadata.uid}')" printf 'HOMELAB_REPO=%s\nKUBE_CONTEXT=%s\nEXPECTED_CLUSTER_UID=%s\n' "$repo" "$context" "$cluster_uid" >"$config/environment" chmod 600 "$config/environment" fi # Do not replace a dispatcher while an existing deploy uses it. if systemctl --user list-units 'homelab-deploy@*' --state=running --no-legend | grep -q .; then echo 'An existing deploy is running; wait before updating the controller' >&2 exit 1 fi install -m 0755 "$here/../workflows/deploy-controller.py" "$HOME/.local/lib/homelab-deploy/controller.py" install -m 0644 "$here/homelab-deploy@.service" "$HOME/.config/systemd/user/homelab-deploy@.service" systemctl --user daemon-reload echo 'Controller ready. Run a checked main SHA in full mode for the initial baseline.'