diff --git a/cmd/jeannie-server/main.go b/cmd/jeannie-server/main.go index 180c4d6..ff3768a 100644 --- a/cmd/jeannie-server/main.go +++ b/cmd/jeannie-server/main.go @@ -15,6 +15,9 @@ import ( "strings" "sync" "time" + + "go.temporal.io/sdk/client" + "my-homelab-configs/internal/jeannieworkflow" ) const maxOutputBytes = 1 << 20 @@ -49,6 +52,7 @@ type server struct { exec map[string]*execution repoRoot string token string + temporal client.Client } func main() { @@ -58,6 +62,19 @@ func main() { } root, _ = filepath.Abs(root) s := &server{exec: make(map[string]*execution), repoRoot: root, token: os.Getenv("JEANNIE_SERVER_TOKEN")} + if os.Getenv("JEANNIE_EXECUTION_MODE") == "temporal" { + address := os.Getenv("JEANNIE_TEMPORAL_ADDRESS") + if address == "" { + address = "127.0.0.1:7233" + } + var err error + s.temporal, err = client.Dial(client.Options{HostPort: address}) + if err != nil { + fmt.Fprintf(os.Stderr, "connect to Temporal: %v\n", err) + os.Exit(2) + } + defer s.temporal.Close() + } mux := http.NewServeMux() mux.HandleFunc("/v1/health", s.auth(s.health)) mux.HandleFunc("/v1/commands", s.auth(s.commands)) @@ -134,6 +151,10 @@ func (s *server) execute(w http.ResponseWriter, r *http.Request) { } func (s *server) run(item *execution) { + if s.temporal != nil { + s.runTemporal(item) + return + } s.update(item.ID, func(item *execution) { item.Status = "running" }) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute) defer cancel() @@ -158,6 +179,48 @@ func (s *server) run(item *execution) { }) } +func (s *server) runTemporal(item *execution) { + s.update(item.ID, func(item *execution) { item.Status = "running" }) + taskQueue := os.Getenv("JEANNIE_TEMPORAL_TASK_QUEUE") + if taskQueue == "" { + taskQueue = jeannieworkflow.TaskQueue + } + workflowID := "jeannie-" + item.ID + workflowRun, err := s.temporal.ExecuteWorkflow(context.Background(), client.StartWorkflowOptions{ + ID: workflowID, + TaskQueue: taskQueue, + }, jeannieworkflow.Workflow, jeannieworkflow.CommandInput{ + RepoRoot: s.repoRoot, + Command: item.Command, + Args: item.Args, + }) + if err != nil { + s.finish(item.ID, "", 1, err) + return + } + var result jeannieworkflow.CommandResult + err = workflowRun.Get(context.Background(), &result) + if err != nil { + s.finish(item.ID, result.Output, 1, err) + return + } + if result.ExitCode != 0 { + s.finish(item.ID, result.Output, result.ExitCode, fmt.Errorf("remote command exited with status %d", result.ExitCode)) + return + } + s.finish(item.ID, result.Output, 0, nil) +} + +func (s *server) finish(id, output string, exitCode int, err error) { + ended := time.Now().UTC() + s.update(id, func(item *execution) { + item.Output, item.EndedAt, item.Status, item.ExitCode = redact(output), &ended, "succeeded", exitCode + if err != nil { + item.Status, item.Error = "failed", err.Error() + } + }) +} + func (s *server) execution(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"}) diff --git a/cmd/jeannie-temporal-worker/main.go b/cmd/jeannie-temporal-worker/main.go new file mode 100644 index 0000000..2d88d20 --- /dev/null +++ b/cmd/jeannie-temporal-worker/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "os" + + "go.temporal.io/sdk/client" + "go.temporal.io/sdk/worker" + "my-homelab-configs/internal/jeannieworkflow" +) + +func main() { + address := os.Getenv("JEANNIE_TEMPORAL_ADDRESS") + if address == "" { + address = "127.0.0.1:7233" + } + taskQueue := os.Getenv("JEANNIE_TEMPORAL_TASK_QUEUE") + if taskQueue == "" { + taskQueue = jeannieworkflow.TaskQueue + } + c, err := client.Dial(client.Options{HostPort: address}) + if err != nil { + panic(fmt.Errorf("connect to Temporal: %w", err)) + } + defer c.Close() + w := worker.New(c, taskQueue, worker.Options{}) + w.RegisterWorkflow(jeannieworkflow.Workflow) + w.RegisterActivity((*jeannieworkflow.CommandActivity).Run) + if err := w.Run(worker.InterruptCh()); err != nil { + panic(err) + } +} diff --git a/go.mod b/go.mod index 2894bd8..0ed259c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module my-homelab-configs go 1.23 + +require go.temporal.io/sdk v1.25.1 diff --git a/infra/jeannie-temporal/deploy-remote.sh b/infra/jeannie-temporal/deploy-remote.sh new file mode 100644 index 0000000..4ff3842 --- /dev/null +++ b/infra/jeannie-temporal/deploy-remote.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +set -euo pipefail + +install_dir="${JEANNIE_PHASE2_INSTALL_DIR:-/opt/jeannie}" +repo_root="${JEANNIE_PHASE2_REPO_ROOT:-${install_dir}/repo}" +server_addr="${JEANNIE_SERVER_ADDR:-0.0.0.0:8787}" +server_token="${JEANNIE_SERVER_TOKEN:?JEANNIE_SERVER_TOKEN is required}" + +command -v docker >/dev/null 2>&1 || { echo 'Docker is required on Debian' >&2; exit 1; } +command -v go >/dev/null 2>&1 || { echo 'Go is required on Debian' >&2; exit 1; } + +sudo install -d -m 0755 "${install_dir}" "${repo_root}" /etc/jeannie +sudo cp infra/jeannie-temporal/docker-compose.yml "${install_dir}/docker-compose.yml" +sudo cp -R . "${repo_root}/" +sudo chown -R "$(id -u):$(id -g)" "${repo_root}" + +cd "${repo_root}" +GOTOOLCHAIN=local go mod download +go build -o "${install_dir}/jeannie-server" ./cmd/jeannie-server +go build -o "${install_dir}/jeannie-temporal-worker" ./cmd/jeannie-temporal-worker + +sudo tee /etc/jeannie/temporal.env >/dev/null </dev/null </dev/null <&2 + return 1 + fi + + if [[ "$(uname -s)" == "Linux" ]] && [[ -r /etc/os-release ]] && + grep -q '^ID=debian$' /etc/os-release; then + JEANNIE_SERVER_TOKEN="${server_token}" bash "${REPO_ROOT}/infra/jeannie-temporal/deploy-remote.sh" + return 0 + fi + + quoted_token="$(printf '%q' "${server_token}")" + echo "Staging the Jeannie API on ${target}..." + tar -czf - cmd internal infra go.mod jeannie lib scripts homelab.yml | + ssh -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${target}" \ + "rm -rf '${staging}' && mkdir -p '${staging}' && tar -xzf - -C '${staging}'" + ssh -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${target}" \ + "cd '${staging}' && JEANNIE_SERVER_TOKEN=${quoted_token} bash infra/jeannie-temporal/deploy-remote.sh" + echo "Jeannie API deployed on ${target}." +} + deploy_heimdall() { local mode="${LAB_HEIMDALL_DEPLOY:-true}" local install_dir="${LAB_HEIMDALL_INSTALL_DIR:-/data/homelab-heimdall}" @@ -7932,6 +7959,11 @@ spec = { "title": "Gitea deploy", "command": [jeannie, "__up-task", "gitea-deploy"], }, + { + "id": "api-deploy", + "title": "Jeannie API deploy", + "command": [jeannie, "__up-task", "api-deploy"], + }, { "id": "gitea-bootstrap", "title": "Gitea repo bootstrap", @@ -8020,6 +8052,9 @@ up_task() { gitea-deploy) deploy_gitea ;; + api-deploy) + deploy_api + ;; gitea-bootstrap) bootstrap_gitea_repo ;;