Schedule homelab power window from OCI edge

This commit is contained in:
juvenal.diaz 2026-11-05 08:24:54 -06:00
parent 20b005be8d
commit 1adf33b739
8 changed files with 205 additions and 0 deletions

View File

@ -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 <<EOF
LAB_WINDOW_API_URL=$lab_schedule_api_url
LAB_WINDOW_API_TOKEN=$lab_schedule_api_token
EOF
sudo chmod 0600 /etc/homelab-lab-window.env
sudo tee /usr/local/sbin/homelab-lab-window >/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 <<SERVICE_EOT
[Unit]
Description=Start Jeannie lab at 08:00 local time
[Timer]
OnCalendar=*-*-* 08:00:00 ${lab_schedule_timezone}
Persistent=true
Unit=homelab-lab-window-start.service
[Install]
WantedBy=timers.target
SERVICE_EOT
sudo tee /etc/systemd/system/homelab-lab-window-stop.timer >/dev/null <<SERVICE_EOT
[Unit]
Description=Stop Jeannie lab at 17:00 local time
[Timer]
OnCalendar=*-*-* 17:00:00 ${lab_schedule_timezone}
Persistent=true
Unit=homelab-lab-window-stop.service
[Install]
WantedBy=timers.target
SERVICE_EOT
sudo systemctl daemon-reload
sudo systemctl enable --now homelab-lab-window-start.timer homelab-lab-window-stop.timer
else
sudo systemctl disable --now homelab-lab-window-start.timer homelab-lab-window-stop.timer 2>/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

View File

@ -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 = ""

View File

@ -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,

View File

@ -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@<edge-host> 'systemctl list-timers homelab-lab-window-*'
ssh ubuntu@<edge-host> '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

View File

@ -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@<edge-host> 'systemctl list-timers homelab-lab-window-*'
ssh ubuntu@<edge-host> '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

View File

@ -80,6 +80,7 @@ Build And Bootstrap
deploy-ai-tools Install/wire local AI CLIs to the AI gateway.
goose-graph <update|context|learn|propose> [question]
Explore the repo with Goose and Graphify.
lab-window <start|stop|status> 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}"
;;

View File

@ -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"

View File

@ -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"