feat(rebuild): add informational messages and error handling to rebuild script
This commit is contained in:
parent
d1c62bc67f
commit
caf464c5df
1 changed files with 36 additions and 13 deletions
|
@ -6,6 +6,26 @@ pkgs.writeShellScriptBin "rebuild" # sh
|
|||
TEST_SHELL=false
|
||||
REMOTE_HOSTS=()
|
||||
REBUILDING_ALL=false
|
||||
# ANSI color codes
|
||||
GREEN='\033[0;32m'
|
||||
ORANGE='\033[0;33m'
|
||||
RED='\033[0;31m'
|
||||
NC='\033[0m'
|
||||
|
||||
info() {
|
||||
timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
echo -e "''${GREEN}''${timestamp} INFO: $1''${NC}"
|
||||
}
|
||||
|
||||
warn() {
|
||||
timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
echo -e "''${ORANGE}''${timestamp} WARN: $1''${NC}"
|
||||
}
|
||||
|
||||
err() {
|
||||
timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
echo -e "''${RED}''${timestamp} ERROR: $1''${NC}"
|
||||
}
|
||||
|
||||
prompt() {
|
||||
local PROMPT="$1"
|
||||
|
@ -15,12 +35,12 @@ pkgs.writeShellScriptBin "rebuild" # sh
|
|||
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
|
||||
"$*"
|
||||
else
|
||||
echo "$PROMPT aborted."
|
||||
info "$PROMPT aborted."
|
||||
fi
|
||||
}
|
||||
|
||||
spawn_test_shell() {
|
||||
echo "Spawning test shell on $1..."
|
||||
info "Spawning test shell on $1..."
|
||||
(export PS1="Test shell> "
|
||||
exec ${pkgs.bash}/bin/bash ssh "$1") || {
|
||||
${pkgs.cowsay}/bin/cowsay "You aborted."
|
||||
|
@ -33,28 +53,30 @@ pkgs.writeShellScriptBin "rebuild" # sh
|
|||
local CURRENT_GENERATION=$(ssh "$1" readlink /nix/var/nix/profiles/system | cut -d- -f2)
|
||||
|
||||
if "$TEST_SHELL"; then
|
||||
echo "Testing $1..."
|
||||
info "Testing $1..."
|
||||
nh os test "''${args[@]}"
|
||||
git diff HEAD --color=always --stat --patch
|
||||
spawn_test_shell "$1"
|
||||
echo "Rebuilding $1..."
|
||||
info "Rebuilding $1..."
|
||||
nh os boot "''${args[@]}"
|
||||
else
|
||||
echo "Rebuilding $1 on $HOSTNAME..."
|
||||
info "Rebuilding $1 on $HOSTNAME..."
|
||||
nh os switch "''${args[@]}"
|
||||
fi
|
||||
|
||||
if ! "$NO_GENERATION_CHECK"; then
|
||||
local NEW_GENERATION=$(ssh "$1" readlink /nix/var/nix/profiles/system | cut -d- -f2)
|
||||
echo "$1 - New generation is $NEW_GENERATION. Current is $CURRENT_GENERATION."
|
||||
info "$1 - New generation is $NEW_GENERATION. Current is $CURRENT_GENERATION."
|
||||
if [ ! $NEW_GENERATION -gt $CURRENT_GENERATION ]; then
|
||||
echo "WARNING: New config was not added to bootloader."
|
||||
warn "New config was not added to bootloader."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
info "Starting rebuild script."
|
||||
|
||||
if [ ! -f "flake.nix" ]; then
|
||||
echo "Error: flake.nix not found in the current directory. Exiting."
|
||||
err "flake.nix not found in the current directory. Exiting."
|
||||
exit 1 # Indicate an error
|
||||
fi
|
||||
|
||||
|
@ -76,12 +98,12 @@ pkgs.writeShellScriptBin "rebuild" # sh
|
|||
reachable_hosts=()
|
||||
hostnames=$(nix flake show --all-systems --json | , jq -r '.nixosConfigurations | keys | .[]')
|
||||
for host in ''${hostnames[@]}; do
|
||||
echo "Checking if $host is reachable..."
|
||||
info "Checking if $host is reachable..."
|
||||
if ping -c 1 -W 1 "$host" > /dev/null 2>&1 ; then
|
||||
echo "$host is reachable."
|
||||
info "$host is reachable."
|
||||
reachable_hosts+=("$host")
|
||||
else
|
||||
echo "$host is unreachable."
|
||||
warn "$host is unreachable."
|
||||
fi
|
||||
done
|
||||
REMOTE_HOSTS=(''${reachable_hosts[@]})
|
||||
|
@ -93,7 +115,7 @@ pkgs.writeShellScriptBin "rebuild" # sh
|
|||
if ping -c 1 -W 1 "$1" > /dev/null 2>&1 ; then
|
||||
REMOTE_HOSTS+=("$1")
|
||||
else
|
||||
echo "$1 is unreachable. Exiting."
|
||||
err "$1 is unreachable. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
@ -103,7 +125,7 @@ pkgs.writeShellScriptBin "rebuild" # sh
|
|||
done
|
||||
|
||||
if [ ''${#REMOTE_HOSTS[@]} == 0 ]; then
|
||||
echo "No hostnames provided."
|
||||
info "No hostnames provided."
|
||||
REMOTE_HOSTS=("$HOSTNAME")
|
||||
fi
|
||||
|
||||
|
@ -118,5 +140,6 @@ pkgs.writeShellScriptBin "rebuild" # sh
|
|||
prompt "Reboot system" sudo systemctl reboot
|
||||
fi
|
||||
|
||||
info "Rebuild script completed successfully."
|
||||
exit 0
|
||||
''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue