Clean config handling and use example files

This commit is contained in:
Ed Nielsen 2026-03-18 13:47:57 +01:00
parent df40fa474b
commit 322bbb90ea
2 changed files with 72 additions and 87 deletions

View file

@ -1,2 +1,2 @@
HOSTS_FILE="/opt/update-manager/hosts.conf" HOSTS_FILE="/opt/update-manager/hosts.conf"
NTFY_URL="" SSH_OPTIONS="-o BatchMode=yes -o ConnectTimeout=5"

View file

@ -1,8 +1,11 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#!/usr/bin/env bash ########################################
# Load config
########################################
CONFIG_FILE="/etc/update-manager.conf" CONFIG_FILE="/etc/update-manager.conf"
[[ -f "$CONFIG_FILE" ]] || CONFIG_FILE="./update-manager.conf"
if [[ -f "$CONFIG_FILE" ]]; then if [[ -f "$CONFIG_FILE" ]]; then
# shellcheck disable=SC1090 # shellcheck disable=SC1090
@ -10,104 +13,86 @@ if [[ -f "$CONFIG_FILE" ]]; then
fi fi
HOSTS_FILE="${HOSTS_FILE:-./hosts.conf}" HOSTS_FILE="${HOSTS_FILE:-./hosts.conf}"
NTFY_URL="${NTFY_URL:-}" SSH_OPTIONS="${SSH_OPTIONS:--o BatchMode=yes -o ConnectTimeout=5}"
########################################
# Check hosts
########################################
check_host() { check_host() {
local name="$1" local name="$1"
local ip="$2" local ip="$2"
local user="$3" local user="$3"
local result local result count
echo "===== $name ($ip) =====" result=$(ssh $SSH_OPTIONS "$user@$ip" \
"apt list --upgradable 2>/dev/null | grep -v '^Listing'" 2>/dev/null)
result=$(ssh -n -o BatchMode=yes -o ConnectTimeout=5 "${user}@${ip}" \ if [[ $? -ne 0 ]]; then
"apt-get -s upgrade 2>/dev/null | grep '^Inst'") printf "%-15s ❌ connection failed\n" "$name"
return
fi
if [[ -z "$result" ]]; then if [[ -z "$result" ]]; then
echo "[OK] Up-to-date" printf "%-15s 🟢 up-to-date\n" "$name"
else else
echo "[UPDATES]" count=$(echo "$result" | wc -l)
echo "$result" printf "%-15s 🔴 %d updates\n" "$name" "$count"
fi fi
echo
} }
update_host() { check_all() {
local name="$1" while read -r name ip user; do
local ip="$2" [[ -z "$name" ]] && continue
local user="$3" [[ "$name" =~ ^# ]] && continue
echo "===== $name ($ip) ====="
ssh -n -o BatchMode=yes -o ConnectTimeout=5 "${user}@${ip}" \
"sudo -n apt-get update && sudo -n apt-get upgrade -y" || {
echo "[FEJL] kunne ikke opdatere"
}
echo
}
autoremove_host() {
local name="$1"
local ip="$2"
local user="$3"
echo "===== $name ($ip) ====="
ssh -n -o BatchMode=yes -o ConnectTimeout=5 "${user}@${ip}" \
"sudo -n apt-get autoremove -y" || {
echo "[FEJL] kunne ikke køre autoremove"
}
echo
}
full_host() {
local name="$1"
local ip="$2"
local user="$3"
echo "===== $name ($ip) ====="
ssh -n -o BatchMode=yes -o ConnectTimeout=5 "${user}@${ip}" \
"sudo -n apt-get update && sudo -n apt-get upgrade -y && sudo -n apt-get autoremove -y" || {
echo "[FEJL] kunne ikke køre full maintenance"
}
echo
}
run_action() {
local action="$1"
if [[ ! -f "$HOSTS_FILE" ]]; then
echo "FEJL: hosts-fil findes ikke: $HOSTS_FILE"
exit 1
fi
while IFS='|' read -r name ip user || [[ -n "$name" ]]; do
[[ -z "$name" || "$name" =~ ^# ]] && continue
case "$action" in
check)
check_host "$name" "$ip" "$user" check_host "$name" "$ip" "$user"
;;
update)
update_host "$name" "$ip" "$user"
;;
autoremove)
autoremove_host "$name" "$ip" "$user"
;;
full)
full_host "$name" "$ip" "$user"
;;
*)
echo "Brug: $0 {check|update|autoremove|full}"
exit 1
;;
esac
done < "$HOSTS_FILE" done < "$HOSTS_FILE"
} }
run_action "$1" ########################################
# Install
########################################
install_update_manager() {
echo "Installing update-manager..."
local INSTALL_DIR="/opt/update-manager"
# Opret mappe
sudo mkdir -p "$INSTALL_DIR"
# Kopiér filer
sudo cp update-manager.sh "$INSTALL_DIR/"
sudo cp hosts.conf "$INSTALL_DIR/" 2>/dev/null
sudo cp update-manager.conf "$INSTALL_DIR/" 2>/dev/null
# Gør eksekverbar
sudo chmod +x "$INSTALL_DIR/update-manager.sh"
# Global config (kun hvis ikke findes)
if [[ -f update-manager.conf && ! -f /etc/update-manager.conf ]]; then
sudo cp update-manager.conf /etc/update-manager.conf
fi
# Symlink → global kommando
sudo ln -sf "$INSTALL_DIR/update-manager.sh" /usr/local/bin/update-manager
echo "Done!"
echo "Run: update-manager check"
}
########################################
# Main
########################################
case "$1" in
check)
check_all
;;
install)
install_update_manager
;;
*)
echo "Usage: $0 {check|install}"
;;
esac