refactor(scripts): modernize dynu installer for automation and idempotency

Refactor run-once.sh to transition from a manual helper to a reproducible
installation script.

- Replace interactive editor prompts with CLI argument parsing.
- Guarantee idempotency by using  for atomic file placement
  and  for service management.
- Enhance security and reliability through explicit root checks,
  strict mode (set -euo pipefail), and atomic environment file generation.
- Eliminate redundant cleanup logic in favor of overwrite semantics.
This commit is contained in:
kiskaadee committed 2026-04-26 15:46:56 -05:00
1 parent 306db58c32
commit da6cd1c352
1 file changed
+54 -22
+54 -22
View File
@@ -1,31 +1,63 @@
# Bootstrap: this copies the scripts to the correct locations and enables the services #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
# Flow summary: # --- argument parsing ---
# copy files DYNU_HOST=""
# daemon-reload DYNU_USER=""
# edit config DYNU_PASSWORD=""
# enable + start timer
# done
sudo mkdir -p /etc/conf.d while [[ $# -gt 0 ]]; do
sudo install -m 755 ip-monitor.sh /usr/local/bin/ip-monitor.sh case "$1" in
sudo cp dynu.service /etc/systemd/system/dynu.service --host) DYNU_HOST="$2"; shift 2 ;;
sudo cp dynu.timer /etc/systemd/system/dynu.timer --user) DYNU_USER="$2"; shift 2 ;;
sudo cp dynu-environment /etc/conf.d/dynu-environment --password) DYNU_PASSWORD="$2"; shift 2 ;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
sudo systemctl daemon-reload # --- validation ---
: "${DYNU_HOST:?--host required}"
: "${DYNU_USER:?--user required}"
: "${DYNU_PASSWORD:?--password required}"
echo "Configure Dynu credentials..." # --- root check ---
sudo ${EDITOR:-vi} /etc/conf.d/dynu-environment if [[ $EUID -ne 0 ]]; then
echo "Run as root"
exit 1
fi
sudo chmod 600 /etc/conf.d/dynu-environment # --- paths ---
sudo chown root:root /etc/conf.d/dynu-environment BIN_PATH="/usr/local/bin/ip-monitor.sh"
SERVICE_PATH="/etc/systemd/system/dynu.service"
TIMER_PATH="/etc/systemd/system/dynu.timer"
ENV_PATH="/etc/conf.d/dynu-environment"
# --- install files ---
install -m 755 ip-monitor.sh "$BIN_PATH"
install -m 644 dynu.service "$SERVICE_PATH"
install -m 644 dynu.timer "$TIMER_PATH"
sudo systemctl enable dynu.timer # --- write env file atomically ---
sudo systemctl start dynu.timer TMP_ENV=$(mktemp)
cat > "$TMP_ENV" <<EOF
DYNU_HOST=$DYNU_HOST
DYNU_USER=$DYNU_USER
DYNU_PASSWORD=$DYNU_PASSWORD
EOF
install -m 600 "$TMP_ENV" "$ENV_PATH"
rm -f "$TMP_ENV"
# --- systemd reload ---
systemctl daemon-reload
# --- enable + start timer ---
systemctl enable --now dynu.timer
echo "Installation complete"
echo "Check logs with: journalctl -u dynu.service -f"
echo "Done!"
echo "Last IP: "
echo "Use: journalctl -u dynu.service -f"