feat: deploy Jeannie API through pipeline
This commit is contained in:
parent
53f769ee9a
commit
ef4eee8162
|
|
@ -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"})
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
|||
module my-homelab-configs
|
||||
|
||||
go 1.23
|
||||
|
||||
require go.temporal.io/sdk v1.25.1
|
||||
|
|
|
|||
|
|
@ -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 <<EOF
|
||||
JEANNIE_SERVER_ADDR=${server_addr}
|
||||
JEANNIE_SERVER_TOKEN=${server_token}
|
||||
JEANNIE_SERVER_REPO_ROOT=${repo_root}
|
||||
JEANNIE_EXECUTION_MODE=temporal
|
||||
JEANNIE_TEMPORAL_ADDRESS=127.0.0.1:7233
|
||||
JEANNIE_TEMPORAL_TASK_QUEUE=jeannie
|
||||
EOF
|
||||
sudo chown root:"$(id -gn)" /etc/jeannie/temporal.env
|
||||
sudo chmod 640 /etc/jeannie/temporal.env
|
||||
|
||||
sudo tee /etc/systemd/system/jeannie-server.service >/dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Jeannie remote API
|
||||
After=network-online.target docker.service
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
User=$(id -un)
|
||||
WorkingDirectory=${repo_root}
|
||||
EnvironmentFile=/etc/jeannie/temporal.env
|
||||
ExecStart=${install_dir}/jeannie-server
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
sudo tee /etc/systemd/system/jeannie-temporal-worker.service >/dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Jeannie Temporal worker
|
||||
After=network-online.target docker.service
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
User=$(id -un)
|
||||
WorkingDirectory=${repo_root}
|
||||
EnvironmentFile=/etc/jeannie/temporal.env
|
||||
ExecStart=${install_dir}/jeannie-temporal-worker
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
sudo docker compose -f "${install_dir}/docker-compose.yml" up -d
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now jeannie-temporal-worker.service jeannie-server.service
|
||||
sudo systemctl --no-pager --full status jeannie-temporal-worker.service jeannie-server.service
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
services:
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_USER: temporal
|
||||
POSTGRES_PASSWORD: temporal
|
||||
POSTGRES_DB: temporal
|
||||
volumes:
|
||||
- temporal-postgres:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U temporal -d temporal"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 12
|
||||
|
||||
temporal:
|
||||
image: temporalio/auto-setup:1.25.0
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
DB: postgres12
|
||||
DB_PORT: 5432
|
||||
POSTGRES_USER: temporal
|
||||
POSTGRES_PWD: temporal
|
||||
POSTGRES_SEEDS: postgres
|
||||
ports:
|
||||
- "127.0.0.1:7233:7233"
|
||||
|
||||
temporal-ui:
|
||||
image: temporalio/ui:2.34.0
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
- temporal
|
||||
environment:
|
||||
TEMPORAL_ADDRESS: temporal:7233
|
||||
ports:
|
||||
- "127.0.0.1:8088:8080"
|
||||
|
||||
volumes:
|
||||
temporal-postgres:
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package jeannieworkflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"go.temporal.io/sdk/temporal"
|
||||
"go.temporal.io/sdk/workflow"
|
||||
)
|
||||
|
||||
const (
|
||||
TaskQueue = "jeannie"
|
||||
WorkflowName = "JeannieCommandWorkflow"
|
||||
)
|
||||
|
||||
type CommandInput struct {
|
||||
RepoRoot string
|
||||
Command string
|
||||
Args []string
|
||||
}
|
||||
|
||||
type CommandResult struct {
|
||||
Output string
|
||||
ExitCode int
|
||||
}
|
||||
|
||||
type CommandActivity struct{}
|
||||
|
||||
func Workflow(ctx workflow.Context, input CommandInput) (CommandResult, error) {
|
||||
options := workflow.ActivityOptions{
|
||||
StartToCloseTimeout: 30 * time.Minute,
|
||||
RetryPolicy: &temporal.RetryPolicy{
|
||||
MaximumAttempts: 1,
|
||||
},
|
||||
}
|
||||
ctx = workflow.WithActivityOptions(ctx, options)
|
||||
var result CommandResult
|
||||
if err := workflow.ExecuteActivity(ctx, (*CommandActivity).Run, input).Get(ctx, &result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *CommandActivity) Run(ctx context.Context, input CommandInput) (CommandResult, error) {
|
||||
args := append([]string{input.Command}, input.Args...)
|
||||
command := exec.CommandContext(ctx, filepath.Join(input.RepoRoot, "jeannie"), args...)
|
||||
command.Dir = input.RepoRoot
|
||||
command.Env = append(os.Environ(), "JEANNIE_REMOTE_SERVER=true")
|
||||
output, err := command.CombinedOutput()
|
||||
result := CommandResult{Output: string(output)}
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
result.ExitCode = exitErr.ExitCode()
|
||||
return result, nil
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
4
jeannie
4
jeannie
|
|
@ -53,6 +53,7 @@ Base, Inventory, And Planning
|
|||
Build And Bootstrap
|
||||
up Deploy the full homelab pipeline.
|
||||
deploy-gitea Deploy the Debian-hosted Gitea container.
|
||||
deploy-api Deploy the Temporal-backed Jeannie API on Debian.
|
||||
deploy-heimdall Deploy the Debian-hosted Heimdall dashboard.
|
||||
deploy-dex Deploy the Debian-hosted Dex SSO stack.
|
||||
deploy-matrix Deploy the Matrix homeserver (Synapse + Element + Postgres).
|
||||
|
|
@ -396,6 +397,9 @@ case "${1:-}" in
|
|||
record_change_journal "deploy-gitea" "$@"
|
||||
deploy_gitea
|
||||
;;
|
||||
deploy-api)
|
||||
deploy_api
|
||||
;;
|
||||
deploy-heimdall)
|
||||
record_change_journal "deploy-heimdall" "$@"
|
||||
deploy_heimdall
|
||||
|
|
|
|||
|
|
@ -5116,6 +5116,33 @@ repair_local_docker_iptables() {
|
|||
exit 1
|
||||
}
|
||||
|
||||
deploy_api() {
|
||||
local target="${JEANNIE_SSH_TARGET:-debian-lab}"
|
||||
local staging="${JEANNIE_API_STAGING_DIR:-/tmp/jeannie-api}"
|
||||
local server_token="${JEANNIE_SERVER_TOKEN:-${JEANNIE_REMOTE_TOKEN:-}}"
|
||||
local quoted_token
|
||||
|
||||
if [[ -z "${server_token}" ]]; then
|
||||
echo "Set JEANNIE_SERVER_TOKEN before deploying the Jeannie API." >&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
|
||||
;;
|
||||
|
|
|
|||
Loading…
Reference in New Issue