home-lab/packages/lab-tool/main.scm
Geir Okkenhaug Jerstad 9d8952c4ce feat: Complete Ollama CPU optimization for TaskMaster AI
- Optimize Ollama service configuration for maximum CPU performance
  - Increase OLLAMA_NUM_PARALLEL from 2 to 4 workers
  - Increase OLLAMA_CONTEXT_LENGTH from 4096 to 8192 tokens
  - Add OLLAMA_KV_CACHE_TYPE=q8_0 for memory efficiency
  - Set OLLAMA_LLM_LIBRARY=cpu_avx2 for optimal CPU performance
  - Configure OpenMP threading with 8 threads and core binding
  - Add comprehensive systemd resource limits and CPU quotas
  - Remove incompatible NUMA policy setting

- Upgrade TaskMaster AI model ecosystem
  - Main model: qwen3:4b → qwen2.5-coder:7b (specialized coding model)
  - Research model: deepseek-r1:1.5b → deepseek-r1:7b (enhanced reasoning)
  - Fallback model: gemma3:4b-it-qat → llama3.3:8b (reliable general purpose)

- Create comprehensive optimization and management scripts
  - Add ollama-optimize.sh for system optimization and benchmarking
  - Add update-taskmaster-models.sh for TaskMaster configuration management
  - Include model installation, performance testing, and system info functions

- Update TaskMaster AI configuration
  - Configure optimized models with grey-area:11434 endpoint
  - Set performance parameters for 8192 context window
  - Add connection timeout and retry settings

- Fix flake configuration issues
  - Remove nested packages attribute in packages/default.nix
  - Fix package references in modules/users/geir.nix
  - Clean up obsolete package files

- Add comprehensive documentation
  - Document complete optimization process and results
  - Include performance benchmarking results
  - Provide deployment instructions and troubleshooting guide

Successfully deployed via deploy-rs with 3-4x performance improvement estimated.
All optimizations tested and verified on grey-area server (24-core Xeon, 31GB RAM).
2025-06-18 13:08:24 +02:00

258 lines
No EOL
8.5 KiB
Scheme
Executable file

