Add logging, dialog UI, README updates and initial release

This commit is contained in:
Ed Nielsen 2026-03-18 19:25:01 +01:00
parent 15d3f0b2d6
commit ab945b1caf
4 changed files with 339 additions and 118 deletions

View file

@ -1,127 +1,79 @@
#!/usr/bin/env bash
CONFIG_FILE="/etc/update-manager.conf"
[[ -f "$CONFIG_FILE" ]] || CONFIG_FILE="./update-manager.conf"
UPDATE_MANAGER_SCRIPT="$HOME/update-manager/update-manager.sh"
HOSTS_FILE="/opt/update-manager/hosts.conf"
if [[ -f "$CONFIG_FILE" ]]; then
# shellcheck disable=SC1090
source "$CONFIG_FILE"
fi
get_log_file() {
local primary="/opt/update-manager/log/update-manager.log"
local fallback="$HOME/update-manager/log/update-manager.log"
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"
if [[ -f "$primary" ]]; then
echo "$primary"
else
echo "$DEFAULT_HOSTS_FILE"
echo "$fallback"
fi
}
usage() {
cat <<USAGE
Usage:
$0 check [hosts_file]
while true; do
choice=$(dialog --clear \
--backtitle "Update Manager" \
--title "Choose an action" \
--menu "Select option:" 15 50 10 \
1 "Check all hosts" \
2 "View hosts file" \
3 "Edit hosts file" \
4 "Add host" \
5 "Remove host" \
6 "View log" \
7 "Follow log live" \
8 "Show log location" \
0 "Exit" \
3>&1 1>&2 2>&3)
Examples:
$0 check
$0 check /path/to/hosts.conf
HOSTS_FILE=/path/to/hosts.conf $0 check
USAGE
}
clear
########################################
# Check single host
########################################
check_host() {
local name="$1"
local ip="$2"
local user="$3"
local result
local rc
result=$(ssh -n $SSH_OPTIONS "${user}@${ip}" \
"apt list --upgradable 2>/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
case $choice in
1)
bash "$UPDATE_MANAGER_SCRIPT" check
read -rp "Press Enter to continue..."
;;
2)
less "$HOSTS_FILE"
;;
3)
nano "$HOSTS_FILE"
;;
4)
read -rp "Name: " name
read -rp "IP: " ip
read -rp "User: " user
echo "$name $ip $user" >> "$HOSTS_FILE"
echo "Host added."
read -rp "Press Enter to continue..."
;;
5)
nl -w2 -s'. ' "$HOSTS_FILE"
read -rp "Line to remove: " line
sed -i "${line}d" "$HOSTS_FILE"
echo "Host removed."
read -rp "Press Enter to continue..."
;;
6)
less "$(get_log_file)"
;;
7)
echo "Press Ctrl+C to stop"
tail -f "$(get_log_file)"
read -rp "Press Enter to continue..."
;;
8)
echo "Log file:"
echo "$(get_log_file)"
read -rp "Press Enter to continue..."
;;
0)
clear
exit 0
;;
esac
done