2026-05-07 21:16:03 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# deployment-node.sh - To be run on the execution node (SOLARIA, PIHA, VPS)
|
|
|
|
|
# This script pulls the latest changes and ensures services are running.
|
|
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
# Configuration
|
|
|
|
|
REPO_PATH="${HOME}/homelab-codex-ws"
|
|
|
|
|
RUNTIME_PATH="/opt/homelab"
|
|
|
|
|
HOSTNAME=$(hostname | tr '[:lower:]' '[:upper:]')
|
2026-06-24 22:24:40 +02:00
|
|
|
CURRENT_OS_HOST=$(hostname)
|
2026-05-07 21:16:03 +02:00
|
|
|
|
|
|
|
|
echo "--- Starting Deployment on ${HOSTNAME} ---"
|
|
|
|
|
|
|
|
|
|
# 1. Update Repository
|
|
|
|
|
if [ ! -d "$REPO_PATH" ]; then
|
|
|
|
|
echo "Error: Repository not found at $REPO_PATH"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
cd "$REPO_PATH"
|
|
|
|
|
echo "Pulling latest changes..."
|
|
|
|
|
git pull
|
|
|
|
|
|
2026-06-24 22:24:40 +02:00
|
|
|
# Resolve host directory: search hosts/*/host.yaml for os_hostname == $(hostname)
|
|
|
|
|
HOST_LOGICAL=$(python3 -c "
|
|
|
|
|
import yaml, os, sys
|
|
|
|
|
current = '${CURRENT_OS_HOST}'
|
|
|
|
|
hosts_dir = os.path.join('${REPO_PATH}', 'hosts')
|
|
|
|
|
for entry in sorted(os.listdir(hosts_dir)):
|
|
|
|
|
host_yaml = os.path.join(hosts_dir, entry, 'host.yaml')
|
|
|
|
|
if not os.path.isfile(host_yaml):
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
with open(host_yaml) as f:
|
|
|
|
|
data = yaml.safe_load(f)
|
|
|
|
|
if data and data.get('os_hostname') == current:
|
|
|
|
|
print(entry)
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
")
|
|
|
|
|
|
|
|
|
|
if [ -n "$HOST_LOGICAL" ]; then
|
|
|
|
|
HOST_DIR="${REPO_PATH}/hosts/${HOST_LOGICAL}"
|
|
|
|
|
else
|
|
|
|
|
HOST_LOGICAL_FB="$(echo "$CURRENT_OS_HOST" | tr '[:upper:]' '[:lower:]')"
|
|
|
|
|
echo "WARN: no os_hostname match for ${CURRENT_OS_HOST}, falling back to lowercase hostname dir" >&2
|
|
|
|
|
HOST_DIR="${REPO_PATH}/hosts/${HOST_LOGICAL_FB}"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ ! -d "$HOST_DIR" ]; then
|
|
|
|
|
echo "Error: No host directory found for ${CURRENT_OS_HOST} (tried ${HOST_DIR})" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2026-05-07 21:16:03 +02:00
|
|
|
# 2. Identify Services
|
2026-05-20 14:50:01 +02:00
|
|
|
SERVICES=()
|
|
|
|
|
if [ -f "${HOST_DIR}/services.txt" ]; then
|
|
|
|
|
mapfile -t SERVICES < <(grep -v '^\s*#' "${HOST_DIR}/services.txt" | grep -v '^\s*$')
|
|
|
|
|
elif [ -f "${HOST_DIR}/services.yaml" ]; then
|
|
|
|
|
SERVICES=($(python3 -c "
|
|
|
|
|
import yaml, sys
|
|
|
|
|
try:
|
|
|
|
|
with open('${HOST_DIR}/services.yaml', 'r') as f:
|
|
|
|
|
data = yaml.safe_load(f)
|
|
|
|
|
if data and 'services' in data:
|
|
|
|
|
if isinstance(data['services'], dict):
|
|
|
|
|
print(' '.join(data['services'].keys()))
|
|
|
|
|
elif isinstance(data['services'], list):
|
|
|
|
|
print(' '.join(data['services']))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f'Error parsing YAML: {e}', file=sys.stderr)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
"))
|
|
|
|
|
fi
|
2026-05-07 21:16:03 +02:00
|
|
|
|
2026-05-20 14:50:01 +02:00
|
|
|
if [ ${#SERVICES[@]} -eq 0 ]; then
|
|
|
|
|
echo "No services found for ${HOSTNAME}. Skipping service deployment."
|
2026-05-07 21:16:03 +02:00
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 3. Deploy Services
|
2026-05-20 14:50:01 +02:00
|
|
|
for service in "${SERVICES[@]}"; do
|
2026-05-07 21:16:03 +02:00
|
|
|
echo "Deploying service: ${service}..."
|
2026-05-20 14:50:01 +02:00
|
|
|
|
2026-05-07 21:16:03 +02:00
|
|
|
COMPOSE_FILE="${REPO_PATH}/services/${service}/docker-compose.yml"
|
2026-05-20 14:50:01 +02:00
|
|
|
|
2026-05-07 21:16:03 +02:00
|
|
|
if [ ! -f "$COMPOSE_FILE" ]; then
|
|
|
|
|
echo "Warning: Compose file not found for ${service} at ${COMPOSE_FILE}"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
TARGET_DIR="${RUNTIME_PATH}/services/${service}"
|
|
|
|
|
mkdir -p "$TARGET_DIR"
|
|
|
|
|
|
2026-05-20 14:50:01 +02:00
|
|
|
OVERRIDE_FILE="${HOST_DIR}/runtime/${service}/docker-compose.override.yml"
|
|
|
|
|
|
2026-05-07 21:16:03 +02:00
|
|
|
COMPOSE_CMD="docker compose -f ${COMPOSE_FILE}"
|
|
|
|
|
if [ -f "$OVERRIDE_FILE" ]; then
|
|
|
|
|
echo "Using override file for ${service}"
|
|
|
|
|
COMPOSE_CMD="${COMPOSE_CMD} -f ${OVERRIDE_FILE}"
|
|
|
|
|
fi
|
|
|
|
|
|
2026-06-25 13:41:55 +02:00
|
|
|
ENV_FILE="${REPO_PATH}/services/${service}/.env"
|
|
|
|
|
if [ -f "$ENV_FILE" ]; then
|
|
|
|
|
COMPOSE_CMD="${COMPOSE_CMD} --env-file ${ENV_FILE}"
|
|
|
|
|
fi
|
|
|
|
|
|
2026-05-07 21:16:03 +02:00
|
|
|
$COMPOSE_CMD up -d --remove-orphans
|
2026-05-20 14:50:01 +02:00
|
|
|
done
|
2026-05-07 21:16:03 +02:00
|
|
|
|
|
|
|
|
echo "--- Deployment Complete ---"
|