-- Purpose: Minimal log watcher using `docker logs -f` as input. -- Requirements: lua 5.4 + luasocket + docker CLI inside the container. -- Notes: -- - Pattern match is plain substring (fast & simple). -- - Optional Wake-on-LAN is native (no external tools). -- - Optional port-wait is provided but commented out (mirrors original bash idea). local socket = require("socket") -- Import modules local config = require("config") local utils = require("utils") local network = require("network") local ssh = require("ssh") local ollama_manager = require("ollama_manager") -- Handle error pattern detection and recovery local function handle_error_pattern(config, powered_on) utils.log(("Detected EHOSTUNREACH for Ollama (%s:%d)."):format(config.OLLAMA_HOST, config.OLLAMA_PORT)) -- Send Wake-on-LAN if configured if config.WOL_MAC ~= "" then utils.log(("Sending WOL to %s via %s:%d"):format(config.WOL_MAC, config.WOL_BCAST, config.WOL_PORT)) local ok, err = network.send_wol(config.WOL_MAC, config.WOL_BCAST, config.WOL_PORT) if ok then powered_on = true utils.log(("Successfully sent WOL to %s via %s:%d"):format(config.WOL_MAC, config.WOL_BCAST, config.WOL_PORT)) else utils.log("WOL failed: " .. tostring(err)) end end -- Wait for SSH and start service utils.log("Waiting for SSH to become reachable...") if network.port_is_up(config.OLLAMA_HOST, config.SSH_PORT, 60) then ollama_manager.start_service(config) end end -- Handle finish pattern detection and shutdown local function handle_finish_pattern(config) utils.log(("Detected finish pattern: %q"):format(config.FINISH_PATTERN)) ollama_manager.stop_service_and_shutdown(config) end -- Main application logic local function main() utils.log(("Watching container='%s' since='%s'"):format(config.CONTAINER_NAME, config.SINCE)) utils.log(("Looking for pattern: %q"):format(config.ERROR_PATTERN)) local cmd = ("docker logs -f --since %q %q 2>&1"):format(config.SINCE, config.CONTAINER_NAME) local powered_on = false while true do local fh = assert(io.popen(cmd, "r")) for line in fh:lines() do -- Handle error pattern detection if line:find(config.ERROR_PATTERN, 1, true) ~= nil then handle_error_pattern(config, powered_on) powered_on = true end -- Handle finish pattern detection if line:find(config.FINISH_PATTERN, 1, true) ~= nil and powered_on == true then handle_finish_pattern(config) powered_on = false break end end fh:close() utils.log("Restarting log watch loop...") end end -- Run the application main()