expanded lab script maybe we need to switvh to smoething other than bash soon
This commit is contained in:
parent
9274ab1e17
commit
4cb3852039
1 changed files with 77 additions and 49 deletions
|
@ -1,57 +1,63 @@
|
||||||
{ lib, stdenv, writeShellScriptBin, rsync, openssh, ... }:
|
{
|
||||||
|
lib,
|
||||||
|
stdenv,
|
||||||
|
writeShellScriptBin,
|
||||||
|
rsync,
|
||||||
|
openssh,
|
||||||
|
...
|
||||||
|
}:
|
||||||
writeShellScriptBin "lab" ''
|
writeShellScriptBin "lab" ''
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# Home-lab administration tools
|
# Home-lab administration tools
|
||||||
# Deploy and manage NixOS configurations across home lab infrastructure
|
# Deploy and manage NixOS configurations across home lab infrastructure
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
HOMELAB_ROOT="/home/geir/Home-lab"
|
HOMELAB_ROOT="/home/geir/Home-lab"
|
||||||
TEMP_CONFIG_DIR="/tmp/home-lab-config"
|
TEMP_CONFIG_DIR="/tmp/home-lab-config"
|
||||||
|
|
||||||
# Color output
|
# Color output
|
||||||
RED='\033[0;31m'
|
RED='\033[0;31m'
|
||||||
GREEN='\033[0;32m'
|
GREEN='\033[0;32m'
|
||||||
YELLOW='\033[1;33m'
|
YELLOW='\033[1;33m'
|
||||||
BLUE='\033[0;34m'
|
BLUE='\033[0;34m'
|
||||||
NC='\033[0m' # No Color
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
echo -e "''${BLUE}[lab]''${NC} $1"
|
echo -e "''${BLUE}[lab]''${NC} $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
success() {
|
success() {
|
||||||
echo -e "''${GREEN}[lab]''${NC} $1"
|
echo -e "''${GREEN}[lab]''${NC} $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
warn() {
|
warn() {
|
||||||
echo -e "''${YELLOW}[lab]''${NC} $1"
|
echo -e "''${YELLOW}[lab]''${NC} $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
error() {
|
error() {
|
||||||
echo -e "''${RED}[lab]''${NC} $1" >&2
|
echo -e "''${RED}[lab]''${NC} $1" >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
# Deployment function
|
# Deployment function
|
||||||
deploy_machine() {
|
deploy_machine() {
|
||||||
local machine="$1"
|
local machine="$1"
|
||||||
local mode="''${2:-boot}" # boot, test, or switch
|
local mode="''${2:-boot}" # boot, test, or switch
|
||||||
|
|
||||||
case "$machine" in
|
case "$machine" in
|
||||||
"congenital-optimist")
|
"congenital-optimist")
|
||||||
# Local deployment - no SSH needed
|
# Local deployment - no SSH needed
|
||||||
log "Deploying $machine (mode: $mode) locally"
|
log "Deploying $machine (mode: $mode) locally"
|
||||||
|
|
||||||
# Deploy the configuration locally
|
# Deploy the configuration locally
|
||||||
log "Running nixos-rebuild $mode locally..."
|
log "Running nixos-rebuild $mode locally..."
|
||||||
if ! sudo nixos-rebuild $mode --flake "$HOMELAB_ROOT#$machine"; then
|
if ! sudo nixos-rebuild $mode --flake "$HOMELAB_ROOT#$machine"; then
|
||||||
error "Failed to deploy configuration to $machine"
|
error "Failed to deploy configuration to $machine"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
success "Successfully deployed $machine"
|
success "Successfully deployed $machine"
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
@ -62,7 +68,7 @@ writeShellScriptBin "lab" ''
|
||||||
local target_host="sma@grey-area"
|
local target_host="sma@grey-area"
|
||||||
;;
|
;;
|
||||||
"reverse-proxy")
|
"reverse-proxy")
|
||||||
local target_host="sma@reverse-proxy"
|
local target_host="sma@reverse-proxy.tail807ea.ts.net"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
error "Unknown machine: $machine"
|
error "Unknown machine: $machine"
|
||||||
|
@ -70,34 +76,34 @@ writeShellScriptBin "lab" ''
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
log "Deploying $machine (mode: $mode)"
|
log "Deploying $machine (mode: $mode)"
|
||||||
|
|
||||||
# Sync configuration to target machine
|
# Sync configuration to target machine
|
||||||
log "Syncing configuration to $target_host..."
|
log "Syncing configuration to $target_host..."
|
||||||
if ! ${rsync}/bin/rsync -av --delete "$HOMELAB_ROOT/" "$target_host:$TEMP_CONFIG_DIR/"; then
|
if ! ${rsync}/bin/rsync -av --delete "$HOMELAB_ROOT/" "$target_host:$TEMP_CONFIG_DIR/"; then
|
||||||
error "Failed to sync configuration to $machine"
|
error "Failed to sync configuration to $machine"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Deploy the configuration
|
# Deploy the configuration
|
||||||
log "Running nixos-rebuild $mode on $machine..."
|
log "Running nixos-rebuild $mode on $machine..."
|
||||||
if ! ${openssh}/bin/ssh "$target_host" "cd $TEMP_CONFIG_DIR && sudo nixos-rebuild $mode --flake .#$machine"; then
|
if ! ${openssh}/bin/ssh "$target_host" "cd $TEMP_CONFIG_DIR && sudo nixos-rebuild $mode --flake .#$machine"; then
|
||||||
error "Failed to deploy configuration to $machine"
|
error "Failed to deploy configuration to $machine"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
success "Successfully deployed $machine"
|
success "Successfully deployed $machine"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Update all machines function
|
# Update all machines function
|
||||||
update_all_machines() {
|
update_all_machines() {
|
||||||
local mode="''${1:-boot}" # boot, test, or switch
|
local mode="''${1:-boot}" # boot, test, or switch
|
||||||
local machines=("congenital-optimist" "sleeper-service" "grey-area" "reverse-proxy")
|
local machines=("congenital-optimist" "sleeper-service" "grey-area" "reverse-proxy")
|
||||||
local failed_machines=()
|
local failed_machines=()
|
||||||
|
|
||||||
log "Starting update of all machines (mode: $mode)"
|
log "Starting update of all machines (mode: $mode)"
|
||||||
|
|
||||||
for machine in "''${machines[@]}"; do
|
for machine in "''${machines[@]}"; do
|
||||||
log "Updating $machine..."
|
log "Updating $machine..."
|
||||||
if deploy_machine "$machine" "$mode"; then
|
if deploy_machine "$machine" "$mode"; then
|
||||||
|
@ -108,7 +114,7 @@ writeShellScriptBin "lab" ''
|
||||||
fi
|
fi
|
||||||
echo "" # Add spacing between machines
|
echo "" # Add spacing between machines
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ ''${#failed_machines[@]} -eq 0 ]]; then
|
if [[ ''${#failed_machines[@]} -eq 0 ]]; then
|
||||||
success "All machines updated successfully!"
|
success "All machines updated successfully!"
|
||||||
else
|
else
|
||||||
|
@ -120,45 +126,67 @@ writeShellScriptBin "lab" ''
|
||||||
# Show deployment status
|
# Show deployment status
|
||||||
show_status() {
|
show_status() {
|
||||||
log "Home-lab infrastructure status:"
|
log "Home-lab infrastructure status:"
|
||||||
|
|
||||||
# Check congenital-optimist (local)
|
# Check congenital-optimist (local)
|
||||||
if /run/current-system/sw/bin/systemctl is-active --quiet tailscaled; then
|
if /run/current-system/sw/bin/systemctl is-active --quiet tailscaled; then
|
||||||
success " congenital-optimist: ✓ Online (local)"
|
success " congenital-optimist: ✓ Online (local)"
|
||||||
else
|
else
|
||||||
warn " congenital-optimist: ⚠ Tailscale inactive"
|
warn " congenital-optimist: ⚠ Tailscale inactive"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if -v (verbose) flag is passed
|
# Check if -v (verbose) flag is passed
|
||||||
local verbose=0
|
local verbose=0
|
||||||
if [[ "''${1:-}" == "-v" ]]; then
|
if [[ "''${1:-}" == "-v" ]]; then
|
||||||
verbose=1
|
verbose=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check remote machines
|
# Check remote machines
|
||||||
for machine in sleeper-service grey-area reverse-proxy; do
|
for machine in sleeper-service grey-area reverse-proxy; do
|
||||||
local ssh_user="sma" # Using sma as the admin user for remote machines
|
local ssh_user="sma" # Using sma as the admin user for remote machines
|
||||||
|
|
||||||
# Test SSH connectivity with debug info if in verbose mode
|
# Test SSH connectivity with debug info if in verbose mode
|
||||||
if [[ $verbose -eq 1 ]]; then
|
if [[ $verbose -eq 1 ]]; then
|
||||||
log "Testing SSH connection to $machine (LAN)..."
|
log "Testing SSH connection to $machine (LAN)..."
|
||||||
${openssh}/bin/ssh -v -o ConnectTimeout=5 -o BatchMode=yes "$ssh_user@$machine" "echo ✓ SSH connection to $machine successful" 2>&1 | grep -E '(debug1|Authentication|Connection)'
|
${openssh}/bin/ssh -v -o ConnectTimeout=5 -o BatchMode=yes "$ssh_user@$machine" "echo ✓ SSH connection to $machine successful" 2>&1
|
||||||
|
|
||||||
log "Testing SSH connection to $machine.tailnet (Tailscale)..."
|
# Use specific hostname for reverse-proxy
|
||||||
${openssh}/bin/ssh -v -o ConnectTimeout=5 -o BatchMode=yes "$ssh_user@$machine.tailnet" "echo ✓ SSH connection to $machine.tailnet successful" 2>&1 | grep -E '(debug1|Authentication|Connection)'
|
if [[ "$machine" == "reverse-proxy" ]]; then
|
||||||
|
log "Testing SSH connection to reverse-proxy.tail807ea.ts.net (Tailscale)..."
|
||||||
|
${openssh}/bin/ssh -v -o ConnectTimeout=5 -o BatchMode=yes "$ssh_user@reverse-proxy.tail807ea.ts.net" "echo ✓ SSH connection to reverse-proxy.tail807ea.ts.net successful" 2>&1
|
||||||
|
else
|
||||||
|
log "Testing SSH connection to $machine.tailnet (Tailscale)..."
|
||||||
|
${openssh}/bin/ssh -v -o ConnectTimeout=5 -o BatchMode=yes "$ssh_user@$machine.tailnet" "echo ✓ SSH connection to $machine.tailnet successful" 2>&1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Try with normal SSH first (for LAN)
|
# For reverse-proxy, try Tailscale first as it's likely only accessible that way
|
||||||
if ${openssh}/bin/ssh -o ConnectTimeout=2 -o BatchMode=yes "$ssh_user@$machine" "echo OK" >/dev/null 2>&1; then
|
if [[ "$machine" == "reverse-proxy" ]]; then
|
||||||
success " $machine: ✓ Online (LAN)"
|
# Use the specific Tailscale hostname for reverse-proxy
|
||||||
# Try with Tailscale hostname as fallback
|
if ${openssh}/bin/ssh -o ConnectTimeout=5 -o BatchMode=yes "$ssh_user@reverse-proxy.tail807ea.ts.net" "echo OK" >/dev/null 2>&1; then
|
||||||
elif ${openssh}/bin/ssh -o ConnectTimeout=2 -o BatchMode=yes "$ssh_user@$machine.tailnet" "echo OK" >/dev/null 2>&1; then
|
success " $machine: ✓ Online (Tailscale)"
|
||||||
success " $machine: ✓ Online (Tailscale)"
|
elif ${openssh}/bin/ssh -o ConnectTimeout=2 -o BatchMode=yes "$ssh_user@$machine" "echo OK" >/dev/null 2>&1; then
|
||||||
|
success " $machine: ✓ Online (LAN)"
|
||||||
|
else
|
||||||
|
warn " $machine: ⚠ Unreachable"
|
||||||
|
if [[ $verbose -eq 1 ]]; then
|
||||||
|
log " ℹ️ Note: reverse-proxy is likely only accessible via Tailscale"
|
||||||
|
log " ℹ️ Check if Tailscale is running on both machines and if the SSH service is active"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
# For other machines, try LAN first then Tailscale as fallback
|
||||||
else
|
else
|
||||||
warn " $machine: ⚠ Unreachable"
|
if ${openssh}/bin/ssh -o ConnectTimeout=2 -o BatchMode=yes "$ssh_user@$machine" "echo OK" >/dev/null 2>&1; then
|
||||||
|
success " $machine: ✓ Online (LAN)"
|
||||||
|
# Try with Tailscale hostname as fallback
|
||||||
|
elif ${openssh}/bin/ssh -o ConnectTimeout=3 -o BatchMode=yes "$ssh_user@$machine.tailnet" "echo OK" >/dev/null 2>&1; then
|
||||||
|
success " $machine: ✓ Online (Tailscale)"
|
||||||
|
else
|
||||||
|
warn " $machine: ⚠ Unreachable"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main command handling
|
# Main command handling
|
||||||
case "''${1:-}" in
|
case "''${1:-}" in
|
||||||
"deploy")
|
"deploy")
|
||||||
|
@ -168,33 +196,33 @@ writeShellScriptBin "lab" ''
|
||||||
error "Modes: boot (default), test, switch"
|
error "Modes: boot (default), test, switch"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
machine="$2"
|
machine="$2"
|
||||||
mode="''${3:-boot}"
|
mode="''${3:-boot}"
|
||||||
|
|
||||||
if [[ ! "$mode" =~ ^(boot|test|switch)$ ]]; then
|
if [[ ! "$mode" =~ ^(boot|test|switch)$ ]]; then
|
||||||
error "Invalid mode: $mode. Use boot, test, or switch"
|
error "Invalid mode: $mode. Use boot, test, or switch"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
deploy_machine "$machine" "$mode"
|
deploy_machine "$machine" "$mode"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
"status")
|
"status")
|
||||||
show_status
|
show_status
|
||||||
;;
|
;;
|
||||||
|
|
||||||
"update")
|
"update")
|
||||||
mode="''${2:-boot}"
|
mode="''${2:-boot}"
|
||||||
|
|
||||||
if [[ ! "$mode" =~ ^(boot|test|switch)$ ]]; then
|
if [[ ! "$mode" =~ ^(boot|test|switch)$ ]]; then
|
||||||
error "Invalid mode: $mode. Use boot, test, or switch"
|
error "Invalid mode: $mode. Use boot, test, or switch"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
update_all_machines "$mode"
|
update_all_machines "$mode"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
echo "Home-lab Management Tool"
|
echo "Home-lab Management Tool"
|
||||||
echo ""
|
echo ""
|
||||||
|
@ -202,7 +230,7 @@ writeShellScriptBin "lab" ''
|
||||||
echo ""
|
echo ""
|
||||||
echo "Available commands:"
|
echo "Available commands:"
|
||||||
echo " deploy <machine> [mode] - Deploy configuration to a machine"
|
echo " deploy <machine> [mode] - Deploy configuration to a machine"
|
||||||
echo " Machines: congenital-optimist, sleeper-service, grey-area, reverse-proxy"
|
echo " Machines: congenital-optimist, sleeper-service, grey-area, reverse-proxy"
|
||||||
echo " Modes: boot (default), test, switch"
|
echo " Modes: boot (default), test, switch"
|
||||||
echo " update [mode] - Update all machines"
|
echo " update [mode] - Update all machines"
|
||||||
echo " Modes: boot (default), test, switch"
|
echo " Modes: boot (default), test, switch"
|
||||||
|
@ -217,4 +245,4 @@ writeShellScriptBin "lab" ''
|
||||||
echo " lab status # Check all machines"
|
echo " lab status # Check all machines"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
''
|
''
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue