fix: make RPi services deployment idempotent
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-07 13:42:35 -06:00
parent 3fcb8dff98
commit a6e654822c
2 changed files with 76 additions and 14 deletions

View File

@ -131,8 +131,10 @@ PY
}
write_env_file() {
local env_file
PIHOLE_WEBPASSWORD="$(resolve_pihole_webpassword)"
sudo tee "${INSTALL_DIR}/.env" >/dev/null <<ENV_EOT
env_file="$(mktemp)"
cat >"${env_file}" <<ENV_EOT
TZ=${TZ:-America/Mexico_City}
PIHOLE_IMAGE=${PIHOLE_IMAGE:-pihole/pihole:latest}
PIHOLE_CONTAINER_NAME=${PIHOLE_CONTAINER_NAME:-homelab-pihole}
@ -158,6 +160,21 @@ UPTIME_KUMA_PORT=${UPTIME_KUMA_PORT:-3001}
UPTIME_KUMA_DATA_DIR=${UPTIME_KUMA_DATA_DIR:-${DATA_DIR}/uptime-kuma}
RPI_SERVICES_NETWORK=${RPI_SERVICES_NETWORK:-homelab-rpi-services}
ENV_EOT
install_if_changed "${env_file}" "${INSTALL_DIR}/.env" 0600 || true
}
install_if_changed() {
local source_file="$1"
local target_file="$2"
local mode="${3:-0644}"
if sudo test -f "${target_file}" && sudo cmp -s "${source_file}" "${target_file}"; then
rm -f "${source_file}"
return 1
fi
sudo install -m "${mode}" "${source_file}" "${target_file}"
rm -f "${source_file}"
return 0
}
sync_pihole_adlists() {
@ -215,6 +232,7 @@ sync_pihole_managed_dns() {
local cname_records_file="${INSTALL_DIR}/cname-records.txt"
local static_dhcp_hosts_file="${INSTALL_DIR}/static-dhcp-hosts.txt"
local dnsmasq_file
local existing_file
dnsmasq_file="$(mktemp)"
python3 - "${local_records_file}" "${cname_records_file}" "${static_dhcp_hosts_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

View File

@ -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();
}