From a6e654822c06efad269dfc76861c820304c5867c Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Wed, 7 Oct 2026 13:42:35 -0600 Subject: [PATCH] fix: make RPi services deployment idempotent --- infra/rpi-services/bootstrap.sh | 68 +++++++++++++++++++++----- infra/rpi-services/seed-uptime-kuma.js | 22 +++++++-- 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/infra/rpi-services/bootstrap.sh b/infra/rpi-services/bootstrap.sh index f7f1b2d..28b6e5e 100755 --- a/infra/rpi-services/bootstrap.sh +++ b/infra/rpi-services/bootstrap.sh @@ -131,8 +131,10 @@ PY } write_env_file() { + local env_file PIHOLE_WEBPASSWORD="$(resolve_pihole_webpassword)" - sudo tee "${INSTALL_DIR}/.env" >/dev/null <"${env_file}" <"${dnsmasq_file}" <<'PY' @@ -253,6 +271,14 @@ for line in useful_lines(static_dhcp_hosts_file): raise SystemExit(f"Invalid static DHCP entry: {line}") print(line) PY + existing_file="$(mktemp)" + if sudo docker exec "${pihole_container}" cat /etc/dnsmasq.d/02-homelab-managed-dns.conf >"${existing_file}" 2>/dev/null && + cmp -s "${dnsmasq_file}" "${existing_file}"; then + echo "Pi-hole managed DNS unchanged; skipping Pi-hole restart." + rm -f "${dnsmasq_file}" "${existing_file}" + return 0 + fi + rm -f "${existing_file}" sudo docker cp "${dnsmasq_file}" "${pihole_container}:/etc/dnsmasq.d/02-homelab-managed-dns.conf" rm -f "${dnsmasq_file}" sudo docker restart "${pihole_container}" >/dev/null @@ -263,6 +289,7 @@ seed_uptime_kuma_monitors() { local monitor_file="${INSTALL_DIR}/uptime-kuma-monitors.json" local seed_script="${INSTALL_DIR}/seed-uptime-kuma.js" local elapsed=0 + local seed_output if [[ ! -s "${monitor_file}" || ! -s "${seed_script}" ]]; then echo "Uptime Kuma monitor seed files are missing; skipping monitor seed." @@ -280,9 +307,15 @@ seed_uptime_kuma_monitors() { sudo docker cp "${monitor_file}" "${uptime_kuma_container}:/tmp/homelab-kuma-monitors.json" sudo docker cp "${seed_script}" "${uptime_kuma_container}:/tmp/homelab-seed-kuma.js" - if sudo docker exec -w /app "${uptime_kuma_container}" node /tmp/homelab-seed-kuma.js /tmp/homelab-kuma-monitors.json; then - sudo docker restart "${uptime_kuma_container}" >/dev/null + if seed_output="$(sudo docker exec -w /app "${uptime_kuma_container}" node /tmp/homelab-seed-kuma.js /tmp/homelab-kuma-monitors.json)"; then + printf '%s\n' "${seed_output}" + if grep -q '^changed=true$' <<<"${seed_output}"; then + sudo docker restart "${uptime_kuma_container}" >/dev/null + else + echo "Uptime Kuma monitors unchanged; skipping Uptime Kuma restart." + fi else + printf '%s\n' "${seed_output:-}" >&2 echo "Uptime Kuma monitor seed failed; leaving the running service unchanged." >&2 fi } @@ -306,19 +339,32 @@ if ! sudo docker compose version >/dev/null 2>&1; then fi sudo mkdir -p "${INSTALL_DIR}" "${DATA_DIR}/pihole/etc-pihole" "${DATA_DIR}/pihole/etc-dnsmasq.d" "${DATA_DIR}/uptime-kuma" -sudo cp "${SCRIPT_DIR}/docker-compose.yml" "${INSTALL_DIR}/docker-compose.yml" -sudo cp "${SCRIPT_DIR}/adlists.txt" "${INSTALL_DIR}/adlists.txt" -sudo cp "${SCRIPT_DIR}/local-dns-records.txt" "${INSTALL_DIR}/local-dns-records.txt" -sudo cp "${SCRIPT_DIR}/cname-records.txt" "${INSTALL_DIR}/cname-records.txt" -sudo cp "${SCRIPT_DIR}/static-dhcp-hosts.txt" "${INSTALL_DIR}/static-dhcp-hosts.txt" -sudo cp "${SCRIPT_DIR}/uptime-kuma-monitors.json" "${INSTALL_DIR}/uptime-kuma-monitors.json" -sudo cp "${SCRIPT_DIR}/seed-uptime-kuma.js" "${INSTALL_DIR}/seed-uptime-kuma.js" +install_if_changed "${SCRIPT_DIR}/docker-compose.yml" "${INSTALL_DIR}/docker-compose.yml" 0644 || true +install_if_changed "${SCRIPT_DIR}/adlists.txt" "${INSTALL_DIR}/adlists.txt" 0644 || true +install_if_changed "${SCRIPT_DIR}/local-dns-records.txt" "${INSTALL_DIR}/local-dns-records.txt" 0644 || true +install_if_changed "${SCRIPT_DIR}/cname-records.txt" "${INSTALL_DIR}/cname-records.txt" 0644 || true +install_if_changed "${SCRIPT_DIR}/static-dhcp-hosts.txt" "${INSTALL_DIR}/static-dhcp-hosts.txt" 0644 || true +install_if_changed "${SCRIPT_DIR}/uptime-kuma-monitors.json" "${INSTALL_DIR}/uptime-kuma-monitors.json" 0644 || true +install_if_changed "${SCRIPT_DIR}/seed-uptime-kuma.js" "${INSTALL_DIR}/seed-uptime-kuma.js" 0644 || true write_env_file stop_legacy_pihole cd "${INSTALL_DIR}" -sudo docker compose pull +case "${LAB_RPI_SERVICES_PULL_POLICY:-missing}" in + always) + sudo docker compose pull + ;; + missing) + sudo docker compose pull --policy missing || echo "docker compose pull --policy missing is unsupported; docker compose up will pull missing images." + ;; + never|false|off|disabled) + ;; + *) + echo "LAB_RPI_SERVICES_PULL_POLICY must be always, missing, or never." >&2 + exit 1 + ;; +esac sudo docker compose up -d --remove-orphans sync_pihole_adlists sync_pihole_managed_dns diff --git a/infra/rpi-services/seed-uptime-kuma.js b/infra/rpi-services/seed-uptime-kuma.js index 0dbf944..fca4135 100755 --- a/infra/rpi-services/seed-uptime-kuma.js +++ b/infra/rpi-services/seed-uptime-kuma.js @@ -160,19 +160,32 @@ function monitorValues(monitor, columns, userId, includeCreatedDate) { function upsertMonitor(database, monitor, columns, userId) { const existing = database - .prepare("select id from monitor where user_id = ? and name = ? limit 1") + .prepare("select * from monitor where user_id = ? and name = ? limit 1") .get(userId, monitor.name); if (existing) { const values = monitorValues(monitor, columns, userId, false); delete values.name; delete values.user_id; + delete values.modified_date; + const changed = Object.entries(values).some(([key, value]) => { + const existingValue = existing[key]; + if (existingValue === null || existingValue === undefined) { + return value !== null && value !== undefined && value !== ""; + } + return String(existingValue) !== String(value); + }); + if (!changed) { + console.log(`unchanged ${monitor.name}`); + return false; + } + values.modified_date = nowSql(); const assignments = Object.keys(values).map((key) => `"${key}" = @${key}`); database .prepare(`update monitor set ${assignments.join(", ")} where id = @id`) .run({ ...values, id: existing.id }); console.log(`updated ${monitor.name}`); - return; + return true; } const values = monitorValues(monitor, columns, userId, true); @@ -185,6 +198,7 @@ function upsertMonitor(database, monitor, columns, userId) { ) .run(values); console.log(`created ${monitor.name}`); + return true; } function main() { @@ -218,9 +232,11 @@ function main() { } const transaction = database.transaction(() => { + let changed = false; for (const monitor of monitors) { - upsertMonitor(database, monitor, columns, user.id); + changed = upsertMonitor(database, monitor, columns, user.id) || changed; } + console.log(changed ? "changed=true" : "changed=false"); }); transaction(); }