Add HA watchdog

This commit is contained in:
2026-03-27 20:00:44 -03:00
parent 345f12530b
commit 4a68f00fd7

View File

@@ -9,12 +9,7 @@ NC='\033[0m'
# Configuration for UI
MAX_LOG_LINES=8
LINE_SERVER=4
LINE_DOCKER=5
LINE_ZSH=6
LINE_TAILSCALE=7
LINE_WIFITUI=8
LINE_HA_WATCHDOG=9
declare -a ACTIVE_STEPS=()
teardown_ui() {
local ROWS
@@ -30,10 +25,13 @@ setup_ui() {
local ROWS
ROWS=$(tput lines 2>/dev/null || echo 24)
LOG_END=$ROWS
LOG_START=$(( LOG_END - MAX_LOG_LINES + 1 ))
local num_steps=${#ACTIVE_STEPS[@]}
local LOG_END=$ROWS
local LOG_START=$(( LOG_END - MAX_LOG_LINES + 1 ))
local track_end=$(( 3 + num_steps ))
local MIN_LOG_START=$(( track_end + 3 ))
local MIN_LOG_START=$(( LINE_HA_WATCHDOG + 4 ))
if [ "$LOG_START" -le "$(( MIN_LOG_START - 1 ))" ]; then
LOG_START=$MIN_LOG_START
fi
@@ -45,37 +43,43 @@ setup_ui() {
printf "\033[2;1H${CYAN} HOMELOGIC SETUP SCRIPT ${NC}\n"
printf "\033[3;1H${BLUE}=========================================${NC}\n"
printf "\033[${LINE_SERVER};1H [ ] Server\n"
printf "\033[${LINE_DOCKER};1H [ ] Docker\n"
printf "\033[${LINE_ZSH};1H [ ] ZSH\n"
printf "\033[${LINE_TAILSCALE};1H [ ] Tailscale\n"
printf "\033[${LINE_WIFITUI};1H [ ] Wifitui\n"
printf "\033[${LINE_HA_WATCHDOG};1H [ ] HA Watchdog\n"
for i in "${!ACTIVE_STEPS[@]}"; do
local line=$(( 4 + i ))
printf "\033[${line};1H [ ] ${ACTIVE_STEPS[$i]}\n"
update_step_status "${ACTIVE_STEPS[$i]}" "Pending"
done
printf "\033[${SEP_LINE};1H${YELLOW}--- Verbose Logs ------------------------${NC}\n"
update_status "$LINE_SERVER" "Pending"
update_status "$LINE_DOCKER" "Pending"
update_status "$LINE_ZSH" "Pending"
update_status "$LINE_TAILSCALE" "Pending"
update_status "$LINE_WIFITUI" "Pending"
update_status "$LINE_HA_WATCHDOG" "Pending"
printf "\033[${LOG_START};${LOG_END}r"
printf "\033[${LOG_END};1H"
}
update_status() {
local line="$1"
get_line_for_step() {
for i in "${!ACTIVE_STEPS[@]}"; do
if [ "${ACTIVE_STEPS[$i]}" = "$1" ]; then
echo $(( 4 + i ))
return
fi
done
echo 0
}
update_step_status() {
local step_name="$1"
local status="$2"
local color="${NC}"
case "$status" in
Pending) color="${NC}" ;;
Running) color="${BLUE}" ;;
Installed) color="${GREEN}" ;;
Skipped|Removed) color="${YELLOW}" ;;
esac
printf "\0337\033[%d;4H${color}%-10s${NC}\0338" "$line" "$status"
local line=$(get_line_for_step "$step_name")
if [ "$line" -gt 0 ]; then
local color="${NC}"
case "$status" in
Pending) color="${NC}" ;;
Running) color="${BLUE}" ;;
Installed) color="${GREEN}" ;;
Skipped|Removed) color="${YELLOW}" ;;
esac
printf "\0337\033[%d;4H${color}%-10s${NC}\0338" "$line" "$status"
fi
}
print_step() {
@@ -83,32 +87,31 @@ print_step() {
}
print_info() {
local IL=${INFO_LINE:-$(( LINE_HA_WATCHDOG + 2 ))}
local IL=${INFO_LINE:-10}
printf "\0337\033[${IL};1H\033[K${CYAN} ➜ %s${NC}\0338" "$1"
printf "${CYAN}➜ %s${NC}\n" "$1"
}
ask_optional_step() {
local line="$1"
local step_name="$2"
local callback="$3"
local step_name="$1"
local callback="$2"
printf "\n${YELLOW}❓ Do you want to install and setup %s? [Y/n] ${NC}" "$step_name"
read choice
case "$choice" in
n|N|no|No|NO )
print_info "Skipping $step_name setup."
update_status "$line" "Skipped"
update_step_status "$step_name" "Skipped"
;;
* )
update_status "$line" "Running"
$callback "$line"
update_step_status "$step_name" "Running"
$callback
;;
esac
}
setup_server() {
update_status "$LINE_SERVER" "Running"
update_step_status "Server" "Running"
print_step "Setting up Server"
print_info "Updating packages and configuring firewall..."
sudo apt-get update
@@ -122,11 +125,11 @@ setup_server() {
sudo ufw allow 3001/tcp #zigbee2mqtt
sudo ufw allow 1883/tcp #mosquitto
sudo ufw allow 2001/tcp #arcane
update_status "$LINE_SERVER" "Installed"
update_step_status "Server" "Installed"
}
setup_docker() {
update_status "$LINE_DOCKER" "Running"
update_step_status "Docker" "Running"
print_step "Setting up Docker"
print_info "Installing prerequisites..."
sudo apt -y install lsb-release gnupg apt-transport-https ca-certificates curl software-properties-common
@@ -149,11 +152,11 @@ setup_docker() {
sudo usermod -aG docker "$USER"
fi
sudo docker network create homelogic 2>/dev/null || true
update_status "$LINE_DOCKER" "Installed"
update_step_status "Docker" "Installed"
}
setup_zsh() {
update_status "$LINE_ZSH" "Running"
update_step_status "ZSH" "Running"
print_step "Setting up ZSH"
print_info "Configuring .zshrc and hushlogin..."
touch ../.hushlogin
@@ -169,11 +172,10 @@ setup_zsh() {
print_info "Installing Oh My Zsh and plugins..."
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions 2>/dev/null || true
RUNZSH=no CHSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
update_status "$LINE_ZSH" "Installed"
update_step_status "ZSH" "Installed"
}
setup_tailscale() {
local line="$1"
print_step "Setting up Tailscale"
if ! command -v tailscale > /dev/null 2>&1; then
print_info "Installing Tailscale..."
@@ -182,11 +184,10 @@ setup_tailscale() {
else
print_info "Tailscale is already installed."
fi
update_status "$line" "Installed"
update_step_status "Tailscale" "Installed"
}
setup_wifitui() {
local line="$1"
print_step "Setting up Wifitui"
print_info "Installing Network Manager..."
sudo apt-get install network-manager -y
@@ -197,11 +198,10 @@ setup_wifitui() {
curl -Lfs "${LATEST_RELEASE}.deb" -o /tmp/wifitui.deb
sudo apt install -y /tmp/wifitui.deb
rm -f /tmp/wifitui.deb
update_status "$line" "Installed"
update_step_status "Wifitui" "Installed"
}
setup_ha_watchdog() {
local line="$1"
local watchdog_path="$(cd "$(dirname "$0")" && pwd)/ha_watchdog.sh"
print_step "Setting up HA Watchdog"
@@ -209,7 +209,7 @@ setup_ha_watchdog() {
print_info "Uninstalling HA Watchdog..."
sudo rm -f "/etc/cron.d/ha_watchdog"
sudo rm -f "/usr/local/bin/ha_watchdog.sh" 2>/dev/null || true
update_status "$line" "Removed"
update_step_status "HA Watchdog" "Removed"
else
print_info "Configuring HA Watchdog script permissions..."
chmod +x "$watchdog_path"
@@ -217,7 +217,7 @@ setup_ha_watchdog() {
print_info "Creating cron job (every 5 minutes)..."
echo "*/5 * * * * root $watchdog_path" | sudo tee /etc/cron.d/ha_watchdog > /dev/null
update_status "$line" "Installed"
update_step_status "HA Watchdog" "Installed"
fi
}
@@ -230,18 +230,42 @@ main() {
printf " 2) Run Main Steps Only\n"
printf " 3) Run Optional Steps Only\n"
printf " 4) Install/Uninstall HA Watchdog\n"
printf " 0) Exit\n"
printf " q) Exit\n"
printf "${BLUE}=========================================${NC}\n"
printf "${YELLOW}Select an option [0-4]: ${NC}"
printf "${YELLOW}Select an option [1-4 or q]: ${NC}"
read opt
case "$opt" in
0|exit|quit|q|Q)
exit|quit|q|Q|0)
printf "\nExiting setup.\n"
exit 0
;;
1|2|3|4)
1)
ACTIVE_STEPS=("Server" "Docker" "ZSH" "Tailscale" "Wifitui")
setup_ui
setup_server
setup_docker
setup_zsh
ask_optional_step "Tailscale" setup_tailscale
ask_optional_step "Wifitui" setup_wifitui
;;
2)
ACTIVE_STEPS=("Server" "Docker" "ZSH")
setup_ui
setup_server
setup_docker
setup_zsh
;;
3)
ACTIVE_STEPS=("Tailscale" "Wifitui")
setup_ui
ask_optional_step "Tailscale" setup_tailscale
ask_optional_step "Wifitui" setup_wifitui
;;
4)
ACTIVE_STEPS=("HA Watchdog")
setup_ui
setup_ha_watchdog
;;
*)
printf "\n${YELLOW}⚠ Invalid option selected.${NC}\n\n"
@@ -251,41 +275,6 @@ main() {
;;
esac
case "$opt" in
1)
update_status "$LINE_HA_WATCHDOG" "Skipped"
setup_server
setup_docker
setup_zsh
ask_optional_step "$LINE_TAILSCALE" "Tailscale" setup_tailscale
ask_optional_step "$LINE_WIFITUI" "Wifitui" setup_wifitui
;;
2)
update_status "$LINE_TAILSCALE" "Skipped"
update_status "$LINE_WIFITUI" "Skipped"
update_status "$LINE_HA_WATCHDOG" "Skipped"
setup_server
setup_docker
setup_zsh
;;
3)
update_status "$LINE_SERVER" "Skipped"
update_status "$LINE_DOCKER" "Skipped"
update_status "$LINE_ZSH" "Skipped"
update_status "$LINE_HA_WATCHDOG" "Skipped"
ask_optional_step "$LINE_TAILSCALE" "Tailscale" setup_tailscale
ask_optional_step "$LINE_WIFITUI" "Wifitui" setup_wifitui
;;
4)
update_status "$LINE_SERVER" "Skipped"
update_status "$LINE_DOCKER" "Skipped"
update_status "$LINE_ZSH" "Skipped"
update_status "$LINE_TAILSCALE" "Skipped"
update_status "$LINE_WIFITUI" "Skipped"
setup_ha_watchdog "$LINE_HA_WATCHDOG"
;;
esac
teardown_ui
printf "\n${GREEN}✔ Task complete! Please log out and log back in for shell changes to take effect.${NC}\n"
}