regressed lab-tool to 0.10-dev to make it again

This commit is contained in:
Geir Okkenhaug Jerstad 2025-07-04 19:31:48 +02:00
parent 646c8bbc20
commit 2fdf7e4b0c
31 changed files with 732 additions and 635 deletions

View file

@ -0,0 +1,53 @@
;; core/commands.scm - Pure command building logic
(define-module (core commands)
#:use-module (ice-9 format)
#:use-module (core config)
#:use-module (deploy ssh-strategy)
#:use-module (deploy executor)
#:export (build-flake-update-command
deploy-to-machine
list-available-machines))
;; Pure function to build flake update command
(define (build-flake-update-command . flake-path-override)
"Build a command to update flake inputs"
(let ((flake-path (if (null? flake-path-override)
(get-flake-path)
(car flake-path-override))))
(format #f "nix flake update ~a" flake-path)))
;; Command to deploy to a specific machine
(define (deploy-to-machine machine-name . options)
"Deploy to a specific machine using centralized configuration"
(let* ((ssh-config (get-ssh-config machine-name))
(deploy-options (if (null? options) '() (car options)))
(deploy-plan (build-ssh-deploy-commands machine-name deploy-options)))
(if ssh-config
(begin
(display (format #f "Deploying to machine: ~a\n" machine-name))
(display (format #f "SSH Config: ~a\n" ssh-config))
;; Execute the deployment
(execute-deploy-commands deploy-plan))
(begin
(display (format #f "Error: Unknown machine '~a'\n" machine-name))
(display "Available machines:\n")
(list-available-machines)
#f))))
;; Command to list available machines
(define (list-available-machines)
"List all available machines for deployment"
(let ((machines (get-all-hosts)))
(display "Available machines:\n")
(for-each (lambda (machine)
(let ((ssh-config (get-ssh-config machine)))
(if ssh-config
(display (format #f " ~a - ~a@~a\n"
machine
(assoc-ref ssh-config 'user)
(assoc-ref ssh-config 'hostname))))))
machines)
machines))

View file

@ -0,0 +1,81 @@
;; core/config.scm - Pure config data and accessors
(define-module (core config)
#:use-module (srfi srfi-1) ; for fold
#:export (default-config
get-config-value
host-configs
get-host-config
get-all-hosts
validate-host-name
get-ssh-config
get-flake-path
get-ssh-key))
;; Declarative configuration (source of truth)
(define default-config
'((ssh-user . "sma")
(ssh-key . "~/.ssh/id_ed25519_admin")
(flake-path . "~/Projects/home-lab")
(hosts . ((congenital-optimist (hostname . "congenital-optimist"))
(sleeper-service (hostname . "sleeper-service"))
(grey-area (hostname . "grey-area"))
(reverse-proxy (hostname . "reverse-proxy"))
(little-rascal (hostname . "little-rascal"))))
(deployment . ((default-mode . "boot")
(timeout . 300)
(retry-count . 3)))
(monitoring . ((interval . 30)
(timeout . 10)))
(mcp . ((port . 3001)
(host . "localhost")
(log-level . "info")))))
;; Accessors (pure, no mutation, no IO)
(define (get-config-value path . default)
(let ((result (fold (lambda (key acc)
(if (and acc (list? acc))
(assoc-ref acc key)
#f))
default-config path)))
(if result
result
(if (null? default) #f (car default)))))
(define (host-configs)
(get-config-value '(hosts)))
(define (get-host-config host-name)
(let ((host-symbol (if (symbol? host-name)
host-name
(string->symbol host-name))))
(assoc-ref (host-configs) host-symbol)))
(define (get-all-hosts)
(map (lambda (host-entry)
(symbol->string (car host-entry)))
(host-configs)))
(define (validate-host-name host-name)
(let ((hosts (get-all-hosts)))
(if (member host-name hosts)
#t
#f)))
(define (get-ssh-config host-name)
(let ((host-config (get-host-config host-name))
(ssh-user (get-config-value '(ssh-user) "sma"))
(ssh-key (get-config-value '(ssh-key) "~/.ssh/id_ed25519_admin")))
(if host-config
(let ((hostname (assoc-ref host-config 'hostname)))
`((hostname . ,hostname)
(user . ,ssh-user)
(ssh-user . ,ssh-user)
(identity-file . ,ssh-key)))
#f)))
(define (get-flake-path)
(get-config-value '(flake-path) "~/Projects/home-lab"))
(define (get-ssh-key)
(get-config-value '(ssh-key) "~/.ssh/id_ed25519_admin"))