Refactor auto-boot-ollama-host script into modular structure with separate configuration, network, SSH, and service management modules for improved maintainability and readability
This commit is contained in:
50
scripts/ollama_manager.lua
Normal file
50
scripts/ollama_manager.lua
Normal file
@@ -0,0 +1,50 @@
|
||||
-- Ollama service management module for auto-boot-ollama-host
|
||||
-- Handles starting, stopping, and monitoring Ollama service
|
||||
|
||||
local socket = require("socket")
|
||||
local utils = require("utils")
|
||||
local network = require("network")
|
||||
local ssh = require("ssh")
|
||||
|
||||
local ollama_manager = {}
|
||||
|
||||
-- Start Ollama service via SSH
|
||||
function ollama_manager.start_service(config)
|
||||
utils.log("SSH is reachable. Starting ollama service...")
|
||||
socket.sleep(5)
|
||||
|
||||
-- Start ollama service using nssm
|
||||
ssh.execute("nssm start ollama", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
|
||||
-- Alternative systemctl commands (commented out)
|
||||
-- ssh.execute('wsl.exe -d Debian -- sudo systemctl enable ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
-- ssh.execute('wsl.exe -d Debian -- sudo systemctl start ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
|
||||
-- Wait for service to become available
|
||||
if network.port_is_up(config.OLLAMA_HOST, config.OLLAMA_PORT, 90) then
|
||||
utils.log("Ollama service is reachable again.")
|
||||
socket.sleep(30)
|
||||
return true
|
||||
else
|
||||
utils.log("Timeout waiting for Ollama service to come up after SSH command.")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- Stop Ollama service and shutdown host
|
||||
function ollama_manager.stop_service_and_shutdown(config)
|
||||
utils.log("Shutting down Ollama host to save power...")
|
||||
|
||||
-- Stop ollama service using nssm
|
||||
ssh.execute("nssm stop ollama", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
|
||||
-- Alternative systemctl commands (commented out)
|
||||
-- ssh.execute('wsl.exe -d Debian -- sudo systemctl disable ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
-- ssh.execute('wsl.exe -d Debian -- sudo systemctl stop ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
|
||||
-- Shutdown the host
|
||||
ssh.execute("shutdown.exe /s /t 0", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
socket.sleep(5)
|
||||
end
|
||||
|
||||
return ollama_manager
|
||||
Reference in New Issue
Block a user