From bc43a7c66c60b6b8305e6a5f55c41cdcd82ceb53 Mon Sep 17 00:00:00 2001 From: Ed Nielsen Date: Wed, 18 Mar 2026 16:47:56 +0100 Subject: [PATCH] Add main script (NodeFox) --- update-manager.sh | 127 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100755 update-manager.sh diff --git a/update-manager.sh b/update-manager.sh new file mode 100755 index 0000000..89b5ef9 --- /dev/null +++ b/update-manager.sh @@ -0,0 +1,127 @@ +#!/usr/bin/env bash + +CONFIG_FILE="/etc/update-manager.conf" +[[ -f "$CONFIG_FILE" ]] || CONFIG_FILE="./update-manager.conf" + +if [[ -f "$CONFIG_FILE" ]]; then + # shellcheck disable=SC1090 + source "$CONFIG_FILE" +fi + +DEFAULT_HOSTS_FILE="${HOSTS_FILE:-/opt/update-manager/hosts.conf}" +SSH_OPTIONS="${SSH_OPTIONS:--o BatchMode=yes -o ConnectTimeout=5}" + +######################################## +# Ensure hosts file exists +######################################## + +ensure_hosts_file() { + if [[ ! -f "$DEFAULT_HOSTS_FILE" ]]; then + echo "Creating example hosts file: $DEFAULT_HOSTS_FILE" + + mkdir -p "$(dirname "$DEFAULT_HOSTS_FILE")" + + cat > "$DEFAULT_HOSTS_FILE" <<'EOF' +# Example hosts file +# Format: +# name ip user +# +# server1 192.168.1.10 user +# server2 192.168.1.20 user +# server3 10.0.0.5 root +EOF + fi +} + +######################################## +# Helpers +######################################## + +get_hosts_file() { + if [[ -n "$2" ]]; then + echo "$2" + else + echo "$DEFAULT_HOSTS_FILE" + fi +} + +usage() { + cat </dev/null | sed '/^Listing/d'" 2>&1) + rc=$? + + printf "===== %s (%s) =====\n" "$name" "$ip" + + if [[ $rc -ne 0 ]]; then + echo "❌ connection failed" + echo "$result" + echo + return + fi + + if [[ -z "$result" ]]; then + echo "Up-to-date" + else + echo "$result" + fi + + echo +} + +######################################## +# Check all hosts +######################################## + +check_all() { + local hosts_file="$1" + + ensure_hosts_file + + while IFS= read -r line; do + [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue + + local name ip user + read -r name ip user <<< "$line" + + [[ -z "$name" || -z "$ip" || -z "$user" ]] && continue + + check_host "$name" "$ip" "$user" + done < "$hosts_file" +} + +######################################## +# Main +######################################## + +case "${1:-}" in + check) + HOSTS_FILE_TO_USE="$(get_hosts_file "$@")" + check_all "$HOSTS_FILE_TO_USE" + ;; + *) + usage + exit 1 + ;; +esac