8d6a527a2b
Completes the workload-first refactor's plugin layer:
- internal/workload/plugin/ — Source/Trigger plugin contract,
registry, types (Workload, DeploymentIntent, InboundEvent,
PublicFace). Self-registering init() pattern + blank-import
in cmd/server/main.go.
- Source plugins: image (blue-green with multi-face proxy routing),
compose, static. Trigger plugins: registry, git, manual.
- internal/deployer/dispatch.go — DispatchPlugin/Teardown/Reconcile
seam routing the legacy deployer through plugins.
- internal/api/workload_*.go — REST surface: workloads, env,
volumes, chain (parent/children), promote-from. hooks.go
serves /api/hooks/kinds/{kind}/schema for the wizard.
- internal/store: workload_env (encrypt-at-rest secrets) and
workload_volumes tables, keyed on workload_id.
- cmd/server/static_backend.go — phantom-row adapter delegating
the static source plugin to the legacy staticsite.Manager
(deleted at hard cutover once the static inline port lands).
- web/src/routes/apps/ — /apps list + /apps/new wizard +
/apps/[id] detail with kind-aware compose / image / static
forms (Advanced JSON toggle), env panel, volumes panel,
webhook panel, chain panel, manual deploy.
Volume scope generalization (v2 resolver):
- internal/volume.ResolveWorkloadPath (workload-keyed, sits
next to legacy ResolvePath). Honors all VolumeScope values:
absolute, ephemeral, instance, stage, project, project_named,
named. internal/workload/plugin/source/image/image.go
computeMounts wires settings + imageTag through. Coverage in
internal/volume/resolver_test.go (portable Linux/Windows via
t.TempDir).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
283 lines
8.4 KiB
Go
283 lines
8.4 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const workloadColumns = `id, kind, ref_id, name, app_id,
|
|
source_kind, source_config, trigger_kind, trigger_config,
|
|
public_faces, parent_workload_id,
|
|
notification_url, notification_secret,
|
|
webhook_secret, webhook_signing_secret, webhook_require_signature,
|
|
created_at, updated_at`
|
|
|
|
func scanWorkload(scanner interface{ Scan(...any) error }) (Workload, error) {
|
|
var w Workload
|
|
err := scanner.Scan(
|
|
&w.ID, &w.Kind, &w.RefID, &w.Name, &w.AppID,
|
|
&w.SourceKind, &w.SourceConfig, &w.TriggerKind, &w.TriggerConfig,
|
|
&w.PublicFaces, &w.ParentWorkloadID,
|
|
&w.NotificationURL, &w.NotificationSecret,
|
|
&w.WebhookSecret, &w.WebhookSigningSecret, &w.WebhookRequireSignature,
|
|
&w.CreatedAt, &w.UpdatedAt,
|
|
)
|
|
return w, err
|
|
}
|
|
|
|
// CreateWorkload inserts a new workload row. The (Kind, RefID) pair must be
|
|
// unique; the caller is responsible for matching this to a project/stack/site.
|
|
func (s *Store) CreateWorkload(w Workload) (Workload, error) {
|
|
if w.ID == "" {
|
|
w.ID = uuid.New().String()
|
|
}
|
|
w.CreatedAt = Now()
|
|
w.UpdatedAt = w.CreatedAt
|
|
|
|
if w.SourceConfig == "" {
|
|
w.SourceConfig = "{}"
|
|
}
|
|
if w.TriggerConfig == "" {
|
|
w.TriggerConfig = "{}"
|
|
}
|
|
if w.PublicFaces == "" {
|
|
w.PublicFaces = "[]"
|
|
}
|
|
_, err := s.db.Exec(
|
|
`INSERT INTO workloads (`+workloadColumns+`)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
w.ID, w.Kind, w.RefID, w.Name, w.AppID,
|
|
w.SourceKind, w.SourceConfig, w.TriggerKind, w.TriggerConfig,
|
|
w.PublicFaces, w.ParentWorkloadID,
|
|
w.NotificationURL, w.NotificationSecret,
|
|
w.WebhookSecret, w.WebhookSigningSecret, BoolToInt(w.WebhookRequireSignature),
|
|
w.CreatedAt, w.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return Workload{}, fmt.Errorf("insert workload: %w", err)
|
|
}
|
|
return w, nil
|
|
}
|
|
|
|
// GetWorkloadByID returns a single workload by its ID.
|
|
func (s *Store) GetWorkloadByID(id string) (Workload, error) {
|
|
w, err := scanWorkload(s.db.QueryRow(
|
|
`SELECT `+workloadColumns+` FROM workloads WHERE id = ?`, id,
|
|
))
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Workload{}, fmt.Errorf("workload %s: %w", id, ErrNotFound)
|
|
}
|
|
if err != nil {
|
|
return Workload{}, fmt.Errorf("query workload: %w", err)
|
|
}
|
|
return w, nil
|
|
}
|
|
|
|
// GetWorkloadByRef returns the workload paired with a given (kind, ref_id).
|
|
// Returns ErrNotFound if the project/stack/site has no workload row yet
|
|
// (which means the boot-time backfill hasn't run, or the kind/ref pair is wrong).
|
|
func (s *Store) GetWorkloadByRef(kind WorkloadKind, refID string) (Workload, error) {
|
|
w, err := scanWorkload(s.db.QueryRow(
|
|
`SELECT `+workloadColumns+` FROM workloads WHERE kind = ? AND ref_id = ?`,
|
|
string(kind), refID,
|
|
))
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Workload{}, fmt.Errorf("workload (%s,%s): %w", kind, refID, ErrNotFound)
|
|
}
|
|
if err != nil {
|
|
return Workload{}, fmt.Errorf("query workload by ref: %w", err)
|
|
}
|
|
return w, nil
|
|
}
|
|
|
|
// GetWorkloadByWebhookSecret looks up a workload by its inbound webhook URL secret.
|
|
// Returns ErrNotFound when no match — used by the webhook router.
|
|
func (s *Store) GetWorkloadByWebhookSecret(secret string) (Workload, error) {
|
|
if secret == "" {
|
|
return Workload{}, fmt.Errorf("empty secret: %w", ErrNotFound)
|
|
}
|
|
w, err := scanWorkload(s.db.QueryRow(
|
|
`SELECT `+workloadColumns+` FROM workloads WHERE webhook_secret = ?`, secret,
|
|
))
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Workload{}, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return Workload{}, fmt.Errorf("query workload by webhook secret: %w", err)
|
|
}
|
|
return w, nil
|
|
}
|
|
|
|
// ListWorkloads returns all workloads, optionally filtered by kind. Pass
|
|
// empty string to get every workload regardless of kind.
|
|
func (s *Store) ListWorkloads(kind WorkloadKind) ([]Workload, error) {
|
|
var rows *sql.Rows
|
|
var err error
|
|
if kind == "" {
|
|
rows, err = s.db.Query(
|
|
`SELECT ` + workloadColumns + ` FROM workloads ORDER BY name`,
|
|
)
|
|
} else {
|
|
rows, err = s.db.Query(
|
|
`SELECT `+workloadColumns+` FROM workloads WHERE kind = ? ORDER BY name`,
|
|
string(kind),
|
|
)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query workloads: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []Workload{}
|
|
for rows.Next() {
|
|
w, err := scanWorkload(rows)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scan workload: %w", err)
|
|
}
|
|
out = append(out, w)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// UpdateWorkload updates the mutable fields of a workload (name, app_id,
|
|
// source/trigger config, public faces, parent chain, notification + webhook
|
|
// config). Kind and RefID are immutable post-create.
|
|
func (s *Store) UpdateWorkload(w Workload) error {
|
|
w.UpdatedAt = Now()
|
|
if w.SourceConfig == "" {
|
|
w.SourceConfig = "{}"
|
|
}
|
|
if w.TriggerConfig == "" {
|
|
w.TriggerConfig = "{}"
|
|
}
|
|
if w.PublicFaces == "" {
|
|
w.PublicFaces = "[]"
|
|
}
|
|
result, err := s.db.Exec(
|
|
`UPDATE workloads SET name=?, app_id=?,
|
|
source_kind=?, source_config=?, trigger_kind=?, trigger_config=?,
|
|
public_faces=?, parent_workload_id=?,
|
|
notification_url=?, notification_secret=?,
|
|
webhook_secret=?, webhook_signing_secret=?, webhook_require_signature=?,
|
|
updated_at=?
|
|
WHERE id=?`,
|
|
w.Name, w.AppID,
|
|
w.SourceKind, w.SourceConfig, w.TriggerKind, w.TriggerConfig,
|
|
w.PublicFaces, w.ParentWorkloadID,
|
|
w.NotificationURL, w.NotificationSecret,
|
|
w.WebhookSecret, w.WebhookSigningSecret, BoolToInt(w.WebhookRequireSignature),
|
|
w.UpdatedAt, w.ID,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("update workload: %w", err)
|
|
}
|
|
n, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("rows affected: %w", err)
|
|
}
|
|
if n == 0 {
|
|
return fmt.Errorf("workload %s: %w", w.ID, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteWorkload removes a workload row. Cascading deletes for the matching
|
|
// project/stack/site row stay with the kind-specific Delete functions; this
|
|
// only removes the workload entry.
|
|
func (s *Store) DeleteWorkload(id string) error {
|
|
result, err := s.db.Exec(`DELETE FROM workloads WHERE id = ?`, id)
|
|
if err != nil {
|
|
return fmt.Errorf("delete workload: %w", err)
|
|
}
|
|
n, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("rows affected: %w", err)
|
|
}
|
|
if n == 0 {
|
|
return fmt.Errorf("workload %s: %w", id, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListChildrenByParent returns every workload whose parent_workload_id
|
|
// equals the given id. Used to render the stages chain ("dev → staging
|
|
// → prod") on /apps/[id] without forcing a separate stages table.
|
|
//
|
|
// Returns rows ordered by name for a stable UI.
|
|
func (s *Store) ListChildrenByParent(parentID string) ([]Workload, error) {
|
|
if parentID == "" {
|
|
return []Workload{}, nil
|
|
}
|
|
rows, err := s.db.Query(
|
|
`SELECT `+workloadColumns+` FROM workloads WHERE parent_workload_id = ? ORDER BY name`,
|
|
parentID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query workload children: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []Workload{}
|
|
for rows.Next() {
|
|
w, err := scanWorkload(rows)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scan child workload: %w", err)
|
|
}
|
|
out = append(out, w)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// SetWorkloadWebhookSecret rotates the inbound webhook URL secret. Pass
|
|
// empty to disable inbound webhooks for this workload.
|
|
func (s *Store) SetWorkloadWebhookSecret(id, secret string) error {
|
|
result, err := s.db.Exec(
|
|
`UPDATE workloads SET webhook_secret=?, updated_at=? WHERE id=?`,
|
|
secret, Now(), id,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("update workload webhook_secret: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("workload %s: %w", id, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EnsureWorkloadWebhookSecret returns the current secret, generating one
|
|
// lazily for workloads that predate the column. Mirrors the project /
|
|
// site equivalents.
|
|
func (s *Store) EnsureWorkloadWebhookSecret(id string) (string, error) {
|
|
w, err := s.GetWorkloadByID(id)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if w.WebhookSecret != "" {
|
|
return w.WebhookSecret, nil
|
|
}
|
|
secret := generateWebhookSecret()
|
|
if err := s.SetWorkloadWebhookSecret(id, secret); err != nil {
|
|
return "", err
|
|
}
|
|
return secret, nil
|
|
}
|
|
|
|
// DeleteWorkloadByRef removes the workload paired with a given (kind, ref_id).
|
|
// Idempotent — returns nil if no row exists, since the kind-specific Delete
|
|
// callers don't always know whether a workload row was created.
|
|
func (s *Store) DeleteWorkloadByRef(kind WorkloadKind, refID string) error {
|
|
_, err := s.db.Exec(
|
|
`DELETE FROM workloads WHERE kind = ? AND ref_id = ?`,
|
|
string(kind), refID,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("delete workload by ref: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|