#!/usr/bin/env guile
!#
;; Home Lab Tool - Main Entry Point
;; K.I.S.S Refactored Implementation
;; Load path is set by the wrapper script in default.nix
;; No need to add current directory when running from Nix
(use-modules (ice-9 match)
(ice-9 format)
(utils config)
(utils logging)
(lab core)
(lab machines)
(lab deployment))
;; Initialize logging
(set-log-level! 'info)
;; Pure function: Command help text
(define (get-help-text)
"Pure function returning help text"
"Home Lab Tool - K.I.S.S Refactored Edition
USAGE: lab <command> [args...]
COMMANDS:
status Show infrastructure status
machines List all machines
deploy <machine> Deploy configuration to machine
deploy-all Deploy to all machines
update Update flake inputs
health [machine] Check machine health (all if no machine specified)
ssh <machine> SSH to machine
test-modules Test modular implementation
help Show this help
EXAMPLES:
lab status
lab machines
lab deploy congenital-optimist
lab deploy-all
lab update
lab health
lab health sleeper-service
lab ssh sleeper-service
lab test-modules
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
")
;; Pure function: Format machine list
(define (format-machine-list machines)
"Pure function to format machine list for display"
(if (null? machines)
"No machines configured"
(string-join
(map (lambda (machine) (format #f " - ~a" machine)) machines)
"\n")))
;; Pure function: Format status info
(define (format-status-info machines config)
"Pure function to format infrastructure status"
(format #f "Infrastructure Status:
Total machines: ~a
Home lab root: ~a
~a"
(length machines)
(get-config-value '(homelab-root))
(format-machine-list machines)))
;; Command implementations
(define (cmd-status)
"Show infrastructure status"
(log-info "Checking infrastructure status...")
(let* ((machines (list-machines))
(status (get-infrastructure-status))
(config (get-current-config))
(status-text (format-status-info machines config)))
(display status-text)
(newline)
(for-each (lambda (machine-status)
(let ((machine (assoc-ref machine-status 'machine))
(status (assoc-ref machine-status 'status)))
(format #t " ~a: ~a\n" machine status)))
status)
(log-success "Status check complete")))
(define (cmd-machines)
"List all configured machines"
(log-info "Listing configured machines...")
(let* ((machines (list-machines))
(machine-list (format-machine-list machines)))
(format #t "Configured Machines:\n~a\n" machine-list)
(log-success "Machine list complete")))
(define (cmd-deploy machine-name)
"Deploy configuration to machine"
(log-info "Deploying to machine: ~a" machine-name)
(if (validate-machine-name machine-name)
(let ((result (deploy-machine machine-name "boot" '())))
(if result
(log-success "Deployment to ~a complete" machine-name)
(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) ", ")))))
(define (cmd-ssh machine-name)
"SSH to machine"
(log-info "Connecting to machine: ~a" 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))
(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)))
(log-info "Connecting to ~a via SSH..." target)
(system (format #f "ssh ~a" target)))))
(log-error "No SSH configuration found for ~a" machine-name)))
(log-error "Invalid machine: ~a" machine-name)))
(define (cmd-test-modules)
"Test the modular implementation"
(log-info "Testing modular implementation...")
;; Test pure functions
(use-modules (utils config accessor))
(let* ((config (get-current-config))
(machines (get-all-machines-pure config))
(blue-color (get-color 'blue)))
(format #t "\n=== Modular Implementation Test ===\n")
(format #t "Pure config access: ~a machines\n" (length machines))
(format #t "Pure color function: ~ablue text~a\n" blue-color (get-color 'reset))
(format #t "\n✅ All pure functions working correctly!\n\n")
(log-success "Modular implementation test complete")))
(define (cmd-update)
"Update flake inputs"
(log-info "Updating flake inputs...")
(let ((result (update-flake '())))
(if result
(log-success "Flake update complete")
(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))))
(define (cmd-health args)
"Check machine health"
(let ((machine-name (if (null? args) #f (car args))))
(if machine-name
;; Check specific machine
(if (validate-machine-name machine-name)
(let ((health (check-machine-health machine-name)))
(format #t "Health check for ~a:\n" machine-name)
(format #t " SSH: ~a\n" (assoc-ref health 'ssh-connectivity))
(format #t " Status: ~a\n" (assoc-ref health 'status))
(format #t " Services: ~a configured\n" (assoc-ref health 'services-configured)))
(log-error "Invalid machine: ~a" machine-name))
;; Check all machines
(let ((results (discover-machines)))
(format #t "Health Summary:\n")
(for-each (lambda (health)
(let ((machine (assoc-ref health 'machine))
(status (assoc-ref health 'status)))
(format #t " ~a: ~a\n" machine status)))
results)))))
;; Main command dispatcher
(define (dispatch-command command args)
"Dispatch command with appropriate handler"
(match command
('help
(display (get-help-text)))
('status
(cmd-status))
('machines
(cmd-machines))
('deploy
(if (null? args)
(begin
(log-error "deploy command requires machine name")
(format #t "Usage: lab deploy <machine>\n"))
(cmd-deploy (car args))))
('deploy-all
(cmd-deploy-all))
('update
(cmd-update))
('health
(cmd-health args))
('ssh
(if (null? args)
(begin
(log-error "ssh command requires machine name")
(format #t "Usage: lab ssh <machine>\n"))
(cmd-ssh (car args))))
('test-modules
(cmd-test-modules))
(_
(log-error "Unknown command: ~a" command)
(format #t "Use 'lab help' for available commands\n")
(exit 1))))
;; Main entry point
(define (main args)
"Main entry point for lab tool"
(log-info "Home Lab Tool - K.I.S.S Refactored Edition")
(let* ((parsed-cmd (if (> (length args) 1) (cdr args) '("help")))
(command (string->symbol (car parsed-cmd)))
(cmd-args (cdr parsed-cmd)))
(catch #t
(lambda () (dispatch-command command cmd-args))
(lambda (key . error-args)
(log-error "Command failed: ~a ~a" key error-args)
(exit 1))))
(log-debug "Command execution complete"))
;; Run main function if script is executed directly
(when (and (defined? 'command-line) (not (null? (command-line))))
(main (command-line)))