We have made an emacs conf with profiles. And refactored lab tool to use deploy-rs

This commit is contained in:
Geir Okkenhaug Jerstad 2025-07-03 15:09:33 +02:00
parent 24b01ae4f0
commit bff56e4ffc
22 changed files with 1448 additions and 176 deletions

View file

@ -1,4 +1,4 @@
;; lab/deployment.scm - Deployment operations (impure)
;; lab/deployment.scm - Deploy-rs based deployment operations
(define-module (lab deployment)
#:use-module (ice-9 format)
@ -7,10 +7,10 @@
#:use-module (srfi srfi-1)
#:use-module (utils logging)
#:use-module (utils config)
#:use-module (utils ssh)
#:export (deploy-machine
update-flake
execute-nixos-rebuild
deploy-all-machines
deploy-with-rollback
option-ref))
;; Helper function for option handling
@ -19,26 +19,128 @@
(let ((value (assoc-ref options key)))
(if value value default)))
;; Impure function: Deploy machine configuration
;; Main deployment function using deploy-rs
(define (deploy-machine machine-name . args)
"Deploy configuration to machine (impure - has side effects)"
(let* ((mode (if (null? args) "boot" (car args)))
"Deploy configuration to machine using deploy-rs (impure - has side effects)"
(let* ((mode (if (null? args) "default" (car args)))
(options (if (< (length args) 2) '() (cadr args)))
(valid-modes '("boot" "test" "switch"))
(dry-run (option-ref options 'dry-run #f)))
(dry-run (option-ref options 'dry-run #f))
(skip-checks (option-ref options 'skip-checks #f)))
(if (not (validate-machine-name machine-name))
#f
(if (not (member mode valid-modes))
(begin
(log-error "Invalid deployment mode: ~a" mode)
(log-error "Valid modes: ~a" (string-join valid-modes ", "))
#f)
(begin
(log-info "Starting deployment: ~a (mode: ~a)" machine-name mode)
(execute-nixos-rebuild machine-name mode options))))))
(begin
(log-info "Starting deploy-rs deployment: ~a" machine-name)
(execute-deploy-rs machine-name mode options)))))
;; Impure function: Update flake inputs
;; Execute deploy-rs deployment
(define (execute-deploy-rs machine-name mode options)
"Execute deployment using deploy-rs with automatic rollback"
(let* ((homelab-root (get-homelab-root))
(dry-run (option-ref options 'dry-run #f))
(skip-checks (option-ref options 'skip-checks #f))
(auto-rollback (option-ref options 'auto-rollback #t))
(magic-rollback (option-ref options 'magic-rollback #t)))
(log-info "Deploying ~a using deploy-rs..." machine-name)
(if dry-run
(begin
(log-info "DRY RUN: Would execute deploy-rs for ~a" machine-name)
(log-info "Command would be: deploy '.#~a'" machine-name)
#t)
(let* ((deploy-cmd (build-deploy-command machine-name skip-checks auto-rollback magic-rollback))
(start-time (current-time)))
(log-debug "Deploy command: ~a" deploy-cmd)
(log-info "Executing deployment with automatic rollback protection...")
(let* ((port (open-pipe* OPEN_READ "/bin/sh" "-c" deploy-cmd))
(output (get-string-all port))
(status (close-pipe port))
(elapsed (- (current-time) start-time)))
(if (zero? status)
(begin
(log-success "Deploy-rs deployment completed successfully in ~as" elapsed)
(log-info "Deployment output:")
(log-info "~a" output)
#t)
(begin
(log-error "Deploy-rs deployment failed (exit: ~a)" status)
(log-error "Error output:")
(log-error "~a" output)
(log-info "Deploy-rs automatic rollback should have been triggered")
#f)))))))
;; Build deploy-rs command with options
(define (build-deploy-command machine-name skip-checks auto-rollback magic-rollback)
"Build the deploy-rs command with appropriate flags"
(let ((base-cmd (format #f "cd ~a && deploy '.#~a'" (get-homelab-root) machine-name))
(flags '()))
;; Add flags based on options
(when skip-checks
(set! flags (cons "--skip-checks" flags)))
(when auto-rollback
(set! flags (cons "--auto-rollback" flags)))
(when magic-rollback
(set! flags (cons "--magic-rollback" flags)))
;; Combine command with flags
(if (null? flags)
base-cmd
(format #f "~a ~a" base-cmd (string-join flags " ")))))
;; Deploy to all machines
(define (deploy-all-machines . args)
"Deploy to all machines using deploy-rs"
(let* ((options (if (null? args) '() (car args)))
(dry-run (option-ref options 'dry-run #f))
(machines (get-all-machines)))
(log-info "Starting deployment to all machines (~a total)" (length machines))
(let ((results
(map (lambda (machine)
(log-info "Deploying to ~a..." machine)
(let ((result (deploy-machine machine "default" options)))
(if result
(log-success "✓ ~a deployed successfully" machine)
(log-error "✗ ~a deployment failed" machine))
(cons machine result)))
machines)))
;; Summary
(let ((successful (filter cdr results))
(failed (filter (lambda (r) (not (cdr r))) results)))
(log-info "Deployment summary:")
(log-info " Successful: ~a/~a machines" (length successful) (length machines))
(when (not (null? failed))
(log-error " Failed: ~a" (string-join (map car failed) ", ")))
;; Return true if all succeeded
(= (length successful) (length machines))))))
;; Deploy with explicit rollback testing
(define (deploy-with-rollback machine-name . args)
"Deploy with explicit rollback capability testing"
(let* ((options (if (null? args) '() (car args)))
(test-rollback (option-ref options 'test-rollback #f)))
(log-info "Deploying ~a with rollback testing..." machine-name)
(if test-rollback
(begin
(log-info "Testing rollback mechanism (deploy will be reverted)")
;; Deploy with magic rollback disabled to test manual rollback
(let ((modified-options (cons '(magic-rollback . #f) options)))
(execute-deploy-rs machine-name "default" modified-options)))
(execute-deploy-rs machine-name "default" options))))
;; Update flake inputs (moved from old deployment module)
(define (update-flake . args)
"Update flake inputs (impure - has side effects)"
(let* ((options (if (null? args) '() (car args)))
@ -64,76 +166,3 @@
(log-error "Flake update failed (exit: ~a)" status)
(log-error "Error output: ~a" output)
#f))))))
;; Impure function: Execute nixos-rebuild
(define (execute-nixos-rebuild machine-name mode options)
"Execute nixos-rebuild command (impure - has side effects)"
(let* ((dry-run (option-ref options 'dry-run #f))
(ssh-config (get-ssh-config machine-name))
(is-local (and ssh-config (assoc-ref ssh-config 'is-local)))
(homelab-root (get-homelab-root)))
(if is-local
;; Local deployment
(let ((rebuild-cmd (format #f "sudo nixos-rebuild ~a --flake ~a#~a"
mode homelab-root machine-name)))
(log-debug "Local rebuild command: ~a" rebuild-cmd)
(if dry-run
(begin
(log-info "DRY RUN: Would execute: ~a" rebuild-cmd)
#t)
(let* ((port (open-pipe* OPEN_READ "/bin/sh" "-c" rebuild-cmd))
(output (get-string-all port))
(status (close-pipe port)))
(if (zero? status)
(begin
(log-success "Local nixos-rebuild completed")
#t)
(begin
(log-error "Local nixos-rebuild failed (exit: ~a)" status)
#f)))))
;; Remote deployment
(let* ((hostname (assoc-ref ssh-config 'hostname))
(ssh-alias (or (assoc-ref ssh-config 'ssh-alias) hostname))
(temp-dir "/tmp/homelab-deploy")
(sync-cmd (format #f "rsync -av --delete ~a/ ~a:~a/"
homelab-root ssh-alias temp-dir))
(rebuild-cmd (format #f "ssh ~a 'cd ~a && sudo nixos-rebuild ~a --flake .#~a'"
ssh-alias temp-dir mode machine-name)))
(log-debug "Remote sync command: ~a" sync-cmd)
(log-debug "Remote rebuild command: ~a" rebuild-cmd)
(if dry-run
(begin
(log-info "DRY RUN: Would sync and rebuild remotely")
#t)
(begin
;; Sync configuration
(log-info "Syncing configuration to ~a..." machine-name)
(let* ((sync-port (open-pipe* OPEN_READ "/bin/sh" "-c" sync-cmd))
(sync-output (get-string-all sync-port))
(sync-status (close-pipe sync-port)))
(if (zero? sync-status)
(begin
(log-success "Configuration synced")
;; Execute rebuild
(log-info "Executing nixos-rebuild on ~a..." machine-name)
(let* ((rebuild-port (open-pipe* OPEN_READ "/bin/sh" "-c" rebuild-cmd))
(rebuild-output (get-string-all rebuild-port))
(rebuild-status (close-pipe rebuild-port)))
(if (zero? rebuild-status)
(begin
(log-success "Remote nixos-rebuild completed")
#t)
(begin
(log-error "Remote nixos-rebuild failed (exit: ~a)" rebuild-status)
#f))))
(begin
(log-error "Configuration sync failed (exit: ~a)" sync-status)
#f)))))))))

View file

@ -22,44 +22,48 @@
;; Pure function: Command help text
(define (get-help-text)
"Pure function returning help text"
"Home Lab Tool - K.I.S.S Refactored Edition
"Home Lab Tool - Deploy-rs Edition
USAGE: lab <command> [args...]
COMMANDS:
status Show infrastructure status
machines List all machines
deploy <machine> [mode] Deploy configuration to machine
Available modes: boot (default), test, switch
deploy-all Deploy to all machines
machines List all machines
deploy <machine> [options] Deploy configuration to machine using deploy-rs
Options: --dry-run, --skip-checks
deploy-all [options] Deploy to all machines using deploy-rs
update Update flake inputs
auto-update Perform automatic system update with health checks
auto-update-status Show auto-update service status and logs
health [machine] Check machine health (all if no machine specified)
ssh <machine> SSH to machine
test-modules Test modular implementation
ssh <machine> SSH to machine (using sma user)
test-rollback <machine> Test deployment with rollback
help Show this help
EXAMPLES:
lab status
lab machines
lab deploy congenital-optimist # Deploy with boot mode (default)
lab deploy congenital-optimist switch # Deploy and activate immediately
lab deploy congenital-optimist test # Deploy temporarily for testing
lab deploy-all
lab update
lab auto-update # Perform automatic update with reboot
lab auto-update-status # Show auto-update logs and status
lab health
lab health sleeper-service
lab ssh sleeper-service
lab test-modules
lab deploy congenital-optimist # Deploy with deploy-rs safety
lab deploy sleeper-service --dry-run # Test deployment without applying
lab deploy grey-area --skip-checks # Deploy without health checks
lab deploy-all # Deploy to all machines
lab deploy-all --dry-run # Test deployment to all machines
lab update # Update flake inputs
lab test-rollback sleeper-service # Test rollback functionality
lab ssh sleeper-service # SSH to machine as sma user
This implementation follows K.I.S.S principles:
- Modular: Each module has single responsibility
- Functional: Pure functions separated from side effects
- Small: Individual modules under 50 lines
- Simple: One function does one thing well
Deploy-rs Features:
- Automatic rollback on deployment failure
- Health checks after deployment
- Magic rollback for network connectivity issues
- Atomic deployments with safety guarantees
- Consistent sma user for all deployments
This implementation uses deploy-rs for all deployments:
- Robust: Automatic rollback protection
- Safe: Health checks and validation
- Consistent: Same deployment method for all machines
- Flexible: Dry-run and skip-checks options available
")
;; Pure function: Format machine list
@ -109,36 +113,33 @@ Home lab root: ~a
(log-success "Machine list complete")))
(define (cmd-deploy machine-name . args)
"Deploy configuration to machine"
(let* ((mode (if (null? args) "boot" (car args)))
(valid-modes '("boot" "test" "switch")))
(log-info "Deploying to machine: ~a (mode: ~a)" machine-name mode)
(if (not (member mode valid-modes))
"Deploy configuration to machine using deploy-rs"
(let* ((options (parse-deploy-options args)))
(log-info "Deploying to machine: ~a using deploy-rs" machine-name)
(if (validate-machine-name machine-name)
(let ((result (deploy-machine machine-name "default" options)))
(if result
(log-success "Deploy-rs deployment to ~a completed successfully" machine-name)
(log-error "Deploy-rs deployment to ~a failed" machine-name)))
(begin
(log-error "Invalid deployment mode: ~a" mode)
(log-error "Valid modes: ~a" (string-join valid-modes ", "))
(format #t "Usage: lab deploy <machine> [boot|test|switch]\n"))
(if (validate-machine-name machine-name)
(let ((result (deploy-machine machine-name mode '())))
(if result
(log-success "Deployment to ~a complete (mode: ~a)" machine-name mode)
(log-error "Deployment to ~a failed" machine-name)))
(begin
(log-error "Invalid machine: ~a" machine-name)
(log-info "Available machines: ~a" (string-join (get-all-machines) ", ")))))))
(log-error "Invalid machine: ~a" machine-name)
(log-info "Available machines: ~a" (string-join (get-all-machines) ", "))))))
(define (cmd-ssh machine-name)
"SSH to machine"
(log-info "Connecting to machine: ~a" machine-name)
"SSH to machine using sma user"
(log-info "Connecting to machine: ~a as sma user" machine-name)
(if (validate-machine-name machine-name)
(let ((ssh-config (get-ssh-config machine-name)))
(if ssh-config
(let ((hostname (assoc-ref ssh-config 'hostname))
(ssh-alias (assoc-ref ssh-config 'ssh-alias))
(ssh-user (assoc-ref ssh-config 'ssh-user))
(is-local (assoc-ref ssh-config 'is-local)))
(if is-local
(log-info "Machine ~a is local - no SSH needed" machine-name)
(let ((target (or ssh-alias hostname)))
(begin
(log-info "Machine ~a is local - switching to sma user locally" machine-name)
(system "sudo -u sma -i"))
(let ((target (format #f "~a@~a" (or ssh-user "sma") (or ssh-alias hostname))))
(log-info "Connecting to ~a via SSH..." target)
(system (format #f "ssh ~a" target)))))
(log-error "No SSH configuration found for ~a" machine-name)))
@ -171,20 +172,12 @@ Home lab root: ~a
(log-error "Flake update failed"))))
(define (cmd-deploy-all)
"Deploy to all machines"
(log-info "Deploying to all machines...")
(let* ((machines (list-machines))
(results (map (lambda (machine)
(log-info "Deploying to ~a..." machine)
(let ((result (deploy-machine machine "boot" '())))
(if result
(log-success "✓ ~a deployed" machine)
(log-error "✗ ~a failed" machine))
result))
machines))
(successful (filter identity results)))
(log-info "Deployment summary: ~a/~a successful"
(length successful) (length machines))))
"Deploy to all machines using deploy-rs"
(log-info "Deploying to all machines using deploy-rs...")
(let ((result (deploy-all-machines '())))
(if result
(log-success "All deploy-rs deployments completed successfully")
(log-error "Some deploy-rs deployments failed"))))
(define (cmd-health args)
"Check machine health"
@ -219,6 +212,33 @@ Home lab root: ~a
"Show auto-update status and logs"
(auto-update-status))
;; Parse deployment options from command line arguments
(define (parse-deploy-options args)
"Parse deployment options from command line arguments"
(let ((options '()))
(for-each
(lambda (arg)
(cond
((string=? arg "--dry-run")
(set! options (cons '(dry-run . #t) options)))
((string=? arg "--skip-checks")
(set! options (cons '(skip-checks . #t) options)))
(else
(log-warn "Unknown option: ~a" arg))))
args)
options))
(define (cmd-test-rollback machine-name)
"Test deployment with rollback functionality"
(log-info "Testing rollback deployment for machine: ~a" machine-name)
(if (validate-machine-name machine-name)
(let ((options '((test-rollback . #t))))
(let ((result (deploy-with-rollback machine-name options)))
(if result
(log-success "Rollback test completed for ~a" machine-name)
(log-error "Rollback test failed for ~a" machine-name))))
(log-error "Invalid machine: ~a" machine-name)))
;; Main command dispatcher
(define (dispatch-command command args)
"Dispatch command with appropriate handler"
@ -236,12 +256,20 @@ Home lab root: ~a
(if (null? args)
(begin
(log-error "deploy command requires machine name")
(format #t "Usage: lab deploy <machine> [boot|test|switch]\n"))
(format #t "Usage: lab deploy <machine> [options]\n")
(format #t "Options: --dry-run, --skip-checks\n"))
(apply cmd-deploy args)))
('deploy-all
(cmd-deploy-all))
('test-rollback
(if (null? args)
(begin
(log-error "test-rollback command requires machine name")
(format #t "Usage: lab test-rollback <machine>\n"))
(cmd-test-rollback (car args))))
('update
(cmd-update))
@ -264,6 +292,13 @@ Home lab root: ~a
('test-modules
(cmd-test-modules))
('test-rollback
(if (null? args)
(begin
(log-error "test-rollback command requires machine name")
(format #t "Usage: lab test-rollback <machine>\n"))
(cmd-test-rollback (car args))))
(_
(log-error "Unknown command: ~a" command)
(format #t "Use 'lab help' for available commands\n")
@ -272,7 +307,7 @@ Home lab root: ~a
;; Main entry point
(define (main args)
"Main entry point for lab tool"
(log-info "Home Lab Tool - K.I.S.S Refactored Edition")
(log-info "Home Lab Tool - Deploy-rs Edition")
(let* ((parsed-cmd (if (> (length args) 1) (cdr args) '("help")))
(command (string->symbol (car parsed-cmd)))

View file

@ -22,26 +22,31 @@
(machines . ((congenital-optimist
(type . local)
(hostname . "localhost")
(ssh-user . "sma")
(services . (workstation development)))
(sleeper-service
(type . remote)
(hostname . "sleeper-service.tail807ea.ts.net")
(ssh-alias . "admin-sleeper")
(ssh-alias . "sleeper-service.tail807ea.ts.net")
(ssh-user . "sma")
(services . (nfs zfs storage)))
(grey-area
(type . remote)
(hostname . "grey-area.tail807ea.ts.net")
(ssh-alias . "admin-grey")
(ssh-alias . "grey-area.tail807ea.ts.net")
(ssh-user . "sma")
(services . (ollama forgejo git)))
(reverse-proxy
(type . remote)
(hostname . "reverse-proxy.tail807ea.ts.net")
(ssh-alias . "admin-reverse")
(ssh-alias . "reverse-proxy.tail807ea.ts.net")
(ssh-user . "sma")
(services . (nginx proxy ssl)))
(little-rascal
(type . remote)
(hostname . "little-rascal.tail807ea.ts.net")
(ssh-alias . "little-rascal")
(ssh-alias . "little-rascal.tail807ea.ts.net")
(ssh-user . "sma")
(services . (development niri desktop ai-tools)))))
(deployment . ((default-mode . "boot")
(timeout . 300)
@ -124,10 +129,12 @@
(if machine-config
(let ((type (assoc-ref machine-config 'type))
(hostname (assoc-ref machine-config 'hostname))
(ssh-alias (assoc-ref machine-config 'ssh-alias)))
(ssh-alias (assoc-ref machine-config 'ssh-alias))
(ssh-user (assoc-ref machine-config 'ssh-user)))
`((type . ,type)
(hostname . ,hostname)
(ssh-alias . ,ssh-alias)
(ssh-user . ,ssh-user)
(is-local . ,(eq? type 'local))))
#f)))