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