From 1adf33b739e0893841f327ebd6620cca49e5d350 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Thu, 5 Nov 2026 08:24:54 -0600 Subject: [PATCH] Schedule homelab power window from OCI edge --- bootstrap/edge/main.tf | 79 ++++++++++++++++++++++++++++++++++ bootstrap/edge/variables.tf | 21 +++++++++ cmd/jeannie-server/main.go | 1 + docs/app-ops.md | 34 +++++++++++++++ docs/templates/app-ops.md.tmpl | 34 +++++++++++++++ jeannie | 4 ++ lib/jeannie/commands.sh | 30 +++++++++++++ lib/jeannie/env.sh | 2 + 8 files changed, 205 insertions(+) diff --git a/bootstrap/edge/main.tf b/bootstrap/edge/main.tf index 607cae4..c050ccb 100644 --- a/bootstrap/edge/main.tf +++ b/bootstrap/edge/main.tf @@ -123,6 +123,9 @@ resource "null_resource" "edge_services" { letsencrypt_email = var.letsencrypt_email letsencrypt_staging = tostring(var.letsencrypt_staging) certbot_version = "2" + lab_schedule_enabled = tostring(var.lab_schedule_enabled) + lab_schedule_api_url = var.lab_schedule_api_url + lab_schedule_timezone = var.lab_schedule_timezone config_hash = local.config_hash } @@ -198,6 +201,10 @@ server_name="${self.triggers.server_name}" matrix_server_name="${self.triggers.matrix_server_name}" element_server_name="${self.triggers.element_server_name}" matrix_enabled="${self.triggers.matrix_enabled}" +lab_schedule_enabled="${self.triggers.lab_schedule_enabled}" +lab_schedule_api_url="${self.triggers.lab_schedule_api_url}" +lab_schedule_timezone="${self.triggers.lab_schedule_timezone}" +lab_schedule_api_token='${var.lab_schedule_api_token}' server_names="${self.triggers.server_names}" certbot_domain_args="${self.triggers.certbot_domain_args}" backend_host="${self.triggers.backend_host}" @@ -412,6 +419,78 @@ fi RENEW_EOT sudo chmod 0755 /usr/local/sbin/homelab-edge-renew-certs.sh +if [ "$lab_schedule_enabled" = "true" ] && [ -n "$lab_schedule_api_url" ] && [ -n "$lab_schedule_api_token" ]; then + sudo tee /etc/homelab-lab-window.env >/dev/null </dev/null <<'SCHEDULE_EOT' +#!/bin/sh +set -eu +. /etc/homelab-lab-window.env +action="${1:?start or stop is required}" +case "$action" in start|stop) ;; *) exit 2 ;; esac +payload="$(printf '{"command":"lab-window","args":["%s"]}' "$action")" +response="$(curl --noproxy '*' -fsS --connect-timeout 10 --max-time 30 \ + -H "Authorization: Bearer $LAB_WINDOW_API_TOKEN" -H 'Content-Type: application/json' \ + -d "$payload" "$LAB_WINDOW_API_URL/v1/execute")" +execution_id="$(printf '%s' "$response" | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])')" +deadline=$(( $(date +%s) + 1800 )) +while [ "$(date +%s)" -lt "$deadline" ]; do + result="$(curl --noproxy '*' -fsS --connect-timeout 10 --max-time 30 \ + -H "Authorization: Bearer $LAB_WINDOW_API_TOKEN" "$LAB_WINDOW_API_URL/v1/executions/$execution_id")" + status="$(printf '%s' "$result" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("status",""))')" + case "$status" in + succeeded) exit 0 ;; + failed) printf '%s\n' "$result" >&2; exit 1 ;; + esac + sleep 15 +done +echo "Timed out waiting for Jeannie lab-window $action" >&2 +exit 1 +SCHEDULE_EOT + sudo chmod 0755 /usr/local/sbin/homelab-lab-window + sudo tee /etc/systemd/system/homelab-lab-window-start.service >/dev/null <<'SERVICE_EOT' +[Unit] +Description=Start the Jeannie homelab power window +[Service] +Type=oneshot +ExecStart=/usr/local/sbin/homelab-lab-window start +SERVICE_EOT + sudo tee /etc/systemd/system/homelab-lab-window-stop.service >/dev/null <<'SERVICE_EOT' +[Unit] +Description=Stop the Jeannie homelab power window +[Service] +Type=oneshot +ExecStart=/usr/local/sbin/homelab-lab-window stop +SERVICE_EOT + sudo tee /etc/systemd/system/homelab-lab-window-start.timer >/dev/null </dev/null </dev/null || true +fi + sudo tee /etc/systemd/system/homelab-edge-renew-certs.service >/dev/null <<'SERVICE_EOT' [Unit] Description=Renew Homelab Edge TLS certificates diff --git a/bootstrap/edge/variables.tf b/bootstrap/edge/variables.tf index 0d9beb7..903d22f 100644 --- a/bootstrap/edge/variables.tf +++ b/bootstrap/edge/variables.tf @@ -83,6 +83,27 @@ variable "edge_install_docker" { default = true } +variable "lab_schedule_enabled" { + type = bool + default = true +} + +variable "lab_schedule_api_url" { + type = string + default = "" +} + +variable "lab_schedule_api_token" { + type = string + sensitive = true + default = "" +} + +variable "lab_schedule_timezone" { + type = string + default = "America/Mexico_City" +} + variable "server_name" { type = string default = "" diff --git a/cmd/jeannie-server/main.go b/cmd/jeannie-server/main.go index 39193cf..e63ed55 100644 --- a/cmd/jeannie-server/main.go +++ b/cmd/jeannie-server/main.go @@ -27,6 +27,7 @@ const maxOutputBytes = 1 << 20 // recursive remote command are intentionally excluded. var allowedCommands = map[string]bool{ "help": true, "up": true, "plan": true, "cluster": true, "cluster-context": true, + "lab-window": true, "cluster-profile": true, "cluster-runtime": true, "cluster-transition": true, "rebuild-cluster": true, "stop-cluster": true, "start-cluster": true, "cleanup-debian-k8s": true, "shutdown": true, "start": true, diff --git a/docs/app-ops.md b/docs/app-ops.md index 19c3d09..ded39ef 100644 --- a/docs/app-ops.md +++ b/docs/app-ops.md @@ -214,6 +214,40 @@ when Pimox workers or cluster networking are the thing being investigated. ./jeannie doctor-k8s ``` +## Scheduled lab power window + +The OCI edge can keep the management plane available while starting the +selected cluster and host services at 08:00 and stopping the data plane at +17:00 in `America/Mexico_City`. The edge timer calls the authenticated Jeannie +API directly over Tailscale, so stopping the public edge proxy does not prevent +the next morning's start. `jeannie-server` and the edge VM remain running for +management; Kubernetes, Debian containers, RPi services, and the selected +profile runtime are stopped outside the window. + +Set the API token in the environment used by `./jeannie edge apply`, then apply +the edge stack: + +```bash +export LAB_LAB_SCHEDULE_API_TOKEN="$JEANNIE_SERVER_TOKEN" +./jeannie edge apply +ssh ubuntu@ 'systemctl list-timers homelab-lab-window-*' +ssh ubuntu@ 'sudo systemctl start homelab-lab-window-stop.service' +``` + +The schedule is every day by default. Disable it by setting +`TF_VAR_lab_schedule_enabled=false` and reapplying the edge stack. The manual +control remains available from Debian: + +```bash +./jeannie lab-window status +./jeannie lab-window start +./jeannie lab-window stop +``` + +The stop action deliberately leaves `jeannie-server` and the OCI edge alive; +otherwise the edge could not call the API for the next start. Review the first +manual stop before relying on the timer. + ## Storage OpenEBS provides the platform storage provisioner. Stateful Kubernetes apps use diff --git a/docs/templates/app-ops.md.tmpl b/docs/templates/app-ops.md.tmpl index e9efb2a..2150fd5 100644 --- a/docs/templates/app-ops.md.tmpl +++ b/docs/templates/app-ops.md.tmpl @@ -214,6 +214,40 @@ when Pimox workers or cluster networking are the thing being investigated. ./{{ main_script }} doctor-k8s ``` +## Scheduled lab power window + +The OCI edge can keep the management plane available while starting the +selected cluster and host services at 08:00 and stopping the data plane at +17:00 in `America/Mexico_City`. The edge timer calls the authenticated Jeannie +API directly over Tailscale, so stopping the public edge proxy does not prevent +the next morning's start. `jeannie-server` and the edge VM remain running for +management; Kubernetes, Debian containers, RPi services, and the selected +profile runtime are stopped outside the window. + +Set the API token in the environment used by `./{{ main_script }} edge apply`, +then apply the edge stack: + +```bash +export LAB_LAB_SCHEDULE_API_TOKEN="$JEANNIE_SERVER_TOKEN" +./{{ main_script }} edge apply +ssh ubuntu@ 'systemctl list-timers homelab-lab-window-*' +ssh ubuntu@ 'sudo systemctl start homelab-lab-window-stop.service' +``` + +The schedule is every day by default. Disable it by setting +`TF_VAR_lab_schedule_enabled=false` and reapplying the edge stack. Manual +control remains available from Debian: + +```bash +./{{ main_script }} lab-window status +./{{ main_script }} lab-window start +./{{ main_script }} lab-window stop +``` + +The stop action deliberately leaves `jeannie-server` and the OCI edge alive; +otherwise the edge could not call the API for the next start. Review the first +manual stop before relying on the timer. + ## Storage OpenEBS provides the platform storage provisioner. Stateful Kubernetes apps use diff --git a/jeannie b/jeannie index 4a86781..fa00c72 100755 --- a/jeannie +++ b/jeannie @@ -80,6 +80,7 @@ Build And Bootstrap deploy-ai-tools Install/wire local AI CLIs to the AI gateway. goose-graph [question] Explore the repo with Goose and Graphify. + lab-window Apply the scheduled lab power window. deploy-omniroute Deploy the OmniRoute LLM routing gateway. deploy-hermes Deploy the Hermes agent (git-install + venv + gateway service). hermes-connect Connect Hermes to read-only Jeannie status and runbooks. @@ -302,6 +303,9 @@ case "${1:-}" in goose-graph) python3 "${REPO_ROOT}/scripts/goose-graph.py" "${@:2}" ;; + lab-window) + lab_window "${@:2}" + ;; cluster-transition) python3 "${REPO_ROOT}/scripts/cluster-transition.py" "${@:2}" ;; diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 0557744..98d11ab 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -759,6 +759,36 @@ stop_cluster() { echo "Kubernetes runtime stopped. OpenTofu state, kubeadm files, PV data, and VM disks were left intact." } +lab_window() { + local action="${1:-status}" + case "${action}" in + status) + printf 'profile=%s runtime=%s\n' "${LAB_CLUSTER_PROFILE:-unset}" "${LAB_CLUSTER_RUNTIME:-unset}" + status_systemd_units docker jeannie-server.service + cluster_runtime_require + ;; + start) + require_debian_server "lab-window start" + echo "Starting the scheduled lab window for profile ${LAB_CLUSTER_PROFILE:-unset}..." + start_debian_services + start_rpi_services || return 1 + start_cluster || return 1 + ;; + stop) + require_debian_server "lab-window stop" + echo "Stopping the scheduled lab window; keeping jeannie-server and the OCI edge available." + shutdown_rpi_services || return 1 + stop_cluster || return 1 + shutdown_oci_email_services || return 1 + shutdown_debian_services + ;; + *) + echo "Usage: ./jeannie lab-window {start|stop|status}" >&2 + return 2 + ;; + esac +} + shutdown_remote_compose_stack() { local label="$1" local target="$2" diff --git a/lib/jeannie/env.sh b/lib/jeannie/env.sh index 392a6cc..22415b0 100644 --- a/lib/jeannie/env.sh +++ b/lib/jeannie/env.sh @@ -303,6 +303,8 @@ export_homelab_inventory_tf_vars() { export_if_unset TF_VAR_additional_server_names "${LAB_ADDITIONAL_SERVER_NAMES_JSON:-}" export_if_unset TF_VAR_jeannie_api_metrics_target "${LAB_JEANNIE_API_METRICS_TARGET:-api.${LAB_DOMAIN:-lab2025.duckdns.org}:443}" export_if_unset TF_VAR_jeannie_api_metrics_token "${JEANNIE_SERVER_TOKEN:-${JEANNIE_REMOTE_TOKEN:-}}" + export_if_unset TF_VAR_lab_schedule_api_url "${LAB_LAB_SCHEDULE_API_URL:-http://${LAB_DEBIAN_TAILSCALE_IP:-127.0.0.1}:8787}" + export_if_unset TF_VAR_lab_schedule_api_token "${LAB_LAB_SCHEDULE_API_TOKEN:-${JEANNIE_SERVER_TOKEN:-${JEANNIE_REMOTE_TOKEN:-}}}" export_if_unset TF_VAR_debian_host_metrics_target "${LAB_DEBIAN_LAN_IP:-192.168.100.73}:9100" export_if_unset TF_VAR_pimox_host_metrics_target "${LAB_PIMOX_HOST:-192.168.100.80}:9100" export_if_unset TF_VAR_rpi_host_metrics_target "${LAB_RPI_HOST:-192.168.100.89}:9100"