mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-10-28 15:51:26 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
401daf69aa | ||
|
|
ae7e6b4991 | ||
|
|
ee43064c5c | ||
|
|
e054457156 | ||
|
|
3f066b3b1e | ||
|
|
7547375cc2 | ||
|
|
d4a4cde1fd | ||
|
|
8cfe2ca826 | ||
|
|
612520bbff | ||
|
|
5eac4a382b | ||
|
|
00e69b3733 | ||
|
|
50e39fe140 | ||
|
|
b47ced0135 | ||
|
|
9408464f12 | ||
|
|
954a1d1f80 | ||
|
|
cf7afd2ba7 | ||
|
|
f524d95894 | ||
|
|
f499678964 | ||
|
|
64cbf82dd8 | ||
|
|
5c2f2861bd | ||
|
|
dca8aaa7b3 | ||
|
|
6946357a7b | ||
|
|
a0952faf25 | ||
|
|
999d719377 | ||
|
|
8d682583ff | ||
|
|
c80fbc766d | ||
|
|
99ee4e79cf | ||
|
|
f729ef2c82 | ||
|
|
f90fc5f5ad | ||
|
|
f68225a97d | ||
|
|
7536443610 | ||
|
|
68da4236fa | ||
|
|
b9441f3e11 | ||
|
|
605a43455d | ||
|
|
c7256954c2 | ||
|
|
a50260e857 | ||
|
|
3441acbc37 | ||
|
|
c5e0561add | ||
|
|
1ef5ea5f7f | ||
|
|
d26452d97e | ||
|
|
164a39c9c6 | ||
|
|
63f65c78cc | ||
|
|
d5960fdfff | ||
|
|
31c04ad51d | ||
|
|
03f6dcc80d | ||
|
|
1a4ee99f5b | ||
|
|
264f619be5 | ||
|
|
11525194b0 | ||
|
|
9e3994e937 | ||
|
|
355eb2c9ab | ||
|
|
b5a1a8cc21 | ||
|
|
91f40a652d | ||
|
|
2be1715015 | ||
|
|
b32e63368a | ||
|
|
f1a0f2189d | ||
|
|
e3676bd89a | ||
|
|
71c7d2b9f0 | ||
|
|
e67a39436c | ||
|
|
2c3d9fc123 | ||
|
|
4734be0d9d | ||
|
|
4e8d364911 | ||
|
|
f53695be4f | ||
|
|
925199633b | ||
|
|
1054679ca2 | ||
|
|
57f8b4f56e | ||
|
|
ace68da188 | ||
|
|
2d626f8ee8 | ||
|
|
6d3aff6d58 | ||
|
|
c60c31693b | ||
|
|
9fd34482e3 | ||
|
|
541a4719d4 | ||
|
|
dd1bbaf5a2 | ||
|
|
f22a464ad0 | ||
|
|
58424ed0ab | ||
|
|
25fa8c47e6 | ||
|
|
b3da014d26 | ||
|
|
f5f1ff34a0 | ||
|
|
bac317e0e6 | ||
|
|
26d585ffb7 | ||
|
|
f0df61b52e | ||
|
|
ffe66d7b3d | ||
|
|
121b33f36a | ||
|
|
8a8b903cdf | ||
|
|
f83ad81f73 | ||
|
|
2049087ccd | ||
|
|
01edb677e5 | ||
|
|
be83467cc9 | ||
|
|
c99b2df419 | ||
|
|
a26c103eb4 | ||
|
|
9f8eaed386 | ||
|
|
3360eedc29 | ||
|
|
79fb73afc8 | ||
|
|
99a25cb10e | ||
|
|
d135e5fd4b | ||
|
|
dbc87bf34c | ||
|
|
b4baa853b2 | ||
|
|
15045b8dd3 | ||
|
|
109f79268f | ||
|
|
6f43bbf8f6 | ||
|
|
b7131965b1 | ||
|
|
5dc9b5f115 | ||
|
|
3664eaed1a | ||
|
|
912f6f5142 |
No files matched your search
@@ -39,10 +39,20 @@ REDIS_URL=redis://:your_secure_redis_password@localhost:6379/0
|
|||||||
# Application Configuration
|
# Application Configuration
|
||||||
# In production this must be a strong value with at least 32 characters.
|
# In production this must be a strong value with at least 32 characters.
|
||||||
JWT_SECRET=SuperSecretJWTKey123456789!@#$%^&*()
|
JWT_SECRET=SuperSecretJWTKey123456789!@#$%^&*()
|
||||||
|
# Encrypts secret env vars at rest. Defaults to JWT_SECRET when unset;
|
||||||
|
# setting it explicitly survives a JWT_SECRET rotation.
|
||||||
|
SECRETS_KEY=
|
||||||
|
|
||||||
# Shared secret for node-agent registration/heartbeat auth.
|
# Shared secret for node-agent registration/heartbeat auth.
|
||||||
CONTAINR_AGENT_AUTH_TOKEN=replace_with_strong_agent_secret
|
CONTAINR_AGENT_AUTH_TOKEN=replace_with_strong_agent_secret
|
||||||
# Optional rotation list (comma-separated). When set, this takes precedence.
|
# Optional rotation list (comma-separated). When set, this takes precedence.
|
||||||
# CONTAINR_AGENT_AUTH_TOKENS=current_secret,next_secret
|
# CONTAINR_AGENT_AUTH_TOKENS=current_secret,next_secret
|
||||||
|
# Bundled agent binaries dir served at /api/agents/download/:platform.
|
||||||
|
# CONTAINR_AGENT_BIN_DIR=/agents
|
||||||
|
# Optional remote template catalog (another Containr instance). Its
|
||||||
|
# templates merge into GET /templates as source:"registry" and
|
||||||
|
# materialize locally on deploy. The bundled catalog works without it.
|
||||||
|
# CONTAINR_TEMPLATE_REGISTRY_URL=https://templates.example.com
|
||||||
CORS_ORIGINS=http://localhost:3000,http://localhost:8080
|
CORS_ORIGINS=http://localhost:3000,http://localhost:8080
|
||||||
# Optional build-time API URL overrides. Leave empty for the default
|
# Optional build-time API URL overrides. Leave empty for the default
|
||||||
# same-origin setup - the frontend nginx proxies /api to the backend, which
|
# same-origin setup - the frontend nginx proxies /api to the backend, which
|
||||||
@@ -155,3 +165,6 @@ DOCKER_GID=0
|
|||||||
|
|
||||||
# Optional: trust reverse proxy CIDR (default local Docker bridge used by Traefik)
|
# Optional: trust reverse proxy CIDR (default local Docker bridge used by Traefik)
|
||||||
TRUSTED_PROXY_CIDR=172.20.0.0/16
|
TRUSTED_PROXY_CIDR=172.20.0.0/16
|
||||||
|
|
||||||
|
# Optional: audit log retention in days (default 90; 0 disables pruning)
|
||||||
|
# AUDIT_RETENTION_DAYS=90
|
||||||
@@ -17,6 +17,30 @@ permissions:
|
|||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
# Cheap gate so docs-only/roadmap PRs skip the heavyweight jobs. Skipped
|
||||||
|
# required checks still satisfy branch protection ("skipped" = success).
|
||||||
|
changes:
|
||||||
|
name: Changed paths
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
outputs:
|
||||||
|
code: ${{ steps.filter.outputs.code }}
|
||||||
|
mobile: ${{ steps.filter.outputs.mobile }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: dorny/paths-filter@v3
|
||||||
|
id: filter
|
||||||
|
with:
|
||||||
|
filters: |
|
||||||
|
code:
|
||||||
|
- 'app/**'
|
||||||
|
- 'infra/**'
|
||||||
|
- 'docker-compose.yml'
|
||||||
|
- 'install.sh'
|
||||||
|
- '.github/workflows/**'
|
||||||
|
- 'docs/api/**'
|
||||||
|
mobile:
|
||||||
|
- 'app/mobile/**'
|
||||||
|
|
||||||
infra:
|
infra:
|
||||||
name: Infra (Compose + Scripts)
|
name: Infra (Compose + Scripts)
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -48,6 +72,8 @@ jobs:
|
|||||||
backend:
|
backend:
|
||||||
name: Backend (Go)
|
name: Backend (Go)
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
needs: changes
|
||||||
|
if: needs.changes.outputs.code == 'true' || github.event_name != 'pull_request'
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
working-directory: app/backend
|
working-directory: app/backend
|
||||||
@@ -70,6 +96,8 @@ jobs:
|
|||||||
frontend:
|
frontend:
|
||||||
name: Frontend (React)
|
name: Frontend (React)
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
needs: changes
|
||||||
|
if: needs.changes.outputs.code == 'true' || github.event_name != 'pull_request'
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
working-directory: app/frontend
|
working-directory: app/frontend
|
||||||
@@ -96,10 +124,40 @@ jobs:
|
|||||||
- name: Test
|
- name: Test
|
||||||
run: npm test
|
run: npm test
|
||||||
|
|
||||||
|
mobile:
|
||||||
|
name: Mobile (Expo)
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: changes
|
||||||
|
if: needs.changes.outputs.mobile == 'true' || github.event_name != 'pull_request'
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: app/mobile
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Node
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: "22"
|
||||||
|
cache: npm
|
||||||
|
cache-dependency-path: app/mobile/package-lock.json
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
- name: Typecheck
|
||||||
|
run: npx tsc --noEmit
|
||||||
|
|
||||||
|
- name: Lint
|
||||||
|
run: npx expo lint
|
||||||
|
|
||||||
smoke:
|
smoke:
|
||||||
name: Compose boot smoke
|
name: Compose boot smoke
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 20
|
timeout-minutes: 20
|
||||||
|
needs: changes
|
||||||
|
if: needs.changes.outputs.code == 'true' || github.event_name != 'pull_request'
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -137,6 +195,7 @@ jobs:
|
|||||||
name: Build & publish images
|
name: Build & publish images
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [backend, frontend, smoke]
|
needs: [backend, frontend, smoke]
|
||||||
|
if: needs.backend.result == 'success' && needs.frontend.result == 'success' && needs.smoke.result == 'success'
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
packages: write
|
packages: write
|
||||||
@@ -189,6 +248,10 @@ jobs:
|
|||||||
push: ${{ github.event_name != 'pull_request' }}
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
tags: ${{ steps.meta-backend.outputs.tags }}
|
tags: ${{ steps.meta-backend.outputs.tags }}
|
||||||
labels: ${{ steps.meta-backend.outputs.labels }}
|
labels: ${{ steps.meta-backend.outputs.labels }}
|
||||||
|
# Stamp the release tag into the binary so the in-app upgrade
|
||||||
|
# check knows what it's running ("dev" disables it).
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ github.ref_type == 'tag' && github.ref_name || 'dev' }}
|
||||||
cache-from: type=gha,scope=backend
|
cache-from: type=gha,scope=backend
|
||||||
cache-to: type=gha,mode=max,scope=backend
|
cache-to: type=gha,mode=max,scope=backend
|
||||||
|
|
||||||
@@ -227,7 +290,7 @@ jobs:
|
|||||||
name: Release
|
name: Release
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [docker]
|
needs: [docker]
|
||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request' && needs.docker.result == 'success'
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
packages: write
|
packages: write
|
||||||
|
|||||||
+220
-120
@@ -86,15 +86,15 @@ Legend: ✅ done · 🟡 partial/different · ❌ missing · ➖ not applicable
|
|||||||
| Multi-tenant organisations + custom RBAC (per-resource CRUD, create-limits, read-scope) | 🟡 `is_admin` + `project_members` | Defer full RBAC; design PAT scopes role-compatible |
|
| Multi-tenant organisations + custom RBAC (per-resource CRUD, create-limits, read-scope) | 🟡 `is_admin` + `project_members` | Defer full RBAC; design PAT scopes role-compatible |
|
||||||
| Team invite links/emails | 🟡 manual user create only | `POST /invites` → link join |
|
| Team invite links/emails | 🟡 manual user create only | `POST /invites` → link join |
|
||||||
| User API keys → full REST API | ❌ **the big gap** — session/JWT only | PATs (Phase A) |
|
| User API keys → full REST API | ❌ **the big gap** — session/JWT only | PATs (Phase A) |
|
||||||
| Outbound webhooks (collection events, HMAC, custom headers) | ❌ | `webhooks` table + delivery log |
|
| Outbound webhooks (collection events, HMAC, custom headers) | ✅ `outbound_webhooks` + `webhook_deliveries`, HMAC `X-Containr-Signature`, wildcard events, 3-retry, UI page | — |
|
||||||
| Banners (global + per-tenant, dismissible) | ❌ | `banners` table + top bar |
|
| Banners (global + per-tenant, dismissible) | ❌ | `banners` table + top bar |
|
||||||
| White-label (logo/favicon/title/OG/theme colors) | ❌ | `app_settings.branding` — **Containr remains the default brand** |
|
| White-label (logo/favicon/title/OG/theme colors) | ❌ | `app_settings.branding` — **Containr remains the default brand** |
|
||||||
| Activity feed (icon/severity/category/metadata, 90-day TTL job) | 🟡 `audit_logs` coarser | Enrich + project feed UI |
|
| Activity feed (icon/severity/category/metadata, 90-day TTL job) | ✅ `/activity` + project view | — |
|
||||||
| Admin metrics dashboard (users/servers/queued/failed counts) | 🟡 `/admin` exists | Extend |
|
| Admin metrics dashboard (users/servers/queued/failed counts) | 🟡 `/admin` exists | Extend |
|
||||||
| **Impersonate user** | ❌ | Admin support tool |
|
| **Impersonate user** | ✅ admin token + `impersonated_by` claim (browser swap pending) | Done (CLI/API/MCP) |
|
||||||
| In-app docs (content-collections markdown) | ✅ `/docs` | — |
|
| In-app docs (content-collections markdown) | ✅ `/docs` | — |
|
||||||
| Dynamic client-side filter framework (535-line `filter.utils`) | 🟡 audit page filters only | Reuse pattern for tables |
|
| Dynamic client-side filter framework (535-line `filter.utils`) | ✅ `dynamic-filter.ts` + URL state; audit/builds/activity | Done |
|
||||||
| Ops "bubble" (queue monitor, terminal, sync) | ❌ | Operations page |
|
| Ops "bubble" (queue monitor, terminal, sync) | ✅ `/operations` + interactive terminal | Done |
|
||||||
|
|
||||||
#### Agent surface
|
#### Agent surface
|
||||||
| dflow capability | Containr status | Action |
|
| dflow capability | Containr status | Action |
|
||||||
@@ -103,8 +103,8 @@ Legend: ✅ done · 🟡 partial/different · ❌ missing · ➖ not applicable
|
|||||||
| CLI | ❌ | none in dflow — **Containr CLI is a genuine leap past it** |
|
| CLI | ❌ | none in dflow — **Containr CLI is a genuine leap past it** |
|
||||||
| MCP | ❌ | none — same leap |
|
| MCP | ❌ | none — same leap |
|
||||||
| Railway migrator script | ❌ | `containr migrate railway` |
|
| Railway migrator script | ❌ | `containr migrate railway` |
|
||||||
| GitHub App **manifest flow** (instance self-provisions the App) | 🟡 env-configured App | `POST /git/github-app/manifest` flow |
|
| GitHub App **manifest flow** (instance self-provisions the App) | ✅ manifest + convert + App webhook | Done |
|
||||||
| fork-sync webhook deploy trigger | ❌ | ~20 lines on receiver |
|
| fork-sync webhook deploy trigger | ✅ sync/repository_dispatch/workflow_run | Done |
|
||||||
| Azure DevOps provider | ❌ | Optional (dflow does token-clone only) |
|
| Azure DevOps provider | ❌ | Optional (dflow does token-clone only) |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -121,16 +121,16 @@ Legend: ✅ done · 🟡 partial/different · ❌ missing · ➖ not applicable
|
|||||||
- `openapi.yaml` stays the single source of truth; UI, CLI, MCP are thin
|
- `openapi.yaml` stays the single source of truth; UI, CLI, MCP are thin
|
||||||
clients.
|
clients.
|
||||||
|
|
||||||
### Phase A — Agent surface (do this FIRST; everything after inherits it)
|
### Phase A — Agent surface (do this FIRST; everything after inherits it) ✅ SHIPPED (PRs #14–#18)
|
||||||
|
|
||||||
- [ ] **Personal access tokens.** `user_tokens` table — `id`, `user_id`,
|
- [x] **Personal access tokens.** `user_tokens` table — `id`, `user_id`,
|
||||||
`name`, `key_prefix` (`cnp_…`), `key_hash` (sha256), `scopes`
|
`name`, `key_prefix` (`cnp_…`), `key_hash` (sha256), `scopes`
|
||||||
(`read`/`write`/`admin`, later resource-scoped), `expires_at`,
|
(`read`/`write`/`admin`, later resource-scoped), `expires_at`,
|
||||||
`last_used_at`. Model on `agent_auth_tokens` (raw shown once, hash stored).
|
`last_used_at`. Model on `agent_auth_tokens` (raw shown once, hash stored).
|
||||||
`POST/GET/DELETE /api/v1/user/tokens`; `middleware.Auth` accepts
|
`POST/GET/DELETE /api/v1/user/tokens`; `middleware.Auth` accepts
|
||||||
`Bearer cnp_…`; Settings → Access tokens UI with one-time display +
|
`Bearer cnp_…`; Settings → Access tokens UI with one-time display +
|
||||||
copyable CLI/MCP config snippets.
|
copyable CLI/MCP config snippets.
|
||||||
- [ ] **CLI v2.** Current `cmd/cli` is auth + projects only. Full client:
|
- [x] **CLI v2.** Current `cmd/cli` is auth + projects only. Full client:
|
||||||
- `auth login [--token] [--url] [--profile]` (multi-instance profiles).
|
- `auth login [--token] [--url] [--profile]` (multi-instance profiles).
|
||||||
- Resources: `projects services deploy logs exec variables databases
|
- Resources: `projects services deploy logs exec variables databases
|
||||||
cron templates nodes scaling ha security gateway notifications
|
cron templates nodes scaling ha security gateway notifications
|
||||||
@@ -143,16 +143,16 @@ Legend: ✅ done · 🟡 partial/different · ❌ missing · ➖ not applicable
|
|||||||
0/1/2/3/4 (ok/err/not-found/unauthorized/conflict).
|
0/1/2/3/4 (ok/err/not-found/unauthorized/conflict).
|
||||||
- GoReleaser → GH releases + `install.sh`; `version` command.
|
- GoReleaser → GH releases + `install.sh`; `version` command.
|
||||||
- Conventions from docsync/envdiff/cix/repolint.
|
- Conventions from docsync/envdiff/cix/repolint.
|
||||||
- [ ] **MCP server** (`cmd/mcp`, same module, stdio). Tools mirror the
|
- [x] **MCP server** (`cmd/mcp`, same module, stdio). Tools mirror the
|
||||||
CLI: `list_projects create_service deploy get_logs set_variable
|
CLI: `list_projects create_service deploy get_logs set_variable
|
||||||
restart rollback create_database trigger_cron deploy_template exec
|
restart rollback create_database trigger_cron deploy_template exec
|
||||||
get_metrics security_scan …`. Auth: `CONTAINR_URL` + `CONTAINR_TOKEN`
|
get_metrics security_scan …`. Auth: `CONTAINR_URL` + `CONTAINR_TOKEN`
|
||||||
or shared `~/.containr/config`. `containr mcp serve` + install snippets
|
or shared `~/.containr/config`. `containr mcp serve` + install snippets
|
||||||
for Devin/Claude/Cursor.
|
for Devin/Claude/Cursor.
|
||||||
- [ ] **API agent ergonomics.** `Idempotency-Key` on mutating endpoints;
|
- [x] **API agent ergonomics.** `Idempotency-Key` on mutating endpoints;
|
||||||
uniform `{error, code, details}` envelope (audit bare-string returns);
|
uniform `{error, code, details}` envelope (audit bare-string returns);
|
||||||
cursor pagination on growing lists; name-or-id lookup everywhere.
|
cursor pagination on growing lists; name-or-id lookup everywhere.
|
||||||
- [ ] **Agent docs.** Root `AGENTS.md`, `/docs` serves `llms.txt` +
|
- [x] **Agent docs.** Root `AGENTS.md`, `/docs` serves `llms.txt` +
|
||||||
`llms-full.txt`, MCP tool list generated from OpenAPI where possible.
|
`llms-full.txt`, MCP tool list generated from OpenAPI where possible.
|
||||||
|
|
||||||
**Gate**: an agent holding only a PAT registers a project, deploys from a
|
**Gate**: an agent holding only a PAT registers a project, deploys from a
|
||||||
@@ -161,78 +161,68 @@ for a human.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 3. Phase B — Deployment pipeline maturity
|
## 3. Phase B — Deployment pipeline maturity ✅ SHIPPED (PR #19)
|
||||||
|
|
||||||
dflow's one real architectural edge. Containr deploys are fire-and-forget
|
dflow's one real architectural edge. Containr deploys are fire-and-forget
|
||||||
goroutines; this fixes correctness *and* unlocks the ops surface.
|
goroutines; this fixes correctness *and* unlocks the ops surface.
|
||||||
|
|
||||||
- [ ] **Deploy job queue.** Lightweight internal queue (Postgres
|
- [x] **Deploy job queue.** Lightweight internal queue — per-service
|
||||||
`deployment_queue` table + single worker loop — no new infra; Redis
|
FIFO in `internal/deployqueue`; status gains `queued`.
|
||||||
exists but don't add BullMQ-style sprawl). Serialize per service;
|
- [x] **Cancel deployments.** `POST /deployments/:id/cancel` — queued
|
||||||
optional per-node serialization. Status gains `queued`.
|
jobs cancel immediately, active jobs cancel context; `cancelled` status.
|
||||||
- [ ] **Cancel deployments.** Route `POST /deployments/:id/cancel` →
|
- [x] **Cache modes.** `no_cache` on deploys; webhook-triggered deploys
|
||||||
engine `CancelDeployment` (exists, unrouted). Queued jobs cancel
|
default to no-cache (dflow convention — prevents stale-layer surprises).
|
||||||
cleanly; building jobs cancel context where safe.
|
- [x] **Queued→building→running status + persisted step logs.** `queued`
|
||||||
- [ ] **Cache modes.** `?cache=full` on redeploy → rebuild from existing
|
state lands; streaming over existing log surface.
|
||||||
image layers vs clean pull/checkout. Webhook-triggered deploys default
|
- [x] **Env var apply → redeploy.** `--redeploy` flag on variable writes;
|
||||||
to no-cache (dflow convention — prevents stale-layer surprises).
|
backend requeues honoring running state.
|
||||||
- [ ] **Queued→building→running status + persisted step logs.** Already
|
- [x] **Encrypt variables at rest.** `is_secret` values encrypted
|
||||||
mostly there; add `queued`, stream over existing `/ws`.
|
AES-GCM `enc:v1:` (key from `SECRETS_KEY`); masked-echo preserves
|
||||||
- [ ] **Env var apply → redeploy.** Saving variables asks "restart to
|
ciphertext.
|
||||||
apply?" (or `--apply` flag in CLI); backend triggers redeploy honoring
|
|
||||||
running state. Currently rows update but containers keep stale env —
|
|
||||||
silent-drift bug class, not just UX.
|
|
||||||
- [ ] **Encrypt variables at rest.** `environment_variables.value` is
|
|
||||||
plaintext today (verified). Envelope-encrypt `is_secret` values
|
|
||||||
(AES-GCM, key from `JWT_SECRET` or dedicated `SECRETS_KEY`); dflow
|
|
||||||
encrypts every var, we encrypt secrets — migration = transparent
|
|
||||||
encrypt-on-write + decrypt-on-read.
|
|
||||||
|
|
||||||
## 4. Phase C — Service configuration parity
|
## 4. Phase C — Service configuration parity ✅ SHIPPED (PRs #20–#23)
|
||||||
|
|
||||||
- [ ] **Volumes on services** — `services.volumes` jsonb
|
- [x] **Volumes on services** — `services.volumes` jsonb
|
||||||
`[{volume|host_path, container_path, read_only}]`; engine accepts
|
`[{volume|host_path, container_path, read_only}]`; Volumes tab +
|
||||||
`VolumeMounts` already. Volumes tab + orphan-volume listing
|
admin volume inventory.
|
||||||
(`docker volume ls -f dangling`) with delete/attach.
|
- [x] **Multi-domain** — `service_domains(service_id, domain, is_default,
|
||||||
- [ ] **Multi-domain** — `service_domains(service_id, domain, is_default,
|
cert_type, status, last_checked_at)`. Domains UI: add/remove/
|
||||||
cert_type, status, last_checked_at)`. Domains tab: add/remove/
|
set-default/DNS-status badge. `services.domain` stays derived default.
|
||||||
set-default/DNS-status badge. Keep `services.domain` as derived
|
- [x] **DNS preflight** — `GET /services/:id/domains/check` resolves and
|
||||||
default for compat.
|
|
||||||
- [ ] **DNS preflight** — `GET /services/:id/domains/check` resolves and
|
|
||||||
compares vs edge IP → `ok|wrong-target|pending`.
|
compares vs edge IP → `ok|wrong-target|pending`.
|
||||||
- [ ] **Maintenance mode + basic-auth gate** — Traefik middleware labels;
|
- [x] **Maintenance mode + basic-auth gate** — Traefik middleware labels;
|
||||||
no proxy code needed.
|
no proxy code needed.
|
||||||
- [ ] **Routing overrides** — `services.traefik_labels` jsonb with
|
- [ ] **Routing overrides** — `services.traefik_labels` jsonb with
|
||||||
allowlist validation (middlewares, headers, redirects).
|
allowlist validation (middlewares, headers, redirects).
|
||||||
- [ ] **Builder override** — `services.builder` enum
|
- [x] **Builder override** — `services.builder` enum
|
||||||
`auto|railpack|nixpacks|dockerfile|static`; fix hardcoded `nixpacks`
|
`auto|railpack|nixpacks|dockerfile|static`; static builder = build +
|
||||||
at `deployments.go:406`. Static builder = build + nginx sidecar.
|
nginx serve. Fixed hardcoded `nixpacks` + `BuildImage` stream drain bug.
|
||||||
- [ ] **Resource limits + reserves** — adopt dflow-style explicit units
|
- [x] **Resource limits + reserves** — `cpu`/`memory` caps +
|
||||||
(`"0.5"`/`"512m"`); add `cpu_reserve`/`memory_reserve` →
|
`cpu_reserve`/`memory_reserve` → NanoCPUs/Memory/MemoryReservation;
|
||||||
`NanoCPUs`/`Memory`/`MemoryReservation`; instance defaults in
|
instance defaults in `app_settings`.
|
||||||
`app_settings`.
|
- [x] **Pre-deploy capacity check** — reservations vs node capacity;
|
||||||
- [ ] **Pre-deploy capacity check** — sum reservations vs node capacity
|
`app_settings.capacity_policy=block` rejects, default warns.
|
||||||
(agent telemetry); warn or block per policy flag.
|
- [x] **Service clone** + **move between projects** — copies volumes,
|
||||||
- [ ] **Service clone** + **move between projects**.
|
domains, access gates, vars (ciphertext verbatim); audit-logged.
|
||||||
- [ ] **Private registries** — `registry_credentials`
|
- [x] **Private registries** — `registry_credentials`
|
||||||
(server/username/secret, encrypted); `docker login` at pull time;
|
(server/username/secret, encrypted); pull auth resolved at deploy;
|
||||||
per-service registry link.
|
per-service registry link.
|
||||||
- [ ] **Service detail tabs to match dflow's surface**: add Domains,
|
- [x] **Service detail tabs to match dflow's surface**: Domains,
|
||||||
Volumes, Proxy/access-controls, Backups sections to the service page
|
Volumes, access-controls sections added to the service page.
|
||||||
(Cron/Previews/Scaling already exist).
|
|
||||||
|
|
||||||
## 5. Phase D — Data layer maturity
|
## 5. Phase D — Data layer maturity
|
||||||
|
|
||||||
- [ ] **External DB registration** — `provider='managed'|'external'`,
|
- [x] **External DB registration** — `provider='managed'|'external'`,
|
||||||
conn fields, `POST /databases/test-connection`, same bind flow.
|
conn fields, `POST /databases/test-connection`, same bind flow. (PR #24)
|
||||||
- [ ] **Public DB port expose** — opt-in Traefik TCP route or host port;
|
- [x] **Public DB port expose** — `public_port` opt-in `0.0.0.0` host
|
||||||
default stays loopback.
|
bind; default stays loopback. (PR #25)
|
||||||
- [ ] **Offsite backups** — `backup_targets` (S3-compatible:
|
- [x] **Offsite backups** — `backup_targets` (S3-compatible:
|
||||||
endpoint/bucket/keys encrypted); per-DB destination; ship archives via
|
endpoint/bucket/keys encrypted); per-DB destination; archives shipped
|
||||||
utility container + s3 SDK; restore-from-S3.
|
via holder container + minio-go; restore-from-S3 fallback. (PR #26)
|
||||||
- [ ] **Cross-node DB migration** — `POST /databases/:id/migrate
|
- [ ] **Cross-node DB migration** — `POST /databases/:id/migrate
|
||||||
{target_node_id}`: dump → stream transfer → provision → restore →
|
{target_node_id}`: dump → stream transfer → provision → restore →
|
||||||
re-point bound services. Needs ≥2 nodes or local↔node.
|
re-point bound services. Deferred until Phase F lands real second
|
||||||
|
nodes — nothing to migrate between today.
|
||||||
- [ ] **Backup import** — upload archive → new restore point (pairs with
|
- [ ] **Backup import** — upload archive → new restore point (pairs with
|
||||||
migrators).
|
migrators).
|
||||||
|
|
||||||
@@ -262,18 +252,31 @@ and wired refs, one click or one CLI call.
|
|||||||
|
|
||||||
## 7. Phase F — Nodes & infrastructure
|
## 7. Phase F — Nodes & infrastructure
|
||||||
|
|
||||||
- [ ] **SSH bootstrap install** — `containr nodes add --ssh user@host` +
|
- [x] **SSH bootstrap install** — `containr nodes add <user@host>` mints an
|
||||||
UI flow: one SSH session runs agent install (binary + systemd +
|
enroll token, SSHes once (agent/key/default keys), runs
|
||||||
enroll token), SSH closed forever. dflow onboarding UX, agent-model
|
`GET /api/agents/install.sh | bash` remotely, closes the session.
|
||||||
security.
|
`--print` emits the one-liner for manual runs; the Usage page shows the
|
||||||
- [ ] **Mesh awareness** — agent detects + reports tailscale0/netbird0/
|
same command. Agent binaries ship in the image under `/agents` and are
|
||||||
zt0 IPs at register; backend stores + displays. Optional
|
served at `/api/agents/download/{linux-amd64,linux-arm64}` (install
|
||||||
`TAILSCALE_AUTH_KEY` enroll-at-provision later.
|
script falls back to GH releases). `cli/commands/nodes_add.go`,
|
||||||
- [ ] **Node detail page** — placed services, telemetry history, agent
|
`api/assets/install-agent.sh`, `agents.go:ServeInstallScript`/`ServeAgentBinary`.
|
||||||
version + self-upgrade, **drain/cordon**, remove.
|
- [x] **Mesh awareness** — agent reports tailscale/netbird/wireguard/
|
||||||
- [ ] **Node housekeeping** — agent `prune` command (docker system
|
zerotier interface IPs at register (`mesh` field, authoritative on
|
||||||
prune, bounded) + optional schedule (`setServerAutoCleanup` analog —
|
re-register), stored in `node_agents.metadata.mesh`, shown on the node
|
||||||
disk pressure is the #1 small-VPS killer).
|
detail page. `TAILSCALE_AUTH_KEY` enroll-at-provision still open.
|
||||||
|
- [x] **Node detail page** — `/nodes/:id` (admin): heartbeat telemetry
|
||||||
|
(cpu/mem sparklines over 1h/24h/7d via `GET /agents/:id/metrics`),
|
||||||
|
containers, command history, rename, auto-prune toggle, prune-now,
|
||||||
|
remove. Drain/cordon and self-upgrade deferred — they need
|
||||||
|
`services.node_id` routing which lands with multi-node scheduling.
|
||||||
|
- [x] **Node housekeeping** — agent `prune` + `system_df` commands
|
||||||
|
(`docker system prune -af`, volumes excluded unless requested, optional
|
||||||
|
`--until`). `node_agents.auto_prune` + the health sweep enqueue a prune
|
||||||
|
at most once per 24h per node. Surfaces: `POST /agents/:id/prune`,
|
||||||
|
`nodes update --auto-prune` / `nodes prune [--volumes --until]` /
|
||||||
|
`nodes commands`, MCP `containr_nodes_update|prune|commands`
|
||||||
|
(confirm-gated), Usage-page per-node Prune button + auto toggle.
|
||||||
|
`cmd/agent/main.go`, `notification_producers.go:scheduleAutoPrune`.
|
||||||
- [ ] **Multi-node scheduling** — wire `scheduling_rules` (table exists):
|
- [ ] **Multi-node scheduling** — wire `scheduling_rules` (table exists):
|
||||||
pin/spread/affinity, prefer-lowest-usage; deploy engine routes to
|
pin/spread/affinity, prefer-lowest-usage; deploy engine routes to
|
||||||
`services.node_id` via agent commands (agent already runs containers).
|
`services.node_id` via agent commands (agent already runs containers).
|
||||||
@@ -288,52 +291,149 @@ and wired refs, one click or one CLI call.
|
|||||||
|
|
||||||
## 8. Phase G — Platform polish
|
## 8. Phase G — Platform polish
|
||||||
|
|
||||||
- [ ] **Outbound webhooks** — `webhooks(url, secret, events[], headers,
|
- [x] **Outbound webhooks** — `outbound_webhooks(url, secret, events[],
|
||||||
enabled)` + `webhook_deliveries` log; HMAC `X-Containr-Signature`;
|
headers, enabled)` + `webhook_deliveries` log; HMAC
|
||||||
events: `deployment.* service.* database.* security.* agent.*`.
|
`X-Containr-Signature`; wildcard events on every audited action;
|
||||||
- [ ] **Activity feed** — enrich `audit_logs` with `severity category
|
CLI + MCP + `/settings/webhooks` page with delivery log + test ping.
|
||||||
icon label metadata`; per-project timeline + global feed; TTL job.
|
- [x] **Activity feed** — `audit_logs` enriched with `severity`
|
||||||
- [ ] **Banners** — `banners(title, body, level, scope, active)`; top
|
(`info|success|warning|error`), `category`, human `label`; derived at
|
||||||
bar in UI + surfaced to CLI on `auth status`.
|
write time, classified on read for legacy rows, boot-time backfill.
|
||||||
- [ ] **White-label** — `app_settings.branding`: product name, logos,
|
`GET /activity` (global, visibility-scoped, filters + `since`) and
|
||||||
favicon, accent override, docs/support links. `GET /api/v1/branding`
|
`GET /projects/:id/activity` (owner/member/approved/admin). Frontend:
|
||||||
public + cached. **Containr/Vertice remains default;** this is for
|
`/activity` page with severity chips + project workspace `Activity`
|
||||||
self-hosters who rebrand their instance.
|
view. `containr activity` + `containr_activity_list`/`containr_project_activity`
|
||||||
- [ ] **Operations page** — unified view of running/queued/failed jobs
|
MCP tools. Retention worker prunes > `AUDIT_RETENTION_DAYS` (default 90).
|
||||||
(deploys, builds, backups, cron, scans, agent commands) — dflow's
|
- [x] **Banners** — `banners` table with title/body/level/active/
|
||||||
bubble panel as a real page; flush/stuck-job tools once queue exists.
|
dismissible/schedule window. `GET /banners/active` for signed-in
|
||||||
- [ ] **Interactive terminal** — upgrade exec to WS PTY (`xterm.js`);
|
users; admin CRUD at `/admin/banners`. `BannerBar` renders above the
|
||||||
`nodes/:id/terminal` via agent (dflow's wetty without the extra
|
header (per-banner dismiss via localStorage); admin console manages
|
||||||
container).
|
them; `containr banners` CLI + `auth status` surfaces active ones.
|
||||||
- [ ] **Team invites** — `POST /invites` → signed link → join flow;
|
- [x] **White-label** — `app_settings` `branding.*` keys: product name,
|
||||||
replaces manual-only user creation.
|
logo/favicon URL, accent color, docs/support links. Public
|
||||||
- [ ] **Impersonate user** — admin support tool, audit-logged.
|
`GET /api/v1/branding` (defaults = Containr); admin writes via
|
||||||
- [ ] **GitHub App manifest flow** — `POST /git/github-app/manifest`
|
`PUT /settings`. `useBranding()` applies title/accent/favicon;
|
||||||
returns manifest + redirect; instance self-provisions its App
|
`BrandWordmark` replaces hardcoded marks; admin Platform section
|
||||||
(dflow pattern — removes the env-setup barrier).
|
edits all fields. MCP `containr_branding_get` + `settings_update`.
|
||||||
- [ ] **fork-sync trigger** — accept repo-sync events on webhook
|
- [x] **Operations page** — `GET /operations` aggregates active
|
||||||
receiver.
|
deployments, in-memory deploy-queue depth (`Queue.Snapshot()`),
|
||||||
- [ ] **Dynamic filter framework** — port dflow's `filter.utils`
|
24h failures, cron runs and backups scoped to the caller's projects.
|
||||||
pattern (typed filter configs → URL state → client filtering) for
|
UI at `/operations` (Operate nav) with live refetch; `containr
|
||||||
audit logs, deployments, notifications tables.
|
operations` CLI + `containr_operations_get` MCP. Remaining: agent
|
||||||
|
commands once node fleet lands; flush/stuck-job tools.
|
||||||
|
- [x] **Interactive terminal** — `GET /services/:id/terminal` upgrades
|
||||||
|
to a WebSocket bridged to a `docker exec` PTY (bash→sh fallback,
|
||||||
|
`TERM=xterm-256color`, resize frames). xterm.js Console section on the
|
||||||
|
service page; `containr shell <id>` bridges a local TTY; distroless
|
||||||
|
images get a clean "no shell" error. Remaining: `nodes/:id/terminal`
|
||||||
|
via agent.
|
||||||
|
- [x] **Team invites** — `POST /admin/invites` returns a one-time
|
||||||
|
link (sha256-hashed token at rest, optional email binding, TTL);
|
||||||
|
public `GET /auth/invites/:token` validates and
|
||||||
|
`POST /auth/accept-invite` registers — works while public signup is
|
||||||
|
closed. `/auth/accept-invite` page, admin manage/revoke UI,
|
||||||
|
`containr invites` CLI + MCP tools.
|
||||||
|
- [x] **Impersonate user** — `POST /admin/users/:id/impersonate`
|
||||||
|
mints a 15-minute bearer token (`impersonated_by` claim),
|
||||||
|
audit-logged; admin accounts are not impersonatable. CLI
|
||||||
|
`admin impersonate`, MCP tool, admin UI token modal. Remaining:
|
||||||
|
in-browser session swap needs a session bridge.
|
||||||
|
- [x] **GitHub App manifest flow** — `POST /admin/git/github-app/manifest`
|
||||||
|
returns manifest + submission URL; the callback page exchanges
|
||||||
|
`code` via `POST /admin/git/github-app/convert` and credentials
|
||||||
|
persist in `app_settings` (overriding `GITHUB_APP_*` env). New
|
||||||
|
public `POST /api/git/github-app/webhook` receives App-level push
|
||||||
|
events (HMAC-verified) and enqueues matching services. Settings→env
|
||||||
|
fallback wired into install-URL + installation-token paths.
|
||||||
|
`containr admin github-app` CLI + MCP + OpenAPI.
|
||||||
|
- [x] **fork-sync trigger** — webhook receiver accepts `sync`,
|
||||||
|
`repository_dispatch`, and `workflow_run` events (no `ref`) alongside
|
||||||
|
pushes: branch falls back to the webhook filter, then the service's
|
||||||
|
own `git_branch`; repo-only matching when no branch is known.
|
||||||
|
Also fixed a latent bug where the webhook's deployment insert omitted
|
||||||
|
the NOT NULL `version` column — push-triggered deploys silently
|
||||||
|
failed for every service.
|
||||||
|
- [x] **Dynamic filter framework** — ported dflow's `filter.utils`
|
||||||
|
pattern as `lib/dynamic-filter.ts` + `use-dynamic-filter.ts`
|
||||||
|
(`FilterConfig` schema → `FilterEngine` → `FilterBar`; state lives in
|
||||||
|
`?f.<key>` search params so filtered views are shareable links).
|
||||||
|
`useFilterState` exposes URL state pre-fetch for server-backed
|
||||||
|
filters. Applied: audit logs (server-backed), builds (server + client
|
||||||
|
date-range), activity (severity server + client category/search).
|
||||||
|
Notifications table has no list surface yet — apply when one lands.
|
||||||
- [ ] **Notification producers round-out** — backups, agent offline,
|
- [ ] **Notification producers round-out** — backups, agent offline,
|
||||||
upgrade available, security findings.
|
upgrade available, security findings.
|
||||||
|
- [x] **Sleep-on-idle / wake-on-traffic** (serverless-lite) —
|
||||||
|
`services.sleep_enabled` + `sleep_idle_minutes` (PR #29). Sweeper
|
||||||
|
polls container NetworkIO deltas; idle past threshold → workload
|
||||||
|
removed, status `sleeping`. Wake-on-traffic via per-service
|
||||||
|
placeholder container on the edge network carrying the same Traefik
|
||||||
|
host rule at higher priority — serves a waking page, calls
|
||||||
|
`/internal/wake/:id`, exits; Traefik falls back once the app is up.
|
||||||
|
Verified `running → sleeping → waking → running` end to end.
|
||||||
|
- [x] **Env doctor** — `GET /services/:id/env-check` surfaces
|
||||||
|
unresolved `${{…}}` refs, empty values, and unreadable
|
||||||
|
(rotated-key) secrets; template `plan` endpoint reports missing
|
||||||
|
required vars before deploy (PR #30). Frontend Variables section
|
||||||
|
shows the banner.
|
||||||
|
- [x] **Shared project variables** — `project_variables` table,
|
||||||
|
`GET/PUT /projects/:id/variables`, `${{shared.KEY}}` refs resolved
|
||||||
|
at deploy/reconcile, secrets `enc:v1:` at rest and masked (PR #32).
|
||||||
|
- [x] **One-click install** — `install.sh` (curl|sh): detects docker,
|
||||||
|
writes compose + .env with generated secrets incl. `SECRETS_KEY`,
|
||||||
|
pulls images, runs migrations, prints admin URL. GHCR images mean
|
||||||
|
installs never build from source.
|
||||||
|
- [x] **CI/CD v2** — required checks (Go test/vet, frontend
|
||||||
|
lint/build/test, compose boot smoke), `dorny/paths-filter` gate so
|
||||||
|
docs-only PRs skip heavyweight jobs, and release on main/tag:
|
||||||
|
conventional-commit version bump → tag → GHCR images (backend,
|
||||||
|
frontend) → cross-platform CLI + MCP binaries → SHA-256 checksums
|
||||||
|
→ GitHub Release → Trivy scans (PR #33).
|
||||||
- [ ] **Custom roles/RBAC** — defer until demanded; PAT scopes designed
|
- [ ] **Custom roles/RBAC** — defer until demanded; PAT scopes designed
|
||||||
forward-compatible now.
|
forward-compatible now.
|
||||||
|
|
||||||
## 9. Phase H — Migration & ecosystem
|
## 9. Phase H — Migration & ecosystem
|
||||||
|
|
||||||
- [ ] **Railway migrator** — `containr migrate railway --token --project
|
- [x] **Railway migrator** — `containr migrate railway --token --project
|
||||||
--dry-run`: Railway GraphQL → services/vars/domains → recreate via
|
--environment --into --dry-run`: Railway GraphQL (backboard) →
|
||||||
PAT. Landing-page pitch: "off Railway in one command."
|
services (git-repo/image + command + replicas), variables, custom +
|
||||||
- [ ] **dflow importer** — `containr migrate dflow` via Payload REST
|
service domains → recreates via the Containr API. Project tokens and
|
||||||
export or Mongo dump: servers→nodes, projects, services(+vars/
|
account tokens both supported. `cli/commands/migrate.go`.
|
||||||
domains/volumes), databases, templates. Onboards dflow refugees.
|
- [x] **dflow importer** — `containr migrate dflow --url --api-key
|
||||||
- [ ] **`containr import compose`** — Phase-E parser straight to a
|
--project --into --dry-run`: pages the Payload REST collections
|
||||||
project (no template intermediate).
|
(users API-Key auth) → services (app→git/web, docker→image,
|
||||||
- [ ] **Template registry** — optional `CONTAINR_TEMPLATE_REGISTRY_URL`
|
database→typed image + DATABASE_URL for external providers),
|
||||||
fetch/publish; bundled catalog works offline (dflow's self-contained
|
variables (empty encrypted values flagged), bind volumes, domains.
|
||||||
principle — nothing phones home).
|
Soft-deleted services excluded. `cli/commands/migrate_dflow.go`.
|
||||||
|
- [x] **`containr import compose`** — `POST /projects/:id/import-compose`
|
||||||
|
deploys a compose file straight into a project; CLI `import compose`,
|
||||||
|
MCP tool, workspace import modal; project delete cascades to
|
||||||
|
graph-provisioned managed databases (`database_services.project_id`).
|
||||||
|
- [x] **Template registry** — `CONTAINR_TEMPLATE_REGISTRY_URL` merges a
|
||||||
|
remote Containr catalog into `GET /templates` as `source:"registry"`
|
||||||
|
(60s cache, dead registry never blocks local). `GET /templates/:id`
|
||||||
|
falls through to the registry; deploy materializes a deterministic
|
||||||
|
`reg-<hash>` private copy for the deployer so re-deploys don't dup.
|
||||||
|
`api/templateregistry.go`, `.env.example`.
|
||||||
|
|
||||||
|
## 9b. Phase I — Companion surfaces
|
||||||
|
|
||||||
|
- [x] **Phone app (v1)** — `app/mobile`, Expo/React Native thin client
|
||||||
|
over the agent-shaped API; dark UI mirroring the design system. PAT
|
||||||
|
auth against any instance URL, stored in platform secure storage.
|
||||||
|
- **Dashboard** — service status counts (running/sleeping/failed/
|
||||||
|
stopped), per-project service health dots, recent deployments feed.
|
||||||
|
- **Operate** — start/stop/restart/redeploy, sleep/wake, rollback to
|
||||||
|
a prior deployment, database start/stop/restart/backup-now.
|
||||||
|
- **Observe** — polling log tail (300 lines, 5s refresh), per-service
|
||||||
|
deployment history, env-doctor issues surfaced on the service view.
|
||||||
|
- [ ] **env var edit + apply-redeploy, domain management, replica
|
||||||
|
scaling, cron execution history** — next iteration.
|
||||||
|
- [ ] **Push notifications** — zero-infra path = self-hosted
|
||||||
|
ntfy/Gotify relay (no Firebase dependency), FCM/APNs optional.
|
||||||
|
- [ ] **Offline resilience** — cached last-known state + unreachable
|
||||||
|
banner.
|
||||||
|
- [ ] **Status/offline resilience** — app + CLI degrade gracefully when
|
||||||
|
the instance is unreachable (cached last-known state, clear banner).
|
||||||
|
|
||||||
## 10. Carried-over debt (ROADMAP.md items still open)
|
## 10. Carried-over debt (ROADMAP.md items still open)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,13 @@ COPY go.mod go.sum ./
|
|||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main cmd/server/main.go
|
ARG VERSION=dev
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo \
|
||||||
|
-ldflags "-X containr/internal/api.Version=${VERSION}" -o main cmd/server/main.go
|
||||||
|
|
||||||
|
# Node-agent binaries served by the API for SSH/bootstrap installs.
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /agents/containr-agent-linux-amd64 ./cmd/agent && \
|
||||||
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o /agents/containr-agent-linux-arm64 ./cmd/agent
|
||||||
|
|
||||||
# Build embedded Better Auth runtime dependencies
|
# Build embedded Better Auth runtime dependencies
|
||||||
FROM node:22-alpine AS auth-builder
|
FROM node:22-alpine AS auth-builder
|
||||||
@@ -33,6 +39,7 @@ WORKDIR /app
|
|||||||
|
|
||||||
COPY --from=go-builder /app/main ./main
|
COPY --from=go-builder /app/main ./main
|
||||||
COPY --from=go-builder /app/migrations_goose ./migrations_goose
|
COPY --from=go-builder /app/migrations_goose ./migrations_goose
|
||||||
|
COPY --from=go-builder /agents /agents
|
||||||
COPY --from=auth-builder /auth ./auth
|
COPY --from=auth-builder /auth ./auth
|
||||||
|
|
||||||
RUN chown -R appuser:appgroup /app
|
RUN chown -R appuser:appgroup /app
|
||||||
|
|||||||
@@ -167,6 +167,8 @@ func register(ctx context.Context, client *http.Client, cfg agentConfig) (string
|
|||||||
"ip_address": cfg.IP,
|
"ip_address": cfg.IP,
|
||||||
"port": cfg.Port,
|
"port": cfg.Port,
|
||||||
"capabilities": detectCapabilities(),
|
"capabilities": detectCapabilities(),
|
||||||
|
// Always send mesh so a removed interface clears stored state.
|
||||||
|
"mesh": detectMeshInterfaces(),
|
||||||
}
|
}
|
||||||
var response registerResponse
|
var response registerResponse
|
||||||
if err := postJSON(ctx, client, cfg, "/api/agents/register", payload, &response); err != nil {
|
if err := postJSON(ctx, client, cfg, "/api/agents/register", payload, &response); err != nil {
|
||||||
@@ -246,11 +248,29 @@ func executeCommand(item command) (string, error) {
|
|||||||
return runDocker("restart", commandTarget(item.Payload))
|
return runDocker("restart", commandTarget(item.Payload))
|
||||||
case "remove_container":
|
case "remove_container":
|
||||||
return runDocker("rm", "-f", commandTarget(item.Payload))
|
return runDocker("rm", "-f", commandTarget(item.Payload))
|
||||||
|
case "prune":
|
||||||
|
return dockerPrune(item.Payload)
|
||||||
|
case "system_df":
|
||||||
|
return runDocker("system", "df", "--format", "json")
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("unsupported command type %q", item.Type)
|
return "", fmt.Errorf("unsupported command type %q", item.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dockerPrune runs a bounded docker system prune. Volumes are excluded
|
||||||
|
// unless the payload explicitly opts in — a stray volume prune destroys
|
||||||
|
// database data.
|
||||||
|
func dockerPrune(payload map[string]interface{}) (string, error) {
|
||||||
|
args := []string{"system", "prune", "-af"}
|
||||||
|
if boolValue(payload["volumes"]) {
|
||||||
|
args = append(args, "--volumes")
|
||||||
|
}
|
||||||
|
if until := stringValue(payload["until"]); until != "" {
|
||||||
|
args = append(args, "--filter", fmt.Sprintf("until=%s", until))
|
||||||
|
}
|
||||||
|
return runDocker(args...)
|
||||||
|
}
|
||||||
|
|
||||||
func createContainer(payload map[string]interface{}) (string, error) {
|
func createContainer(payload map[string]interface{}) (string, error) {
|
||||||
container, ok := payload["container"].(map[string]interface{})
|
container, ok := payload["container"].(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -354,6 +374,42 @@ func detectCapabilities() capabilities {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// detectMeshInterfaces reports overlay-network interface IPs (tailscale,
|
||||||
|
// netbird, zerotier, wireguard) so the backend can display reachable
|
||||||
|
// mesh addresses alongside the primary IP.
|
||||||
|
func detectMeshInterfaces() map[string]string {
|
||||||
|
ifaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
mesh := map[string]string{}
|
||||||
|
for _, iface := range ifaces {
|
||||||
|
name := strings.ToLower(iface.Name)
|
||||||
|
isMesh := strings.HasPrefix(name, "tailscale") || strings.HasPrefix(name, "ts.") ||
|
||||||
|
strings.HasPrefix(name, "netbird") || strings.HasPrefix(name, "wt") ||
|
||||||
|
strings.HasPrefix(name, "zt") || strings.HasPrefix(name, "wg")
|
||||||
|
if !isMesh || iface.Flags&net.FlagUp == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ip, _, err := net.ParseCIDR(addr.String())
|
||||||
|
if err != nil || ip.IsLoopback() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v4 := ip.To4(); v4 != nil {
|
||||||
|
mesh[iface.Name] = v4.String()
|
||||||
|
} else if _, exists := mesh[iface.Name]; !exists {
|
||||||
|
mesh[iface.Name] = ip.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mesh
|
||||||
|
}
|
||||||
|
|
||||||
func collectResources(ip string) nodeResources {
|
func collectResources(ip string) nodeResources {
|
||||||
mem := readMemory()
|
mem := readMemory()
|
||||||
storage := readStorage("/")
|
storage := readStorage("/")
|
||||||
|
|||||||
+12
-3
@@ -13,6 +13,7 @@ require (
|
|||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/lib/pq v1.10.9
|
github.com/lib/pq v1.10.9
|
||||||
github.com/miekg/dns v1.1.72
|
github.com/miekg/dns v1.1.72
|
||||||
|
github.com/minio/minio-go/v7 v7.0.95
|
||||||
github.com/modelcontextprotocol/go-sdk v1.7.0
|
github.com/modelcontextprotocol/go-sdk v1.7.0
|
||||||
github.com/pressly/goose/v3 v3.24.1
|
github.com/pressly/goose/v3 v3.24.1
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
@@ -23,6 +24,7 @@ require (
|
|||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
golang.org/x/crypto v0.57.0
|
golang.org/x/crypto v0.57.0
|
||||||
golang.org/x/term v0.46.0
|
golang.org/x/term v0.46.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -37,23 +39,28 @@ require (
|
|||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/distribution/reference v0.6.0 // indirect
|
github.com/distribution/reference v0.6.0 // indirect
|
||||||
github.com/docker/go-units v0.5.0 // indirect
|
github.com/docker/go-units v0.5.0 // indirect
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
|
github.com/go-ini/ini v1.67.0 // indirect
|
||||||
github.com/go-logr/logr v1.4.3 // indirect
|
github.com/go-logr/logr v1.4.3 // indirect
|
||||||
github.com/go-logr/stdr v1.2.2 // indirect
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.5 // indirect
|
||||||
github.com/google/jsonschema-go v0.4.3 // indirect
|
github.com/google/jsonschema-go v0.4.3 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
github.com/klauspost/compress v1.18.0 // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mfridman/interpolate v0.0.2 // indirect
|
github.com/mfridman/interpolate v0.0.2 // indirect
|
||||||
|
github.com/minio/crc64nvme v1.0.2 // indirect
|
||||||
|
github.com/minio/md5-simd v1.1.2 // indirect
|
||||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||||
github.com/moby/sys/atomicwriter v0.1.0 // indirect
|
github.com/moby/sys/atomicwriter v0.1.0 // indirect
|
||||||
github.com/moby/term v0.5.2 // indirect
|
github.com/moby/term v0.5.2 // indirect
|
||||||
@@ -63,8 +70,10 @@ require (
|
|||||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||||
github.com/opencontainers/image-spec v1.1.1 // indirect
|
github.com/opencontainers/image-spec v1.1.1 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||||
|
github.com/philhofer/fwd v1.2.0 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/rs/xid v1.6.0 // indirect
|
||||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||||
github.com/segmentio/asm v1.2.0 // indirect
|
github.com/segmentio/asm v1.2.0 // indirect
|
||||||
github.com/segmentio/encoding v0.5.4 // indirect
|
github.com/segmentio/encoding v0.5.4 // indirect
|
||||||
@@ -74,6 +83,7 @@ require (
|
|||||||
github.com/spf13/cast v1.10.0 // indirect
|
github.com/spf13/cast v1.10.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.10 // indirect
|
github.com/spf13/pflag v1.0.10 // indirect
|
||||||
github.com/subosito/gotenv v1.6.0 // indirect
|
github.com/subosito/gotenv v1.6.0 // indirect
|
||||||
|
github.com/tinylib/msgp v1.3.0 // indirect
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||||
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
||||||
@@ -97,7 +107,6 @@ require (
|
|||||||
golang.org/x/time v0.15.0 // indirect
|
golang.org/x/time v0.15.0 // indirect
|
||||||
golang.org/x/tools v0.49.0 // indirect
|
golang.org/x/tools v0.49.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.11 // indirect
|
google.golang.org/protobuf v1.36.11 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
gotest.tools/v3 v3.5.2 // indirect
|
gotest.tools/v3 v3.5.2 // indirect
|
||||||
modernc.org/sqlite v1.39.0 // indirect
|
modernc.org/sqlite v1.39.0 // indirect
|
||||||
)
|
)
|
||||||
+21
-5
@@ -46,6 +46,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
|
|||||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
|
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
|
||||||
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
|
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
|
||||||
|
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||||
|
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||||
@@ -63,8 +65,8 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
|
|||||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
@@ -82,9 +84,12 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
|
|||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||||
|
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
|
github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
@@ -99,6 +104,12 @@ github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6B
|
|||||||
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
||||||
github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI=
|
github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI=
|
||||||
github.com/miekg/dns v1.1.72/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs=
|
github.com/miekg/dns v1.1.72/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs=
|
||||||
|
github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg=
|
||||||
|
github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||||
|
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||||
|
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||||
|
github.com/minio/minio-go/v7 v7.0.95 h1:ywOUPg+PebTMTzn9VDsoFJy32ZuARN9zhB+K3IYEvYU=
|
||||||
|
github.com/minio/minio-go/v7 v7.0.95/go.mod h1:wOOX3uxS334vImCNRVyIDdXX9OsXDm89ToynKgqUKlo=
|
||||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||||
github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw=
|
github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw=
|
||||||
@@ -130,6 +141,8 @@ github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJw
|
|||||||
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||||
|
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||||
|
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
@@ -144,6 +157,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
|
|||||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||||
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
|
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
|
||||||
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||||
|
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||||
|
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||||
@@ -182,6 +197,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
|
|||||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||||
|
github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww=
|
||||||
|
github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0=
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
||||||
@@ -227,7 +244,6 @@ golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ=
|
|||||||
golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
|
golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
|
||||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
|
|||||||
@@ -0,0 +1,300 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Activity feed — enriched audit_logs rows (severity/category/label) served
|
||||||
|
// as a global feed and per-project timelines. Visibility mirrors the
|
||||||
|
// deployment/operations scoping: own actions, plus events on resources
|
||||||
|
// inside visible projects (approved / owned / member; admin sees all).
|
||||||
|
|
||||||
|
const activityVisibility = `(
|
||||||
|
a.user_id = $1::uuid OR $2::bool
|
||||||
|
OR (a.resource = 'project' AND a.resource_id IN (
|
||||||
|
SELECT id FROM projects p WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||||
|
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||||
|
OR (a.resource = 'service' AND a.resource_id IN (
|
||||||
|
SELECT s.id FROM services s JOIN projects p ON p.id = s.project_id
|
||||||
|
WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||||
|
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||||
|
OR (a.resource = 'deployment' AND a.resource_id IN (
|
||||||
|
SELECT d.id FROM deployments d
|
||||||
|
JOIN services s ON s.id = d.service_id
|
||||||
|
JOIN projects p ON p.id = s.project_id
|
||||||
|
WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||||
|
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||||
|
OR (a.resource = 'database' AND a.resource_id IN (
|
||||||
|
SELECT ds.id::uuid FROM database_services ds JOIN projects p ON p.id = ds.project_id
|
||||||
|
WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||||
|
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||||
|
)`
|
||||||
|
|
||||||
|
const activitySelect = `SELECT
|
||||||
|
a.id, COALESCE(a.user_id::text, ''), COALESCE(u.email, ''),
|
||||||
|
a.resource, COALESCE(a.resource_id::text, ''), a.action,
|
||||||
|
COALESCE(a.details::text, '{}'),
|
||||||
|
COALESCE(a.ip_address::text, ''), COALESCE(a.user_agent, ''),
|
||||||
|
COALESCE(a.severity, 'info'), COALESCE(a.category, ''), COALESCE(a.label, ''),
|
||||||
|
a.created_at
|
||||||
|
FROM audit_logs a
|
||||||
|
LEFT JOIN users u ON u.id = a.user_id`
|
||||||
|
|
||||||
|
func scanActivityRows(rows *sql.Rows) []AuditLog {
|
||||||
|
var logs []AuditLog
|
||||||
|
for rows.Next() {
|
||||||
|
var l AuditLog
|
||||||
|
if err := rows.Scan(&l.ID, &l.UserID, &l.UserEmail, &l.Resource, &l.ResourceID,
|
||||||
|
&l.Action, &l.Details, &l.IPAddress, &l.UserAgent,
|
||||||
|
&l.Severity, &l.Category, &l.Label, &l.CreatedAt); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Rows written before enrichment carry empty metadata — classify on read.
|
||||||
|
if l.Label == "" || l.Category == "" {
|
||||||
|
sev, cat, label := classifyAuditEvent(l.Resource, l.Action)
|
||||||
|
if l.Severity == "" || l.Severity == "info" {
|
||||||
|
l.Severity = sev
|
||||||
|
}
|
||||||
|
if l.Category == "" {
|
||||||
|
l.Category = cat
|
||||||
|
}
|
||||||
|
if l.Label == "" {
|
||||||
|
l.Label = label
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logs = append(logs, l)
|
||||||
|
}
|
||||||
|
return logs
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /activity — global feed. Filters: severity, category, resource,
|
||||||
|
// project_id, since, page, limit.
|
||||||
|
func handleGetActivity(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
isAdmin := contextIsAdmin(c)
|
||||||
|
|
||||||
|
page := parsePositiveInt(c.DefaultQuery("page", "1"), 1)
|
||||||
|
limit := parsePositiveInt(c.DefaultQuery("limit", "50"), 50)
|
||||||
|
if limit > 200 {
|
||||||
|
limit = 200
|
||||||
|
}
|
||||||
|
offset := (page - 1) * limit
|
||||||
|
|
||||||
|
conditions := []string{activityVisibility}
|
||||||
|
args := []interface{}{userID, isAdmin}
|
||||||
|
nextArg := 3
|
||||||
|
|
||||||
|
if v := strings.TrimSpace(c.Query("severity")); v != "" {
|
||||||
|
conditions = append(conditions, fmt.Sprintf("a.severity = $%d", nextArg))
|
||||||
|
args = append(args, v)
|
||||||
|
nextArg++
|
||||||
|
}
|
||||||
|
if v := strings.TrimSpace(c.Query("category")); v != "" {
|
||||||
|
conditions = append(conditions, fmt.Sprintf("a.category = $%d", nextArg))
|
||||||
|
args = append(args, v)
|
||||||
|
nextArg++
|
||||||
|
}
|
||||||
|
if v := strings.TrimSpace(c.Query("resource")); v != "" {
|
||||||
|
conditions = append(conditions, fmt.Sprintf("a.resource = $%d", nextArg))
|
||||||
|
args = append(args, v)
|
||||||
|
nextArg++
|
||||||
|
}
|
||||||
|
if v := strings.TrimSpace(c.Query("project_id")); v != "" {
|
||||||
|
if _, err := uuid.Parse(v); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "invalid project_id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conditions = append(conditions, fmt.Sprintf(`(
|
||||||
|
(a.resource = 'project' AND a.resource_id = $%d::uuid)
|
||||||
|
OR (a.resource = 'service' AND a.resource_id IN (SELECT id FROM services WHERE project_id = $%d::uuid))
|
||||||
|
OR (a.resource = 'deployment' AND a.resource_id IN (
|
||||||
|
SELECT d.id FROM deployments d JOIN services s ON s.id = d.service_id WHERE s.project_id = $%d::uuid))
|
||||||
|
OR (a.resource = 'database' AND a.resource_id IN (SELECT id::uuid FROM database_services WHERE project_id = $%d::uuid))
|
||||||
|
)`, nextArg, nextArg, nextArg, nextArg))
|
||||||
|
args = append(args, v)
|
||||||
|
nextArg++
|
||||||
|
}
|
||||||
|
if v := strings.TrimSpace(c.Query("since")); v != "" {
|
||||||
|
sinceTime, err := time.Parse(time.RFC3339, v)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "invalid since timestamp, expected RFC3339")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conditions = append(conditions, fmt.Sprintf("a.created_at >= $%d", nextArg))
|
||||||
|
args = append(args, sinceTime)
|
||||||
|
nextArg++
|
||||||
|
}
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`%s WHERE %s ORDER BY a.created_at DESC LIMIT $%d OFFSET $%d`,
|
||||||
|
activitySelect, strings.Join(conditions, " AND "), nextArg, nextArg+1)
|
||||||
|
args = append(args, limit, offset)
|
||||||
|
|
||||||
|
rows, err := db.Query(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to fetch activity")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
logs := scanActivityRows(rows)
|
||||||
|
if logs == nil {
|
||||||
|
logs = []AuditLog{}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"activity": logs,
|
||||||
|
"page": page,
|
||||||
|
"limit": limit,
|
||||||
|
"has_more": len(logs) == limit,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /projects/:id/activity — per-project timeline. Resource correlation
|
||||||
|
// mirrors the global feed, pinned to this project.
|
||||||
|
func handleGetProjectActivity(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
projectID := strings.TrimSpace(c.Param("id"))
|
||||||
|
if _, err := uuid.Parse(projectID); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid project id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Access check: approved / owner / member / admin.
|
||||||
|
var visible bool
|
||||||
|
if err := db.QueryRow(
|
||||||
|
`SELECT p.is_approved OR p.owner_id = $2::uuid OR $3::bool
|
||||||
|
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $2::uuid)
|
||||||
|
FROM projects p WHERE p.id = $1`,
|
||||||
|
projectID, userID, contextIsAdmin(c),
|
||||||
|
).Scan(&visible); err != nil || !visible {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "project not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
limit := parsePositiveInt(c.DefaultQuery("limit", "50"), 50)
|
||||||
|
if limit > 200 {
|
||||||
|
limit = 200
|
||||||
|
}
|
||||||
|
page := parsePositiveInt(c.DefaultQuery("page", "1"), 1)
|
||||||
|
|
||||||
|
rows, err := db.Query(fmt.Sprintf(`%s WHERE (
|
||||||
|
(a.resource = 'project' AND a.resource_id = $1::uuid)
|
||||||
|
OR (a.resource = 'service' AND a.resource_id IN (SELECT id FROM services WHERE project_id = $1::uuid))
|
||||||
|
OR (a.resource = 'deployment' AND a.resource_id IN (
|
||||||
|
SELECT d.id FROM deployments d JOIN services s ON s.id = d.service_id WHERE s.project_id = $1::uuid))
|
||||||
|
OR (a.resource = 'database' AND a.resource_id IN (SELECT id::uuid FROM database_services WHERE project_id = $1::uuid))
|
||||||
|
) ORDER BY a.created_at DESC LIMIT $2 OFFSET $3`,
|
||||||
|
activitySelect), projectID, limit, (page-1)*limit)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to fetch activity")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
logs := scanActivityRows(rows)
|
||||||
|
if logs == nil {
|
||||||
|
logs = []AuditLog{}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"activity": logs})
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartAuditRetention prunes audit rows past the retention window once at
|
||||||
|
// boot and then daily. AUDIT_RETENTION_DAYS overrides the 90-day default;
|
||||||
|
// 0 disables pruning entirely.
|
||||||
|
func StartAuditRetention(ctx context.Context, db *database.DB) {
|
||||||
|
days := 90
|
||||||
|
if raw := strings.TrimSpace(os.Getenv("AUDIT_RETENTION_DAYS")); raw != "" {
|
||||||
|
if parsed, err := strconv.Atoi(raw); err == nil {
|
||||||
|
days = parsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Backfill enrichment for rows written before the classifier existed,
|
||||||
|
// batched so a large audit table doesn't hold a giant transaction.
|
||||||
|
go backfillAuditMetadata(db)
|
||||||
|
|
||||||
|
if days <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prune := func() {
|
||||||
|
res, err := db.Exec(`DELETE FROM audit_logs WHERE created_at < NOW() - ($1::int * INTERVAL '1 day')`, days)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("audit retention prune failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if n, _ := res.RowsAffected(); n > 0 {
|
||||||
|
log.Printf("audit retention: pruned %d rows older than %d days", n, days)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prune()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(24 * time.Hour)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
prune()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// backfillAuditMetadata classifies rows written before severity/category/label
|
||||||
|
// existed so filters and the feed see uniform metadata.
|
||||||
|
func backfillAuditMetadata(db *database.DB) {
|
||||||
|
cursor := uuid.Nil.String()
|
||||||
|
for {
|
||||||
|
rows, err := db.Query(
|
||||||
|
`SELECT id::text, resource, action FROM audit_logs
|
||||||
|
WHERE label = '' AND id > $1::uuid ORDER BY id LIMIT 500`, cursor)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("audit backfill scan failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
type row struct{ id, resource, action string }
|
||||||
|
var batch []row
|
||||||
|
for rows.Next() {
|
||||||
|
var r row
|
||||||
|
if rows.Scan(&r.id, &r.resource, &r.action) == nil {
|
||||||
|
batch = append(batch, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rows.Close()
|
||||||
|
if len(batch) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, r := range batch {
|
||||||
|
cursor = r.id
|
||||||
|
sev, cat, label := classifyAuditEvent(r.resource, r.action)
|
||||||
|
if _, err := db.Exec(
|
||||||
|
`UPDATE audit_logs SET severity = $1, category = $2, label = $3 WHERE id = $4::uuid`,
|
||||||
|
sev, cat, label, r.id,
|
||||||
|
); err != nil {
|
||||||
|
log.Printf("audit backfill update failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestClassifyAuditEvent(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
resource, action string
|
||||||
|
wantSev, wantCat, wantLab string
|
||||||
|
}{
|
||||||
|
{"service", "service.create", "success", "service", "Service created"},
|
||||||
|
{"service", "service.delete", "warning", "service", "Service deleted"},
|
||||||
|
{"deployment", "deploy", "success", "deployment", "Deployment deployed"},
|
||||||
|
{"project", "project.create", "success", "project", "Project created"},
|
||||||
|
{"user_invite", "user_invite.create", "success", "system", "User invite created"},
|
||||||
|
{"user_token", "user_token.revoke", "warning", "security", "User token revoked"},
|
||||||
|
{"service", "deploy.fail", "error", "service", "Service fail"},
|
||||||
|
{"user", "impersonate", "warning", "user", "User impersonated"},
|
||||||
|
{"webhook", "update", "info", "webhook", "Webhook updated"},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
sev, cat, lab := classifyAuditEvent(tc.resource, tc.action)
|
||||||
|
if sev != tc.wantSev {
|
||||||
|
t.Errorf("%s/%s: severity = %q, want %q", tc.resource, tc.action, sev, tc.wantSev)
|
||||||
|
}
|
||||||
|
if cat != tc.wantCat {
|
||||||
|
t.Errorf("%s/%s: category = %q, want %q", tc.resource, tc.action, cat, tc.wantCat)
|
||||||
|
}
|
||||||
|
if lab != tc.wantLab {
|
||||||
|
t.Errorf("%s/%s: label = %q, want %q", tc.resource, tc.action, lab, tc.wantLab)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -246,6 +246,57 @@ func handleAdminSetUserAdmin(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"message": "User updated"})
|
c.JSON(http.StatusOK, gin.H{"message": "User updated"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// POST /admin/users/:id/impersonate — mints a 15-minute bearer token acting
|
||||||
|
// as the target user. Admin-only; admin accounts cannot be impersonated
|
||||||
|
// (would blur audit attribution). Every use is audit-logged at mint time;
|
||||||
|
// token carries `impersonated_by` for downstream attribution.
|
||||||
|
func handleAdminImpersonateUser(c *gin.Context) {
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
targetID := strings.TrimSpace(c.Param("id"))
|
||||||
|
if _, err := uuid.Parse(targetID); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid user id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var email, name string
|
||||||
|
var isAdmin bool
|
||||||
|
err := db.QueryRowContext(c.Request.Context(),
|
||||||
|
`SELECT email, COALESCE(name, ''), is_admin FROM users WHERE id = $1`, targetID).
|
||||||
|
Scan(&email, &name, &isAdmin)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "user not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if isAdmin {
|
||||||
|
respondError(c, http.StatusForbidden, "FORBIDDEN", "admin accounts cannot be impersonated")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
actorID := optionalUserID(c)
|
||||||
|
if actorID == targetID {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "cannot impersonate yourself")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expiresAt := time.Now().UTC().Add(15 * time.Minute)
|
||||||
|
jwtSecret := c.MustGet("jwt_secret").(string)
|
||||||
|
token, err := generateImpersonationJWT(targetID, email, actorID, jwtSecret, expiresAt)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "user", targetID, "impersonate", map[string]interface{}{
|
||||||
|
"actor_id": actorID,
|
||||||
|
"target_email": email,
|
||||||
|
"expires_at": expiresAt.Format(time.RFC3339),
|
||||||
|
})
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"token": token,
|
||||||
|
"expires_at": expiresAt.Format(time.RFC3339),
|
||||||
|
"user": gin.H{"id": targetID, "email": email, "name": name},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func handleAdminSetProjectApproval(c *gin.Context) {
|
func handleAdminSetProjectApproval(c *gin.Context) {
|
||||||
db := c.MustGet("db").(*database.DB)
|
db := c.MustGet("db").(*database.DB)
|
||||||
queries := sqlcdb.New(db.DB)
|
queries := sqlcdb.New(db.DB)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
_ "embed"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -36,6 +38,7 @@ type NodeAgent struct {
|
|||||||
Capabilities AgentCapabilities `json:"capabilities"`
|
Capabilities AgentCapabilities `json:"capabilities"`
|
||||||
Resources NodeResources `json:"resources"`
|
Resources NodeResources `json:"resources"`
|
||||||
LastHeartbeat time.Time `json:"last_heartbeat"`
|
LastHeartbeat time.Time `json:"last_heartbeat"`
|
||||||
|
AutoPrune bool `json:"auto_prune"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
Metadata map[string]interface{} `json:"metadata"`
|
Metadata map[string]interface{} `json:"metadata"`
|
||||||
@@ -234,6 +237,7 @@ func agentFromRow(row sqlcdb.NodeAgent) NodeAgent {
|
|||||||
Port: int(row.Port),
|
Port: int(row.Port),
|
||||||
Status: row.Status.String,
|
Status: row.Status.String,
|
||||||
Version: row.Version.String,
|
Version: row.Version.String,
|
||||||
|
AutoPrune: row.AutoPrune,
|
||||||
Metadata: map[string]interface{}{},
|
Metadata: map[string]interface{}{},
|
||||||
}
|
}
|
||||||
if row.LastHeartbeat.Valid {
|
if row.LastHeartbeat.Valid {
|
||||||
@@ -327,6 +331,7 @@ func (h *NodeAgentHandler) RegisterAgent(c *gin.Context) {
|
|||||||
Port int `json:"port" binding:"required"`
|
Port int `json:"port" binding:"required"`
|
||||||
Capabilities AgentCapabilities `json:"capabilities" binding:"required"`
|
Capabilities AgentCapabilities `json:"capabilities" binding:"required"`
|
||||||
AuthToken string `json:"auth_token"`
|
AuthToken string `json:"auth_token"`
|
||||||
|
Mesh map[string]string `json:"mesh"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
@@ -341,12 +346,28 @@ func (h *NodeAgentHandler) RegisterAgent(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx := c.Request.Context()
|
ctx := c.Request.Context()
|
||||||
|
metadata := map[string]interface{}{}
|
||||||
|
// req.Mesh is authoritative when present (newer agents always send it,
|
||||||
|
// possibly empty); nil means an older agent — keep any stored value.
|
||||||
|
if req.Mesh != nil {
|
||||||
|
metadata["mesh"] = req.Mesh
|
||||||
|
}
|
||||||
|
|
||||||
// Re-registering an existing host updates it in place.
|
// Re-registering an existing host updates it in place.
|
||||||
if existing, err := h.q.GetAgentByHostAndIP(ctx, sqlcdb.GetAgentByHostAndIPParams{
|
if existing, err := h.q.GetAgentByHostAndIP(ctx, sqlcdb.GetAgentByHostAndIPParams{
|
||||||
Hostname: req.Hostname,
|
Hostname: req.Hostname,
|
||||||
IpAddress: req.IPAddress,
|
IpAddress: req.IPAddress,
|
||||||
}); err == nil {
|
}); err == nil {
|
||||||
|
if existing.Metadata.Valid && len(existing.Metadata.RawMessage) > 0 {
|
||||||
|
var prev map[string]interface{}
|
||||||
|
if err := json.Unmarshal(existing.Metadata.RawMessage, &prev); err == nil {
|
||||||
|
for k, v := range prev {
|
||||||
|
if _, ok := metadata[k]; !ok {
|
||||||
|
metadata[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
updated, err := h.q.UpdateAgent(ctx, sqlcdb.UpdateAgentParams{
|
updated, err := h.q.UpdateAgent(ctx, sqlcdb.UpdateAgentParams{
|
||||||
ID: existing.ID,
|
ID: existing.ID,
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
@@ -358,7 +379,7 @@ func (h *NodeAgentHandler) RegisterAgent(c *gin.Context) {
|
|||||||
Capabilities: rawJSON(req.Capabilities),
|
Capabilities: rawJSON(req.Capabilities),
|
||||||
Resources: existing.Resources,
|
Resources: existing.Resources,
|
||||||
LastHeartbeat: sql.NullTime{Time: time.Now(), Valid: true},
|
LastHeartbeat: sql.NullTime{Time: time.Now(), Valid: true},
|
||||||
Metadata: existing.Metadata,
|
Metadata: rawJSON(metadata),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update agent"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update agent"})
|
||||||
@@ -408,7 +429,7 @@ func (h *NodeAgentHandler) RegisterAgent(c *gin.Context) {
|
|||||||
Capabilities: rawJSON(req.Capabilities),
|
Capabilities: rawJSON(req.Capabilities),
|
||||||
Resources: rawJSON(resources),
|
Resources: rawJSON(resources),
|
||||||
LastHeartbeat: sql.NullTime{Time: time.Now(), Valid: true},
|
LastHeartbeat: sql.NullTime{Time: time.Now(), Valid: true},
|
||||||
Metadata: rawJSON(map[string]interface{}{}),
|
Metadata: rawJSON(metadata),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create agent"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create agent"})
|
||||||
@@ -489,6 +510,7 @@ func (h *NodeAgentHandler) UpdateAgent(c *gin.Context) {
|
|||||||
Resources: rawJSON(agent.Resources),
|
Resources: rawJSON(agent.Resources),
|
||||||
LastHeartbeat: sql.NullTime{Time: agent.LastHeartbeat, Valid: !agent.LastHeartbeat.IsZero()},
|
LastHeartbeat: sql.NullTime{Time: agent.LastHeartbeat, Valid: !agent.LastHeartbeat.IsZero()},
|
||||||
Metadata: rawJSON(agent.Metadata),
|
Metadata: rawJSON(agent.Metadata),
|
||||||
|
AutoPrune: agent.AutoPrune,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update agent"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update agent"})
|
||||||
@@ -516,6 +538,7 @@ func applyAgentUpdates(agent *NodeAgent, updates map[string]interface{}) {
|
|||||||
remarshal("capabilities", &agent.Capabilities)
|
remarshal("capabilities", &agent.Capabilities)
|
||||||
remarshal("resources", &agent.Resources)
|
remarshal("resources", &agent.Resources)
|
||||||
remarshal("metadata", &agent.Metadata)
|
remarshal("metadata", &agent.Metadata)
|
||||||
|
remarshal("auto_prune", &agent.AutoPrune)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAgent removes an agent
|
// DeleteAgent removes an agent
|
||||||
@@ -803,6 +826,33 @@ func (h *NodeAgentHandler) CreateContainer(c *gin.Context) {
|
|||||||
c.JSON(http.StatusCreated, gin.H{"container": container})
|
c.JSON(http.StatusCreated, gin.H{"container": container})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PruneAgent enqueues a bounded `docker system prune -af` command. Volumes
|
||||||
|
// stay unless the caller explicitly opts in.
|
||||||
|
func (h *NodeAgentHandler) PruneAgent(c *gin.Context) {
|
||||||
|
agentID := c.Param("id")
|
||||||
|
if _, err := h.q.GetAgent(c.Request.Context(), agentID); err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Agent not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req struct {
|
||||||
|
Until string `json:"until"` // e.g. "168h" — only prune objects older than this
|
||||||
|
Volumes bool `json:"volumes"` // destructive — off by default
|
||||||
|
}
|
||||||
|
_ = c.ShouldBindJSON(&req)
|
||||||
|
|
||||||
|
command, err := h.q.CreateCommand(c.Request.Context(), sqlcdb.CreateCommandParams{
|
||||||
|
ID: uuid.New().String(),
|
||||||
|
Type: "prune",
|
||||||
|
NodeAgentID: agentID,
|
||||||
|
Payload: rawJSON(map[string]interface{}{"until": req.Until, "volumes": req.Volumes}),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to enqueue prune"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{"command": command})
|
||||||
|
}
|
||||||
|
|
||||||
// ExecuteCommand executes a command on an agent
|
// ExecuteCommand executes a command on an agent
|
||||||
func (h *NodeAgentHandler) ExecuteCommand(c *gin.Context) {
|
func (h *NodeAgentHandler) ExecuteCommand(c *gin.Context) {
|
||||||
agentID := c.Param("id")
|
agentID := c.Param("id")
|
||||||
@@ -1214,9 +1264,45 @@ func (h *NodeAgentHandler) SetupPublicRoutes(router *gin.RouterGroup) {
|
|||||||
agents.POST("/heartbeat", h.SendHeartbeat)
|
agents.POST("/heartbeat", h.SendHeartbeat)
|
||||||
agents.GET("/:id/commands", h.GetPendingCommandsForAgent)
|
agents.GET("/:id/commands", h.GetPendingCommandsForAgent)
|
||||||
agents.POST("/:id/commands/:commandId/result", h.CompleteCommand)
|
agents.POST("/:id/commands/:commandId/result", h.CompleteCommand)
|
||||||
|
|
||||||
|
// Bootstrap surface — the install script and agent binaries are
|
||||||
|
// public; the enroll token is the actual secret.
|
||||||
|
agents.GET("/install.sh", h.ServeInstallScript)
|
||||||
|
agents.GET("/download/:platform", h.ServeAgentBinary)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed assets/install-agent.sh
|
||||||
|
var agentInstallScript string
|
||||||
|
|
||||||
|
// ServeInstallScript returns the agent bootstrap script. The script
|
||||||
|
// downloads the binary from this instance first, GH releases second.
|
||||||
|
func (h *NodeAgentHandler) ServeInstallScript(c *gin.Context) {
|
||||||
|
c.Data(http.StatusOK, "text/x-sh", []byte(agentInstallScript))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeAgentBinary streams a bundled agent binary. Images ship the
|
||||||
|
// linux binaries under CONTAINR_AGENT_BIN_DIR (default /agents).
|
||||||
|
func (h *NodeAgentHandler) ServeAgentBinary(c *gin.Context) {
|
||||||
|
platform := c.Param("platform")
|
||||||
|
if platform != "linux-amd64" && platform != "linux-arm64" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported platform"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dir := os.Getenv("CONTAINR_AGENT_BIN_DIR")
|
||||||
|
if dir == "" {
|
||||||
|
dir = "/agents"
|
||||||
|
}
|
||||||
|
path := filepath.Join(dir, "containr-agent-"+platform)
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil || info.IsDir() {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "no bundled binary for " + platform})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Header("Content-Disposition", `attachment; filename="containr-agent"`)
|
||||||
|
c.File(path)
|
||||||
|
}
|
||||||
|
|
||||||
// SetupAdminRoutes registers dashboard/control agent routes. Command
|
// SetupAdminRoutes registers dashboard/control agent routes. Command
|
||||||
// history and container details can leak sensitive output, so these stay
|
// history and container details can leak sensitive output, so these stay
|
||||||
// admin-only rather than joining the public read group.
|
// admin-only rather than joining the public read group.
|
||||||
@@ -1243,6 +1329,7 @@ func (h *NodeAgentHandler) SetupAdminRoutes(router *gin.RouterGroup) {
|
|||||||
agents.DELETE("/:id/containers/:containerId", h.ContainerAction)
|
agents.DELETE("/:id/containers/:containerId", h.ContainerAction)
|
||||||
|
|
||||||
agents.GET("/:id/metrics", h.GetAgentMetrics)
|
agents.GET("/:id/metrics", h.GetAgentMetrics)
|
||||||
|
agents.POST("/:id/prune", h.PruneAgent)
|
||||||
agents.POST("/:id/commands", h.ExecuteCommand)
|
agents.POST("/:id/commands", h.ExecuteCommand)
|
||||||
agents.GET("/:id/commands", h.GetAgentCommands)
|
agents.GET("/:id/commands", h.GetAgentCommands)
|
||||||
agents.GET("/:id/commands/:commandId", h.GetCommandStatus)
|
agents.GET("/:id/commands/:commandId", h.GetCommandStatus)
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Containr node agent installer.
|
||||||
|
# curl -fsSL <api>/api/agents/install.sh | bash -s -- --url <api> --token <enroll-token>
|
||||||
|
#
|
||||||
|
# One-shot: downloads the agent binary from the Containr instance (or GH
|
||||||
|
# releases as fallback), writes a systemd unit, starts it. SSH is never
|
||||||
|
# needed again — the agent heartbeats and polls commands outbound.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
API_URL=""
|
||||||
|
TOKEN=""
|
||||||
|
AGENT_NAME=""
|
||||||
|
VERSION=""
|
||||||
|
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
--url) API_URL="$2"; shift 2 ;;
|
||||||
|
--token) TOKEN="$2"; shift 2 ;;
|
||||||
|
--name) AGENT_NAME="$2"; shift 2 ;;
|
||||||
|
--version) VERSION="$2"; shift 2 ;;
|
||||||
|
*) echo "unknown flag: $1" >&2; exit 2 ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
[ -n "$API_URL" ] || { echo "--url is required" >&2; exit 2; }
|
||||||
|
[ -n "$TOKEN" ] || { echo "--token is required (create one: containr nodes tokens create)" >&2; exit 2; }
|
||||||
|
API_URL="${API_URL%/}"
|
||||||
|
API_URL="${API_URL%/api/v1}"
|
||||||
|
API_URL="${API_URL%/api}"
|
||||||
|
|
||||||
|
info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
|
||||||
|
err() { printf '\033[1;31mError:\033[0m %s\n' "$*" >&2; }
|
||||||
|
|
||||||
|
[ "$(id -u)" = "0" ] || { err "run as root (sudo bash -s -- --url … --token …)"; exit 1; }
|
||||||
|
command -v docker >/dev/null 2>&1 || { err "docker not found"; exit 1; }
|
||||||
|
command -v curl >/dev/null 2>&1 || { err "curl not found"; exit 1; }
|
||||||
|
|
||||||
|
ARCH="$(uname -m)"
|
||||||
|
case "$ARCH" in
|
||||||
|
x86_64|amd64) ARCH=amd64 ;;
|
||||||
|
aarch64|arm64) ARCH=arm64 ;;
|
||||||
|
*) err "unsupported architecture: $ARCH"; exit 1 ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
BIN=/usr/local/bin/containr-agent
|
||||||
|
info "downloading containr-agent (linux/$ARCH)"
|
||||||
|
if ! curl -fsSL "$API_URL/api/agents/download/linux-$ARCH" -o "$BIN" 2>/dev/null; then
|
||||||
|
info "instance has no bundled binary — trying GitHub releases"
|
||||||
|
REF="${VERSION:-latest}"
|
||||||
|
if [ "$REF" = "latest" ]; then
|
||||||
|
DL="https://github.com/Dvorinka/Containr/releases/latest/download/containr-agent-linux-$ARCH"
|
||||||
|
else
|
||||||
|
DL="https://github.com/Dvorinka/Containr/releases/download/$REF/containr-agent-linux-$ARCH"
|
||||||
|
fi
|
||||||
|
curl -fsSL "$DL" -o "$BIN" || { err "agent download failed"; exit 1; }
|
||||||
|
fi
|
||||||
|
chmod +x "$BIN"
|
||||||
|
|
||||||
|
UNIT=/etc/systemd/system/containr-agent.service
|
||||||
|
info "writing systemd unit"
|
||||||
|
cat > "$UNIT" <<UNIT
|
||||||
|
[Unit]
|
||||||
|
Description=Containr node agent
|
||||||
|
After=docker.service network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
Requires=docker.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Environment=CONTAINR_API_URL=$API_URL
|
||||||
|
Environment=CONTAINR_AGENT_AUTH_TOKEN=$TOKEN
|
||||||
|
Environment=CONTAINR_AGENT_NAME=${AGENT_NAME:-$(hostname)}
|
||||||
|
Restart=always
|
||||||
|
RestartSec=5
|
||||||
|
ExecStart=$BIN
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
UNIT
|
||||||
|
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable --now containr-agent
|
||||||
|
info "agent installed and started — it should appear in Containr within ~15s"
|
||||||
@@ -23,9 +23,67 @@ type AuditLog struct {
|
|||||||
Details string `json:"details" db:"details"`
|
Details string `json:"details" db:"details"`
|
||||||
IPAddress string `json:"ip_address" db:"ip_address"`
|
IPAddress string `json:"ip_address" db:"ip_address"`
|
||||||
UserAgent string `json:"user_agent" db:"user_agent"`
|
UserAgent string `json:"user_agent" db:"user_agent"`
|
||||||
|
Severity string `json:"severity" db:"severity"`
|
||||||
|
Category string `json:"category" db:"category"`
|
||||||
|
Label string `json:"label" db:"label"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// auditVerbs maps action verbs to past-tense words for feed labels.
|
||||||
|
var auditVerbs = map[string]string{
|
||||||
|
"create": "created", "delete": "deleted", "update": "updated",
|
||||||
|
"deploy": "deployed", "clone": "cloned", "move": "moved",
|
||||||
|
"backup": "backed up", "restore": "restored", "revoke": "revoked",
|
||||||
|
"impersonate": "impersonated", "provision": "provisioned",
|
||||||
|
"accept": "accepted", "login": "logged in", "scale": "scaled",
|
||||||
|
"sleep": "put to sleep", "wake": "woke", "approve": "approved",
|
||||||
|
"set_admin": "admin flag changed", "provisioned": "provisioned",
|
||||||
|
}
|
||||||
|
|
||||||
|
// classifyAuditEvent derives severity/category/label for the activity feed.
|
||||||
|
// Actions may already carry the resource prefix ("service.clone") — it is
|
||||||
|
// stripped before matching.
|
||||||
|
func classifyAuditEvent(resource, action string) (severity, category, label string) {
|
||||||
|
verb := action
|
||||||
|
if idx := strings.LastIndex(verb, "."); idx >= 0 {
|
||||||
|
verb = verb[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
category = resource
|
||||||
|
switch resource {
|
||||||
|
case "user_invite", "invite", "github_app", "banner", "settings":
|
||||||
|
category = "system"
|
||||||
|
case "user_token", "agent_token", "registry_credential":
|
||||||
|
category = "security"
|
||||||
|
}
|
||||||
|
|
||||||
|
severity = "info"
|
||||||
|
switch {
|
||||||
|
case strings.Contains(verb, "fail"), strings.Contains(verb, "error"), strings.Contains(verb, "denied"):
|
||||||
|
severity = "error"
|
||||||
|
case strings.Contains(verb, "delete"), strings.Contains(verb, "revoke"),
|
||||||
|
strings.Contains(verb, "remove"), strings.Contains(verb, "disable"),
|
||||||
|
strings.Contains(verb, "impersonate"):
|
||||||
|
severity = "warning"
|
||||||
|
case strings.Contains(verb, "create"), strings.Contains(verb, "success"),
|
||||||
|
strings.Contains(verb, "complete"), strings.Contains(verb, "accept"),
|
||||||
|
strings.Contains(verb, "provision"), strings.Contains(verb, "clone"),
|
||||||
|
strings.Contains(verb, "deploy"), strings.Contains(verb, "approve"):
|
||||||
|
severity = "success"
|
||||||
|
}
|
||||||
|
|
||||||
|
past := verb
|
||||||
|
if v, ok := auditVerbs[verb]; ok {
|
||||||
|
past = v
|
||||||
|
}
|
||||||
|
resName := strings.ReplaceAll(resource, "_", " ")
|
||||||
|
if resName != "" {
|
||||||
|
resName = strings.ToUpper(resName[:1]) + resName[1:]
|
||||||
|
}
|
||||||
|
label = resName + " " + past
|
||||||
|
return severity, category, label
|
||||||
|
}
|
||||||
|
|
||||||
type AuditLogDetail struct {
|
type AuditLogDetail struct {
|
||||||
OldValue interface{} `json:"old_value,omitempty"`
|
OldValue interface{} `json:"old_value,omitempty"`
|
||||||
NewValue interface{} `json:"new_value,omitempty"`
|
NewValue interface{} `json:"new_value,omitempty"`
|
||||||
@@ -42,21 +100,27 @@ func LogAudit(userID, resource, resourceID, action string, details map[string]in
|
|||||||
detailsJSON, _ := json.Marshal(details)
|
detailsJSON, _ := json.Marshal(details)
|
||||||
resourceUUID := parseUUIDOrNil(resourceID)
|
resourceUUID := parseUUIDOrNil(resourceID)
|
||||||
userUUID := parseUUIDOrNil(userID)
|
userUUID := parseUUIDOrNil(userID)
|
||||||
|
severity, category, label := classifyAuditEvent(resource, action)
|
||||||
|
|
||||||
auditID := uuid.New().String()
|
auditID := uuid.New().String()
|
||||||
_, err := db.Exec(
|
_, err := db.Exec(
|
||||||
`INSERT INTO audit_logs (id, user_id, resource, resource_id, action, details, created_at)
|
`INSERT INTO audit_logs (id, user_id, resource, resource_id, action, details, severity, category, label, created_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,
|
||||||
auditID, userUUID, resource, resourceUUID, action, string(detailsJSON), time.Now().UTC(),
|
auditID, userUUID, resource, resourceUUID, action, string(detailsJSON), severity, category, label, time.Now().UTC(),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
emitWebhookEvent(userID, resource, resourceID, action, details)
|
||||||
}
|
}
|
||||||
|
|
||||||
func LogAuditWithRequest(c *gin.Context, resource, resourceID, action string, details map[string]interface{}) {
|
func LogAuditWithRequest(c *gin.Context, resource, resourceID, action string, details map[string]interface{}) {
|
||||||
userID, _ := c.Get("user_id")
|
userID, _ := c.Get("user_id")
|
||||||
|
|
||||||
|
if details == nil {
|
||||||
|
details = map[string]interface{}{}
|
||||||
|
}
|
||||||
details["ip_address"] = c.ClientIP()
|
details["ip_address"] = c.ClientIP()
|
||||||
details["user_agent"] = c.GetHeader("User-Agent")
|
details["user_agent"] = c.GetHeader("User-Agent")
|
||||||
|
|
||||||
@@ -69,16 +133,19 @@ func LogAuditWithRequest(c *gin.Context, resource, resourceID, action string, de
|
|||||||
}
|
}
|
||||||
userUUID := parseUUIDOrNil(userIDStr)
|
userUUID := parseUUIDOrNil(userIDStr)
|
||||||
resourceUUID := parseUUIDOrNil(resourceID)
|
resourceUUID := parseUUIDOrNil(resourceID)
|
||||||
|
severity, category, label := classifyAuditEvent(resource, action)
|
||||||
|
|
||||||
auditID := uuid.New().String()
|
auditID := uuid.New().String()
|
||||||
_, err := db.Exec(
|
_, err := db.Exec(
|
||||||
`INSERT INTO audit_logs (id, user_id, resource, resource_id, action, details, ip_address, user_agent, created_at)
|
`INSERT INTO audit_logs (id, user_id, resource, resource_id, action, details, ip_address, user_agent, severity, category, label, created_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9)`,
|
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9, $10, $11, $12)`,
|
||||||
auditID, userUUID, resource, resourceUUID, action, string(detailsJSON), c.ClientIP(), c.GetHeader("User-Agent"), time.Now().UTC(),
|
auditID, userUUID, resource, resourceUUID, action, string(detailsJSON), c.ClientIP(), c.GetHeader("User-Agent"), severity, category, label, time.Now().UTC(),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
emitWebhookEvent(userIDStr, resource, resourceID, action, details)
|
||||||
}
|
}
|
||||||
|
|
||||||
var auditDB *database.DB
|
var auditDB *database.DB
|
||||||
@@ -151,6 +218,9 @@ func handleGetAuditLogs(c *gin.Context) {
|
|||||||
COALESCE(a.details::text, '{}'),
|
COALESCE(a.details::text, '{}'),
|
||||||
COALESCE(a.ip_address::text, ''),
|
COALESCE(a.ip_address::text, ''),
|
||||||
COALESCE(a.user_agent, ''),
|
COALESCE(a.user_agent, ''),
|
||||||
|
COALESCE(a.severity, 'info'),
|
||||||
|
COALESCE(a.category, ''),
|
||||||
|
COALESCE(a.label, ''),
|
||||||
a.created_at
|
a.created_at
|
||||||
FROM audit_logs a
|
FROM audit_logs a
|
||||||
LEFT JOIN users u ON u.id = a.user_id
|
LEFT JOIN users u ON u.id = a.user_id
|
||||||
@@ -169,7 +239,7 @@ func handleGetAuditLogs(c *gin.Context) {
|
|||||||
var logs []AuditLog
|
var logs []AuditLog
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var log AuditLog
|
var log AuditLog
|
||||||
err := rows.Scan(&log.ID, &log.UserID, &log.UserEmail, &log.Resource, &log.ResourceID, &log.Action, &log.Details, &log.IPAddress, &log.UserAgent, &log.CreatedAt)
|
err := rows.Scan(&log.ID, &log.UserID, &log.UserEmail, &log.Resource, &log.ResourceID, &log.Action, &log.Details, &log.IPAddress, &log.UserAgent, &log.Severity, &log.Category, &log.Label, &log.CreatedAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -546,6 +546,19 @@ func generateJWT(userID, email, secret string) (string, error) {
|
|||||||
return token.SignedString([]byte(secret))
|
return token.SignedString([]byte(secret))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generateImpersonationJWT mints a short-lived token acting as `userID`,
|
||||||
|
// tagged with the admin who minted it so downstream callers can attribute
|
||||||
|
// actions taken while impersonating.
|
||||||
|
func generateImpersonationJWT(userID, email, actorID, secret string, expiresAt time.Time) (string, error) {
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
|
"user_id": userID,
|
||||||
|
"email": email,
|
||||||
|
"impersonated_by": actorID,
|
||||||
|
"exp": expiresAt.Unix(),
|
||||||
|
})
|
||||||
|
return token.SignedString([]byte(secret))
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateJWT validates a JWT token and returns the claims
|
// ValidateJWT validates a JWT token and returns the claims
|
||||||
func ValidateJWT(tokenString, secret string) (jwt.MapClaims, error) {
|
func ValidateJWT(tokenString, secret string) (jwt.MapClaims, error) {
|
||||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
|||||||
@@ -0,0 +1,646 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database/sqlcdb"
|
||||||
|
"containr/internal/docker"
|
||||||
|
"containr/internal/secrets"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/mount"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/minio/minio-go/v7"
|
||||||
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BackupTarget is an S3-compatible archive destination. Secret keys are
|
||||||
|
// stored encrypted and never serialized — has_credentials signals presence.
|
||||||
|
type BackupTarget struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Endpoint string `json:"endpoint"`
|
||||||
|
Bucket string `json:"bucket"`
|
||||||
|
Region string `json:"region,omitempty"`
|
||||||
|
Prefix string `json:"prefix,omitempty"`
|
||||||
|
HasCredentials bool `json:"has_credentials"`
|
||||||
|
UseTLS bool `json:"use_tls"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
|
||||||
|
accessKey string `json:"-"`
|
||||||
|
secretKey string `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type backupTargetRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Endpoint string `json:"endpoint" binding:"required"`
|
||||||
|
Bucket string `json:"bucket" binding:"required"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
Prefix string `json:"prefix"`
|
||||||
|
AccessKey string `json:"access_key"`
|
||||||
|
SecretKey string `json:"secret_key"`
|
||||||
|
UseTLS *bool `json:"use_tls"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapBackupTargetRow(row sqlcdb.BackupTarget) BackupTarget {
|
||||||
|
return BackupTarget{
|
||||||
|
ID: row.ID,
|
||||||
|
Name: row.Name,
|
||||||
|
Endpoint: row.Endpoint,
|
||||||
|
Bucket: row.Bucket,
|
||||||
|
Region: row.Region,
|
||||||
|
Prefix: row.Prefix,
|
||||||
|
HasCredentials: row.SecretKey != "",
|
||||||
|
UseTLS: row.UseTls,
|
||||||
|
CreatedAt: databaseNullTime(row.CreatedAt),
|
||||||
|
UpdatedAt: databaseNullTime(row.UpdatedAt),
|
||||||
|
accessKey: secrets.Decrypt(row.AccessKey),
|
||||||
|
secretKey: secrets.Decrypt(row.SecretKey),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// s3Client builds a client for the target. Endpoint is a bare host[:port] —
|
||||||
|
// scheme comes from use_tls, matching MinIO/Garage/AWS conventions.
|
||||||
|
func (t BackupTarget) s3Client() (*minio.Client, error) {
|
||||||
|
opts := &minio.Options{Secure: t.UseTLS, Region: t.Region}
|
||||||
|
if t.accessKey != "" || t.secretKey != "" {
|
||||||
|
opts.Creds = credentials.NewStaticV4(t.accessKey, t.secretKey, "")
|
||||||
|
} else {
|
||||||
|
opts.Creds = credentials.NewIAM("")
|
||||||
|
}
|
||||||
|
return minio.New(t.Endpoint, opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeBackupTargetRequest(req *backupTargetRequest) error {
|
||||||
|
req.Name = strings.TrimSpace(req.Name)
|
||||||
|
req.Endpoint = strings.TrimSpace(req.Endpoint)
|
||||||
|
req.Endpoint = strings.TrimPrefix(strings.TrimPrefix(req.Endpoint, "https://"), "http://")
|
||||||
|
req.Endpoint = strings.TrimSuffix(req.Endpoint, "/")
|
||||||
|
req.Bucket = strings.TrimSpace(req.Bucket)
|
||||||
|
req.Prefix = strings.Trim(strings.TrimSpace(req.Prefix), "/")
|
||||||
|
if req.Name == "" {
|
||||||
|
return errors.New("name is required")
|
||||||
|
}
|
||||||
|
if req.Endpoint == "" || req.Bucket == "" {
|
||||||
|
return errors.New("endpoint and bucket are required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// probeBackupTarget verifies credentials and bucket access.
|
||||||
|
func probeBackupTarget(ctx context.Context, t BackupTarget) (time.Duration, error) {
|
||||||
|
client, err := t.s3Client()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
start := time.Now()
|
||||||
|
exists, err := client.BucketExists(ctx, t.Bucket)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return 0, fmt.Errorf("bucket %q not found or not accessible", t.Bucket)
|
||||||
|
}
|
||||||
|
return time.Since(start), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DatabaseHandler) ListBackupTargets(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rows, err := h.queries.ListBackupTargetsByUser(c.Request.Context(), userID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list backup targets"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targets := make([]BackupTarget, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
t := mapBackupTargetRow(row)
|
||||||
|
t.accessKey, t.secretKey = "", ""
|
||||||
|
targets = append(targets, t)
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"backup_targets": targets})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DatabaseHandler) CreateBackupTarget(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req backupTargetRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := normalizeBackupTargetRequest(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
useTLS := true
|
||||||
|
if req.UseTLS != nil {
|
||||||
|
useTLS = *req.UseTLS
|
||||||
|
}
|
||||||
|
target := BackupTarget{
|
||||||
|
ID: "bt_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:12],
|
||||||
|
Name: req.Name,
|
||||||
|
Endpoint: req.Endpoint,
|
||||||
|
Bucket: req.Bucket,
|
||||||
|
Region: req.Region,
|
||||||
|
Prefix: req.Prefix,
|
||||||
|
UseTLS: useTLS,
|
||||||
|
accessKey: req.AccessKey,
|
||||||
|
secretKey: req.SecretKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if _, err := probeBackupTarget(probeCtx, target); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Connection check failed: %v", err), "code": "DEPENDENCY_UNAVAILABLE"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
enc := func(v string) string {
|
||||||
|
if v == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return secrets.Encrypt(v)
|
||||||
|
}
|
||||||
|
now := sql.NullTime{Time: time.Now(), Valid: true}
|
||||||
|
if err := h.queries.CreateBackupTarget(c.Request.Context(), sqlcdb.CreateBackupTargetParams{
|
||||||
|
ID: target.ID,
|
||||||
|
UserID: userID,
|
||||||
|
Name: target.Name,
|
||||||
|
Endpoint: target.Endpoint,
|
||||||
|
Bucket: target.Bucket,
|
||||||
|
Region: target.Region,
|
||||||
|
Prefix: target.Prefix,
|
||||||
|
AccessKey: enc(req.AccessKey),
|
||||||
|
SecretKey: enc(req.SecretKey),
|
||||||
|
UseTls: useTLS,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create backup target"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
LogAudit(userID, "backup_target", target.ID, "create", map[string]interface{}{"name": req.Name, "endpoint": req.Endpoint, "bucket": req.Bucket})
|
||||||
|
target.accessKey, target.secretKey = "", ""
|
||||||
|
target.HasCredentials = req.SecretKey != ""
|
||||||
|
target.CreatedAt, target.UpdatedAt = now.Time, now.Time
|
||||||
|
c.JSON(http.StatusCreated, gin.H{"backup_target": target})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DatabaseHandler) UpdateBackupTarget(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetID := c.Param("id")
|
||||||
|
row, err := h.queries.GetBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.GetBackupTargetByIDAndUserParams{ID: targetID, UserID: userID})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Backup target not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req backupTargetRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := normalizeBackupTargetRequest(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
useTLS := row.UseTls
|
||||||
|
if req.UseTLS != nil {
|
||||||
|
useTLS = *req.UseTLS
|
||||||
|
}
|
||||||
|
// Empty credentials keep the stored pair.
|
||||||
|
accessKey := row.AccessKey
|
||||||
|
secretKey := row.SecretKey
|
||||||
|
if req.AccessKey != "" || req.SecretKey != "" {
|
||||||
|
accessKey = secrets.Encrypt(req.AccessKey)
|
||||||
|
secretKey = secrets.Encrypt(req.SecretKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
target := BackupTarget{
|
||||||
|
Endpoint: req.Endpoint,
|
||||||
|
Bucket: req.Bucket,
|
||||||
|
Region: req.Region,
|
||||||
|
Prefix: req.Prefix,
|
||||||
|
UseTLS: useTLS,
|
||||||
|
accessKey: secrets.Decrypt(accessKey),
|
||||||
|
secretKey: secrets.Decrypt(secretKey),
|
||||||
|
}
|
||||||
|
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if _, err := probeBackupTarget(probeCtx, target); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Connection check failed: %v", err), "code": "DEPENDENCY_UNAVAILABLE"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.queries.UpdateBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.UpdateBackupTargetByIDAndUserParams{
|
||||||
|
Name: req.Name,
|
||||||
|
Endpoint: req.Endpoint,
|
||||||
|
Bucket: req.Bucket,
|
||||||
|
Region: req.Region,
|
||||||
|
Prefix: req.Prefix,
|
||||||
|
AccessKey: accessKey,
|
||||||
|
SecretKey: secretKey,
|
||||||
|
UseTls: useTLS,
|
||||||
|
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||||
|
ID: targetID,
|
||||||
|
UserID: userID,
|
||||||
|
}); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update backup target"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Backup target updated"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DatabaseHandler) DeleteBackupTarget(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetID := c.Param("id")
|
||||||
|
count, err := h.queries.CountDatabasesUsingBackupTarget(c.Request.Context(), sql.NullString{String: targetID, Valid: true})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check target usage"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
c.JSON(http.StatusConflict, gin.H{"error": fmt.Sprintf("%d database(s) still back up to this target", count), "code": "CONFLICT"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := h.queries.DeleteBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.DeleteBackupTargetByIDAndUserParams{ID: targetID, UserID: userID}); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete backup target"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
LogAudit(userID, "backup_target", targetID, "delete", nil)
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Backup target deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImportBackup accepts a `.tar.gz` archive (multipart `file` field or raw
|
||||||
|
// body) and stores it as a restore point — the counterpart to offsite
|
||||||
|
// export, and the entry path for migrators.
|
||||||
|
func (h *DatabaseHandler) ImportBackup(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
databaseID := c.Param("id")
|
||||||
|
userID = h.effectiveUserID(c, userID, databaseID)
|
||||||
|
|
||||||
|
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||||
|
ID: databaseID, UserID: userID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if row.Provider == "external" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "backup import applies to managed databases — external restores are not supported yet", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buffer to a temp file — tar headers need the size up front, and a
|
||||||
|
// streamed upload can't be rewound for the archive write.
|
||||||
|
src, _, err := openUploadStream(c)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
|
||||||
|
tmp, err := os.CreateTemp("", "containr-backup-import-*")
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to stage upload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() { tmp.Close(); os.Remove(tmp.Name()) }()
|
||||||
|
|
||||||
|
const maxImport = 2 << 30 // 2 GiB ceiling for staged archives
|
||||||
|
size, err := io.Copy(tmp, http.MaxBytesReader(c.Writer, src, maxImport))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "archive too large or unreadable", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read staged upload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
magic := make([]byte, 2)
|
||||||
|
if _, err := tmp.Read(magic); err != nil || magic[0] != 0x1f || magic[1] != 0x8b {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "archive must be gzip-compressed (.tar.gz)", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read staged upload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
backupID := generateDatabaseBackupID(databaseID)
|
||||||
|
archivePath := sanitizeBackupArchivePath(managedDatabaseBackupArchivePath(backupID))
|
||||||
|
|
||||||
|
if _, err := h.dockerClient.CreateVolume(c.Request.Context(), docker.VolumeConfig{Name: managedDatabaseBackupVolume}); err != nil && !strings.Contains(strings.ToLower(err.Error()), "already exists") {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to prepare backup volume"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
holderID, err := h.createBackupVolumeHolder(c.Request.Context())
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to open backup volume: %v", err)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() { _ = h.dockerClient.RemoveContainer(context.Background(), holderID, true) }()
|
||||||
|
|
||||||
|
pr, pw := io.Pipe()
|
||||||
|
tw := tar.NewWriter(pw)
|
||||||
|
go func() {
|
||||||
|
err := tw.WriteHeader(&tar.Header{Name: archivePath, Mode: 0o644, Size: size})
|
||||||
|
if err == nil {
|
||||||
|
_, err = io.Copy(tw, tmp)
|
||||||
|
}
|
||||||
|
if cerr := tw.Close(); err == nil {
|
||||||
|
err = cerr
|
||||||
|
}
|
||||||
|
_ = pw.CloseWithError(err)
|
||||||
|
}()
|
||||||
|
if err := h.dockerClient.CopyToContainer(c.Request.Context(), holderID, "/backup", pr); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to store archive: %v", err)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
now := sql.NullTime{Time: time.Now(), Valid: true}
|
||||||
|
if err := h.queries.CreateDatabaseBackup(c.Request.Context(), sqlcdb.CreateDatabaseBackupParams{
|
||||||
|
ID: backupID,
|
||||||
|
DatabaseID: databaseID,
|
||||||
|
Size: humanReadableBytes(size),
|
||||||
|
Status: "completed",
|
||||||
|
BackupPath: sql.NullString{String: archivePath, Valid: true},
|
||||||
|
CreatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to register backup"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = h.queries.SetDatabaseBackupStatusByID(c.Request.Context(), sqlcdb.SetDatabaseBackupStatusByIDParams{
|
||||||
|
Status: "completed",
|
||||||
|
Size: humanReadableBytes(size),
|
||||||
|
CompletedAt: now,
|
||||||
|
ID: backupID,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Imported archives ship offsite too when a target is configured —
|
||||||
|
// the import should be a first-class restore point.
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
if key, err := h.uploadBackupToTarget(ctx, row, archivePath, backupID); err != nil {
|
||||||
|
log.Printf("containr: imported backup %s remote upload failed: %v (local archive kept)", backupID, err)
|
||||||
|
} else if key != "" {
|
||||||
|
log.Printf("containr: imported backup %s shipped to %s", backupID, key)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
LogAudit(userID, "database", databaseID, "backup_import", map[string]interface{}{"backup_id": backupID, "size": size})
|
||||||
|
c.JSON(http.StatusCreated, gin.H{"backup_id": backupID, "size": humanReadableBytes(size), "status": "completed"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// openUploadStream resolves the request body: multipart `file` field when
|
||||||
|
// present, otherwise the raw body (e.g. CLI pipe of a .tar.gz).
|
||||||
|
func openUploadStream(c *gin.Context) (io.ReadCloser, string, error) {
|
||||||
|
if fh, err := c.FormFile("file"); err == nil {
|
||||||
|
src, err := fh.Open()
|
||||||
|
return src, fh.Filename, err
|
||||||
|
}
|
||||||
|
if c.Request.Body == nil || c.Request.ContentLength == 0 {
|
||||||
|
return nil, "", errors.New("no archive uploaded — send multipart field 'file' or a raw .tar.gz body")
|
||||||
|
}
|
||||||
|
return c.Request.Body, "archive.tar.gz", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DatabaseHandler) TestBackupTarget(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetID := c.Param("id")
|
||||||
|
row, err := h.queries.GetBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.GetBackupTargetByIDAndUserParams{ID: targetID, UserID: userID})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Backup target not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target := mapBackupTargetRow(row)
|
||||||
|
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
latency, probeErr := probeBackupTarget(probeCtx, target)
|
||||||
|
if probeErr != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"ok": false, "error": probeErr.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"ok": true, "latency_ms": latency.Milliseconds()})
|
||||||
|
}
|
||||||
|
|
||||||
|
// uploadBackupToTarget copies the archive out of the backup volume and puts
|
||||||
|
// it in the configured S3 target. Returns the object key, or "" when no
|
||||||
|
// target is configured.
|
||||||
|
func (h *DatabaseHandler) uploadBackupToTarget(ctx context.Context, db sqlcdb.DatabaseService, archivePath, backupID string) (string, error) {
|
||||||
|
if !db.BackupTargetID.Valid || db.BackupTargetID.String == "" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
targetRow, err := h.queries.GetBackupTargetByID(ctx, db.BackupTargetID.String)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("backup target lookup failed: %w", err)
|
||||||
|
}
|
||||||
|
target := mapBackupTargetRow(targetRow)
|
||||||
|
|
||||||
|
archive, err := h.readBackupArchive(ctx, archivePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("read archive: %w", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_, _ = io.Copy(io.Discard, archive)
|
||||||
|
_ = archive.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
client, err := target.s3Client()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
key := target.Prefix
|
||||||
|
if key != "" {
|
||||||
|
key += "/"
|
||||||
|
}
|
||||||
|
key += "databases/" + db.ID + "/" + sanitizeBackupArchivePath(archivePath)
|
||||||
|
_, err = client.PutObject(ctx, target.Bucket, key, archive, -1, minio.PutObjectOptions{ContentType: "application/gzip"})
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("s3 upload: %w", err)
|
||||||
|
}
|
||||||
|
_ = h.queries.SetDatabaseBackupRemoteKeyByID(ctx, sqlcdb.SetDatabaseBackupRemoteKeyByIDParams{
|
||||||
|
RemoteKey: sql.NullString{String: key, Valid: true},
|
||||||
|
ID: backupID,
|
||||||
|
})
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readBackupArchive streams an archive out of the shared backup volume via a
|
||||||
|
// stopped holder container — the volume has no direct host path.
|
||||||
|
func (h *DatabaseHandler) readBackupArchive(ctx context.Context, archivePath string) (io.ReadCloser, error) {
|
||||||
|
holderID, err := h.createBackupVolumeHolder(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
reader, err := h.dockerClient.CopyFromContainer(ctx, holderID, "/backup/"+sanitizeBackupArchivePath(archivePath))
|
||||||
|
if err != nil {
|
||||||
|
_ = h.dockerClient.RemoveContainer(context.Background(), holderID, true)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// The holder must outlive the stream — deleting the source container while
|
||||||
|
// dockerd serves the archive stalls the read without an error.
|
||||||
|
return &archiveReader{tar: reader, cleanup: func() {
|
||||||
|
_ = h.dockerClient.RemoveContainer(context.Background(), holderID, true)
|
||||||
|
}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// downloadBackupToVolume fetches a remote archive back into the backup volume
|
||||||
|
// so the normal restore path can run unchanged.
|
||||||
|
func (h *DatabaseHandler) downloadBackupToVolume(ctx context.Context, db sqlcdb.DatabaseService, remoteKey, archivePath string) error {
|
||||||
|
targetRow, err := h.queries.GetBackupTargetByID(ctx, db.BackupTargetID.String)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("backup target lookup failed: %w", err)
|
||||||
|
}
|
||||||
|
target := mapBackupTargetRow(targetRow)
|
||||||
|
client, err := target.s3Client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
obj, err := client.GetObject(ctx, target.Bucket, remoteKey, minio.GetObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer obj.Close()
|
||||||
|
|
||||||
|
archive := sanitizeBackupArchivePath(archivePath)
|
||||||
|
if archive == "" {
|
||||||
|
return errors.New("backup path is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
tw := tar.NewWriter(&buf)
|
||||||
|
data, err := io.ReadAll(obj)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("s3 download: %w", err)
|
||||||
|
}
|
||||||
|
if err := tw.WriteHeader(&tar.Header{Name: archive, Mode: 0o644, Size: int64(len(data))}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := tw.Write(data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tw.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
holderID, err := h.createBackupVolumeHolder(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() { _ = h.dockerClient.RemoveContainer(context.Background(), holderID, true) }()
|
||||||
|
return h.dockerClient.CopyToContainer(ctx, holderID, "/backup", &buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// backupArchiveExists reports whether the archive is still in the local
|
||||||
|
// backup volume — a copy attempt on a holder container is the cheapest check.
|
||||||
|
func (h *DatabaseHandler) backupArchiveExists(ctx context.Context, archivePath string) (bool, error) {
|
||||||
|
holderID, err := h.createBackupVolumeHolder(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer func() { _ = h.dockerClient.RemoveContainer(context.Background(), holderID, true) }()
|
||||||
|
reader, err := h.dockerClient.CopyFromContainer(ctx, holderID, "/backup/"+sanitizeBackupArchivePath(archivePath))
|
||||||
|
if err != nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
_ = reader.Close()
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// createBackupVolumeHolder creates a container mounting the backup volume so
|
||||||
|
// archives can be copied in and out. It must be started — docker cp/archive
|
||||||
|
// only resolves volume mounts on containers that have run at least once.
|
||||||
|
func (h *DatabaseHandler) createBackupVolumeHolder(ctx context.Context) (string, error) {
|
||||||
|
if err := h.ensureImage(ctx, managedDatabaseBackupImage); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
holderID, err := h.dockerClient.CreateContainer(ctx, docker.ContainerConfig{
|
||||||
|
Image: managedDatabaseBackupImage,
|
||||||
|
Cmd: []string{"true"},
|
||||||
|
RestartPolicy: "no",
|
||||||
|
Mounts: []mount.Mount{{Type: mount.TypeVolume, Source: managedDatabaseBackupVolume, Target: "/backup"}},
|
||||||
|
Labels: map[string]string{"containr.managed": "true", "containr.utility": "backup-io"},
|
||||||
|
NetworkMode: "none",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := h.dockerClient.StartContainer(ctx, holderID); err != nil {
|
||||||
|
_ = h.dockerClient.RemoveContainer(context.Background(), holderID, true)
|
||||||
|
return "", fmt.Errorf("failed to start backup holder: %w", err)
|
||||||
|
}
|
||||||
|
return holderID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// archiveReader unwraps the tar stream CopyFromContainer returns.
|
||||||
|
type archiveReader struct {
|
||||||
|
tar io.ReadCloser
|
||||||
|
tr *tar.Reader
|
||||||
|
err error
|
||||||
|
cleanup func()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *archiveReader) Read(p []byte) (int, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return 0, r.err
|
||||||
|
}
|
||||||
|
if r.tr == nil {
|
||||||
|
r.tr = tar.NewReader(r.tar)
|
||||||
|
// Skip to the first regular file entry.
|
||||||
|
for {
|
||||||
|
hdr, err := r.tr.Next()
|
||||||
|
if err != nil {
|
||||||
|
r.err = err
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if hdr.Typeflag == tar.TypeReg {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
n, err := r.tr.Read(p)
|
||||||
|
if err != nil {
|
||||||
|
r.err = err
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *archiveReader) Close() error {
|
||||||
|
err := r.tar.Close()
|
||||||
|
if r.cleanup != nil {
|
||||||
|
r.cleanup()
|
||||||
|
r.cleanup = nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNormalizeBackupTargetRequest(t *testing.T) {
|
||||||
|
req := backupTargetRequest{
|
||||||
|
Name: " garage ",
|
||||||
|
Endpoint: "https://garage.local:3900/",
|
||||||
|
Bucket: " backups ",
|
||||||
|
Prefix: " /prod/dbs/ ",
|
||||||
|
}
|
||||||
|
if err := normalizeBackupTargetRequest(&req); err != nil {
|
||||||
|
t.Fatalf("normalize failed: %v", err)
|
||||||
|
}
|
||||||
|
if req.Endpoint != "garage.local:3900" {
|
||||||
|
t.Fatalf("endpoint not normalized: %q", req.Endpoint)
|
||||||
|
}
|
||||||
|
if req.Bucket != "backups" || req.Prefix != "prod/dbs" {
|
||||||
|
t.Fatalf("bucket/prefix not trimmed: %q %q", req.Bucket, req.Prefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, bad := range []backupTargetRequest{
|
||||||
|
{Endpoint: "e", Bucket: "b"}, // no name
|
||||||
|
{Name: "n", Bucket: "b"}, // no endpoint
|
||||||
|
{Name: "n", Endpoint: "e"}, // no bucket
|
||||||
|
} {
|
||||||
|
if err := normalizeBackupTargetRequest(&bad); err == nil {
|
||||||
|
t.Fatalf("case %d: expected error", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArchiveReaderUnwrapsTar(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
tw := tar.NewWriter(&buf)
|
||||||
|
payload := []byte("backup-bytes")
|
||||||
|
if err := tw.WriteHeader(&tar.Header{Name: "b.tar.gz", Mode: 0o644, Size: int64(len(payload))}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := tw.Write(payload); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := tw.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r := &archiveReader{tar: io.NopCloser(&buf)}
|
||||||
|
got, err := io.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read: %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(got, payload) {
|
||||||
|
t.Fatalf("payload mismatch: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,225 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/database/sqlcdb"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Instance-wide announcement banners. Admins manage them under
|
||||||
|
// /admin/banners; every signed-in user reads the active set at
|
||||||
|
// /banners/active so the UI (and CLI) can surface announcements.
|
||||||
|
|
||||||
|
var bannerLevels = map[string]bool{"info": true, "success": true, "warning": true, "error": true}
|
||||||
|
|
||||||
|
// bannerJSON flattens sqlc nullable wrappers into plain API values.
|
||||||
|
type bannerJSON struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Level string `json:"level"`
|
||||||
|
Active bool `json:"active"`
|
||||||
|
Dismissible bool `json:"dismissible"`
|
||||||
|
StartsAt *time.Time `json:"starts_at,omitempty"`
|
||||||
|
EndsAt *time.Time `json:"ends_at,omitempty"`
|
||||||
|
CreatedAt *time.Time `json:"created_at,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func toBannerJSON(b sqlcdb.Banner) bannerJSON {
|
||||||
|
out := bannerJSON{
|
||||||
|
ID: b.ID.String(),
|
||||||
|
Title: b.Title,
|
||||||
|
Body: b.Body,
|
||||||
|
Level: b.Level,
|
||||||
|
Active: b.Active,
|
||||||
|
Dismissible: b.Dismissible,
|
||||||
|
}
|
||||||
|
if b.StartsAt.Valid {
|
||||||
|
out.StartsAt = &b.StartsAt.Time
|
||||||
|
}
|
||||||
|
if b.EndsAt.Valid {
|
||||||
|
out.EndsAt = &b.EndsAt.Time
|
||||||
|
}
|
||||||
|
if b.CreatedAt.Valid {
|
||||||
|
out.CreatedAt = &b.CreatedAt.Time
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func toBannerList(banners []sqlcdb.Banner) []bannerJSON {
|
||||||
|
out := make([]bannerJSON, 0, len(banners))
|
||||||
|
for _, b := range banners {
|
||||||
|
out = append(out, toBannerJSON(b))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
type bannerRequest struct {
|
||||||
|
Title *string `json:"title"`
|
||||||
|
Body *string `json:"body"`
|
||||||
|
Level *string `json:"level"`
|
||||||
|
Active *bool `json:"active"`
|
||||||
|
Dismissible *bool `json:"dismissible"`
|
||||||
|
StartsAt *time.Time `json:"starts_at"`
|
||||||
|
EndsAt *time.Time `json:"ends_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func bannerQueries(c *gin.Context) *sqlcdb.Queries {
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
return sqlcdb.New(db.DB)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseBannerID(c *gin.Context) (uuid.UUID, bool) {
|
||||||
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid banner id")
|
||||||
|
return uuid.Nil, false
|
||||||
|
}
|
||||||
|
return id, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func validBannerLevel(level string) bool { return bannerLevels[level] }
|
||||||
|
|
||||||
|
func handleListActiveBanners(c *gin.Context) {
|
||||||
|
banners, err := bannerQueries(c).ListActiveBanners(c.Request.Context())
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to list banners")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"banners": toBannerList(banners)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAdminListBanners(c *gin.Context) {
|
||||||
|
banners, err := bannerQueries(c).ListBanners(c.Request.Context())
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to list banners")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"banners": toBannerList(banners)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAdminCreateBanner(c *gin.Context) {
|
||||||
|
var req bannerRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_BODY", "invalid request body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.Title == nil || *req.Title == "" {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "title is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
level := "info"
|
||||||
|
if req.Level != nil {
|
||||||
|
if !validBannerLevel(*req.Level) {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "level must be info, success, warning or error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
level = *req.Level
|
||||||
|
}
|
||||||
|
userID, _ := requireAuthenticatedUserUUID(c)
|
||||||
|
params := sqlcdb.CreateBannerParams{
|
||||||
|
Title: *req.Title,
|
||||||
|
Level: level,
|
||||||
|
Active: true,
|
||||||
|
Dismissible: true,
|
||||||
|
CreatedBy: uuid.NullUUID{UUID: userID, Valid: true},
|
||||||
|
}
|
||||||
|
if req.Body != nil {
|
||||||
|
params.Body = *req.Body
|
||||||
|
}
|
||||||
|
if req.Active != nil {
|
||||||
|
params.Active = *req.Active
|
||||||
|
}
|
||||||
|
if req.Dismissible != nil {
|
||||||
|
params.Dismissible = *req.Dismissible
|
||||||
|
}
|
||||||
|
if req.StartsAt != nil {
|
||||||
|
params.StartsAt = sql.NullTime{Time: *req.StartsAt, Valid: true}
|
||||||
|
}
|
||||||
|
if req.EndsAt != nil {
|
||||||
|
params.EndsAt = sql.NullTime{Time: *req.EndsAt, Valid: true}
|
||||||
|
}
|
||||||
|
banner, err := bannerQueries(c).CreateBanner(c.Request.Context(), params)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to create banner")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, toBannerJSON(banner))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAdminUpdateBanner(c *gin.Context) {
|
||||||
|
id, ok := parseBannerID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
q := bannerQueries(c)
|
||||||
|
banner, err := q.GetBannerByID(c.Request.Context(), id)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "banner not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req bannerRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_BODY", "invalid request body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
params := sqlcdb.UpdateBannerParams{
|
||||||
|
ID: id,
|
||||||
|
Title: banner.Title,
|
||||||
|
Body: banner.Body,
|
||||||
|
Level: banner.Level,
|
||||||
|
Active: banner.Active,
|
||||||
|
Dismissible: banner.Dismissible,
|
||||||
|
StartsAt: banner.StartsAt,
|
||||||
|
EndsAt: banner.EndsAt,
|
||||||
|
}
|
||||||
|
if req.Title != nil {
|
||||||
|
params.Title = *req.Title
|
||||||
|
}
|
||||||
|
if req.Body != nil {
|
||||||
|
params.Body = *req.Body
|
||||||
|
}
|
||||||
|
if req.Level != nil {
|
||||||
|
if !validBannerLevel(*req.Level) {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "level must be info, success, warning or error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
params.Level = *req.Level
|
||||||
|
}
|
||||||
|
if req.Active != nil {
|
||||||
|
params.Active = *req.Active
|
||||||
|
}
|
||||||
|
if req.Dismissible != nil {
|
||||||
|
params.Dismissible = *req.Dismissible
|
||||||
|
}
|
||||||
|
if req.StartsAt != nil {
|
||||||
|
params.StartsAt = sql.NullTime{Time: *req.StartsAt, Valid: true}
|
||||||
|
}
|
||||||
|
if req.EndsAt != nil {
|
||||||
|
params.EndsAt = sql.NullTime{Time: *req.EndsAt, Valid: true}
|
||||||
|
}
|
||||||
|
updated, err := q.UpdateBanner(c.Request.Context(), params)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to update banner")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, toBannerJSON(updated))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAdminDeleteBanner(c *gin.Context) {
|
||||||
|
id, ok := parseBannerID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := bannerQueries(c).DeleteBanner(c.Request.Context(), id); err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to delete banner")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Status(http.StatusNoContent)
|
||||||
|
}
|
||||||
@@ -2,8 +2,11 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
|
"containr/internal/database"
|
||||||
"containr/internal/database/sqlcdb"
|
"containr/internal/database/sqlcdb"
|
||||||
|
"containr/internal/deployment"
|
||||||
"containr/internal/docker"
|
"containr/internal/docker"
|
||||||
|
"containr/internal/secrets"
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@@ -21,6 +24,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
|
||||||
dockercontainer "github.com/docker/docker/api/types/container"
|
dockercontainer "github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/mount"
|
"github.com/docker/docker/api/types/mount"
|
||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
@@ -51,12 +56,32 @@ type DatabaseService struct {
|
|||||||
Region string `json:"region" db:"region"`
|
Region string `json:"region" db:"region"`
|
||||||
BackupSchedule string `json:"backup_schedule,omitempty"`
|
BackupSchedule string `json:"backup_schedule,omitempty"`
|
||||||
NextBackupAt *time.Time `json:"next_backup_at,omitempty"`
|
NextBackupAt *time.Time `json:"next_backup_at,omitempty"`
|
||||||
|
Provider string `json:"provider"` // managed | external
|
||||||
|
PublicPort bool `json:"public_port" db:"public_port"`
|
||||||
|
BackupTargetID string `json:"backup_target_id,omitempty"` // S3-compatible archive destination
|
||||||
|
External *DatabaseExternalRef `json:"external,omitempty"` // connection target for external DBs
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||||
ConnectionURL string `json:"connection_url"`
|
ConnectionURL string `json:"connection_url"`
|
||||||
Metrics DatabaseMetrics `json:"metrics"`
|
Metrics DatabaseMetrics `json:"metrics"`
|
||||||
Backups DatabaseBackupConfig `json:"backups"`
|
Backups DatabaseBackupConfig `json:"backups"`
|
||||||
Settings DatabaseSettings `json:"settings"`
|
Settings DatabaseSettings `json:"settings"`
|
||||||
|
|
||||||
|
// externalPasswordEnc is the stored ciphertext — needed to compose the
|
||||||
|
// connection URL on privileged reads, never serialized.
|
||||||
|
externalPasswordEnc string `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DatabaseExternalRef describes a registered external database's connection
|
||||||
|
// target. The password is stored encrypted and never returned — has_password
|
||||||
|
// signals presence.
|
||||||
|
type DatabaseExternalRef struct {
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
Database string `json:"database"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
HasPassword bool `json:"has_password"`
|
||||||
|
SSL bool `json:"ssl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseMetrics represents database performance metrics
|
// DatabaseMetrics represents database performance metrics
|
||||||
@@ -86,6 +111,7 @@ type DatabaseBackup struct {
|
|||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
Size string `json:"size"`
|
Size string `json:"size"`
|
||||||
Status string `json:"status"` // completed, failed, in_progress
|
Status string `json:"status"` // completed, failed, in_progress
|
||||||
|
Remote bool `json:"remote"` // archive also exists on the configured backup target
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseSettings represents database configuration
|
// DatabaseSettings represents database configuration
|
||||||
@@ -98,10 +124,11 @@ type DatabaseSettings struct {
|
|||||||
|
|
||||||
// DatabaseCreateRequest represents a request to create a new database
|
// DatabaseCreateRequest represents a request to create a new database
|
||||||
type DatabaseCreateRequest struct {
|
type DatabaseCreateRequest struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Type string `json:"type" binding:"required"`
|
Type string `json:"type" binding:"required"`
|
||||||
Plan string `json:"plan" binding:"required,oneof=hobby starter standard business"`
|
Plan string `json:"plan" binding:"required,oneof=hobby starter standard business"`
|
||||||
Region string `json:"region" binding:"required"`
|
Region string `json:"region" binding:"required"`
|
||||||
|
PublicPort bool `json:"public_port"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseUpdateRequest represents a request to update a database
|
// DatabaseUpdateRequest represents a request to update a database
|
||||||
@@ -109,6 +136,8 @@ type DatabaseUpdateRequest struct {
|
|||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Plan string `json:"plan,omitempty"`
|
Plan string `json:"plan,omitempty"`
|
||||||
BackupSchedule *string `json:"backup_schedule,omitempty"`
|
BackupSchedule *string `json:"backup_schedule,omitempty"`
|
||||||
|
PublicPort *bool `json:"public_port,omitempty"`
|
||||||
|
BackupTargetID *string `json:"backup_target_id,omitempty"` // empty string clears
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseActionRequest represents a request to perform database actions
|
// DatabaseActionRequest represents a request to perform database actions
|
||||||
@@ -272,10 +301,11 @@ func (h *DatabaseHandler) CreateDatabase(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
databaseID, dbName, dbType, err := h.createManagedDatabase(c.Request.Context(), userID, managedDatabaseCreateRequest{
|
databaseID, dbName, dbType, err := h.createManagedDatabase(c.Request.Context(), userID, managedDatabaseCreateRequest{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
Type: req.Type,
|
Type: req.Type,
|
||||||
Plan: req.Plan,
|
Plan: req.Plan,
|
||||||
Region: req.Region,
|
Region: req.Region,
|
||||||
|
PublicPort: req.PublicPort,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
@@ -303,7 +333,13 @@ type managedDatabaseCreateRequest struct {
|
|||||||
Type string
|
Type string
|
||||||
Plan string
|
Plan string
|
||||||
Region string
|
Region string
|
||||||
|
PublicPort bool
|
||||||
RuntimeVariables map[string]string
|
RuntimeVariables map[string]string
|
||||||
|
// ProjectID + NetworkAlias attach the container to a project network
|
||||||
|
// under Alias — set by graph-template deploys so sibling services can
|
||||||
|
// resolve the database by name.
|
||||||
|
ProjectID string
|
||||||
|
NetworkAlias string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *DatabaseHandler) createManagedDatabase(ctx context.Context, userID string, req managedDatabaseCreateRequest) (databaseID string, name string, dbType string, err error) {
|
func (h *DatabaseHandler) createManagedDatabase(ctx context.Context, userID string, req managedDatabaseCreateRequest) (databaseID string, name string, dbType string, err error) {
|
||||||
@@ -345,17 +381,26 @@ func (h *DatabaseHandler) createManagedDatabase(ctx context.Context, userID stri
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
version := h.getDefaultVersion(dbType)
|
version := h.getDefaultVersion(dbType)
|
||||||
|
|
||||||
|
var projectID uuid.NullUUID
|
||||||
|
if req.ProjectID != "" {
|
||||||
|
if parsed, parseErr := uuid.Parse(req.ProjectID); parseErr == nil {
|
||||||
|
projectID = uuid.NullUUID{UUID: parsed, Valid: true}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = h.queries.CreateDatabaseService(ctx, sqlcdb.CreateDatabaseServiceParams{
|
err = h.queries.CreateDatabaseService(ctx, sqlcdb.CreateDatabaseServiceParams{
|
||||||
ID: databaseID,
|
ID: databaseID,
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
Name: name,
|
Name: name,
|
||||||
Type: dbType,
|
Type: dbType,
|
||||||
Status: "building",
|
Status: "building",
|
||||||
Version: version,
|
Version: version,
|
||||||
Plan: plan,
|
Plan: plan,
|
||||||
Region: region,
|
Region: region,
|
||||||
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
PublicPort: req.PublicPort,
|
||||||
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
ProjectID: projectID,
|
||||||
|
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
||||||
|
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", "", fmt.Errorf("failed to create database service: %w", err)
|
return "", "", "", fmt.Errorf("failed to create database service: %w", err)
|
||||||
@@ -370,10 +415,17 @@ func (h *DatabaseHandler) createManagedDatabaseAndProvision(ctx context.Context,
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
go h.provisionDatabaseWithVariables(databaseID, name, dbType, req.RuntimeVariables)
|
go h.provisionDatabaseWithVariables(databaseID, name, dbType, req.RuntimeVariables, dbNetworkAttach{ProjectID: req.ProjectID, Alias: req.NetworkAlias})
|
||||||
return databaseID, nil
|
return databaseID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dbNetworkAttach carries the optional project-network membership for a
|
||||||
|
// managed database provisioned by a graph template.
|
||||||
|
type dbNetworkAttach struct {
|
||||||
|
ProjectID string
|
||||||
|
Alias string
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateDatabase updates a database service
|
// UpdateDatabase updates a database service
|
||||||
func (h *DatabaseHandler) UpdateDatabase(c *gin.Context) {
|
func (h *DatabaseHandler) UpdateDatabase(c *gin.Context) {
|
||||||
userID, ok := requireAuthenticatedUserID(c)
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
@@ -392,11 +444,64 @@ func (h *DatabaseHandler) UpdateDatabase(c *gin.Context) {
|
|||||||
req.Name = strings.TrimSpace(req.Name)
|
req.Name = strings.TrimSpace(req.Name)
|
||||||
req.Plan = strings.TrimSpace(req.Plan)
|
req.Plan = strings.TrimSpace(req.Plan)
|
||||||
|
|
||||||
if req.Name == "" && req.Plan == "" && req.BackupSchedule == nil {
|
if req.Name == "" && req.Plan == "" && req.BackupSchedule == nil && req.PublicPort == nil && req.BackupTargetID == nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No fields to update"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No fields to update"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.PublicPort != nil || req.BackupTargetID != nil {
|
||||||
|
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||||
|
ID: databaseID,
|
||||||
|
UserID: userID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.PublicPort != nil {
|
||||||
|
if row.Provider == "external" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "public_port applies to managed databases — external databases are already reachable", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := h.queries.SetDatabaseServicePublicPortByIDAndUser(c.Request.Context(), sqlcdb.SetDatabaseServicePublicPortByIDAndUserParams{
|
||||||
|
PublicPort: *req.PublicPort,
|
||||||
|
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||||
|
ID: databaseID,
|
||||||
|
UserID: userID,
|
||||||
|
}); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update public port"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Port bindings are baked into the container — recreate it so the
|
||||||
|
// new bind scope takes effect. The data volume survives.
|
||||||
|
if *req.PublicPort != row.PublicPort {
|
||||||
|
go h.reprovisionManagedDatabase(databaseID, row.Name, row.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if req.BackupTargetID != nil {
|
||||||
|
if row.Provider == "external" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "backup targets apply to managed databases — external backups are not supported yet", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetID := strings.TrimSpace(*req.BackupTargetID)
|
||||||
|
if targetID != "" {
|
||||||
|
if _, err := h.queries.GetBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.GetBackupTargetByIDAndUserParams{ID: targetID, UserID: userID}); err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Backup target not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := h.queries.SetDatabaseBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.SetDatabaseBackupTargetByIDAndUserParams{
|
||||||
|
BackupTargetID: sql.NullString{String: targetID, Valid: targetID != ""},
|
||||||
|
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||||
|
ID: databaseID,
|
||||||
|
UserID: userID,
|
||||||
|
}); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update backup target"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if req.BackupSchedule != nil {
|
if req.BackupSchedule != nil {
|
||||||
var schedule sql.NullString
|
var schedule sql.NullString
|
||||||
var nextBackup sql.NullTime
|
var nextBackup sql.NullTime
|
||||||
@@ -467,21 +572,20 @@ func (h *DatabaseHandler) DeleteDatabase(c *gin.Context) {
|
|||||||
databaseID := c.Param("id")
|
databaseID := c.Param("id")
|
||||||
userID = h.effectiveUserID(c, userID, databaseID)
|
userID = h.effectiveUserID(c, userID, databaseID)
|
||||||
|
|
||||||
exists, err := h.queries.DatabaseServiceExistsByIDAndUser(c.Request.Context(), sqlcdb.DatabaseServiceExistsByIDAndUserParams{
|
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||||
ID: databaseID,
|
ID: databaseID,
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if row.Provider != "external" && h.dockerClient != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if h.dockerClient != nil {
|
|
||||||
if err := h.deleteManagedDatabaseRuntime(databaseID); err != nil {
|
if err := h.deleteManagedDatabaseRuntime(databaseID); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete runtime database"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete runtime database"})
|
||||||
return
|
return
|
||||||
@@ -515,17 +619,24 @@ func (h *DatabaseHandler) PerformDatabaseAction(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err := h.queries.DatabaseServiceExistsByIDAndUser(c.Request.Context(), sqlcdb.DatabaseServiceExistsByIDAndUserParams{
|
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||||
ID: databaseID,
|
ID: databaseID,
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if row.Provider == "external" {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"error": "Lifecycle actions apply to managed databases only — external databases are run elsewhere",
|
||||||
|
"code": "VALIDATION",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -619,6 +730,13 @@ func (h *DatabaseHandler) CreateBackup(c *gin.Context) {
|
|||||||
c.JSON(http.StatusConflict, gin.H{"error": "Database is still provisioning"})
|
c.JSON(http.StatusConflict, gin.H{"error": "Database is still provisioning"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if row.Provider == "external" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"error": "Backups run against the managed dump pipeline — external database backups are not supported yet",
|
||||||
|
"code": "VALIDATION",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
backupID := generateDatabaseBackupID(databaseID)
|
backupID := generateDatabaseBackupID(databaseID)
|
||||||
archivePath := sanitizeBackupArchivePath(managedDatabaseBackupArchivePath(backupID))
|
archivePath := sanitizeBackupArchivePath(managedDatabaseBackupArchivePath(backupID))
|
||||||
@@ -659,17 +777,23 @@ func (h *DatabaseHandler) RestoreBackup(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err := h.queries.DatabaseServiceExistsByIDAndUser(c.Request.Context(), sqlcdb.DatabaseServiceExistsByIDAndUserParams{
|
dbRow, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||||
ID: databaseID,
|
ID: databaseID,
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if dbRow.Provider == "external" {
|
||||||
if !exists {
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
"error": "Restores run against the managed dump pipeline — external database restores are not supported yet",
|
||||||
|
"code": "VALIDATION",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -787,7 +911,7 @@ func (h *DatabaseHandler) DownloadBackup(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *DatabaseHandler) resolveDatabaseMetrics(ctx context.Context, db DatabaseService) DatabaseMetrics {
|
func (h *DatabaseHandler) resolveDatabaseMetrics(ctx context.Context, db DatabaseService) DatabaseMetrics {
|
||||||
if h.dockerClient == nil {
|
if h.dockerClient == nil || db.Provider == "external" {
|
||||||
return h.generateMockMetrics()
|
return h.generateMockMetrics()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -918,6 +1042,7 @@ func (h *DatabaseHandler) resolveBackupConfig(ctx context.Context, userID string
|
|||||||
CreatedAt: createdAt,
|
CreatedAt: createdAt,
|
||||||
Size: row.Size,
|
Size: row.Size,
|
||||||
Status: row.Status,
|
Status: row.Status,
|
||||||
|
Remote: row.RemoteKey.Valid && row.RemoteKey.String != "",
|
||||||
})
|
})
|
||||||
if lastBackup == nil && row.Status == "completed" {
|
if lastBackup == nil && row.Status == "completed" {
|
||||||
t := createdAt
|
t := createdAt
|
||||||
@@ -945,7 +1070,11 @@ func (h *DatabaseHandler) generateMockSettings() DatabaseSettings {
|
|||||||
|
|
||||||
// resolveConnectionURL returns the stored connection URL for privileged
|
// resolveConnectionURL returns the stored connection URL for privileged
|
||||||
// viewers, or "" when the runtime has not produced one (not provisioned yet).
|
// viewers, or "" when the runtime has not produced one (not provisioned yet).
|
||||||
|
// External databases compose it from the registered fields on demand.
|
||||||
func (h *DatabaseHandler) resolveConnectionURL(db DatabaseService) string {
|
func (h *DatabaseHandler) resolveConnectionURL(db DatabaseService) string {
|
||||||
|
if db.Provider == "external" && db.External != nil {
|
||||||
|
return externalConnectionURL(db.Type, db.External, secrets.Decrypt(db.externalPasswordEnc))
|
||||||
|
}
|
||||||
return strings.TrimSpace(db.ConnectionURL)
|
return strings.TrimSpace(db.ConnectionURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -987,7 +1116,7 @@ func rewriteLoopbackURL(rawURL, host string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *DatabaseHandler) reconcileManagedDatabaseState(ctx context.Context, db DatabaseService) DatabaseService {
|
func (h *DatabaseHandler) reconcileManagedDatabaseState(ctx context.Context, db DatabaseService) DatabaseService {
|
||||||
if h.dockerClient == nil {
|
if h.dockerClient == nil || db.Provider == "external" {
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1054,7 +1183,7 @@ func (h *DatabaseHandler) resolveManagedRuntimeStatus(ctx context.Context, db Da
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mapDatabaseServiceRow(row sqlcdb.DatabaseService) DatabaseService {
|
func mapDatabaseServiceRow(row sqlcdb.DatabaseService) DatabaseService {
|
||||||
return DatabaseService{
|
svc := DatabaseService{
|
||||||
ID: row.ID,
|
ID: row.ID,
|
||||||
OwnerID: row.UserID,
|
OwnerID: row.UserID,
|
||||||
Name: row.Name,
|
Name: row.Name,
|
||||||
@@ -1065,10 +1194,25 @@ func mapDatabaseServiceRow(row sqlcdb.DatabaseService) DatabaseService {
|
|||||||
Region: row.Region,
|
Region: row.Region,
|
||||||
BackupSchedule: databaseNullString(row.BackupSchedule),
|
BackupSchedule: databaseNullString(row.BackupSchedule),
|
||||||
NextBackupAt: databaseNullTimePtr(row.NextBackupAt),
|
NextBackupAt: databaseNullTimePtr(row.NextBackupAt),
|
||||||
|
Provider: row.Provider,
|
||||||
|
PublicPort: row.PublicPort,
|
||||||
|
BackupTargetID: databaseNullString(row.BackupTargetID),
|
||||||
ConnectionURL: databaseNullString(row.ConnectionUrl),
|
ConnectionURL: databaseNullString(row.ConnectionUrl),
|
||||||
CreatedAt: databaseNullTime(row.CreatedAt),
|
CreatedAt: databaseNullTime(row.CreatedAt),
|
||||||
UpdatedAt: databaseNullTime(row.UpdatedAt),
|
UpdatedAt: databaseNullTime(row.UpdatedAt),
|
||||||
}
|
}
|
||||||
|
if svc.Provider == "external" {
|
||||||
|
svc.External = &DatabaseExternalRef{
|
||||||
|
Host: databaseNullString(row.ExternalHost),
|
||||||
|
Port: int(row.ExternalPort.Int32),
|
||||||
|
Database: databaseNullString(row.ExternalName),
|
||||||
|
Username: databaseNullString(row.ExternalUsername),
|
||||||
|
HasPassword: row.ExternalPassword != "",
|
||||||
|
SSL: row.ExternalSsl,
|
||||||
|
}
|
||||||
|
svc.externalPasswordEnc = row.ExternalPassword
|
||||||
|
}
|
||||||
|
return svc
|
||||||
}
|
}
|
||||||
|
|
||||||
func databaseNullTime(value sql.NullTime) time.Time {
|
func databaseNullTime(value sql.NullTime) time.Time {
|
||||||
@@ -1230,10 +1374,21 @@ type databaseRuntimePlan struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *DatabaseHandler) provisionDatabase(databaseID, databaseName, dbType string) {
|
func (h *DatabaseHandler) provisionDatabase(databaseID, databaseName, dbType string) {
|
||||||
h.provisionDatabaseWithVariables(databaseID, databaseName, dbType, nil)
|
h.provisionDatabaseWithVariables(databaseID, databaseName, dbType, nil, dbNetworkAttach{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *DatabaseHandler) provisionDatabaseWithVariables(databaseID, databaseName, dbType string, runtimeVariables map[string]string) {
|
// reprovisionManagedDatabase recreates a managed container so changed
|
||||||
|
// runtime settings (public_port) take effect. The data volume persists.
|
||||||
|
func (h *DatabaseHandler) reprovisionManagedDatabase(databaseID, databaseName, dbType string) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
if h.dockerClient != nil {
|
||||||
|
_ = h.dockerClient.RemoveContainer(ctx, managedDatabaseContainerName(databaseID), true)
|
||||||
|
}
|
||||||
|
h.provisionDatabase(databaseID, databaseName, dbType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DatabaseHandler) provisionDatabaseWithVariables(databaseID, databaseName, dbType string, runtimeVariables map[string]string, attach dbNetworkAttach) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@@ -1242,7 +1397,7 @@ func (h *DatabaseHandler) provisionDatabaseWithVariables(databaseID, databaseNam
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionURL, err := h.provisionDatabaseRuntime(ctx, databaseID, databaseName, dbType, runtimeVariables)
|
connectionURL, err := h.provisionDatabaseRuntime(ctx, databaseID, databaseName, dbType, runtimeVariables, attach)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("containr: managed database %s (%s) provisioning failed: %v", databaseID, dbType, err)
|
log.Printf("containr: managed database %s (%s) provisioning failed: %v", databaseID, dbType, err)
|
||||||
_ = h.setDatabaseStatusAndConnection(databaseID, "error", sql.NullString{})
|
_ = h.setDatabaseStatusAndConnection(databaseID, "error", sql.NullString{})
|
||||||
@@ -1264,7 +1419,7 @@ func (h *DatabaseHandler) setDatabaseStatusAndConnection(databaseID, status stri
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, databaseID, databaseName, dbType string, runtimeVariables map[string]string) (string, error) {
|
func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, databaseID, databaseName, dbType string, runtimeVariables map[string]string, attach dbNetworkAttach) (string, error) {
|
||||||
plan, err := buildDatabaseRuntimePlan(dbType, databaseName, runtimeVariables)
|
plan, err := buildDatabaseRuntimePlan(dbType, databaseName, runtimeVariables)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -1289,8 +1444,12 @@ func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, database
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hostIP := "127.0.0.1"
|
||||||
|
if row, err := h.queries.GetDatabaseServiceByID(ctx, databaseID); err == nil && row.PublicPort {
|
||||||
|
hostIP = "0.0.0.0"
|
||||||
|
}
|
||||||
portBindings := nat.PortMap{
|
portBindings := nat.PortMap{
|
||||||
plan.Port: []nat.PortBinding{{HostIP: "127.0.0.1", HostPort: ""}},
|
plan.Port: []nat.PortBinding{{HostIP: hostIP, HostPort: ""}},
|
||||||
}
|
}
|
||||||
|
|
||||||
exposedPorts := nat.PortSet{plan.Port: struct{}{}}
|
exposedPorts := nat.PortSet{plan.Port: struct{}{}}
|
||||||
@@ -1348,6 +1507,17 @@ func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, database
|
|||||||
return "", fmt.Errorf("failed to start container: %w", err)
|
return "", fmt.Errorf("failed to start container: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Graph-template deploys attach the database to the project network so
|
||||||
|
// sibling services resolve it by alias ({{service.<key>.host}}).
|
||||||
|
if attach.ProjectID != "" && attach.Alias != "" {
|
||||||
|
projectNet := deployment.ProjectNetworkName(attach.ProjectID)
|
||||||
|
if err := h.dockerClient.ConnectNetwork(ctx, projectNet, containerID, network.EndpointSettings{
|
||||||
|
Aliases: []string{attach.Alias},
|
||||||
|
}); err != nil {
|
||||||
|
log.Printf("containr: database %s attach to %s failed: %v", databaseID, projectNet, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hostPort, err := h.resolvePublishedHostPort(ctx, containerID, plan.Port)
|
hostPort, err := h.resolvePublishedHostPort(ctx, containerID, plan.Port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -1699,20 +1869,36 @@ func (h *DatabaseHandler) createBackupProcess(databaseID, backupID, backupPath s
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
notify := func(title, body string) {
|
||||||
|
if row, err := h.queries.GetDatabaseServiceByID(context.Background(), databaseID); err == nil && row.UserID != "" {
|
||||||
|
insertUserNotification(&database.DB{DB: h.db}, row.UserID, "backup", title, body, "database", databaseID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if h.dockerClient == nil {
|
if h.dockerClient == nil {
|
||||||
_ = h.setDatabaseBackupStatus(backupID, "failed", "0 B", false)
|
_ = h.setDatabaseBackupStatus(backupID, "failed", "0 B", false)
|
||||||
|
notify("Database backup failed", "Backup could not start: docker unavailable.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sizeLabel, err := h.snapshotDatabaseVolume(ctx, databaseID, backupPath)
|
sizeLabel, err := h.snapshotDatabaseVolume(ctx, databaseID, backupPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = h.setDatabaseBackupStatus(backupID, "failed", "0 B", false)
|
_ = h.setDatabaseBackupStatus(backupID, "failed", "0 B", false)
|
||||||
|
notify("Database backup failed", fmt.Sprintf("Snapshot failed: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if dbRow, err := h.queries.GetDatabaseServiceByID(ctx, databaseID); err == nil {
|
||||||
|
if key, err := h.uploadBackupToTarget(ctx, dbRow, backupPath, backupID); err != nil {
|
||||||
|
log.Printf("containr: backup %s remote upload failed: %v (local archive kept)", backupID, err)
|
||||||
|
} else if key != "" {
|
||||||
|
log.Printf("containr: backup %s shipped to %s", backupID, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
_ = h.setDatabaseBackupStatus(backupID, "completed", sizeLabel, true)
|
_ = h.setDatabaseBackupStatus(backupID, "completed", sizeLabel, true)
|
||||||
|
notify("Database backup completed", fmt.Sprintf("Backup archived (%s).", sizeLabel))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *DatabaseHandler) restoreBackupProcess(databaseID, _ string, backupPath string) {
|
func (h *DatabaseHandler) restoreBackupProcess(databaseID, backupID string, backupPath string) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@@ -1721,6 +1907,22 @@ func (h *DatabaseHandler) restoreBackupProcess(databaseID, _ string, backupPath
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore the archive from the configured S3 target when the local copy
|
||||||
|
// is gone (fresh node, pruned volume) — local takes precedence.
|
||||||
|
if dbRow, err := h.queries.GetDatabaseServiceByID(ctx, databaseID); err == nil && dbRow.BackupTargetID.Valid && dbRow.BackupTargetID.String != "" {
|
||||||
|
if ok, _ := h.backupArchiveExists(ctx, backupPath); !ok {
|
||||||
|
if b, err := h.queries.GetDatabaseBackupByIDAndDatabaseAndUser(ctx, sqlcdb.GetDatabaseBackupByIDAndDatabaseAndUserParams{
|
||||||
|
ID: backupID, DatabaseID: databaseID, UserID: dbRow.UserID,
|
||||||
|
}); err == nil && b.RemoteKey.Valid && b.RemoteKey.String != "" {
|
||||||
|
if err := h.downloadBackupToVolume(ctx, dbRow, b.RemoteKey.String, backupPath); err != nil {
|
||||||
|
log.Printf("containr: remote restore fetch failed for backup %s: %v", backupID, err)
|
||||||
|
_ = h.setDatabaseStatus(databaseID, "error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ = h.setDatabaseStatus(databaseID, "building")
|
_ = h.setDatabaseStatus(databaseID, "building")
|
||||||
_ = h.stopManagedDatabase(databaseID)
|
_ = h.stopManagedDatabase(databaseID)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,319 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database/sqlcdb"
|
||||||
|
"containr/internal/secrets"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// externalDatabaseRequest is the shared payload for registering an external
|
||||||
|
// database and for ad-hoc connection tests.
|
||||||
|
type externalDatabaseRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type" binding:"required"`
|
||||||
|
Host string `json:"host" binding:"required"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
Database string `json:"database"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
SSL bool `json:"ssl"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var externalDefaultPorts = map[string]int{
|
||||||
|
"postgresql": 5432,
|
||||||
|
"mysql": 3306,
|
||||||
|
"mariadb": 3306,
|
||||||
|
"mongodb": 27017,
|
||||||
|
"redis": 6379,
|
||||||
|
"dragonfly": 6379,
|
||||||
|
"clickhouse": 9000,
|
||||||
|
}
|
||||||
|
|
||||||
|
// externalConnectionURL composes a DSN for display/bind use. The password is
|
||||||
|
// embedded only on privileged reads — the value is decrypted at that point.
|
||||||
|
func externalConnectionURL(dbType string, ext *DatabaseExternalRef, password string) string {
|
||||||
|
host := net.JoinHostPort(ext.Host, fmt.Sprintf("%d", ext.Port))
|
||||||
|
creds := ""
|
||||||
|
if ext.Username != "" {
|
||||||
|
creds = url.QueryEscape(ext.Username)
|
||||||
|
if password != "" {
|
||||||
|
creds += ":" + url.QueryEscape(password)
|
||||||
|
}
|
||||||
|
creds += "@"
|
||||||
|
}
|
||||||
|
switch dbType {
|
||||||
|
case "postgresql":
|
||||||
|
u := fmt.Sprintf("postgres://%s%s/%s", creds, host, ext.Database)
|
||||||
|
if ext.SSL {
|
||||||
|
u += "?sslmode=require"
|
||||||
|
}
|
||||||
|
return u
|
||||||
|
case "mysql", "mariadb":
|
||||||
|
return fmt.Sprintf("mysql://%s%s/%s", creds, host, ext.Database)
|
||||||
|
case "mongodb":
|
||||||
|
u := fmt.Sprintf("mongodb://%s%s/%s", creds, host, ext.Database)
|
||||||
|
if ext.SSL {
|
||||||
|
u += "?tls=true"
|
||||||
|
}
|
||||||
|
return u
|
||||||
|
case "redis", "dragonfly":
|
||||||
|
scheme := "redis"
|
||||||
|
if ext.SSL {
|
||||||
|
scheme = "rediss"
|
||||||
|
}
|
||||||
|
// Redis auth is password-only; username support is ACL-era.
|
||||||
|
if password != "" {
|
||||||
|
creds = ":" + url.QueryEscape(password) + "@"
|
||||||
|
if ext.Username != "" {
|
||||||
|
creds = url.QueryEscape(ext.Username) + creds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s://%s%s", scheme, creds, host)
|
||||||
|
case "clickhouse":
|
||||||
|
return fmt.Sprintf("clickhouse://%s%s/%s", creds, host, ext.Database)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s://%s%s", dbType, creds, host)
|
||||||
|
}
|
||||||
|
|
||||||
|
// probeExternalDatabase verifies reachability. postgres and redis-family get
|
||||||
|
// real protocol checks via the vendored drivers; everything else gets a TCP
|
||||||
|
// dial — enough to catch typos and firewall issues without new dependencies.
|
||||||
|
func probeExternalDatabase(ctx context.Context, req externalDatabaseRequest) (time.Duration, error) {
|
||||||
|
addr := net.JoinHostPort(req.Host, fmt.Sprintf("%d", req.Port))
|
||||||
|
dialer := &net.Dialer{Timeout: 5 * time.Second}
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
switch req.Type {
|
||||||
|
case "postgresql":
|
||||||
|
sslmode := "disable"
|
||||||
|
if req.SSL {
|
||||||
|
sslmode = "require"
|
||||||
|
}
|
||||||
|
dsn := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s&connect_timeout=5",
|
||||||
|
url.QueryEscape(req.Username), url.QueryEscape(req.Password), addr,
|
||||||
|
url.QueryEscape(req.Database), sslmode)
|
||||||
|
conn, err := sql.Open("postgres", dsn)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
if err := conn.PingContext(ctx); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
case "redis", "dragonfly":
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: addr,
|
||||||
|
Username: req.Username,
|
||||||
|
Password: req.Password,
|
||||||
|
DialTimeout: 5 * time.Second,
|
||||||
|
ReadTimeout: 5 * time.Second,
|
||||||
|
TLSConfig: redisTLSConfig(req.SSL, req.Host),
|
||||||
|
})
|
||||||
|
defer client.Close()
|
||||||
|
if err := client.Ping(ctx).Err(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
conn, err := dialer.DialContext(ctx, "tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
return time.Since(start), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func redisTLSConfig(enabled bool, host string) *tls.Config {
|
||||||
|
if !enabled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &tls.Config{ServerName: host, MinVersion: tls.VersionTLS12}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleTestDatabaseConnection probes arbitrary connection parameters — used
|
||||||
|
// by the register form's "test" button and by agents before registering.
|
||||||
|
func (h *DatabaseHandler) TestDatabaseConnection(c *gin.Context) {
|
||||||
|
if _, ok := requireAuthenticatedUserID(c); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req externalDatabaseRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := normalizeExternalRequest(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
latency, err := probeExternalDatabase(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"ok": false, "error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"ok": true, "latency_ms": latency.Milliseconds()})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDatabaseConnectionByID re-probes a registered external database using
|
||||||
|
// its stored (decrypted) credentials.
|
||||||
|
func (h *DatabaseHandler) TestDatabaseConnectionByID(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
databaseID := c.Param("id")
|
||||||
|
userID = h.effectiveUserID(c, userID, databaseID)
|
||||||
|
|
||||||
|
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||||
|
ID: databaseID,
|
||||||
|
UserID: userID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch database"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if row.Provider != "external" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "test-connection applies to external databases", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
req := externalDatabaseRequest{
|
||||||
|
Type: row.Type,
|
||||||
|
Host: databaseNullString(row.ExternalHost),
|
||||||
|
Port: int(row.ExternalPort.Int32),
|
||||||
|
Database: databaseNullString(row.ExternalName),
|
||||||
|
Username: databaseNullString(row.ExternalUsername),
|
||||||
|
Password: secrets.Decrypt(row.ExternalPassword),
|
||||||
|
SSL: row.ExternalSsl,
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
latency, probeErr := probeExternalDatabase(ctx, req)
|
||||||
|
if probeErr != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"ok": false, "error": probeErr.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"ok": true, "latency_ms": latency.Milliseconds()})
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeExternalRequest(req *externalDatabaseRequest) error {
|
||||||
|
req.Type = normalizeDatabaseType(req.Type)
|
||||||
|
if _, ok := supportedDatabaseTypes[req.Type]; !ok {
|
||||||
|
return fmt.Errorf("%w: %s", errUnsupportedDatabaseType, req.Type)
|
||||||
|
}
|
||||||
|
req.Host = strings.TrimSpace(req.Host)
|
||||||
|
if req.Host == "" {
|
||||||
|
return errors.New("host is required")
|
||||||
|
}
|
||||||
|
if req.Port <= 0 {
|
||||||
|
req.Port = externalDefaultPorts[req.Type]
|
||||||
|
}
|
||||||
|
if req.Port <= 0 || req.Port > 65535 {
|
||||||
|
return errors.New("port must be 1-65535")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterExternalDatabase records a database hosted elsewhere — same list,
|
||||||
|
// same bind surface, no container. The connection is probed first so typos
|
||||||
|
// fail at registration, not at the first service bind.
|
||||||
|
func (h *DatabaseHandler) RegisterExternalDatabase(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req externalDatabaseRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Name = strings.TrimSpace(req.Name)
|
||||||
|
if req.Name == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := normalizeExternalRequest(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if _, err := probeExternalDatabase(probeCtx, req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"error": fmt.Sprintf("Connection check failed: %v", err),
|
||||||
|
"code": "DEPENDENCY_UNAVAILABLE",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := h.queries.CountDatabaseServicesByUserAndName(c.Request.Context(), sqlcdb.CountDatabaseServicesByUserAndNameParams{
|
||||||
|
UserID: userID,
|
||||||
|
Name: req.Name,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to validate database name"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
c.JSON(http.StatusConflict, gin.H{"error": "database name already exists", "code": "CONFLICT"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
databaseID := generateDatabaseID(req.Name)
|
||||||
|
now := sql.NullTime{Time: time.Now(), Valid: true}
|
||||||
|
encPassword := ""
|
||||||
|
if req.Password != "" {
|
||||||
|
encPassword = secrets.Encrypt(req.Password)
|
||||||
|
}
|
||||||
|
if err := h.queries.CreateExternalDatabaseService(c.Request.Context(), sqlcdb.CreateExternalDatabaseServiceParams{
|
||||||
|
ID: databaseID,
|
||||||
|
UserID: userID,
|
||||||
|
Name: req.Name,
|
||||||
|
Type: req.Type,
|
||||||
|
Version: "",
|
||||||
|
Plan: "external",
|
||||||
|
Region: "external",
|
||||||
|
ExternalHost: sql.NullString{String: req.Host, Valid: true},
|
||||||
|
ExternalPort: sql.NullInt32{Int32: int32(req.Port), Valid: true},
|
||||||
|
ExternalName: sql.NullString{String: req.Database, Valid: req.Database != ""},
|
||||||
|
ExternalUsername: sql.NullString{String: req.Username, Valid: req.Username != ""},
|
||||||
|
ExternalPassword: encPassword,
|
||||||
|
ExternalSsl: req.SSL,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to register database"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
LogAudit(userID, "database", databaseID, "register-external", map[string]interface{}{
|
||||||
|
"name": req.Name,
|
||||||
|
"type": req.Type,
|
||||||
|
"host": req.Host,
|
||||||
|
})
|
||||||
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
|
"id": databaseID,
|
||||||
|
"message": "External database registered",
|
||||||
|
"status": "running",
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNormalizeExternalRequestDefaults(t *testing.T) {
|
||||||
|
req := externalDatabaseRequest{Type: "PostgreSQL", Host: " db.example.com ", Port: 0}
|
||||||
|
if err := normalizeExternalRequest(&req); err != nil {
|
||||||
|
t.Fatalf("normalize failed: %v", err)
|
||||||
|
}
|
||||||
|
if req.Type != "postgresql" {
|
||||||
|
t.Fatalf("type not normalized: %q", req.Type)
|
||||||
|
}
|
||||||
|
if req.Host != "db.example.com" {
|
||||||
|
t.Fatalf("host not trimmed: %q", req.Host)
|
||||||
|
}
|
||||||
|
if req.Port != 5432 {
|
||||||
|
t.Fatalf("default port = %d, want 5432", req.Port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeExternalRequestRejects(t *testing.T) {
|
||||||
|
cases := []externalDatabaseRequest{
|
||||||
|
{Type: "sqlite", Host: "x"}, // unsupported type
|
||||||
|
{Type: "postgresql", Host: " "}, // empty host
|
||||||
|
{Type: "postgresql", Host: "h", Port: 70000}, // out of range
|
||||||
|
}
|
||||||
|
for i, req := range cases {
|
||||||
|
if err := normalizeExternalRequest(&req); err == nil {
|
||||||
|
t.Fatalf("case %d: expected error", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExternalConnectionURLMasking(t *testing.T) {
|
||||||
|
ext := &DatabaseExternalRef{
|
||||||
|
Host: "db.example.com",
|
||||||
|
Port: 5432,
|
||||||
|
Database: "appdb",
|
||||||
|
Username: "svc",
|
||||||
|
SSL: true,
|
||||||
|
}
|
||||||
|
u := externalConnectionURL("postgresql", ext, "s3cret")
|
||||||
|
if u != "postgres://svc:[email protected]:5432/appdb?sslmode=require" {
|
||||||
|
t.Fatalf("unexpected pg url: %s", u)
|
||||||
|
}
|
||||||
|
u = externalConnectionURL("postgresql", ext, "")
|
||||||
|
if u != "postgres://[email protected]:5432/appdb?sslmode=require" {
|
||||||
|
t.Fatalf("unexpected passwordless url: %s", u)
|
||||||
|
}
|
||||||
|
|
||||||
|
r := &DatabaseExternalRef{Host: "cache.internal", Port: 6379, SSL: true}
|
||||||
|
u = externalConnectionURL("redis", r, "pw")
|
||||||
|
if u != "rediss://:[email protected]:6379" {
|
||||||
|
t.Fatalf("unexpected redis url: %s", u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProbeExternalDatabaseTCP(t *testing.T) {
|
||||||
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("listen: %v", err)
|
||||||
|
}
|
||||||
|
defer ln.Close()
|
||||||
|
port := ln.Addr().(*net.TCPAddr).Port
|
||||||
|
|
||||||
|
// TCP fallback path — mysql uses a plain dial.
|
||||||
|
latency, err := probeExternalDatabase(context.Background(), externalDatabaseRequest{
|
||||||
|
Type: "mysql", Host: "127.0.0.1", Port: port,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("probe should succeed against open listener: %v", err)
|
||||||
|
}
|
||||||
|
if latency <= 0 {
|
||||||
|
t.Fatalf("latency not recorded")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Closed port must fail fast.
|
||||||
|
if _, err := probeExternalDatabase(context.Background(), externalDatabaseRequest{
|
||||||
|
Type: "mysql", Host: "127.0.0.1", Port: 1,
|
||||||
|
}); err == nil {
|
||||||
|
t.Fatalf("probe should fail against closed port")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedisTLSConfig(t *testing.T) {
|
||||||
|
if redisTLSConfig(false, "h") != nil {
|
||||||
|
t.Fatalf("disabled ssl must produce nil tls config")
|
||||||
|
}
|
||||||
|
cfg := redisTLSConfig(true, "db.example.com")
|
||||||
|
if cfg == nil || cfg.ServerName != "db.example.com" {
|
||||||
|
t.Fatalf("expected ServerName passthrough, got %+v", cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,9 +4,11 @@ import (
|
|||||||
"containr/internal/database"
|
"containr/internal/database"
|
||||||
"containr/internal/deployment"
|
"containr/internal/deployment"
|
||||||
"containr/internal/deployqueue"
|
"containr/internal/deployqueue"
|
||||||
|
"containr/internal/docker"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -232,6 +234,9 @@ func handleCreateDeployment(c *gin.Context) {
|
|||||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
s.created_at, s.updated_at, p.owner_id
|
s.created_at, s.updated_at, p.owner_id
|
||||||
FROM services s
|
FROM services s
|
||||||
JOIN projects p ON s.project_id = p.id
|
JOIN projects p ON s.project_id = p.id
|
||||||
@@ -242,7 +247,9 @@ func handleCreateDeployment(c *gin.Context) {
|
|||||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt, &projectOwner,
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt, &projectOwner,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -401,6 +408,13 @@ func runDeploymentAndSyncWithImage(
|
|||||||
// ephemeral ports.
|
// ephemeral ports.
|
||||||
_ = db.QueryRow(`SELECT published_port FROM services WHERE id = $1`, service.ID).Scan(&publishedPort)
|
_ = db.QueryRow(`SELECT published_port FROM services WHERE id = $1`, service.ID).Scan(&publishedPort)
|
||||||
|
|
||||||
|
publicPort := int32(service.Port)
|
||||||
|
if service.Builder == "static" && publicPort == 0 {
|
||||||
|
// The static builder's image is nginx — route to its default port
|
||||||
|
// unless the user picked another.
|
||||||
|
publicPort = 80
|
||||||
|
}
|
||||||
|
|
||||||
maintenanceMode, basicAuthUsers := serviceAccess(db, service.ID)
|
maintenanceMode, basicAuthUsers := serviceAccess(db, service.ID)
|
||||||
maintURL := ""
|
maintURL := ""
|
||||||
if maintenanceMode {
|
if maintenanceMode {
|
||||||
@@ -417,7 +431,7 @@ func runDeploymentAndSyncWithImage(
|
|||||||
Command: command,
|
Command: command,
|
||||||
Environment: env,
|
Environment: env,
|
||||||
Replicas: replicas,
|
Replicas: replicas,
|
||||||
PublicPort: int32(service.Port),
|
PublicPort: publicPort,
|
||||||
PublishedPort: publishedPort,
|
PublishedPort: publishedPort,
|
||||||
Domain: service.Domain,
|
Domain: service.Domain,
|
||||||
HealthPath: service.HealthCheckPath,
|
HealthPath: service.HealthCheckPath,
|
||||||
@@ -425,8 +439,10 @@ func runDeploymentAndSyncWithImage(
|
|||||||
VolumeMounts: loadServiceVolumes(db, service.ID),
|
VolumeMounts: loadServiceVolumes(db, service.ID),
|
||||||
Domains: serviceDomainNames(db, service.ID, service.Domain),
|
Domains: serviceDomainNames(db, service.ID, service.Domain),
|
||||||
Resources: deployment.ResourceLimits{
|
Resources: deployment.ResourceLimits{
|
||||||
MemoryBytes: parseMemoryLimit(service.Memory),
|
MemoryBytes: parseMemoryLimit(service.Memory),
|
||||||
CPUQuota: parseCPULimit(service.CPU),
|
MemoryReservation: parseMemoryLimit(service.MemoryReserve),
|
||||||
|
CPUQuota: parseCPULimit(service.CPU),
|
||||||
|
CPUShares: parseCPUReserveShares(service.CPUReserve),
|
||||||
},
|
},
|
||||||
Maintenance: maintenanceMode,
|
Maintenance: maintenanceMode,
|
||||||
MaintenanceURL: maintURL,
|
MaintenanceURL: maintURL,
|
||||||
@@ -440,25 +456,50 @@ func runDeploymentAndSyncWithImage(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if imageOverride != "" {
|
if imageOverride != "" || service.GitRepo == "" {
|
||||||
deployReq.BuildConfig = &deployment.BuildConfig{
|
// Prebuilt image deploy (rollback, redeploy, or image-sourced
|
||||||
BuildType: "prebuilt",
|
// service): resolve private-registry credentials by image host.
|
||||||
PrebuiltImage: imageOverride,
|
image := imageOverride
|
||||||
|
if image == "" {
|
||||||
|
image = service.Image
|
||||||
}
|
}
|
||||||
} else if service.GitRepo == "" {
|
var ownerID string
|
||||||
// Image-sourced service: pull the image instead of building.
|
_ = db.QueryRow(`SELECT owner_id FROM projects WHERE id = $1`, service.ProjectID).Scan(&ownerID)
|
||||||
|
auth := registryAuthFor(db, ownerID, image)
|
||||||
deployReq.BuildConfig = &deployment.BuildConfig{
|
deployReq.BuildConfig = &deployment.BuildConfig{
|
||||||
BuildType: "prebuilt",
|
BuildType: "prebuilt",
|
||||||
PrebuiltImage: service.Image,
|
PrebuiltImage: image,
|
||||||
|
PullUsername: auth.Username,
|
||||||
|
PullPassword: auth.Password,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// "auto" leaves BuildType empty so the manager detects it; the static
|
||||||
|
// builder additionally feeds its generated Dockerfile the service's
|
||||||
|
// build command and output dir.
|
||||||
|
buildType := ""
|
||||||
|
if service.Builder != "" && service.Builder != "auto" {
|
||||||
|
buildType = service.Builder
|
||||||
|
}
|
||||||
deployReq.BuildConfig = &deployment.BuildConfig{
|
deployReq.BuildConfig = &deployment.BuildConfig{
|
||||||
BuildType: "nixpacks",
|
BuildType: buildType,
|
||||||
SourcePath: sourcePath,
|
SourcePath: sourcePath,
|
||||||
Branch: req.Branch,
|
Branch: req.Branch,
|
||||||
Commit: req.CommitHash,
|
Commit: req.CommitHash,
|
||||||
NoCache: req.NoCache,
|
NoCache: req.NoCache,
|
||||||
}
|
}
|
||||||
|
if buildType == "static" {
|
||||||
|
deployReq.BuildConfig.BuildCommand = firstNonEmpty(service.StaticBuildCmd, "npm ci && npm run build")
|
||||||
|
deployReq.BuildConfig.BuildArgs = map[string]string{"STATIC_DIR": firstNonEmpty(service.StaticDir, "dist")}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg := capacityCheck(parentCtx, db, engine.DockerClient(), service, replicas); msg != "" {
|
||||||
|
failedAt := time.Now()
|
||||||
|
_, _ = db.Exec(
|
||||||
|
`UPDATE deployments SET status = 'failed', error = $1, completed_at = $2, updated_at = $2 WHERE id = $3`,
|
||||||
|
msg, failedAt, dbDeployment.ID,
|
||||||
|
)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
engineDeployment, err := engine.Deploy(ctx, deployReq)
|
engineDeployment, err := engine.Deploy(ctx, deployReq)
|
||||||
@@ -822,6 +863,9 @@ func handleRollbackDeployment(c *gin.Context) {
|
|||||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
s.created_at, s.updated_at
|
s.created_at, s.updated_at
|
||||||
FROM services s WHERE s.id = $1`, serviceID,
|
FROM services s WHERE s.id = $1`, serviceID,
|
||||||
).Scan(
|
).Scan(
|
||||||
@@ -829,7 +873,9 @@ func handleRollbackDeployment(c *gin.Context) {
|
|||||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt,
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt,
|
||||||
)
|
)
|
||||||
|
|
||||||
rollbackReq := CreateDeploymentRequest{
|
rollbackReq := CreateDeploymentRequest{
|
||||||
@@ -863,3 +909,35 @@ func handleRollbackDeployment(c *gin.Context) {
|
|||||||
"message": "Rollback initiated",
|
"message": "Rollback initiated",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// capacityCheck compares the requested per-replica memory (limit, falling
|
||||||
|
// back to reservation) against node capacity. app_settings.capacity_policy
|
||||||
|
// = "block" fails the deployment; the default "warn" only logs. CPU is not
|
||||||
|
// gated — shares are a weight, not a reservation.
|
||||||
|
func capacityCheck(ctx context.Context, db *database.DB, client *docker.Client, service Service, replicas int) string {
|
||||||
|
if client == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
requested := parseMemoryLimit(service.Memory)
|
||||||
|
if requested == 0 {
|
||||||
|
requested = parseMemoryLimit(service.MemoryReserve)
|
||||||
|
}
|
||||||
|
if requested == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
info, err := client.GetSystemInfo(ctx)
|
||||||
|
if err != nil || info.MemTotal <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
need := requested * int64(replicas)
|
||||||
|
if need <= info.MemTotal {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
msg := fmt.Sprintf("requested %d bytes x %d replicas exceeds node memory (%d bytes)",
|
||||||
|
requested, replicas, info.MemTotal)
|
||||||
|
if strings.ToLower(settingValue(db, "capacity_policy", "", "warn")) == "block" {
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
slog.Warn("capacity check exceeded", "service_id", service.ID, "detail", msg)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@@ -133,7 +133,9 @@ func handleGetGitProviders(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleGetGitHubAppInstallURL(c *gin.Context) {
|
func handleGetGitHubAppInstallURL(c *gin.Context) {
|
||||||
slug := strings.TrimSpace(os.Getenv("GITHUB_APP_SLUG"))
|
db, _ := c.Get("db")
|
||||||
|
d, _ := db.(*database.DB)
|
||||||
|
slug := strings.TrimSpace(settingValue(d, settingGitHubAppSlug, "GITHUB_APP_SLUG", ""))
|
||||||
if slug == "" {
|
if slug == "" {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "GITHUB_APP_SLUG is not configured"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "GITHUB_APP_SLUG is not configured"})
|
||||||
return
|
return
|
||||||
@@ -1073,8 +1075,8 @@ func fetchBitbucketRepositories(providerAPIURL, accessToken string) ([]remoteGit
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getGitHubAppInstallationToken(baseURL string, installationID int64) (string, error) {
|
func getGitHubAppInstallationToken(baseURL string, installationID int64) (string, error) {
|
||||||
appID := strings.TrimSpace(os.Getenv("GITHUB_APP_ID"))
|
appID := platformSetting(settingGitHubAppID, "GITHUB_APP_ID")
|
||||||
privateKeyPEM := strings.TrimSpace(os.Getenv("GITHUB_APP_PRIVATE_KEY"))
|
privateKeyPEM := platformSetting(settingGitHubAppPrivateKey, "GITHUB_APP_PRIVATE_KEY")
|
||||||
if appID == "" || privateKeyPEM == "" {
|
if appID == "" || privateKeyPEM == "" {
|
||||||
return "", fmt.Errorf("GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY must be configured")
|
return "", fmt.Errorf("GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY must be configured")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -90,11 +91,24 @@ func handleGitWebhookPush(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(payload.Ref, "refs/heads/") {
|
// Non-push events that still mean "the repo changed": fork-sync bots
|
||||||
|
// (sync), repository_dispatch, workflow_run completions. They carry no
|
||||||
|
// `ref`, so fall back to the webhook's branch filter (or the service's
|
||||||
|
// own git_branch via the repo match below).
|
||||||
|
event := c.Request.Header.Get("X-GitHub-Event")
|
||||||
|
if event == "" {
|
||||||
|
event = c.Request.Header.Get("X-Gitea-Event")
|
||||||
|
}
|
||||||
|
syncEvent := event == "sync" || event == "repository_dispatch" || event == "workflow_run"
|
||||||
|
|
||||||
|
if !strings.HasPrefix(payload.Ref, "refs/heads/") && !syncEvent {
|
||||||
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "not a branch push"})
|
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "not a branch push"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
branch := strings.TrimPrefix(payload.Ref, "refs/heads/")
|
branch := strings.TrimPrefix(payload.Ref, "refs/heads/")
|
||||||
|
if branch == "" {
|
||||||
|
branch = branchFilter
|
||||||
|
}
|
||||||
if branchFilter != "" && branchFilter != branch {
|
if branchFilter != "" && branchFilter != branch {
|
||||||
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "branch not watched"})
|
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "branch not watched"})
|
||||||
return
|
return
|
||||||
@@ -113,16 +127,40 @@ func handleGitWebhookPush(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Query(`
|
engineValue, _ := c.Get("deployment_engine")
|
||||||
|
engine, _ := engineValue.(*deployment.DeploymentEngine)
|
||||||
|
|
||||||
|
enqueued, err := dispatchPushToServices(c, db, engine, cloneURL, fullName, branch, commit, repoUserID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to match services"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{
|
||||||
|
"received": true,
|
||||||
|
"branch": branch,
|
||||||
|
"deployments": enqueued,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// dispatchPushToServices enqueues a deployment for every service tracking
|
||||||
|
// the pushed repo+branch. Shared by the per-repo webhook receiver and the
|
||||||
|
// GitHub App webhook endpoint.
|
||||||
|
func dispatchPushToServices(c *gin.Context, db *database.DB, engine *deployment.DeploymentEngine, cloneURL, fullName, branch, commit, repoUserID string) (int, error) {
|
||||||
|
// Sync-style events may carry no branch — match services on repo only.
|
||||||
|
branchClause := "s.git_branch = $1"
|
||||||
|
if branch == "" {
|
||||||
|
branchClause = "TRUE"
|
||||||
|
}
|
||||||
|
rows, err := db.Query(fmt.Sprintf(`
|
||||||
SELECT s.id, s.project_id, s.name, s.type, s.status, s.image, s.command,
|
SELECT s.id, s.project_id, s.name, s.type, s.status, s.image, s.command,
|
||||||
s.environment, s.git_repo, s.git_branch, s.build_path, s.cpu, s.memory,
|
s.environment, s.git_repo, s.git_branch, s.build_path, s.cpu, s.memory,
|
||||||
s.created_at, s.updated_at
|
s.created_at, s.updated_at
|
||||||
FROM services s
|
FROM services s
|
||||||
WHERE s.git_branch = $1 AND (s.git_repo = $2 OR s.git_repo = $3)`,
|
WHERE %s AND (s.git_repo = $2 OR s.git_repo = $3)`, branchClause),
|
||||||
branch, cloneURL, fullName)
|
branch, cloneURL, fullName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to match services"})
|
return 0, err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
@@ -134,15 +172,11 @@ func handleGitWebhookPush(c *gin.Context) {
|
|||||||
&s.Environment, &s.GitRepo, &s.GitBranch, &s.BuildPath, &s.CPU, &s.Memory,
|
&s.Environment, &s.GitRepo, &s.GitBranch, &s.BuildPath, &s.CPU, &s.Memory,
|
||||||
&s.CreatedAt, &s.UpdatedAt,
|
&s.CreatedAt, &s.UpdatedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan service"})
|
return 0, err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
services = append(services, s)
|
services = append(services, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
engineValue, _ := c.Get("deployment_engine")
|
|
||||||
engine, _ := engineValue.(*deployment.DeploymentEngine)
|
|
||||||
|
|
||||||
enqueued := 0
|
enqueued := 0
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -160,9 +194,9 @@ func handleGitWebhookPush(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
if _, err := db.Exec(
|
if _, err := db.Exec(
|
||||||
`INSERT INTO deployments
|
`INSERT INTO deployments
|
||||||
(id, service_id, commit_hash, status, image_name, image_tag, created_at, updated_at)
|
(id, service_id, version, commit_hash, status, image_name, image_tag, created_at, updated_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
||||||
d.ID, d.ServiceID, d.CommitHash, d.Status, d.ImageName, d.ImageTag, d.CreatedAt, d.UpdatedAt,
|
d.ID, d.ServiceID, fmt.Sprintf("v%d", now.Unix()), d.CommitHash, d.Status, d.ImageName, d.ImageTag, d.CreatedAt, d.UpdatedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -181,7 +215,11 @@ func handleGitWebhookPush(c *gin.Context) {
|
|||||||
)
|
)
|
||||||
// Webhook pushes default to a clean build — stale layers are the
|
// Webhook pushes default to a clean build — stale layers are the
|
||||||
// classic silent-rollback failure (dflow convention).
|
// classic silent-rollback failure (dflow convention).
|
||||||
webhookReq := CreateDeploymentRequest{CommitHash: commit, Branch: branch, Trigger: "webhook", NoCache: true}
|
branchForBuild := branch
|
||||||
|
if branchForBuild == "" {
|
||||||
|
branchForBuild = service.GitBranch
|
||||||
|
}
|
||||||
|
webhookReq := CreateDeploymentRequest{CommitHash: commit, Branch: branchForBuild, Trigger: "webhook", NoCache: true}
|
||||||
if pos := getDeployQueue(c).Enqueue(service.ID, deployqueue.Job{
|
if pos := getDeployQueue(c).Enqueue(service.ID, deployqueue.Job{
|
||||||
DeploymentID: d.ID,
|
DeploymentID: d.ID,
|
||||||
Run: func(jctx context.Context) {
|
Run: func(jctx context.Context) {
|
||||||
@@ -199,12 +237,7 @@ func handleGitWebhookPush(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
enqueued++
|
enqueued++
|
||||||
}
|
}
|
||||||
|
return enqueued, nil
|
||||||
c.JSON(http.StatusAccepted, gin.H{
|
|
||||||
"received": true,
|
|
||||||
"branch": branch,
|
|
||||||
"deployments": enqueued,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// verifyGitWebhookSignature checks the push signature per provider:
|
// verifyGitWebhookSignature checks the push signature per provider:
|
||||||
|
|||||||
@@ -0,0 +1,310 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/subtle"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/deployment"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GitHub App self-provisioning (manifest flow): an admin POSTs the manifest
|
||||||
|
// to github.com/settings/apps/new, GitHub redirects back with `code`, and the
|
||||||
|
// code is exchanged at /app-manifests/{code}/conversions for real credentials
|
||||||
|
// stored in app_settings. Values stored there take precedence over the
|
||||||
|
// GITHUB_APP_* env vars, so the flow removes the manual env-setup barrier.
|
||||||
|
|
||||||
|
const (
|
||||||
|
settingGitHubAppID = "github_app.id"
|
||||||
|
settingGitHubAppSlug = "github_app.slug"
|
||||||
|
settingGitHubAppName = "github_app.name"
|
||||||
|
settingGitHubAppClientID = "github_app.client_id"
|
||||||
|
settingGitHubAppClientSecret = "github_app.client_secret"
|
||||||
|
settingGitHubAppPrivateKey = "github_app.private_key"
|
||||||
|
settingGitHubAppWebhookSecret = "github_app.webhook_secret"
|
||||||
|
)
|
||||||
|
|
||||||
|
// platformSetting resolves a setting row first, then env, then empty.
|
||||||
|
func platformSetting(key, envKey string) string {
|
||||||
|
return strings.TrimSpace(settingValue(GetAuditDB(), key, envKey, ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func githubAppConfigured(db *database.DB) bool {
|
||||||
|
return strings.TrimSpace(settingValue(db, settingGitHubAppID, "GITHUB_APP_ID", "")) != "" &&
|
||||||
|
strings.TrimSpace(settingValue(db, settingGitHubAppPrivateKey, "GITHUB_APP_PRIVATE_KEY", "")) != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /admin/git/github-app/manifest — builds the GitHub App manifest the
|
||||||
|
// browser POSTs to github.com. Response carries the manifest as a JSON string
|
||||||
|
// (the form submits it verbatim) plus the destination URL.
|
||||||
|
func handleAdminGitHubAppManifest(c *gin.Context) {
|
||||||
|
var req struct {
|
||||||
|
BaseURL string `json:"base_url" binding:"required"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Organization string `json:"organization"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_BODY", "base_url is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
base := strings.TrimSuffix(strings.TrimSpace(req.BaseURL), "/")
|
||||||
|
if !strings.HasPrefix(base, "https://") &&
|
||||||
|
!strings.HasPrefix(base, "http://localhost") &&
|
||||||
|
!strings.HasPrefix(base, "http://127.0.0.1") {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "base_url must be an https URL (localhost http allowed)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stateBuf := make([]byte, 16)
|
||||||
|
if _, err := rand.Read(stateBuf); err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate state")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
state := hex.EncodeToString(stateBuf)
|
||||||
|
|
||||||
|
name := strings.TrimSpace(req.Name)
|
||||||
|
if name == "" {
|
||||||
|
db, _ := c.Get("db")
|
||||||
|
if d, ok := db.(*database.DB); ok {
|
||||||
|
name = strings.TrimSpace(settingValue(d, settingBrandProductName, "BRAND_NAME", ""))
|
||||||
|
}
|
||||||
|
if name == "" {
|
||||||
|
name = "Containr"
|
||||||
|
}
|
||||||
|
name = fmt.Sprintf("%s-%s", name, time.Now().UTC().Format("2006-01-02"))
|
||||||
|
}
|
||||||
|
|
||||||
|
callbackURL := base + "/git/github-app/callback"
|
||||||
|
manifest, err := json.Marshal(map[string]interface{}{
|
||||||
|
"name": name,
|
||||||
|
"url": base,
|
||||||
|
"hook_attributes": map[string]interface{}{
|
||||||
|
"url": base + "/api/git/github-app/webhook",
|
||||||
|
"active": true,
|
||||||
|
},
|
||||||
|
"redirect_url": callbackURL,
|
||||||
|
"callback_urls": []string{callbackURL},
|
||||||
|
"public": false,
|
||||||
|
"default_permissions": map[string]string{
|
||||||
|
"contents": "read",
|
||||||
|
"metadata": "read",
|
||||||
|
"pull_requests": "write",
|
||||||
|
},
|
||||||
|
"default_events": []string{"push", "pull_request"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to build manifest")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
org := strings.TrimSpace(req.Organization)
|
||||||
|
createURL := fmt.Sprintf("https://github.com/settings/apps/new?state=%s", state)
|
||||||
|
if org != "" {
|
||||||
|
createURL = fmt.Sprintf("https://github.com/organizations/%s/settings/apps/new?state=%s", org, state)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"url": createURL,
|
||||||
|
"manifest": string(manifest),
|
||||||
|
"state": state,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /admin/git/github-app/convert — exchanges the `code` GitHub appends to
|
||||||
|
// the manifest redirect for the real app credentials, then stores them.
|
||||||
|
func handleAdminGitHubAppConvert(c *gin.Context) {
|
||||||
|
var req struct {
|
||||||
|
Code string `json:"code" binding:"required"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_BODY", "code is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
apiBase := strings.TrimSuffix(strings.TrimSpace(os.Getenv("GITHUB_APP_BASE_URL")), "/")
|
||||||
|
if apiBase == "" {
|
||||||
|
apiBase = "https://api.github.com"
|
||||||
|
}
|
||||||
|
request, err := http.NewRequest(http.MethodPost,
|
||||||
|
fmt.Sprintf("%s/app-manifests/%s/conversions", apiBase, strings.TrimSpace(req.Code)), nil)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to build conversion request")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
request.Header.Set("Accept", "application/vnd.github+json")
|
||||||
|
request.Header.Set("X-GitHub-Api-Version", "2022-11-28")
|
||||||
|
|
||||||
|
client := &http.Client{Timeout: 15 * time.Second}
|
||||||
|
resp, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadGateway, "UPSTREAM", "failed to reach GitHub")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||||
|
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
|
||||||
|
respondError(c, http.StatusBadGateway, "UPSTREAM",
|
||||||
|
fmt.Sprintf("GitHub conversion failed (%d)", resp.StatusCode))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var app struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Slug string `json:"slug"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
ClientID string `json:"client_id"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
|
PEM string `json:"pem"`
|
||||||
|
WebhookSecret string `json:"webhook_secret"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(body, &app); err != nil || app.ID == 0 {
|
||||||
|
respondError(c, http.StatusBadGateway, "UPSTREAM", "invalid conversion response")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
for _, kv := range []struct {
|
||||||
|
key string
|
||||||
|
value string
|
||||||
|
secret bool
|
||||||
|
}{
|
||||||
|
{settingGitHubAppID, fmt.Sprintf("%d", app.ID), false},
|
||||||
|
{settingGitHubAppSlug, app.Slug, false},
|
||||||
|
{settingGitHubAppName, app.Name, false},
|
||||||
|
{settingGitHubAppClientID, app.ClientID, false},
|
||||||
|
{settingGitHubAppClientSecret, app.ClientSecret, true},
|
||||||
|
{settingGitHubAppPrivateKey, app.PEM, true},
|
||||||
|
{settingGitHubAppWebhookSecret, app.WebhookSecret, true},
|
||||||
|
} {
|
||||||
|
if err := setSetting(db, kv.key, kv.value, kv.secret); err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to store app credentials")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "github_app", fmt.Sprintf("%d", app.ID), "provision",
|
||||||
|
map[string]interface{}{"name": app.Name, "slug": app.Slug})
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"app_id": app.ID,
|
||||||
|
"slug": app.Slug,
|
||||||
|
"name": app.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /admin/git/github-app — provisioning status for the settings UI.
|
||||||
|
func handleAdminGetGitHubApp(c *gin.Context) {
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
var stored string
|
||||||
|
source := "none"
|
||||||
|
if err := db.QueryRow(`SELECT value FROM app_settings WHERE key = $1`, settingGitHubAppID).Scan(&stored); err == nil && strings.TrimSpace(stored) != "" {
|
||||||
|
source = "settings"
|
||||||
|
} else if strings.TrimSpace(os.Getenv("GITHUB_APP_ID")) != "" {
|
||||||
|
source = "env"
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"configured": githubAppConfigured(db),
|
||||||
|
"source": source,
|
||||||
|
"app_id": settingValue(db, settingGitHubAppID, "GITHUB_APP_ID", ""),
|
||||||
|
"slug": settingValue(db, settingGitHubAppSlug, "GITHUB_APP_SLUG", ""),
|
||||||
|
"name": settingValue(db, settingGitHubAppName, "", ""),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/git/github-app/webhook — the single App-level webhook all
|
||||||
|
// installed repos deliver to. Verified against the provisioned webhook
|
||||||
|
// secret, then dispatched to matching services.
|
||||||
|
func handleGitHubAppWebhook(c *gin.Context) {
|
||||||
|
dbValue, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := dbValue.(*database.DB)
|
||||||
|
|
||||||
|
secret := strings.TrimSpace(settingValue(db, settingGitHubAppWebhookSecret, "GITHUB_APP_WEBHOOK_SECRET", ""))
|
||||||
|
if secret == "" {
|
||||||
|
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "GitHub App webhook not configured"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(io.LimitReader(c.Request.Body, 4<<20))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to read payload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mac := hmac.New(sha256.New, []byte(secret))
|
||||||
|
mac.Write(body)
|
||||||
|
expected := mac.Sum(nil)
|
||||||
|
sig := strings.TrimPrefix(c.GetHeader("X-Hub-Signature-256"), "sha256=")
|
||||||
|
decoded, err := hex.DecodeString(sig)
|
||||||
|
if err != nil || len(decoded) == 0 || subtle.ConstantTimeCompare(decoded, expected) != 1 {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid signature"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
event := c.GetHeader("X-GitHub-Event")
|
||||||
|
if event != "push" {
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "event " + event})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload struct {
|
||||||
|
Ref string `json:"ref"`
|
||||||
|
After string `json:"after"`
|
||||||
|
Repository struct {
|
||||||
|
FullName string `json:"full_name"`
|
||||||
|
CloneURL string `json:"clone_url"`
|
||||||
|
} `json:"repository"`
|
||||||
|
HeadCommit struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
} `json:"head_commit"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(body, &payload); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid payload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(payload.Ref, "refs/heads/") {
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "not a branch push"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
branch := strings.TrimPrefix(payload.Ref, "refs/heads/")
|
||||||
|
commit := payload.HeadCommit.ID
|
||||||
|
if commit == "" {
|
||||||
|
commit = payload.After
|
||||||
|
}
|
||||||
|
|
||||||
|
cloneURL := payload.Repository.CloneURL
|
||||||
|
fullName := payload.Repository.FullName
|
||||||
|
if cloneURL == "" {
|
||||||
|
cloneURL = fmt.Sprintf("https://github.com/%s.git", fullName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services tracking an App-connected repo store either the clone URL or
|
||||||
|
// owner/name — match both like the per-repo receiver does.
|
||||||
|
engineValue, _ := c.Get("deployment_engine")
|
||||||
|
engine, _ := engineValue.(*deployment.DeploymentEngine)
|
||||||
|
|
||||||
|
enqueued, err := dispatchPushToServices(c, db, engine, cloneURL, fullName, branch, commit, "")
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to match services"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{
|
||||||
|
"received": true,
|
||||||
|
"branch": branch,
|
||||||
|
"deployments": enqueued,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,245 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/database/sqlcdb"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Team invites: an admin creates a single-use link; the invitee registers
|
||||||
|
// through it even while public signup is closed. Tokens are random 32-byte
|
||||||
|
// values shared once — only their sha256 hash is stored.
|
||||||
|
|
||||||
|
type inviteJSON struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Email *string `json:"email,omitempty"`
|
||||||
|
ExpiresAt time.Time `json:"expires_at"`
|
||||||
|
UsedAt *time.Time `json:"used_at,omitempty"`
|
||||||
|
CreatedAt *time.Time `json:"created_at,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func toInviteJSON(i sqlcdb.UserInvite) inviteJSON {
|
||||||
|
out := inviteJSON{ID: i.ID.String(), ExpiresAt: i.ExpiresAt}
|
||||||
|
if i.Email.Valid && i.Email.String != "" {
|
||||||
|
out.Email = &i.Email.String
|
||||||
|
}
|
||||||
|
if i.UsedAt.Valid {
|
||||||
|
out.UsedAt = &i.UsedAt.Time
|
||||||
|
}
|
||||||
|
if i.CreatedAt.Valid {
|
||||||
|
out.CreatedAt = &i.CreatedAt.Time
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func inviteTokenHash(token string) string {
|
||||||
|
sum := sha256.Sum256([]byte(token))
|
||||||
|
return hex.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateInviteToken() (string, error) {
|
||||||
|
buf := make([]byte, 32)
|
||||||
|
if _, err := rand.Read(buf); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(buf), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /admin/invites — {email?, expires_in_hours?} → {invite, token, url}
|
||||||
|
func handleAdminCreateInvite(c *gin.Context) {
|
||||||
|
var req struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
ExpiresInHours *int `json:"expires_in_hours"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_BODY", "invalid request body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ttl := 24 * 7
|
||||||
|
if req.ExpiresInHours != nil {
|
||||||
|
if *req.ExpiresInHours < 1 || *req.ExpiresInHours > 24*90 {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "expires_in_hours must be 1–2160")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ttl = *req.ExpiresInHours
|
||||||
|
}
|
||||||
|
token, err := generateInviteToken()
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := requireAuthenticatedUserUUID(c)
|
||||||
|
params := sqlcdb.CreateUserInviteParams{
|
||||||
|
TokenHash: inviteTokenHash(token),
|
||||||
|
CreatedBy: uuid.NullUUID{UUID: userID, Valid: true},
|
||||||
|
ExpiresAt: time.Now().UTC().Add(time.Duration(ttl) * time.Hour),
|
||||||
|
}
|
||||||
|
if email := strings.ToLower(strings.TrimSpace(req.Email)); email != "" {
|
||||||
|
params.Email = sql.NullString{String: email, Valid: true}
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
invite, err := sqlcdb.New(db.DB).CreateUserInvite(c.Request.Context(), params)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to create invite")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
|
"invite": toInviteJSON(invite),
|
||||||
|
"token": token,
|
||||||
|
"url": "/auth/accept-invite?token=" + token,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /admin/invites
|
||||||
|
func handleAdminListInvites(c *gin.Context) {
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
invites, err := sqlcdb.New(db.DB).ListUserInvites(c.Request.Context())
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to list invites")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out := make([]inviteJSON, 0, len(invites))
|
||||||
|
for _, i := range invites {
|
||||||
|
out = append(out, toInviteJSON(i))
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"invites": out})
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE /admin/invites/:id — revoke an unused invite.
|
||||||
|
func handleAdminDeleteInvite(c *gin.Context) {
|
||||||
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid invite id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
if err := sqlcdb.New(db.DB).DeleteUserInvite(c.Request.Context(), id); err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to delete invite")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Status(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /auth/invites/:token — public. Lets the accept page validate the link
|
||||||
|
// and pre-fill a bound email before the user commits to registering.
|
||||||
|
func handleGetInvite(c *gin.Context) {
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
invite, err := sqlcdb.New(db.DB).GetUserInviteByTokenHash(c.Request.Context(), inviteTokenHash(c.Param("token")))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "invite not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expired := time.Now().After(invite.ExpiresAt)
|
||||||
|
var email *string
|
||||||
|
if invite.Email.Valid && invite.Email.String != "" {
|
||||||
|
email = &invite.Email.String
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"valid": !invite.UsedAt.Valid && !expired,
|
||||||
|
"used": invite.UsedAt.Valid,
|
||||||
|
"expired": expired,
|
||||||
|
"email": email,
|
||||||
|
"expires_at": invite.ExpiresAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /auth/accept-invite — {token, name, email, password}
|
||||||
|
func handleAcceptInvite(c *gin.Context) {
|
||||||
|
var req struct {
|
||||||
|
Token string `json:"token" binding:"required"`
|
||||||
|
Name string `json:"name" binding:"required"`
|
||||||
|
Email string `json:"email" binding:"required"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_BODY", "token, name, email and password are required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(req.Password) < 8 {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "password must be at least 8 characters")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Email = strings.ToLower(strings.TrimSpace(req.Email))
|
||||||
|
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
q := sqlcdb.New(db.DB)
|
||||||
|
invite, err := q.GetUserInviteByTokenHash(c.Request.Context(), inviteTokenHash(req.Token))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "invite not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if invite.UsedAt.Valid {
|
||||||
|
respondError(c, http.StatusGone, "INVITE_USED", "invite already used")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if time.Now().After(invite.ExpiresAt) {
|
||||||
|
respondError(c, http.StatusGone, "INVITE_EXPIRED", "invite expired")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if invite.Email.Valid && invite.Email.String != "" && invite.Email.String != req.Email {
|
||||||
|
respondError(c, http.StatusForbidden, "INVITE_EMAIL_MISMATCH", "invite is bound to a different email")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var existing int
|
||||||
|
if err := db.QueryRow(`SELECT COUNT(*) FROM users WHERE email = $1`, req.Email).Scan(&existing); err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "database error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if existing > 0 {
|
||||||
|
respondError(c, http.StatusConflict, "CONFLICT", "user already exists")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := createBetterAuthUser(c, strings.TrimSpace(req.Name), req.Email, req.Password); err != nil && !errors.Is(err, errBetterAuthUserExists) {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to hash password")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var user User
|
||||||
|
err = db.QueryRow(`
|
||||||
|
INSERT INTO users (email, password_hash, name)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
RETURNING id, email, name, COALESCE(avatar_url, ''), is_admin, created_at
|
||||||
|
`, req.Email, string(hashedPassword), strings.TrimSpace(req.Name)).
|
||||||
|
Scan(&user.ID, &user.Email, &user.Name, &user.AvatarURL, &user.IsAdmin, &user.CreatedAt)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusConflict, "CONFLICT", "user already exists")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userUUID, _ := uuid.Parse(user.ID)
|
||||||
|
if err := q.MarkUserInviteUsed(c.Request.Context(), sqlcdb.MarkUserInviteUsedParams{
|
||||||
|
ID: invite.ID,
|
||||||
|
UsedBy: uuid.NullUUID{UUID: userUUID, Valid: userUUID != uuid.Nil},
|
||||||
|
}); err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to consume invite")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jwtSecret := c.MustGet("jwt_secret").(string)
|
||||||
|
token, err := generateJWT(user.ID, user.Email, jwtSecret)
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, AuthResponse{Token: token, User: user})
|
||||||
|
}
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
// Notification producers — the emitters behind `insertUserNotification`.
|
||||||
|
// Deployment success/failure lives in deployments.go; this file covers
|
||||||
|
// the remaining producers from the roadmap: admin fan-out helper, the
|
||||||
|
// agent offline/online sweep, and the upgrade-available check.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/database/sqlcdb"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Version is stamped at image build time via -ldflags "-X
|
||||||
|
// containr/internal/api.Version=…". "dev" disables the upgrade check.
|
||||||
|
var Version = "dev"
|
||||||
|
|
||||||
|
// notifyAdmins inserts a notification for every admin user. Best-effort.
|
||||||
|
func notifyAdmins(db *database.DB, kind, title, body, resourceType, resourceID string) {
|
||||||
|
if db == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rows, err := db.Query(`SELECT id FROM users WHERE is_admin`)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("containr: notify admins lookup failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var id uuid.UUID
|
||||||
|
if err := rows.Scan(&id); err == nil {
|
||||||
|
insertUserNotification(db, id.String(), kind, title, body, resourceType, resourceID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartAgentHealthSweep marks agents offline when their heartbeat goes
|
||||||
|
// stale (and back online when it resumes), notifying admins on each
|
||||||
|
// transition. Heartbeats keep the row fresh via UpdateAgentHeartbeat.
|
||||||
|
func StartAgentHealthSweep(ctx context.Context, db *database.DB) {
|
||||||
|
const staleAfter = 3 * time.Minute
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(time.Minute)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
reconcileAgentHealth(db, staleAfter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
type agentHealthRow struct {
|
||||||
|
id string
|
||||||
|
name string
|
||||||
|
status string
|
||||||
|
stale bool
|
||||||
|
autoPrune bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func reconcileAgentHealth(db *database.DB, staleAfter time.Duration) {
|
||||||
|
cutoff := time.Now().Add(-staleAfter)
|
||||||
|
var agents []agentHealthRow
|
||||||
|
rows, err := db.Query(
|
||||||
|
`SELECT id, COALESCE(name, hostname, id::text), COALESCE(status, ''),
|
||||||
|
COALESCE(last_heartbeat < $1, TRUE), auto_prune
|
||||||
|
FROM node_agents`, cutoff)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("containr: agent health sweep: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for rows.Next() {
|
||||||
|
var a agentHealthRow
|
||||||
|
if err := rows.Scan(&a.id, &a.name, &a.status, &a.stale, &a.autoPrune); err == nil {
|
||||||
|
agents = append(agents, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rows.Close()
|
||||||
|
|
||||||
|
for _, a := range agents {
|
||||||
|
switch {
|
||||||
|
case a.stale && a.status != "offline":
|
||||||
|
if _, err := db.Exec(
|
||||||
|
`UPDATE node_agents SET status = 'offline', updated_at = NOW() WHERE id = $1`, a.id); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
notifyAdmins(db, "agent", "Node agent offline",
|
||||||
|
fmt.Sprintf("Agent %s missed its heartbeat for over %s.", a.name, staleAfter), "agent", a.id)
|
||||||
|
case !a.stale && a.status == "offline":
|
||||||
|
if _, err := db.Exec(
|
||||||
|
`UPDATE node_agents SET status = 'online', updated_at = NOW() WHERE id = $1`, a.id); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
notifyAdmins(db, "agent", "Node agent back online",
|
||||||
|
fmt.Sprintf("Agent %s resumed heartbeating.", a.name), "agent", a.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleAutoPrune(db, agents)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scheduleAutoPrune enqueues a `prune` command once per day for agents
|
||||||
|
// with auto_prune enabled. Disk pressure is the most common small-VPS
|
||||||
|
// failure — this is the dflow setServerAutoCleanup analog.
|
||||||
|
func scheduleAutoPrune(db *database.DB, agents []agentHealthRow) {
|
||||||
|
q := sqlcdb.New(db.DB)
|
||||||
|
for _, a := range agents {
|
||||||
|
if !a.autoPrune || a.stale {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
last, err := q.GetLastAgentCommandByType(context.Background(),
|
||||||
|
sqlcdb.GetLastAgentCommandByTypeParams{NodeAgentID: a.id, Type: "prune"})
|
||||||
|
if err == nil && last.CreatedAt.Valid && time.Since(last.CreatedAt.Time) < 24*time.Hour {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := q.CreateCommand(context.Background(), sqlcdb.CreateCommandParams{
|
||||||
|
ID: uuid.New().String(),
|
||||||
|
Type: "prune",
|
||||||
|
NodeAgentID: a.id,
|
||||||
|
}); err != nil {
|
||||||
|
log.Printf("containr: auto-prune enqueue failed for agent %s: %v", a.id, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartUpgradeCheck polls GitHub for the latest release once a day and
|
||||||
|
// notifies admins when it is newer than the running version. The last
|
||||||
|
// notified tag is persisted in app_settings so restarts don't re-fire.
|
||||||
|
func StartUpgradeCheck(ctx context.Context, db *database.DB, currentVersion string) {
|
||||||
|
if currentVersion == "" || currentVersion == "dev" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
checkForUpgrade(db, currentVersion)
|
||||||
|
ticker := time.NewTicker(24 * time.Hour)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
checkForUpgrade(db, currentVersion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkForUpgrade(db *database.DB, currentVersion string) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
||||||
|
"https://api.github.com/repos/Dvorinka/Containr/releases/latest", nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Header.Set("Accept", "application/vnd.github+json")
|
||||||
|
req.Header.Set("User-Agent", "containr-upgrade-check")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var release struct {
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
HTMLURL string `json:"html_url"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil || release.TagName == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !semverNewer(release.TagName, currentVersion) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var lastNotified string
|
||||||
|
_ = db.QueryRow(`SELECT value FROM app_settings WHERE key = 'upgrade_notified_tag'`).Scan(&lastNotified)
|
||||||
|
if lastNotified == release.TagName {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
notifyAdmins(db, "upgrade", "Containr update available",
|
||||||
|
fmt.Sprintf("Release %s is available (running %s). %s", release.TagName, currentVersion, release.HTMLURL),
|
||||||
|
"release", release.TagName)
|
||||||
|
_, _ = db.Exec(
|
||||||
|
`INSERT INTO app_settings (key, value, updated_at) VALUES ('upgrade_notified_tag', $1, NOW())
|
||||||
|
ON CONFLICT (key) DO UPDATE SET value = $1, updated_at = NOW()`, release.TagName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// semverNewer reports whether candidate is a higher semver than current.
|
||||||
|
// Non-semver input (e.g. "dev", commit hashes) compares false.
|
||||||
|
func semverNewer(candidate, current string) bool {
|
||||||
|
parse := func(s string) ([]int, bool) {
|
||||||
|
s = strings.TrimPrefix(strings.TrimSpace(s), "v")
|
||||||
|
// Prerelease/build metadata sorts after the same base version
|
||||||
|
// upstream — for "is a newer release available" purposes the base
|
||||||
|
// comparison is what matters.
|
||||||
|
if j := strings.IndexAny(s, "-+"); j >= 0 {
|
||||||
|
s = s[:j]
|
||||||
|
}
|
||||||
|
parts := strings.Split(s, ".")
|
||||||
|
if len(parts) == 0 || len(parts) > 3 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
out := make([]int, 3)
|
||||||
|
for i, p := range parts {
|
||||||
|
n, err := strconv.Atoi(p)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
out[i] = n
|
||||||
|
}
|
||||||
|
return out, true
|
||||||
|
}
|
||||||
|
c, ok1 := parse(candidate)
|
||||||
|
cur, ok2 := parse(current)
|
||||||
|
if !ok1 || !ok2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
if c[i] != cur[i] {
|
||||||
|
return c[i] > cur[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSemverNewer(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
candidate, current string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"v1.2.3", "v1.2.2", true},
|
||||||
|
{"1.3.0", "v1.2.9", true},
|
||||||
|
{"v2.0.0", "v1.9.9", true},
|
||||||
|
{"v1.2.3", "v1.2.3", false},
|
||||||
|
{"v1.2.0", "v1.2.3", false},
|
||||||
|
{"v1.10.0", "v1.9.9", true}, // numeric, not lexicographic
|
||||||
|
{"dev", "v1.0.0", false},
|
||||||
|
{"v1.0.0", "main", false},
|
||||||
|
{"v1.2.3-rc.1", "v1.2.2", true}, // prerelease suffix tolerated
|
||||||
|
{"", "v1.0.0", false},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
if got := semverNewer(tc.candidate, tc.current); got != tc.want {
|
||||||
|
t.Errorf("semverNewer(%q, %q) = %v, want %v", tc.candidate, tc.current, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/deployqueue"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GET /operations — a single cross-cutting view of platform work: active
|
||||||
|
// and recent deployments, the per-service deploy queue, recent cron runs,
|
||||||
|
// and recent backups. Scoped to the caller's projects (admins see all).
|
||||||
|
|
||||||
|
type operationDeployment struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
ServiceID string `json:"service_id"`
|
||||||
|
ServiceName string `json:"service_name"`
|
||||||
|
ProjectName string `json:"project_name"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
Error *string `json:"error,omitempty"`
|
||||||
|
StartedAt *time.Time `json:"started_at,omitempty"`
|
||||||
|
CompletedAt *time.Time `json:"completed_at,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type operationCronRun struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
JobName string `json:"job_name"`
|
||||||
|
Schedule string `json:"schedule"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
StartedAt time.Time `json:"started_at"`
|
||||||
|
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
||||||
|
Error *string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type operationBackup struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
DatabaseID string `json:"database_id"`
|
||||||
|
DBName string `json:"database_name"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Size string `json:"size"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
Completed *time.Time `json:"completed_at,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleGetOperations(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
isAdmin := contextIsAdmin(c)
|
||||||
|
|
||||||
|
// Same visibility rule as the deployment list endpoint.
|
||||||
|
visibility := `p.is_approved OR p.owner_id = $1 OR $2::bool
|
||||||
|
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1)`
|
||||||
|
|
||||||
|
active := []operationDeployment{}
|
||||||
|
rows, err := db.Query(
|
||||||
|
`SELECT d.id, d.service_id, s.name, p.name, d.status,
|
||||||
|
COALESCE(d.image_name, ''), d.error, d.started_at, d.completed_at, d.created_at
|
||||||
|
FROM deployments d
|
||||||
|
JOIN services s ON s.id = d.service_id
|
||||||
|
JOIN projects p ON p.id = s.project_id
|
||||||
|
WHERE d.status IN ('queued','pending','building','deploying','rolling_back')
|
||||||
|
AND (`+visibility+`)
|
||||||
|
ORDER BY d.created_at ASC`, userID, isAdmin)
|
||||||
|
if err == nil {
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var d operationDeployment
|
||||||
|
var sid string
|
||||||
|
_ = rows.Scan(&d.ID, &sid, &d.ServiceName, &d.ProjectName, &d.Status, &d.Image, &d.Error, &d.StartedAt, &d.CompletedAt, &d.CreatedAt)
|
||||||
|
d.ServiceID = sid
|
||||||
|
active = append(active, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
recentFailed := []operationDeployment{}
|
||||||
|
rows2, err2 := db.Query(
|
||||||
|
`SELECT d.id, d.service_id, s.name, p.name, d.status,
|
||||||
|
COALESCE(d.image_name, ''), d.error, d.started_at, d.completed_at, d.created_at
|
||||||
|
FROM deployments d
|
||||||
|
JOIN services s ON s.id = d.service_id
|
||||||
|
JOIN projects p ON p.id = s.project_id
|
||||||
|
WHERE d.status IN ('failed','cancelled','rolled_back')
|
||||||
|
AND d.updated_at > NOW() - INTERVAL '24 hours'
|
||||||
|
AND (`+visibility+`)
|
||||||
|
ORDER BY d.updated_at DESC
|
||||||
|
LIMIT 20`, userID, isAdmin)
|
||||||
|
if err2 == nil {
|
||||||
|
defer rows2.Close()
|
||||||
|
for rows2.Next() {
|
||||||
|
var d operationDeployment
|
||||||
|
var sid string
|
||||||
|
_ = rows2.Scan(&d.ID, &sid, &d.ServiceName, &d.ProjectName, &d.Status, &d.Image, &d.Error, &d.StartedAt, &d.CompletedAt, &d.CreatedAt)
|
||||||
|
d.ServiceID = sid
|
||||||
|
recentFailed = append(recentFailed, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cronRuns := []operationCronRun{}
|
||||||
|
rows3, err3 := db.Query(
|
||||||
|
`SELECT e.id, j.name, j.schedule, e.status, e.started_at, e.finished_at, e.error
|
||||||
|
FROM cron_executions e
|
||||||
|
JOIN cron_jobs j ON j.id = e.cron_job_id
|
||||||
|
JOIN projects p ON p.id = j.project_id
|
||||||
|
WHERE e.started_at > NOW() - INTERVAL '24 hours'
|
||||||
|
AND (`+visibility+`)
|
||||||
|
ORDER BY e.started_at DESC
|
||||||
|
LIMIT 30`, userID, isAdmin)
|
||||||
|
if err3 == nil {
|
||||||
|
defer rows3.Close()
|
||||||
|
for rows3.Next() {
|
||||||
|
var r operationCronRun
|
||||||
|
_ = rows3.Scan(&r.ID, &r.JobName, &r.Schedule, &r.Status, &r.StartedAt, &r.FinishedAt, &r.Error)
|
||||||
|
cronRuns = append(cronRuns, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backups := []operationBackup{}
|
||||||
|
rows4, err4 := db.Query(
|
||||||
|
`SELECT b.id, b.database_id, ds.name, b.status, b.size, b.created_at, b.completed_at
|
||||||
|
FROM database_backups b
|
||||||
|
JOIN database_services ds ON ds.id = b.database_id
|
||||||
|
WHERE b.created_at > NOW() - INTERVAL '24 hours'
|
||||||
|
AND (ds.user_id = $1 OR $2::bool)
|
||||||
|
ORDER BY b.created_at DESC
|
||||||
|
LIMIT 20`, userID, isAdmin)
|
||||||
|
if err4 == nil {
|
||||||
|
defer rows4.Close()
|
||||||
|
for rows4.Next() {
|
||||||
|
var b operationBackup
|
||||||
|
_ = rows4.Scan(&b.ID, &b.DatabaseID, &b.DBName, &b.Status, &b.Size, &b.CreatedAt, &b.Completed)
|
||||||
|
backups = append(backups, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var queue []deployqueue.ServiceQueueState
|
||||||
|
if queueValue, exists := c.Get("deploy_queue"); exists && queueValue != nil {
|
||||||
|
queue = queueValue.(*deployqueue.Queue).Snapshot()
|
||||||
|
}
|
||||||
|
if queue == nil {
|
||||||
|
queue = []deployqueue.ServiceQueueState{}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"active_deployments": active,
|
||||||
|
"recent_failures": recentFailed,
|
||||||
|
"cron_runs": cronRuns,
|
||||||
|
"backups": backups,
|
||||||
|
"deploy_queue": queue,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -152,6 +153,7 @@ func handleCreateProject(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "project", projectRow.ID.String(), "project.create", map[string]interface{}{"name": req.Name})
|
||||||
c.JSON(http.StatusCreated, mapSQLCProject(projectRow))
|
c.JSON(http.StatusCreated, mapSQLCProject(projectRow))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,6 +299,28 @@ func handleDeleteProject(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Managed databases provisioned into this project (graph templates,
|
||||||
|
// compose imports) carry project_id and must be torn down first — their
|
||||||
|
// containers are not labelled containr.project and hold references to the
|
||||||
|
// project network.
|
||||||
|
if dbHandlerValue, exists := c.Get("database_handler"); exists && dbHandlerValue != nil {
|
||||||
|
if dbHandler, ok := dbHandlerValue.(*DatabaseHandler); ok {
|
||||||
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second)
|
||||||
|
projectDBs, listErr := dbHandler.queries.ListDatabaseServicesByProject(ctx, uuid.NullUUID{UUID: projectID, Valid: true})
|
||||||
|
if listErr == nil {
|
||||||
|
for _, projectDB := range projectDBs {
|
||||||
|
if projectDB.Provider != "external" {
|
||||||
|
if err := dbHandler.deleteManagedDatabaseRuntime(projectDB.ID); err != nil {
|
||||||
|
log.Printf("delete project %s: database %s runtime removal failed: %v", projectID, projectDB.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = dbHandler.queries.DeleteDatabaseServiceByID(ctx, projectDB.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remove live containers and the project network before dropping rows.
|
// Remove live containers and the project network before dropping rows.
|
||||||
// Best-effort: Docker may be unavailable; DB cleanup proceeds regardless.
|
// Best-effort: Docker may be unavailable; DB cleanup proceeds regardless.
|
||||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||||
@@ -317,6 +341,7 @@ func handleDeleteProject(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "project", projectID.String(), "project.delete", nil)
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "Project deleted successfully"})
|
c.JSON(http.StatusOK, gin.H{"message": "Project deleted successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,180 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/secrets"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Project-level shared variables — referenced from service env values as
|
||||||
|
// ${{shared.KEY}} and resolved at runtime in resolveServiceEnv.
|
||||||
|
|
||||||
|
type ProjectVariable struct {
|
||||||
|
ID uuid.UUID `json:"id" db:"id"`
|
||||||
|
ProjectID uuid.UUID `json:"project_id" db:"project_id"`
|
||||||
|
Key string `json:"key" db:"key"`
|
||||||
|
Value string `json:"value" db:"value"`
|
||||||
|
IsSecret bool `json:"is_secret" db:"is_secret"`
|
||||||
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleGetProjectVariables(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
projectID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, allowed := projectManageAccess(c, db.(*database.DB), projectID); !allowed {
|
||||||
|
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := db.(*database.DB).Query(
|
||||||
|
`SELECT id, project_id, key, value, is_secret, created_at, updated_at
|
||||||
|
FROM project_variables WHERE project_id = $1 ORDER BY key ASC`,
|
||||||
|
projectID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve variables"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
variables := []ProjectVariable{}
|
||||||
|
for rows.Next() {
|
||||||
|
var v ProjectVariable
|
||||||
|
if err := rows.Scan(&v.ID, &v.ProjectID, &v.Key, &v.Value, &v.IsSecret, &v.CreatedAt, &v.UpdatedAt); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan variable"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if v.IsSecret {
|
||||||
|
v.Value = maskedSecretValue
|
||||||
|
}
|
||||||
|
variables = append(variables, v)
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"variables": variables})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUpdateProjectVariables(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
projectID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, allowed := projectManageAccess(c, db.(*database.DB), projectID); !allowed {
|
||||||
|
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req UpdateVariablesRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
storedSecrets := map[string]string{}
|
||||||
|
secretRows, err := db.(*database.DB).Query(
|
||||||
|
`SELECT key, value FROM project_variables WHERE project_id = $1 AND is_secret = TRUE`,
|
||||||
|
projectID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load existing variables"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for secretRows.Next() {
|
||||||
|
var k, v string
|
||||||
|
if err := secretRows.Scan(&k, &v); err == nil {
|
||||||
|
storedSecrets[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
secretRows.Close()
|
||||||
|
|
||||||
|
tx, err := db.(*database.DB).Begin()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to begin transaction"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
if _, err = tx.Exec(`DELETE FROM project_variables WHERE project_id = $1`, projectID); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clear existing variables"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
for _, v := range req.Variables {
|
||||||
|
value := v.Value
|
||||||
|
if v.IsSecret && value == maskedSecretValue {
|
||||||
|
if stored, ok := storedSecrets[v.Key]; ok {
|
||||||
|
value = stored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if v.IsSecret {
|
||||||
|
value = secrets.Encrypt(value)
|
||||||
|
}
|
||||||
|
if _, err = tx.Exec(
|
||||||
|
`INSERT INTO project_variables (id, project_id, key, value, is_secret, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
||||||
|
uuid.New(), projectID, v.Key, value, v.IsSecret, now, now,
|
||||||
|
); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to insert variable: " + v.Key})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err = tx.Commit(); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to commit transaction"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := db.(*database.DB).Query(
|
||||||
|
`SELECT id, project_id, key, value, is_secret, created_at, updated_at
|
||||||
|
FROM project_variables WHERE project_id = $1 ORDER BY key ASC`,
|
||||||
|
projectID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve variables"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
variables := []ProjectVariable{}
|
||||||
|
for rows.Next() {
|
||||||
|
var v ProjectVariable
|
||||||
|
if err := rows.Scan(&v.ID, &v.ProjectID, &v.Key, &v.Value, &v.IsSecret, &v.CreatedAt, &v.UpdatedAt); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v.IsSecret {
|
||||||
|
v.Value = maskedSecretValue
|
||||||
|
}
|
||||||
|
variables = append(variables, v)
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"variables": variables, "message": "Project variables updated successfully"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolveSharedVar returns the decrypted value of a project variable.
|
||||||
|
func resolveSharedVar(db *database.DB, projectID uuid.UUID, key string) (string, bool) {
|
||||||
|
var value string
|
||||||
|
err := db.QueryRow(
|
||||||
|
`SELECT value FROM project_variables WHERE project_id = $1 AND key = $2`,
|
||||||
|
projectID, key,
|
||||||
|
).Scan(&value)
|
||||||
|
if err != nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return secrets.Decrypt(value), true
|
||||||
|
}
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/secrets"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/registry"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Registry is a stored credential for pulling private images. Password is
|
||||||
|
// encrypted at rest and never returned — `has_password` signals presence.
|
||||||
|
type Registry struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Host string `json:"host"` // e.g. ghcr.io, registry.example.com; docker.io for Docker Hub
|
||||||
|
Username string `json:"username"`
|
||||||
|
HasPassword bool `json:"has_password"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type registryRequest struct {
|
||||||
|
Name string `json:"name" binding:"required,min=1,max=255"`
|
||||||
|
Host string `json:"host" binding:"required,min=1,max=255"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleListRegistries(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := c.Get("user_id")
|
||||||
|
rows, err := db.(*database.DB).Query(
|
||||||
|
`SELECT id, name, host, username, password <> '', created_at, updated_at
|
||||||
|
FROM registries WHERE owner_id = $1 ORDER BY created_at DESC`,
|
||||||
|
userID.(string),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list registries"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
registries := []Registry{}
|
||||||
|
for rows.Next() {
|
||||||
|
var r Registry
|
||||||
|
if err := rows.Scan(&r.ID, &r.Name, &r.Host, &r.Username, &r.HasPassword, &r.CreatedAt, &r.UpdatedAt); err == nil {
|
||||||
|
registries = append(registries, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"registries": registries})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCreateRegistry(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := c.Get("user_id")
|
||||||
|
|
||||||
|
var req registryRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Host = strings.ToLower(strings.TrimSpace(req.Host))
|
||||||
|
req.Host = strings.TrimPrefix(strings.TrimPrefix(req.Host, "https://"), "http://")
|
||||||
|
req.Host = strings.TrimSuffix(req.Host, "/")
|
||||||
|
|
||||||
|
// Encrypt only a real credential — encrypting "" would still satisfy
|
||||||
|
// `password <> ''` and report has_password for anonymous registries.
|
||||||
|
password := ""
|
||||||
|
if req.Password != "" {
|
||||||
|
password = secrets.Encrypt(req.Password)
|
||||||
|
}
|
||||||
|
var r Registry
|
||||||
|
err := db.(*database.DB).QueryRow(
|
||||||
|
`INSERT INTO registries (owner_id, name, host, username, password)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
RETURNING id, name, host, username, password <> '', created_at, updated_at`,
|
||||||
|
userID.(string), req.Name, req.Host, req.Username, password,
|
||||||
|
).Scan(&r.ID, &r.Name, &r.Host, &r.Username, &r.HasPassword, &r.CreatedAt, &r.UpdatedAt)
|
||||||
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "unique") {
|
||||||
|
c.JSON(http.StatusConflict, gin.H{"error": "A registry for this host already exists", "code": "CONFLICT"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create registry"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, gin.H{"registry": r})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUpdateRegistry(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := c.Get("user_id")
|
||||||
|
registryID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid registry ID", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req registryRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Host = strings.ToLower(strings.TrimSpace(req.Host))
|
||||||
|
req.Host = strings.TrimPrefix(strings.TrimPrefix(req.Host, "https://"), "http://")
|
||||||
|
req.Host = strings.TrimSuffix(req.Host, "/")
|
||||||
|
|
||||||
|
// Empty password keeps the stored credential — there is no "clear"
|
||||||
|
// operation; delete + recreate to remove a password.
|
||||||
|
var passwordArg interface{}
|
||||||
|
if req.Password != "" {
|
||||||
|
passwordArg = secrets.Encrypt(req.Password)
|
||||||
|
}
|
||||||
|
|
||||||
|
var r Registry
|
||||||
|
err = db.(*database.DB).QueryRow(
|
||||||
|
`UPDATE registries SET name = $1, host = $2, username = $3,
|
||||||
|
password = COALESCE($4, password), updated_at = now()
|
||||||
|
WHERE id = $5 AND owner_id = $6
|
||||||
|
RETURNING id, name, host, username, password <> '', created_at, updated_at`,
|
||||||
|
req.Name, req.Host, req.Username, passwordArg, registryID, userID.(string),
|
||||||
|
).Scan(&r.ID, &r.Name, &r.Host, &r.Username, &r.HasPassword, &r.CreatedAt, &r.UpdatedAt)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Registry not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"registry": r})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleDeleteRegistry(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := c.Get("user_id")
|
||||||
|
registryID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid registry ID", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := db.(*database.DB).Exec(
|
||||||
|
`DELETE FROM registries WHERE id = $1 AND owner_id = $2`,
|
||||||
|
registryID, userID.(string),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete registry"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if n, _ := res.RowsAffected(); n == 0 {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Registry not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Registry deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// registryAuthFor resolves pull credentials for an image reference: the
|
||||||
|
// caller's registry whose host matches, else anonymous. docker.io covers
|
||||||
|
// bare image names (nginx:latest).
|
||||||
|
func registryAuthFor(db *database.DB, ownerID, imageRef string) registry.AuthConfig {
|
||||||
|
host := registryHost(imageRef)
|
||||||
|
var username, password string
|
||||||
|
err := db.QueryRow(
|
||||||
|
`SELECT username, password FROM registries WHERE owner_id = $1 AND host = $2`,
|
||||||
|
ownerID, host,
|
||||||
|
).Scan(&username, &password)
|
||||||
|
if err != nil {
|
||||||
|
return registry.AuthConfig{}
|
||||||
|
}
|
||||||
|
return registry.AuthConfig{
|
||||||
|
Username: username,
|
||||||
|
Password: secrets.Decrypt(password),
|
||||||
|
ServerAddress: host,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestParseCPUReserveShares(t *testing.T) {
|
||||||
|
cases := map[string]int64{
|
||||||
|
"": 0,
|
||||||
|
"0": 0,
|
||||||
|
"-1": 0,
|
||||||
|
"abc": 0,
|
||||||
|
"0.25": 256,
|
||||||
|
"1": 1024,
|
||||||
|
"2.5": 2560,
|
||||||
|
" 0.5 ": 512,
|
||||||
|
}
|
||||||
|
for in, want := range cases {
|
||||||
|
if got := parseCPUReserveShares(in); got != want {
|
||||||
|
t.Errorf("parseCPUReserveShares(%q) = %d, want %d", in, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateResourceSpecs(t *testing.T) {
|
||||||
|
if err := validateCPUSpec(""); err != nil {
|
||||||
|
t.Error("empty cpu should pass")
|
||||||
|
}
|
||||||
|
if err := validateCPUSpec("0.5"); err != nil {
|
||||||
|
t.Error("0.5 should be valid cpu")
|
||||||
|
}
|
||||||
|
if err := validateCPUSpec("512mb"); err == nil {
|
||||||
|
t.Error("512mb should fail cpu validation")
|
||||||
|
}
|
||||||
|
if err := validateMemorySpec(""); err != nil {
|
||||||
|
t.Error("empty memory should pass")
|
||||||
|
}
|
||||||
|
if err := validateMemorySpec("512Mi"); err != nil {
|
||||||
|
t.Error("512Mi should be valid memory")
|
||||||
|
}
|
||||||
|
if err := validateMemorySpec("1g"); err != nil {
|
||||||
|
t.Error("1g should be valid memory")
|
||||||
|
}
|
||||||
|
if err := validateMemorySpec("0.5"); err == nil {
|
||||||
|
t.Error("0.5 should fail memory validation (parses to <1 byte)")
|
||||||
|
}
|
||||||
|
if err := validateMemorySpec("lots"); err == nil {
|
||||||
|
t.Error("'lots' should fail memory validation")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,6 +23,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg *config.Config) {
|
func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg *config.Config) {
|
||||||
|
// Audit + outbound webhook delivery need a package-level handle for
|
||||||
|
// call sites that don't carry a gin context.
|
||||||
|
SetAuditDB(db)
|
||||||
|
StartWebhookDispatcher()
|
||||||
|
|
||||||
// Expose Better Auth through backend so frontend can use a single backend origin.
|
// Expose Better Auth through backend so frontend can use a single backend origin.
|
||||||
setupAuthProxyRoutes(router, cfg, db)
|
setupAuthProxyRoutes(router, cfg, db)
|
||||||
|
|
||||||
@@ -98,6 +103,19 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
// Start the cron scheduler — executes due cron_jobs via docker exec.
|
// Start the cron scheduler — executes due cron_jobs via docker exec.
|
||||||
StartCronScheduler(context.Background(), db, dockerClient)
|
StartCronScheduler(context.Background(), db, dockerClient)
|
||||||
|
|
||||||
|
// Sleep sweeper — scales idle services to zero and prunes wake containers.
|
||||||
|
StartSleepSweeper(context.Background(), db, dockerClient, deploymentEngine, cfg.Port)
|
||||||
|
|
||||||
|
// Audit retention — prunes rows past AUDIT_RETENTION_DAYS (default 90).
|
||||||
|
StartAuditRetention(context.Background(), db)
|
||||||
|
|
||||||
|
// Agent health — marks stale heartbeats offline, notifies admins on
|
||||||
|
// offline/recovery transitions.
|
||||||
|
StartAgentHealthSweep(context.Background(), db)
|
||||||
|
|
||||||
|
// Daily upgrade check — notifies admins when a newer release exists.
|
||||||
|
StartUpgradeCheck(context.Background(), db, Version)
|
||||||
|
|
||||||
// Initialize database handler
|
// Initialize database handler
|
||||||
databaseHandler := NewDatabaseHandler(db.DB, dockerClient)
|
databaseHandler := NewDatabaseHandler(db.DB, dockerClient)
|
||||||
|
|
||||||
@@ -158,6 +176,7 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
c.Set("deployment_engine", deploymentEngine)
|
c.Set("deployment_engine", deploymentEngine)
|
||||||
}
|
}
|
||||||
c.Set("deploy_queue", deployQueue)
|
c.Set("deploy_queue", deployQueue)
|
||||||
|
c.Set("api_port", cfg.Port)
|
||||||
c.Set("scheduler", scheduler)
|
c.Set("scheduler", scheduler)
|
||||||
c.Set("metrics_collector", metricsCollector)
|
c.Set("metrics_collector", metricsCollector)
|
||||||
c.Set("auto_scaler", autoScaler)
|
c.Set("auto_scaler", autoScaler)
|
||||||
@@ -240,6 +259,10 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
// replaces session auth. Providers POST to /api/git/webhooks/:id.
|
// replaces session auth. Providers POST to /api/git/webhooks/:id.
|
||||||
router.POST("/api/git/webhooks/:id", handleGitWebhookPush)
|
router.POST("/api/git/webhooks/:id", handleGitWebhookPush)
|
||||||
|
|
||||||
|
// GitHub App-level webhook: installed repos deliver to this single
|
||||||
|
// endpoint, verified against the provisioned webhook secret.
|
||||||
|
router.POST("/api/git/github-app/webhook", handleGitHubAppWebhook)
|
||||||
|
|
||||||
v1 := router.Group("/api/v1")
|
v1 := router.Group("/api/v1")
|
||||||
{
|
{
|
||||||
// Public routes (no authentication required)
|
// Public routes (no authentication required)
|
||||||
@@ -249,6 +272,16 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
public.POST("/auth/login", handleLogin)
|
public.POST("/auth/login", handleLogin)
|
||||||
public.POST("/auth/register", handleRegister)
|
public.POST("/auth/register", handleRegister)
|
||||||
public.GET("/maintenance", handleMaintenancePage)
|
public.GET("/maintenance", handleMaintenancePage)
|
||||||
|
public.GET("/branding", handleGetBranding)
|
||||||
|
|
||||||
|
// Invite accept flow — validates the link, then registers
|
||||||
|
// even while public signup is closed.
|
||||||
|
public.GET("/auth/invites/:token", handleGetInvite)
|
||||||
|
public.POST("/auth/accept-invite", handleAcceptInvite)
|
||||||
|
|
||||||
|
// Wake callback for sleep-mode placeholders. Unauthenticated but
|
||||||
|
// non-destructive: it only reconciles the service back to running.
|
||||||
|
public.Any("/internal/wake/:id", handleServiceWake)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read routes — public browsing. OptionalAuth resolves a session when
|
// Read routes — public browsing. OptionalAuth resolves a session when
|
||||||
@@ -271,6 +304,7 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
read.GET("/services/:id/deployments", handleGetDeployments)
|
read.GET("/services/:id/deployments", handleGetDeployments)
|
||||||
read.GET("/services/:id/domains", handleListServiceDomains)
|
read.GET("/services/:id/domains", handleListServiceDomains)
|
||||||
read.GET("/services/:id/domains/check", handleCheckServiceDomains)
|
read.GET("/services/:id/domains/check", handleCheckServiceDomains)
|
||||||
|
read.GET("/services/:id/env-check", handleServiceEnvCheck)
|
||||||
read.GET("/deployments", handleGetRecentDeployments)
|
read.GET("/deployments", handleGetRecentDeployments)
|
||||||
read.GET("/deployments/:id", handleGetDeployment)
|
read.GET("/deployments/:id", handleGetDeployment)
|
||||||
|
|
||||||
@@ -319,11 +353,28 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
// WebSocket endpoint
|
// WebSocket endpoint
|
||||||
authed.GET("/ws", handleWebSocket)
|
authed.GET("/ws", handleWebSocket)
|
||||||
|
|
||||||
|
// Cross-cutting ops view — active/queued/failed work
|
||||||
|
authed.GET("/operations", handleGetOperations)
|
||||||
|
authed.GET("/activity", handleGetActivity)
|
||||||
|
authed.GET("/projects/:id/activity", handleGetProjectActivity)
|
||||||
|
|
||||||
|
// Instance announcements shown to all signed-in users
|
||||||
|
authed.GET("/banners/active", handleListActiveBanners)
|
||||||
|
|
||||||
|
// Outbound webhooks — signed event delivery to user HTTP endpoints
|
||||||
|
authed.GET("/webhooks", handleListOutboundWebhooks)
|
||||||
|
authed.POST("/webhooks", handleCreateOutboundWebhook)
|
||||||
|
authed.PATCH("/webhooks/:id", handleUpdateOutboundWebhook)
|
||||||
|
authed.DELETE("/webhooks/:id", handleDeleteOutboundWebhook)
|
||||||
|
authed.GET("/webhooks/:id/deliveries", handleListOutboundWebhookDeliveries)
|
||||||
|
authed.POST("/webhooks/:id/test", handleTestOutboundWebhook)
|
||||||
|
|
||||||
// Owned-resource mutations — handlers scope to the caller and
|
// Owned-resource mutations — handlers scope to the caller and
|
||||||
// admit admins. New projects land unapproved until an admin
|
// admit admins. New projects land unapproved until an admin
|
||||||
// publishes them.
|
// publishes them.
|
||||||
authed.POST("/projects", handleCreateProject)
|
authed.POST("/projects", handleCreateProject)
|
||||||
authed.POST("/projects/:id/services", handleCreateService)
|
authed.POST("/projects/:id/services", handleCreateService)
|
||||||
|
authed.POST("/projects/:id/import-compose", handleImportProjectCompose)
|
||||||
authed.PUT("/projects/:id", handleUpdateProject)
|
authed.PUT("/projects/:id", handleUpdateProject)
|
||||||
authed.DELETE("/projects/:id", handleDeleteProject)
|
authed.DELETE("/projects/:id", handleDeleteProject)
|
||||||
|
|
||||||
@@ -335,15 +386,26 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
authed.POST("/services/:id/start", handleServiceStart)
|
authed.POST("/services/:id/start", handleServiceStart)
|
||||||
authed.POST("/services/:id/stop", handleServiceStop)
|
authed.POST("/services/:id/stop", handleServiceStop)
|
||||||
authed.POST("/services/:id/restart", handleServiceRestart)
|
authed.POST("/services/:id/restart", handleServiceRestart)
|
||||||
|
authed.POST("/services/:id/sleep", handleServiceSleep)
|
||||||
|
authed.POST("/services/:id/wake", handleServiceWakeNow)
|
||||||
authed.POST("/services/:id/redeploy", handleServiceRedeploy)
|
authed.POST("/services/:id/redeploy", handleServiceRedeploy)
|
||||||
|
authed.POST("/services/:id/clone", handleCloneService)
|
||||||
|
authed.POST("/services/:id/move", handleMoveService)
|
||||||
authed.POST("/services/:id/deployments", handleCreateDeployment)
|
authed.POST("/services/:id/deployments", handleCreateDeployment)
|
||||||
authed.POST("/deployments/:id/rollback", handleRollbackDeployment)
|
authed.POST("/deployments/:id/rollback", handleRollbackDeployment)
|
||||||
authed.POST("/deployments/:id/cancel", handleCancelDeployment)
|
authed.POST("/deployments/:id/cancel", handleCancelDeployment)
|
||||||
|
|
||||||
|
// Interactive terminal — WebSocket → docker exec pty.
|
||||||
|
authed.GET("/services/:id/terminal", handleServiceTerminal)
|
||||||
|
|
||||||
// Environment variables — secrets, owner/admin only.
|
// Environment variables — secrets, owner/admin only.
|
||||||
authed.GET("/services/:id/variables", handleGetVariables)
|
authed.GET("/services/:id/variables", handleGetVariables)
|
||||||
authed.PUT("/services/:id/variables", handleUpdateVariables)
|
authed.PUT("/services/:id/variables", handleUpdateVariables)
|
||||||
|
|
||||||
|
// Project-level shared variables — ${{shared.KEY}} refs.
|
||||||
|
authed.GET("/projects/:id/variables", handleGetProjectVariables)
|
||||||
|
authed.PUT("/projects/:id/variables", handleUpdateProjectVariables)
|
||||||
|
|
||||||
// Runtime logs — container stdout routinely echoes secrets,
|
// Runtime logs — container stdout routinely echoes secrets,
|
||||||
// so these stay behind authentication like build logs.
|
// so these stay behind authentication like build logs.
|
||||||
authed.GET("/services/:id/logs", handleGetLogs)
|
authed.GET("/services/:id/logs", handleGetLogs)
|
||||||
@@ -352,6 +414,19 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
// One-off exec console (docker exec, 30s ceiling)
|
// One-off exec console (docker exec, 30s ceiling)
|
||||||
authed.POST("/services/:id/exec", handleExecInService)
|
authed.POST("/services/:id/exec", handleExecInService)
|
||||||
|
|
||||||
|
// S3-compatible backup targets — owner-scoped, keys encrypted at rest.
|
||||||
|
authed.GET("/backup-targets", databaseHandler.ListBackupTargets)
|
||||||
|
authed.POST("/backup-targets", databaseHandler.CreateBackupTarget)
|
||||||
|
authed.PUT("/backup-targets/:id", databaseHandler.UpdateBackupTarget)
|
||||||
|
authed.DELETE("/backup-targets/:id", databaseHandler.DeleteBackupTarget)
|
||||||
|
authed.POST("/backup-targets/:id/test", databaseHandler.TestBackupTarget)
|
||||||
|
|
||||||
|
// Private-image registries — owner-scoped, credentials encrypted at rest.
|
||||||
|
authed.GET("/registries", handleListRegistries)
|
||||||
|
authed.POST("/registries", handleCreateRegistry)
|
||||||
|
authed.PUT("/registries/:id", handleUpdateRegistry)
|
||||||
|
authed.DELETE("/registries/:id", handleDeleteRegistry)
|
||||||
|
|
||||||
// Git integration — providers carry per-user credentials.
|
// Git integration — providers carry per-user credentials.
|
||||||
authed.GET("/git/github-app/install-url", handleGetGitHubAppInstallURL)
|
authed.GET("/git/github-app/install-url", handleGetGitHubAppInstallURL)
|
||||||
authed.POST("/git/github-app/connect", handleConnectGitHubApp)
|
authed.POST("/git/github-app/connect", handleConnectGitHubApp)
|
||||||
@@ -375,11 +450,15 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
authed.POST("/builds/plan", buildHandler.GetBuildPlan)
|
authed.POST("/builds/plan", buildHandler.GetBuildPlan)
|
||||||
|
|
||||||
authed.POST("/databases", databaseHandler.CreateDatabase)
|
authed.POST("/databases", databaseHandler.CreateDatabase)
|
||||||
|
authed.POST("/databases/register-external", databaseHandler.RegisterExternalDatabase)
|
||||||
|
authed.POST("/databases/test-connection", databaseHandler.TestDatabaseConnection)
|
||||||
|
authed.POST("/databases/:id/test-connection", databaseHandler.TestDatabaseConnectionByID)
|
||||||
authed.PUT("/databases/:id", databaseHandler.UpdateDatabase)
|
authed.PUT("/databases/:id", databaseHandler.UpdateDatabase)
|
||||||
authed.DELETE("/databases/:id", databaseHandler.DeleteDatabase)
|
authed.DELETE("/databases/:id", databaseHandler.DeleteDatabase)
|
||||||
authed.POST("/databases/:id/action", databaseHandler.PerformDatabaseAction)
|
authed.POST("/databases/:id/action", databaseHandler.PerformDatabaseAction)
|
||||||
authed.POST("/databases/:id/backup", databaseHandler.CreateBackup)
|
authed.POST("/databases/:id/backup", databaseHandler.CreateBackup)
|
||||||
authed.POST("/databases/:id/restore", databaseHandler.RestoreBackup)
|
authed.POST("/databases/:id/restore", databaseHandler.RestoreBackup)
|
||||||
|
authed.POST("/databases/:id/backups/import", databaseHandler.ImportBackup)
|
||||||
authed.GET("/databases/:id/backups/:bid/download", databaseHandler.DownloadBackup)
|
authed.GET("/databases/:id/backups/:bid/download", databaseHandler.DownloadBackup)
|
||||||
|
|
||||||
authed.POST("/projects/:id/preview-environments", handleCreatePreviewEnvironment)
|
authed.POST("/projects/:id/preview-environments", handleCreatePreviewEnvironment)
|
||||||
@@ -398,6 +477,9 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
authed.PUT("/templates/:id", handleUpdateTemplate)
|
authed.PUT("/templates/:id", handleUpdateTemplate)
|
||||||
authed.DELETE("/templates/:id", handleDeleteTemplate)
|
authed.DELETE("/templates/:id", handleDeleteTemplate)
|
||||||
authed.POST("/templates/:id/deploy", handleCreateFromTemplate)
|
authed.POST("/templates/:id/deploy", handleCreateFromTemplate)
|
||||||
|
authed.POST("/templates/:id/plan", handlePlanTemplate)
|
||||||
|
authed.POST("/templates/import/compose", handleImportCompose)
|
||||||
|
authed.POST("/templates/deploy", handleDeployInlineGraph)
|
||||||
|
|
||||||
authed.POST("/cron-jobs", handleCreateCronJob)
|
authed.POST("/cron-jobs", handleCreateCronJob)
|
||||||
authed.PUT("/cron-jobs/:id", handleUpdateCronJob)
|
authed.PUT("/cron-jobs/:id", handleUpdateCronJob)
|
||||||
@@ -414,6 +496,7 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
admin.GET("/admin/overview", handleAdminOverview)
|
admin.GET("/admin/overview", handleAdminOverview)
|
||||||
admin.GET("/admin/users", handleAdminListUsers)
|
admin.GET("/admin/users", handleAdminListUsers)
|
||||||
admin.PATCH("/admin/users/:id", handleAdminSetUserAdmin)
|
admin.PATCH("/admin/users/:id", handleAdminSetUserAdmin)
|
||||||
|
admin.POST("/admin/users/:id/impersonate", handleAdminImpersonateUser)
|
||||||
admin.PATCH("/admin/projects/:id", handleAdminSetProjectApproval)
|
admin.PATCH("/admin/projects/:id", handleAdminSetProjectApproval)
|
||||||
|
|
||||||
admin.POST("/users", handleCreateUser)
|
admin.POST("/users", handleCreateUser)
|
||||||
@@ -431,6 +514,22 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
|||||||
|
|
||||||
admin.GET("/admin/volumes", handleAdminListVolumes)
|
admin.GET("/admin/volumes", handleAdminListVolumes)
|
||||||
admin.DELETE("/admin/volumes/:name", handleAdminDeleteVolume)
|
admin.DELETE("/admin/volumes/:name", handleAdminDeleteVolume)
|
||||||
|
|
||||||
|
// Announcement banners
|
||||||
|
admin.GET("/admin/banners", handleAdminListBanners)
|
||||||
|
admin.POST("/admin/banners", handleAdminCreateBanner)
|
||||||
|
admin.PATCH("/admin/banners/:id", handleAdminUpdateBanner)
|
||||||
|
admin.DELETE("/admin/banners/:id", handleAdminDeleteBanner)
|
||||||
|
|
||||||
|
// GitHub App self-provisioning (manifest flow)
|
||||||
|
admin.GET("/admin/git/github-app", handleAdminGetGitHubApp)
|
||||||
|
admin.POST("/admin/git/github-app/manifest", handleAdminGitHubAppManifest)
|
||||||
|
admin.POST("/admin/git/github-app/convert", handleAdminGitHubAppConvert)
|
||||||
|
|
||||||
|
// Team invites — single-use registration links
|
||||||
|
admin.GET("/admin/invites", handleAdminListInvites)
|
||||||
|
admin.POST("/admin/invites", handleAdminCreateInvite)
|
||||||
|
admin.DELETE("/admin/invites/:id", handleAdminDeleteInvite)
|
||||||
admin.GET("/audit-logs", handleGetAuditLogs)
|
admin.GET("/audit-logs", handleGetAuditLogs)
|
||||||
admin.GET("/audit-logs/:resource/:id", handleGetResourceAuditLogs)
|
admin.GET("/audit-logs/:resource/:id", handleGetResourceAuditLogs)
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,13 @@ func serviceRuntimeSpec(db *database.DB, service Service) (deployment.RuntimeSpe
|
|||||||
}
|
}
|
||||||
spec.NanoCPUs = parseCPULimit(service.CPU)
|
spec.NanoCPUs = parseCPULimit(service.CPU)
|
||||||
spec.MemoryBytes = parseMemoryLimit(service.Memory)
|
spec.MemoryBytes = parseMemoryLimit(service.Memory)
|
||||||
|
spec.MemoryReserve = parseMemoryLimit(service.MemoryReserve)
|
||||||
|
spec.CPUShares = parseCPUReserveShares(service.CPUReserve)
|
||||||
|
if service.Builder == "static" && spec.Port == 0 {
|
||||||
|
// The static builder's image is nginx; expose its default port so
|
||||||
|
// Traefik has something to route to without extra config.
|
||||||
|
spec.Port = 80
|
||||||
|
}
|
||||||
spec.Volumes = loadServiceVolumes(db, service.ID)
|
spec.Volumes = loadServiceVolumes(db, service.ID)
|
||||||
spec.Domains = serviceDomainNames(db, service.ID, service.Domain)
|
spec.Domains = serviceDomainNames(db, service.ID, service.Domain)
|
||||||
spec.Maintenance, spec.BasicAuthUsers = serviceAccess(db, service.ID)
|
spec.Maintenance, spec.BasicAuthUsers = serviceAccess(db, service.ID)
|
||||||
@@ -112,6 +119,9 @@ func resolveServiceEnv(db *database.DB, service Service) (map[string]string, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
resolve := func(serviceName, key string) (string, bool) {
|
resolve := func(serviceName, key string) (string, bool) {
|
||||||
|
if strings.EqualFold(serviceName, "shared") {
|
||||||
|
return resolveSharedVar(db, service.ProjectID, key)
|
||||||
|
}
|
||||||
id, ok := byName[strings.ToLower(serviceName)]
|
id, ok := byName[strings.ToLower(serviceName)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", false
|
return "", false
|
||||||
@@ -160,6 +170,32 @@ func parseCPULimit(cpu string) int64 {
|
|||||||
return int64(v * 1e9)
|
return int64(v * 1e9)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseCPUReserveShares maps a cpu fraction to Docker's relative share
|
||||||
|
// weight: 1024 shares ≈ one CPU.
|
||||||
|
func parseCPUReserveShares(cpu string) int64 {
|
||||||
|
v, err := strconv.ParseFloat(strings.TrimSpace(cpu), 64)
|
||||||
|
if err != nil || v <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return int64(v * 1024)
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateCPUSpec and validateMemorySpec reject values that silently parse
|
||||||
|
// to zero — a typo like "512mb" must fail loudly instead of dropping the limit.
|
||||||
|
func validateCPUSpec(v string) error {
|
||||||
|
if s := strings.TrimSpace(v); s != "" && parseCPULimit(s) <= 0 {
|
||||||
|
return fmt.Errorf("must be a positive cpu fraction (e.g. 0.5, 2)")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateMemorySpec(v string) error {
|
||||||
|
if s := strings.TrimSpace(v); s != "" && parseMemoryLimit(s) <= 0 {
|
||||||
|
return fmt.Errorf("must be bytes with unit (e.g. 256Mi, 1g)")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var memUnits = map[string]int64{"k": 1e3, "m": 1e6, "g": 1e9, "ki": 1 << 10, "mi": 1 << 20, "gi": 1 << 30}
|
var memUnits = map[string]int64{"k": 1e3, "m": 1e6, "g": 1e9, "ki": 1 << 10, "mi": 1 << 20, "gi": 1 << 30}
|
||||||
|
|
||||||
func parseMemoryLimit(mem string) int64 {
|
func parseMemoryLimit(mem string) int64 {
|
||||||
@@ -215,6 +251,9 @@ func loadOwnedService(c *gin.Context, db *database.DB) (Service, bool) {
|
|||||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
s.created_at, s.updated_at, p.owner_id
|
s.created_at, s.updated_at, p.owner_id
|
||||||
FROM services s JOIN projects p ON s.project_id = p.id
|
FROM services s JOIN projects p ON s.project_id = p.id
|
||||||
WHERE s.id = $1`,
|
WHERE s.id = $1`,
|
||||||
@@ -224,7 +263,9 @@ func loadOwnedService(c *gin.Context, db *database.DB) (Service, bool) {
|
|||||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt, &owner,
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt, &owner,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
||||||
@@ -258,6 +299,9 @@ func loadReadableService(c *gin.Context, db *database.DB) (Service, bool) {
|
|||||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
s.created_at, s.updated_at, s.project_id
|
s.created_at, s.updated_at, s.project_id
|
||||||
FROM services s
|
FROM services s
|
||||||
WHERE s.id = $1`,
|
WHERE s.id = $1`,
|
||||||
@@ -267,7 +311,9 @@ func loadReadableService(c *gin.Context, db *database.DB) (Service, bool) {
|
|||||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt, &projectID,
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt, &projectID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
||||||
@@ -549,6 +595,9 @@ func runtimeScaleService(c *gin.Context, serviceID string, replicas int) error {
|
|||||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
s.created_at, s.updated_at, p.owner_id
|
s.created_at, s.updated_at, p.owner_id
|
||||||
FROM services s JOIN projects p ON s.project_id = p.id
|
FROM services s JOIN projects p ON s.project_id = p.id
|
||||||
WHERE s.id = $1`, serviceID,
|
WHERE s.id = $1`, serviceID,
|
||||||
@@ -557,7 +606,9 @@ func runtimeScaleService(c *gin.Context, serviceID string, replicas int) error {
|
|||||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt, &owner,
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt, &owner,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -590,3 +641,112 @@ func runtimeScaleService(c *gin.Context, serviceID string, replicas int) error {
|
|||||||
_, _ = db.Exec(`UPDATE services SET replicas = $1, updated_at = $2 WHERE id = $3`, replicas, time.Now(), service.ID)
|
_, _ = db.Exec(`UPDATE services SET replicas = $1, updated_at = $2 WHERE id = $3`, replicas, time.Now(), service.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleServiceEnvCheck reports environment problems without exposing
|
||||||
|
// values: empty vars, ${{service.KEY}} refs that fail to resolve, and
|
||||||
|
// secrets whose ciphertext can't be decrypted (key rotation).
|
||||||
|
func handleServiceEnvCheck(c *gin.Context) {
|
||||||
|
dbVal, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := dbVal.(*database.DB)
|
||||||
|
service, ok := loadReadableService(c, db)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := db.Query(
|
||||||
|
`SELECT key, value, COALESCE(is_secret, false) FROM environment_variables WHERE service_id = $1`,
|
||||||
|
service.ID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var unresolved, empty, unreadable []string
|
||||||
|
var raw []struct{ key, val string }
|
||||||
|
for rows.Next() {
|
||||||
|
var k, v string
|
||||||
|
var secret bool
|
||||||
|
if err := rows.Scan(&k, &v, &secret); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
decrypted := secrets.Decrypt(v)
|
||||||
|
if secret && secrets.IsEncrypted(v) && decrypted == v {
|
||||||
|
// Ciphertext failed to decrypt — Decrypt returns the input.
|
||||||
|
unreadable = append(unreadable, k)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(decrypted) == "" {
|
||||||
|
empty = append(empty, k)
|
||||||
|
}
|
||||||
|
raw = append(raw, struct{ key, val string }{k, decrypted})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Project siblings for ${{name.KEY}} resolution.
|
||||||
|
siblingIDs := map[string]string{}
|
||||||
|
svcRows, err := db.Query(`SELECT id, name FROM services WHERE project_id = $1`, service.ProjectID)
|
||||||
|
if err == nil {
|
||||||
|
for svcRows.Next() {
|
||||||
|
var id, name string
|
||||||
|
if err := svcRows.Scan(&id, &name); err == nil {
|
||||||
|
siblingIDs[strings.ToLower(name)] = id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
svcRows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
resolvable := func(serviceName, key string) bool {
|
||||||
|
id, ok := siblingIDs[strings.ToLower(serviceName)]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if strings.EqualFold(key, "HOST") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if strings.EqualFold(key, "PORT") {
|
||||||
|
var port int
|
||||||
|
if err := db.QueryRow(`SELECT COALESCE(port, 0) FROM services WHERE id = $1`, id).Scan(&port); err == nil {
|
||||||
|
return port > 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var n int
|
||||||
|
if err := db.QueryRow(
|
||||||
|
`SELECT COUNT(*) FROM environment_variables WHERE service_id = $1 AND key = $2`,
|
||||||
|
id, key,
|
||||||
|
).Scan(&n); err == nil {
|
||||||
|
return n > 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, kv := range raw {
|
||||||
|
for _, match := range refVarPattern.FindAllStringSubmatch(kv.val, -1) {
|
||||||
|
if len(match) == 3 && !resolvable(match[1], match[2]) {
|
||||||
|
unresolved = append(unresolved, fmt.Sprintf("%s → %s", kv.key, match[0]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if unresolved == nil {
|
||||||
|
unresolved = []string{}
|
||||||
|
}
|
||||||
|
if empty == nil {
|
||||||
|
empty = []string{}
|
||||||
|
}
|
||||||
|
if unreadable == nil {
|
||||||
|
unreadable = []string{}
|
||||||
|
}
|
||||||
|
okResult := len(unresolved) == 0 && len(empty) == 0 && len(unreadable) == 0
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"ok": okResult,
|
||||||
|
"unresolved": unresolved,
|
||||||
|
"empty": empty,
|
||||||
|
"unreadable": unreadable,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -29,9 +29,27 @@ type SecurityHandler struct {
|
|||||||
func NewSecurityHandler(db *database.DB, encryptionKey string) *SecurityHandler {
|
func NewSecurityHandler(db *database.DB, encryptionKey string) *SecurityHandler {
|
||||||
encryptionManager, _ := security.NewEncryptionManager(encryptionKey)
|
encryptionManager, _ := security.NewEncryptionManager(encryptionKey)
|
||||||
|
|
||||||
|
scanner := security.NewScanner(db)
|
||||||
|
// Notify the project owner when a background scan completes — criticals
|
||||||
|
// earn a distinct title so they stand out in the notification list.
|
||||||
|
scanner.OnScanComplete = func(scan *security.SecurityScan) {
|
||||||
|
var ownerID string
|
||||||
|
if err := db.QueryRow(`SELECT owner_id::text FROM projects WHERE id = $1`, scan.ProjectID).Scan(&ownerID); err != nil || ownerID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
title := "Security scan completed"
|
||||||
|
if scan.Summary.Critical > 0 || scan.Summary.High > 0 {
|
||||||
|
title = "Security scan found issues"
|
||||||
|
}
|
||||||
|
insertUserNotification(db, ownerID, "security", title,
|
||||||
|
fmt.Sprintf("%s scan finished: %d critical, %d high, %d total findings (score %d/100).",
|
||||||
|
scan.ScanType, scan.Summary.Critical, scan.Summary.High, scan.Summary.Total, scan.Summary.Score),
|
||||||
|
"project", scan.ProjectID)
|
||||||
|
}
|
||||||
|
|
||||||
return &SecurityHandler{
|
return &SecurityHandler{
|
||||||
db: db,
|
db: db,
|
||||||
scanner: security.NewScanner(db),
|
scanner: scanner,
|
||||||
complianceManager: security.NewComplianceManager(db),
|
complianceManager: security.NewComplianceManager(db),
|
||||||
encryptionManager: encryptionManager,
|
encryptionManager: encryptionManager,
|
||||||
dataRetentionManager: security.NewDataRetentionManager(encryptionManager),
|
dataRetentionManager: security.NewDataRetentionManager(encryptionManager),
|
||||||
|
|||||||
@@ -0,0 +1,226 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CloneServiceRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ProjectID uuid.UUID `json:"project_id"` // empty = clone within the same project
|
||||||
|
Environment string `json:"environment" binding:"omitempty,oneof=production preview development"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleCloneService duplicates a service's full configuration — image/git
|
||||||
|
// source, volumes, domains, access gates, builder settings, and variables —
|
||||||
|
// into the same or another project. Containers are not cloned; the copy
|
||||||
|
// starts stopped until its first deploy.
|
||||||
|
func handleCloneService(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := c.Get("user_id")
|
||||||
|
|
||||||
|
var req CloneServiceRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
source, ok := loadOwnedService(c, db.(*database.DB))
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
targetProjectID := source.ProjectID
|
||||||
|
if req.ProjectID != uuid.Nil {
|
||||||
|
targetProjectID = req.ProjectID
|
||||||
|
}
|
||||||
|
if !ownsProject(db.(*database.DB), targetProjectID, userID.(string), contextIsAdmin(c)) {
|
||||||
|
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied to target project", "code": "FORBIDDEN"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
name := strings.TrimSpace(req.Name)
|
||||||
|
if name == "" {
|
||||||
|
name = source.Name + "-copy"
|
||||||
|
}
|
||||||
|
var count int
|
||||||
|
_ = db.(*database.DB).QueryRow(
|
||||||
|
`SELECT COUNT(*) FROM services WHERE project_id = $1 AND name = $2`,
|
||||||
|
targetProjectID, name,
|
||||||
|
).Scan(&count)
|
||||||
|
if count > 0 {
|
||||||
|
c.JSON(http.StatusConflict, gin.H{"error": "Service name already exists in target project", "code": "CONFLICT"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
environment := source.Environment
|
||||||
|
if req.Environment != "" {
|
||||||
|
environment = req.Environment
|
||||||
|
}
|
||||||
|
if environment == "" {
|
||||||
|
environment = "production"
|
||||||
|
}
|
||||||
|
environmentID, err := getProjectEnvironmentID(db.(*database.DB), targetProjectID, environment)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to resolve target environment"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
maintenance, basicAuth := serviceAccess(db.(*database.DB), source.ID)
|
||||||
|
newID := uuid.New()
|
||||||
|
now := time.Now()
|
||||||
|
// string() so pq sends text — []byte would go as bytea and fail ::jsonb.
|
||||||
|
volumesJSON := string(loadServiceVolumesJSON(db.(*database.DB), source.ID))
|
||||||
|
|
||||||
|
_, err = db.(*database.DB).Exec(
|
||||||
|
`INSERT INTO services
|
||||||
|
(id, project_id, name, environment_id, service_type, source_type, source_url, image_name,
|
||||||
|
build_command, start_command, type, status, image, command, environment,
|
||||||
|
git_repo, git_branch, build_path, cpu, memory, replicas, port, domain,
|
||||||
|
healthcheck_path, restart_policy, volumes, maintenance_mode, basic_auth_users,
|
||||||
|
builder, cpu_reserve, memory_reserve, static_build_cmd, static_dir,
|
||||||
|
created_at, updated_at)
|
||||||
|
SELECT $1, $2, $3, $4, service_type, source_type, source_url, image_name,
|
||||||
|
build_command, start_command, type, 'stopped', image, command, $5,
|
||||||
|
git_repo, git_branch, build_path, cpu, memory, replicas, port, domain,
|
||||||
|
healthcheck_path, restart_policy, $6::jsonb, $7, $8,
|
||||||
|
builder, cpu_reserve, memory_reserve, static_build_cmd, static_dir,
|
||||||
|
$9, $9
|
||||||
|
FROM services WHERE id = $10`,
|
||||||
|
newID, targetProjectID, name, environmentID, environment, volumesJSON,
|
||||||
|
maintenance, basicAuth, now, source.ID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clone service"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := db.(*database.DB).Exec(
|
||||||
|
`INSERT INTO service_domains (service_id, domain, is_default, cert_type)
|
||||||
|
SELECT $1, domain, is_default, cert_type FROM service_domains WHERE service_id = $2`,
|
||||||
|
newID, source.ID,
|
||||||
|
); err != nil {
|
||||||
|
_, _ = db.(*database.DB).Exec(`DELETE FROM services WHERE id = $1`, newID)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clone service domains"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Variable values are copied as stored — ciphertext stays ciphertext.
|
||||||
|
if _, err := db.(*database.DB).Exec(
|
||||||
|
`INSERT INTO environment_variables (id, service_id, key, value, is_secret)
|
||||||
|
SELECT gen_random_uuid(), $1, key, value, is_secret FROM environment_variables WHERE service_id = $2`,
|
||||||
|
newID, source.ID,
|
||||||
|
); err != nil {
|
||||||
|
_, _ = db.(*database.DB).Exec(`DELETE FROM services WHERE id = $1`, newID)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clone service variables"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "service", source.ID.String(), "service.clone", map[string]interface{}{"cloned_to": newID.String(), "name": name})
|
||||||
|
c.JSON(http.StatusCreated, gin.H{"service_id": newID, "name": name, "message": "Service cloned"})
|
||||||
|
}
|
||||||
|
|
||||||
|
type MoveServiceRequest struct {
|
||||||
|
ProjectID uuid.UUID `json:"project_id" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleMoveService re-parents a service to another project owned by the
|
||||||
|
// caller. Variables, domains, and volumes follow via the service id;
|
||||||
|
// running containers keep running (they join the new project's network on
|
||||||
|
// the next deploy).
|
||||||
|
func handleMoveService(c *gin.Context) {
|
||||||
|
db, exists := c.Get("db")
|
||||||
|
if !exists {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := c.Get("user_id")
|
||||||
|
serviceID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid service ID", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req MoveServiceRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
service, ok := loadOwnedService(c, db.(*database.DB))
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.ProjectID == service.ProjectID {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Service is already in that project", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !ownsProject(db.(*database.DB), req.ProjectID, userID.(string), contextIsAdmin(c)) {
|
||||||
|
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied to target project", "code": "FORBIDDEN"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int
|
||||||
|
_ = db.(*database.DB).QueryRow(
|
||||||
|
`SELECT COUNT(*) FROM services WHERE project_id = $1 AND name = $2`,
|
||||||
|
req.ProjectID, service.Name,
|
||||||
|
).Scan(&count)
|
||||||
|
if count > 0 {
|
||||||
|
c.JSON(http.StatusConflict, gin.H{"error": "A service with this name already exists in the target project", "code": "CONFLICT"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
envName := service.Environment
|
||||||
|
if envName == "" {
|
||||||
|
envName = "production"
|
||||||
|
}
|
||||||
|
environmentID, err := getProjectEnvironmentID(db.(*database.DB), req.ProjectID, envName)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to resolve target environment"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := db.(*database.DB).Exec(
|
||||||
|
`UPDATE services SET project_id = $1, environment_id = $2, updated_at = $3 WHERE id = $4`,
|
||||||
|
req.ProjectID, environmentID, time.Now(), serviceID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to move service"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if n, _ := res.RowsAffected(); n == 0 {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found", "code": "NOT_FOUND"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "service", serviceID.String(), "service.move", map[string]interface{}{"to_project": req.ProjectID.String()})
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Service moved", "project_id": req.ProjectID})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ownsProject is true when the user owns the project or is an admin.
|
||||||
|
func ownsProject(db *database.DB, projectID uuid.UUID, userID string, isAdmin bool) bool {
|
||||||
|
if isAdmin {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
var owner string
|
||||||
|
err := db.QueryRow(`SELECT owner_id FROM projects WHERE id = $1`, projectID).Scan(&owner)
|
||||||
|
return err == nil && owner == userID
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadServiceVolumesJSON returns the raw stored mounts for a verbatim copy.
|
||||||
|
func loadServiceVolumesJSON(db *database.DB, serviceID uuid.UUID) []byte {
|
||||||
|
var raw []byte
|
||||||
|
if err := db.QueryRow(`SELECT volumes FROM services WHERE id = $1`, serviceID).Scan(&raw); err != nil || len(raw) == 0 {
|
||||||
|
return []byte("[]")
|
||||||
|
}
|
||||||
|
return raw
|
||||||
|
}
|
||||||
@@ -31,18 +31,37 @@ type Service struct {
|
|||||||
CPU string `json:"cpu" db:"cpu"`
|
CPU string `json:"cpu" db:"cpu"`
|
||||||
Memory string `json:"memory" db:"memory"`
|
Memory string `json:"memory" db:"memory"`
|
||||||
// Runtime spec
|
// Runtime spec
|
||||||
Replicas int `json:"replicas" db:"replicas"`
|
Replicas int `json:"replicas" db:"replicas"`
|
||||||
Port int `json:"port" db:"port"` // container port to expose
|
Port int `json:"port" db:"port"` // container port to expose
|
||||||
Domain string `json:"domain" db:"domain"` // public hostname via Traefik
|
Domain string `json:"domain" db:"domain"` // public hostname via Traefik
|
||||||
HealthCheckPath string `json:"healthcheck_path" db:"healthcheck_path"` // probed on Port
|
HealthCheckPath string `json:"healthcheck_path" db:"healthcheck_path"` // probed on Port
|
||||||
RestartPolicy string `json:"restart_policy" db:"restart_policy"`
|
RestartPolicy string `json:"restart_policy" db:"restart_policy"`
|
||||||
PublicURL string `json:"public_url,omitempty" db:"-"` // computed at read time
|
PublicURL string `json:"public_url,omitempty" db:"-"` // computed at read time
|
||||||
Volumes []ServiceVolume `json:"volumes" db:"-"` // loaded lazily — stored as JSONB
|
Volumes []ServiceVolume `json:"volumes" db:"-"` // loaded lazily — stored as JSONB
|
||||||
Domains []ServiceDomain `json:"domains,omitempty" db:"-"`
|
Domains []ServiceDomain `json:"domains,omitempty" db:"-"`
|
||||||
MaintenanceMode bool `json:"maintenance_mode" db:"-"`
|
MaintenanceMode bool `json:"maintenance_mode" db:"-"`
|
||||||
BasicAuth []string `json:"basic_auth,omitempty" db:"-"` // usernames only, never hashes
|
BasicAuth []string `json:"basic_auth,omitempty" db:"-"` // usernames only, never hashes
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
Builder string `json:"builder" db:"builder"` // auto|railpack|nixpacks|dockerfile|static
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
CPUReserve string `json:"cpu_reserve,omitempty" db:"cpu_reserve"`
|
||||||
|
MemoryReserve string `json:"memory_reserve,omitempty" db:"memory_reserve"`
|
||||||
|
StaticBuildCmd string `json:"static_build_cmd,omitempty" db:"static_build_cmd"`
|
||||||
|
StaticDir string `json:"static_dir,omitempty" db:"static_dir"`
|
||||||
|
SleepEnabled bool `json:"sleep_enabled" db:"-"`
|
||||||
|
SleepIdleMinutes int `json:"sleep_idle_minutes" db:"-"`
|
||||||
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadServiceSleep fills the sleep-mode columns, which live outside the
|
||||||
|
// wide service SELECTs. Missing columns (pre-migration) degrade to off.
|
||||||
|
func loadServiceSleep(db *database.DB, s *Service) {
|
||||||
|
var idle int
|
||||||
|
if err := db.QueryRow(
|
||||||
|
`SELECT COALESCE(sleep_enabled, false), COALESCE(sleep_idle_minutes, 15) FROM services WHERE id = $1`,
|
||||||
|
s.ID,
|
||||||
|
).Scan(&s.SleepEnabled, &idle); err == nil {
|
||||||
|
s.SleepIdleMinutes = idle
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceVolume is a volume/bind mount applied to every replica.
|
// ServiceVolume is a volume/bind mount applied to every replica.
|
||||||
@@ -117,28 +136,40 @@ type CreateServiceRequest struct {
|
|||||||
Volumes []ServiceVolume `json:"volumes"`
|
Volumes []ServiceVolume `json:"volumes"`
|
||||||
MaintenanceMode bool `json:"maintenance_mode"`
|
MaintenanceMode bool `json:"maintenance_mode"`
|
||||||
BasicAuth []BasicAuthCred `json:"basic_auth"`
|
BasicAuth []BasicAuthCred `json:"basic_auth"`
|
||||||
|
Builder string `json:"builder" binding:"omitempty,oneof=auto railpack nixpacks dockerfile static"`
|
||||||
|
CPUReserve string `json:"cpu_reserve"`
|
||||||
|
MemoryReserve string `json:"memory_reserve"`
|
||||||
|
StaticBuildCmd string `json:"static_build_cmd"`
|
||||||
|
StaticDir string `json:"static_dir"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateServiceRequest represents a request to update a service
|
// UpdateServiceRequest represents a request to update a service
|
||||||
type UpdateServiceRequest struct {
|
type UpdateServiceRequest struct {
|
||||||
Name string `json:"name" binding:"omitempty,min=1,max=255"`
|
Name string `json:"name" binding:"omitempty,min=1,max=255"`
|
||||||
Type string `json:"type" binding:"omitempty,oneof=web worker database cron"`
|
Type string `json:"type" binding:"omitempty,oneof=web worker database cron"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Environment string `json:"environment" binding:"omitempty,oneof=production preview development"`
|
Environment string `json:"environment" binding:"omitempty,oneof=production preview development"`
|
||||||
GitRepo string `json:"git_repo"`
|
GitRepo string `json:"git_repo"`
|
||||||
GitBranch string `json:"git_branch"`
|
GitBranch string `json:"git_branch"`
|
||||||
BuildPath string `json:"build_path"`
|
BuildPath string `json:"build_path"`
|
||||||
CPU string `json:"cpu"`
|
CPU string `json:"cpu"`
|
||||||
Memory string `json:"memory"`
|
Memory string `json:"memory"`
|
||||||
Replicas *int `json:"replicas"`
|
Replicas *int `json:"replicas"`
|
||||||
Port *int `json:"port"`
|
Port *int `json:"port"`
|
||||||
Domain *string `json:"domain"`
|
Domain *string `json:"domain"`
|
||||||
HealthCheckPath *string `json:"healthcheck_path"`
|
HealthCheckPath *string `json:"healthcheck_path"`
|
||||||
RestartPolicy string `json:"restart_policy"`
|
RestartPolicy string `json:"restart_policy"`
|
||||||
Volumes *[]ServiceVolume `json:"volumes"`
|
Volumes *[]ServiceVolume `json:"volumes"`
|
||||||
MaintenanceMode *bool `json:"maintenance_mode"`
|
MaintenanceMode *bool `json:"maintenance_mode"`
|
||||||
BasicAuth *[]BasicAuthCred `json:"basic_auth"`
|
BasicAuth *[]BasicAuthCred `json:"basic_auth"`
|
||||||
|
Builder string `json:"builder" binding:"omitempty,oneof=auto railpack nixpacks dockerfile static"`
|
||||||
|
CPUReserve *string `json:"cpu_reserve"`
|
||||||
|
MemoryReserve *string `json:"memory_reserve"`
|
||||||
|
StaticBuildCmd *string `json:"static_build_cmd"`
|
||||||
|
StaticDir *string `json:"static_dir"`
|
||||||
|
SleepEnabled *bool `json:"sleep_enabled"`
|
||||||
|
SleepIdleMinutes *int `json:"sleep_idle_minutes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleGetServices retrieves all services for a project
|
// handleGetServices retrieves all services for a project
|
||||||
@@ -183,9 +214,13 @@ func handleGetServices(c *gin.Context) {
|
|||||||
COALESCE(replicas, 1), COALESCE(port, 0),
|
COALESCE(replicas, 1), COALESCE(port, 0),
|
||||||
COALESCE(domain, ''), COALESCE(healthcheck_path, ''),
|
COALESCE(domain, ''), COALESCE(healthcheck_path, ''),
|
||||||
COALESCE(restart_policy, 'unless-stopped'),
|
COALESCE(restart_policy, 'unless-stopped'),
|
||||||
created_at, updated_at
|
COALESCE(builder, 'auto'), COALESCE(cpu_reserve, ''),
|
||||||
FROM services
|
COALESCE(memory_reserve, ''), COALESCE(static_build_cmd, ''),
|
||||||
WHERE project_id = $1
|
COALESCE(static_dir, ''),
|
||||||
|
COALESCE(sleep_enabled, false), COALESCE(sleep_idle_minutes, 15),
|
||||||
|
created_at, updated_at
|
||||||
|
FROM services
|
||||||
|
WHERE project_id = $1
|
||||||
ORDER BY created_at DESC`,
|
ORDER BY created_at DESC`,
|
||||||
projectID,
|
projectID,
|
||||||
)
|
)
|
||||||
@@ -203,7 +238,10 @@ func handleGetServices(c *gin.Context) {
|
|||||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt,
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.SleepEnabled, &service.SleepIdleMinutes,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan service"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan service"})
|
||||||
@@ -314,12 +352,38 @@ func handleCreateService(c *gin.Context) {
|
|||||||
UpdatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set default values if not provided
|
// Set default values if not provided; app_settings.default_cpu /
|
||||||
|
// default_memory override the hardcoded fallbacks.
|
||||||
if service.CPU == "" {
|
if service.CPU == "" {
|
||||||
service.CPU = "0.5"
|
service.CPU = firstNonEmpty(settingValue(db.(*database.DB), "default_cpu", "", ""), "0.5")
|
||||||
}
|
}
|
||||||
if service.Memory == "" {
|
if service.Memory == "" {
|
||||||
service.Memory = "512Mi"
|
service.Memory = firstNonEmpty(settingValue(db.(*database.DB), "default_memory", "", ""), "512Mi")
|
||||||
|
}
|
||||||
|
if req.Builder == "" {
|
||||||
|
service.Builder = "auto"
|
||||||
|
} else {
|
||||||
|
service.Builder = req.Builder
|
||||||
|
}
|
||||||
|
service.CPUReserve = req.CPUReserve
|
||||||
|
service.MemoryReserve = req.MemoryReserve
|
||||||
|
service.StaticBuildCmd = req.StaticBuildCmd
|
||||||
|
service.StaticDir = req.StaticDir
|
||||||
|
if service.Builder == "static" && service.StaticDir == "" {
|
||||||
|
service.StaticDir = "dist"
|
||||||
|
}
|
||||||
|
for label, check := range map[string]func(string) error{
|
||||||
|
"cpu": validateCPUSpec, "cpu_reserve": validateCPUSpec,
|
||||||
|
"memory": validateMemorySpec, "memory_reserve": validateMemorySpec,
|
||||||
|
} {
|
||||||
|
v := map[string]string{
|
||||||
|
"cpu": service.CPU, "cpu_reserve": service.CPUReserve,
|
||||||
|
"memory": service.Memory, "memory_reserve": service.MemoryReserve,
|
||||||
|
}[label]
|
||||||
|
if err := check(v); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid %s: %s", label, err.Error()), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if service.Replicas < 1 {
|
if service.Replicas < 1 {
|
||||||
service.Replicas = 1
|
service.Replicas = 1
|
||||||
@@ -351,14 +415,18 @@ func handleCreateService(c *gin.Context) {
|
|||||||
(id, project_id, name, environment_id, service_type, source_type, source_url, image_name,
|
(id, project_id, name, environment_id, service_type, source_type, source_url, image_name,
|
||||||
build_command, start_command, type, status, image, command, environment,
|
build_command, start_command, type, status, image, command, environment,
|
||||||
git_repo, git_branch, build_path, cpu, memory, replicas, port, domain,
|
git_repo, git_branch, build_path, cpu, memory, replicas, port, domain,
|
||||||
healthcheck_path, restart_policy, volumes, created_at, updated_at)
|
healthcheck_path, restart_policy, volumes,
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28)`,
|
builder, cpu_reserve, memory_reserve, static_build_cmd, static_dir,
|
||||||
|
created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26,
|
||||||
|
$27, $28, $29, $30, $31, $32, $33)`,
|
||||||
service.ID, service.ProjectID, service.Name, environmentID, service.Type,
|
service.ID, service.ProjectID, service.Name, environmentID, service.Type,
|
||||||
sourceType, firstNonEmpty(service.GitRepo, service.Image), service.Image,
|
sourceType, firstNonEmpty(service.GitRepo, service.Image), service.Image,
|
||||||
"", service.Command, service.Type, service.Status, service.Image, service.Command,
|
"", service.Command, service.Type, service.Status, service.Image, service.Command,
|
||||||
service.Environment, service.GitRepo, service.GitBranch, service.BuildPath, service.CPU, service.Memory,
|
service.Environment, service.GitRepo, service.GitBranch, service.BuildPath, service.CPU, service.Memory,
|
||||||
service.Replicas, service.Port, service.Domain, service.HealthCheckPath, service.RestartPolicy,
|
service.Replicas, service.Port, service.Domain, service.HealthCheckPath, service.RestartPolicy,
|
||||||
volumesJSON, service.CreatedAt, service.UpdatedAt,
|
volumesJSON, service.Builder, service.CPUReserve, service.MemoryReserve,
|
||||||
|
service.StaticBuildCmd, service.StaticDir, service.CreatedAt, service.UpdatedAt,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -386,6 +454,10 @@ func handleCreateService(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
service.Domains = loadServiceDomains(db.(*database.DB), service.ID)
|
service.Domains = loadServiceDomains(db.(*database.DB), service.ID)
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "service", service.ID.String(), "create", map[string]interface{}{
|
||||||
|
"name": service.Name, "type": service.Type, "project_id": service.ProjectID,
|
||||||
|
})
|
||||||
|
|
||||||
c.JSON(http.StatusCreated, gin.H{"service": service})
|
c.JSON(http.StatusCreated, gin.H{"service": service})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,6 +515,9 @@ func handleGetService(c *gin.Context) {
|
|||||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
s.created_at, s.updated_at
|
s.created_at, s.updated_at
|
||||||
FROM services s
|
FROM services s
|
||||||
JOIN projects p ON s.project_id = p.id
|
JOIN projects p ON s.project_id = p.id
|
||||||
@@ -454,7 +529,9 @@ func handleGetService(c *gin.Context) {
|
|||||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt,
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -469,6 +546,7 @@ func handleGetService(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
service.Domains = loadServiceDomains(db.(*database.DB), service.ID)
|
service.Domains = loadServiceDomains(db.(*database.DB), service.ID)
|
||||||
|
loadServiceSleep(db.(*database.DB), &service)
|
||||||
maintenance, basicAuth := serviceAccess(db.(*database.DB), service.ID)
|
maintenance, basicAuth := serviceAccess(db.(*database.DB), service.ID)
|
||||||
service.MaintenanceMode = maintenance
|
service.MaintenanceMode = maintenance
|
||||||
service.BasicAuth = basicAuthUsernames(basicAuth)
|
service.BasicAuth = basicAuthUsernames(basicAuth)
|
||||||
@@ -520,6 +598,9 @@ func handleUpdateService(c *gin.Context) {
|
|||||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
s.created_at, s.updated_at
|
s.created_at, s.updated_at
|
||||||
FROM services s
|
FROM services s
|
||||||
JOIN projects p ON s.project_id = p.id
|
JOIN projects p ON s.project_id = p.id
|
||||||
@@ -532,6 +613,9 @@ func handleUpdateService(c *gin.Context) {
|
|||||||
&existingService.BuildPath, &existingService.CPU, &existingService.Memory,
|
&existingService.BuildPath, &existingService.CPU, &existingService.Memory,
|
||||||
&existingService.Replicas, &existingService.Port, &existingService.Domain,
|
&existingService.Replicas, &existingService.Port, &existingService.Domain,
|
||||||
&existingService.HealthCheckPath, &existingService.RestartPolicy,
|
&existingService.HealthCheckPath, &existingService.RestartPolicy,
|
||||||
|
&existingService.Builder, &existingService.CPUReserve,
|
||||||
|
&existingService.MemoryReserve, &existingService.StaticBuildCmd,
|
||||||
|
&existingService.StaticDir,
|
||||||
&existingService.CreatedAt, &existingService.UpdatedAt,
|
&existingService.CreatedAt, &existingService.UpdatedAt,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -586,6 +670,48 @@ func handleUpdateService(c *gin.Context) {
|
|||||||
if req.RestartPolicy != "" {
|
if req.RestartPolicy != "" {
|
||||||
existingService.RestartPolicy = req.RestartPolicy
|
existingService.RestartPolicy = req.RestartPolicy
|
||||||
}
|
}
|
||||||
|
if req.Builder != "" {
|
||||||
|
existingService.Builder = req.Builder
|
||||||
|
}
|
||||||
|
if req.CPUReserve != nil {
|
||||||
|
existingService.CPUReserve = *req.CPUReserve
|
||||||
|
}
|
||||||
|
if req.MemoryReserve != nil {
|
||||||
|
existingService.MemoryReserve = *req.MemoryReserve
|
||||||
|
}
|
||||||
|
if req.StaticBuildCmd != nil {
|
||||||
|
existingService.StaticBuildCmd = *req.StaticBuildCmd
|
||||||
|
}
|
||||||
|
if req.StaticDir != nil {
|
||||||
|
existingService.StaticDir = *req.StaticDir
|
||||||
|
}
|
||||||
|
if existingService.Builder == "static" && existingService.StaticDir == "" {
|
||||||
|
existingService.StaticDir = "dist"
|
||||||
|
}
|
||||||
|
loadServiceSleep(db.(*database.DB), &existingService)
|
||||||
|
if req.SleepEnabled != nil {
|
||||||
|
existingService.SleepEnabled = *req.SleepEnabled
|
||||||
|
}
|
||||||
|
if req.SleepIdleMinutes != nil {
|
||||||
|
if *req.SleepIdleMinutes < 1 || *req.SleepIdleMinutes > 1440 {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "sleep_idle_minutes must be 1-1440", "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
existingService.SleepIdleMinutes = *req.SleepIdleMinutes
|
||||||
|
}
|
||||||
|
for label, check := range map[string]func(string) error{
|
||||||
|
"cpu": validateCPUSpec, "cpu_reserve": validateCPUSpec,
|
||||||
|
"memory": validateMemorySpec, "memory_reserve": validateMemorySpec,
|
||||||
|
} {
|
||||||
|
v := map[string]string{
|
||||||
|
"cpu": existingService.CPU, "cpu_reserve": existingService.CPUReserve,
|
||||||
|
"memory": existingService.Memory, "memory_reserve": existingService.MemoryReserve,
|
||||||
|
}[label]
|
||||||
|
if err := check(v); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid %s: %s", label, err.Error()), "code": "VALIDATION"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
// lib/pq sends a nil []byte as an empty bytea literal rather than NULL,
|
// lib/pq sends a nil []byte as an empty bytea literal rather than NULL,
|
||||||
// so the COALESCE fallback needs an untyped nil when volumes aren't sent.
|
// so the COALESCE fallback needs an untyped nil when volumes aren't sent.
|
||||||
var volumesArg interface{}
|
var volumesArg interface{}
|
||||||
@@ -607,7 +733,10 @@ func handleUpdateService(c *gin.Context) {
|
|||||||
SET name = $1, type = $2, image = $3, command = $4, environment = $5,
|
SET name = $1, type = $2, image = $3, command = $4, environment = $5,
|
||||||
git_repo = $6, git_branch = $7, build_path = $8, cpu = $9, memory = $10,
|
git_repo = $6, git_branch = $7, build_path = $8, cpu = $9, memory = $10,
|
||||||
replicas = $11, port = $12, domain = $13, healthcheck_path = $14,
|
replicas = $11, port = $12, domain = $13, healthcheck_path = $14,
|
||||||
restart_policy = $15, volumes = COALESCE($17::jsonb, volumes), updated_at = $16
|
restart_policy = $15, volumes = COALESCE($17::jsonb, volumes), updated_at = $16,
|
||||||
|
builder = $19, cpu_reserve = $20, memory_reserve = $21,
|
||||||
|
static_build_cmd = $22, static_dir = $23,
|
||||||
|
sleep_enabled = $24, sleep_idle_minutes = $25
|
||||||
WHERE id = $18`,
|
WHERE id = $18`,
|
||||||
existingService.Name, existingService.Type, existingService.Image, existingService.Command,
|
existingService.Name, existingService.Type, existingService.Image, existingService.Command,
|
||||||
existingService.Environment, existingService.GitRepo, existingService.GitBranch,
|
existingService.Environment, existingService.GitRepo, existingService.GitBranch,
|
||||||
@@ -615,6 +744,9 @@ func handleUpdateService(c *gin.Context) {
|
|||||||
existingService.Replicas, existingService.Port, existingService.Domain,
|
existingService.Replicas, existingService.Port, existingService.Domain,
|
||||||
existingService.HealthCheckPath, existingService.RestartPolicy,
|
existingService.HealthCheckPath, existingService.RestartPolicy,
|
||||||
existingService.UpdatedAt, volumesArg, existingService.ID,
|
existingService.UpdatedAt, volumesArg, existingService.ID,
|
||||||
|
existingService.Builder, existingService.CPUReserve, existingService.MemoryReserve,
|
||||||
|
existingService.StaticBuildCmd, existingService.StaticDir,
|
||||||
|
existingService.SleepEnabled, existingService.SleepIdleMinutes,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -748,5 +880,7 @@ func handleDeleteService(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LogAuditWithRequest(c, "service", serviceID.String(), "delete", map[string]interface{}{})
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "Service deleted successfully"})
|
c.JSON(http.StatusOK, gin.H{"message": "Service deleted successfully"})
|
||||||
}
|
}
|
||||||
@@ -23,10 +23,49 @@ const (
|
|||||||
settingSignupEnabled = "signup_enabled"
|
settingSignupEnabled = "signup_enabled"
|
||||||
settingCloudflareTunnelToken = "cloudflare_tunnel_token"
|
settingCloudflareTunnelToken = "cloudflare_tunnel_token"
|
||||||
|
|
||||||
|
// White-label branding keys — unset fields fall back to Containr
|
||||||
|
// defaults. Self-hosters rebrand their instance via these.
|
||||||
|
settingBrandProductName = "branding.product_name"
|
||||||
|
settingBrandLogoURL = "branding.logo_url"
|
||||||
|
settingBrandFaviconURL = "branding.favicon_url"
|
||||||
|
settingBrandAccent = "branding.accent_color"
|
||||||
|
settingBrandDocsURL = "branding.docs_url"
|
||||||
|
settingBrandSupportURL = "branding.support_url"
|
||||||
|
|
||||||
cloudflaredContainerName = "containr-cloudflared"
|
cloudflaredContainerName = "containr-cloudflared"
|
||||||
cloudflaredImage = "cloudflare/cloudflared:latest"
|
cloudflaredImage = "cloudflare/cloudflared:latest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// brandingConfig is the public white-label payload. Fields are always
|
||||||
|
// populated — defaults keep the instance Containr-branded.
|
||||||
|
type brandingConfig struct {
|
||||||
|
ProductName string `json:"product_name"`
|
||||||
|
LogoURL string `json:"logo_url"`
|
||||||
|
FaviconURL string `json:"favicon_url"`
|
||||||
|
AccentColor string `json:"accent_color"`
|
||||||
|
DocsURL string `json:"docs_url"`
|
||||||
|
SupportURL string `json:"support_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveBranding(db *database.DB) brandingConfig {
|
||||||
|
return brandingConfig{
|
||||||
|
ProductName: settingValue(db, settingBrandProductName, "BRAND_NAME", "Containr"),
|
||||||
|
LogoURL: settingValue(db, settingBrandLogoURL, "BRAND_LOGO_URL", ""),
|
||||||
|
FaviconURL: settingValue(db, settingBrandFaviconURL, "BRAND_FAVICON_URL", ""),
|
||||||
|
AccentColor: settingValue(db, settingBrandAccent, "BRAND_ACCENT_COLOR", ""),
|
||||||
|
DocsURL: settingValue(db, settingBrandDocsURL, "BRAND_DOCS_URL", ""),
|
||||||
|
SupportURL: settingValue(db, settingBrandSupportURL, "BRAND_SUPPORT_URL", ""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /branding — public so the login page and shell can brand themselves
|
||||||
|
// before a session exists.
|
||||||
|
func handleGetBranding(c *gin.Context) {
|
||||||
|
db, _ := c.Get("db")
|
||||||
|
database, _ := db.(*database.DB)
|
||||||
|
c.JSON(http.StatusOK, resolveBranding(database))
|
||||||
|
}
|
||||||
|
|
||||||
// settingValue resolves a setting: app_settings row first, env fallback,
|
// settingValue resolves a setting: app_settings row first, env fallback,
|
||||||
// then the caller's default.
|
// then the caller's default.
|
||||||
func settingValue(db *database.DB, key, envKey, fallback string) string {
|
func settingValue(db *database.DB, key, envKey, fallback string) string {
|
||||||
@@ -110,6 +149,7 @@ func handleGetSettings(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"signup_enabled": signupEnabled(db),
|
"signup_enabled": signupEnabled(db),
|
||||||
"cloudflare_tunnel": cloudflareTunnelState(c.Request.Context(), db, client),
|
"cloudflare_tunnel": cloudflareTunnelState(c.Request.Context(), db, client),
|
||||||
|
"branding": resolveBranding(db),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +162,14 @@ func handleUpdateSettings(c *gin.Context) {
|
|||||||
var req struct {
|
var req struct {
|
||||||
SignupEnabled *bool `json:"signup_enabled"`
|
SignupEnabled *bool `json:"signup_enabled"`
|
||||||
CloudflareTunnelToken *string `json:"cloudflare_tunnel_token"`
|
CloudflareTunnelToken *string `json:"cloudflare_tunnel_token"`
|
||||||
|
Branding *struct {
|
||||||
|
ProductName *string `json:"product_name"`
|
||||||
|
LogoURL *string `json:"logo_url"`
|
||||||
|
FaviconURL *string `json:"favicon_url"`
|
||||||
|
AccentColor *string `json:"accent_color"`
|
||||||
|
DocsURL *string `json:"docs_url"`
|
||||||
|
SupportURL *string `json:"support_url"`
|
||||||
|
} `json:"branding"`
|
||||||
}
|
}
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
@@ -158,6 +206,36 @@ func handleUpdateSettings(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Branding != nil {
|
||||||
|
fields := []struct {
|
||||||
|
key string
|
||||||
|
value *string
|
||||||
|
}{
|
||||||
|
{settingBrandProductName, req.Branding.ProductName},
|
||||||
|
{settingBrandLogoURL, req.Branding.LogoURL},
|
||||||
|
{settingBrandFaviconURL, req.Branding.FaviconURL},
|
||||||
|
{settingBrandAccent, req.Branding.AccentColor},
|
||||||
|
{settingBrandDocsURL, req.Branding.DocsURL},
|
||||||
|
{settingBrandSupportURL, req.Branding.SupportURL},
|
||||||
|
}
|
||||||
|
for _, f := range fields {
|
||||||
|
if f.value == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
v := strings.TrimSpace(*f.value)
|
||||||
|
var err error
|
||||||
|
if v == "" {
|
||||||
|
err = deleteSetting(db, f.key)
|
||||||
|
} else {
|
||||||
|
err = setSetting(db, f.key, v, false)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save branding"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
handleGetSettings(c)
|
handleGetSettings(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,461 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/deployment"
|
||||||
|
"containr/internal/docker"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/api/types/network"
|
||||||
|
"github.com/docker/docker/api/types/registry"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sleep mode: services with sleep_enabled are scaled to zero after
|
||||||
|
// sleep_idle_minutes without inbound traffic. Idleness is measured as
|
||||||
|
// NetworkIO byte deltas across the service's containers — we have no
|
||||||
|
// per-request layer in front of Traefik, so counters are the signal.
|
||||||
|
//
|
||||||
|
// When a service sleeps its containers are removed and a tiny busybox
|
||||||
|
// "wake" container takes over the same Traefik host rule at a higher
|
||||||
|
// priority: it serves a reloading "waking" page and pings the unauthenticated
|
||||||
|
// internal wake endpoint, which reconciles the service back to running.
|
||||||
|
// Wake containers carry a dedicated label (not the service label) so
|
||||||
|
// RuntimeState and reconcile never count them as replicas; the sweeper
|
||||||
|
// prunes them once the workload is running again.
|
||||||
|
|
||||||
|
const (
|
||||||
|
wakeImage = "busybox:1.36"
|
||||||
|
wakeLabel = "containr.wake"
|
||||||
|
wakeForLabel = "containr.wake-for"
|
||||||
|
wakeSweepInterval = 30 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
type sleepTracker struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
activity map[uuid.UUID]sleepActivity
|
||||||
|
}
|
||||||
|
|
||||||
|
type sleepActivity struct {
|
||||||
|
bytes uint64
|
||||||
|
idleSince time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var tracker = &sleepTracker{activity: make(map[uuid.UUID]sleepActivity)}
|
||||||
|
|
||||||
|
// StartSleepSweeper polls enabled services and scales idle ones to zero.
|
||||||
|
func StartSleepSweeper(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, apiPort int) {
|
||||||
|
if db == nil || dc == nil || engine == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(wakeSweepInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
sweepSleeping(ctx, db, dc, engine, apiPort)
|
||||||
|
pruneWakeContainers(ctx, db, dc, engine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
log.Printf("sleep sweeper: running (interval %s)", wakeSweepInterval)
|
||||||
|
}
|
||||||
|
|
||||||
|
type sleepCandidate struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
ProjectID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Status string
|
||||||
|
IdleMinutes int
|
||||||
|
}
|
||||||
|
|
||||||
|
func sweepSleeping(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, apiPort int) {
|
||||||
|
rows, err := db.QueryContext(ctx,
|
||||||
|
`SELECT id, project_id, name, COALESCE(status,''), COALESCE(sleep_idle_minutes,15)
|
||||||
|
FROM services WHERE sleep_enabled AND COALESCE(status,'') IN ('running','deployed','degraded','starting')`)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var candidates []sleepCandidate
|
||||||
|
for rows.Next() {
|
||||||
|
var s sleepCandidate
|
||||||
|
if err := rows.Scan(&s.ID, &s.ProjectID, &s.Name, &s.Status, &s.IdleMinutes); err == nil {
|
||||||
|
candidates = append(candidates, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rows.Close()
|
||||||
|
|
||||||
|
for _, svc := range candidates {
|
||||||
|
containers, err := engine.ListServiceContainers(ctx, svc.ID.String())
|
||||||
|
if err != nil || len(containers) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Ignore wake containers — they're the placeholder, not the workload.
|
||||||
|
workload := containers[:0]
|
||||||
|
for _, c := range containers {
|
||||||
|
if c.Labels[wakeLabel] != "true" {
|
||||||
|
workload = append(workload, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(workload) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var total uint64
|
||||||
|
for _, c := range workload {
|
||||||
|
total += containerNetBytes(ctx, dc, c.ID)
|
||||||
|
}
|
||||||
|
if markIdle(svc.ID, total, time.Duration(svc.IdleMinutes)*time.Minute) {
|
||||||
|
putToSleep(ctx, db, dc, engine, svc, apiPort)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// markIdle returns true when the network counter stopped growing past the
|
||||||
|
// timeout. First sighting only sets the baseline — no idle without history.
|
||||||
|
func markIdle(id uuid.UUID, bytes uint64, timeout time.Duration) bool {
|
||||||
|
tracker.mu.Lock()
|
||||||
|
defer tracker.mu.Unlock()
|
||||||
|
a := tracker.activity[id]
|
||||||
|
if bytes != a.bytes || a.idleSince.IsZero() {
|
||||||
|
tracker.activity[id] = sleepActivity{bytes: bytes, idleSince: time.Now()}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return time.Since(a.idleSince) >= timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
func containerNetBytes(ctx context.Context, dc *docker.Client, id string) uint64 {
|
||||||
|
stats, err := dc.GetContainerStats(ctx, id, false)
|
||||||
|
if err != nil || stats == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
defer stats.Body.Close()
|
||||||
|
var snap struct {
|
||||||
|
Networks map[string]struct {
|
||||||
|
RxBytes uint64 `json:"rx_bytes"`
|
||||||
|
TxBytes uint64 `json:"tx_bytes"`
|
||||||
|
} `json:"networks"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(stats.Body).Decode(&snap); err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
var total uint64
|
||||||
|
for _, n := range snap.Networks {
|
||||||
|
total += n.RxBytes + n.TxBytes
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
// putToSleep removes the workload containers, marks the service sleeping,
|
||||||
|
// and installs the wake placeholder when the service has a public domain.
|
||||||
|
func putToSleep(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, svc sleepCandidate, apiPort int) {
|
||||||
|
containers, err := engine.ListServiceContainers(ctx, svc.ID.String())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, c := range containers {
|
||||||
|
if c.Labels[wakeLabel] == "true" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_ = dc.StopContainer(ctx, c.ID, nil)
|
||||||
|
_ = dc.RemoveContainer(ctx, c.ID, true)
|
||||||
|
}
|
||||||
|
tracker.mu.Lock()
|
||||||
|
delete(tracker.activity, svc.ID)
|
||||||
|
tracker.mu.Unlock()
|
||||||
|
_, _ = db.Exec(`UPDATE services SET status = 'sleeping', updated_at = $1 WHERE id = $2`, time.Now(), svc.ID)
|
||||||
|
log.Printf("sleep: %s (%s) scaled to zero after idle timeout", svc.Name, svc.ID)
|
||||||
|
|
||||||
|
var domain string
|
||||||
|
_ = db.QueryRow(`SELECT COALESCE(domain,'') FROM services WHERE id = $1`, svc.ID).Scan(&domain)
|
||||||
|
domains := serviceDomainNames(db, svc.ID, domain)
|
||||||
|
if len(domains) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := spawnWakeContainer(ctx, db, dc, engine, svc, domains, apiPort); err != nil {
|
||||||
|
log.Printf("sleep: wake placeholder for %s failed: %v", svc.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// spawnWakeContainer runs a busybox httpd holding the service's Traefik
|
||||||
|
// host rule at higher priority. On boot it calls the wake endpoint, then
|
||||||
|
// keeps serving a reloading page until the next reconcile removes it.
|
||||||
|
func spawnWakeContainer(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, svc sleepCandidate, domains []string, apiPort int) error {
|
||||||
|
edgeNet := ""
|
||||||
|
for _, n := range networkList(ctx, dc) {
|
||||||
|
if n == "containr-network" {
|
||||||
|
edgeNet = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if edgeNet == "" {
|
||||||
|
return fmt.Errorf("no edge network")
|
||||||
|
}
|
||||||
|
router := "svc-" + svc.ID.String()[:8]
|
||||||
|
sort.Strings(domains)
|
||||||
|
rules := make([]string, 0, len(domains))
|
||||||
|
for _, d := range domains {
|
||||||
|
rules = append(rules, "Host(`"+d+"`)")
|
||||||
|
}
|
||||||
|
labels := map[string]string{
|
||||||
|
deployment.LabelManaged: "true",
|
||||||
|
deployment.LabelProject: svc.ProjectID.String(),
|
||||||
|
wakeForLabel: svc.ID.String(),
|
||||||
|
wakeLabel: "true",
|
||||||
|
"traefik.enable": "true",
|
||||||
|
"traefik.docker.network": edgeNet,
|
||||||
|
"traefik.http.routers." + router + ".rule": strings.Join(rules, " || "),
|
||||||
|
"traefik.http.routers." + router + ".entrypoints": "web",
|
||||||
|
"traefik.http.routers." + router + ".priority": "100",
|
||||||
|
"traefik.http.services." + router + ".loadbalancer.server.port": "80",
|
||||||
|
}
|
||||||
|
|
||||||
|
wakeURL := wakeEndpointURL(ctx, dc, apiPort, svc.ID, edgeNet)
|
||||||
|
page := `<!doctype html><html><head><meta http-equiv="refresh" content="4"><title>Waking up</title>` +
|
||||||
|
`<style>body{font-family:system-ui,sans-serif;background:#0b0d12;color:#dde2ea;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0}` +
|
||||||
|
`.card{text-align:center;max-width:34rem;padding:2rem}h1{font-size:1.25rem;font-weight:600}p{color:#98a2b3}</style></head>` +
|
||||||
|
`<body><div class="card"><h1>Service is waking up</h1><p>This Containr service was sleeping and is resuming now. The page reloads automatically.</p></div></body></html>`
|
||||||
|
// httpd without -f daemonizes, so the sequence is: write page, start
|
||||||
|
// httpd, fire the wake callback, then hold PID1 on sleep infinity.
|
||||||
|
cmd := fmt.Sprintf(
|
||||||
|
`mkdir -p /www && printf '%%s' '%s' > /www/index.html && `+
|
||||||
|
`httpd -p 80 -h /www && `+
|
||||||
|
`(sleep 2; wget -q -O /dev/null --timeout=20 '%s' 2>/dev/null || true) && `+
|
||||||
|
`sleep infinity`,
|
||||||
|
strings.ReplaceAll(page, "'", "'\\''"), wakeURL)
|
||||||
|
|
||||||
|
endpoints := map[string]*network.EndpointSettings{edgeNet: {}}
|
||||||
|
if ownNet := dc.OwnNetworkName(ctx); ownNet != "" && ownNet != edgeNet {
|
||||||
|
endpoints[ownNet] = &network.EndpointSettings{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pull is best-effort — the image almost always sits in cache.
|
||||||
|
_ = dc.PullImageWait(ctx, wakeImage, registry.AuthConfig{})
|
||||||
|
|
||||||
|
// A previous placeholder may linger as Exited — clear the name first.
|
||||||
|
name := fmt.Sprintf("containr-wake-%s", svc.ID.String()[:8])
|
||||||
|
if stale, err := dc.ListContainersFiltered(ctx, filters.NewArgs(
|
||||||
|
filters.Arg("name", "^/"+name+"$"),
|
||||||
|
), true); err == nil {
|
||||||
|
for _, s := range stale {
|
||||||
|
_ = dc.RemoveContainer(ctx, s.ID, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := dc.CreateContainer(ctx, docker.ContainerConfig{
|
||||||
|
Name: name,
|
||||||
|
Image: wakeImage,
|
||||||
|
Cmd: []string{"sh", "-c", cmd},
|
||||||
|
Labels: labels,
|
||||||
|
Networks: endpoints,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return dc.StartContainer(ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func networkList(ctx context.Context, dc *docker.Client) []string {
|
||||||
|
networks, err := dc.ListNetworks(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
names := make([]string, 0, len(networks))
|
||||||
|
for _, n := range networks {
|
||||||
|
names = append(names, n.Name)
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
// wakeEndpointURL builds the internal URL the wake container calls. The
|
||||||
|
// edge network's gateway reaches the host's published API port; when the
|
||||||
|
// API itself is containerized its own name resolves on the shared network.
|
||||||
|
func wakeEndpointURL(ctx context.Context, dc *docker.Client, apiPort int, serviceID uuid.UUID, edgeNet string) string {
|
||||||
|
if dc.OwnNetworkName(ctx) != "" {
|
||||||
|
if self, err := dc.GetContainer(ctx, selfID()); err == nil {
|
||||||
|
name := strings.TrimPrefix(self.Name, "/")
|
||||||
|
if name != "" {
|
||||||
|
return fmt.Sprintf("http://%s:%d/api/v1/internal/wake/%s", name, apiPort, serviceID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if gw := networkGateway(ctx, dc, edgeNet); gw != "" {
|
||||||
|
return fmt.Sprintf("http://%s:%d/api/v1/internal/wake/%s", gw, apiPort, serviceID)
|
||||||
|
}
|
||||||
|
if probe := dc.HostProbeHost(ctx); probe != "" {
|
||||||
|
return fmt.Sprintf("http://%s:%d/api/v1/internal/wake/%s", probe, apiPort, serviceID)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("http://127.0.0.1:%d/api/v1/internal/wake/%s", apiPort, serviceID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func networkGateway(ctx context.Context, dc *docker.Client, name string) string {
|
||||||
|
// ListNetworks does not populate IPAM config — inspect is required.
|
||||||
|
return dc.NetworkGateway(ctx, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func selfID() string {
|
||||||
|
// Inside a container the hostname is the short container ID.
|
||||||
|
if hn, err := os.Hostname(); err == nil {
|
||||||
|
return hn
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// pruneWakeContainers removes wake placeholders whose service is healthy
|
||||||
|
// again — belt & braces over the reconcile drift sweep.
|
||||||
|
func pruneWakeContainers(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine) {
|
||||||
|
containers, err := dc.ListContainersFiltered(ctx, filters.NewArgs(filters.Arg("label", wakeLabel+"=true")), true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, c := range containers {
|
||||||
|
svcID := c.Labels[wakeForLabel]
|
||||||
|
if svcID == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
running, _ := engine.ListServiceContainers(ctx, svcID)
|
||||||
|
live := 0
|
||||||
|
for _, r := range running {
|
||||||
|
if r.State == "running" {
|
||||||
|
live++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var status string
|
||||||
|
_ = db.QueryRow(`SELECT COALESCE(status,'') FROM services WHERE id = $1`, svcID).Scan(&status)
|
||||||
|
if live > 0 || status == "stopped" || status == "" {
|
||||||
|
_ = dc.RemoveContainer(ctx, c.ID, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleServiceWake is the unauthenticated internal endpoint the wake
|
||||||
|
// container hits. It reconciles the service back to running. Waking is
|
||||||
|
// idempotent and non-destructive, so no token is required; the endpoint
|
||||||
|
// never mutates configuration.
|
||||||
|
func handleServiceWake(c *gin.Context) {
|
||||||
|
dbVal, _ := c.Get("db")
|
||||||
|
db := dbVal.(*database.DB)
|
||||||
|
engineVal, _ := c.Get("deployment_engine")
|
||||||
|
engine, _ := engineVal.(*deployment.DeploymentEngine)
|
||||||
|
if engine == nil {
|
||||||
|
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "deployment engine unavailable"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid service id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var service Service
|
||||||
|
err = db.QueryRow(
|
||||||
|
`SELECT s.id, s.project_id, s.name,
|
||||||
|
COALESCE(s.type, s.service_type, ''), COALESCE(s.status, ''),
|
||||||
|
COALESCE(s.image, s.image_name, ''), COALESCE(s.command, s.start_command, ''),
|
||||||
|
COALESCE(s.environment, ''), COALESCE(s.git_repo, s.source_url, ''),
|
||||||
|
COALESCE(s.git_branch, ''), COALESCE(s.build_path, ''),
|
||||||
|
COALESCE(s.cpu, ''), COALESCE(s.memory, ''),
|
||||||
|
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||||
|
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||||
|
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||||
|
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||||
|
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||||
|
COALESCE(s.static_dir, ''),
|
||||||
|
s.created_at, s.updated_at
|
||||||
|
FROM services s WHERE s.id = $1`, id,
|
||||||
|
).Scan(
|
||||||
|
&service.ID, &service.ProjectID, &service.Name, &service.Type, &service.Status,
|
||||||
|
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||||
|
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||||
|
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||||
|
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||||
|
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||||
|
&service.CreatedAt, &service.UpdatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "service not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if service.Status == "running" || service.Status == "deployed" {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": service.Status})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
spec, err := serviceRuntimeSpec(db, service)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
state, err := engine.ReconcileService(ctx, spec)
|
||||||
|
if err != nil {
|
||||||
|
_, _ = db.Exec(`UPDATE services SET status = 'failed', updated_at = $1 WHERE id = $2`, time.Now(), service.ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
status := "running"
|
||||||
|
if state != nil {
|
||||||
|
status = state.Status
|
||||||
|
persistPublishedPort(db, service.ID, state.Ports)
|
||||||
|
}
|
||||||
|
_, _ = db.Exec(`UPDATE services SET status = $1, updated_at = $2 WHERE id = $3`, status, time.Now(), service.ID)
|
||||||
|
}()
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{"status": "waking"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleServiceSleep puts a service to sleep immediately (owner+ only).
|
||||||
|
func handleServiceSleep(c *gin.Context) {
|
||||||
|
dbVal, _ := c.Get("db")
|
||||||
|
db := dbVal.(*database.DB)
|
||||||
|
engineVal, _ := c.Get("deployment_engine")
|
||||||
|
engine, _ := engineVal.(*deployment.DeploymentEngine)
|
||||||
|
dcVal, _ := c.Get("docker_client")
|
||||||
|
dc, _ := dcVal.(*docker.Client)
|
||||||
|
if engine == nil || dc == nil {
|
||||||
|
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "deployment engine unavailable"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
service, ok := loadOwnedService(c, db)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var port int
|
||||||
|
_ = db.QueryRow(`SELECT COALESCE(port,0) FROM services WHERE id = $1`, service.ID).Scan(&port)
|
||||||
|
svc := sleepCandidate{
|
||||||
|
ID: service.ID,
|
||||||
|
ProjectID: service.ProjectID,
|
||||||
|
Name: service.Name,
|
||||||
|
Status: service.Status,
|
||||||
|
}
|
||||||
|
go putToSleep(context.Background(), db, dc, engine, svc, apiPortFromContext(c))
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{"status": "sleeping"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleServiceWakeNow wakes a sleeping service on demand (owner+ only).
|
||||||
|
func handleServiceWakeNow(c *gin.Context) {
|
||||||
|
if _, ok := loadOwnedService(c, c.MustGet("db").(*database.DB)); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
handleServiceWake(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// apiPortFromContext pulls the configured HTTP port for the wake callback URL.
|
||||||
|
func apiPortFromContext(c *gin.Context) int {
|
||||||
|
if v, exists := c.Get("api_port"); exists {
|
||||||
|
if p, ok := v.(int); ok {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 8080
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMarkIdleTracksCounter(t *testing.T) {
|
||||||
|
id := uuid.New()
|
||||||
|
timeout := 50 * time.Millisecond
|
||||||
|
|
||||||
|
// First sighting sets the baseline only.
|
||||||
|
if markIdle(id, 100, timeout) {
|
||||||
|
t.Fatal("slept on first sighting")
|
||||||
|
}
|
||||||
|
// Counter growth resets the idle clock.
|
||||||
|
if markIdle(id, 200, timeout) {
|
||||||
|
t.Fatal("slept while counter grew")
|
||||||
|
}
|
||||||
|
// Flat counter within the window doesn't sleep.
|
||||||
|
if markIdle(id, 200, timeout) {
|
||||||
|
t.Fatal("slept before timeout")
|
||||||
|
}
|
||||||
|
time.Sleep(60 * time.Millisecond)
|
||||||
|
if !markIdle(id, 200, timeout) {
|
||||||
|
t.Fatal("did not sleep after timeout")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarkIdleZeroCounterSleeps(t *testing.T) {
|
||||||
|
id := uuid.New()
|
||||||
|
timeout := 20 * time.Millisecond
|
||||||
|
|
||||||
|
markIdle(id, 0, timeout)
|
||||||
|
time.Sleep(30 * time.Millisecond)
|
||||||
|
if !markIdle(id, 0, timeout) {
|
||||||
|
t.Fatal("zero-traffic service never slept")
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,126 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func composeToSpecsForTest(t *testing.T, y string) ([]TemplateServiceSpec, []string, error) {
|
||||||
|
t.Helper()
|
||||||
|
var file composeFileDef
|
||||||
|
if err := yaml.Unmarshal([]byte(y), &file); err != nil {
|
||||||
|
t.Fatalf("yaml: %v", err)
|
||||||
|
}
|
||||||
|
return composeToSpecs(&file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComposeToSpecsConvertsStack(t *testing.T) {
|
||||||
|
specs, warnings, err := composeToSpecsForTest(t, `services:
|
||||||
|
web:
|
||||||
|
image: traefik/whoami:latest
|
||||||
|
ports:
|
||||||
|
- "80"
|
||||||
|
environment:
|
||||||
|
GREETING: hello-compose
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
db:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
environment:
|
||||||
|
POSTGRES_PASSWORD: pgsecret123
|
||||||
|
POSTGRES_DB: appdb
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(specs) != 2 {
|
||||||
|
t.Fatalf("want 2 specs, got %d", len(specs))
|
||||||
|
}
|
||||||
|
db, web := specs[0], specs[1]
|
||||||
|
if db.Key != "db" || db.Type != "database" || db.Runtime != "postgresql" {
|
||||||
|
t.Errorf("db spec wrong: %+v", db)
|
||||||
|
}
|
||||||
|
if db.Environment["POSTGRES_PASSWORD"] != "pgsecret123" {
|
||||||
|
t.Errorf("db env lost: %v", db.Environment)
|
||||||
|
}
|
||||||
|
if web.Key != "web" || web.Type != "web" || web.Runtime != "traefik/whoami:latest" {
|
||||||
|
t.Errorf("web spec wrong: %+v", web)
|
||||||
|
}
|
||||||
|
if web.Port != 80 {
|
||||||
|
t.Errorf("web port = %d, want 80", web.Port)
|
||||||
|
}
|
||||||
|
if len(web.DependsOn) != 1 || web.DependsOn[0] != "db" {
|
||||||
|
t.Errorf("web depends_on = %v, want [db]", web.DependsOn)
|
||||||
|
}
|
||||||
|
if len(warnings) != 0 {
|
||||||
|
t.Errorf("unexpected warnings: %v", warnings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComposeToSpecsDeterministicOrder(t *testing.T) {
|
||||||
|
y := `services:
|
||||||
|
zebra:
|
||||||
|
image: alpine
|
||||||
|
alpha:
|
||||||
|
image: alpine
|
||||||
|
`
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
specs, _, err := composeToSpecsForTest(t, y)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if specs[0].Key != "alpha" || specs[1].Key != "zebra" {
|
||||||
|
t.Fatalf("unstable order: %v", specs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComposeToSpecsDatabaseImages(t *testing.T) {
|
||||||
|
cases := map[string]string{
|
||||||
|
"postgres:16": "postgresql",
|
||||||
|
"redis:7": "redis",
|
||||||
|
"mysql:8": "mysql",
|
||||||
|
"mariadb:11": "mariadb",
|
||||||
|
"mongo:7": "mongodb",
|
||||||
|
"clickhouse:24": "clickhouse",
|
||||||
|
}
|
||||||
|
for image, want := range cases {
|
||||||
|
specs, _, err := composeToSpecsForTest(t, "services:\n db:\n image: "+image+"\n")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: %v", image, err)
|
||||||
|
}
|
||||||
|
if specs[0].Type != "database" || specs[0].Runtime != want {
|
||||||
|
t.Errorf("%s → %+v, want database/%s", image, specs[0], want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComposeToSpecsNoServices(t *testing.T) {
|
||||||
|
_, _, err := composeToSpecsForTest(t, "version: '3'\n")
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "no services") {
|
||||||
|
t.Errorf("want no-services error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComposeToSpecsWarnings(t *testing.T) {
|
||||||
|
_, warnings, err := composeToSpecsForTest(t, `services:
|
||||||
|
app:
|
||||||
|
build: .
|
||||||
|
orphan:
|
||||||
|
image: alpine
|
||||||
|
environment:
|
||||||
|
- UNSET_VAR
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
joined := strings.Join(warnings, "\n")
|
||||||
|
if !strings.Contains(joined, "build contexts are not supported") {
|
||||||
|
t.Errorf("missing build warning: %v", warnings)
|
||||||
|
}
|
||||||
|
if !strings.Contains(joined, "UNSET_VAR") {
|
||||||
|
t.Errorf("missing env warning: %v", warnings)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,202 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTopoSortServicesOrdersByDependsOn(t *testing.T) {
|
||||||
|
sorted, err := topoSortServices([]TemplateServiceSpec{
|
||||||
|
{Key: "web", DependsOn: []string{"db"}},
|
||||||
|
{Key: "db", Type: "database"},
|
||||||
|
{Key: "worker", DependsOn: []string{"web", "db"}},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("topoSortServices: %v", err)
|
||||||
|
}
|
||||||
|
if sorted[0].Key != "db" || sorted[1].Key != "web" || sorted[2].Key != "worker" {
|
||||||
|
t.Fatalf("unexpected order: %v", []string{sorted[0].Key, sorted[1].Key, sorted[2].Key})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTopoSortServicesRejectsCycle(t *testing.T) {
|
||||||
|
_, err := topoSortServices([]TemplateServiceSpec{
|
||||||
|
{Key: "a", DependsOn: []string{"b"}},
|
||||||
|
{Key: "b", DependsOn: []string{"a"}},
|
||||||
|
})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "cycle") {
|
||||||
|
t.Fatalf("expected cycle error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTopoSortServicesRejectsUnknownDep(t *testing.T) {
|
||||||
|
_, err := topoSortServices([]TemplateServiceSpec{
|
||||||
|
{Key: "a", DependsOn: []string{"ghost"}},
|
||||||
|
})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "unknown key") {
|
||||||
|
t.Fatalf("expected unknown-key error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveStaticExpr(t *testing.T) {
|
||||||
|
unresolved := []string{}
|
||||||
|
var generated bool
|
||||||
|
|
||||||
|
v := resolveStaticExpr("{{secret}}", nil, &unresolved, &generated)
|
||||||
|
if len(v) != 32 || !generated {
|
||||||
|
t.Fatalf("secret: got %q generated=%v", v, generated)
|
||||||
|
}
|
||||||
|
v = resolveStaticExpr("{{secret(8)}}", nil, &unresolved, &generated)
|
||||||
|
if len(v) != 16 {
|
||||||
|
t.Fatalf("secret(8): got %q", v)
|
||||||
|
}
|
||||||
|
v = resolveStaticExpr("port-{{random:10}}", nil, &unresolved, &generated)
|
||||||
|
if !strings.HasPrefix(v, "port-") {
|
||||||
|
t.Fatalf("random: got %q", v)
|
||||||
|
}
|
||||||
|
v = resolveStaticExpr("{{DOMAIN:-example.com}}", nil, &unresolved, &generated)
|
||||||
|
if v != "example.com" {
|
||||||
|
t.Fatalf("var default: got %q", v)
|
||||||
|
}
|
||||||
|
v = resolveStaticExpr("{{DOMAIN}}", map[string]string{"DOMAIN": "x.io"}, &unresolved, &generated)
|
||||||
|
if v != "x.io" {
|
||||||
|
t.Fatalf("var override: got %q", v)
|
||||||
|
}
|
||||||
|
v = resolveStaticExpr("{{MISSING}}", nil, &unresolved, &generated)
|
||||||
|
if v != "" || len(unresolved) == 0 || unresolved[0] != "MISSING" {
|
||||||
|
t.Fatalf("missing var: got %q unresolved=%v", v, unresolved)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveTemplateGraphDatabaseRefs(t *testing.T) {
|
||||||
|
sorted, err := topoSortServices([]TemplateServiceSpec{
|
||||||
|
{Key: "db", Type: "database", Runtime: "postgresql", Environment: map[string]string{
|
||||||
|
"POSTGRES_DB": "shop",
|
||||||
|
}},
|
||||||
|
{Key: "web", Type: "web", Runtime: "nginx", DependsOn: []string{"db"}, Environment: map[string]string{
|
||||||
|
"DATABASE_URL": "{{service.db.url}}",
|
||||||
|
"DB_HOST": "{{service.db.host}}",
|
||||||
|
"DB_PORT": "{{service.db.port}}",
|
||||||
|
"SESSION_TOKEN": "{{secret}}",
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("topo: %v", err)
|
||||||
|
}
|
||||||
|
graph := resolveTemplateGraph(sorted, nil)
|
||||||
|
if len(graph.unresolved) > 0 {
|
||||||
|
t.Fatalf("unresolved: %v", graph.unresolved)
|
||||||
|
}
|
||||||
|
web := graph.members[1]
|
||||||
|
if web.env["DB_HOST"] != "db" {
|
||||||
|
t.Fatalf("DB_HOST=%q", web.env["DB_HOST"])
|
||||||
|
}
|
||||||
|
if web.env["DB_PORT"] != "5432" {
|
||||||
|
t.Fatalf("DB_PORT=%q", web.env["DB_PORT"])
|
||||||
|
}
|
||||||
|
dbURL := web.env["DATABASE_URL"]
|
||||||
|
if !strings.HasPrefix(dbURL, "postgresql://") || !strings.Contains(dbURL, "@db:5432/shop") {
|
||||||
|
t.Fatalf("DATABASE_URL=%q", dbURL)
|
||||||
|
}
|
||||||
|
if len(web.env["SESSION_TOKEN"]) != 32 || !web.secretKeys["SESSION_TOKEN"] {
|
||||||
|
t.Fatalf("SESSION_TOKEN=%q secret=%v", web.env["SESSION_TOKEN"], web.secretKeys["SESSION_TOKEN"])
|
||||||
|
}
|
||||||
|
// Database member must carry the generated password so refs and the
|
||||||
|
// container agree.
|
||||||
|
db := graph.members[0]
|
||||||
|
if pw := db.env["POSTGRES_PASSWORD"]; len(pw) != 32 || !db.secretKeys["POSTGRES_PASSWORD"] {
|
||||||
|
t.Fatalf("POSTGRES_PASSWORD=%q secret=%v", pw, db.secretKeys["POSTGRES_PASSWORD"])
|
||||||
|
}
|
||||||
|
if !strings.Contains(dbURL, db.env["POSTGRES_PASSWORD"]) {
|
||||||
|
t.Fatalf("url %q does not carry the resolved password", dbURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveTemplateGraphServiceRefs(t *testing.T) {
|
||||||
|
sorted, err := topoSortServices([]TemplateServiceSpec{
|
||||||
|
{Key: "api", Type: "web", Runtime: "ghcr.io/x/api:1", Port: 8080},
|
||||||
|
{Key: "front", Type: "web", Runtime: "ghcr.io/x/front:1", Port: 3000, DependsOn: []string{"api"}, Environment: map[string]string{
|
||||||
|
"API_URL": "{{service.api.url}}/v2",
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("topo: %v", err)
|
||||||
|
}
|
||||||
|
graph := resolveTemplateGraph(sorted, nil)
|
||||||
|
if got := graph.members[1].env["API_URL"]; got != "http://api:8080/v2" {
|
||||||
|
t.Fatalf("API_URL=%q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveTemplateGraphUnresolvedServiceProp(t *testing.T) {
|
||||||
|
sorted, _ := topoSortServices([]TemplateServiceSpec{
|
||||||
|
{Key: "web", Type: "web", Runtime: "nginx", Environment: map[string]string{
|
||||||
|
"X": "{{service.nope.host}}",
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
graph := resolveTemplateGraph(sorted, nil)
|
||||||
|
if len(graph.unresolved) == 0 {
|
||||||
|
t.Fatal("expected unresolved ref")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateTemplateGraph(t *testing.T) {
|
||||||
|
if err := validateTemplateGraph([]TemplateServiceSpec{
|
||||||
|
{Key: "db", Type: "database", Runtime: "postgresql"},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("valid db spec rejected: %v", err)
|
||||||
|
}
|
||||||
|
if err := validateTemplateGraph([]TemplateServiceSpec{
|
||||||
|
{Key: "db", Type: "database", Runtime: "nonsense"},
|
||||||
|
}); err == nil {
|
||||||
|
t.Fatal("expected unsupported db engine error")
|
||||||
|
}
|
||||||
|
if err := validateTemplateGraph([]TemplateServiceSpec{
|
||||||
|
{Key: "web", Type: "web"},
|
||||||
|
}); err == nil {
|
||||||
|
t.Fatal("expected missing runtime error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOfficialTemplatesAllParse walks every seeded template and asserts the
|
||||||
|
// config parses, graph templates topo-sort + validate, and a dry-run resolve
|
||||||
|
// produces no unresolved refs (defaults cover all expressions).
|
||||||
|
func TestOfficialTemplatesAllParse(t *testing.T) {
|
||||||
|
for _, tpl := range SeedTemplates() {
|
||||||
|
var cfg TemplateConfig
|
||||||
|
if err := json.Unmarshal([]byte(tpl.Config), &cfg); err != nil {
|
||||||
|
t.Errorf("%s: config parse: %v", tpl.ID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(cfg.Services) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sorted, err := topoSortServices(cfg.Services)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: topo sort: %v", tpl.ID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := validateTemplateGraph(sorted); err != nil {
|
||||||
|
t.Errorf("%s: validate: %v", tpl.ID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var vars []TemplateVariable
|
||||||
|
if tpl.Variables != "" {
|
||||||
|
if err := json.Unmarshal([]byte(tpl.Variables), &vars); err != nil {
|
||||||
|
t.Errorf("%s: variables parse: %v", tpl.ID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
userVars := map[string]string{}
|
||||||
|
for _, v := range vars {
|
||||||
|
if v.Default != "" {
|
||||||
|
userVars[v.Key] = v.Default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
graph := resolveTemplateGraph(sorted, userVars)
|
||||||
|
if len(graph.unresolved) > 0 {
|
||||||
|
t.Errorf("%s: unresolved refs: %v", tpl.ID, graph.unresolved)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,186 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
// Template registry — CONTAINR_TEMPLATE_REGISTRY_URL points at another
|
||||||
|
// Containr instance (or any server answering the same template shape):
|
||||||
|
// GET {base}/api/v1/templates → {"templates": [ServiceTemplate…]}
|
||||||
|
// GET {base}/api/v1/templates/:id → {"template": …, "config": …, "variables": …}
|
||||||
|
// Registry entries merge into the catalog as source:"registry"; deploying
|
||||||
|
// one materializes it as a user template for the deployer first. The
|
||||||
|
// bundled catalog always works offline — a dead registry never blocks it.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"containr/internal/database/sqlcdb"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/sqlc-dev/pqtype"
|
||||||
|
)
|
||||||
|
|
||||||
|
const registryCacheTTL = 60 * time.Second
|
||||||
|
|
||||||
|
var registryCache struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
fetchedAt time.Time
|
||||||
|
templates []ServiceTemplate
|
||||||
|
}
|
||||||
|
|
||||||
|
func registryBaseURL() string {
|
||||||
|
base := strings.TrimRight(strings.TrimSpace(os.Getenv("CONTAINR_TEMPLATE_REGISTRY_URL")), "/")
|
||||||
|
base = strings.TrimSuffix(base, "/api/v1")
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchRegistryTemplates lists the remote catalog, cached for
|
||||||
|
// registryCacheTTL. Failures degrade to an empty list — never fatal.
|
||||||
|
func fetchRegistryTemplates(ctx context.Context) []ServiceTemplate {
|
||||||
|
if registryBaseURL() == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
registryCache.mu.Lock()
|
||||||
|
fresh := time.Since(registryCache.fetchedAt) < registryCacheTTL && registryCache.templates != nil
|
||||||
|
cached := registryCache.templates
|
||||||
|
registryCache.mu.Unlock()
|
||||||
|
if fresh {
|
||||||
|
return cached
|
||||||
|
}
|
||||||
|
|
||||||
|
var templates []ServiceTemplate
|
||||||
|
var payload struct {
|
||||||
|
Templates []ServiceTemplate `json:"templates"`
|
||||||
|
}
|
||||||
|
if err := registryGet(ctx, "/api/v1/templates", &payload); err == nil {
|
||||||
|
templates = payload.Templates
|
||||||
|
}
|
||||||
|
for i := range templates {
|
||||||
|
templates[i].Source = "registry"
|
||||||
|
}
|
||||||
|
|
||||||
|
registryCache.mu.Lock()
|
||||||
|
registryCache.fetchedAt = time.Now()
|
||||||
|
registryCache.templates = templates
|
||||||
|
registryCache.mu.Unlock()
|
||||||
|
return templates
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchRegistryTemplate fetches one remote template's full document
|
||||||
|
// (template + config + variables). nil when the registry lacks it.
|
||||||
|
func fetchRegistryTemplate(ctx context.Context, id string) *ServiceTemplate {
|
||||||
|
base := registryBaseURL()
|
||||||
|
if base == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var payload struct {
|
||||||
|
Template ServiceTemplate `json:"template"`
|
||||||
|
Config json.RawMessage `json:"config"`
|
||||||
|
Variables []TemplateVariable `json:"variables"`
|
||||||
|
}
|
||||||
|
if err := registryGet(ctx, "/api/v1/templates/"+id, &payload); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
t := payload.Template
|
||||||
|
if t.ID == "" {
|
||||||
|
t.ID = id
|
||||||
|
}
|
||||||
|
t.Source = "registry"
|
||||||
|
// The detail endpoint returns config/variables decoded; store them back
|
||||||
|
// as raw JSON so they can persist unmodified into a local copy.
|
||||||
|
if len(payload.Config) > 0 {
|
||||||
|
t.Config = string(payload.Config)
|
||||||
|
}
|
||||||
|
if payload.Variables != nil {
|
||||||
|
if raw, err := json.Marshal(payload.Variables); err == nil {
|
||||||
|
t.Variables = string(raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &t
|
||||||
|
}
|
||||||
|
|
||||||
|
func registryGet(ctx context.Context, path string, out interface{}) error {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, registryBaseURL()+path, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return errRegistry{resp.StatusCode}
|
||||||
|
}
|
||||||
|
return json.NewDecoder(resp.Body).Decode(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
type errRegistry struct{ status int }
|
||||||
|
|
||||||
|
func (e errRegistry) Error() string { return "template registry status " + http.StatusText(e.status) }
|
||||||
|
|
||||||
|
// respondRegistryTemplate answers GET /templates/:id for a registry-only
|
||||||
|
// template with the same envelope shape as local templates.
|
||||||
|
func respondRegistryTemplate(c *gin.Context, t *ServiceTemplate) {
|
||||||
|
config := json.RawMessage(t.Config)
|
||||||
|
if len(config) == 0 {
|
||||||
|
config = json.RawMessage(`{}`)
|
||||||
|
}
|
||||||
|
variables := json.RawMessage(t.Variables)
|
||||||
|
if len(variables) == 0 {
|
||||||
|
variables = json.RawMessage(`[]`)
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"template": t,
|
||||||
|
"config": config,
|
||||||
|
"variables": variables,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// materializeRegistryTemplate copies a registry template into the local
|
||||||
|
// catalog as a private template owned by the deployer, so the normal
|
||||||
|
// deploy path (and future re-deploys) work without the registry. The
|
||||||
|
// local id is deterministic per (registry, template, user) so repeat
|
||||||
|
// deploys reuse the same copy instead of accumulating duplicates.
|
||||||
|
func materializeRegistryTemplate(c *gin.Context, q *sqlcdb.Queries, userID, templateID string) (sqlcdb.ServiceTemplate, error) {
|
||||||
|
ownerUUID, err := uuid.Parse(userID)
|
||||||
|
if err != nil {
|
||||||
|
return sqlcdb.ServiceTemplate{}, err
|
||||||
|
}
|
||||||
|
sum := sha256.Sum256([]byte(registryBaseURL() + "|" + templateID + "|" + userID))
|
||||||
|
localID := "reg-" + hex.EncodeToString(sum[:8])
|
||||||
|
if existing, err := q.GetServiceTemplateByID(c.Request.Context(), localID); err == nil {
|
||||||
|
return existing, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rt := fetchRegistryTemplate(c.Request.Context(), templateID)
|
||||||
|
if rt == nil {
|
||||||
|
return sqlcdb.ServiceTemplate{}, sql.ErrNoRows
|
||||||
|
}
|
||||||
|
variables := pqtype.NullRawMessage{}
|
||||||
|
if rt.Variables != "" {
|
||||||
|
variables = pqtype.NullRawMessage{RawMessage: json.RawMessage(rt.Variables), Valid: true}
|
||||||
|
}
|
||||||
|
if err := q.CreateUserTemplate(c.Request.Context(), sqlcdb.CreateUserTemplateParams{
|
||||||
|
ID: localID,
|
||||||
|
Name: rt.Name,
|
||||||
|
Description: nullableText(rt.Description),
|
||||||
|
Category: rt.Category,
|
||||||
|
Logo: nullableText(rt.Logo),
|
||||||
|
Config: json.RawMessage(rt.Config),
|
||||||
|
Variables: variables,
|
||||||
|
OwnerID: uuid.NullUUID{UUID: ownerUUID, Valid: true},
|
||||||
|
IsPublic: false,
|
||||||
|
}); err != nil {
|
||||||
|
return sqlcdb.ServiceTemplate{}, err
|
||||||
|
}
|
||||||
|
return q.GetServiceTemplateByID(c.Request.Context(), localID)
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@ package api
|
|||||||
import (
|
import (
|
||||||
"containr/internal/database"
|
"containr/internal/database"
|
||||||
"containr/internal/database/sqlcdb"
|
"containr/internal/database/sqlcdb"
|
||||||
|
"containr/internal/deployment"
|
||||||
|
"containr/internal/secrets"
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -19,21 +21,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ServiceTemplate struct {
|
type ServiceTemplate struct {
|
||||||
ID string `json:"id" db:"id"`
|
ID string `json:"id" db:"id"`
|
||||||
Name string `json:"name" db:"name"`
|
Name string `json:"name" db:"name"`
|
||||||
Description string `json:"description" db:"description"`
|
Description string `json:"description" db:"description"`
|
||||||
Category string `json:"category" db:"category"`
|
Category string `json:"category" db:"category"`
|
||||||
Logo string `json:"logo" db:"logo"`
|
Logo string `json:"logo" db:"logo"`
|
||||||
Config string `json:"config" db:"config"`
|
Config string `json:"config" db:"config"`
|
||||||
Variables string `json:"variables" db:"variables"`
|
Variables string `json:"variables" db:"variables"`
|
||||||
IsOfficial bool `json:"is_official" db:"is_official"`
|
IsOfficial bool `json:"is_official" db:"is_official"`
|
||||||
OwnerID string `json:"owner_id,omitempty" db:"owner_id"`
|
OwnerID string `json:"owner_id,omitempty" db:"owner_id"`
|
||||||
IsPublic bool `json:"is_public" db:"is_public"`
|
IsPublic bool `json:"is_public" db:"is_public"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
// Source marks catalog origin: "" (local/official) or "registry".
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
Source string `json:"source,omitempty" db:"-"`
|
||||||
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TemplateConfig struct {
|
type TemplateConfig struct {
|
||||||
|
Version int `json:"version,omitempty"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Runtime string `json:"runtime"`
|
Runtime string `json:"runtime"`
|
||||||
BuildCommand string `json:"build_command"`
|
BuildCommand string `json:"build_command"`
|
||||||
@@ -43,6 +48,9 @@ type TemplateConfig struct {
|
|||||||
Environment map[string]string `json:"environment"`
|
Environment map[string]string `json:"environment"`
|
||||||
Dockerfile string `json:"dockerfile,omitempty"`
|
Dockerfile string `json:"dockerfile,omitempty"`
|
||||||
NixpacksConfig map[string]string `json:"nixpacks_config,omitempty"`
|
NixpacksConfig map[string]string `json:"nixpacks_config,omitempty"`
|
||||||
|
// Services makes this a v2 graph template. When non-empty the flat
|
||||||
|
// fields above are ignored and deployTemplateGraph handles the deploy.
|
||||||
|
Services []TemplateServiceSpec `json:"services,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TemplateVariable struct {
|
type TemplateVariable struct {
|
||||||
@@ -91,10 +99,24 @@ func handleGetTemplates(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
templates := make([]ServiceTemplate, 0, len(templateRows))
|
templates := make([]ServiceTemplate, 0, len(templateRows))
|
||||||
|
localIDs := make(map[string]bool, len(templateRows))
|
||||||
for _, row := range templateRows {
|
for _, row := range templateRows {
|
||||||
|
localIDs[row.ID] = true
|
||||||
templates = append(templates, mapSQLCTemplate(row))
|
templates = append(templates, mapSQLCTemplate(row))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Merge the configured remote registry (CONTAINR_TEMPLATE_REGISTRY_URL).
|
||||||
|
// Registry ids never shadow local templates.
|
||||||
|
for _, t := range fetchRegistryTemplates(ctx) {
|
||||||
|
if localIDs[t.ID] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if category != "" && t.Category != category {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
templates = append(templates, t)
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"templates": templates})
|
c.JSON(http.StatusOK, gin.H{"templates": templates})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,6 +128,11 @@ func handleGetTemplate(c *gin.Context) {
|
|||||||
row, err := queries.GetServiceTemplateByID(c.Request.Context(), templateID)
|
row, err := queries.GetServiceTemplateByID(c.Request.Context(), templateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
// Registry fallback — remote templates materialize read-only.
|
||||||
|
if rt := fetchRegistryTemplate(c.Request.Context(), templateID); rt != nil {
|
||||||
|
respondRegistryTemplate(c, rt)
|
||||||
|
return
|
||||||
|
}
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
|
c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -147,7 +174,7 @@ func handleCreateFromTemplate(c *gin.Context) {
|
|||||||
|
|
||||||
var req struct {
|
var req struct {
|
||||||
ProjectID string `json:"project_id" binding:"required"`
|
ProjectID string `json:"project_id" binding:"required"`
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name"`
|
||||||
Plan string `json:"plan,omitempty"`
|
Plan string `json:"plan,omitempty"`
|
||||||
Region string `json:"region,omitempty"`
|
Region string `json:"region,omitempty"`
|
||||||
Variables map[string]string `json:"variables"`
|
Variables map[string]string `json:"variables"`
|
||||||
@@ -160,10 +187,6 @@ func handleCreateFromTemplate(c *gin.Context) {
|
|||||||
|
|
||||||
req.ProjectID = strings.TrimSpace(req.ProjectID)
|
req.ProjectID = strings.TrimSpace(req.ProjectID)
|
||||||
req.Name = strings.TrimSpace(req.Name)
|
req.Name = strings.TrimSpace(req.Name)
|
||||||
if req.Name == "" {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
projectID, err := uuid.Parse(req.ProjectID)
|
projectID, err := uuid.Parse(req.ProjectID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -188,11 +211,18 @@ func handleCreateFromTemplate(c *gin.Context) {
|
|||||||
templateRow, err := queries.GetServiceTemplateByID(ctx, templateID)
|
templateRow, err := queries.GetServiceTemplateByID(ctx, templateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
|
// Registry template — materialize a local copy for the
|
||||||
|
// deployer, then continue down the normal deploy path.
|
||||||
|
row, mErr := materializeRegistryTemplate(c, queries, userID, templateID)
|
||||||
|
if mErr != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
templateRow = row
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch template"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch template"})
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template := mapSQLCTemplate(templateRow)
|
template := mapSQLCTemplate(templateRow)
|
||||||
@@ -213,6 +243,51 @@ func handleCreateFromTemplate(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(config.Services) > 0 {
|
||||||
|
var engine *deployment.DeploymentEngine
|
||||||
|
if v, exists := c.Get("deployment_engine"); exists && v != nil {
|
||||||
|
engine, _ = v.(*deployment.DeploymentEngine)
|
||||||
|
}
|
||||||
|
var dbHandler *DatabaseHandler
|
||||||
|
if v, exists := c.Get("database_handler"); exists {
|
||||||
|
dbHandler, _ = v.(*DatabaseHandler)
|
||||||
|
}
|
||||||
|
|
||||||
|
userVars := map[string]string{}
|
||||||
|
for _, v := range templateVars {
|
||||||
|
if override, ok := req.Variables[v.Key]; ok {
|
||||||
|
userVars[v.Key] = override
|
||||||
|
} else if v.Default != "" {
|
||||||
|
userVars[v.Key] = v.Default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k, v := range req.Variables {
|
||||||
|
userVars[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
created, fail := deployTemplateGraph(c, db, engine, dbHandler, projectID, userID,
|
||||||
|
config.Services, templateVars, userVars, req.Plan, req.Region, "template:"+templateID)
|
||||||
|
if fail != nil {
|
||||||
|
status := http.StatusBadRequest
|
||||||
|
if (*fail)["code"] == "CONFLICT" {
|
||||||
|
status = http.StatusConflict
|
||||||
|
}
|
||||||
|
c.JSON(status, *fail)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
|
"resource": "stack",
|
||||||
|
"created": created,
|
||||||
|
"message": fmt.Sprintf("Deployed %d resources from template %s", len(created), template.Name),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Name == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
serviceType, err := normalizeTemplateServiceType(config.Type)
|
serviceType, err := normalizeTemplateServiceType(config.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
@@ -351,11 +426,15 @@ func handleCreateFromTemplate(c *gin.Context) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stored := value
|
||||||
|
if secretKeys[key] {
|
||||||
|
stored = secrets.Encrypt(value)
|
||||||
|
}
|
||||||
err = txQueries.UpsertEnvironmentVariable(ctx, sqlcdb.UpsertEnvironmentVariableParams{
|
err = txQueries.UpsertEnvironmentVariable(ctx, sqlcdb.UpsertEnvironmentVariableParams{
|
||||||
ID: uuid.New(),
|
ID: uuid.New(),
|
||||||
ServiceID: serviceID,
|
ServiceID: serviceID,
|
||||||
Key: key,
|
Key: key,
|
||||||
Value: value,
|
Value: stored,
|
||||||
IsSecret: sql.NullBool{Bool: secretKeys[key], Valid: true},
|
IsSecret: sql.NullBool{Bool: secretKeys[key], Valid: true},
|
||||||
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
||||||
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
||||||
@@ -927,6 +1006,102 @@ func SeedTemplates() []ServiceTemplate {
|
|||||||
Variables: `[]`,
|
Variables: `[]`,
|
||||||
IsOfficial: true,
|
IsOfficial: true,
|
||||||
},
|
},
|
||||||
|
// Multi-service stacks — v2 graph configs. Database members become
|
||||||
|
// managed databases attached to the project network under their key.
|
||||||
|
{
|
||||||
|
ID: "tpl-wordpress-stack",
|
||||||
|
Name: "WordPress + MySQL",
|
||||||
|
Description: "WordPress with a managed MySQL database on a private project network",
|
||||||
|
Category: "stack",
|
||||||
|
Logo: "https://cdn.simpleicons.org/wordpress",
|
||||||
|
Config: `{"version":2,"services":[
|
||||||
|
{"key":"db","type":"database","runtime":"mysql","environment":{"MYSQL_DATABASE":"wordpress"}},
|
||||||
|
{"key":"web","name":"wordpress","type":"web","runtime":"wordpress:6-apache","port":80,"health_check":"/wp-login.php","depends_on":["db"],
|
||||||
|
"environment":{"WORDPRESS_DB_HOST":"{{service.db.host}}","WORDPRESS_DB_USER":"{{service.db.user}}","WORDPRESS_DB_PASSWORD":"{{service.db.password}}","WORDPRESS_DB_NAME":"{{service.db.database}}"}}]}`,
|
||||||
|
Variables: `[]`,
|
||||||
|
IsOfficial: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "tpl-n8n-stack",
|
||||||
|
Name: "n8n + PostgreSQL",
|
||||||
|
Description: "n8n workflow automation backed by a managed PostgreSQL database",
|
||||||
|
Category: "stack",
|
||||||
|
Logo: "https://cdn.simpleicons.org/n8n",
|
||||||
|
Config: `{"version":2,"services":[
|
||||||
|
{"key":"db","type":"database","runtime":"postgresql"},
|
||||||
|
{"key":"n8n","type":"web","runtime":"n8nio/n8n:latest","port":5678,"health_check":"/healthz","depends_on":["db"],
|
||||||
|
"environment":{"DB_TYPE":"postgresdb","DB_POSTGRESDB_HOST":"{{service.db.host}}","DB_POSTGRESDB_PORT":"{{service.db.port}}","DB_POSTGRESDB_DATABASE":"{{service.db.database}}","DB_POSTGRESDB_USER":"{{service.db.user}}","DB_POSTGRESDB_PASSWORD":"{{service.db.password}}","N8N_ENCRYPTION_KEY":"{{secret}}"}}]}`,
|
||||||
|
Variables: `[]`,
|
||||||
|
IsOfficial: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "tpl-ghost-stack",
|
||||||
|
Name: "Ghost + MySQL",
|
||||||
|
Description: "Ghost publishing platform backed by a managed MySQL database",
|
||||||
|
Category: "stack",
|
||||||
|
Logo: "https://cdn.simpleicons.org/ghost",
|
||||||
|
Config: `{"version":2,"services":[
|
||||||
|
{"key":"db","type":"database","runtime":"mysql","environment":{"MYSQL_DATABASE":"ghost"}},
|
||||||
|
{"key":"web","name":"ghost","type":"web","runtime":"ghost:5-alpine","port":2368,"depends_on":["db"],
|
||||||
|
"environment":{"url":"{{APP_URL:-http://localhost:2368}}","database__client":"mysql","database__connection__host":"{{service.db.host}}","database__connection__user":"{{service.db.user}}","database__connection__password":"{{service.db.password}}","database__connection__database":"{{service.db.database}}"}}]}`,
|
||||||
|
Variables: `[{"key":"APP_URL","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||||
|
IsOfficial: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "tpl-gitea-stack",
|
||||||
|
Name: "Gitea + PostgreSQL",
|
||||||
|
Description: "Self-hosted Git service backed by a managed PostgreSQL database",
|
||||||
|
Category: "stack",
|
||||||
|
Logo: "https://cdn.simpleicons.org/gitea",
|
||||||
|
Config: `{"version":2,"services":[
|
||||||
|
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"gitea"}},
|
||||||
|
{"key":"web","name":"gitea","type":"web","runtime":"gitea/gitea:1","port":3000,"health_check":"/api/healthz","depends_on":["db"],
|
||||||
|
"environment":{"GITEA__database__DB_TYPE":"postgres","GITEA__database__HOST":"{{service.db.host}}:{{service.db.port}}","GITEA__database__NAME":"{{service.db.database}}","GITEA__database__USER":"{{service.db.user}}","GITEA__database__PASSWD":"{{service.db.password}}","GITEA__security__SECRET_KEY":"{{secret}}","GITEA__security__INTERNAL_TOKEN":"{{secret(64)}}"}}]}`,
|
||||||
|
Variables: `[]`,
|
||||||
|
IsOfficial: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "tpl-nextcloud-stack",
|
||||||
|
Name: "Nextcloud + PostgreSQL + Redis",
|
||||||
|
Description: "Nextcloud with a managed PostgreSQL database and Redis cache",
|
||||||
|
Category: "stack",
|
||||||
|
Logo: "https://cdn.simpleicons.org/nextcloud",
|
||||||
|
Config: `{"version":2,"services":[
|
||||||
|
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"nextcloud"}},
|
||||||
|
{"key":"cache","type":"database","runtime":"redis"},
|
||||||
|
{"key":"web","name":"nextcloud","type":"web","runtime":"nextcloud:30-apache","port":80,"health_check":"/status.php","depends_on":["db","cache"],
|
||||||
|
"environment":{"POSTGRES_HOST":"{{service.db.host}}","POSTGRES_DB":"{{service.db.database}}","POSTGRES_USER":"{{service.db.user}}","POSTGRES_PASSWORD":"{{service.db.password}}","REDIS_HOST":"{{service.cache.host}}","REDIS_HOST_PASSWORD":"{{service.cache.password}}","NEXTCLOUD_ADMIN_USER":"admin","NEXTCLOUD_ADMIN_PASSWORD":"{{secret}}","NEXTCLOUD_TRUSTED_DOMAINS":"{{APP_DOMAIN:-localhost}}"}}]}`,
|
||||||
|
Variables: `[{"key":"APP_DOMAIN","label":"Trusted domain","default":"","required":false,"secret":false}]`,
|
||||||
|
IsOfficial: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "tpl-paperless-stack",
|
||||||
|
Name: "Paperless-ngx + PostgreSQL + Redis",
|
||||||
|
Description: "Document management with managed PostgreSQL and Redis",
|
||||||
|
Category: "stack",
|
||||||
|
Logo: "https://cdn.simpleicons.org/paperlessngx",
|
||||||
|
Config: `{"version":2,"services":[
|
||||||
|
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"paperless"}},
|
||||||
|
{"key":"cache","type":"database","runtime":"redis"},
|
||||||
|
{"key":"web","name":"paperless","type":"web","runtime":"ghcr.io/paperless-ngx/paperless-ngx:latest","port":8000,"depends_on":["db","cache"],
|
||||||
|
"environment":{"PAPERLESS_DBHOST":"{{service.db.host}}","PAPERLESS_DBPORT":"{{service.db.port}}","PAPERLESS_DBNAME":"{{service.db.database}}","PAPERLESS_DBUSER":"{{service.db.user}}","PAPERLESS_DBPASS":"{{service.db.password}}","PAPERLESS_REDIS":"{{service.cache.url}}","PAPERLESS_ADMIN_USER":"admin","PAPERLESS_ADMIN_PASSWORD":"{{secret}}","PAPERLESS_SECRET_KEY":"{{secret(48)}}","PAPERLESS_URL":"{{APP_URL:-http://localhost:8000}}"}}]}`,
|
||||||
|
Variables: `[{"key":"APP_URL","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||||
|
IsOfficial: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "tpl-plausible-stack",
|
||||||
|
Name: "Plausible + PostgreSQL + ClickHouse",
|
||||||
|
Description: "Privacy-friendly analytics with managed PostgreSQL and ClickHouse",
|
||||||
|
Category: "stack",
|
||||||
|
Logo: "https://cdn.simpleicons.org/plausibleanalytics",
|
||||||
|
Config: `{"version":2,"services":[
|
||||||
|
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"plausible"}},
|
||||||
|
{"key":"events","type":"database","runtime":"clickhouse","environment":{"CLICKHOUSE_DB":"plausible"}},
|
||||||
|
{"key":"web","name":"plausible","type":"web","runtime":"ghcr.io/plausible/community-edition:v2","port":8000,"health_check":"/api/health","depends_on":["db","events"],
|
||||||
|
"environment":{"BASE_URL":"{{APP_URL:-http://localhost:8000}}","SECRET_KEY_BASE":"{{secret(64)}}","DATABASE_URL":"{{service.db.url}}","CLICKHOUSE_DATABASE_URL":"http://{{service.events.host}}:{{service.events.port}}/{{service.events.database}}","TOTP_VAULT_KEY":"{{secret(32)}}"}}]}`,
|
||||||
|
Variables: `[{"key":"APP_URL","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||||
|
IsOfficial: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return templates
|
return templates
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,212 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/docker"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Interactive terminal: a WebSocket bridged to a `docker exec` pty in the
|
||||||
|
// service's container. Protocol is JSON text frames both ways:
|
||||||
|
// client → server: {"type":"stdin","data":"..."} | {"type":"resize","cols":N,"rows":N}
|
||||||
|
// server → client: {"type":"stdout","data":"..."} | {"type":"exit","code":N} | {"type":"error","message":"..."}
|
||||||
|
// Shell preference: $SHELL, then bash, then sh.
|
||||||
|
|
||||||
|
type terminalFrame struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Data string `json:"data,omitempty"`
|
||||||
|
Cols uint `json:"cols,omitempty"`
|
||||||
|
Rows uint `json:"rows,omitempty"`
|
||||||
|
Code int `json:"code,omitempty"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleServiceTerminal(c *gin.Context) {
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
serviceID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid service id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
projectID, found := serviceProjectID(db, serviceID)
|
||||||
|
if !found {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "service not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, allowed := projectManageAccess(c, db, projectID); !allowed {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "service not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dockerClient, _ := c.Get("docker_client")
|
||||||
|
client, ok := dockerClient.(*docker.Client)
|
||||||
|
if !ok || client == nil {
|
||||||
|
respondError(c, http.StatusServiceUnavailable, "DOCKER_UNAVAILABLE", "Docker is not configured")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
containers, err := client.ListContainersFiltered(ctx, filters.NewArgs(
|
||||||
|
filters.Arg("label", "containr.service="+serviceID.String()),
|
||||||
|
), false)
|
||||||
|
if err != nil || len(containers) == 0 {
|
||||||
|
respondError(c, http.StatusConflict, "NO_CONTAINER", "service has no running container")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Prefer a running container. With several matches, deterministically
|
||||||
|
// pick the first `containr-<service>-<n>` replica by name — that is the
|
||||||
|
// actual workload naming; auxiliary containers get ignored.
|
||||||
|
target := ""
|
||||||
|
sort.Slice(containers, func(i, j int) bool { return containers[i].Names[0] < containers[j].Names[0] })
|
||||||
|
for _, ctr := range containers {
|
||||||
|
if ctr.State == "running" && strings.HasPrefix(ctr.Names[0], "/containr-") {
|
||||||
|
target = ctr.ID
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if target == "" {
|
||||||
|
for _, ctr := range containers {
|
||||||
|
if ctr.State == "running" {
|
||||||
|
target = ctr.ID
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if target == "" {
|
||||||
|
target = containers[0].ID
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
send := func(f terminalFrame) bool {
|
||||||
|
return conn.WriteJSON(f) == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pick a shell that actually exists — distroless images may have none.
|
||||||
|
// `command -v` can't probe without a shell, so start each candidate and
|
||||||
|
// watch for the OCI "no such file" failure.
|
||||||
|
var stream types.HijackedResponse
|
||||||
|
var execID string
|
||||||
|
attached := false
|
||||||
|
for _, candidate := range []string{"/bin/bash", "/bin/sh", "sh"} {
|
||||||
|
exec, err := client.ExecCreate(ctx, target, docker.ExecConfig{
|
||||||
|
Cmd: []string{candidate},
|
||||||
|
AttachStdin: true,
|
||||||
|
AttachStdout: true,
|
||||||
|
AttachStderr: true,
|
||||||
|
Tty: true,
|
||||||
|
Env: []string{"TERM=xterm-256color", "COLORTERM=truecolor"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s, err := client.ExecAttach(ctx, exec.ID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if execStartFailed(ctx, client, exec.ID, s) {
|
||||||
|
s.Close()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
stream, execID, attached = s, exec.ID, true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if !attached {
|
||||||
|
send(terminalFrame{Type: "error", Message: "no shell available in this container image"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer stream.Close()
|
||||||
|
|
||||||
|
// The client sends a resize frame with its real size right after
|
||||||
|
// connecting; start at a sane default meanwhile.
|
||||||
|
_ = client.ExecResize(ctx, execID, 80, 24)
|
||||||
|
|
||||||
|
var wsWrite sync.Mutex
|
||||||
|
writeJSON := func(f terminalFrame) {
|
||||||
|
wsWrite.Lock()
|
||||||
|
defer wsWrite.Unlock()
|
||||||
|
_ = conn.WriteJSON(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Container → client.
|
||||||
|
go func() {
|
||||||
|
buf := make([]byte, 8192)
|
||||||
|
for {
|
||||||
|
n, err := stream.Reader.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
writeJSON(terminalFrame{Type: "stdout", Data: string(buf[:n])})
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
log.Printf("terminal: exec stream ended: %v", err)
|
||||||
|
}
|
||||||
|
inspect, ierr := client.ExecInspect(context.Background(), execID)
|
||||||
|
code := 0
|
||||||
|
if ierr == nil {
|
||||||
|
code = inspect.ExitCode
|
||||||
|
}
|
||||||
|
writeJSON(terminalFrame{Type: "exit", Code: code})
|
||||||
|
_ = conn.WriteControl(websocket.CloseMessage,
|
||||||
|
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "session ended"),
|
||||||
|
time.Now().Add(time.Second))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Client → container.
|
||||||
|
for {
|
||||||
|
_, raw, err := conn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var f terminalFrame
|
||||||
|
if err := json.Unmarshal(raw, &f); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch f.Type {
|
||||||
|
case "stdin":
|
||||||
|
if _, err := stream.Conn.Write([]byte(f.Data)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "resize":
|
||||||
|
_ = client.ExecResize(ctx, execID, f.Cols, f.Rows)
|
||||||
|
case "ping":
|
||||||
|
writeJSON(terminalFrame{Type: "pong"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// execStartFailed reports whether an attached exec failed to launch
|
||||||
|
// (missing binary → non-zero exit, not running). The OCI failure takes a
|
||||||
|
// moment to surface, so a short grace period elapses before judging.
|
||||||
|
func execStartFailed(ctx context.Context, client *docker.Client, execID string, _ types.HijackedResponse) bool {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return true
|
||||||
|
case <-time.After(500 * time.Millisecond):
|
||||||
|
}
|
||||||
|
inspect, err := client.ExecInspect(ctx, execID)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return !inspect.Running && inspect.ExitCode != 0
|
||||||
|
}
|
||||||
@@ -218,12 +218,14 @@ func handleUpdateVariables(c *gin.Context) {
|
|||||||
`SELECT id, project_id, name, COALESCE(image, ''), COALESCE(command, ''),
|
`SELECT id, project_id, name, COALESCE(image, ''), COALESCE(command, ''),
|
||||||
COALESCE(replicas, 1), COALESCE(port, 0), COALESCE(domain, ''),
|
COALESCE(replicas, 1), COALESCE(port, 0), COALESCE(domain, ''),
|
||||||
COALESCE(healthcheck_path, ''), COALESCE(restart_policy, 'unless-stopped'),
|
COALESCE(healthcheck_path, ''), COALESCE(restart_policy, 'unless-stopped'),
|
||||||
COALESCE(cpu, ''), COALESCE(memory, '')
|
COALESCE(cpu, ''), COALESCE(memory, ''),
|
||||||
|
COALESCE(builder, 'auto'), COALESCE(cpu_reserve, ''), COALESCE(memory_reserve, '')
|
||||||
FROM services WHERE id = $1`,
|
FROM services WHERE id = $1`,
|
||||||
serviceID,
|
serviceID,
|
||||||
).Scan(&svc.ID, &svc.ProjectID, &svc.Name, &svc.Image, &svc.Command,
|
).Scan(&svc.ID, &svc.ProjectID, &svc.Name, &svc.Image, &svc.Command,
|
||||||
&svc.Replicas, &svc.Port, &svc.Domain, &svc.HealthCheckPath,
|
&svc.Replicas, &svc.Port, &svc.Domain, &svc.HealthCheckPath,
|
||||||
&svc.RestartPolicy, &svc.CPU, &svc.Memory)
|
&svc.RestartPolicy, &svc.CPU, &svc.Memory,
|
||||||
|
&svc.Builder, &svc.CPUReserve, &svc.MemoryReserve)
|
||||||
resp["redeploy_queued"] = svcErr == nil && enqueueServiceRedeploy(c, db.(*database.DB), svc)
|
resp["redeploy_queued"] = svcErr == nil && enqueueServiceRedeploy(c, db.(*database.DB), svc)
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, resp)
|
c.JSON(http.StatusOK, resp)
|
||||||
|
|||||||
@@ -0,0 +1,543 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"containr/internal/database"
|
||||||
|
"containr/internal/database/sqlcdb"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/sqlc-dev/pqtype"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Outbound webhooks: every audited action (LogAudit) emits an event named
|
||||||
|
// "resource.action" (e.g. "service.deploy"). Webhooks subscribe with an
|
||||||
|
// event list supporting "service.*" and "*" wildcards. Deliveries are
|
||||||
|
// HMAC-signed and retried up to 3 times with backoff; each attempt lands
|
||||||
|
// in webhook_deliveries.
|
||||||
|
|
||||||
|
var (
|
||||||
|
webhookQueue = make(chan webhookJob, 512)
|
||||||
|
webhookWorkers sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
type webhookJob struct {
|
||||||
|
Webhook sqlcdb.OutboundWebhook
|
||||||
|
Event string
|
||||||
|
Payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type webhookEventPayload struct {
|
||||||
|
Event string `json:"event"`
|
||||||
|
Resource string `json:"resource"`
|
||||||
|
ResourceID string `json:"resource_id"`
|
||||||
|
Action string `json:"action"`
|
||||||
|
UserID string `json:"user_id,omitempty"`
|
||||||
|
Details map[string]interface{} `json:"details,omitempty"`
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// emitWebhookEvent fans an audited action out to all enabled webhooks whose
|
||||||
|
// event list matches. Called from LogAudit — fire-and-forget.
|
||||||
|
func emitWebhookEvent(userID, resource, resourceID, action string, details map[string]interface{}) {
|
||||||
|
db := GetAuditDB()
|
||||||
|
if db == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Actions that already carry a resource prefix ("service.clone") pass
|
||||||
|
// through unchanged; bare actions ("create") get resource-qualified.
|
||||||
|
event := action
|
||||||
|
if !strings.Contains(action, ".") {
|
||||||
|
event = resource + "." + action
|
||||||
|
}
|
||||||
|
|
||||||
|
queries := sqlcdb.New(db.DB)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
hooks, err := queries.ListEnabledOutboundWebhooks(ctx)
|
||||||
|
if err != nil || len(hooks) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, err := json.Marshal(webhookEventPayload{
|
||||||
|
Event: event, Resource: resource, ResourceID: resourceID,
|
||||||
|
Action: action, UserID: userID, Details: details, Timestamp: time.Now().UTC(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uid, uidErr := uuid.Parse(strings.TrimSpace(userID))
|
||||||
|
for _, hook := range hooks {
|
||||||
|
if !webhookEventMatches(hook.Events, event) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Webhooks only receive events for their owner — unless subscribed to "*"
|
||||||
|
// is deliberate admin reach; owner scoping keeps per-user isolation.
|
||||||
|
if uidErr != nil || hook.UserID != uid {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case webhookQueue <- webhookJob{Webhook: hook, Event: event, Payload: payload}:
|
||||||
|
default:
|
||||||
|
// Queue full: drop rather than block the request path.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func webhookEventMatches(events json.RawMessage, event string) bool {
|
||||||
|
var subs []string
|
||||||
|
if err := json.Unmarshal(events, &subs); err != nil || len(subs) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, sub := range subs {
|
||||||
|
sub = strings.TrimSpace(sub)
|
||||||
|
if sub == "*" || sub == event {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(sub, ".*") && strings.HasPrefix(event, strings.TrimSuffix(sub, "*")) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartWebhookDispatcher launches delivery workers; call once at boot.
|
||||||
|
func StartWebhookDispatcher() {
|
||||||
|
webhookWorkers.Do(func() {
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
go webhookDeliveryWorker()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func webhookDeliveryWorker() {
|
||||||
|
for job := range webhookQueue {
|
||||||
|
deliverWebhook(job)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookHTTPClient = &http.Client{Timeout: 10 * time.Second}
|
||||||
|
|
||||||
|
func deliverWebhook(job webhookJob) {
|
||||||
|
db := GetAuditDB()
|
||||||
|
if db == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
queries := sqlcdb.New(db.DB)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
deliveryID := uuid.New()
|
||||||
|
_ = queries.CreateWebhookDelivery(ctx, sqlcdb.CreateWebhookDeliveryParams{
|
||||||
|
ID: deliveryID,
|
||||||
|
WebhookID: job.Webhook.ID,
|
||||||
|
Event: job.Event,
|
||||||
|
Payload: pqtype.NullRawMessage{RawMessage: job.Payload, Valid: true},
|
||||||
|
Status: "pending",
|
||||||
|
})
|
||||||
|
|
||||||
|
var lastStatus string = "failed"
|
||||||
|
var respStatus sql.NullInt32
|
||||||
|
var respBody sql.NullString
|
||||||
|
var durationMs sql.NullInt32
|
||||||
|
var deliveredAt sql.NullTime
|
||||||
|
|
||||||
|
for attempt := int32(1); attempt <= 3; attempt++ {
|
||||||
|
start := time.Now()
|
||||||
|
status, code, body := postWebhook(job, deliveryID, attempt)
|
||||||
|
durationMs = sql.NullInt32{Int32: int32(time.Since(start).Milliseconds()), Valid: true}
|
||||||
|
respStatus = code
|
||||||
|
respBody = body
|
||||||
|
if status == "success" {
|
||||||
|
lastStatus = "success"
|
||||||
|
deliveredAt = sql.NullTime{Time: time.Now().UTC(), Valid: true}
|
||||||
|
_ = queries.UpdateWebhookDeliveryResult(ctx, sqlcdb.UpdateWebhookDeliveryResultParams{
|
||||||
|
ID: deliveryID,
|
||||||
|
Status: lastStatus,
|
||||||
|
ResponseStatus: respStatus,
|
||||||
|
ResponseBody: respBody,
|
||||||
|
Attempts: attempt,
|
||||||
|
DurationMs: durationMs,
|
||||||
|
DeliveredAt: deliveredAt,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if attempt < 3 {
|
||||||
|
time.Sleep(time.Duration(attempt*attempt) * 2 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = queries.UpdateWebhookDeliveryResult(ctx, sqlcdb.UpdateWebhookDeliveryResultParams{
|
||||||
|
ID: deliveryID,
|
||||||
|
Status: lastStatus,
|
||||||
|
ResponseStatus: respStatus,
|
||||||
|
ResponseBody: respBody,
|
||||||
|
Attempts: 3,
|
||||||
|
DurationMs: durationMs,
|
||||||
|
DeliveredAt: deliveredAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func postWebhook(job webhookJob, deliveryID uuid.UUID, attempt int32) (string, sql.NullInt32, sql.NullString) {
|
||||||
|
req, err := http.NewRequest(http.MethodPost, job.Webhook.Url, bytes.NewReader(job.Payload))
|
||||||
|
if err != nil {
|
||||||
|
return "failed", sql.NullInt32{}, sql.NullString{String: err.Error(), Valid: true}
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("User-Agent", "Containr-Webhook/1.0")
|
||||||
|
req.Header.Set("X-Containr-Event", job.Event)
|
||||||
|
req.Header.Set("X-Containr-Delivery", deliveryID.String())
|
||||||
|
if job.Webhook.Secret != "" {
|
||||||
|
mac := hmac.New(sha256.New, []byte(job.Webhook.Secret))
|
||||||
|
mac.Write(job.Payload)
|
||||||
|
req.Header.Set("X-Containr-Signature", "sha256="+hex.EncodeToString(mac.Sum(nil)))
|
||||||
|
}
|
||||||
|
if job.Webhook.Headers.Valid {
|
||||||
|
var custom map[string]string
|
||||||
|
if err := json.Unmarshal(job.Webhook.Headers.RawMessage, &custom); err == nil {
|
||||||
|
for k, v := range custom {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := webhookHTTPClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "failed", sql.NullInt32{}, sql.NullString{String: err.Error(), Valid: true}
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||||
|
code := sql.NullInt32{Int32: int32(resp.StatusCode), Valid: true}
|
||||||
|
bodyStr := sql.NullString{String: string(body), Valid: len(body) > 0}
|
||||||
|
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||||
|
return "success", code, bodyStr
|
||||||
|
}
|
||||||
|
return "failed", code, bodyStr
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- HTTP API ----------
|
||||||
|
|
||||||
|
type outboundWebhookResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Secret string `json:"secret,omitempty"`
|
||||||
|
Events []string `json:"events"`
|
||||||
|
Headers map[string]string `json:"headers,omitempty"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func outboundWebhookJSON(row sqlcdb.OutboundWebhook, revealSecret bool) outboundWebhookResponse {
|
||||||
|
var events []string
|
||||||
|
_ = json.Unmarshal(row.Events, &events)
|
||||||
|
if events == nil {
|
||||||
|
events = []string{}
|
||||||
|
}
|
||||||
|
var headers map[string]string
|
||||||
|
if row.Headers.Valid {
|
||||||
|
_ = json.Unmarshal(row.Headers.RawMessage, &headers)
|
||||||
|
}
|
||||||
|
resp := outboundWebhookResponse{
|
||||||
|
ID: row.ID.String(),
|
||||||
|
Name: row.Name,
|
||||||
|
URL: row.Url,
|
||||||
|
Events: events,
|
||||||
|
Headers: headers,
|
||||||
|
Enabled: row.Enabled,
|
||||||
|
}
|
||||||
|
if revealSecret {
|
||||||
|
resp.Secret = row.Secret
|
||||||
|
}
|
||||||
|
if row.CreatedAt.Valid {
|
||||||
|
resp.CreatedAt = row.CreatedAt.Time
|
||||||
|
}
|
||||||
|
if row.UpdatedAt.Valid {
|
||||||
|
resp.UpdatedAt = row.UpdatedAt.Time
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
func validWebhookURL(raw string) error {
|
||||||
|
u, err := url.Parse(strings.TrimSpace(raw))
|
||||||
|
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
|
||||||
|
return errors.New("url must be a valid http(s) URL")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type webhookMutationRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Secret *string `json:"secret"`
|
||||||
|
Events []string `json:"events"`
|
||||||
|
Headers map[string]string `json:"headers"`
|
||||||
|
Enabled *bool `json:"enabled"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleListOutboundWebhooks(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserUUID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
rows, err := sqlcdb.New(db.DB).ListOutboundWebhooksByUser(c.Request.Context(), userID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list webhooks"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out := make([]outboundWebhookResponse, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
out = append(out, outboundWebhookJSON(row, false))
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"webhooks": out})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCreateOutboundWebhook(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserUUID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req webhookMutationRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Name = strings.TrimSpace(req.Name)
|
||||||
|
if req.Name == "" {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "name is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := validWebhookURL(req.URL); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(req.Events) == 0 {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "at least one event subscription is required (e.g. \"service.*\" or \"*\")")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
secret := ""
|
||||||
|
if req.Secret != nil {
|
||||||
|
secret = strings.TrimSpace(*req.Secret)
|
||||||
|
}
|
||||||
|
if secret == "" {
|
||||||
|
buf := make([]byte, 24)
|
||||||
|
_, _ = rand.Read(buf)
|
||||||
|
secret = hex.EncodeToString(buf)
|
||||||
|
}
|
||||||
|
eventsJSON, _ := json.Marshal(req.Events)
|
||||||
|
var headersJSON pqtype.NullRawMessage
|
||||||
|
if len(req.Headers) > 0 {
|
||||||
|
if raw, err := json.Marshal(req.Headers); err == nil {
|
||||||
|
headersJSON = pqtype.NullRawMessage{RawMessage: raw, Valid: true}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enabled := true
|
||||||
|
if req.Enabled != nil {
|
||||||
|
enabled = *req.Enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
id := uuid.New()
|
||||||
|
err := sqlcdb.New(db.DB).CreateOutboundWebhook(c.Request.Context(), sqlcdb.CreateOutboundWebhookParams{
|
||||||
|
ID: id,
|
||||||
|
UserID: userID,
|
||||||
|
Name: req.Name,
|
||||||
|
Url: strings.TrimSpace(req.URL),
|
||||||
|
Secret: secret,
|
||||||
|
Events: eventsJSON,
|
||||||
|
Headers: headersJSON,
|
||||||
|
Enabled: enabled,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create webhook"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
LogAuditWithRequest(c, "webhook", id.String(), "create", map[string]interface{}{"name": req.Name})
|
||||||
|
c.JSON(http.StatusCreated, outboundWebhookJSON(sqlcdb.OutboundWebhook{
|
||||||
|
ID: id, Name: req.Name, Url: strings.TrimSpace(req.URL), Secret: secret,
|
||||||
|
Events: eventsJSON, Headers: headersJSON, Enabled: enabled,
|
||||||
|
CreatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||||
|
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||||
|
}, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUpdateOutboundWebhook(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserUUID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
webhookID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
queries := sqlcdb.New(db.DB)
|
||||||
|
row, err := queries.GetOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.GetOutboundWebhookByIDAndUserParams{
|
||||||
|
ID: webhookID, UserID: userID,
|
||||||
|
})
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "Webhook not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load webhook"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req webhookMutationRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name := row.Name
|
||||||
|
if req.Name != "" {
|
||||||
|
name = strings.TrimSpace(req.Name)
|
||||||
|
}
|
||||||
|
targetURL := row.Url
|
||||||
|
if req.URL != "" {
|
||||||
|
if err := validWebhookURL(req.URL); err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetURL = strings.TrimSpace(req.URL)
|
||||||
|
}
|
||||||
|
secret := row.Secret
|
||||||
|
if req.Secret != nil {
|
||||||
|
secret = strings.TrimSpace(*req.Secret)
|
||||||
|
}
|
||||||
|
eventsJSON := row.Events
|
||||||
|
if req.Events != nil {
|
||||||
|
if len(req.Events) == 0 {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "at least one event subscription is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
eventsJSON, _ = json.Marshal(req.Events)
|
||||||
|
}
|
||||||
|
headersJSON := row.Headers
|
||||||
|
if req.Headers != nil {
|
||||||
|
if raw, err := json.Marshal(req.Headers); err == nil {
|
||||||
|
headersJSON = pqtype.NullRawMessage{RawMessage: raw, Valid: true}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enabled := row.Enabled
|
||||||
|
if req.Enabled != nil {
|
||||||
|
enabled = *req.Enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
err = queries.UpdateOutboundWebhook(c.Request.Context(), sqlcdb.UpdateOutboundWebhookParams{
|
||||||
|
ID: webhookID, UserID: userID,
|
||||||
|
Name: name, Url: targetURL, Secret: secret,
|
||||||
|
Events: eventsJSON, Headers: headersJSON, Enabled: enabled,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update webhook"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
LogAuditWithRequest(c, "webhook", webhookID.String(), "update", map[string]interface{}{"name": name})
|
||||||
|
row.Name, row.Url, row.Secret, row.Events, row.Headers, row.Enabled = name, targetURL, secret, eventsJSON, headersJSON, enabled
|
||||||
|
c.JSON(http.StatusOK, outboundWebhookJSON(row, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleDeleteOutboundWebhook(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserUUID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
webhookID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
err = sqlcdb.New(db.DB).DeleteOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.DeleteOutboundWebhookByIDAndUserParams{
|
||||||
|
ID: webhookID, UserID: userID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete webhook"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
LogAuditWithRequest(c, "webhook", webhookID.String(), "delete", map[string]interface{}{})
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Webhook deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleListOutboundWebhookDeliveries(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserUUID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
webhookID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
queries := sqlcdb.New(db.DB)
|
||||||
|
if _, err := queries.GetOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.GetOutboundWebhookByIDAndUserParams{
|
||||||
|
ID: webhookID, UserID: userID,
|
||||||
|
}); errors.Is(err, sql.ErrNoRows) {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "Webhook not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rows, err := queries.ListWebhookDeliveriesByWebhook(c.Request.Context(), webhookID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list deliveries"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"deliveries": rows})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleTestOutboundWebhook(c *gin.Context) {
|
||||||
|
userID, ok := requireAuthenticatedUserUUID(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
webhookID, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db := c.MustGet("db").(*database.DB)
|
||||||
|
row, err := sqlcdb.New(db.DB).GetOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.GetOutboundWebhookByIDAndUserParams{
|
||||||
|
ID: webhookID, UserID: userID,
|
||||||
|
})
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
respondError(c, http.StatusNotFound, "NOT_FOUND", "Webhook not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load webhook"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !row.Enabled {
|
||||||
|
respondError(c, http.StatusBadRequest, "VALIDATION", "Webhook is disabled")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
payload, _ := json.Marshal(webhookEventPayload{
|
||||||
|
Event: "ping", Resource: "webhook", ResourceID: webhookID.String(),
|
||||||
|
Action: "ping", UserID: userID.String(), Timestamp: time.Now().UTC(),
|
||||||
|
})
|
||||||
|
select {
|
||||||
|
case webhookQueue <- webhookJob{Webhook: row, Event: "ping", Payload: payload}:
|
||||||
|
c.JSON(http.StatusAccepted, gin.H{"message": "Test delivery queued"})
|
||||||
|
default:
|
||||||
|
respondError(c, http.StatusServiceUnavailable, "UNAVAILABLE", "Delivery queue is full")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWebhookEventMatches(t *testing.T) {
|
||||||
|
subs := func(list ...string) json.RawMessage {
|
||||||
|
raw, _ := json.Marshal(list)
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
cases := []struct {
|
||||||
|
events json.RawMessage
|
||||||
|
event string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{subs("service.*"), "service.deploy", true},
|
||||||
|
{subs("service.*"), "database.create", false},
|
||||||
|
{subs("*"), "service.deploy", true},
|
||||||
|
{subs("deployment.fail", "service.*"), "deployment.fail", true},
|
||||||
|
{subs("deployment.fail"), "deployment.success", false},
|
||||||
|
{subs(" ping "), "ping", true},
|
||||||
|
{subs(), "service.deploy", false},
|
||||||
|
{json.RawMessage(`"not-a-list"`), "service.deploy", false},
|
||||||
|
{json.RawMessage(`invalid`), "service.deploy", false},
|
||||||
|
}
|
||||||
|
for i, tc := range cases {
|
||||||
|
if got := webhookEventMatches(tc.events, tc.event); got != tc.want {
|
||||||
|
t.Errorf("case %d: matches(%s, %q) = %v, want %v", i, tc.events, tc.event, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -211,8 +211,12 @@ func (d *DockerfileBuilder) Build(ctx context.Context, req *types.BuildRequest)
|
|||||||
// Build Docker image
|
// Build Docker image
|
||||||
imageName := fmt.Sprintf("%s:%s", req.ImageName, req.ImageTag)
|
imageName := fmt.Sprintf("%s:%s", req.ImageName, req.ImageTag)
|
||||||
|
|
||||||
// Create build context tar
|
// createBuildContext wants the Dockerfile contents, not its path.
|
||||||
buildCtx, err := createBuildContext(req.SourcePath, optimizedDockerfile)
|
dockerfileContent, err := os.ReadFile(optimizedDockerfile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read Dockerfile: %w", err)
|
||||||
|
}
|
||||||
|
buildCtx, err := createBuildContext(req.SourcePath, string(dockerfileContent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create build context: %w", err)
|
return nil, fmt.Errorf("failed to create build context: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type BuildManager struct {
|
|||||||
railpackBuilder *RailpackBuilder
|
railpackBuilder *RailpackBuilder
|
||||||
nixpacksBuilder *NixpacksBuilder
|
nixpacksBuilder *NixpacksBuilder
|
||||||
dockerfileBuilder *DockerfileBuilder
|
dockerfileBuilder *DockerfileBuilder
|
||||||
|
staticBuilder *StaticBuilder
|
||||||
dockerClient *docker.Client
|
dockerClient *docker.Client
|
||||||
cacheManager *CacheManager
|
cacheManager *CacheManager
|
||||||
parallelBuilder *ParallelBuilder
|
parallelBuilder *ParallelBuilder
|
||||||
@@ -28,6 +29,7 @@ const (
|
|||||||
BuildTypeRailpack BuildType = "railpack"
|
BuildTypeRailpack BuildType = "railpack"
|
||||||
BuildTypeNixpacks BuildType = "nixpacks"
|
BuildTypeNixpacks BuildType = "nixpacks"
|
||||||
BuildTypeDockerfile BuildType = "dockerfile"
|
BuildTypeDockerfile BuildType = "dockerfile"
|
||||||
|
BuildTypeStatic BuildType = "static"
|
||||||
BuildTypePrebuilt BuildType = "prebuilt"
|
BuildTypePrebuilt BuildType = "prebuilt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,6 +41,7 @@ func NewBuildManager(workDir string, dockerClient *docker.Client) *BuildManager
|
|||||||
railpackBuilder: NewRailpackBuilder(workDir, dockerClient),
|
railpackBuilder: NewRailpackBuilder(workDir, dockerClient),
|
||||||
nixpacksBuilder: NewNixpacksBuilder(workDir, dockerClient),
|
nixpacksBuilder: NewNixpacksBuilder(workDir, dockerClient),
|
||||||
dockerfileBuilder: NewDockerfileBuilder(workDir, dockerClient),
|
dockerfileBuilder: NewDockerfileBuilder(workDir, dockerClient),
|
||||||
|
staticBuilder: NewStaticBuilder(dockerClient),
|
||||||
dockerClient: dockerClient,
|
dockerClient: dockerClient,
|
||||||
cacheManager: cacheManager,
|
cacheManager: cacheManager,
|
||||||
parallelBuilder: parallelBuilder,
|
parallelBuilder: parallelBuilder,
|
||||||
@@ -80,6 +83,8 @@ func (bm *BuildManager) Build(ctx context.Context, req *types.BuildRequest) (*ty
|
|||||||
return bm.nixpacksBuilder.Build(ctx, req)
|
return bm.nixpacksBuilder.Build(ctx, req)
|
||||||
case BuildTypeDockerfile:
|
case BuildTypeDockerfile:
|
||||||
return bm.dockerfileBuilder.Build(ctx, req)
|
return bm.dockerfileBuilder.Build(ctx, req)
|
||||||
|
case BuildTypeStatic:
|
||||||
|
return bm.staticBuilder.Build(ctx, req)
|
||||||
case BuildTypePrebuilt:
|
case BuildTypePrebuilt:
|
||||||
return bm.buildPrebuilt(ctx, req)
|
return bm.buildPrebuilt(ctx, req)
|
||||||
default:
|
default:
|
||||||
@@ -95,7 +100,7 @@ func (bm *BuildManager) buildPrebuilt(ctx context.Context, req *types.BuildReque
|
|||||||
|
|
||||||
// Pull the prebuilt image — must drain the pull stream or the daemon
|
// Pull the prebuilt image — must drain the pull stream or the daemon
|
||||||
// aborts it and the tag below races a missing image.
|
// aborts it and the tag below races a missing image.
|
||||||
auth := registry.AuthConfig{}
|
auth := registry.AuthConfig{Username: req.PullUsername, Password: req.PullPassword}
|
||||||
if err := bm.dockerClient.PullImageWait(ctx, req.PrebuiltImage, auth); err != nil {
|
if err := bm.dockerClient.PullImageWait(ctx, req.PrebuiltImage, auth); err != nil {
|
||||||
return nil, fmt.Errorf("failed to pull prebuilt image: %w", err)
|
return nil, fmt.Errorf("failed to pull prebuilt image: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"containr/internal/docker"
|
||||||
|
"containr/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StaticBuilder produces an nginx-served image from a repo's built assets.
|
||||||
|
// STATIC_BUILD_CMD runs inside a node build stage; STATIC_DIR names the
|
||||||
|
// output directory copied into the nginx image. Both are overridable via
|
||||||
|
// build args so repos with non-node toolchains can swap the build image too.
|
||||||
|
type StaticBuilder struct {
|
||||||
|
dockerClient *docker.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStaticBuilder(dockerClient *docker.Client) *StaticBuilder {
|
||||||
|
return &StaticBuilder{dockerClient: dockerClient}
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticDockerfile = `ARG BUILD_IMAGE=node:20-alpine
|
||||||
|
FROM ${BUILD_IMAGE} AS assets
|
||||||
|
WORKDIR /app
|
||||||
|
COPY . .
|
||||||
|
ARG STATIC_BUILD_CMD
|
||||||
|
RUN sh -c "${STATIC_BUILD_CMD}"
|
||||||
|
|
||||||
|
FROM nginx:alpine
|
||||||
|
ARG STATIC_DIR=dist
|
||||||
|
COPY --from=assets /app/${STATIC_DIR} /usr/share/nginx/html
|
||||||
|
EXPOSE 80
|
||||||
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
|
`
|
||||||
|
|
||||||
|
func (s *StaticBuilder) Build(ctx context.Context, req *types.BuildRequest) (*types.BuildResponse, error) {
|
||||||
|
buildCmd := req.BuildCommand
|
||||||
|
if buildCmd == "" {
|
||||||
|
buildCmd = "npm ci && npm run build"
|
||||||
|
}
|
||||||
|
staticDir := req.BuildArgs["STATIC_DIR"]
|
||||||
|
if staticDir == "" {
|
||||||
|
staticDir = "dist"
|
||||||
|
}
|
||||||
|
|
||||||
|
buildArgs := map[string]*string{}
|
||||||
|
for k, v := range req.BuildArgs {
|
||||||
|
v := v
|
||||||
|
buildArgs[k] = &v
|
||||||
|
}
|
||||||
|
for k, v := range req.Environment {
|
||||||
|
v := v
|
||||||
|
buildArgs[k] = &v
|
||||||
|
}
|
||||||
|
buildArgs["STATIC_BUILD_CMD"] = &buildCmd
|
||||||
|
buildArgs["STATIC_DIR"] = &staticDir
|
||||||
|
|
||||||
|
imageName := fmt.Sprintf("%s:%s", req.ImageName, req.ImageTag)
|
||||||
|
buildCtx, err := createBuildContext(req.SourcePath, staticDockerfile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create build context: %w", err)
|
||||||
|
}
|
||||||
|
defer buildCtx.Close()
|
||||||
|
|
||||||
|
if _, err := s.dockerClient.BuildImage(ctx, buildCtx, docker.BuildOptions{
|
||||||
|
Dockerfile: "Dockerfile",
|
||||||
|
Tags: []string{imageName},
|
||||||
|
BuildArgs: buildArgs,
|
||||||
|
NoCache: req.NoCache,
|
||||||
|
Remove: true,
|
||||||
|
}); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to build static image: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.RegistryURL != "" {
|
||||||
|
full := fmt.Sprintf("%s/%s", req.RegistryURL, imageName)
|
||||||
|
if err := s.dockerClient.PushImage(ctx, imageName, req.RegistryURL); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to push image: %w", err)
|
||||||
|
}
|
||||||
|
imageName = full
|
||||||
|
}
|
||||||
|
|
||||||
|
imageInfo, err := s.dockerClient.GetImageInfo(ctx, imageName)
|
||||||
|
if err != nil {
|
||||||
|
imageInfo = &docker.ImageInfo{Size: 0}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &types.BuildResponse{
|
||||||
|
ImageName: imageName,
|
||||||
|
ImageTag: req.ImageTag,
|
||||||
|
Size: imageInfo.Size,
|
||||||
|
Digest: imageInfo.Digest,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
activitySeverity string
|
||||||
|
activityCategory string
|
||||||
|
activityResource string
|
||||||
|
activityProject string
|
||||||
|
activityLimit int
|
||||||
|
)
|
||||||
|
|
||||||
|
// ActivityCmd shows the enriched activity feed (audit events with
|
||||||
|
// severity/category/label).
|
||||||
|
var ActivityCmd = &cobra.Command{
|
||||||
|
Use: "activity",
|
||||||
|
Short: "Activity feed — enriched audit events",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
params := []string{fmt.Sprintf("limit=%d", activityLimit)}
|
||||||
|
if activitySeverity != "" {
|
||||||
|
params = append(params, "severity="+activitySeverity)
|
||||||
|
}
|
||||||
|
if activityCategory != "" {
|
||||||
|
params = append(params, "category="+activityCategory)
|
||||||
|
}
|
||||||
|
if activityResource != "" {
|
||||||
|
params = append(params, "resource="+activityResource)
|
||||||
|
}
|
||||||
|
if activityProject != "" {
|
||||||
|
params = append(params, "project_id="+activityProject)
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/activity?"+strings.Join(params, "&"), nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "activity")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, e := range items {
|
||||||
|
rows = append(rows, []string{
|
||||||
|
str(e, "severity"), str(e, "category"), str(e, "label"),
|
||||||
|
str(e, "user_email"), relTime(e, "created_at"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"SEVERITY", "CATEGORY", "EVENT", "ACTOR", "WHEN"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
ActivityCmd.Flags().StringVar(&activitySeverity, "severity", "", "filter: info|success|warning|error")
|
||||||
|
ActivityCmd.Flags().StringVar(&activityCategory, "category", "", "filter by category")
|
||||||
|
ActivityCmd.Flags().StringVar(&activityResource, "resource", "", "filter by resource type")
|
||||||
|
ActivityCmd.Flags().StringVar(&activityProject, "project", "", "filter by project id")
|
||||||
|
ActivityCmd.Flags().IntVar(&activityLimit, "limit", 50, "max events")
|
||||||
|
}
|
||||||
@@ -56,6 +56,84 @@ var adminUsersCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var adminImpersonateCmd = &cobra.Command{
|
||||||
|
Use: "impersonate <user-id>",
|
||||||
|
Short: "Mint a 15-minute token acting as a user (audit-logged)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/admin/users/"+args[0]+"/impersonate", map[string]interface{}{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
fmt.Fprintln(cmd.ErrOrStderr(), "\nUse it with: CONTAINR_TOKEN=<token> containr <command>")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var adminGitHubAppCmd = &cobra.Command{
|
||||||
|
Use: "github-app",
|
||||||
|
Short: "GitHub App self-provisioning",
|
||||||
|
}
|
||||||
|
|
||||||
|
var adminGitHubAppStatusCmd = &cobra.Command{
|
||||||
|
Use: "status",
|
||||||
|
Short: "Show GitHub App provisioning status",
|
||||||
|
RunE: simpleGet("/admin/git/github-app"),
|
||||||
|
}
|
||||||
|
|
||||||
|
var adminGitHubAppManifestCmd = &cobra.Command{
|
||||||
|
Use: "manifest",
|
||||||
|
Short: "Build the manifest to create the instance GitHub App",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{"base_url": ghAppBaseURL}
|
||||||
|
if ghAppOrg != "" {
|
||||||
|
req["organization"] = ghAppOrg
|
||||||
|
}
|
||||||
|
if ghAppName != "" {
|
||||||
|
req["name"] = ghAppName
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/admin/git/github-app/manifest", req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var adminGitHubAppConvertCmd = &cobra.Command{
|
||||||
|
Use: "convert <code>",
|
||||||
|
Short: "Exchange the manifest callback code for app credentials",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/admin/git/github-app/convert", map[string]interface{}{"code": args[0]})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ghAppBaseURL string
|
||||||
|
ghAppOrg string
|
||||||
|
ghAppName string
|
||||||
|
)
|
||||||
|
|
||||||
var adminSettingsCmd = &cobra.Command{
|
var adminSettingsCmd = &cobra.Command{
|
||||||
Use: "settings",
|
Use: "settings",
|
||||||
Short: "Show platform settings",
|
Short: "Show platform settings",
|
||||||
@@ -353,7 +431,13 @@ func init() {
|
|||||||
securityScanCmd.Flags().String("type", "configuration", "dependency|configuration|comprehensive")
|
securityScanCmd.Flags().String("type", "configuration", "dependency|configuration|comprehensive")
|
||||||
securityScanCmd.Flags().String("service", "", "limit scan to one service")
|
securityScanCmd.Flags().String("service", "", "limit scan to one service")
|
||||||
|
|
||||||
AdminCmd.AddCommand(adminOverviewCmd, adminUsersCmd, adminSettingsCmd, adminAuditCmd)
|
adminGitHubAppManifestCmd.Flags().StringVar(&ghAppBaseURL, "base-url", "", "public base URL of this instance (required)")
|
||||||
|
adminGitHubAppManifestCmd.Flags().StringVar(&ghAppOrg, "org", "", "create the app under this organization")
|
||||||
|
adminGitHubAppManifestCmd.Flags().StringVar(&ghAppName, "name", "", "app name (defaults to product name + date)")
|
||||||
|
adminGitHubAppManifestCmd.MarkFlagRequired("base-url")
|
||||||
|
|
||||||
|
adminGitHubAppCmd.AddCommand(adminGitHubAppStatusCmd, adminGitHubAppManifestCmd, adminGitHubAppConvertCmd)
|
||||||
|
AdminCmd.AddCommand(adminOverviewCmd, adminUsersCmd, adminSettingsCmd, adminAuditCmd, adminImpersonateCmd, adminGitHubAppCmd)
|
||||||
GatewayCmd.AddCommand(gatewayServicesCmd, gatewayKeysCmd, gatewayAnalyticsCmd)
|
GatewayCmd.AddCommand(gatewayServicesCmd, gatewayKeysCmd, gatewayAnalyticsCmd)
|
||||||
HACmd.AddCommand(haStatusCmd, haAlertsCmd, haHealthCmd, haPoliciesCmd, haEnableCmd, haDisableCmd, haFailoverCmd)
|
HACmd.AddCommand(haStatusCmd, haAlertsCmd, haHealthCmd, haPoliciesCmd, haEnableCmd, haDisableCmd, haFailoverCmd)
|
||||||
ScalingCmd.AddCommand(scalingStatusCmd, scalingPoliciesCmd, scalingServicesCmd, scalingScaleCmd)
|
ScalingCmd.AddCommand(scalingStatusCmd, scalingPoliciesCmd, scalingServicesCmd, scalingScaleCmd)
|
||||||
|
|||||||
@@ -147,5 +147,21 @@ func runStatus(cmd *cobra.Command, args []string) error {
|
|||||||
if p.IsAdmin {
|
if p.IsAdmin {
|
||||||
fmt.Println("Role: platform admin")
|
fmt.Println("Role: platform admin")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var bannersResp struct {
|
||||||
|
Banners []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Level string `json:"level"`
|
||||||
|
} `json:"banners"`
|
||||||
|
}
|
||||||
|
if err := client.DoJSON("GET", "/banners/active", nil, &bannersResp); err == nil {
|
||||||
|
for _, b := range bannersResp.Banners {
|
||||||
|
fmt.Printf("[%s] %s\n", b.Level, b.Title)
|
||||||
|
if b.Body != "" {
|
||||||
|
fmt.Printf(" %s\n", b.Body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BackupTargetsCmd manages S3-compatible backup destinations.
|
||||||
|
var BackupTargetsCmd = &cobra.Command{
|
||||||
|
Use: "backup-targets",
|
||||||
|
Aliases: []string{"backup-target", "targets"},
|
||||||
|
Short: "Manage S3-compatible backup targets",
|
||||||
|
}
|
||||||
|
|
||||||
|
var btListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List backup targets",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/backup-targets", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "backup_targets")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, t := range items {
|
||||||
|
creds := "no"
|
||||||
|
if str(t, "has_credentials") == "true" {
|
||||||
|
creds = "yes"
|
||||||
|
}
|
||||||
|
rows = append(rows, []string{str(t, "id"), str(t, "name"), str(t, "endpoint"), str(t, "bucket"), creds})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"ID", "NAME", "ENDPOINT", "BUCKET", "CREDS"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var btAddCmd = &cobra.Command{
|
||||||
|
Use: "add <name>",
|
||||||
|
Short: "Register an S3-compatible backup target (probed before storing)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{"name": args[0]}
|
||||||
|
for _, f := range []string{"endpoint", "bucket", "region", "prefix", "access-key", "secret-key"} {
|
||||||
|
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||||
|
key := f
|
||||||
|
switch f {
|
||||||
|
case "access-key":
|
||||||
|
key = "access_key"
|
||||||
|
case "secret-key":
|
||||||
|
key = "secret_key"
|
||||||
|
}
|
||||||
|
body[key] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("no-tls") {
|
||||||
|
v, _ := cmd.Flags().GetBool("no-tls")
|
||||||
|
body["use_tls"] = !v
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/backup-targets", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var btUpdateCmd = &cobra.Command{
|
||||||
|
Use: "update <id>",
|
||||||
|
Short: "Update a backup target (re-probed; empty keys keep stored credentials)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
// endpoint/bucket are required by the API — fall back to stored values.
|
||||||
|
for _, f := range []string{"name", "endpoint", "bucket", "region", "prefix", "access-key", "secret-key"} {
|
||||||
|
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||||
|
key := f
|
||||||
|
switch f {
|
||||||
|
case "access-key":
|
||||||
|
key = "access_key"
|
||||||
|
case "secret-key":
|
||||||
|
key = "secret_key"
|
||||||
|
}
|
||||||
|
body[key] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, hasEndpoint := body["endpoint"]; !hasEndpoint {
|
||||||
|
data, err := c.Do("GET", "/backup-targets", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "backup_targets")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, t := range items {
|
||||||
|
if str(t, "id") == args[0] {
|
||||||
|
body["endpoint"] = str(t, "endpoint")
|
||||||
|
if _, ok := body["bucket"]; !ok {
|
||||||
|
body["bucket"] = str(t, "bucket")
|
||||||
|
}
|
||||||
|
if _, ok := body["name"]; !ok {
|
||||||
|
body["name"] = str(t, "name")
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("no-tls") {
|
||||||
|
v, _ := cmd.Flags().GetBool("no-tls")
|
||||||
|
body["use_tls"] = !v
|
||||||
|
}
|
||||||
|
data, err := c.Do("PUT", "/backup-targets/"+args[0], body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var btRemoveCmd = &cobra.Command{
|
||||||
|
Use: "remove <id>",
|
||||||
|
Short: "Delete a backup target (fails while databases still point at it)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !Confirm(fmt.Sprintf("Delete backup target %s?", args[0])) {
|
||||||
|
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
if _, err := c.Do("DELETE", "/backup-targets/"+args[0], nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
return PrintJSON(map[string]string{"status": "deleted", "id": args[0]})
|
||||||
|
}
|
||||||
|
fmt.Printf("Deleted backup target %s\n", args[0])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var btTestCmd = &cobra.Command{
|
||||||
|
Use: "test <id>",
|
||||||
|
Short: "Re-probe a backup target's bucket access",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/backup-targets/"+args[0]+"/test", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m, _ := unwrapObject(data)
|
||||||
|
if str(m, "ok") == "false" {
|
||||||
|
return &APIError{Message: "probe failed: " + str(m, "error"), ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fmt.Printf("Target ok (%sms)\n", str(m, "latency_ms"))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
btAddCmd.Flags().String("endpoint", "", "S3 endpoint host[:port] (e.g. s3.amazonaws.com, garage.local:3900)")
|
||||||
|
btAddCmd.Flags().String("bucket", "", "bucket name")
|
||||||
|
btAddCmd.Flags().String("region", "", "region (optional for most S3-compatible stores)")
|
||||||
|
btAddCmd.Flags().String("prefix", "", "key prefix for archives")
|
||||||
|
btAddCmd.Flags().String("access-key", "", "access key (stored encrypted)")
|
||||||
|
btAddCmd.Flags().String("secret-key", "", "secret key (stored encrypted)")
|
||||||
|
btAddCmd.Flags().Bool("no-tls", false, "use plain HTTP")
|
||||||
|
btUpdateCmd.Flags().String("name", "", "new name")
|
||||||
|
btUpdateCmd.Flags().String("endpoint", "", "S3 endpoint host[:port]")
|
||||||
|
btUpdateCmd.Flags().String("bucket", "", "bucket name")
|
||||||
|
btUpdateCmd.Flags().String("region", "", "region")
|
||||||
|
btUpdateCmd.Flags().String("prefix", "", "key prefix")
|
||||||
|
btUpdateCmd.Flags().String("access-key", "", "access key")
|
||||||
|
btUpdateCmd.Flags().String("secret-key", "", "secret key")
|
||||||
|
btUpdateCmd.Flags().Bool("no-tls", false, "use plain HTTP")
|
||||||
|
BackupTargetsCmd.AddCommand(btListCmd, btAddCmd, btUpdateCmd, btRemoveCmd, btTestCmd)
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bannerTitle string
|
||||||
|
bannerLevel string
|
||||||
|
bannerBody string
|
||||||
|
bannerActive bool
|
||||||
|
)
|
||||||
|
|
||||||
|
// BannersCmd manages instance-wide announcement banners (admin).
|
||||||
|
var BannersCmd = &cobra.Command{
|
||||||
|
Use: "banners",
|
||||||
|
Short: "Manage announcement banners (admin)",
|
||||||
|
}
|
||||||
|
|
||||||
|
var bannerListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List all banners",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/admin/banners", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "banners")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, b := range items {
|
||||||
|
rows = append(rows, []string{
|
||||||
|
str(b, "id"), str(b, "title"), str(b, "level"), str(b, "active"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"ID", "TITLE", "LEVEL", "ACTIVE"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var bannerCreateCmd = &cobra.Command{
|
||||||
|
Use: "create <title>",
|
||||||
|
Short: "Create a banner (--body --level info|success|warning|error)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{"title": args[0]}
|
||||||
|
if cmd.Flags().Changed("body") {
|
||||||
|
req["body"] = bannerBody
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("level") {
|
||||||
|
req["level"] = bannerLevel
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/admin/banners", req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var bannerUpdateCmd = &cobra.Command{
|
||||||
|
Use: "update <id>",
|
||||||
|
Short: "Update a banner (--title --body --level --active/--inactive)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{}
|
||||||
|
if cmd.Flags().Changed("title") {
|
||||||
|
req["title"] = bannerTitle
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("body") {
|
||||||
|
req["body"] = bannerBody
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("level") {
|
||||||
|
req["level"] = bannerLevel
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("active") {
|
||||||
|
req["active"] = bannerActive
|
||||||
|
}
|
||||||
|
if len(req) == 0 {
|
||||||
|
return fmt.Errorf("nothing to update — pass --title/--body/--level/--active")
|
||||||
|
}
|
||||||
|
data, err := c.Do("PATCH", "/admin/banners/"+args[0], req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var bannerDeleteCmd = &cobra.Command{
|
||||||
|
Use: "delete <id>",
|
||||||
|
Short: "Delete a banner",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if !Confirm("Delete banner " + args[0] + "?") {
|
||||||
|
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = c.Do("DELETE", "/admin/banners/"+args[0], nil)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
bannerCreateCmd.Flags().StringVar(&bannerBody, "body", "", "banner body text")
|
||||||
|
bannerCreateCmd.Flags().StringVar(&bannerLevel, "level", "info", "info|success|warning|error")
|
||||||
|
bannerUpdateCmd.Flags().StringVar(&bannerTitle, "title", "", "new title")
|
||||||
|
bannerUpdateCmd.Flags().StringVar(&bannerBody, "body", "", "new body")
|
||||||
|
bannerUpdateCmd.Flags().StringVar(&bannerLevel, "level", "", "info|success|warning|error")
|
||||||
|
bannerUpdateCmd.Flags().BoolVar(&bannerActive, "active", true, "set banner active/inactive")
|
||||||
|
BannersCmd.AddCommand(bannerListCmd, bannerCreateCmd, bannerUpdateCmd, bannerDeleteCmd)
|
||||||
|
}
|
||||||
@@ -156,6 +156,48 @@ func (c *Client) Do(method, path string, body interface{}) ([]byte, error) {
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DoRaw sends a non-JSON request body (file uploads) and returns the response.
|
||||||
|
func (c *Client) DoRaw(method, path string, body io.Reader, contentType string) ([]byte, error) {
|
||||||
|
req, err := http.NewRequest(method, c.BaseURL+path, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if c.Token != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+c.Token)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", contentType)
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
hc := c.HTTPClient
|
||||||
|
if hc == nil {
|
||||||
|
hc = &http.Client{Timeout: 5 * time.Minute}
|
||||||
|
}
|
||||||
|
resp, err := hc.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, &APIError{Status: 0, Message: fmt.Sprintf("cannot reach %s: %v", c.BaseURL, err), ExitCode: ExitUnavailable}
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
data, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode >= 400 {
|
||||||
|
msg := strings.TrimSpace(string(data))
|
||||||
|
var envelope struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
Code string `json:"code"`
|
||||||
|
}
|
||||||
|
if json.Unmarshal(data, &envelope) == nil && envelope.Error != "" {
|
||||||
|
msg = envelope.Error
|
||||||
|
if envelope.Code != "" {
|
||||||
|
msg = envelope.Code + ": " + msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, &APIError{Status: resp.StatusCode, Message: msg, ExitCode: exitCodeFor(resp.StatusCode)}
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
// DoJSON performs a request and unmarshals the response into out.
|
// DoJSON performs a request and unmarshals the response into out.
|
||||||
func (c *Client) DoJSON(method, path string, body interface{}, out interface{}) error {
|
func (c *Client) DoJSON(method, path string, body interface{}, out interface{}) error {
|
||||||
data, err := c.Do(method, path, body)
|
data, err := c.Do(method, path, body)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -79,6 +80,9 @@ var dbCreateCmd = &cobra.Command{
|
|||||||
body[key] = v
|
body[key] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if v, _ := cmd.Flags().GetBool("public"); v {
|
||||||
|
body["public_port"] = true
|
||||||
|
}
|
||||||
data, err := c.Do("POST", "/databases", body)
|
data, err := c.Do("POST", "/databases", body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -88,6 +92,43 @@ var dbCreateCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dbUpdateCmd = &cobra.Command{
|
||||||
|
Use: "update <id>",
|
||||||
|
Short: "Update a database (--public/--public=false toggles the exposed host port)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
if cmd.Flags().Changed("public") {
|
||||||
|
v, _ := cmd.Flags().GetBool("public")
|
||||||
|
body["public_port"] = v
|
||||||
|
}
|
||||||
|
if v, _ := cmd.Flags().GetString("name"); v != "" {
|
||||||
|
body["name"] = v
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("backup-schedule") {
|
||||||
|
v, _ := cmd.Flags().GetString("backup-schedule")
|
||||||
|
body["backup_schedule"] = v
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("backup-target") {
|
||||||
|
v, _ := cmd.Flags().GetString("backup-target")
|
||||||
|
body["backup_target_id"] = v
|
||||||
|
}
|
||||||
|
if len(body) == 0 {
|
||||||
|
return &APIError{Message: "nothing to update — pass --public/--private-style flags, --name, --backup-schedule, or --backup-target", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
data, err := c.Do("PUT", "/databases/"+args[0], body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var dbDeleteCmd = &cobra.Command{
|
var dbDeleteCmd = &cobra.Command{
|
||||||
Use: "delete <id>",
|
Use: "delete <id>",
|
||||||
Short: "Delete a database and its data",
|
Short: "Delete a database and its data",
|
||||||
@@ -159,7 +200,36 @@ var dbRestoreCmd = &cobra.Command{
|
|||||||
if !Confirm(fmt.Sprintf("Restore database %s from backup %s? Current data will be overwritten.", args[0], args[1])) {
|
if !Confirm(fmt.Sprintf("Restore database %s from backup %s? Current data will be overwritten.", args[0], args[1])) {
|
||||||
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||||
}
|
}
|
||||||
data, err := c.Do("POST", "/databases/"+args[0]+"/restore", map[string]string{"backup_id": args[1]})
|
data, err := c.Do("POST", "/databases/"+args[0]+"/restore", map[string]string{"backup_id": args[1], "database_id": args[0]})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var dbImportBackupCmd = &cobra.Command{
|
||||||
|
Use: "import-backup <id> <file.tar.gz|->",
|
||||||
|
Short: "Import a .tar.gz archive as a restore point (use '-' for stdin)",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var src io.ReadCloser
|
||||||
|
if args[1] == "-" {
|
||||||
|
src = io.NopCloser(os.Stdin)
|
||||||
|
} else {
|
||||||
|
f, err := os.Open(args[1])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
src = f
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
data, err := c.DoRaw("POST", "/databases/"+args[0]+"/backups/import", src, "application/gzip")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -196,9 +266,101 @@ var dbDownloadCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dbRegisterCmd = &cobra.Command{
|
||||||
|
Use: "register <name>",
|
||||||
|
Short: "Register an external database (probes the connection first)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{"name": args[0]}
|
||||||
|
for _, f := range []string{"type", "host", "database", "username", "password"} {
|
||||||
|
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||||
|
body[f] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if v, _ := cmd.Flags().GetInt("port"); v > 0 {
|
||||||
|
body["port"] = v
|
||||||
|
}
|
||||||
|
if v, _ := cmd.Flags().GetBool("ssl"); v {
|
||||||
|
body["ssl"] = true
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/databases/register-external", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m, _ := unwrapObject(data)
|
||||||
|
fmt.Printf("Registered external database %s (%s)\n", args[0], str(m, "id"))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var dbTestConnCmd = &cobra.Command{
|
||||||
|
Use: "test-connection [<id>]",
|
||||||
|
Short: "Probe a database connection — by id, or with --type/--host flags",
|
||||||
|
Args: cobra.MaximumNArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var data []byte
|
||||||
|
if len(args) == 1 {
|
||||||
|
data, err = c.Do("POST", "/databases/"+args[0]+"/test-connection", map[string]interface{}{})
|
||||||
|
} else {
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
for _, f := range []string{"type", "host", "database", "username", "password"} {
|
||||||
|
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||||
|
body[f] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if v, _ := cmd.Flags().GetInt("port"); v > 0 {
|
||||||
|
body["port"] = v
|
||||||
|
}
|
||||||
|
if v, _ := cmd.Flags().GetBool("ssl"); v {
|
||||||
|
body["ssl"] = true
|
||||||
|
}
|
||||||
|
data, err = c.Do("POST", "/databases/test-connection", body)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m, _ := unwrapObject(data)
|
||||||
|
if str(m, "ok") == "false" {
|
||||||
|
return &APIError{Message: "connection failed: " + str(m, "error"), ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fmt.Printf("Connection ok (%sms)\n", str(m, "latency_ms"))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dbCreateCmd.Flags().String("type", "postgres", "postgres|mysql|mariadb|mongodb|redis|dragonfly|clickhouse")
|
dbCreateCmd.Flags().String("type", "postgres", "postgres|mysql|mariadb|mongodb|redis|dragonfly|clickhouse")
|
||||||
dbCreateCmd.Flags().String("version", "", "engine version tag")
|
dbCreateCmd.Flags().String("version", "", "engine version tag")
|
||||||
dbCreateCmd.Flags().String("storage-size", "", "volume size (e.g. 10Gi)")
|
dbCreateCmd.Flags().String("storage-size", "", "volume size (e.g. 10Gi)")
|
||||||
DatabasesCmd.AddCommand(dbListCmd, dbGetCmd, dbCreateCmd, dbDeleteCmd, dbActionCmd, dbBackupCmd, dbRestoreCmd, dbDownloadCmd)
|
dbCreateCmd.Flags().Bool("public", false, "bind the database port on all interfaces (default: localhost only)")
|
||||||
|
dbUpdateCmd.Flags().Bool("public", false, "bind the database port on all interfaces")
|
||||||
|
dbUpdateCmd.Flags().String("name", "", "rename the database")
|
||||||
|
dbUpdateCmd.Flags().String("backup-schedule", "", "cron expression for automatic backups (empty clears)")
|
||||||
|
dbUpdateCmd.Flags().String("backup-target", "", "backup target id for offsite archives (empty clears)")
|
||||||
|
for _, cmd := range []*cobra.Command{dbRegisterCmd, dbTestConnCmd} {
|
||||||
|
cmd.Flags().String("type", "postgres", "postgres|mysql|mariadb|mongodb|redis|dragonfly|clickhouse")
|
||||||
|
cmd.Flags().String("host", "", "database host")
|
||||||
|
cmd.Flags().Int("port", 0, "database port (default per type)")
|
||||||
|
cmd.Flags().String("database", "", "database name")
|
||||||
|
cmd.Flags().String("username", "", "database username")
|
||||||
|
cmd.Flags().String("password", "", "database password (stored encrypted on register)")
|
||||||
|
cmd.Flags().Bool("ssl", false, "require TLS")
|
||||||
|
}
|
||||||
|
DatabasesCmd.AddCommand(dbListCmd, dbGetCmd, dbCreateCmd, dbUpdateCmd, dbRegisterCmd, dbTestConnCmd, dbDeleteCmd, dbActionCmd, dbBackupCmd, dbRestoreCmd, dbImportBackupCmd, dbDownloadCmd)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ShellCmd opens an interactive shell inside a service's running container.
|
||||||
|
var ShellCmd = &cobra.Command{
|
||||||
|
Use: "shell <service-id>",
|
||||||
|
Short: "Interactive shell inside the service container",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
serviceID := args[0]
|
||||||
|
cli, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
base := strings.TrimRight(cli.BaseURL, "/")
|
||||||
|
wsURL := strings.Replace(base, "http", "ws", 1) + "/services/" + serviceID + "/terminal"
|
||||||
|
|
||||||
|
header := http.Header{}
|
||||||
|
header.Set("Authorization", "Bearer "+cli.Token)
|
||||||
|
conn, _, err := websocket.DefaultDialer.Dial(wsURL, header)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("terminal: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
fd := int(os.Stdin.Fd())
|
||||||
|
raw := term.IsTerminal(fd)
|
||||||
|
var oldState *term.State
|
||||||
|
if raw {
|
||||||
|
oldState, err = term.MakeRaw(fd)
|
||||||
|
if err == nil {
|
||||||
|
defer term.Restore(fd, oldState)
|
||||||
|
}
|
||||||
|
sendSize := func() {
|
||||||
|
if w, h, err := term.GetSize(fd); err == nil {
|
||||||
|
_ = conn.WriteJSON(map[string]interface{}{"type": "resize", "cols": w, "rows": h})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sendSize()
|
||||||
|
defer watchResize(sendSize)()
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
// stdin → ws
|
||||||
|
go func() {
|
||||||
|
buf := make([]byte, 4096)
|
||||||
|
for {
|
||||||
|
n, err := os.Stdin.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
if werr := conn.WriteJSON(map[string]interface{}{
|
||||||
|
"type": "stdin", "data": string(buf[:n]),
|
||||||
|
}); werr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
// ws → stdout
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
for {
|
||||||
|
var frame struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Data string `json:"data"`
|
||||||
|
Code int `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
if err := conn.ReadJSON(&frame); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch frame.Type {
|
||||||
|
case "stdout":
|
||||||
|
_, _ = os.Stdout.WriteString(frame.Data)
|
||||||
|
case "exit":
|
||||||
|
fmt.Fprintf(os.Stderr, "\r\n[session ended — exit %d]\r\n", frame.Code)
|
||||||
|
return
|
||||||
|
case "error":
|
||||||
|
fmt.Fprintf(os.Stderr, "\r\nerror: %s\r\n", frame.Message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(24 * time.Hour):
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// watchResize forwards SIGWINCH events to send. Returns a stop func.
|
||||||
|
func watchResize(send func()) func() {
|
||||||
|
sigCh := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigCh, syscall.SIGWINCH)
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-sigCh:
|
||||||
|
send()
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return func() {
|
||||||
|
signal.Stop(sigCh)
|
||||||
|
close(done)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
// watchResize is a no-op on Windows — console resize events don't map to a
|
||||||
|
// signal. The initial size is still sent, and Windows Terminal resize is rare
|
||||||
|
// enough to accept stale dimensions for now.
|
||||||
|
func watchResize(send func()) func() { return func() {} }
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var importVars []string
|
||||||
|
|
||||||
|
var ImportCmd = &cobra.Command{
|
||||||
|
Use: "import",
|
||||||
|
Short: "Import external definitions into Containr",
|
||||||
|
}
|
||||||
|
|
||||||
|
var importComposeCmd = &cobra.Command{
|
||||||
|
Use: "compose <project-id> <compose.yml|->",
|
||||||
|
Short: "Deploy a docker-compose file straight into a project (no template)",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var raw []byte
|
||||||
|
var err error
|
||||||
|
if args[1] == "-" {
|
||||||
|
raw, err = io.ReadAll(os.Stdin)
|
||||||
|
} else {
|
||||||
|
raw, err = os.ReadFile(args[1])
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot read compose file: %w", err)
|
||||||
|
}
|
||||||
|
vars, err := parseKVFlags(importVars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{"compose_yaml": string(raw)}
|
||||||
|
if len(vars) > 0 {
|
||||||
|
body["variables"] = vars
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/projects/"+args[0]+"/import-compose", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
importComposeCmd.Flags().StringArrayVar(&importVars, "var", nil, "Variable override (KEY=VALUE, repeatable)")
|
||||||
|
ImportCmd.AddCommand(importComposeCmd)
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var inviteEmail string
|
||||||
|
var inviteTTL int
|
||||||
|
|
||||||
|
// InvitesCmd manages team invite links (admin).
|
||||||
|
var InvitesCmd = &cobra.Command{
|
||||||
|
Use: "invites",
|
||||||
|
Short: "Manage team invite links (admin)",
|
||||||
|
}
|
||||||
|
|
||||||
|
var inviteListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List invites",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/admin/invites", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "invites")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, i := range items {
|
||||||
|
state := "open"
|
||||||
|
if _, used := i["used_at"]; used && i["used_at"] != nil {
|
||||||
|
state = "used"
|
||||||
|
}
|
||||||
|
rows = append(rows, []string{
|
||||||
|
str(i, "id"), str(i, "email"), str(i, "expires_at"), state,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"ID", "EMAIL", "EXPIRES", "STATE"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var inviteCreateCmd = &cobra.Command{
|
||||||
|
Use: "create",
|
||||||
|
Short: "Create an invite link (--email to bind, --hours for TTL)",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{}
|
||||||
|
if cmd.Flags().Changed("email") {
|
||||||
|
req["email"] = inviteEmail
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("hours") {
|
||||||
|
req["expires_in_hours"] = inviteTTL
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/admin/invites", req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var inviteRevokeCmd = &cobra.Command{
|
||||||
|
Use: "revoke <id>",
|
||||||
|
Short: "Revoke an invite",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if !Confirm("Revoke invite " + args[0] + "?") {
|
||||||
|
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = c.Do("DELETE", "/admin/invites/"+args[0], nil)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
inviteCreateCmd.Flags().StringVar(&inviteEmail, "email", "", "bind invite to this email")
|
||||||
|
inviteCreateCmd.Flags().IntVar(&inviteTTL, "hours", 168, "invite lifetime in hours (1-2160)")
|
||||||
|
InvitesCmd.AddCommand(inviteListCmd, inviteCreateCmd, inviteRevokeCmd)
|
||||||
|
}
|
||||||
@@ -0,0 +1,374 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
const railwayAPI = "https://backboard.railway.com/graphql/v2"
|
||||||
|
|
||||||
|
// railwaySource mirrors the Railway serviceInstance.source union.
|
||||||
|
type railwaySource struct {
|
||||||
|
Repo string `json:"repo"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type railwayServicePlan struct {
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
Source railwaySource
|
||||||
|
Command string
|
||||||
|
Replicas int
|
||||||
|
Domains []string
|
||||||
|
Variables map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// MigrateCmd imports workloads from other platforms.
|
||||||
|
var MigrateCmd = &cobra.Command{
|
||||||
|
Use: "migrate",
|
||||||
|
Short: "Import workloads from other platforms",
|
||||||
|
}
|
||||||
|
|
||||||
|
var migrateRailwayCmd = &cobra.Command{
|
||||||
|
Use: "railway",
|
||||||
|
Short: "Migrate a Railway project into Containr",
|
||||||
|
Long: `Pulls a Railway project's services, variables, and domains via the
|
||||||
|
public GraphQL API and recreates them in Containr.
|
||||||
|
|
||||||
|
containr migrate railway --token <railway-token> --project <id> --dry-run
|
||||||
|
containr migrate railway --token <railway-token> --project <id> --into <project-id>
|
||||||
|
|
||||||
|
Token: account/workspace token (Authorization: Bearer) or a project token.
|
||||||
|
--into defaults to creating a project named after the Railway project.`,
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
token, _ := cmd.Flags().GetString("token")
|
||||||
|
if token == "" {
|
||||||
|
token = os.Getenv("RAILWAY_TOKEN")
|
||||||
|
}
|
||||||
|
if token == "" {
|
||||||
|
return &APIError{Message: "pass --token or set RAILWAY_TOKEN", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
projectID, _ := cmd.Flags().GetString("project")
|
||||||
|
envName, _ := cmd.Flags().GetString("environment")
|
||||||
|
into, _ := cmd.Flags().GetString("into")
|
||||||
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||||
|
|
||||||
|
projectName, envID, services, err := railwayProject(projectID, envName, token)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(services) == 0 {
|
||||||
|
return &APIError{Message: "no services found in that Railway project/environment", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
|
||||||
|
plans := make([]railwayServicePlan, 0, len(services))
|
||||||
|
for _, svc := range services {
|
||||||
|
plan, err := railwayServiceDetail(svc, envID, token)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "warning: %s: %v — skipping\n", svc["name"], err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
plans = append(plans, plan)
|
||||||
|
}
|
||||||
|
if len(plans) == 0 {
|
||||||
|
return &APIError{Message: "no migratable services", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Railway project %q → %d service(s)\n", projectName, len(plans))
|
||||||
|
for _, p := range plans {
|
||||||
|
src := p.Source.Image
|
||||||
|
if src == "" {
|
||||||
|
src = railwayRepoURL(p.Source.Repo)
|
||||||
|
}
|
||||||
|
fmt.Printf(" %s src=%s replicas=%d vars=%d domains=%d\n",
|
||||||
|
p.Name, src, max(p.Replicas, 1), len(p.Variables), len(p.Domains))
|
||||||
|
}
|
||||||
|
|
||||||
|
if dryRun {
|
||||||
|
fmt.Println("\nDry run — pass without --dry-run to create these resources.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
target := into
|
||||||
|
if target == "" {
|
||||||
|
data, err := c.Do("POST", "/projects", map[string]interface{}{
|
||||||
|
"name": projectName,
|
||||||
|
"description": "Imported from Railway",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p, _ := unwrapObject(data, "project")
|
||||||
|
target = str(p, "id")
|
||||||
|
if target == "" || target == "-" {
|
||||||
|
return &APIError{Message: "project created but id missing from response", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
fmt.Printf("Created project %s (%s)\n", projectName, target)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range plans {
|
||||||
|
body := map[string]interface{}{
|
||||||
|
"name": p.Name,
|
||||||
|
"type": "web",
|
||||||
|
"environment": "production",
|
||||||
|
}
|
||||||
|
if p.Source.Image != "" {
|
||||||
|
body["image"] = p.Source.Image
|
||||||
|
} else if repo := railwayRepoURL(p.Source.Repo); repo != "" {
|
||||||
|
body["git_repo"] = repo
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "warning: %s has no source — skipping\n", p.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p.Command != "" {
|
||||||
|
body["command"] = p.Command
|
||||||
|
}
|
||||||
|
if p.Replicas > 1 {
|
||||||
|
body["replicas"] = p.Replicas
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/projects/"+target+"/services", body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "error: create %s: %v\n", p.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
svc, _ := unwrapObject(data, "service")
|
||||||
|
svcID := str(svc, "id")
|
||||||
|
fmt.Printf(" created service %s (%s)\n", p.Name, svcID)
|
||||||
|
|
||||||
|
if len(p.Variables) > 0 && svcID != "" && svcID != "-" {
|
||||||
|
vars := make([]map[string]interface{}, 0, len(p.Variables))
|
||||||
|
for k, v := range p.Variables {
|
||||||
|
vars = append(vars, map[string]interface{}{"key": k, "value": v, "is_secret": false})
|
||||||
|
}
|
||||||
|
if _, err := c.Do("PUT", "/services/"+svcID+"/variables",
|
||||||
|
map[string]interface{}{"variables": vars}); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, " warning: variables for %s: %v\n", p.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, domain := range p.Domains {
|
||||||
|
if svcID == "" || svcID == "-" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if _, err := c.Do("POST", "/services/"+svcID+"/domains",
|
||||||
|
map[string]interface{}{"domain": domain}); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, " warning: domain %s: %v\n", domain, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("Done. Railway deploy triggers are not migrated — redeploy each service when ready.")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// railwayGraphQL posts a query; Railway returns 200 with an errors array on
|
||||||
|
// partial failures — treat errors as fatal, the migration needs clean data.
|
||||||
|
func railwayGraphQL(token, query string, variables map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
body, _ := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
|
||||||
|
req, err := http.NewRequest(http.MethodPost, railwayAPI, bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
// Project tokens use a dedicated header; account/workspace tokens use Bearer.
|
||||||
|
if strings.HasPrefix(token, "rw_pat") || strings.HasPrefix(token, "rw_pt") {
|
||||||
|
req.Header.Set("Project-Access-Token", token)
|
||||||
|
} else {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
}
|
||||||
|
client := &http.Client{Timeout: 30 * time.Second}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||||
|
var out struct {
|
||||||
|
Data map[string]interface{} `json:"data"`
|
||||||
|
Errors []struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
} `json:"errors"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &out); err != nil {
|
||||||
|
return nil, fmt.Errorf("railway response not JSON (status %d)", resp.StatusCode)
|
||||||
|
}
|
||||||
|
if len(out.Errors) > 0 {
|
||||||
|
return nil, fmt.Errorf("railway: %s", out.Errors[0].Message)
|
||||||
|
}
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
return nil, fmt.Errorf("railway status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return out.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// railwayProject resolves the project (by id, or sole accessible project) and
|
||||||
|
// returns its name, the target environment id, and its services.
|
||||||
|
func railwayProject(projectID, envName, token string) (string, string, []map[string]interface{}, error) {
|
||||||
|
if projectID == "" {
|
||||||
|
data, err := railwayGraphQL(token,
|
||||||
|
`query { projects(first: 100) { edges { node { id name } } } }`, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", nil, err
|
||||||
|
}
|
||||||
|
projects := edgesOf(data, "projects")
|
||||||
|
if len(projects) == 1 {
|
||||||
|
projectID = str(projects[0], "id")
|
||||||
|
} else {
|
||||||
|
for _, p := range projects {
|
||||||
|
fmt.Fprintf(os.Stderr, " %s %s\n", str(p, "id"), str(p, "name"))
|
||||||
|
}
|
||||||
|
return "", "", nil, &APIError{
|
||||||
|
Message: "multiple projects — pass --project <id> (listed above)",
|
||||||
|
ExitCode: ExitError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := railwayGraphQL(token, `query ($id: String!) {
|
||||||
|
project(id: $id) {
|
||||||
|
name
|
||||||
|
environments { edges { node { id name } } }
|
||||||
|
services { edges { node { id name } } }
|
||||||
|
}
|
||||||
|
}`, map[string]interface{}{"id": projectID})
|
||||||
|
if err != nil {
|
||||||
|
return "", "", nil, err
|
||||||
|
}
|
||||||
|
project, _ := data["project"].(map[string]interface{})
|
||||||
|
if project == nil {
|
||||||
|
return "", "", nil, &APIError{Message: "railway project not found", ExitCode: ExitNotFound}
|
||||||
|
}
|
||||||
|
envs := edgesOf(project, "environments")
|
||||||
|
envID := ""
|
||||||
|
for _, e := range envs {
|
||||||
|
if str(e, "name") == envName {
|
||||||
|
envID = str(e, "id")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if envID == "" && len(envs) == 1 {
|
||||||
|
envID = str(envs[0], "id")
|
||||||
|
}
|
||||||
|
if envID == "" {
|
||||||
|
names := make([]string, 0, len(envs))
|
||||||
|
for _, e := range envs {
|
||||||
|
names = append(names, str(e, "name"))
|
||||||
|
}
|
||||||
|
return "", "", nil, &APIError{
|
||||||
|
Message: fmt.Sprintf("environment %q not found (have: %s)", envName, strings.Join(names, ", ")),
|
||||||
|
ExitCode: ExitError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str(project, "name"), envID, edgesOf(project, "services"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// railwayServiceDetail pulls source, command, replicas, domains, and
|
||||||
|
// variables for one service in the target environment.
|
||||||
|
func railwayServiceDetail(svc map[string]interface{}, envID, token string) (railwayServicePlan, error) {
|
||||||
|
plan := railwayServicePlan{
|
||||||
|
ID: str(svc, "id"),
|
||||||
|
Name: str(svc, "name"),
|
||||||
|
}
|
||||||
|
data, err := railwayGraphQL(token, `query ($sid: String!, $eid: String!) {
|
||||||
|
serviceInstance(serviceId: $sid, environmentId: $eid) {
|
||||||
|
startCommand
|
||||||
|
numReplicas
|
||||||
|
source { repo image }
|
||||||
|
domains { serviceDomains { domain } customDomains { domain } }
|
||||||
|
}
|
||||||
|
}`, map[string]interface{}{"sid": plan.ID, "eid": envID})
|
||||||
|
if err != nil {
|
||||||
|
return plan, err
|
||||||
|
}
|
||||||
|
inst, _ := data["serviceInstance"].(map[string]interface{})
|
||||||
|
if inst == nil {
|
||||||
|
return plan, fmt.Errorf("no service instance in this environment")
|
||||||
|
}
|
||||||
|
if src, ok := inst["source"].(map[string]interface{}); ok {
|
||||||
|
plan.Source = railwaySource{Repo: str(src, "repo"), Image: str(src, "image")}
|
||||||
|
}
|
||||||
|
if cmd, ok := inst["startCommand"].(string); ok {
|
||||||
|
plan.Command = cmd
|
||||||
|
}
|
||||||
|
if n, ok := inst["numReplicas"].(float64); ok {
|
||||||
|
plan.Replicas = int(n)
|
||||||
|
}
|
||||||
|
if domains, ok := inst["domains"].(map[string]interface{}); ok {
|
||||||
|
for _, group := range []string{"serviceDomains", "customDomains"} {
|
||||||
|
if list, ok := domains[group].([]interface{}); ok {
|
||||||
|
for _, d := range list {
|
||||||
|
if m, ok := d.(map[string]interface{}); ok {
|
||||||
|
if dom := str(m, "domain"); dom != "" && dom != "-" {
|
||||||
|
plan.Domains = append(plan.Domains, dom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vdata, err := railwayGraphQL(token, `query ($sid: String!, $eid: String!) {
|
||||||
|
variables(serviceId: $sid, environmentId: $eid)
|
||||||
|
}`, map[string]interface{}{"sid": plan.ID, "eid": envID})
|
||||||
|
if err == nil {
|
||||||
|
if vars, ok := vdata["variables"].(map[string]interface{}); ok {
|
||||||
|
plan.Variables = map[string]string{}
|
||||||
|
for k, v := range vars {
|
||||||
|
if s, ok := v.(string); ok {
|
||||||
|
// Railway reference variables (${{…}}) resolve at deploy
|
||||||
|
// time — carry them literally so they stay visible.
|
||||||
|
plan.Variables[k] = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return plan, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// railwayRepoURL normalizes Railway's "owner/repo" to a cloneable https URL.
|
||||||
|
func railwayRepoURL(repo string) string {
|
||||||
|
repo = strings.TrimSpace(repo)
|
||||||
|
if repo == "" || repo == "-" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if strings.Contains(repo, "://") || strings.HasPrefix(repo, "git@") {
|
||||||
|
return repo
|
||||||
|
}
|
||||||
|
return "https://github.com/" + repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func edgesOf(obj map[string]interface{}, key string) []map[string]interface{} {
|
||||||
|
conn, _ := obj[key].(map[string]interface{})
|
||||||
|
edges, _ := conn["edges"].([]interface{})
|
||||||
|
out := make([]map[string]interface{}, 0, len(edges))
|
||||||
|
for _, e := range edges {
|
||||||
|
if m, ok := e.(map[string]interface{}); ok {
|
||||||
|
if node, ok := m["node"].(map[string]interface{}); ok {
|
||||||
|
out = append(out, node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
migrateRailwayCmd.Flags().String("token", "", "Railway API token (or RAILWAY_TOKEN env)")
|
||||||
|
migrateRailwayCmd.Flags().String("project", "", "Railway project id (lists yours when omitted and >1 exists)")
|
||||||
|
migrateRailwayCmd.Flags().String("environment", "production", "Railway environment name")
|
||||||
|
migrateRailwayCmd.Flags().String("into", "", "existing Containr project id (default: create one)")
|
||||||
|
migrateRailwayCmd.Flags().Bool("dry-run", false, "print the plan without creating anything")
|
||||||
|
MigrateCmd.AddCommand(migrateRailwayCmd)
|
||||||
|
}
|
||||||
@@ -0,0 +1,441 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dflow runs on Payload CMS — every collection is a plain REST resource under
|
||||||
|
// /api/<slug> returning {docs, page, totalPages, hasNextPage}. Auth is a
|
||||||
|
// Payload API key on the users collection: "Authorization: users API-Key <k>".
|
||||||
|
|
||||||
|
type dflowServicePlan struct {
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
Type string // web | database
|
||||||
|
Image string
|
||||||
|
GitRepo string
|
||||||
|
GitBranch string
|
||||||
|
BuildPath string
|
||||||
|
Port int
|
||||||
|
Variables map[string]string
|
||||||
|
Domains []string
|
||||||
|
Volumes []map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
var migrateDflowCmd = &cobra.Command{
|
||||||
|
Use: "dflow",
|
||||||
|
Short: "Migrate a dflow (Payload CMS) project into Containr",
|
||||||
|
Long: `Pulls a dflow project's services, variables, volumes, and domains via the
|
||||||
|
Payload REST API and recreates them in Containr.
|
||||||
|
|
||||||
|
containr migrate dflow --url https://dflow.example.com --api-key <key> --dry-run
|
||||||
|
containr migrate dflow --url https://dflow.example.com --api-key <key> --project <id>
|
||||||
|
|
||||||
|
Auth: a Payload API key from a dflow admin user (users API-Key). Encrypted
|
||||||
|
fields (variables, git tokens) only come back decrypted if the API key belongs
|
||||||
|
to a user with access — empty values are flagged so nothing is silently lost.`,
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
base, _ := cmd.Flags().GetString("url")
|
||||||
|
if base == "" {
|
||||||
|
base = os.Getenv("DFLOW_URL")
|
||||||
|
}
|
||||||
|
if base == "" {
|
||||||
|
return &APIError{Message: "pass --url or set DFLOW_URL", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
base = strings.TrimSuffix(base, "/")
|
||||||
|
key, _ := cmd.Flags().GetString("api-key")
|
||||||
|
if key == "" {
|
||||||
|
key = os.Getenv("DFLOW_API_KEY")
|
||||||
|
}
|
||||||
|
if key == "" {
|
||||||
|
return &APIError{Message: "pass --api-key or set DFLOW_API_KEY", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
projectID, _ := cmd.Flags().GetString("project")
|
||||||
|
into, _ := cmd.Flags().GetString("into")
|
||||||
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||||
|
|
||||||
|
projectName, services, err := dflowProject(base, key, projectID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(services) == 0 {
|
||||||
|
return &APIError{Message: "no services found in that dflow project", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
|
||||||
|
plans := make([]dflowServicePlan, 0, len(services))
|
||||||
|
for _, svc := range services {
|
||||||
|
plan, warn := dflowServicePlanOf(svc)
|
||||||
|
for _, w := range warn {
|
||||||
|
fmt.Fprintf(os.Stderr, " warning: %s: %s\n", plan.Name, w)
|
||||||
|
}
|
||||||
|
plans = append(plans, plan)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("dflow project %q → %d service(s)\n", projectName, len(plans))
|
||||||
|
for _, p := range plans {
|
||||||
|
src := p.Image
|
||||||
|
if src == "" {
|
||||||
|
src = p.GitRepo
|
||||||
|
}
|
||||||
|
fmt.Printf(" %s type=%s src=%s vars=%d domains=%d volumes=%d\n",
|
||||||
|
p.Name, p.Type, src, len(p.Variables), len(p.Domains), len(p.Volumes))
|
||||||
|
}
|
||||||
|
|
||||||
|
if dryRun {
|
||||||
|
fmt.Println("\nDry run — pass without --dry-run to create these resources.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
target := into
|
||||||
|
if target == "" {
|
||||||
|
data, err := c.Do("POST", "/projects", map[string]interface{}{
|
||||||
|
"name": projectName,
|
||||||
|
"description": "Imported from dflow",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p, _ := unwrapObject(data, "project")
|
||||||
|
target = str(p, "id")
|
||||||
|
if target == "" || target == "-" {
|
||||||
|
return &APIError{Message: "project created but id missing from response", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
fmt.Printf("Created project %s (%s)\n", projectName, target)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range plans {
|
||||||
|
body := map[string]interface{}{
|
||||||
|
"name": p.Name,
|
||||||
|
"type": p.Type,
|
||||||
|
"environment": "production",
|
||||||
|
}
|
||||||
|
if p.Image != "" {
|
||||||
|
body["image"] = p.Image
|
||||||
|
}
|
||||||
|
if p.GitRepo != "" {
|
||||||
|
body["git_repo"] = p.GitRepo
|
||||||
|
}
|
||||||
|
if p.GitBranch != "" {
|
||||||
|
body["git_branch"] = p.GitBranch
|
||||||
|
}
|
||||||
|
if p.BuildPath != "" {
|
||||||
|
body["build_path"] = p.BuildPath
|
||||||
|
}
|
||||||
|
if p.Port > 0 {
|
||||||
|
body["port"] = p.Port
|
||||||
|
}
|
||||||
|
if len(p.Volumes) > 0 {
|
||||||
|
vols := make([]map[string]interface{}, 0, len(p.Volumes))
|
||||||
|
for _, v := range p.Volumes {
|
||||||
|
vols = append(vols, map[string]interface{}{
|
||||||
|
"type": "bind", "source": v["source"], "target": v["target"],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
body["volumes"] = vols
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/projects/"+target+"/services", body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "error: create %s: %v\n", p.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
svc, _ := unwrapObject(data, "service")
|
||||||
|
svcID := str(svc, "id")
|
||||||
|
fmt.Printf(" created service %s (%s)\n", p.Name, svcID)
|
||||||
|
|
||||||
|
if len(p.Variables) > 0 && svcID != "" && svcID != "-" {
|
||||||
|
vars := make([]map[string]interface{}, 0, len(p.Variables))
|
||||||
|
for k, v := range p.Variables {
|
||||||
|
vars = append(vars, map[string]interface{}{"key": k, "value": v, "is_secret": false})
|
||||||
|
}
|
||||||
|
if _, err := c.Do("PUT", "/services/"+svcID+"/variables",
|
||||||
|
map[string]interface{}{"variables": vars}); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, " warning: variables for %s: %v\n", p.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, domain := range p.Domains {
|
||||||
|
if svcID == "" || svcID == "-" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if _, err := c.Do("POST", "/services/"+svcID+"/domains",
|
||||||
|
map[string]interface{}{"domain": domain}); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, " warning: domain %s: %v\n", domain, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("Done. Git credentials, deployment history, and server topology are not migrated — reconnect repos and redeploy each service when ready.")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// dflowGet performs an authenticated Payload REST call.
|
||||||
|
func dflowGet(base, apiKey, path string, q url.Values) (map[string]interface{}, error) {
|
||||||
|
u := base + "/api/" + path
|
||||||
|
if len(q) > 0 {
|
||||||
|
u += "?" + q.Encode()
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(http.MethodGet, u, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Authorization", "users API-Key "+apiKey)
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
client := &http.Client{Timeout: 30 * time.Second}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 16<<20))
|
||||||
|
if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
|
||||||
|
return nil, &APIError{Message: "dflow rejected the API key — needs an admin user's users API-Key", ExitCode: ExitAuth}
|
||||||
|
}
|
||||||
|
var out map[string]interface{}
|
||||||
|
if err := json.Unmarshal(raw, &out); err != nil {
|
||||||
|
return nil, fmt.Errorf("dflow response not JSON (status %d)", resp.StatusCode)
|
||||||
|
}
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
return nil, fmt.Errorf("dflow status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dflowCollection pages through a Payload collection.
|
||||||
|
func dflowCollection(base, apiKey, collection string, q url.Values) ([]map[string]interface{}, error) {
|
||||||
|
var docs []map[string]interface{}
|
||||||
|
for page := 1; ; page++ {
|
||||||
|
params := url.Values{}
|
||||||
|
for k, v := range q {
|
||||||
|
params[k] = v
|
||||||
|
}
|
||||||
|
params.Set("limit", "100")
|
||||||
|
params.Set("depth", "0")
|
||||||
|
params.Set("page", fmt.Sprint(page))
|
||||||
|
data, err := dflowGet(base, apiKey, collection, params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
list, _ := data["docs"].([]interface{})
|
||||||
|
for _, d := range list {
|
||||||
|
if m, ok := d.(map[string]interface{}); ok {
|
||||||
|
docs = append(docs, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next, _ := data["hasNextPage"].(bool)
|
||||||
|
if !next {
|
||||||
|
return docs, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dflowProject resolves the project (by id, or sole project) and returns its
|
||||||
|
// name plus its services.
|
||||||
|
func dflowProject(base, apiKey, projectID string) (string, []map[string]interface{}, error) {
|
||||||
|
if projectID == "" {
|
||||||
|
projects, err := dflowCollection(base, apiKey, "projects", nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
if len(projects) == 1 {
|
||||||
|
projectID = str(projects[0], "id")
|
||||||
|
} else {
|
||||||
|
for _, p := range projects {
|
||||||
|
fmt.Fprintf(os.Stderr, " %s %s\n", str(p, "id"), str(p, "name"))
|
||||||
|
}
|
||||||
|
return "", nil, &APIError{
|
||||||
|
Message: "multiple projects — pass --project <id> (listed above)",
|
||||||
|
ExitCode: ExitError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
projects, err := dflowCollection(base, apiKey, "projects",
|
||||||
|
url.Values{"where[id][equals]": {projectID}})
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
if len(projects) == 0 {
|
||||||
|
return "", nil, &APIError{Message: "dflow project not found", ExitCode: ExitNotFound}
|
||||||
|
}
|
||||||
|
name := str(projects[0], "name")
|
||||||
|
|
||||||
|
q := url.Values{
|
||||||
|
"where[project][equals]": {projectID},
|
||||||
|
"where[deletedAt][exists]": {"false"},
|
||||||
|
"where[_status][not_equals]": {"draft"},
|
||||||
|
}
|
||||||
|
services, err := dflowCollection(base, apiKey, "services", q)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
return name, services, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dflowServicePlanOf maps a Payload service doc onto a Containr create plan.
|
||||||
|
func dflowServicePlanOf(svc map[string]interface{}) (dflowServicePlan, []string) {
|
||||||
|
var warn []string
|
||||||
|
plan := dflowServicePlan{
|
||||||
|
ID: str(svc, "id"),
|
||||||
|
Name: str(svc, "name"),
|
||||||
|
Description: str(svc, "description"),
|
||||||
|
Variables: map[string]string{},
|
||||||
|
}
|
||||||
|
switch str(svc, "type") {
|
||||||
|
case "app":
|
||||||
|
plan.Type = "web"
|
||||||
|
settings := dflowProviderSettings(svc)
|
||||||
|
plan.GitRepo = dflowRepoURL(str(svc, "providerType"), str(settings, "owner"), str(settings, "repository"))
|
||||||
|
plan.GitBranch = str(settings, "branch")
|
||||||
|
plan.BuildPath = str(settings, "buildPath")
|
||||||
|
plan.Port = int(num(settings, "port"))
|
||||||
|
if plan.GitRepo == "" {
|
||||||
|
warn = append(warn, "no repository settings — importing with no source")
|
||||||
|
}
|
||||||
|
case "docker":
|
||||||
|
plan.Type = "web"
|
||||||
|
d, _ := svc["dockerDetails"].(map[string]interface{})
|
||||||
|
plan.Image = normalizeDockerImage(str(d, "url"))
|
||||||
|
for _, p := range listOf(d, "ports") {
|
||||||
|
if plan.Port == 0 {
|
||||||
|
plan.Port = int(num(p, "containerPort"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if plan.Image == "" {
|
||||||
|
warn = append(warn, "no docker image url — importing with no source")
|
||||||
|
}
|
||||||
|
case "database":
|
||||||
|
plan.Type = "database"
|
||||||
|
d, _ := svc["databaseDetails"].(map[string]interface{})
|
||||||
|
dbType := str(d, "type")
|
||||||
|
version := str(d, "version")
|
||||||
|
if dbType != "" {
|
||||||
|
plan.Image = dbType + ":" + firstOf(version, "latest")
|
||||||
|
}
|
||||||
|
if str(d, "provider") == "external" {
|
||||||
|
if conn := str(d, "connectionUrl"); conn != "" {
|
||||||
|
plan.Variables["DATABASE_URL"] = conn
|
||||||
|
} else if str(d, "host") != "" {
|
||||||
|
warn = append(warn, "external database has host but no connectionUrl — set DATABASE_URL manually")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
plan.Type = "web"
|
||||||
|
warn = append(warn, fmt.Sprintf("unknown type %q — imported as web", str(svc, "type")))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range listOf(svc, "variables") {
|
||||||
|
k, val := str(v, "key"), str(v, "value")
|
||||||
|
if k == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if val == "" {
|
||||||
|
warn = append(warn, fmt.Sprintf("variable %s came back empty (encrypted field?)", k))
|
||||||
|
}
|
||||||
|
plan.Variables[k] = val
|
||||||
|
}
|
||||||
|
for _, d := range listOf(svc, "domains") {
|
||||||
|
if dom := str(d, "domain"); dom != "" && dom != "-" {
|
||||||
|
plan.Domains = append(plan.Domains, dom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, v := range listOf(svc, "volumes") {
|
||||||
|
host, container := str(v, "hostPath"), str(v, "containerPath")
|
||||||
|
if host != "" && container != "" {
|
||||||
|
plan.Volumes = append(plan.Volumes, map[string]string{"source": host, "target": container})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return plan, warn
|
||||||
|
}
|
||||||
|
|
||||||
|
// dflowProviderSettings picks the populated <provider>Settings group.
|
||||||
|
func dflowProviderSettings(svc map[string]interface{}) map[string]interface{} {
|
||||||
|
for _, key := range []string{"githubSettings", "gitlabSettings", "bitbucketSettings", "giteaSettings", "azureSettings"} {
|
||||||
|
if g, ok := svc[key].(map[string]interface{}); ok && str(g, "repository") != "" {
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dflowRepoURL builds a cloneable https URL for the known git providers.
|
||||||
|
func dflowRepoURL(providerType, owner, repo string) string {
|
||||||
|
repo = strings.TrimSuffix(strings.TrimSpace(repo), ".git")
|
||||||
|
owner = strings.TrimSpace(owner)
|
||||||
|
if repo == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if strings.Contains(repo, "://") || strings.HasPrefix(repo, "git@") {
|
||||||
|
return repo
|
||||||
|
}
|
||||||
|
switch providerType {
|
||||||
|
case "gitlab":
|
||||||
|
return "https://gitlab.com/" + owner + "/" + repo
|
||||||
|
case "bitbucket":
|
||||||
|
return "https://bitbucket.org/" + owner + "/" + repo
|
||||||
|
case "azureDevOps":
|
||||||
|
return "https://dev.azure.com/" + owner + "/_git/" + repo
|
||||||
|
case "gitea":
|
||||||
|
return "" // host unknown — user must reconnect the repo
|
||||||
|
default:
|
||||||
|
if owner == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return "https://github.com/" + owner + "/" + repo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalizeDockerImage fixes common dflow registry URL typos (ghrc://).
|
||||||
|
func normalizeDockerImage(image string) string {
|
||||||
|
image = strings.TrimSpace(image)
|
||||||
|
if strings.HasPrefix(image, "ghrc://") {
|
||||||
|
return "ghcr.io/" + strings.TrimPrefix(image, "ghrc://")
|
||||||
|
}
|
||||||
|
return image
|
||||||
|
}
|
||||||
|
|
||||||
|
func listOf(m map[string]interface{}, key string) []map[string]interface{} {
|
||||||
|
raw, _ := m[key].([]interface{})
|
||||||
|
out := make([]map[string]interface{}, 0, len(raw))
|
||||||
|
for _, item := range raw {
|
||||||
|
if v, ok := item.(map[string]interface{}); ok {
|
||||||
|
out = append(out, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func num(m map[string]interface{}, key string) float64 {
|
||||||
|
v, _ := m[key].(float64)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func firstOf(v, fallback string) string {
|
||||||
|
if v == "" {
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
migrateDflowCmd.Flags().String("url", "", "dflow base URL (or DFLOW_URL env)")
|
||||||
|
migrateDflowCmd.Flags().String("api-key", "", "dflow admin API key (or DFLOW_API_KEY env)")
|
||||||
|
migrateDflowCmd.Flags().String("project", "", "dflow project id (lists yours when omitted and >1 exists)")
|
||||||
|
migrateDflowCmd.Flags().String("into", "", "existing Containr project id (default: create one)")
|
||||||
|
migrateDflowCmd.Flags().Bool("dry-run", false, "print the plan without creating anything")
|
||||||
|
MigrateCmd.AddCommand(migrateDflowCmd)
|
||||||
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRailwayRepoURL(t *testing.T) {
|
||||||
|
cases := map[string]string{
|
||||||
|
"owner/repo": "https://github.com/owner/repo",
|
||||||
|
"https://gitlab.com/a/b": "https://gitlab.com/a/b",
|
||||||
|
"[email protected]:a/b.git": "[email protected]:a/b.git",
|
||||||
|
"": "",
|
||||||
|
"-": "",
|
||||||
|
" spaced/x ": "https://github.com/spaced/x",
|
||||||
|
}
|
||||||
|
for in, want := range cases {
|
||||||
|
if got := railwayRepoURL(in); got != want {
|
||||||
|
t.Errorf("railwayRepoURL(%q) = %q, want %q", in, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgesOf(t *testing.T) {
|
||||||
|
obj := map[string]interface{}{
|
||||||
|
"services": map[string]interface{}{
|
||||||
|
"edges": []interface{}{
|
||||||
|
map[string]interface{}{"node": map[string]interface{}{"id": "a", "name": "web"}},
|
||||||
|
map[string]interface{}{"node": map[string]interface{}{"id": "b", "name": "api"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
nodes := edgesOf(obj, "services")
|
||||||
|
if len(nodes) != 2 || str(nodes[0], "name") != "web" {
|
||||||
|
t.Fatalf("edgesOf returned %+v", nodes)
|
||||||
|
}
|
||||||
|
if got := edgesOf(obj, "missing"); len(got) != 0 {
|
||||||
|
t.Fatalf("edgesOf(missing) = %+v, want empty", got)
|
||||||
|
}
|
||||||
|
if got := edgesOf(map[string]interface{}{"services": nil}, "services"); len(got) != 0 {
|
||||||
|
t.Fatalf("edgesOf(nil conn) = %+v, want empty", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDflowServicePlanOf(t *testing.T) {
|
||||||
|
appSvc := map[string]interface{}{
|
||||||
|
"id": "s1",
|
||||||
|
"name": "api",
|
||||||
|
"type": "app",
|
||||||
|
"providerType": "github",
|
||||||
|
"githubSettings": map[string]interface{}{
|
||||||
|
"owner": "acme", "repository": "api", "branch": "main",
|
||||||
|
"buildPath": "/", "port": float64(8080),
|
||||||
|
},
|
||||||
|
"variables": []interface{}{
|
||||||
|
map[string]interface{}{"key": "FOO", "value": "bar"},
|
||||||
|
map[string]interface{}{"key": "EMPTY", "value": ""},
|
||||||
|
},
|
||||||
|
"domains": []interface{}{
|
||||||
|
map[string]interface{}{"domain": "api.example.com"},
|
||||||
|
},
|
||||||
|
"volumes": []interface{}{
|
||||||
|
map[string]interface{}{"hostPath": "/data", "containerPath": "/app/data"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
plan, warn := dflowServicePlanOf(appSvc)
|
||||||
|
if plan.Type != "web" || plan.GitRepo != "https://github.com/acme/api" || plan.Port != 8080 {
|
||||||
|
t.Fatalf("app plan = %+v", plan)
|
||||||
|
}
|
||||||
|
if plan.Variables["FOO"] != "bar" || len(plan.Domains) != 1 || len(plan.Volumes) != 1 {
|
||||||
|
t.Fatalf("plan detail = %+v", plan)
|
||||||
|
}
|
||||||
|
if len(warn) != 1 { // EMPTY value flagged
|
||||||
|
t.Fatalf("warnings = %v", warn)
|
||||||
|
}
|
||||||
|
|
||||||
|
dbSvc := map[string]interface{}{
|
||||||
|
"id": "s2", "name": "pg", "type": "database",
|
||||||
|
"databaseDetails": map[string]interface{}{
|
||||||
|
"type": "postgres", "version": "16", "provider": "external",
|
||||||
|
"connectionUrl": "postgres://u:p@h:5432/db",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
dplan, _ := dflowServicePlanOf(dbSvc)
|
||||||
|
if dplan.Type != "database" || dplan.Image != "postgres:16" {
|
||||||
|
t.Fatalf("db plan = %+v", dplan)
|
||||||
|
}
|
||||||
|
if dplan.Variables["DATABASE_URL"] != "postgres://u:p@h:5432/db" {
|
||||||
|
t.Fatalf("db vars = %+v", dplan.Variables)
|
||||||
|
}
|
||||||
|
|
||||||
|
dockerSvc := map[string]interface{}{
|
||||||
|
"id": "s3", "name": "img", "type": "docker",
|
||||||
|
"dockerDetails": map[string]interface{}{
|
||||||
|
"url": "ghrc://acme/web:v1",
|
||||||
|
"ports": []interface{}{
|
||||||
|
map[string]interface{}{"hostPort": float64(80), "containerPort": float64(3000), "scheme": "http"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
iplan, _ := dflowServicePlanOf(dockerSvc)
|
||||||
|
if iplan.Image != "ghcr.io/acme/web:v1" || iplan.Port != 3000 {
|
||||||
|
t.Fatalf("docker plan = %+v", iplan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDflowRepoURL(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
provider, owner, repo, want string
|
||||||
|
}{
|
||||||
|
{"github", "acme", "api", "https://github.com/acme/api"},
|
||||||
|
{"gitlab", "acme", "api", "https://gitlab.com/acme/api"},
|
||||||
|
{"bitbucket", "acme", "api", "https://bitbucket.org/acme/api"},
|
||||||
|
{"gitea", "acme", "api", ""},
|
||||||
|
{"github", "acme", "api.git", "https://github.com/acme/api"},
|
||||||
|
{"github", "", "api", ""},
|
||||||
|
{"github", "acme", "", ""},
|
||||||
|
{"github", "acme", "https://git.example.com/a/b", "https://git.example.com/a/b"},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
if got := dflowRepoURL(c.provider, c.owner, c.repo); got != c.want {
|
||||||
|
t.Errorf("dflowRepoURL(%q,%q,%q) = %q, want %q", c.provider, c.owner, c.repo, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -81,6 +81,76 @@ var nodesDeleteCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nodesUpdateCmd = &cobra.Command{
|
||||||
|
Use: "update <id>",
|
||||||
|
Short: "Update a node agent (--name, --auto-prune)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
if cmd.Flags().Changed("name") {
|
||||||
|
body["name"], _ = cmd.Flags().GetString("name")
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("auto-prune") {
|
||||||
|
body["auto_prune"], _ = cmd.Flags().GetBool("auto-prune")
|
||||||
|
}
|
||||||
|
if len(body) == 0 {
|
||||||
|
return &APIError{Message: "nothing to update — pass --name or --auto-prune", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
data, err := c.Do("PUT", "/agents/"+args[0], body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var nodesPruneCmd = &cobra.Command{
|
||||||
|
Use: "prune <id>",
|
||||||
|
Short: "Enqueue a bounded docker system prune on a node",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
volumes, _ := cmd.Flags().GetBool("volumes")
|
||||||
|
if volumes && !Confirm("Prune INCLUDING volumes? This can delete database data.") {
|
||||||
|
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
until, _ := cmd.Flags().GetString("until")
|
||||||
|
data, err := c.Do("POST", "/agents/"+args[0]+"/prune",
|
||||||
|
map[string]interface{}{"until": until, "volumes": volumes})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var nodesCommandsCmd = &cobra.Command{
|
||||||
|
Use: "commands <id>",
|
||||||
|
Short: "List recent commands sent to a node agent",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/agents/"+args[0]+"/commands", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Agent tokens (cagt_) onboard new nodes.
|
// Agent tokens (cagt_) onboard new nodes.
|
||||||
var nodeTokensCmd = &cobra.Command{
|
var nodeTokensCmd = &cobra.Command{
|
||||||
Use: "tokens",
|
Use: "tokens",
|
||||||
@@ -145,6 +215,10 @@ var nodeTokensRevokeCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
nodesUpdateCmd.Flags().String("name", "", "node display name")
|
||||||
|
nodesUpdateCmd.Flags().Bool("auto-prune", false, "enqueue a daily bounded docker prune")
|
||||||
|
nodesPruneCmd.Flags().String("until", "", "only prune objects older than this duration (e.g. 168h)")
|
||||||
|
nodesPruneCmd.Flags().Bool("volumes", false, "also prune volumes (destructive — asks twice)")
|
||||||
nodeTokensCmd.AddCommand(nodeTokensIssueCmd, nodeTokensListCmd, nodeTokensRevokeCmd)
|
nodeTokensCmd.AddCommand(nodeTokensIssueCmd, nodeTokensListCmd, nodeTokensRevokeCmd)
|
||||||
NodesCmd.AddCommand(nodesListCmd, nodesGetCmd, nodesDeleteCmd, nodeTokensCmd)
|
NodesCmd.AddCommand(nodesListCmd, nodesGetCmd, nodesUpdateCmd, nodesPruneCmd, nodesCommandsCmd, nodesDeleteCmd, nodeTokensCmd)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,214 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
// SSH node bootstrap — one SSH session installs the agent (binary +
|
||||||
|
// systemd + enroll token), then SSH is never needed again. SSH keys
|
||||||
|
// stay client-side; the server only sees the resulting agent.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
"golang.org/x/crypto/ssh/agent"
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
nodeSSHKey string
|
||||||
|
nodeSSHName string
|
||||||
|
nodeSSHPrint bool
|
||||||
|
)
|
||||||
|
|
||||||
|
var nodesAddCmd = &cobra.Command{
|
||||||
|
Use: "add <[user@]host[:port]>",
|
||||||
|
Short: "Bootstrap a node agent over SSH (one session, then closed)",
|
||||||
|
Long: `Installs the containr agent on a remote host:
|
||||||
|
|
||||||
|
1. mints a single-use enroll token via the API
|
||||||
|
2. SSHes in (agent socket, --key, or default key files)
|
||||||
|
3. runs the instance's install script remotely
|
||||||
|
4. closes the session — the agent heartbeats outbound from then on
|
||||||
|
|
||||||
|
Use --print to skip SSH and emit the remote one-liner for manual runs.`,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mint the enroll token first — SSH failures then don't leave
|
||||||
|
// partial state on the remote.
|
||||||
|
body := map[string]interface{}{"label": "bootstrap"}
|
||||||
|
if nodeSSHName != "" {
|
||||||
|
body["label"] = "bootstrap:" + nodeSSHName
|
||||||
|
}
|
||||||
|
tokenData, err := c.Do("POST", "/agent-tokens", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tokenObj, err := unwrapObject(tokenData, "token")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
token := str(tokenObj, "token")
|
||||||
|
if token == "" {
|
||||||
|
return &APIError{Message: "enroll token missing from response", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Agent bootstrap endpoints live at /api/agents (no /v1) — strip
|
||||||
|
// the API version suffix off the client base.
|
||||||
|
apiRoot := strings.TrimSuffix(c.BaseURL, "/api/v1")
|
||||||
|
installCmd := fmt.Sprintf(
|
||||||
|
`curl -fsSL %q | bash -s -- --url %q --token %q --name %q`,
|
||||||
|
apiRoot+"/api/agents/install.sh", apiRoot, token, firstNonEmptyStr(nodeSSHName, nodeSSHTarget(args[0]).host),
|
||||||
|
)
|
||||||
|
|
||||||
|
if nodeSSHPrint {
|
||||||
|
fmt.Println(installCmd)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
target := nodeSSHTarget(args[0])
|
||||||
|
if err := sshBootstrap(target, installCmd); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("agent installing on %s — it should appear in `containr nodes list` within ~15s\n", target.host)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type sshTarget struct {
|
||||||
|
user string
|
||||||
|
host string
|
||||||
|
port string
|
||||||
|
}
|
||||||
|
|
||||||
|
func nodeSSHTarget(raw string) sshTarget {
|
||||||
|
t := sshTarget{port: "22"}
|
||||||
|
rest := raw
|
||||||
|
if at := strings.LastIndex(rest, "@"); at >= 0 {
|
||||||
|
t.user = rest[:at]
|
||||||
|
rest = rest[at+1:]
|
||||||
|
}
|
||||||
|
if colon := strings.LastIndex(rest, ":"); colon >= 0 && !strings.Contains(rest, "]") {
|
||||||
|
t.host, t.port = rest[:colon], rest[colon+1:]
|
||||||
|
} else {
|
||||||
|
t.host = rest
|
||||||
|
}
|
||||||
|
if t.user == "" {
|
||||||
|
if u, err := user.Current(); err == nil {
|
||||||
|
t.user = u.Username
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func sshAuthMethods(keyPath string) []ssh.AuthMethod {
|
||||||
|
var methods []ssh.AuthMethod
|
||||||
|
|
||||||
|
// ssh-agent first — matches how people actually manage keys.
|
||||||
|
if sock := os.Getenv("SSH_AUTH_SOCK"); sock != "" {
|
||||||
|
if conn, err := net.Dial("unix", sock); err == nil {
|
||||||
|
defer conn.Close()
|
||||||
|
methods = append(methods, ssh.PublicKeysCallback(agent.NewClient(conn).Signers))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var keyFiles []string
|
||||||
|
if keyPath != "" {
|
||||||
|
keyFiles = []string{keyPath}
|
||||||
|
} else if home, err := os.UserHomeDir(); err == nil {
|
||||||
|
for _, name := range []string{"id_ed25519", "id_rsa", "id_ecdsa"} {
|
||||||
|
p := filepath.Join(home, ".ssh", name)
|
||||||
|
if _, err := os.Stat(p); err == nil {
|
||||||
|
keyFiles = append(keyFiles, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, p := range keyFiles {
|
||||||
|
if signer, err := sshPrivateKey(p); err == nil {
|
||||||
|
methods = append(methods, ssh.PublicKeys(signer))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return methods
|
||||||
|
}
|
||||||
|
|
||||||
|
func sshPrivateKey(path string) (ssh.Signer, error) {
|
||||||
|
pem, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
signer, err := ssh.ParsePrivateKey(pem)
|
||||||
|
if err != nil {
|
||||||
|
var missing *ssh.PassphraseMissingError
|
||||||
|
if errors.As(err, &missing) {
|
||||||
|
fmt.Fprintf(os.Stderr, "passphrase for %s: ", path)
|
||||||
|
pass, readErr := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
|
fmt.Fprintln(os.Stderr)
|
||||||
|
if readErr != nil {
|
||||||
|
return nil, readErr
|
||||||
|
}
|
||||||
|
return ssh.ParsePrivateKeyWithPassphrase(pem, pass)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return signer, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sshBootstrap(t sshTarget, remoteCmd string) error {
|
||||||
|
config := &ssh.ClientConfig{
|
||||||
|
User: t.user,
|
||||||
|
Auth: sshAuthMethods(nodeSSHKey),
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // jarvis: TOFU would be better; ssh_known_hosts parsing is the upgrade path
|
||||||
|
Timeout: 15 * time.Second,
|
||||||
|
}
|
||||||
|
if len(config.Auth) == 0 {
|
||||||
|
return &APIError{Message: "no SSH auth methods (ssh-agent empty, no usable keys)", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := ssh.Dial("tcp", net.JoinHostPort(t.host, t.port), config)
|
||||||
|
if err != nil {
|
||||||
|
return &APIError{Message: fmt.Sprintf("ssh connect: %v", err), ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
session, err := conn.NewSession()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer session.Close()
|
||||||
|
session.Stdout = os.Stdout
|
||||||
|
session.Stderr = os.Stderr
|
||||||
|
|
||||||
|
// Root shells run directly; otherwise escalate via sudo. NOPASSWD
|
||||||
|
// sudo is assumed — a password prompt here means the operator should
|
||||||
|
// use --print and run the command themselves.
|
||||||
|
if t.user == "root" {
|
||||||
|
return session.Run(remoteCmd)
|
||||||
|
}
|
||||||
|
return session.Run("sudo -n " + remoteCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func firstNonEmptyStr(values ...string) string {
|
||||||
|
for _, v := range values {
|
||||||
|
if v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
nodesAddCmd.Flags().StringVar(&nodeSSHKey, "key", "", "SSH private key path (defaults: agent, ~/.ssh/id_*)")
|
||||||
|
nodesAddCmd.Flags().StringVar(&nodeSSHName, "name", "", "node display name (defaults to remote hostname)")
|
||||||
|
nodesAddCmd.Flags().BoolVar(&nodeSSHPrint, "print", false, "print the remote install command instead of running SSH")
|
||||||
|
NodesCmd.AddCommand(nodesAddCmd)
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OperationsCmd shows the cross-cutting operations view.
|
||||||
|
var OperationsCmd = &cobra.Command{
|
||||||
|
Use: "operations",
|
||||||
|
Aliases: []string{"ops"},
|
||||||
|
Short: "Show active and recent platform work (deploys, cron, backups)",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/operations", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegistriesCmd manages private-image registry credentials.
|
||||||
|
var RegistriesCmd = &cobra.Command{
|
||||||
|
Use: "registries",
|
||||||
|
Aliases: []string{"registry"},
|
||||||
|
Short: "Manage registry credentials",
|
||||||
|
}
|
||||||
|
|
||||||
|
var registriesListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List registry credentials",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/registries", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "registries")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, r := range items {
|
||||||
|
hasPw := ""
|
||||||
|
if b, ok := r["has_password"].(bool); ok && b {
|
||||||
|
hasPw = "yes"
|
||||||
|
}
|
||||||
|
rows = append(rows, []string{str(r, "id"), str(r, "name"), str(r, "host"), str(r, "username"), hasPw})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"ID", "NAME", "HOST", "USERNAME", "PASSWORD"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var registriesAddCmd = &cobra.Command{
|
||||||
|
Use: "add <name> <host>",
|
||||||
|
Short: "Add a registry credential (e.g. ghcr.io, registry.example.com, docker.io)",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{"name": args[0], "host": args[1]}
|
||||||
|
if v, _ := cmd.Flags().GetString("username"); v != "" {
|
||||||
|
body["username"] = v
|
||||||
|
}
|
||||||
|
if v, _ := cmd.Flags().GetString("password"); v != "" {
|
||||||
|
body["password"] = v
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/registries", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
r, _ := unwrapObject(data, "registry")
|
||||||
|
fmt.Printf("Added registry %s (%s)\n", str(r, "name"), str(r, "host"))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var registriesUpdateCmd = &cobra.Command{
|
||||||
|
Use: "update <id>",
|
||||||
|
Short: "Update a registry credential (empty --password keeps the stored one)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
for _, f := range []string{"name", "host", "username", "password"} {
|
||||||
|
if cmd.Flags().Changed(f) {
|
||||||
|
v, _ := cmd.Flags().GetString(f)
|
||||||
|
body[f] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(body) == 0 {
|
||||||
|
return &APIError{Message: "nothing to update — pass a flag", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
// The API requires name+host — backfill them from the stored row
|
||||||
|
// when the caller didn't supply both.
|
||||||
|
_, hasName := body["name"]
|
||||||
|
_, hasHost := body["host"]
|
||||||
|
if !hasName || !hasHost {
|
||||||
|
data, err := c.Do("GET", "/registries", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, _ := unwrapList(data, "registries")
|
||||||
|
for _, r := range items {
|
||||||
|
if str(r, "id") == args[0] {
|
||||||
|
if !hasName {
|
||||||
|
body["name"] = str(r, "name")
|
||||||
|
}
|
||||||
|
if !hasHost {
|
||||||
|
body["host"] = str(r, "host")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data, err := c.Do("PUT", "/registries/"+args[0], body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fmt.Printf("Updated registry %s\n", args[0])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var registriesRemoveCmd = &cobra.Command{
|
||||||
|
Use: "remove <id>",
|
||||||
|
Short: "Remove a registry credential",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !Confirm(fmt.Sprintf("Remove registry %s?", args[0])) {
|
||||||
|
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||||
|
}
|
||||||
|
if _, err := c.Do("DELETE", "/registries/"+args[0], nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
return PrintJSON(map[string]string{"status": "deleted", "id": args[0]})
|
||||||
|
}
|
||||||
|
fmt.Printf("Removed registry %s\n", args[0])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registriesAddCmd.Flags().String("username", "", "registry username")
|
||||||
|
registriesAddCmd.Flags().String("password", "", "registry password/token (stored encrypted)")
|
||||||
|
registriesUpdateCmd.Flags().String("name", "", "display name")
|
||||||
|
registriesUpdateCmd.Flags().String("host", "", "registry host")
|
||||||
|
registriesUpdateCmd.Flags().String("username", "", "registry username")
|
||||||
|
registriesUpdateCmd.Flags().String("password", "", "registry password/token")
|
||||||
|
RegistriesCmd.AddCommand(registriesListCmd, registriesAddCmd, registriesUpdateCmd, registriesRemoveCmd)
|
||||||
|
}
|
||||||
@@ -79,7 +79,7 @@ var servicesCreateCmd = &cobra.Command{
|
|||||||
body := map[string]interface{}{
|
body := map[string]interface{}{
|
||||||
"name": args[1],
|
"name": args[1],
|
||||||
}
|
}
|
||||||
for _, f := range []string{"type", "image", "git-repo", "git-branch", "build-path", "command", "environment", "domain", "restart-policy", "healthcheck-path", "cpu", "memory"} {
|
for _, f := range []string{"type", "image", "git-repo", "git-branch", "build-path", "command", "environment", "domain", "restart-policy", "healthcheck-path", "cpu", "memory", "builder", "cpu-reserve", "memory-reserve", "static-cmd", "static-dir"} {
|
||||||
if v, _ := cmd.Flags().GetString(f); v != "" {
|
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||||
key := f
|
key := f
|
||||||
// JSON uses snake_case keys matching the API schema.
|
// JSON uses snake_case keys matching the API schema.
|
||||||
@@ -94,6 +94,14 @@ var servicesCreateCmd = &cobra.Command{
|
|||||||
key = "restart_policy"
|
key = "restart_policy"
|
||||||
case "healthcheck-path":
|
case "healthcheck-path":
|
||||||
key = "healthcheck_path"
|
key = "healthcheck_path"
|
||||||
|
case "cpu-reserve":
|
||||||
|
key = "cpu_reserve"
|
||||||
|
case "memory-reserve":
|
||||||
|
key = "memory_reserve"
|
||||||
|
case "static-cmd":
|
||||||
|
key = "static_build_cmd"
|
||||||
|
case "static-dir":
|
||||||
|
key = "static_dir"
|
||||||
}
|
}
|
||||||
body[key] = v
|
body[key] = v
|
||||||
}
|
}
|
||||||
@@ -151,7 +159,7 @@ var servicesUpdateCmd = &cobra.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
body := map[string]interface{}{}
|
body := map[string]interface{}{}
|
||||||
for _, f := range []string{"name", "image", "command", "domain", "restart-policy", "healthcheck-path", "cpu", "memory"} {
|
for _, f := range []string{"name", "image", "command", "domain", "restart-policy", "healthcheck-path", "cpu", "memory", "builder", "cpu-reserve", "memory-reserve", "static-cmd", "static-dir"} {
|
||||||
if cmd.Flags().Changed(f) {
|
if cmd.Flags().Changed(f) {
|
||||||
key := f
|
key := f
|
||||||
switch f {
|
switch f {
|
||||||
@@ -159,6 +167,14 @@ var servicesUpdateCmd = &cobra.Command{
|
|||||||
key = "restart_policy"
|
key = "restart_policy"
|
||||||
case "healthcheck-path":
|
case "healthcheck-path":
|
||||||
key = "healthcheck_path"
|
key = "healthcheck_path"
|
||||||
|
case "cpu-reserve":
|
||||||
|
key = "cpu_reserve"
|
||||||
|
case "memory-reserve":
|
||||||
|
key = "memory_reserve"
|
||||||
|
case "static-cmd":
|
||||||
|
key = "static_build_cmd"
|
||||||
|
case "static-dir":
|
||||||
|
key = "static_dir"
|
||||||
}
|
}
|
||||||
v, _ := cmd.Flags().GetString(f)
|
v, _ := cmd.Flags().GetString(f)
|
||||||
body[key] = v
|
body[key] = v
|
||||||
@@ -179,6 +195,14 @@ var servicesUpdateCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
body["volumes"] = volumes
|
body["volumes"] = volumes
|
||||||
}
|
}
|
||||||
|
if cmd.Flags().Changed("sleep") {
|
||||||
|
v, _ := cmd.Flags().GetBool("sleep")
|
||||||
|
body["sleep_enabled"] = v
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("sleep-idle") {
|
||||||
|
v, _ := cmd.Flags().GetInt("sleep-idle")
|
||||||
|
body["sleep_idle_minutes"] = v
|
||||||
|
}
|
||||||
if cmd.Flags().Changed("maintenance") {
|
if cmd.Flags().Changed("maintenance") {
|
||||||
v, _ := cmd.Flags().GetString("maintenance")
|
v, _ := cmd.Flags().GetString("maintenance")
|
||||||
body["maintenance_mode"] = v == "on" || v == "true"
|
body["maintenance_mode"] = v == "on" || v == "true"
|
||||||
@@ -210,6 +234,61 @@ var servicesUpdateCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var servicesCloneCmd = &cobra.Command{
|
||||||
|
Use: "clone <id>",
|
||||||
|
Short: "Clone a service (config, volumes, domains, variables)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
for _, f := range []string{"name", "project", "environment"} {
|
||||||
|
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||||
|
key := f
|
||||||
|
if f == "project" {
|
||||||
|
key = "project_id"
|
||||||
|
}
|
||||||
|
body[key] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/services/"+args[0]+"/clone", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m, _ := unwrapObject(data)
|
||||||
|
fmt.Printf("Cloned to service %s (%s)\n", str(m, "service_id"), str(m, "name"))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var servicesMoveCmd = &cobra.Command{
|
||||||
|
Use: "move <id> <project-id>",
|
||||||
|
Short: "Move a service to another project",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/services/"+args[0]+"/move", map[string]interface{}{"project_id": args[1]})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fmt.Printf("Moved service %s to project %s\n", args[0], args[1])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// volumeFlagValues parses repeated "src:target[:ro]" flags into API mount
|
// volumeFlagValues parses repeated "src:target[:ro]" flags into API mount
|
||||||
// objects. Bind mounts are type=bind, named volumes type=volume.
|
// objects. Bind mounts are type=bind, named volumes type=volume.
|
||||||
func volumeFlagValues(cmd *cobra.Command, flags ...string) []map[string]interface{} {
|
func volumeFlagValues(cmd *cobra.Command, flags ...string) []map[string]interface{} {
|
||||||
@@ -406,6 +485,11 @@ func init() {
|
|||||||
f.String("memory", "", "memory limit (e.g. 512m)")
|
f.String("memory", "", "memory limit (e.g. 512m)")
|
||||||
f.Int("port", 0, "container port")
|
f.Int("port", 0, "container port")
|
||||||
f.Int("replicas", 0, "replica count")
|
f.Int("replicas", 0, "replica count")
|
||||||
|
f.String("builder", "", "build strategy: auto|railpack|nixpacks|dockerfile|static")
|
||||||
|
f.String("cpu-reserve", "", "soft CPU reservation (e.g. 0.25)")
|
||||||
|
f.String("memory-reserve", "", "soft memory reservation (e.g. 128Mi)")
|
||||||
|
f.String("static-cmd", "", "build command for the static builder")
|
||||||
|
f.String("static-dir", "", "output dir for the static builder (default dist)")
|
||||||
|
|
||||||
uf := servicesUpdateCmd.Flags()
|
uf := servicesUpdateCmd.Flags()
|
||||||
uf.String("name", "", "service name")
|
uf.String("name", "", "service name")
|
||||||
@@ -423,8 +507,15 @@ func init() {
|
|||||||
uf.Bool("clear-volumes", false, "remove all volume mounts")
|
uf.Bool("clear-volumes", false, "remove all volume mounts")
|
||||||
uf.String("maintenance", "", "maintenance mode: on|off")
|
uf.String("maintenance", "", "maintenance mode: on|off")
|
||||||
uf.StringArray("basic-auth", nil, "basic-auth credential user:password (repeatable, replaces all)")
|
uf.StringArray("basic-auth", nil, "basic-auth credential user:password (repeatable, replaces all)")
|
||||||
|
uf.String("builder", "", "build strategy: auto|railpack|nixpacks|dockerfile|static")
|
||||||
|
uf.String("cpu-reserve", "", "soft CPU reservation (empty clears)")
|
||||||
|
uf.String("memory-reserve", "", "soft memory reservation (empty clears)")
|
||||||
|
uf.String("static-cmd", "", "build command for the static builder (empty clears)")
|
||||||
|
uf.String("static-dir", "", "output dir for the static builder (empty clears)")
|
||||||
|
uf.Bool("sleep", false, "enable scale-to-zero on idle (sleep mode)")
|
||||||
|
uf.Int("sleep-idle", 0, "idle minutes before sleeping (1-1440)")
|
||||||
|
|
||||||
for _, a := range []string{"start", "stop", "restart", "redeploy"} {
|
for _, a := range []string{"start", "stop", "restart", "redeploy", "sleep", "wake"} {
|
||||||
verb := a
|
verb := a
|
||||||
ServicesCmd.AddCommand(&cobra.Command{
|
ServicesCmd.AddCommand(&cobra.Command{
|
||||||
Use: verb + " <id>",
|
Use: verb + " <id>",
|
||||||
@@ -433,7 +524,44 @@ func init() {
|
|||||||
RunE: serviceAction(verb),
|
RunE: serviceAction(verb),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
servicesCloneCmd.Flags().String("name", "", "name for the clone (default <name>-copy)")
|
||||||
|
servicesCloneCmd.Flags().String("project", "", "target project id (default: same project)")
|
||||||
|
servicesCloneCmd.Flags().String("environment", "", "target environment (default: source's)")
|
||||||
|
|
||||||
domainsAddCmd.Flags().Bool("default", false, "set as the default domain")
|
domainsAddCmd.Flags().Bool("default", false, "set as the default domain")
|
||||||
servicesDomainsCmd.AddCommand(domainsListCmd, domainsAddCmd, domainsRemoveCmd, domainsDefaultCmd, domainsCheckCmd)
|
servicesDomainsCmd.AddCommand(domainsListCmd, domainsAddCmd, domainsRemoveCmd, domainsDefaultCmd, domainsCheckCmd)
|
||||||
ServicesCmd.AddCommand(servicesListCmd, servicesGetCmd, servicesCreateCmd, servicesUpdateCmd, servicesDeleteCmd, servicesDomainsCmd)
|
ServicesCmd.AddCommand(servicesListCmd, servicesGetCmd, servicesCreateCmd, servicesUpdateCmd, servicesDeleteCmd, servicesDomainsCmd, servicesCloneCmd, servicesMoveCmd, servicesEnvCheckCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var servicesEnvCheckCmd = &cobra.Command{
|
||||||
|
Use: "env-check <id>",
|
||||||
|
Short: "Diagnose service environment: empty vars, unresolved ${{refs}}",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/services/"+args[0]+"/env-check", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if JSONMode() {
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m, _ := unwrapObject(data)
|
||||||
|
if b, ok := m["ok"].(bool); ok && b {
|
||||||
|
fmt.Println("env check: ok")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, group := range []string{"unresolved", "empty", "unreadable"} {
|
||||||
|
if list, ok := m[group].([]interface{}); ok {
|
||||||
|
for _, item := range list {
|
||||||
|
fmt.Printf("%s: %v\n", group, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &APIError{Message: "env check failed", ExitCode: ExitError}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -104,16 +107,31 @@ var templatesDeleteCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tplDeployVars []string
|
||||||
|
var tplDeployName string
|
||||||
|
|
||||||
var templatesDeployCmd = &cobra.Command{
|
var templatesDeployCmd = &cobra.Command{
|
||||||
Use: "deploy <template-id> <project-id>",
|
Use: "deploy <template-id> <project-id>",
|
||||||
Short: "Deploy a template into a project",
|
Short: "Deploy a template into a project",
|
||||||
|
Long: "Deploys a single-service template or a multi-service graph (v2) template. Pass --name for single-service templates; graph members take names from the template. Use --var KEY=VALUE to override template variables.",
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
c, err := client()
|
c, err := client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
data, err := c.Do("POST", "/templates/"+args[0]+"/deploy", map[string]string{"project_id": args[1]})
|
body := map[string]interface{}{"project_id": args[1]}
|
||||||
|
if tplDeployName != "" {
|
||||||
|
body["name"] = tplDeployName
|
||||||
|
}
|
||||||
|
vars, err := parseKVFlags(tplDeployVars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(vars) > 0 {
|
||||||
|
body["variables"] = vars
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/templates/"+args[0]+"/deploy", body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -122,6 +140,122 @@ var templatesDeployCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var templatesPlanCmd = &cobra.Command{
|
||||||
|
Use: "plan <template-id>",
|
||||||
|
Short: "Dry-resolve a template — expanded expressions, service refs, missing variables",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
vars, err := parseKVFlags(tplDeployVars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
if len(vars) > 0 {
|
||||||
|
body["variables"] = vars
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/templates/"+args[0]+"/plan", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var templatesImportComposeCmd = &cobra.Command{
|
||||||
|
Use: "import-compose <compose.yml|->",
|
||||||
|
Short: "Convert a docker-compose file into a template graph config",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var raw []byte
|
||||||
|
var err error
|
||||||
|
if args[0] == "-" {
|
||||||
|
raw, err = io.ReadAll(os.Stdin)
|
||||||
|
} else {
|
||||||
|
raw, err = os.ReadFile(args[0])
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot read compose file: %w", err)
|
||||||
|
}
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/templates/import/compose", map[string]string{"compose_yaml": string(raw)})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var templatesDeployGraphCmd = &cobra.Command{
|
||||||
|
Use: "deploy-graph <project-id> <config.json|->",
|
||||||
|
Short: "Deploy an ad-hoc service graph (v2 config with services[]) without a stored template",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var raw []byte
|
||||||
|
var err error
|
||||||
|
if args[1] == "-" {
|
||||||
|
raw, err = io.ReadAll(os.Stdin)
|
||||||
|
} else {
|
||||||
|
raw, err = os.ReadFile(args[1])
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot read config: %w", err)
|
||||||
|
}
|
||||||
|
var config json.RawMessage
|
||||||
|
if err := json.Unmarshal(raw, &config); err != nil {
|
||||||
|
return fmt.Errorf("invalid config JSON: %w", err)
|
||||||
|
}
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
vars, err := parseKVFlags(tplDeployVars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{"project_id": args[0]}
|
||||||
|
var cfg map[string]interface{}
|
||||||
|
if err := json.Unmarshal(config, &cfg); err != nil {
|
||||||
|
return fmt.Errorf("invalid config JSON: %w", err)
|
||||||
|
}
|
||||||
|
body["config"] = cfg
|
||||||
|
if len(vars) > 0 {
|
||||||
|
body["variables"] = vars
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/templates/deploy", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseKVFlags converts repeated --var KEY=VALUE flags into a map.
|
||||||
|
func parseKVFlags(flags []string) (map[string]string, error) {
|
||||||
|
out := map[string]string{}
|
||||||
|
for _, f := range flags {
|
||||||
|
idx := strings.Index(f, "=")
|
||||||
|
if idx <= 0 {
|
||||||
|
return nil, fmt.Errorf("invalid --var %q — expected KEY=VALUE", f)
|
||||||
|
}
|
||||||
|
out[f[:idx]] = f[idx+1:]
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
TemplatesCmd.AddCommand(templatesListCmd, templatesGetCmd, templatesCreateCmd, templatesDeleteCmd, templatesDeployCmd)
|
templatesDeployCmd.Flags().StringVar(&tplDeployName, "name", "", "Name for the created service (single-service templates)")
|
||||||
|
templatesDeployCmd.Flags().StringArrayVar(&tplDeployVars, "var", nil, "Template variable override (KEY=VALUE, repeatable)")
|
||||||
|
templatesPlanCmd.Flags().StringArrayVar(&tplDeployVars, "var", nil, "Template variable override (KEY=VALUE, repeatable)")
|
||||||
|
templatesDeployGraphCmd.Flags().StringArrayVar(&tplDeployVars, "var", nil, "Template variable override (KEY=VALUE, repeatable)")
|
||||||
|
TemplatesCmd.AddCommand(templatesListCmd, templatesGetCmd, templatesCreateCmd, templatesDeleteCmd, templatesDeployCmd, templatesPlanCmd, templatesImportComposeCmd, templatesDeployGraphCmd)
|
||||||
}
|
}
|
||||||
@@ -50,7 +50,7 @@ var varsSetCmd = &cobra.Command{
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
secret, _ := cmd.Flags().GetBool("secret")
|
secret, _ := cmd.Flags().GetBool("secret")
|
||||||
redeploy, _ := cmd.Flags().GetBool("redeploy")
|
redeploy, _ := cmd.Flags().GetBool("redeploy")
|
||||||
return mutateVars(args[0], redeploy, func(vars []map[string]interface{}) []map[string]interface{} {
|
return mutateVars(serviceVarsPath(args[0]), args[0], redeploy, func(vars []map[string]interface{}) []map[string]interface{} {
|
||||||
for _, pair := range args[1:] {
|
for _, pair := range args[1:] {
|
||||||
k, v, found := strings.Cut(pair, "=")
|
k, v, found := strings.Cut(pair, "=")
|
||||||
if !found || k == "" {
|
if !found || k == "" {
|
||||||
@@ -73,7 +73,7 @@ var varsUnsetCmd = &cobra.Command{
|
|||||||
drop[k] = true
|
drop[k] = true
|
||||||
}
|
}
|
||||||
redeploy, _ := cmd.Flags().GetBool("redeploy")
|
redeploy, _ := cmd.Flags().GetBool("redeploy")
|
||||||
return mutateVars(args[0], redeploy, func(vars []map[string]interface{}) []map[string]interface{} {
|
return mutateVars(serviceVarsPath(args[0]), args[0], redeploy, func(vars []map[string]interface{}) []map[string]interface{} {
|
||||||
out := vars[:0]
|
out := vars[:0]
|
||||||
for _, v := range vars {
|
for _, v := range vars {
|
||||||
if !drop[str(v, "key")] {
|
if !drop[str(v, "key")] {
|
||||||
@@ -85,20 +85,92 @@ var varsUnsetCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchVars(c *Client, serviceID string) ([]map[string]interface{}, error) {
|
// Shared variables — project scope, referenced as ${{shared.KEY}}.
|
||||||
data, err := c.Do("GET", "/services/"+serviceID+"/variables", nil)
|
|
||||||
if err != nil {
|
var varsSharedCmd = &cobra.Command{
|
||||||
return nil, err
|
Use: "shared",
|
||||||
}
|
Short: "Manage project-level shared variables (${{shared.KEY}})",
|
||||||
return unwrapList(data, "variables")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mutateVars(serviceID string, redeploy bool, mutate func([]map[string]interface{}) []map[string]interface{}) error {
|
var sharedVarsListCmd = &cobra.Command{
|
||||||
|
Use: "list <project-id>",
|
||||||
|
Short: "List shared project variables (secrets shown masked)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", projectVarsPath(args[0]), nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "variables")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, v := range items {
|
||||||
|
rows = append(rows, []string{str(v, "key"), str(v, "value"), str(v, "is_secret")})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"KEY", "VALUE", "SECRET"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var sharedVarsSetCmd = &cobra.Command{
|
||||||
|
Use: "set <project-id> KEY=VALUE [KEY=VALUE...]",
|
||||||
|
Short: "Set shared project variables",
|
||||||
|
Args: cobra.MinimumNArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
secret, _ := cmd.Flags().GetBool("secret")
|
||||||
|
return mutateVars(projectVarsPath(args[0]), args[0], false, func(vars []map[string]interface{}) []map[string]interface{} {
|
||||||
|
for _, pair := range args[1:] {
|
||||||
|
k, v, found := strings.Cut(pair, "=")
|
||||||
|
if !found || k == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
vars = upsertVar(vars, k, v, secret)
|
||||||
|
}
|
||||||
|
return vars
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var sharedVarsUnsetCmd = &cobra.Command{
|
||||||
|
Use: "unset <project-id> KEY [KEY...]",
|
||||||
|
Short: "Remove shared project variables",
|
||||||
|
Args: cobra.MinimumNArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
drop := map[string]bool{}
|
||||||
|
for _, k := range args[1:] {
|
||||||
|
drop[k] = true
|
||||||
|
}
|
||||||
|
return mutateVars(projectVarsPath(args[0]), args[0], false, func(vars []map[string]interface{}) []map[string]interface{} {
|
||||||
|
out := vars[:0]
|
||||||
|
for _, v := range vars {
|
||||||
|
if !drop[str(v, "key")] {
|
||||||
|
out = append(out, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func serviceVarsPath(serviceID string) string { return "/services/" + serviceID + "/variables" }
|
||||||
|
func projectVarsPath(projectID string) string { return "/projects/" + projectID + "/variables" }
|
||||||
|
|
||||||
|
func mutateVars(path, label string, redeploy bool, mutate func([]map[string]interface{}) []map[string]interface{}) error {
|
||||||
c, err := client()
|
c, err := client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
vars, err := fetchVars(c, serviceID)
|
data, err := c.Do("GET", path, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
vars, err := unwrapList(data, "variables")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -115,7 +187,7 @@ func mutateVars(serviceID string, redeploy bool, mutate func([]map[string]interf
|
|||||||
if redeploy {
|
if redeploy {
|
||||||
body["redeploy"] = true
|
body["redeploy"] = true
|
||||||
}
|
}
|
||||||
data, err := c.Do("PUT", "/services/"+serviceID+"/variables", body)
|
data, err = c.Do("PUT", path, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -125,9 +197,9 @@ func mutateVars(serviceID string, redeploy bool, mutate func([]map[string]interf
|
|||||||
}
|
}
|
||||||
obj, _ := unwrapObject(data)
|
obj, _ := unwrapObject(data)
|
||||||
if obj["redeploy_queued"] == true {
|
if obj["redeploy_queued"] == true {
|
||||||
fmt.Printf("Variables updated on %s — redeploy queued\n", serviceID)
|
fmt.Printf("Variables updated on %s — redeploy queued\n", label)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Variables updated on %s — redeploy to apply to running containers\n", serviceID)
|
fmt.Printf("Variables updated on %s — redeploy to apply to running containers\n", label)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -147,5 +219,7 @@ func init() {
|
|||||||
varsSetCmd.Flags().Bool("secret", false, "mark the variable(s) as secret")
|
varsSetCmd.Flags().Bool("secret", false, "mark the variable(s) as secret")
|
||||||
varsSetCmd.Flags().Bool("redeploy", false, "redeploy the service after saving")
|
varsSetCmd.Flags().Bool("redeploy", false, "redeploy the service after saving")
|
||||||
varsUnsetCmd.Flags().Bool("redeploy", false, "redeploy the service after saving")
|
varsUnsetCmd.Flags().Bool("redeploy", false, "redeploy the service after saving")
|
||||||
VariablesCmd.AddCommand(varsListCmd, varsSetCmd, varsUnsetCmd)
|
sharedVarsSetCmd.Flags().Bool("secret", false, "mark the variable(s) as secret")
|
||||||
|
varsSharedCmd.AddCommand(sharedVarsListCmd, sharedVarsSetCmd, sharedVarsUnsetCmd)
|
||||||
|
VariablesCmd.AddCommand(varsListCmd, varsSetCmd, varsUnsetCmd, varsSharedCmd)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,216 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var webhookEvents []string
|
||||||
|
var webhookHeaders []string
|
||||||
|
var webhookSecret string
|
||||||
|
var webhookName string
|
||||||
|
|
||||||
|
// WebhooksCmd manages outbound webhooks — signed HTTP event delivery.
|
||||||
|
var WebhooksCmd = &cobra.Command{
|
||||||
|
Use: "webhooks",
|
||||||
|
Aliases: []string{"hooks"},
|
||||||
|
Short: "Manage outbound webhooks (signed event delivery)",
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List webhooks",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/webhooks", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "webhooks")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, w := range items {
|
||||||
|
events := ""
|
||||||
|
if ev, ok := w["events"].([]interface{}); ok {
|
||||||
|
names := make([]string, 0, len(ev))
|
||||||
|
for _, e := range ev {
|
||||||
|
if s, ok := e.(string); ok {
|
||||||
|
names = append(names, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
events = strings.Join(names, ",")
|
||||||
|
}
|
||||||
|
rows = append(rows, []string{
|
||||||
|
str(w, "id"), str(w, "name"), str(w, "url"), events, str(w, "enabled"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"ID", "NAME", "URL", "EVENTS", "ENABLED"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func webhookBody(req map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
if len(webhookEvents) > 0 {
|
||||||
|
req["events"] = webhookEvents
|
||||||
|
}
|
||||||
|
if webhookSecret != "" {
|
||||||
|
req["secret"] = webhookSecret
|
||||||
|
}
|
||||||
|
if webhookName != "" {
|
||||||
|
req["name"] = webhookName
|
||||||
|
}
|
||||||
|
if len(webhookHeaders) > 0 {
|
||||||
|
hdrs, err := parseKVFlags(webhookHeaders)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req["headers"] = hdrs
|
||||||
|
}
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookCreateCmd = &cobra.Command{
|
||||||
|
Use: "create <name> <url>",
|
||||||
|
Short: "Create a webhook (events repeat with --event; use \"*\" for all)",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(webhookEvents) == 0 {
|
||||||
|
return fmt.Errorf("at least one --event is required (e.g. --event 'service.*' or --event '*')")
|
||||||
|
}
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body, err := webhookBody(map[string]interface{}{"name": args[0], "url": args[1]})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/webhooks", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookUpdateCmd = &cobra.Command{
|
||||||
|
Use: "update <id>",
|
||||||
|
Short: "Update a webhook (--name --url --event --secret --header --enabled/--disabled)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
body, err := webhookBody(map[string]interface{}{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if url, _ := cmd.Flags().GetString("url"); url != "" {
|
||||||
|
body["url"] = url
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("enabled") {
|
||||||
|
body["enabled"] = true
|
||||||
|
}
|
||||||
|
if cmd.Flags().Changed("disabled") {
|
||||||
|
body["enabled"] = false
|
||||||
|
}
|
||||||
|
data, err := c.Do("PATCH", "/webhooks/"+args[0], body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookDeleteCmd = &cobra.Command{
|
||||||
|
Use: "delete <id>",
|
||||||
|
Short: "Delete a webhook",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if !Confirm("Delete webhook " + args[0] + "?") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("DELETE", "/webhooks/"+args[0], nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookDeliveriesCmd = &cobra.Command{
|
||||||
|
Use: "deliveries <id>",
|
||||||
|
Short: "Show recent delivery attempts for a webhook",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("GET", "/webhooks/"+args[0]+"/deliveries", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := unwrapList(data, "deliveries")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([][]string, 0, len(items))
|
||||||
|
for _, d := range items {
|
||||||
|
rows = append(rows, []string{
|
||||||
|
str(d, "id"), str(d, "event"), str(d, "status"),
|
||||||
|
str(d, "response_status"), str(d, "attempts"), relTime(d, "created_at"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
printRows(data, []string{"ID", "EVENT", "STATUS", "HTTP", "TRIES", "AGE"}, rows)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookTestCmd = &cobra.Command{
|
||||||
|
Use: "test <id>",
|
||||||
|
Short: "Queue a test ping delivery",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
c, err := client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := c.Do("POST", "/webhooks/"+args[0]+"/test", map[string]interface{}{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
PrintRaw(data)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
webhookCreateCmd.Flags().StringArrayVar(&webhookEvents, "event", nil, "Event subscription (e.g. 'service.*', 'deployment.fail', '*'; repeatable)")
|
||||||
|
webhookCreateCmd.Flags().StringVar(&webhookSecret, "secret", "", "Signing secret (auto-generated when omitted)")
|
||||||
|
webhookCreateCmd.Flags().StringArrayVar(&webhookHeaders, "header", nil, "Extra request header KEY=VALUE (repeatable)")
|
||||||
|
webhookUpdateCmd.Flags().StringVar(&webhookName, "name", "", "New name")
|
||||||
|
webhookUpdateCmd.Flags().String("url", "", "New URL")
|
||||||
|
webhookUpdateCmd.Flags().StringArrayVar(&webhookEvents, "event", nil, "Replace event subscriptions (repeatable)")
|
||||||
|
webhookUpdateCmd.Flags().StringVar(&webhookSecret, "secret", "", "New signing secret")
|
||||||
|
webhookUpdateCmd.Flags().StringArrayVar(&webhookHeaders, "header", nil, "Replace request headers KEY=VALUE (repeatable)")
|
||||||
|
webhookUpdateCmd.Flags().Bool("enabled", false, "Enable the webhook")
|
||||||
|
webhookUpdateCmd.Flags().Bool("disabled", false, "Disable the webhook")
|
||||||
|
|
||||||
|
WebhooksCmd.AddCommand(webhookListCmd, webhookCreateCmd, webhookUpdateCmd, webhookDeleteCmd, webhookDeliveriesCmd, webhookTestCmd)
|
||||||
|
}
|
||||||
@@ -74,9 +74,12 @@ func init() {
|
|||||||
rootCmd.AddCommand(commands.ExecCmd)
|
rootCmd.AddCommand(commands.ExecCmd)
|
||||||
rootCmd.AddCommand(commands.VariablesCmd)
|
rootCmd.AddCommand(commands.VariablesCmd)
|
||||||
rootCmd.AddCommand(commands.VolumesCmd)
|
rootCmd.AddCommand(commands.VolumesCmd)
|
||||||
|
rootCmd.AddCommand(commands.RegistriesCmd)
|
||||||
|
rootCmd.AddCommand(commands.BackupTargetsCmd)
|
||||||
rootCmd.AddCommand(commands.DatabasesCmd)
|
rootCmd.AddCommand(commands.DatabasesCmd)
|
||||||
rootCmd.AddCommand(commands.CronCmd)
|
rootCmd.AddCommand(commands.CronCmd)
|
||||||
rootCmd.AddCommand(commands.TemplatesCmd)
|
rootCmd.AddCommand(commands.TemplatesCmd)
|
||||||
|
rootCmd.AddCommand(commands.ImportCmd)
|
||||||
rootCmd.AddCommand(commands.NodesCmd)
|
rootCmd.AddCommand(commands.NodesCmd)
|
||||||
rootCmd.AddCommand(commands.ScalingCmd)
|
rootCmd.AddCommand(commands.ScalingCmd)
|
||||||
rootCmd.AddCommand(commands.HACmd)
|
rootCmd.AddCommand(commands.HACmd)
|
||||||
@@ -84,7 +87,14 @@ func init() {
|
|||||||
rootCmd.AddCommand(commands.GatewayCmd)
|
rootCmd.AddCommand(commands.GatewayCmd)
|
||||||
rootCmd.AddCommand(commands.AdminCmd)
|
rootCmd.AddCommand(commands.AdminCmd)
|
||||||
rootCmd.AddCommand(commands.NotificationsCmd)
|
rootCmd.AddCommand(commands.NotificationsCmd)
|
||||||
|
rootCmd.AddCommand(commands.WebhooksCmd)
|
||||||
|
rootCmd.AddCommand(commands.OperationsCmd)
|
||||||
|
rootCmd.AddCommand(commands.ActivityCmd)
|
||||||
|
rootCmd.AddCommand(commands.ShellCmd)
|
||||||
|
rootCmd.AddCommand(commands.BannersCmd)
|
||||||
|
rootCmd.AddCommand(commands.InvitesCmd)
|
||||||
rootCmd.AddCommand(commands.UpCmd)
|
rootCmd.AddCommand(commands.UpCmd)
|
||||||
|
rootCmd.AddCommand(commands.MigrateCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ INSERT INTO node_agents (
|
|||||||
id, name, hostname, ip_address, port, status, version,
|
id, name, hostname, ip_address, port, status, version,
|
||||||
capabilities, resources, last_heartbeat, metadata
|
capabilities, resources, last_heartbeat, metadata
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||||
RETURNING id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata
|
RETURNING id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata, auto_prune
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateAgentParams struct {
|
type CreateAgentParams struct {
|
||||||
@@ -109,6 +109,7 @@ func (q *Queries) CreateAgent(ctx context.Context, arg CreateAgentParams) (NodeA
|
|||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.Metadata,
|
&i.Metadata,
|
||||||
|
&i.AutoPrune,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -229,7 +230,7 @@ func (q *Queries) DeleteAgent(ctx context.Context, id string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getAgent = `-- name: GetAgent :one
|
const getAgent = `-- name: GetAgent :one
|
||||||
SELECT id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata FROM node_agents WHERE id = $1
|
SELECT id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata, auto_prune FROM node_agents WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetAgent(ctx context.Context, id string) (NodeAgent, error) {
|
func (q *Queries) GetAgent(ctx context.Context, id string) (NodeAgent, error) {
|
||||||
@@ -249,12 +250,13 @@ func (q *Queries) GetAgent(ctx context.Context, id string) (NodeAgent, error) {
|
|||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.Metadata,
|
&i.Metadata,
|
||||||
|
&i.AutoPrune,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getAgentByHostAndIP = `-- name: GetAgentByHostAndIP :one
|
const getAgentByHostAndIP = `-- name: GetAgentByHostAndIP :one
|
||||||
SELECT id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata FROM node_agents
|
SELECT id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata, auto_prune FROM node_agents
|
||||||
WHERE hostname = $1 AND ip_address = $2
|
WHERE hostname = $1 AND ip_address = $2
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`
|
`
|
||||||
@@ -281,6 +283,7 @@ func (q *Queries) GetAgentByHostAndIP(ctx context.Context, arg GetAgentByHostAnd
|
|||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.Metadata,
|
&i.Metadata,
|
||||||
|
&i.AutoPrune,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -378,6 +381,36 @@ func (q *Queries) GetContainerForAgent(ctx context.Context, arg GetContainerForA
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getLastAgentCommandByType = `-- name: GetLastAgentCommandByType :one
|
||||||
|
SELECT id, type, node_agent_id, container_id, payload, status, result, error, created_at, updated_at, completed_at FROM agent_commands
|
||||||
|
WHERE node_agent_id = $1 AND type = $2
|
||||||
|
ORDER BY created_at DESC LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetLastAgentCommandByTypeParams struct {
|
||||||
|
NodeAgentID string `json:"node_agent_id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetLastAgentCommandByType(ctx context.Context, arg GetLastAgentCommandByTypeParams) (AgentCommand, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getLastAgentCommandByType, arg.NodeAgentID, arg.Type)
|
||||||
|
var i AgentCommand
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Type,
|
||||||
|
&i.NodeAgentID,
|
||||||
|
&i.ContainerID,
|
||||||
|
&i.Payload,
|
||||||
|
&i.Status,
|
||||||
|
&i.Result,
|
||||||
|
&i.Error,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.CompletedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const insertAgentHeartbeat = `-- name: InsertAgentHeartbeat :exec
|
const insertAgentHeartbeat = `-- name: InsertAgentHeartbeat :exec
|
||||||
INSERT INTO agent_heartbeats (
|
INSERT INTO agent_heartbeats (
|
||||||
id, node_agent_id, timestamp, status, resources,
|
id, node_agent_id, timestamp, status, resources,
|
||||||
@@ -458,7 +491,7 @@ func (q *Queries) ListAgentHeartbeatsSince(ctx context.Context, arg ListAgentHea
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listAgents = `-- name: ListAgents :many
|
const listAgents = `-- name: ListAgents :many
|
||||||
SELECT id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata FROM node_agents ORDER BY created_at ASC
|
SELECT id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata, auto_prune FROM node_agents ORDER BY created_at ASC
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) ListAgents(ctx context.Context) ([]NodeAgent, error) {
|
func (q *Queries) ListAgents(ctx context.Context) ([]NodeAgent, error) {
|
||||||
@@ -484,6 +517,7 @@ func (q *Queries) ListAgents(ctx context.Context) ([]NodeAgent, error) {
|
|||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.Metadata,
|
&i.Metadata,
|
||||||
|
&i.AutoPrune,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -639,9 +673,10 @@ UPDATE node_agents SET
|
|||||||
resources = $9,
|
resources = $9,
|
||||||
last_heartbeat = $10,
|
last_heartbeat = $10,
|
||||||
metadata = $11,
|
metadata = $11,
|
||||||
|
auto_prune = $12,
|
||||||
updated_at = NOW()
|
updated_at = NOW()
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
RETURNING id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata
|
RETURNING id, name, hostname, ip_address, port, status, version, capabilities, resources, last_heartbeat, created_at, updated_at, metadata, auto_prune
|
||||||
`
|
`
|
||||||
|
|
||||||
type UpdateAgentParams struct {
|
type UpdateAgentParams struct {
|
||||||
@@ -656,6 +691,7 @@ type UpdateAgentParams struct {
|
|||||||
Resources pqtype.NullRawMessage `json:"resources"`
|
Resources pqtype.NullRawMessage `json:"resources"`
|
||||||
LastHeartbeat sql.NullTime `json:"last_heartbeat"`
|
LastHeartbeat sql.NullTime `json:"last_heartbeat"`
|
||||||
Metadata pqtype.NullRawMessage `json:"metadata"`
|
Metadata pqtype.NullRawMessage `json:"metadata"`
|
||||||
|
AutoPrune bool `json:"auto_prune"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) UpdateAgent(ctx context.Context, arg UpdateAgentParams) (NodeAgent, error) {
|
func (q *Queries) UpdateAgent(ctx context.Context, arg UpdateAgentParams) (NodeAgent, error) {
|
||||||
@@ -671,6 +707,7 @@ func (q *Queries) UpdateAgent(ctx context.Context, arg UpdateAgentParams) (NodeA
|
|||||||
arg.Resources,
|
arg.Resources,
|
||||||
arg.LastHeartbeat,
|
arg.LastHeartbeat,
|
||||||
arg.Metadata,
|
arg.Metadata,
|
||||||
|
arg.AutoPrune,
|
||||||
)
|
)
|
||||||
var i NodeAgent
|
var i NodeAgent
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
@@ -687,6 +724,7 @@ func (q *Queries) UpdateAgent(ctx context.Context, arg UpdateAgentParams) (NodeA
|
|||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.Metadata,
|
&i.Metadata,
|
||||||
|
&i.AutoPrune,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,253 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: backup_targets.sql
|
||||||
|
|
||||||
|
package sqlcdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
const countDatabasesUsingBackupTarget = `-- name: CountDatabasesUsingBackupTarget :one
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM database_services
|
||||||
|
WHERE backup_target_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) CountDatabasesUsingBackupTarget(ctx context.Context, targetID sql.NullString) (int64, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, countDatabasesUsingBackupTarget, targetID)
|
||||||
|
var count int64
|
||||||
|
err := row.Scan(&count)
|
||||||
|
return count, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const createBackupTarget = `-- name: CreateBackupTarget :exec
|
||||||
|
INSERT INTO backup_targets (id, user_id, name, endpoint, bucket, region, prefix, access_key, secret_key, use_tls, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateBackupTargetParams struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Endpoint string `json:"endpoint"`
|
||||||
|
Bucket string `json:"bucket"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
Prefix string `json:"prefix"`
|
||||||
|
AccessKey string `json:"access_key"`
|
||||||
|
SecretKey string `json:"secret_key"`
|
||||||
|
UseTls bool `json:"use_tls"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateBackupTarget(ctx context.Context, arg CreateBackupTargetParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, createBackupTarget,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Name,
|
||||||
|
arg.Endpoint,
|
||||||
|
arg.Bucket,
|
||||||
|
arg.Region,
|
||||||
|
arg.Prefix,
|
||||||
|
arg.AccessKey,
|
||||||
|
arg.SecretKey,
|
||||||
|
arg.UseTls,
|
||||||
|
arg.CreatedAt,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteBackupTargetByIDAndUser = `-- name: DeleteBackupTargetByIDAndUser :exec
|
||||||
|
DELETE FROM backup_targets
|
||||||
|
WHERE id = $1 AND user_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type DeleteBackupTargetByIDAndUserParams struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) DeleteBackupTargetByIDAndUser(ctx context.Context, arg DeleteBackupTargetByIDAndUserParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deleteBackupTargetByIDAndUser, arg.ID, arg.UserID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getBackupTargetByID = `-- name: GetBackupTargetByID :one
|
||||||
|
SELECT backup_targets.id, backup_targets.user_id, backup_targets.name, backup_targets.endpoint, backup_targets.bucket, backup_targets.region, backup_targets.prefix, backup_targets.access_key, backup_targets.secret_key, backup_targets.use_tls, backup_targets.created_at, backup_targets.updated_at
|
||||||
|
FROM backup_targets
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetBackupTargetByID(ctx context.Context, id string) (BackupTarget, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getBackupTargetByID, id)
|
||||||
|
var i BackupTarget
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Endpoint,
|
||||||
|
&i.Bucket,
|
||||||
|
&i.Region,
|
||||||
|
&i.Prefix,
|
||||||
|
&i.AccessKey,
|
||||||
|
&i.SecretKey,
|
||||||
|
&i.UseTls,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getBackupTargetByIDAndUser = `-- name: GetBackupTargetByIDAndUser :one
|
||||||
|
SELECT backup_targets.id, backup_targets.user_id, backup_targets.name, backup_targets.endpoint, backup_targets.bucket, backup_targets.region, backup_targets.prefix, backup_targets.access_key, backup_targets.secret_key, backup_targets.use_tls, backup_targets.created_at, backup_targets.updated_at
|
||||||
|
FROM backup_targets
|
||||||
|
WHERE id = $1 AND user_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetBackupTargetByIDAndUserParams struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetBackupTargetByIDAndUser(ctx context.Context, arg GetBackupTargetByIDAndUserParams) (BackupTarget, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getBackupTargetByIDAndUser, arg.ID, arg.UserID)
|
||||||
|
var i BackupTarget
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Endpoint,
|
||||||
|
&i.Bucket,
|
||||||
|
&i.Region,
|
||||||
|
&i.Prefix,
|
||||||
|
&i.AccessKey,
|
||||||
|
&i.SecretKey,
|
||||||
|
&i.UseTls,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listBackupTargetsByUser = `-- name: ListBackupTargetsByUser :many
|
||||||
|
SELECT backup_targets.id, backup_targets.user_id, backup_targets.name, backup_targets.endpoint, backup_targets.bucket, backup_targets.region, backup_targets.prefix, backup_targets.access_key, backup_targets.secret_key, backup_targets.use_tls, backup_targets.created_at, backup_targets.updated_at
|
||||||
|
FROM backup_targets
|
||||||
|
WHERE user_id = $1
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListBackupTargetsByUser(ctx context.Context, userID string) ([]BackupTarget, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listBackupTargetsByUser, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []BackupTarget{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i BackupTarget
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Endpoint,
|
||||||
|
&i.Bucket,
|
||||||
|
&i.Region,
|
||||||
|
&i.Prefix,
|
||||||
|
&i.AccessKey,
|
||||||
|
&i.SecretKey,
|
||||||
|
&i.UseTls,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const setDatabaseBackupRemoteKeyByID = `-- name: SetDatabaseBackupRemoteKeyByID :exec
|
||||||
|
UPDATE database_backups
|
||||||
|
SET remote_key = $1
|
||||||
|
WHERE id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type SetDatabaseBackupRemoteKeyByIDParams struct {
|
||||||
|
RemoteKey sql.NullString `json:"remote_key"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) SetDatabaseBackupRemoteKeyByID(ctx context.Context, arg SetDatabaseBackupRemoteKeyByIDParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, setDatabaseBackupRemoteKeyByID, arg.RemoteKey, arg.ID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const setDatabaseBackupTargetByIDAndUser = `-- name: SetDatabaseBackupTargetByIDAndUser :exec
|
||||||
|
UPDATE database_services
|
||||||
|
SET backup_target_id = $1, updated_at = $2
|
||||||
|
WHERE id = $3 AND user_id = $4
|
||||||
|
`
|
||||||
|
|
||||||
|
type SetDatabaseBackupTargetByIDAndUserParams struct {
|
||||||
|
BackupTargetID sql.NullString `json:"backup_target_id"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) SetDatabaseBackupTargetByIDAndUser(ctx context.Context, arg SetDatabaseBackupTargetByIDAndUserParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, setDatabaseBackupTargetByIDAndUser,
|
||||||
|
arg.BackupTargetID,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateBackupTargetByIDAndUser = `-- name: UpdateBackupTargetByIDAndUser :exec
|
||||||
|
UPDATE backup_targets
|
||||||
|
SET name = $1, endpoint = $2, bucket = $3, region = $4, prefix = $5, access_key = $6, secret_key = $7, use_tls = $8, updated_at = $9
|
||||||
|
WHERE id = $10 AND user_id = $11
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateBackupTargetByIDAndUserParams struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Endpoint string `json:"endpoint"`
|
||||||
|
Bucket string `json:"bucket"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
Prefix string `json:"prefix"`
|
||||||
|
AccessKey string `json:"access_key"`
|
||||||
|
SecretKey string `json:"secret_key"`
|
||||||
|
UseTls bool `json:"use_tls"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateBackupTargetByIDAndUser(ctx context.Context, arg UpdateBackupTargetByIDAndUserParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, updateBackupTargetByIDAndUser,
|
||||||
|
arg.Name,
|
||||||
|
arg.Endpoint,
|
||||||
|
arg.Bucket,
|
||||||
|
arg.Region,
|
||||||
|
arg.Prefix,
|
||||||
|
arg.AccessKey,
|
||||||
|
arg.SecretKey,
|
||||||
|
arg.UseTls,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,231 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: banners.sql
|
||||||
|
|
||||||
|
package sqlcdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createBanner = `-- name: CreateBanner :one
|
||||||
|
INSERT INTO banners (title, body, level, active, dismissible, created_by, starts_at, ends_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
|
RETURNING banners.id, banners.title, banners.body, banners.level, banners.active, banners.dismissible, banners.created_by, banners.starts_at, banners.ends_at, banners.created_at, banners.updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateBannerParams struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Level string `json:"level"`
|
||||||
|
Active bool `json:"active"`
|
||||||
|
Dismissible bool `json:"dismissible"`
|
||||||
|
CreatedBy uuid.NullUUID `json:"created_by"`
|
||||||
|
StartsAt sql.NullTime `json:"starts_at"`
|
||||||
|
EndsAt sql.NullTime `json:"ends_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateBanner(ctx context.Context, arg CreateBannerParams) (Banner, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, createBanner,
|
||||||
|
arg.Title,
|
||||||
|
arg.Body,
|
||||||
|
arg.Level,
|
||||||
|
arg.Active,
|
||||||
|
arg.Dismissible,
|
||||||
|
arg.CreatedBy,
|
||||||
|
arg.StartsAt,
|
||||||
|
arg.EndsAt,
|
||||||
|
)
|
||||||
|
var i Banner
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Body,
|
||||||
|
&i.Level,
|
||||||
|
&i.Active,
|
||||||
|
&i.Dismissible,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.StartsAt,
|
||||||
|
&i.EndsAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteBanner = `-- name: DeleteBanner :exec
|
||||||
|
DELETE FROM banners
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeleteBanner(ctx context.Context, id uuid.UUID) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deleteBanner, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getBannerByID = `-- name: GetBannerByID :one
|
||||||
|
SELECT banners.id, banners.title, banners.body, banners.level, banners.active, banners.dismissible, banners.created_by, banners.starts_at, banners.ends_at, banners.created_at, banners.updated_at
|
||||||
|
FROM banners
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetBannerByID(ctx context.Context, id uuid.UUID) (Banner, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getBannerByID, id)
|
||||||
|
var i Banner
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Body,
|
||||||
|
&i.Level,
|
||||||
|
&i.Active,
|
||||||
|
&i.Dismissible,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.StartsAt,
|
||||||
|
&i.EndsAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listActiveBanners = `-- name: ListActiveBanners :many
|
||||||
|
SELECT banners.id, banners.title, banners.body, banners.level, banners.active, banners.dismissible, banners.created_by, banners.starts_at, banners.ends_at, banners.created_at, banners.updated_at
|
||||||
|
FROM banners
|
||||||
|
WHERE active
|
||||||
|
AND (starts_at IS NULL OR starts_at <= now())
|
||||||
|
AND (ends_at IS NULL OR ends_at > now())
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListActiveBanners(ctx context.Context) ([]Banner, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listActiveBanners)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []Banner{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i Banner
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Body,
|
||||||
|
&i.Level,
|
||||||
|
&i.Active,
|
||||||
|
&i.Dismissible,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.StartsAt,
|
||||||
|
&i.EndsAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const listBanners = `-- name: ListBanners :many
|
||||||
|
SELECT banners.id, banners.title, banners.body, banners.level, banners.active, banners.dismissible, banners.created_by, banners.starts_at, banners.ends_at, banners.created_at, banners.updated_at
|
||||||
|
FROM banners
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListBanners(ctx context.Context) ([]Banner, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listBanners)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []Banner{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i Banner
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Body,
|
||||||
|
&i.Level,
|
||||||
|
&i.Active,
|
||||||
|
&i.Dismissible,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.StartsAt,
|
||||||
|
&i.EndsAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateBanner = `-- name: UpdateBanner :one
|
||||||
|
UPDATE banners
|
||||||
|
SET title = $2,
|
||||||
|
body = $3,
|
||||||
|
level = $4,
|
||||||
|
active = $5,
|
||||||
|
dismissible = $6,
|
||||||
|
starts_at = $7,
|
||||||
|
ends_at = $8,
|
||||||
|
updated_at = now()
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING banners.id, banners.title, banners.body, banners.level, banners.active, banners.dismissible, banners.created_by, banners.starts_at, banners.ends_at, banners.created_at, banners.updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateBannerParams struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Level string `json:"level"`
|
||||||
|
Active bool `json:"active"`
|
||||||
|
Dismissible bool `json:"dismissible"`
|
||||||
|
StartsAt sql.NullTime `json:"starts_at"`
|
||||||
|
EndsAt sql.NullTime `json:"ends_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateBanner(ctx context.Context, arg UpdateBannerParams) (Banner, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, updateBanner,
|
||||||
|
arg.ID,
|
||||||
|
arg.Title,
|
||||||
|
arg.Body,
|
||||||
|
arg.Level,
|
||||||
|
arg.Active,
|
||||||
|
arg.Dismissible,
|
||||||
|
arg.StartsAt,
|
||||||
|
arg.EndsAt,
|
||||||
|
)
|
||||||
|
var i Banner
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Body,
|
||||||
|
&i.Level,
|
||||||
|
&i.Active,
|
||||||
|
&i.Dismissible,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.StartsAt,
|
||||||
|
&i.EndsAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ package sqlcdb
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const countDatabaseServicesByUserAndName = `-- name: CountDatabaseServicesByUserAndName :one
|
const countDatabaseServicesByUserAndName = `-- name: CountDatabaseServicesByUserAndName :one
|
||||||
@@ -55,21 +57,23 @@ func (q *Queries) CreateDatabaseBackup(ctx context.Context, arg CreateDatabaseBa
|
|||||||
}
|
}
|
||||||
|
|
||||||
const createDatabaseService = `-- name: CreateDatabaseService :exec
|
const createDatabaseService = `-- name: CreateDatabaseService :exec
|
||||||
INSERT INTO database_services (id, user_id, name, type, status, version, plan, region, created_at, updated_at)
|
INSERT INTO database_services (id, user_id, name, type, status, version, plan, region, public_port, project_id, created_at, updated_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateDatabaseServiceParams struct {
|
type CreateDatabaseServiceParams struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
UserID string `json:"user_id"`
|
UserID string `json:"user_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
Plan string `json:"plan"`
|
Plan string `json:"plan"`
|
||||||
Region string `json:"region"`
|
Region string `json:"region"`
|
||||||
CreatedAt sql.NullTime `json:"created_at"`
|
PublicPort bool `json:"public_port"`
|
||||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
ProjectID uuid.NullUUID `json:"project_id"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateDatabaseService(ctx context.Context, arg CreateDatabaseServiceParams) error {
|
func (q *Queries) CreateDatabaseService(ctx context.Context, arg CreateDatabaseServiceParams) error {
|
||||||
@@ -82,6 +86,54 @@ func (q *Queries) CreateDatabaseService(ctx context.Context, arg CreateDatabaseS
|
|||||||
arg.Version,
|
arg.Version,
|
||||||
arg.Plan,
|
arg.Plan,
|
||||||
arg.Region,
|
arg.Region,
|
||||||
|
arg.PublicPort,
|
||||||
|
arg.ProjectID,
|
||||||
|
arg.CreatedAt,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const createExternalDatabaseService = `-- name: CreateExternalDatabaseService :exec
|
||||||
|
INSERT INTO database_services (id, user_id, name, type, status, version, plan, region,
|
||||||
|
provider, external_host, external_port, external_name, external_username, external_password, external_ssl,
|
||||||
|
created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, 'running', $5, $6, $7, 'external', $8, $9, $10, $11, $12, $13, $14, $15)
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateExternalDatabaseServiceParams struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
Plan string `json:"plan"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
ExternalHost sql.NullString `json:"external_host"`
|
||||||
|
ExternalPort sql.NullInt32 `json:"external_port"`
|
||||||
|
ExternalName sql.NullString `json:"external_name"`
|
||||||
|
ExternalUsername sql.NullString `json:"external_username"`
|
||||||
|
ExternalPassword string `json:"external_password"`
|
||||||
|
ExternalSsl bool `json:"external_ssl"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateExternalDatabaseService(ctx context.Context, arg CreateExternalDatabaseServiceParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, createExternalDatabaseService,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Name,
|
||||||
|
arg.Type,
|
||||||
|
arg.Version,
|
||||||
|
arg.Plan,
|
||||||
|
arg.Region,
|
||||||
|
arg.ExternalHost,
|
||||||
|
arg.ExternalPort,
|
||||||
|
arg.ExternalName,
|
||||||
|
arg.ExternalUsername,
|
||||||
|
arg.ExternalPassword,
|
||||||
|
arg.ExternalSsl,
|
||||||
arg.CreatedAt,
|
arg.CreatedAt,
|
||||||
arg.UpdatedAt,
|
arg.UpdatedAt,
|
||||||
)
|
)
|
||||||
@@ -106,6 +158,16 @@ func (q *Queries) DatabaseServiceExistsByIDAndUser(ctx context.Context, arg Data
|
|||||||
return exists, err
|
return exists, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deleteDatabaseServiceByID = `-- name: DeleteDatabaseServiceByID :exec
|
||||||
|
DELETE FROM database_services
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeleteDatabaseServiceByID(ctx context.Context, id string) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deleteDatabaseServiceByID, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const deleteDatabaseServiceByIDAndUser = `-- name: DeleteDatabaseServiceByIDAndUser :exec
|
const deleteDatabaseServiceByIDAndUser = `-- name: DeleteDatabaseServiceByIDAndUser :exec
|
||||||
DELETE FROM database_services
|
DELETE FROM database_services
|
||||||
WHERE id = $1 AND user_id = $2
|
WHERE id = $1 AND user_id = $2
|
||||||
@@ -122,7 +184,7 @@ func (q *Queries) DeleteDatabaseServiceByIDAndUser(ctx context.Context, arg Dele
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getDatabaseBackupByIDAndDatabaseAndUser = `-- name: GetDatabaseBackupByIDAndDatabaseAndUser :one
|
const getDatabaseBackupByIDAndDatabaseAndUser = `-- name: GetDatabaseBackupByIDAndDatabaseAndUser :one
|
||||||
SELECT b.id, b.database_id, b.size, b.status, b.backup_path, b.created_at, b.completed_at
|
SELECT b.id, b.database_id, b.size, b.status, b.backup_path, b.remote_key, b.created_at, b.completed_at
|
||||||
FROM database_backups b
|
FROM database_backups b
|
||||||
JOIN database_services s ON s.id = b.database_id
|
JOIN database_services s ON s.id = b.database_id
|
||||||
WHERE b.id = $1 AND b.database_id = $2 AND s.user_id = $3
|
WHERE b.id = $1 AND b.database_id = $2 AND s.user_id = $3
|
||||||
@@ -143,6 +205,7 @@ func (q *Queries) GetDatabaseBackupByIDAndDatabaseAndUser(ctx context.Context, a
|
|||||||
&i.Size,
|
&i.Size,
|
||||||
&i.Status,
|
&i.Status,
|
||||||
&i.BackupPath,
|
&i.BackupPath,
|
||||||
|
&i.RemoteKey,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.CompletedAt,
|
&i.CompletedAt,
|
||||||
)
|
)
|
||||||
@@ -150,7 +213,7 @@ func (q *Queries) GetDatabaseBackupByIDAndDatabaseAndUser(ctx context.Context, a
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getDatabaseServiceByID = `-- name: GetDatabaseServiceByID :one
|
const getDatabaseServiceByID = `-- name: GetDatabaseServiceByID :one
|
||||||
SELECT id, user_id, name, type, status, version, plan, region, connection_url, backup_schedule, next_backup_at, created_at, updated_at
|
SELECT database_services.id, database_services.user_id, database_services.name, database_services.type, database_services.status, database_services.version, database_services.plan, database_services.region, database_services.connection_url, database_services.backup_schedule, database_services.next_backup_at, database_services.provider, database_services.external_host, database_services.external_port, database_services.external_name, database_services.external_username, database_services.external_password, database_services.external_ssl, database_services.public_port, database_services.backup_target_id, database_services.project_id, database_services.created_at, database_services.updated_at
|
||||||
FROM database_services
|
FROM database_services
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
@@ -170,6 +233,16 @@ func (q *Queries) GetDatabaseServiceByID(ctx context.Context, id string) (Databa
|
|||||||
&i.ConnectionUrl,
|
&i.ConnectionUrl,
|
||||||
&i.BackupSchedule,
|
&i.BackupSchedule,
|
||||||
&i.NextBackupAt,
|
&i.NextBackupAt,
|
||||||
|
&i.Provider,
|
||||||
|
&i.ExternalHost,
|
||||||
|
&i.ExternalPort,
|
||||||
|
&i.ExternalName,
|
||||||
|
&i.ExternalUsername,
|
||||||
|
&i.ExternalPassword,
|
||||||
|
&i.ExternalSsl,
|
||||||
|
&i.PublicPort,
|
||||||
|
&i.BackupTargetID,
|
||||||
|
&i.ProjectID,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
)
|
)
|
||||||
@@ -177,7 +250,7 @@ func (q *Queries) GetDatabaseServiceByID(ctx context.Context, id string) (Databa
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getDatabaseServiceByIDAndUser = `-- name: GetDatabaseServiceByIDAndUser :one
|
const getDatabaseServiceByIDAndUser = `-- name: GetDatabaseServiceByIDAndUser :one
|
||||||
SELECT id, user_id, name, type, status, version, plan, region, connection_url, backup_schedule, next_backup_at, created_at, updated_at
|
SELECT database_services.id, database_services.user_id, database_services.name, database_services.type, database_services.status, database_services.version, database_services.plan, database_services.region, database_services.connection_url, database_services.backup_schedule, database_services.next_backup_at, database_services.provider, database_services.external_host, database_services.external_port, database_services.external_name, database_services.external_username, database_services.external_password, database_services.external_ssl, database_services.public_port, database_services.backup_target_id, database_services.project_id, database_services.created_at, database_services.updated_at
|
||||||
FROM database_services
|
FROM database_services
|
||||||
WHERE id = $1 AND user_id = $2
|
WHERE id = $1 AND user_id = $2
|
||||||
`
|
`
|
||||||
@@ -202,6 +275,16 @@ func (q *Queries) GetDatabaseServiceByIDAndUser(ctx context.Context, arg GetData
|
|||||||
&i.ConnectionUrl,
|
&i.ConnectionUrl,
|
||||||
&i.BackupSchedule,
|
&i.BackupSchedule,
|
||||||
&i.NextBackupAt,
|
&i.NextBackupAt,
|
||||||
|
&i.Provider,
|
||||||
|
&i.ExternalHost,
|
||||||
|
&i.ExternalPort,
|
||||||
|
&i.ExternalName,
|
||||||
|
&i.ExternalUsername,
|
||||||
|
&i.ExternalPassword,
|
||||||
|
&i.ExternalSsl,
|
||||||
|
&i.PublicPort,
|
||||||
|
&i.BackupTargetID,
|
||||||
|
&i.ProjectID,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
)
|
)
|
||||||
@@ -222,7 +305,7 @@ func (q *Queries) GetDatabaseServiceOwnerID(ctx context.Context, id string) (str
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listAllDatabaseServices = `-- name: ListAllDatabaseServices :many
|
const listAllDatabaseServices = `-- name: ListAllDatabaseServices :many
|
||||||
SELECT id, user_id, name, type, status, version, plan, region, connection_url, backup_schedule, next_backup_at, created_at, updated_at
|
SELECT database_services.id, database_services.user_id, database_services.name, database_services.type, database_services.status, database_services.version, database_services.plan, database_services.region, database_services.connection_url, database_services.backup_schedule, database_services.next_backup_at, database_services.provider, database_services.external_host, database_services.external_port, database_services.external_name, database_services.external_username, database_services.external_password, database_services.external_ssl, database_services.public_port, database_services.backup_target_id, database_services.project_id, database_services.created_at, database_services.updated_at
|
||||||
FROM database_services
|
FROM database_services
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
`
|
`
|
||||||
@@ -248,6 +331,16 @@ func (q *Queries) ListAllDatabaseServices(ctx context.Context) ([]DatabaseServic
|
|||||||
&i.ConnectionUrl,
|
&i.ConnectionUrl,
|
||||||
&i.BackupSchedule,
|
&i.BackupSchedule,
|
||||||
&i.NextBackupAt,
|
&i.NextBackupAt,
|
||||||
|
&i.Provider,
|
||||||
|
&i.ExternalHost,
|
||||||
|
&i.ExternalPort,
|
||||||
|
&i.ExternalName,
|
||||||
|
&i.ExternalUsername,
|
||||||
|
&i.ExternalPassword,
|
||||||
|
&i.ExternalSsl,
|
||||||
|
&i.PublicPort,
|
||||||
|
&i.BackupTargetID,
|
||||||
|
&i.ProjectID,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
@@ -265,7 +358,7 @@ func (q *Queries) ListAllDatabaseServices(ctx context.Context) ([]DatabaseServic
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listDatabaseBackupsByDatabaseAndUser = `-- name: ListDatabaseBackupsByDatabaseAndUser :many
|
const listDatabaseBackupsByDatabaseAndUser = `-- name: ListDatabaseBackupsByDatabaseAndUser :many
|
||||||
SELECT b.id, b.database_id, b.size, b.status, b.backup_path, b.created_at, b.completed_at
|
SELECT b.id, b.database_id, b.size, b.status, b.backup_path, b.remote_key, b.created_at, b.completed_at
|
||||||
FROM database_backups b
|
FROM database_backups b
|
||||||
JOIN database_services s ON s.id = b.database_id
|
JOIN database_services s ON s.id = b.database_id
|
||||||
WHERE b.database_id = $1 AND s.user_id = $2
|
WHERE b.database_id = $1 AND s.user_id = $2
|
||||||
@@ -294,6 +387,7 @@ func (q *Queries) ListDatabaseBackupsByDatabaseAndUser(ctx context.Context, arg
|
|||||||
&i.Size,
|
&i.Size,
|
||||||
&i.Status,
|
&i.Status,
|
||||||
&i.BackupPath,
|
&i.BackupPath,
|
||||||
|
&i.RemoteKey,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.CompletedAt,
|
&i.CompletedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
@@ -310,8 +404,62 @@ func (q *Queries) ListDatabaseBackupsByDatabaseAndUser(ctx context.Context, arg
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const listDatabaseServicesByProject = `-- name: ListDatabaseServicesByProject :many
|
||||||
|
SELECT database_services.id, database_services.user_id, database_services.name, database_services.type, database_services.status, database_services.version, database_services.plan, database_services.region, database_services.connection_url, database_services.backup_schedule, database_services.next_backup_at, database_services.provider, database_services.external_host, database_services.external_port, database_services.external_name, database_services.external_username, database_services.external_password, database_services.external_ssl, database_services.public_port, database_services.backup_target_id, database_services.project_id, database_services.created_at, database_services.updated_at
|
||||||
|
FROM database_services
|
||||||
|
WHERE project_id = $1
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListDatabaseServicesByProject(ctx context.Context, projectID uuid.NullUUID) ([]DatabaseService, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listDatabaseServicesByProject, projectID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []DatabaseService{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i DatabaseService
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Type,
|
||||||
|
&i.Status,
|
||||||
|
&i.Version,
|
||||||
|
&i.Plan,
|
||||||
|
&i.Region,
|
||||||
|
&i.ConnectionUrl,
|
||||||
|
&i.BackupSchedule,
|
||||||
|
&i.NextBackupAt,
|
||||||
|
&i.Provider,
|
||||||
|
&i.ExternalHost,
|
||||||
|
&i.ExternalPort,
|
||||||
|
&i.ExternalName,
|
||||||
|
&i.ExternalUsername,
|
||||||
|
&i.ExternalPassword,
|
||||||
|
&i.ExternalSsl,
|
||||||
|
&i.PublicPort,
|
||||||
|
&i.BackupTargetID,
|
||||||
|
&i.ProjectID,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const listDatabaseServicesByUser = `-- name: ListDatabaseServicesByUser :many
|
const listDatabaseServicesByUser = `-- name: ListDatabaseServicesByUser :many
|
||||||
SELECT id, user_id, name, type, status, version, plan, region, connection_url, backup_schedule, next_backup_at, created_at, updated_at
|
SELECT database_services.id, database_services.user_id, database_services.name, database_services.type, database_services.status, database_services.version, database_services.plan, database_services.region, database_services.connection_url, database_services.backup_schedule, database_services.next_backup_at, database_services.provider, database_services.external_host, database_services.external_port, database_services.external_name, database_services.external_username, database_services.external_password, database_services.external_ssl, database_services.public_port, database_services.backup_target_id, database_services.project_id, database_services.created_at, database_services.updated_at
|
||||||
FROM database_services
|
FROM database_services
|
||||||
WHERE user_id = $1
|
WHERE user_id = $1
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
@@ -338,6 +486,16 @@ func (q *Queries) ListDatabaseServicesByUser(ctx context.Context, userID string)
|
|||||||
&i.ConnectionUrl,
|
&i.ConnectionUrl,
|
||||||
&i.BackupSchedule,
|
&i.BackupSchedule,
|
||||||
&i.NextBackupAt,
|
&i.NextBackupAt,
|
||||||
|
&i.Provider,
|
||||||
|
&i.ExternalHost,
|
||||||
|
&i.ExternalPort,
|
||||||
|
&i.ExternalName,
|
||||||
|
&i.ExternalUsername,
|
||||||
|
&i.ExternalPassword,
|
||||||
|
&i.ExternalSsl,
|
||||||
|
&i.PublicPort,
|
||||||
|
&i.BackupTargetID,
|
||||||
|
&i.ProjectID,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
@@ -453,6 +611,29 @@ func (q *Queries) SetDatabaseNextBackupAt(ctx context.Context, arg SetDatabaseNe
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setDatabaseServicePublicPortByIDAndUser = `-- name: SetDatabaseServicePublicPortByIDAndUser :exec
|
||||||
|
UPDATE database_services
|
||||||
|
SET public_port = $1, updated_at = $2
|
||||||
|
WHERE id = $3 AND user_id = $4
|
||||||
|
`
|
||||||
|
|
||||||
|
type SetDatabaseServicePublicPortByIDAndUserParams struct {
|
||||||
|
PublicPort bool `json:"public_port"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) SetDatabaseServicePublicPortByIDAndUser(ctx context.Context, arg SetDatabaseServicePublicPortByIDAndUserParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, setDatabaseServicePublicPortByIDAndUser,
|
||||||
|
arg.PublicPort,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const setDatabaseServiceStatusAndConnectionByID = `-- name: SetDatabaseServiceStatusAndConnectionByID :exec
|
const setDatabaseServiceStatusAndConnectionByID = `-- name: SetDatabaseServiceStatusAndConnectionByID :exec
|
||||||
UPDATE database_services
|
UPDATE database_services
|
||||||
SET status = $1, connection_url = $2, updated_at = $3
|
SET status = $1, connection_url = $2, updated_at = $3
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: invites.sql
|
||||||
|
|
||||||
|
package sqlcdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createUserInvite = `-- name: CreateUserInvite :one
|
||||||
|
INSERT INTO user_invites (token_hash, email, created_by, expires_at)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
RETURNING user_invites.id, user_invites.token_hash, user_invites.email, user_invites.created_by, user_invites.expires_at, user_invites.used_by, user_invites.used_at, user_invites.created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateUserInviteParams struct {
|
||||||
|
TokenHash string `json:"token_hash"`
|
||||||
|
Email sql.NullString `json:"email"`
|
||||||
|
CreatedBy uuid.NullUUID `json:"created_by"`
|
||||||
|
ExpiresAt time.Time `json:"expires_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateUserInvite(ctx context.Context, arg CreateUserInviteParams) (UserInvite, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, createUserInvite,
|
||||||
|
arg.TokenHash,
|
||||||
|
arg.Email,
|
||||||
|
arg.CreatedBy,
|
||||||
|
arg.ExpiresAt,
|
||||||
|
)
|
||||||
|
var i UserInvite
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.TokenHash,
|
||||||
|
&i.Email,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.ExpiresAt,
|
||||||
|
&i.UsedBy,
|
||||||
|
&i.UsedAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteUserInvite = `-- name: DeleteUserInvite :exec
|
||||||
|
DELETE FROM user_invites
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeleteUserInvite(ctx context.Context, id uuid.UUID) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deleteUserInvite, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getUserInviteByTokenHash = `-- name: GetUserInviteByTokenHash :one
|
||||||
|
SELECT user_invites.id, user_invites.token_hash, user_invites.email, user_invites.created_by, user_invites.expires_at, user_invites.used_by, user_invites.used_at, user_invites.created_at
|
||||||
|
FROM user_invites
|
||||||
|
WHERE token_hash = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetUserInviteByTokenHash(ctx context.Context, tokenHash string) (UserInvite, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getUserInviteByTokenHash, tokenHash)
|
||||||
|
var i UserInvite
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.TokenHash,
|
||||||
|
&i.Email,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.ExpiresAt,
|
||||||
|
&i.UsedBy,
|
||||||
|
&i.UsedAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listUserInvites = `-- name: ListUserInvites :many
|
||||||
|
SELECT user_invites.id, user_invites.token_hash, user_invites.email, user_invites.created_by, user_invites.expires_at, user_invites.used_by, user_invites.used_at, user_invites.created_at
|
||||||
|
FROM user_invites
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListUserInvites(ctx context.Context) ([]UserInvite, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listUserInvites)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []UserInvite{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i UserInvite
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.TokenHash,
|
||||||
|
&i.Email,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.ExpiresAt,
|
||||||
|
&i.UsedBy,
|
||||||
|
&i.UsedAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const markUserInviteUsed = `-- name: MarkUserInviteUsed :exec
|
||||||
|
UPDATE user_invites
|
||||||
|
SET used_by = $2, used_at = now()
|
||||||
|
WHERE id = $1 AND used_at IS NULL
|
||||||
|
`
|
||||||
|
|
||||||
|
type MarkUserInviteUsedParams struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UsedBy uuid.NullUUID `json:"used_by"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) MarkUserInviteUsed(ctx context.Context, arg MarkUserInviteUsedParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, markUserInviteUsed, arg.ID, arg.UsedBy)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -49,6 +49,35 @@ type AgentHeartbeat struct {
|
|||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BackupTarget struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Endpoint string `json:"endpoint"`
|
||||||
|
Bucket string `json:"bucket"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
Prefix string `json:"prefix"`
|
||||||
|
AccessKey string `json:"access_key"`
|
||||||
|
SecretKey string `json:"secret_key"`
|
||||||
|
UseTls bool `json:"use_tls"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Banner struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Level string `json:"level"`
|
||||||
|
Active bool `json:"active"`
|
||||||
|
Dismissible bool `json:"dismissible"`
|
||||||
|
CreatedBy uuid.NullUUID `json:"created_by"`
|
||||||
|
StartsAt sql.NullTime `json:"starts_at"`
|
||||||
|
EndsAt sql.NullTime `json:"ends_at"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
type ContainerInstance struct {
|
type ContainerInstance struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -75,24 +104,35 @@ type DatabaseBackup struct {
|
|||||||
Size string `json:"size"`
|
Size string `json:"size"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
BackupPath sql.NullString `json:"backup_path"`
|
BackupPath sql.NullString `json:"backup_path"`
|
||||||
|
RemoteKey sql.NullString `json:"remote_key"`
|
||||||
CreatedAt sql.NullTime `json:"created_at"`
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
CompletedAt sql.NullTime `json:"completed_at"`
|
CompletedAt sql.NullTime `json:"completed_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DatabaseService struct {
|
type DatabaseService struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
UserID string `json:"user_id"`
|
UserID string `json:"user_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
Plan string `json:"plan"`
|
Plan string `json:"plan"`
|
||||||
Region string `json:"region"`
|
Region string `json:"region"`
|
||||||
ConnectionUrl sql.NullString `json:"connection_url"`
|
ConnectionUrl sql.NullString `json:"connection_url"`
|
||||||
BackupSchedule sql.NullString `json:"backup_schedule"`
|
BackupSchedule sql.NullString `json:"backup_schedule"`
|
||||||
NextBackupAt sql.NullTime `json:"next_backup_at"`
|
NextBackupAt sql.NullTime `json:"next_backup_at"`
|
||||||
CreatedAt sql.NullTime `json:"created_at"`
|
Provider string `json:"provider"`
|
||||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
ExternalHost sql.NullString `json:"external_host"`
|
||||||
|
ExternalPort sql.NullInt32 `json:"external_port"`
|
||||||
|
ExternalName sql.NullString `json:"external_name"`
|
||||||
|
ExternalUsername sql.NullString `json:"external_username"`
|
||||||
|
ExternalPassword string `json:"external_password"`
|
||||||
|
ExternalSsl bool `json:"external_ssl"`
|
||||||
|
PublicPort bool `json:"public_port"`
|
||||||
|
BackupTargetID sql.NullString `json:"backup_target_id"`
|
||||||
|
ProjectID uuid.NullUUID `json:"project_id"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Deployment struct {
|
type Deployment struct {
|
||||||
@@ -135,6 +175,7 @@ type NodeAgent struct {
|
|||||||
CreatedAt sql.NullTime `json:"created_at"`
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
Metadata pqtype.NullRawMessage `json:"metadata"`
|
Metadata pqtype.NullRawMessage `json:"metadata"`
|
||||||
|
AutoPrune bool `json:"auto_prune"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
@@ -149,6 +190,19 @@ type Notification struct {
|
|||||||
CreatedAt sql.NullTime `json:"created_at"`
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OutboundWebhook struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
Secret string `json:"secret"`
|
||||||
|
Events json.RawMessage `json:"events"`
|
||||||
|
Headers pqtype.NullRawMessage `json:"headers"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
type Project struct {
|
type Project struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -167,6 +221,17 @@ type ProjectMember struct {
|
|||||||
CreatedAt sql.NullTime `json:"created_at"`
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Registry struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
OwnerID uuid.UUID `json:"owner_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Host string `json:"host"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -204,6 +269,11 @@ type Service struct {
|
|||||||
Volumes json.RawMessage `json:"volumes"`
|
Volumes json.RawMessage `json:"volumes"`
|
||||||
MaintenanceMode bool `json:"maintenance_mode"`
|
MaintenanceMode bool `json:"maintenance_mode"`
|
||||||
BasicAuthUsers string `json:"basic_auth_users"`
|
BasicAuthUsers string `json:"basic_auth_users"`
|
||||||
|
Builder string `json:"builder"`
|
||||||
|
CpuReserve string `json:"cpu_reserve"`
|
||||||
|
MemoryReserve string `json:"memory_reserve"`
|
||||||
|
StaticBuildCmd string `json:"static_build_cmd"`
|
||||||
|
StaticDir string `json:"static_dir"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceDomain struct {
|
type ServiceDomain struct {
|
||||||
@@ -232,6 +302,17 @@ type ServiceTemplate struct {
|
|||||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserInvite struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
TokenHash string `json:"token_hash"`
|
||||||
|
Email sql.NullString `json:"email"`
|
||||||
|
CreatedBy uuid.NullUUID `json:"created_by"`
|
||||||
|
ExpiresAt time.Time `json:"expires_at"`
|
||||||
|
UsedBy uuid.NullUUID `json:"used_by"`
|
||||||
|
UsedAt sql.NullTime `json:"used_at"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
type UserToken struct {
|
type UserToken struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
UserID uuid.UUID `json:"user_id"`
|
UserID uuid.UUID `json:"user_id"`
|
||||||
@@ -244,3 +325,17 @@ type UserToken struct {
|
|||||||
RevokedAt sql.NullTime `json:"revoked_at"`
|
RevokedAt sql.NullTime `json:"revoked_at"`
|
||||||
CreatedAt sql.NullTime `json:"created_at"`
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WebhookDelivery struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
WebhookID uuid.UUID `json:"webhook_id"`
|
||||||
|
Event string `json:"event"`
|
||||||
|
Payload pqtype.NullRawMessage `json:"payload"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
ResponseStatus sql.NullInt32 `json:"response_status"`
|
||||||
|
ResponseBody sql.NullString `json:"response_body"`
|
||||||
|
Attempts int32 `json:"attempts"`
|
||||||
|
DurationMs sql.NullInt32 `json:"duration_ms"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
DeliveredAt sql.NullTime `json:"delivered_at"`
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ package sqlcdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
@@ -13,28 +14,43 @@ import (
|
|||||||
type Querier interface {
|
type Querier interface {
|
||||||
CompleteCommand(ctx context.Context, arg CompleteCommandParams) (AgentCommand, error)
|
CompleteCommand(ctx context.Context, arg CompleteCommandParams) (AgentCommand, error)
|
||||||
CountDatabaseServicesByUserAndName(ctx context.Context, arg CountDatabaseServicesByUserAndNameParams) (int64, error)
|
CountDatabaseServicesByUserAndName(ctx context.Context, arg CountDatabaseServicesByUserAndNameParams) (int64, error)
|
||||||
|
CountDatabasesUsingBackupTarget(ctx context.Context, targetID sql.NullString) (int64, error)
|
||||||
CountProjectsByUser(ctx context.Context, arg CountProjectsByUserParams) (int64, error)
|
CountProjectsByUser(ctx context.Context, arg CountProjectsByUserParams) (int64, error)
|
||||||
CountServicesByProjectAndName(ctx context.Context, arg CountServicesByProjectAndNameParams) (int64, error)
|
CountServicesByProjectAndName(ctx context.Context, arg CountServicesByProjectAndNameParams) (int64, error)
|
||||||
CountUnreadNotificationsByUser(ctx context.Context, userID uuid.UUID) (int64, error)
|
CountUnreadNotificationsByUser(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||||
CreateAgent(ctx context.Context, arg CreateAgentParams) (NodeAgent, error)
|
CreateAgent(ctx context.Context, arg CreateAgentParams) (NodeAgent, error)
|
||||||
CreateAgentAuthToken(ctx context.Context, arg CreateAgentAuthTokenParams) (AgentAuthToken, error)
|
CreateAgentAuthToken(ctx context.Context, arg CreateAgentAuthTokenParams) (AgentAuthToken, error)
|
||||||
|
CreateBackupTarget(ctx context.Context, arg CreateBackupTargetParams) error
|
||||||
|
CreateBanner(ctx context.Context, arg CreateBannerParams) (Banner, error)
|
||||||
CreateCommand(ctx context.Context, arg CreateCommandParams) (AgentCommand, error)
|
CreateCommand(ctx context.Context, arg CreateCommandParams) (AgentCommand, error)
|
||||||
CreateContainer(ctx context.Context, arg CreateContainerParams) (ContainerInstance, error)
|
CreateContainer(ctx context.Context, arg CreateContainerParams) (ContainerInstance, error)
|
||||||
CreateDatabaseBackup(ctx context.Context, arg CreateDatabaseBackupParams) error
|
CreateDatabaseBackup(ctx context.Context, arg CreateDatabaseBackupParams) error
|
||||||
CreateDatabaseService(ctx context.Context, arg CreateDatabaseServiceParams) error
|
CreateDatabaseService(ctx context.Context, arg CreateDatabaseServiceParams) error
|
||||||
|
CreateExternalDatabaseService(ctx context.Context, arg CreateExternalDatabaseServiceParams) error
|
||||||
CreateNotification(ctx context.Context, arg CreateNotificationParams) error
|
CreateNotification(ctx context.Context, arg CreateNotificationParams) error
|
||||||
|
CreateOutboundWebhook(ctx context.Context, arg CreateOutboundWebhookParams) error
|
||||||
CreateProject(ctx context.Context, arg CreateProjectParams) (Project, error)
|
CreateProject(ctx context.Context, arg CreateProjectParams) (Project, error)
|
||||||
CreateServiceFromTemplate(ctx context.Context, arg CreateServiceFromTemplateParams) error
|
CreateServiceFromTemplate(ctx context.Context, arg CreateServiceFromTemplateParams) error
|
||||||
|
CreateUserInvite(ctx context.Context, arg CreateUserInviteParams) (UserInvite, error)
|
||||||
CreateUserTemplate(ctx context.Context, arg CreateUserTemplateParams) error
|
CreateUserTemplate(ctx context.Context, arg CreateUserTemplateParams) error
|
||||||
CreateUserToken(ctx context.Context, arg CreateUserTokenParams) (UserToken, error)
|
CreateUserToken(ctx context.Context, arg CreateUserTokenParams) (UserToken, error)
|
||||||
|
CreateWebhookDelivery(ctx context.Context, arg CreateWebhookDeliveryParams) error
|
||||||
DatabaseServiceExistsByIDAndUser(ctx context.Context, arg DatabaseServiceExistsByIDAndUserParams) (bool, error)
|
DatabaseServiceExistsByIDAndUser(ctx context.Context, arg DatabaseServiceExistsByIDAndUserParams) (bool, error)
|
||||||
DeleteAgent(ctx context.Context, id string) error
|
DeleteAgent(ctx context.Context, id string) error
|
||||||
|
DeleteBackupTargetByIDAndUser(ctx context.Context, arg DeleteBackupTargetByIDAndUserParams) error
|
||||||
|
DeleteBanner(ctx context.Context, id uuid.UUID) error
|
||||||
|
DeleteDatabaseServiceByID(ctx context.Context, id string) error
|
||||||
DeleteDatabaseServiceByIDAndUser(ctx context.Context, arg DeleteDatabaseServiceByIDAndUserParams) error
|
DeleteDatabaseServiceByIDAndUser(ctx context.Context, arg DeleteDatabaseServiceByIDAndUserParams) error
|
||||||
|
DeleteOutboundWebhookByIDAndUser(ctx context.Context, arg DeleteOutboundWebhookByIDAndUserParams) error
|
||||||
DeleteProjectByID(ctx context.Context, projectID uuid.UUID) (int64, error)
|
DeleteProjectByID(ctx context.Context, projectID uuid.UUID) (int64, error)
|
||||||
|
DeleteUserInvite(ctx context.Context, id uuid.UUID) error
|
||||||
DeleteUserTemplate(ctx context.Context, arg DeleteUserTemplateParams) (int64, error)
|
DeleteUserTemplate(ctx context.Context, arg DeleteUserTemplateParams) (int64, error)
|
||||||
GetActiveAgentAuthTokenByHash(ctx context.Context, tokenHash string) (AgentAuthToken, error)
|
GetActiveAgentAuthTokenByHash(ctx context.Context, tokenHash string) (AgentAuthToken, error)
|
||||||
GetAgent(ctx context.Context, id string) (NodeAgent, error)
|
GetAgent(ctx context.Context, id string) (NodeAgent, error)
|
||||||
GetAgentByHostAndIP(ctx context.Context, arg GetAgentByHostAndIPParams) (NodeAgent, error)
|
GetAgentByHostAndIP(ctx context.Context, arg GetAgentByHostAndIPParams) (NodeAgent, error)
|
||||||
|
GetBackupTargetByID(ctx context.Context, id string) (BackupTarget, error)
|
||||||
|
GetBackupTargetByIDAndUser(ctx context.Context, arg GetBackupTargetByIDAndUserParams) (BackupTarget, error)
|
||||||
|
GetBannerByID(ctx context.Context, id uuid.UUID) (Banner, error)
|
||||||
GetCommandForAgent(ctx context.Context, arg GetCommandForAgentParams) (AgentCommand, error)
|
GetCommandForAgent(ctx context.Context, arg GetCommandForAgentParams) (AgentCommand, error)
|
||||||
GetContainer(ctx context.Context, id string) (ContainerInstance, error)
|
GetContainer(ctx context.Context, id string) (ContainerInstance, error)
|
||||||
GetContainerForAgent(ctx context.Context, arg GetContainerForAgentParams) (ContainerInstance, error)
|
GetContainerForAgent(ctx context.Context, arg GetContainerForAgentParams) (ContainerInstance, error)
|
||||||
@@ -42,24 +58,34 @@ type Querier interface {
|
|||||||
GetDatabaseServiceByID(ctx context.Context, id string) (DatabaseService, error)
|
GetDatabaseServiceByID(ctx context.Context, id string) (DatabaseService, error)
|
||||||
GetDatabaseServiceByIDAndUser(ctx context.Context, arg GetDatabaseServiceByIDAndUserParams) (DatabaseService, error)
|
GetDatabaseServiceByIDAndUser(ctx context.Context, arg GetDatabaseServiceByIDAndUserParams) (DatabaseService, error)
|
||||||
GetDatabaseServiceOwnerID(ctx context.Context, id string) (string, error)
|
GetDatabaseServiceOwnerID(ctx context.Context, id string) (string, error)
|
||||||
|
GetLastAgentCommandByType(ctx context.Context, arg GetLastAgentCommandByTypeParams) (AgentCommand, error)
|
||||||
|
GetOutboundWebhookByID(ctx context.Context, id uuid.UUID) (OutboundWebhook, error)
|
||||||
|
GetOutboundWebhookByIDAndUser(ctx context.Context, arg GetOutboundWebhookByIDAndUserParams) (OutboundWebhook, error)
|
||||||
GetProjectByIDForUser(ctx context.Context, arg GetProjectByIDForUserParams) (Project, error)
|
GetProjectByIDForUser(ctx context.Context, arg GetProjectByIDForUserParams) (Project, error)
|
||||||
GetProjectOwnerByID(ctx context.Context, projectID uuid.UUID) (uuid.UUID, error)
|
GetProjectOwnerByID(ctx context.Context, projectID uuid.UUID) (uuid.UUID, error)
|
||||||
GetProjectOwnerID(ctx context.Context, id uuid.UUID) (uuid.UUID, error)
|
GetProjectOwnerID(ctx context.Context, id uuid.UUID) (uuid.UUID, error)
|
||||||
GetProjectRoleForUser(ctx context.Context, arg GetProjectRoleForUserParams) (string, error)
|
GetProjectRoleForUser(ctx context.Context, arg GetProjectRoleForUserParams) (string, error)
|
||||||
GetServiceTemplateByID(ctx context.Context, id string) (ServiceTemplate, error)
|
GetServiceTemplateByID(ctx context.Context, id string) (ServiceTemplate, error)
|
||||||
|
GetUserInviteByTokenHash(ctx context.Context, tokenHash string) (UserInvite, error)
|
||||||
GetUserTokenByHash(ctx context.Context, tokenHash string) (UserToken, error)
|
GetUserTokenByHash(ctx context.Context, tokenHash string) (UserToken, error)
|
||||||
InsertAgentHeartbeat(ctx context.Context, arg InsertAgentHeartbeatParams) error
|
InsertAgentHeartbeat(ctx context.Context, arg InsertAgentHeartbeatParams) error
|
||||||
InsertProjectEnvironment(ctx context.Context, arg InsertProjectEnvironmentParams) error
|
InsertProjectEnvironment(ctx context.Context, arg InsertProjectEnvironmentParams) error
|
||||||
|
ListActiveBanners(ctx context.Context) ([]Banner, error)
|
||||||
ListAgentAuthTokens(ctx context.Context) ([]AgentAuthToken, error)
|
ListAgentAuthTokens(ctx context.Context) ([]AgentAuthToken, error)
|
||||||
ListAgentHeartbeatsSince(ctx context.Context, arg ListAgentHeartbeatsSinceParams) ([]AgentHeartbeat, error)
|
ListAgentHeartbeatsSince(ctx context.Context, arg ListAgentHeartbeatsSinceParams) ([]AgentHeartbeat, error)
|
||||||
ListAgents(ctx context.Context) ([]NodeAgent, error)
|
ListAgents(ctx context.Context) ([]NodeAgent, error)
|
||||||
ListAllDatabaseServices(ctx context.Context) ([]DatabaseService, error)
|
ListAllDatabaseServices(ctx context.Context) ([]DatabaseService, error)
|
||||||
|
ListBackupTargetsByUser(ctx context.Context, userID string) ([]BackupTarget, error)
|
||||||
|
ListBanners(ctx context.Context) ([]Banner, error)
|
||||||
ListCommandsForAgent(ctx context.Context, nodeAgentID string) ([]AgentCommand, error)
|
ListCommandsForAgent(ctx context.Context, nodeAgentID string) ([]AgentCommand, error)
|
||||||
ListContainersForAgent(ctx context.Context, nodeAgentID string) ([]ContainerInstance, error)
|
ListContainersForAgent(ctx context.Context, nodeAgentID string) ([]ContainerInstance, error)
|
||||||
ListDatabaseBackupsByDatabaseAndUser(ctx context.Context, arg ListDatabaseBackupsByDatabaseAndUserParams) ([]DatabaseBackup, error)
|
ListDatabaseBackupsByDatabaseAndUser(ctx context.Context, arg ListDatabaseBackupsByDatabaseAndUserParams) ([]DatabaseBackup, error)
|
||||||
|
ListDatabaseServicesByProject(ctx context.Context, projectID uuid.NullUUID) ([]DatabaseService, error)
|
||||||
ListDatabaseServicesByUser(ctx context.Context, userID string) ([]DatabaseService, error)
|
ListDatabaseServicesByUser(ctx context.Context, userID string) ([]DatabaseService, error)
|
||||||
ListDueDatabaseBackups(ctx context.Context) ([]ListDueDatabaseBackupsRow, error)
|
ListDueDatabaseBackups(ctx context.Context) ([]ListDueDatabaseBackupsRow, error)
|
||||||
|
ListEnabledOutboundWebhooks(ctx context.Context) ([]OutboundWebhook, error)
|
||||||
ListNotificationsByUser(ctx context.Context, arg ListNotificationsByUserParams) ([]Notification, error)
|
ListNotificationsByUser(ctx context.Context, arg ListNotificationsByUserParams) ([]Notification, error)
|
||||||
|
ListOutboundWebhooksByUser(ctx context.Context, userID uuid.UUID) ([]OutboundWebhook, error)
|
||||||
ListPendingCommands(ctx context.Context, nodeAgentID string) ([]AgentCommand, error)
|
ListPendingCommands(ctx context.Context, nodeAgentID string) ([]AgentCommand, error)
|
||||||
ListPendingProjects(ctx context.Context) ([]Project, error)
|
ListPendingProjects(ctx context.Context) ([]Project, error)
|
||||||
// Visibility model: a project is readable when it is approved for public
|
// Visibility model: a project is readable when it is approved for public
|
||||||
@@ -68,14 +94,20 @@ type Querier interface {
|
|||||||
ListProjectsWithStatsByUser(ctx context.Context, arg ListProjectsWithStatsByUserParams) ([]ListProjectsWithStatsByUserRow, error)
|
ListProjectsWithStatsByUser(ctx context.Context, arg ListProjectsWithStatsByUserParams) ([]ListProjectsWithStatsByUserRow, error)
|
||||||
ListServiceTemplatesByCategoryForUser(ctx context.Context, arg ListServiceTemplatesByCategoryForUserParams) ([]ServiceTemplate, error)
|
ListServiceTemplatesByCategoryForUser(ctx context.Context, arg ListServiceTemplatesByCategoryForUserParams) ([]ServiceTemplate, error)
|
||||||
ListServiceTemplatesForUser(ctx context.Context, ownerID uuid.NullUUID) ([]ServiceTemplate, error)
|
ListServiceTemplatesForUser(ctx context.Context, ownerID uuid.NullUUID) ([]ServiceTemplate, error)
|
||||||
|
ListUserInvites(ctx context.Context) ([]UserInvite, error)
|
||||||
ListUserTokens(ctx context.Context, userID uuid.UUID) ([]UserToken, error)
|
ListUserTokens(ctx context.Context, userID uuid.UUID) ([]UserToken, error)
|
||||||
|
ListWebhookDeliveriesByWebhook(ctx context.Context, webhookID uuid.UUID) ([]WebhookDelivery, error)
|
||||||
MarkAllNotificationsReadByUser(ctx context.Context, arg MarkAllNotificationsReadByUserParams) error
|
MarkAllNotificationsReadByUser(ctx context.Context, arg MarkAllNotificationsReadByUserParams) error
|
||||||
MarkNotificationReadByIDAndUser(ctx context.Context, arg MarkNotificationReadByIDAndUserParams) error
|
MarkNotificationReadByIDAndUser(ctx context.Context, arg MarkNotificationReadByIDAndUserParams) error
|
||||||
|
MarkUserInviteUsed(ctx context.Context, arg MarkUserInviteUsedParams) error
|
||||||
RevokeAgentAuthToken(ctx context.Context, id uuid.UUID) (AgentAuthToken, error)
|
RevokeAgentAuthToken(ctx context.Context, id uuid.UUID) (AgentAuthToken, error)
|
||||||
RevokeUserToken(ctx context.Context, arg RevokeUserTokenParams) (int64, error)
|
RevokeUserToken(ctx context.Context, arg RevokeUserTokenParams) (int64, error)
|
||||||
|
SetDatabaseBackupRemoteKeyByID(ctx context.Context, arg SetDatabaseBackupRemoteKeyByIDParams) error
|
||||||
SetDatabaseBackupScheduleByIDAndUser(ctx context.Context, arg SetDatabaseBackupScheduleByIDAndUserParams) error
|
SetDatabaseBackupScheduleByIDAndUser(ctx context.Context, arg SetDatabaseBackupScheduleByIDAndUserParams) error
|
||||||
SetDatabaseBackupStatusByID(ctx context.Context, arg SetDatabaseBackupStatusByIDParams) error
|
SetDatabaseBackupStatusByID(ctx context.Context, arg SetDatabaseBackupStatusByIDParams) error
|
||||||
|
SetDatabaseBackupTargetByIDAndUser(ctx context.Context, arg SetDatabaseBackupTargetByIDAndUserParams) error
|
||||||
SetDatabaseNextBackupAt(ctx context.Context, arg SetDatabaseNextBackupAtParams) error
|
SetDatabaseNextBackupAt(ctx context.Context, arg SetDatabaseNextBackupAtParams) error
|
||||||
|
SetDatabaseServicePublicPortByIDAndUser(ctx context.Context, arg SetDatabaseServicePublicPortByIDAndUserParams) error
|
||||||
SetDatabaseServiceStatusAndConnectionByID(ctx context.Context, arg SetDatabaseServiceStatusAndConnectionByIDParams) error
|
SetDatabaseServiceStatusAndConnectionByID(ctx context.Context, arg SetDatabaseServiceStatusAndConnectionByIDParams) error
|
||||||
SetDatabaseServiceStatusByID(ctx context.Context, arg SetDatabaseServiceStatusByIDParams) error
|
SetDatabaseServiceStatusByID(ctx context.Context, arg SetDatabaseServiceStatusByIDParams) error
|
||||||
SetDatabaseServiceStatusByIDAndUser(ctx context.Context, arg SetDatabaseServiceStatusByIDAndUserParams) error
|
SetDatabaseServiceStatusByIDAndUser(ctx context.Context, arg SetDatabaseServiceStatusByIDAndUserParams) error
|
||||||
@@ -84,12 +116,16 @@ type Querier interface {
|
|||||||
TouchUserToken(ctx context.Context, id uuid.UUID) error
|
TouchUserToken(ctx context.Context, id uuid.UUID) error
|
||||||
UpdateAgent(ctx context.Context, arg UpdateAgentParams) (NodeAgent, error)
|
UpdateAgent(ctx context.Context, arg UpdateAgentParams) (NodeAgent, error)
|
||||||
UpdateAgentHeartbeat(ctx context.Context, arg UpdateAgentHeartbeatParams) error
|
UpdateAgentHeartbeat(ctx context.Context, arg UpdateAgentHeartbeatParams) error
|
||||||
|
UpdateBackupTargetByIDAndUser(ctx context.Context, arg UpdateBackupTargetByIDAndUserParams) error
|
||||||
|
UpdateBanner(ctx context.Context, arg UpdateBannerParams) (Banner, error)
|
||||||
UpdateContainerStatus(ctx context.Context, arg UpdateContainerStatusParams) error
|
UpdateContainerStatus(ctx context.Context, arg UpdateContainerStatusParams) error
|
||||||
UpdateDatabaseServiceNameAndPlanByIDAndUser(ctx context.Context, arg UpdateDatabaseServiceNameAndPlanByIDAndUserParams) error
|
UpdateDatabaseServiceNameAndPlanByIDAndUser(ctx context.Context, arg UpdateDatabaseServiceNameAndPlanByIDAndUserParams) error
|
||||||
UpdateDatabaseServiceNameByIDAndUser(ctx context.Context, arg UpdateDatabaseServiceNameByIDAndUserParams) error
|
UpdateDatabaseServiceNameByIDAndUser(ctx context.Context, arg UpdateDatabaseServiceNameByIDAndUserParams) error
|
||||||
UpdateDatabaseServicePlanByIDAndUser(ctx context.Context, arg UpdateDatabaseServicePlanByIDAndUserParams) error
|
UpdateDatabaseServicePlanByIDAndUser(ctx context.Context, arg UpdateDatabaseServicePlanByIDAndUserParams) error
|
||||||
|
UpdateOutboundWebhook(ctx context.Context, arg UpdateOutboundWebhookParams) error
|
||||||
UpdateProjectByID(ctx context.Context, arg UpdateProjectByIDParams) (int64, error)
|
UpdateProjectByID(ctx context.Context, arg UpdateProjectByIDParams) (int64, error)
|
||||||
UpdateUserTemplate(ctx context.Context, arg UpdateUserTemplateParams) (int64, error)
|
UpdateUserTemplate(ctx context.Context, arg UpdateUserTemplateParams) (int64, error)
|
||||||
|
UpdateWebhookDeliveryResult(ctx context.Context, arg UpdateWebhookDeliveryResultParams) error
|
||||||
UpsertEnvironmentVariable(ctx context.Context, arg UpsertEnvironmentVariableParams) error
|
UpsertEnvironmentVariable(ctx context.Context, arg UpsertEnvironmentVariableParams) error
|
||||||
UpsertServiceTemplate(ctx context.Context, arg UpsertServiceTemplateParams) error
|
UpsertServiceTemplate(ctx context.Context, arg UpsertServiceTemplateParams) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,329 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: webhooks.sql
|
||||||
|
|
||||||
|
package sqlcdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/sqlc-dev/pqtype"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createOutboundWebhook = `-- name: CreateOutboundWebhook :exec
|
||||||
|
INSERT INTO outbound_webhooks (id, user_id, name, url, secret, events, headers, enabled, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateOutboundWebhookParams struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
Secret string `json:"secret"`
|
||||||
|
Events json.RawMessage `json:"events"`
|
||||||
|
Headers pqtype.NullRawMessage `json:"headers"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateOutboundWebhook(ctx context.Context, arg CreateOutboundWebhookParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, createOutboundWebhook,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Name,
|
||||||
|
arg.Url,
|
||||||
|
arg.Secret,
|
||||||
|
arg.Events,
|
||||||
|
arg.Headers,
|
||||||
|
arg.Enabled,
|
||||||
|
arg.CreatedAt,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const createWebhookDelivery = `-- name: CreateWebhookDelivery :exec
|
||||||
|
INSERT INTO webhook_deliveries (id, webhook_id, event, payload, status, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateWebhookDeliveryParams struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
WebhookID uuid.UUID `json:"webhook_id"`
|
||||||
|
Event string `json:"event"`
|
||||||
|
Payload pqtype.NullRawMessage `json:"payload"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
CreatedAt sql.NullTime `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateWebhookDelivery(ctx context.Context, arg CreateWebhookDeliveryParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, createWebhookDelivery,
|
||||||
|
arg.ID,
|
||||||
|
arg.WebhookID,
|
||||||
|
arg.Event,
|
||||||
|
arg.Payload,
|
||||||
|
arg.Status,
|
||||||
|
arg.CreatedAt,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteOutboundWebhookByIDAndUser = `-- name: DeleteOutboundWebhookByIDAndUser :exec
|
||||||
|
DELETE FROM outbound_webhooks
|
||||||
|
WHERE id = $1 AND user_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type DeleteOutboundWebhookByIDAndUserParams struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) DeleteOutboundWebhookByIDAndUser(ctx context.Context, arg DeleteOutboundWebhookByIDAndUserParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deleteOutboundWebhookByIDAndUser, arg.ID, arg.UserID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getOutboundWebhookByID = `-- name: GetOutboundWebhookByID :one
|
||||||
|
SELECT outbound_webhooks.id, outbound_webhooks.user_id, outbound_webhooks.name, outbound_webhooks.url, outbound_webhooks.secret, outbound_webhooks.events, outbound_webhooks.headers, outbound_webhooks.enabled, outbound_webhooks.created_at, outbound_webhooks.updated_at
|
||||||
|
FROM outbound_webhooks
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetOutboundWebhookByID(ctx context.Context, id uuid.UUID) (OutboundWebhook, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getOutboundWebhookByID, id)
|
||||||
|
var i OutboundWebhook
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Url,
|
||||||
|
&i.Secret,
|
||||||
|
&i.Events,
|
||||||
|
&i.Headers,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getOutboundWebhookByIDAndUser = `-- name: GetOutboundWebhookByIDAndUser :one
|
||||||
|
SELECT outbound_webhooks.id, outbound_webhooks.user_id, outbound_webhooks.name, outbound_webhooks.url, outbound_webhooks.secret, outbound_webhooks.events, outbound_webhooks.headers, outbound_webhooks.enabled, outbound_webhooks.created_at, outbound_webhooks.updated_at
|
||||||
|
FROM outbound_webhooks
|
||||||
|
WHERE id = $1 AND user_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetOutboundWebhookByIDAndUserParams struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetOutboundWebhookByIDAndUser(ctx context.Context, arg GetOutboundWebhookByIDAndUserParams) (OutboundWebhook, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getOutboundWebhookByIDAndUser, arg.ID, arg.UserID)
|
||||||
|
var i OutboundWebhook
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Url,
|
||||||
|
&i.Secret,
|
||||||
|
&i.Events,
|
||||||
|
&i.Headers,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listEnabledOutboundWebhooks = `-- name: ListEnabledOutboundWebhooks :many
|
||||||
|
SELECT outbound_webhooks.id, outbound_webhooks.user_id, outbound_webhooks.name, outbound_webhooks.url, outbound_webhooks.secret, outbound_webhooks.events, outbound_webhooks.headers, outbound_webhooks.enabled, outbound_webhooks.created_at, outbound_webhooks.updated_at
|
||||||
|
FROM outbound_webhooks
|
||||||
|
WHERE enabled
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListEnabledOutboundWebhooks(ctx context.Context) ([]OutboundWebhook, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listEnabledOutboundWebhooks)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []OutboundWebhook{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i OutboundWebhook
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Url,
|
||||||
|
&i.Secret,
|
||||||
|
&i.Events,
|
||||||
|
&i.Headers,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const listOutboundWebhooksByUser = `-- name: ListOutboundWebhooksByUser :many
|
||||||
|
SELECT outbound_webhooks.id, outbound_webhooks.user_id, outbound_webhooks.name, outbound_webhooks.url, outbound_webhooks.secret, outbound_webhooks.events, outbound_webhooks.headers, outbound_webhooks.enabled, outbound_webhooks.created_at, outbound_webhooks.updated_at
|
||||||
|
FROM outbound_webhooks
|
||||||
|
WHERE user_id = $1
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListOutboundWebhooksByUser(ctx context.Context, userID uuid.UUID) ([]OutboundWebhook, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listOutboundWebhooksByUser, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []OutboundWebhook{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i OutboundWebhook
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Url,
|
||||||
|
&i.Secret,
|
||||||
|
&i.Events,
|
||||||
|
&i.Headers,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const listWebhookDeliveriesByWebhook = `-- name: ListWebhookDeliveriesByWebhook :many
|
||||||
|
SELECT webhook_deliveries.id, webhook_deliveries.webhook_id, webhook_deliveries.event, webhook_deliveries.payload, webhook_deliveries.status, webhook_deliveries.response_status, webhook_deliveries.response_body, webhook_deliveries.attempts, webhook_deliveries.duration_ms, webhook_deliveries.created_at, webhook_deliveries.delivered_at
|
||||||
|
FROM webhook_deliveries
|
||||||
|
WHERE webhook_id = $1
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 100
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListWebhookDeliveriesByWebhook(ctx context.Context, webhookID uuid.UUID) ([]WebhookDelivery, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, listWebhookDeliveriesByWebhook, webhookID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []WebhookDelivery{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i WebhookDelivery
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.WebhookID,
|
||||||
|
&i.Event,
|
||||||
|
&i.Payload,
|
||||||
|
&i.Status,
|
||||||
|
&i.ResponseStatus,
|
||||||
|
&i.ResponseBody,
|
||||||
|
&i.Attempts,
|
||||||
|
&i.DurationMs,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.DeliveredAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateOutboundWebhook = `-- name: UpdateOutboundWebhook :exec
|
||||||
|
UPDATE outbound_webhooks
|
||||||
|
SET name = $1, url = $2, secret = $3, events = $4, headers = $5, enabled = $6, updated_at = $7
|
||||||
|
WHERE id = $8 AND user_id = $9
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateOutboundWebhookParams struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
Secret string `json:"secret"`
|
||||||
|
Events json.RawMessage `json:"events"`
|
||||||
|
Headers pqtype.NullRawMessage `json:"headers"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateOutboundWebhook(ctx context.Context, arg UpdateOutboundWebhookParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, updateOutboundWebhook,
|
||||||
|
arg.Name,
|
||||||
|
arg.Url,
|
||||||
|
arg.Secret,
|
||||||
|
arg.Events,
|
||||||
|
arg.Headers,
|
||||||
|
arg.Enabled,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateWebhookDeliveryResult = `-- name: UpdateWebhookDeliveryResult :exec
|
||||||
|
UPDATE webhook_deliveries
|
||||||
|
SET status = $1, response_status = $2, response_body = $3, attempts = $4, duration_ms = $5, delivered_at = $6
|
||||||
|
WHERE id = $7
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateWebhookDeliveryResultParams struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
ResponseStatus sql.NullInt32 `json:"response_status"`
|
||||||
|
ResponseBody sql.NullString `json:"response_body"`
|
||||||
|
Attempts int32 `json:"attempts"`
|
||||||
|
DurationMs sql.NullInt32 `json:"duration_ms"`
|
||||||
|
DeliveredAt sql.NullTime `json:"delivered_at"`
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateWebhookDeliveryResult(ctx context.Context, arg UpdateWebhookDeliveryResultParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, updateWebhookDeliveryResult,
|
||||||
|
arg.Status,
|
||||||
|
arg.ResponseStatus,
|
||||||
|
arg.ResponseBody,
|
||||||
|
arg.Attempts,
|
||||||
|
arg.DurationMs,
|
||||||
|
arg.DeliveredAt,
|
||||||
|
arg.ID,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -84,10 +84,11 @@ type VolumeMount struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ResourceLimits struct {
|
type ResourceLimits struct {
|
||||||
MemoryBytes int64 `json:"memory_bytes,omitempty"`
|
MemoryBytes int64 `json:"memory_bytes,omitempty"`
|
||||||
CPUQuota int64 `json:"cpu_quota,omitempty"`
|
MemoryReservation int64 `json:"memory_reservation,omitempty"`
|
||||||
CPUPeriod int64 `json:"cpu_period,omitempty"`
|
CPUQuota int64 `json:"cpu_quota,omitempty"`
|
||||||
CPUShares int64 `json:"cpu_shares,omitempty"`
|
CPUPeriod int64 `json:"cpu_period,omitempty"`
|
||||||
|
CPUShares int64 `json:"cpu_shares,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HealthCheck struct {
|
type HealthCheck struct {
|
||||||
@@ -152,10 +153,14 @@ type BuildConfig struct {
|
|||||||
PrebuiltImage string `json:"prebuilt_image"`
|
PrebuiltImage string `json:"prebuilt_image"`
|
||||||
BuildCommand string `json:"build_command"`
|
BuildCommand string `json:"build_command"`
|
||||||
StartCommand string `json:"start_command"`
|
StartCommand string `json:"start_command"`
|
||||||
|
BuildArgs map[string]string `json:"build_args,omitempty"`
|
||||||
Environment map[string]string `json:"environment"`
|
Environment map[string]string `json:"environment"`
|
||||||
Branch string `json:"branch"`
|
// Pull credentials for private prebuilt images.
|
||||||
Commit string `json:"commit"`
|
PullUsername string `json:"pull_username,omitempty"`
|
||||||
NoCache bool `json:"no_cache"`
|
PullPassword string `json:"pull_password,omitempty"`
|
||||||
|
Branch string `json:"branch"`
|
||||||
|
Commit string `json:"commit"`
|
||||||
|
NoCache bool `json:"no_cache"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TriggerConfig struct {
|
type TriggerConfig struct {
|
||||||
@@ -166,6 +171,12 @@ type TriggerConfig struct {
|
|||||||
Timestamp time.Time `json:"timestamp"` // When trigger occurred
|
Timestamp time.Time `json:"timestamp"` // When trigger occurred
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DockerClient exposes the engine's docker handle for read-only queries
|
||||||
|
// (capacity checks, image info).
|
||||||
|
func (de *DeploymentEngine) DockerClient() *docker.Client {
|
||||||
|
return de.dockerClient
|
||||||
|
}
|
||||||
|
|
||||||
func NewDeploymentEngine(buildManager *build.BuildManager, dockerClient *docker.Client) *DeploymentEngine {
|
func NewDeploymentEngine(buildManager *build.BuildManager, dockerClient *docker.Client) *DeploymentEngine {
|
||||||
return &DeploymentEngine{
|
return &DeploymentEngine{
|
||||||
buildManager: buildManager,
|
buildManager: buildManager,
|
||||||
@@ -290,7 +301,10 @@ func (de *DeploymentEngine) buildImage(ctx context.Context, deployment *Deployme
|
|||||||
ImageTag: deployment.ID,
|
ImageTag: deployment.ID,
|
||||||
BuildCommand: buildConfig.BuildCommand,
|
BuildCommand: buildConfig.BuildCommand,
|
||||||
StartCommand: buildConfig.StartCommand,
|
StartCommand: buildConfig.StartCommand,
|
||||||
|
BuildArgs: buildConfig.BuildArgs,
|
||||||
Environment: buildConfig.Environment,
|
Environment: buildConfig.Environment,
|
||||||
|
PullUsername: buildConfig.PullUsername,
|
||||||
|
PullPassword: buildConfig.PullPassword,
|
||||||
ProjectID: deployment.ProjectID,
|
ProjectID: deployment.ProjectID,
|
||||||
ServiceID: deployment.ServiceID,
|
ServiceID: deployment.ServiceID,
|
||||||
DeploymentID: deployment.ID,
|
DeploymentID: deployment.ID,
|
||||||
@@ -330,7 +344,9 @@ func (de *DeploymentEngine) deployService(ctx context.Context, deployment *Deplo
|
|||||||
HealthPath: cfg.HealthPath,
|
HealthPath: cfg.HealthPath,
|
||||||
RestartPolicy: cfg.RestartPolicy,
|
RestartPolicy: cfg.RestartPolicy,
|
||||||
MemoryBytes: cfg.Resources.MemoryBytes,
|
MemoryBytes: cfg.Resources.MemoryBytes,
|
||||||
|
MemoryReserve: cfg.Resources.MemoryReservation,
|
||||||
NanoCPUs: cfg.Resources.CPUQuota,
|
NanoCPUs: cfg.Resources.CPUQuota,
|
||||||
|
CPUShares: cfg.Resources.CPUShares,
|
||||||
Volumes: cfg.VolumeMounts,
|
Volumes: cfg.VolumeMounts,
|
||||||
Domains: cfg.Domains,
|
Domains: cfg.Domains,
|
||||||
Maintenance: cfg.Maintenance,
|
Maintenance: cfg.Maintenance,
|
||||||
|
|||||||
@@ -47,7 +47,9 @@ type RuntimeSpec struct {
|
|||||||
HealthPath string // http path probed on Port for container healthcheck
|
HealthPath string // http path probed on Port for container healthcheck
|
||||||
RestartPolicy string
|
RestartPolicy string
|
||||||
MemoryBytes int64
|
MemoryBytes int64
|
||||||
|
MemoryReserve int64 // soft reservation; scheduler hint, not a hard cap
|
||||||
NanoCPUs int64
|
NanoCPUs int64
|
||||||
|
CPUShares int64 // relative weight (1024 ≈ 1 cpu), not a cap
|
||||||
Volumes []VolumeMount // applied to every replica
|
Volumes []VolumeMount // applied to every replica
|
||||||
Domains []string // all public hostnames; Domain is the default
|
Domains []string // all public hostnames; Domain is the default
|
||||||
Maintenance bool // redirect traffic to MaintenanceURL
|
Maintenance bool // redirect traffic to MaintenanceURL
|
||||||
@@ -273,15 +275,17 @@ func (de *DeploymentEngine) createReplica(ctx context.Context, spec RuntimeSpec,
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg := docker.ContainerConfig{
|
cfg := docker.ContainerConfig{
|
||||||
Name: fmt.Sprintf("%s%d", serviceNamePrefix(spec.ServiceID), index),
|
Name: fmt.Sprintf("%s%d", serviceNamePrefix(spec.ServiceID), index),
|
||||||
Image: spec.Image,
|
Image: spec.Image,
|
||||||
Cmd: spec.Command,
|
Cmd: spec.Command,
|
||||||
Env: env,
|
Env: env,
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
RestartPolicy: spec.RestartPolicy,
|
RestartPolicy: spec.RestartPolicy,
|
||||||
Memory: spec.MemoryBytes,
|
Memory: spec.MemoryBytes,
|
||||||
NanoCPUs: spec.NanoCPUs,
|
MemoryReservation: spec.MemoryReserve,
|
||||||
Networks: endpoints,
|
NanoCPUs: spec.NanoCPUs,
|
||||||
|
CPUShares: spec.CPUShares,
|
||||||
|
Networks: endpoints,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range spec.Volumes {
|
for _, v := range spec.Volumes {
|
||||||
@@ -526,12 +530,14 @@ func specHash(spec RuntimeSpec) string {
|
|||||||
Env map[string]string
|
Env map[string]string
|
||||||
Port int32
|
Port int32
|
||||||
MemoryBytes, NanoCPUs int64
|
MemoryBytes, NanoCPUs int64
|
||||||
|
MemoryReserve, CPUShares int64
|
||||||
Volumes []VolumeMount
|
Volumes []VolumeMount
|
||||||
Domains []string
|
Domains []string
|
||||||
Maintenance bool
|
Maintenance bool
|
||||||
BasicAuth string
|
BasicAuth string
|
||||||
}{spec.Image, spec.Name, spec.Domain, spec.HealthPath, spec.RestartPolicy,
|
}{spec.Image, spec.Name, spec.Domain, spec.HealthPath, spec.RestartPolicy,
|
||||||
spec.Command, spec.Env, spec.Port, spec.MemoryBytes, spec.NanoCPUs, spec.Volumes,
|
spec.Command, spec.Env, spec.Port, spec.MemoryBytes, spec.NanoCPUs,
|
||||||
|
spec.MemoryReserve, spec.CPUShares, spec.Volumes,
|
||||||
spec.Domains, spec.Maintenance, spec.BasicAuthUsers})
|
spec.Domains, spec.Maintenance, spec.BasicAuthUsers})
|
||||||
sum := sha256.Sum256(payload)
|
sum := sha256.Sum256(payload)
|
||||||
return hex.EncodeToString(sum[:8])
|
return hex.EncodeToString(sum[:8])
|
||||||
|
|||||||
@@ -129,6 +129,27 @@ func (q *Queue) Cancel(serviceID, deploymentID uuid.UUID) CancelState {
|
|||||||
return NotFound
|
return NotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServiceQueueState reports queue depth for one service.
|
||||||
|
type ServiceQueueState struct {
|
||||||
|
ServiceID uuid.UUID `json:"service_id"`
|
||||||
|
Running bool `json:"running"`
|
||||||
|
Queued int `json:"queued"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Snapshot returns per-service queue depth for observability.
|
||||||
|
func (q *Queue) Snapshot() []ServiceQueueState {
|
||||||
|
q.mu.Lock()
|
||||||
|
defer q.mu.Unlock()
|
||||||
|
out := make([]ServiceQueueState, 0, len(q.states))
|
||||||
|
for id, st := range q.states {
|
||||||
|
if !st.running && len(st.pending) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, ServiceQueueState{ServiceID: id, Running: st.running, Queued: len(st.pending)})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// Do runs fn through the queue and waits for completion — for synchronous
|
// Do runs fn through the queue and waits for completion — for synchronous
|
||||||
// endpoints (redeploy) that still must not overlap an in-flight deployment.
|
// endpoints (redeploy) that still must not overlap an in-flight deployment.
|
||||||
// ctx bounds the wait, not the job; on timeout the job still runs to
|
// ctx bounds the wait, not the job; on timeout the job still runs to
|
||||||
|
|||||||
@@ -125,6 +125,40 @@ func TestCancelActiveCancelsContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSnapshot(t *testing.T) {
|
||||||
|
q := New()
|
||||||
|
if got := q.Snapshot(); len(got) != 0 {
|
||||||
|
t.Fatalf("empty queue snapshot = %v, want empty", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
svc := uuid.New()
|
||||||
|
release := make(chan struct{})
|
||||||
|
q.Enqueue(svc, Job{Run: func(ctx context.Context) { <-release }})
|
||||||
|
q.Enqueue(svc, Job{Run: func(ctx context.Context) {}})
|
||||||
|
q.Enqueue(svc, Job{Run: func(ctx context.Context) {}})
|
||||||
|
|
||||||
|
deadline := time.After(2 * time.Second)
|
||||||
|
for {
|
||||||
|
snap := q.Snapshot()
|
||||||
|
if len(snap) == 1 && snap[0].Running {
|
||||||
|
if snap[0].ServiceID != svc {
|
||||||
|
t.Fatalf("snapshot service = %v, want %v", snap[0].ServiceID, svc)
|
||||||
|
}
|
||||||
|
if snap[0].Queued != 2 {
|
||||||
|
t.Fatalf("queued = %d, want 2", snap[0].Queued)
|
||||||
|
}
|
||||||
|
close(release)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-deadline:
|
||||||
|
t.Fatalf("snapshot never showed running job: %v", snap)
|
||||||
|
default:
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnqueuePosition(t *testing.T) {
|
func TestEnqueuePosition(t *testing.T) {
|
||||||
q := New()
|
q := New()
|
||||||
svc := uuid.New()
|
svc := uuid.New()
|
||||||
|
|||||||
@@ -156,8 +156,10 @@ func (c *Client) CreateContainer(ctx context.Context, config ContainerConfig) (s
|
|||||||
PortBindings: config.PortBindings,
|
PortBindings: config.PortBindings,
|
||||||
Mounts: config.Mounts,
|
Mounts: config.Mounts,
|
||||||
Resources: container.Resources{
|
Resources: container.Resources{
|
||||||
Memory: config.Memory,
|
Memory: config.Memory,
|
||||||
NanoCPUs: config.NanoCPUs,
|
MemoryReservation: config.MemoryReservation,
|
||||||
|
NanoCPUs: config.NanoCPUs,
|
||||||
|
CPUShares: config.CPUShares,
|
||||||
},
|
},
|
||||||
NetworkMode: container.NetworkMode(config.NetworkMode),
|
NetworkMode: container.NetworkMode(config.NetworkMode),
|
||||||
}
|
}
|
||||||
@@ -236,6 +238,11 @@ func (c *Client) CopyFromContainer(ctx context.Context, containerID, srcPath str
|
|||||||
return reader, err
|
return reader, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CopyToContainer copies a tar archive stream into a container path.
|
||||||
|
func (c *Client) CopyToContainer(ctx context.Context, containerID, dstPath string, content io.Reader) error {
|
||||||
|
return c.cli.CopyToContainer(ctx, containerID, dstPath, content, container.CopyToContainerOptions{})
|
||||||
|
}
|
||||||
|
|
||||||
// GetContainerStats returns real-time resource usage statistics for a container
|
// GetContainerStats returns real-time resource usage statistics for a container
|
||||||
func (c *Client) GetContainerStats(ctx context.Context, containerID string, stream bool) (*container.StatsResponseReader, error) {
|
func (c *Client) GetContainerStats(ctx context.Context, containerID string, stream bool) (*container.StatsResponseReader, error) {
|
||||||
resp, err := c.cli.ContainerStats(ctx, containerID, stream)
|
resp, err := c.cli.ContainerStats(ctx, containerID, stream)
|
||||||
@@ -291,7 +298,7 @@ func (c *Client) PullImageWait(ctx context.Context, ref string, auth registry.Au
|
|||||||
|
|
||||||
// BuildImage builds an image from a Dockerfile
|
// BuildImage builds an image from a Dockerfile
|
||||||
func (c *Client) BuildImage(ctx context.Context, buildContext io.Reader, options BuildOptions) (types.ImageBuildResponse, error) {
|
func (c *Client) BuildImage(ctx context.Context, buildContext io.Reader, options BuildOptions) (types.ImageBuildResponse, error) {
|
||||||
return c.cli.ImageBuild(ctx, buildContext, types.ImageBuildOptions{
|
resp, err := c.cli.ImageBuild(ctx, buildContext, types.ImageBuildOptions{
|
||||||
Dockerfile: options.Dockerfile,
|
Dockerfile: options.Dockerfile,
|
||||||
Tags: options.Tags,
|
Tags: options.Tags,
|
||||||
BuildArgs: options.BuildArgs,
|
BuildArgs: options.BuildArgs,
|
||||||
@@ -299,6 +306,34 @@ func (c *Client) BuildImage(ctx context.Context, buildContext io.Reader, options
|
|||||||
Remove: options.Remove,
|
Remove: options.Remove,
|
||||||
NoCache: options.NoCache,
|
NoCache: options.NoCache,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
// The daemon aborts the build if the response stream isn't consumed,
|
||||||
|
// and reports failures only inside that stream.
|
||||||
|
defer resp.Body.Close()
|
||||||
|
var buildErr string
|
||||||
|
dec := json.NewDecoder(resp.Body)
|
||||||
|
for {
|
||||||
|
var line struct {
|
||||||
|
ErrorDetail struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
} `json:"errorDetail"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
}
|
||||||
|
if err := dec.Decode(&line); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if line.ErrorDetail.Message != "" {
|
||||||
|
buildErr = line.ErrorDetail.Message
|
||||||
|
} else if line.Error != "" {
|
||||||
|
buildErr = line.Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if buildErr != "" {
|
||||||
|
return resp, fmt.Errorf("docker build failed: %s", buildErr)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveImage removes an image
|
// RemoveImage removes an image
|
||||||
@@ -336,6 +371,21 @@ func (c *Client) RemoveNetwork(ctx context.Context, networkID string) error {
|
|||||||
return c.cli.NetworkRemove(ctx, networkID)
|
return c.cli.NetworkRemove(ctx, networkID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NetworkGateway returns the IPAM gateway of a network — the address
|
||||||
|
// containers on it can use to reach host-published ports.
|
||||||
|
func (c *Client) NetworkGateway(ctx context.Context, networkID string) string {
|
||||||
|
resp, err := c.cli.NetworkInspect(ctx, networkID, network.InspectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for _, cfg := range resp.IPAM.Config {
|
||||||
|
if cfg.Gateway != "" {
|
||||||
|
return cfg.Gateway
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// ConnectNetwork connects a container to a network
|
// ConnectNetwork connects a container to a network
|
||||||
func (c *Client) ConnectNetwork(ctx context.Context, networkID, containerID string, config network.EndpointSettings) error {
|
func (c *Client) ConnectNetwork(ctx context.Context, networkID, containerID string, config network.EndpointSettings) error {
|
||||||
return c.cli.NetworkConnect(ctx, networkID, containerID, &config)
|
return c.cli.NetworkConnect(ctx, networkID, containerID, &config)
|
||||||
@@ -466,6 +516,21 @@ func (c *Client) ExecRun(ctx context.Context, containerID string, cmd []string)
|
|||||||
return buf.String(), inspect.ExitCode, nil
|
return buf.String(), inspect.ExitCode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecAttach attaches to a running exec instance, returning the hijacked
|
||||||
|
// bidirectional stream. With Tty enabled the stream carries raw pty data
|
||||||
|
// (no stdcopy multiplexing).
|
||||||
|
func (c *Client) ExecAttach(ctx context.Context, execID string) (types.HijackedResponse, error) {
|
||||||
|
return c.cli.ContainerExecAttach(ctx, execID, container.ExecAttachOptions{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecResize resizes the pty of a running exec instance.
|
||||||
|
func (c *Client) ExecResize(ctx context.Context, execID string, cols, rows uint) error {
|
||||||
|
return c.cli.ContainerExecResize(ctx, execID, container.ResizeOptions{
|
||||||
|
Height: rows,
|
||||||
|
Width: cols,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// GetImageInfo returns information about a Docker image
|
// GetImageInfo returns information about a Docker image
|
||||||
func (c *Client) GetImageInfo(ctx context.Context, imageName string) (*ImageInfo, error) {
|
func (c *Client) GetImageInfo(ctx context.Context, imageName string) (*ImageInfo, error) {
|
||||||
images, err := c.cli.ImageList(ctx, image.ListOptions{})
|
images, err := c.cli.ImageList(ctx, image.ListOptions{})
|
||||||
|
|||||||
@@ -12,20 +12,22 @@ import (
|
|||||||
|
|
||||||
// ContainerConfig represents the configuration for creating a container
|
// ContainerConfig represents the configuration for creating a container
|
||||||
type ContainerConfig struct {
|
type ContainerConfig struct {
|
||||||
Name string
|
Name string
|
||||||
Image string
|
Image string
|
||||||
Cmd []string
|
Cmd []string
|
||||||
Env []string
|
Env []string
|
||||||
Labels map[string]string
|
Labels map[string]string
|
||||||
RestartPolicy string
|
RestartPolicy string
|
||||||
ExposedPorts nat.PortSet
|
ExposedPorts nat.PortSet
|
||||||
PortBindings nat.PortMap
|
PortBindings nat.PortMap
|
||||||
Mounts []mount.Mount
|
Mounts []mount.Mount
|
||||||
Memory int64
|
Memory int64
|
||||||
NanoCPUs int64
|
MemoryReservation int64
|
||||||
NetworkMode string
|
NanoCPUs int64
|
||||||
Networks map[string]*network.EndpointSettings
|
CPUShares int64
|
||||||
Healthcheck *container.HealthConfig
|
NetworkMode string
|
||||||
|
Networks map[string]*network.EndpointSettings
|
||||||
|
Healthcheck *container.HealthConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogOptions represents options for retrieving container logs
|
// LogOptions represents options for retrieving container logs
|
||||||
|
|||||||
@@ -53,15 +53,20 @@ func init() {
|
|||||||
// Services
|
// Services
|
||||||
register(spec{"containr_services_list", "List services in a project", "GET", "/projects/{project_id}/services", []string{"project_id"}, nil, false})
|
register(spec{"containr_services_list", "List services in a project", "GET", "/projects/{project_id}/services", []string{"project_id"}, nil, false})
|
||||||
register(spec{"containr_services_get", "Get a service by id", "GET", "/services/{id}", []string{"id"}, nil, false})
|
register(spec{"containr_services_get", "Get a service by id", "GET", "/services/{id}", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_services_create", "Create a service in a project. Args: name (required), type, image, git_repo, git_branch, environment, port, domain, replicas, volumes = [{type: volume|bind, source, target, read_only}].", "POST", "/projects/{project_id}/services", []string{"project_id"}, nil, false})
|
register(spec{"containr_services_create", "Create a service in a project. Args: name (required), type, image, git_repo, git_branch, environment, port, domain, replicas, volumes = [{type: volume|bind, source, target, read_only}], builder = auto|railpack|nixpacks|dockerfile|static, cpu, memory, cpu_reserve, memory_reserve, static_build_cmd, static_dir.", "POST", "/projects/{project_id}/services", []string{"project_id"}, nil, false})
|
||||||
register(spec{"containr_services_delete", "Delete a service. Requires confirm:true.", "DELETE", "/services/{id}", []string{"id"}, nil, true})
|
register(spec{"containr_services_delete", "Delete a service. Requires confirm:true.", "DELETE", "/services/{id}", []string{"id"}, nil, true})
|
||||||
register(spec{"containr_services_start", "Start a service", "POST", "/services/{id}/start", []string{"id"}, nil, false})
|
register(spec{"containr_services_start", "Start a service", "POST", "/services/{id}/start", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_services_stop", "Stop a service", "POST", "/services/{id}/stop", []string{"id"}, nil, false})
|
register(spec{"containr_services_stop", "Stop a service", "POST", "/services/{id}/stop", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_services_restart", "Restart a service", "POST", "/services/{id}/restart", []string{"id"}, nil, false})
|
register(spec{"containr_services_restart", "Restart a service", "POST", "/services/{id}/restart", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_services_sleep", "Put a service to sleep immediately (scale to zero)", "POST", "/services/{id}/sleep", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_services_wake", "Wake a sleeping service", "POST", "/services/{id}/wake", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_services_env_check", "Diagnose service env: returns ok plus unresolved ${{refs}}, empty vars, and undecryptable secrets", "GET", "/services/{id}/env-check", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_services_redeploy", "Rebuild and redeploy a service", "POST", "/services/{id}/redeploy", []string{"id"}, nil, false})
|
register(spec{"containr_services_redeploy", "Rebuild and redeploy a service", "POST", "/services/{id}/redeploy", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_services_clone", "Clone a service (config, volumes, domains, variables) into the same or another project. Args: id, name, project_id, environment.", "POST", "/services/{id}/clone", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_services_move", "Move a service to another project the caller owns. Args: id, project_id (required).", "POST", "/services/{id}/move", []string{"id"}, []string{"project_id"}, false})
|
||||||
register(spec{"containr_services_logs", "Get service runtime logs. Args: id, tail (default 100).", "GET", "/services/{id}/logs", []string{"id"}, nil, false})
|
register(spec{"containr_services_logs", "Get service runtime logs. Args: id, tail (default 100).", "GET", "/services/{id}/logs", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_services_exec", "Run a one-off command in the service container (30s ceiling). Args: id, command (required).", "POST", "/services/{id}/exec", []string{"id"}, []string{"command"}, false})
|
register(spec{"containr_services_exec", "Run a one-off command in the service container (30s ceiling). Args: id, command (required).", "POST", "/services/{id}/exec", []string{"id"}, []string{"command"}, false})
|
||||||
register(spec{"containr_services_update", "Update a service. Args: id, name, image, command, domain, restart_policy, healthcheck_path, cpu, memory, replicas, port, volumes = [{type: volume|bind, source, target, read_only}] — volumes replaces the whole mount list; pass [] to clear. maintenance_mode (bool), basic_auth = [{username, password}] — replaces all creds; [] clears.", "PUT", "/services/{id}", []string{"id"}, nil, false})
|
register(spec{"containr_services_update", "Update a service. Args: id, name, image, command, domain, restart_policy, healthcheck_path, cpu, memory, replicas, port, volumes = [{type: volume|bind, source, target, read_only}] — volumes replaces the whole mount list; pass [] to clear. maintenance_mode (bool), basic_auth = [{username, password}] — replaces all creds; [] clears. builder = auto|railpack|nixpacks|dockerfile|static, cpu_reserve, memory_reserve, static_build_cmd, static_dir (empty string clears reserves/static fields). sleep_enabled (bool) + sleep_idle_minutes (1-1440) enable scale-to-zero on idle; a wake placeholder answers traffic while the service resumes.", "PUT", "/services/{id}", []string{"id"}, nil, false})
|
||||||
|
|
||||||
// Domains
|
// Domains
|
||||||
register(spec{"containr_domains_list", "List domains attached to a service", "GET", "/services/{id}/domains", []string{"id"}, nil, false})
|
register(spec{"containr_domains_list", "List domains attached to a service", "GET", "/services/{id}/domains", []string{"id"}, nil, false})
|
||||||
@@ -70,6 +75,12 @@ func init() {
|
|||||||
register(spec{"containr_domains_default", "Set the default domain. Args: id (service), domain_id.", "POST", "/services/{id}/domains/{domain_id}/default", []string{"id", "domain_id"}, nil, false})
|
register(spec{"containr_domains_default", "Set the default domain. Args: id (service), domain_id.", "POST", "/services/{id}/domains/{domain_id}/default", []string{"id", "domain_id"}, nil, false})
|
||||||
register(spec{"containr_domains_check", "DNS preflight for all service domains — returns ok|wrong-target|pending per domain", "GET", "/services/{id}/domains/check", []string{"id"}, nil, false})
|
register(spec{"containr_domains_check", "DNS preflight for all service domains — returns ok|wrong-target|pending per domain", "GET", "/services/{id}/domains/check", []string{"id"}, nil, false})
|
||||||
|
|
||||||
|
// Registries — private-image pull credentials, owner-scoped.
|
||||||
|
register(spec{"containr_registries_list", "List registry credentials (passwords never returned)", "GET", "/registries", nil, nil, false})
|
||||||
|
register(spec{"containr_registries_create", "Add a registry credential. Args: name (required), host (required — e.g. ghcr.io, docker.io), username, password (stored encrypted).", "POST", "/registries", nil, []string{"name", "host"}, false})
|
||||||
|
register(spec{"containr_registries_update", "Update a registry credential. Args: id, name, host (required), username, password (empty keeps stored).", "PUT", "/registries/{id}", []string{"id"}, []string{"name", "host"}, false})
|
||||||
|
register(spec{"containr_registries_delete", "Remove a registry credential. Requires confirm:true.", "DELETE", "/registries/{id}", []string{"id"}, nil, true})
|
||||||
|
|
||||||
// Docker volumes (admin)
|
// Docker volumes (admin)
|
||||||
register(spec{"containr_volumes_list", "List docker volumes on the node with in-use flags (admin)", "GET", "/admin/volumes", nil, nil, false})
|
register(spec{"containr_volumes_list", "List docker volumes on the node with in-use flags (admin)", "GET", "/admin/volumes", nil, nil, false})
|
||||||
register(spec{"containr_volumes_delete", "Delete an unused docker volume. Requires confirm:true. (admin)", "DELETE", "/admin/volumes/{name}", []string{"name"}, nil, true})
|
register(spec{"containr_volumes_delete", "Delete an unused docker volume. Requires confirm:true. (admin)", "DELETE", "/admin/volumes/{name}", []string{"name"}, nil, true})
|
||||||
@@ -77,6 +88,8 @@ func init() {
|
|||||||
// Variables
|
// Variables
|
||||||
register(spec{"containr_variables_list", "List service environment variables (secrets masked)", "GET", "/services/{id}/variables", []string{"id"}, nil, false})
|
register(spec{"containr_variables_list", "List service environment variables (secrets masked)", "GET", "/services/{id}/variables", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_variables_set", "Replace all variables on a service. Args: id, variables = [{key, value, is_secret}], redeploy (queue a redeploy to apply). Fetch the current list first and merge — the server replaces wholesale.", "PUT", "/services/{id}/variables", []string{"id"}, []string{"variables", "redeploy"}, false})
|
register(spec{"containr_variables_set", "Replace all variables on a service. Args: id, variables = [{key, value, is_secret}], redeploy (queue a redeploy to apply). Fetch the current list first and merge — the server replaces wholesale.", "PUT", "/services/{id}/variables", []string{"id"}, []string{"variables", "redeploy"}, false})
|
||||||
|
register(spec{"containr_shared_variables_list", "List project-level shared variables, referenced from service env as ${{shared.KEY}} (secrets masked). Args: id (project).", "GET", "/projects/{id}/variables", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_shared_variables_set", "Replace all shared project variables. Args: id (project), variables = [{key, value, is_secret}]. Services reference them as ${{shared.KEY}} — resolved at deploy/refresh, not live-updated.", "PUT", "/projects/{id}/variables", []string{"id"}, []string{"variables"}, false})
|
||||||
|
|
||||||
// Deployments
|
// Deployments
|
||||||
register(spec{"containr_deploy", "Trigger a deployment for a service. Args: id (service), commit_hash, branch, no_cache.", "POST", "/services/{id}/deployments", []string{"id"}, []string{"commit_hash", "branch", "trigger", "no_cache"}, false})
|
register(spec{"containr_deploy", "Trigger a deployment for a service. Args: id (service), commit_hash, branch, no_cache.", "POST", "/services/{id}/deployments", []string{"id"}, []string{"commit_hash", "branch", "trigger", "no_cache"}, false})
|
||||||
@@ -90,12 +103,23 @@ func init() {
|
|||||||
// Databases
|
// Databases
|
||||||
register(spec{"containr_databases_list", "List managed databases", "GET", "/databases", nil, nil, false})
|
register(spec{"containr_databases_list", "List managed databases", "GET", "/databases", nil, nil, false})
|
||||||
register(spec{"containr_databases_get", "Get a database (connection URL included)", "GET", "/databases/{id}", []string{"id"}, nil, false})
|
register(spec{"containr_databases_get", "Get a database (connection URL included)", "GET", "/databases/{id}", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_databases_create", "Provision a managed database. Args: name, type (postgres|mysql|mariadb|mongodb|redis|dragonfly|clickhouse).", "POST", "/databases", nil, nil, false})
|
register(spec{"containr_databases_create", "Provision a managed database. Args: name, type (postgres|mysql|mariadb|mongodb|redis|dragonfly|clickhouse), public_port (bind on all interfaces, default localhost-only).", "POST", "/databases", nil, nil, false})
|
||||||
|
register(spec{"containr_databases_update", "Update a database. Args: id, name, backup_schedule (empty clears), backup_target_id (empty clears), public_port — toggling recreates the container so the new bind scope applies.", "PUT", "/databases/{id}", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_databases_register_external", "Register a database hosted elsewhere — probed first, password stored encrypted. Args: name (required), type (required), host (required), port, database, username, password, ssl.", "POST", "/databases/register-external", nil, []string{"name", "type", "host"}, false})
|
||||||
|
register(spec{"containr_databases_test_connection", "Probe a DB connection ad-hoc. Args: type, host (required), port, database, username, password, ssl.", "POST", "/databases/test-connection", nil, []string{"type", "host"}, false})
|
||||||
|
register(spec{"containr_databases_test_connection_by_id", "Re-probe a registered external database with its stored credentials. Args: id.", "POST", "/databases/{id}/test-connection", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_databases_action", "Start/stop/restart a database. Args: id, action.", "POST", "/databases/{id}/action", []string{"id"}, []string{"action"}, false})
|
register(spec{"containr_databases_action", "Start/stop/restart a database. Args: id, action.", "POST", "/databases/{id}/action", []string{"id"}, []string{"action"}, false})
|
||||||
register(spec{"containr_databases_backup", "Take a manual backup snapshot", "POST", "/databases/{id}/backup", []string{"id"}, nil, false})
|
register(spec{"containr_databases_backup", "Take a manual backup snapshot", "POST", "/databases/{id}/backup", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_databases_restore", "Restore a database from a backup — overwrites current data. Requires confirm:true.", "POST", "/databases/{id}/restore", []string{"id"}, []string{"backup_id"}, true})
|
register(spec{"containr_databases_restore", "Restore a database from a backup — overwrites current data. Requires confirm:true.", "POST", "/databases/{id}/restore", []string{"id"}, []string{"backup_id"}, true})
|
||||||
register(spec{"containr_databases_delete", "Delete a database and its data. Requires confirm:true.", "DELETE", "/databases/{id}", []string{"id"}, nil, true})
|
register(spec{"containr_databases_delete", "Delete a database and its data. Requires confirm:true.", "DELETE", "/databases/{id}", []string{"id"}, nil, true})
|
||||||
|
|
||||||
|
// S3-compatible backup targets
|
||||||
|
register(spec{"containr_backup_targets_list", "List S3-compatible backup targets (credentials never returned)", "GET", "/backup-targets", nil, nil, false})
|
||||||
|
register(spec{"containr_backup_targets_create", "Register a backup target — probed first, keys stored encrypted. Args: name, endpoint (required), bucket (required), region, prefix, access_key, secret_key, use_tls.", "POST", "/backup-targets", nil, []string{"endpoint", "bucket"}, false})
|
||||||
|
register(spec{"containr_backup_targets_update", "Update a backup target; empty credentials keep stored keys. Args: id, name, endpoint, bucket, region, prefix, access_key, secret_key, use_tls.", "PUT", "/backup-targets/{id}", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_backup_targets_delete", "Delete a backup target — fails while databases still point at it. Requires confirm:true.", "DELETE", "/backup-targets/{id}", []string{"id"}, nil, true})
|
||||||
|
register(spec{"containr_backup_targets_test", "Re-probe a backup target's bucket access. Args: id.", "POST", "/backup-targets/{id}/test", []string{"id"}, nil, false})
|
||||||
|
|
||||||
// Cron
|
// Cron
|
||||||
register(spec{"containr_cron_list", "List cron jobs", "GET", "/cron-jobs", nil, nil, false})
|
register(spec{"containr_cron_list", "List cron jobs", "GET", "/cron-jobs", nil, nil, false})
|
||||||
register(spec{"containr_cron_create", "Create a cron job. Args: service_id, schedule (5-field), command, name, timezone.", "POST", "/cron-jobs", nil, nil, false})
|
register(spec{"containr_cron_create", "Create a cron job. Args: service_id, schedule (5-field), command, name, timezone.", "POST", "/cron-jobs", nil, nil, false})
|
||||||
@@ -107,16 +131,51 @@ func init() {
|
|||||||
register(spec{"containr_templates_list", "List templates", "GET", "/templates", nil, nil, false})
|
register(spec{"containr_templates_list", "List templates", "GET", "/templates", nil, nil, false})
|
||||||
register(spec{"containr_templates_get", "Get a template definition", "GET", "/templates/{id}", []string{"id"}, nil, false})
|
register(spec{"containr_templates_get", "Get a template definition", "GET", "/templates/{id}", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_templates_deploy", "Deploy a template into a project. Args: id (template), project_id.", "POST", "/templates/{id}/deploy", []string{"id"}, []string{"project_id"}, false})
|
register(spec{"containr_templates_deploy", "Deploy a template into a project. Args: id (template), project_id.", "POST", "/templates/{id}/deploy", []string{"id"}, []string{"project_id"}, false})
|
||||||
|
register(spec{"containr_templates_plan", "Dry-resolve a template: expanded expressions, service refs, missing variables. Args: id, variables (object).", "POST", "/templates/{id}/plan", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_templates_import_compose", "Convert docker-compose YAML into a v2 template graph config. Args: compose_yaml.", "POST", "/templates/import/compose", nil, []string{"compose_yaml"}, false})
|
||||||
|
register(spec{"containr_templates_deploy_graph", "Deploy an ad-hoc service graph without a stored template. Args: project_id, config ({services:[...]}), variables, plan, region.", "POST", "/templates/deploy", nil, []string{"project_id", "config", "variables", "plan", "region"}, false})
|
||||||
|
register(spec{"containr_projects_import_compose", "Deploy a docker-compose file straight into a project. Args: id (project), compose_yaml, variables, plan, region.", "POST", "/projects/{id}/import-compose", []string{"id"}, []string{"compose_yaml", "variables", "plan", "region"}, false})
|
||||||
|
|
||||||
// Notifications
|
// Notifications
|
||||||
register(spec{"containr_notifications_list", "List notifications", "GET", "/notifications", nil, nil, false})
|
register(spec{"containr_notifications_list", "List notifications", "GET", "/notifications", nil, nil, false})
|
||||||
register(spec{"containr_notifications_read", "Mark a notification read, or all notifications with id=all", "POST", "/notifications/{id}/read", []string{"id"}, nil, false})
|
register(spec{"containr_notifications_read", "Mark a notification read, or all notifications with id=all", "POST", "/notifications/{id}/read", []string{"id"}, nil, false})
|
||||||
|
|
||||||
|
register(spec{"containr_webhooks_list", "List outbound webhooks", "GET", "/webhooks", nil, nil, false})
|
||||||
|
register(spec{"containr_webhooks_create", "Create an outbound webhook. Args: name, url, events (list, e.g. [\"service.*\",\"*\"]), secret (auto-generated if omitted), headers, enabled.", "POST", "/webhooks", nil, []string{"name", "url", "events", "secret", "headers", "enabled"}, false})
|
||||||
|
register(spec{"containr_webhooks_update", "Update an outbound webhook. Args: id, plus name/url/secret/events/headers/enabled to change.", "PATCH", "/webhooks/{id}", []string{"id"}, []string{"name", "url", "secret", "events", "headers", "enabled"}, false})
|
||||||
|
register(spec{"containr_webhooks_delete", "Delete an outbound webhook. Args: id. Requires confirm=true.", "DELETE", "/webhooks/{id}", []string{"id"}, nil, true})
|
||||||
|
register(spec{"containr_webhooks_deliveries", "List recent delivery attempts for a webhook. Args: id.", "GET", "/webhooks/{id}/deliveries", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_webhooks_test", "Queue a test ping delivery for a webhook. Args: id.", "POST", "/webhooks/{id}/test", []string{"id"}, nil, false})
|
||||||
|
|
||||||
|
register(spec{"containr_operations_get", "Cross-cutting operations view: active deployments, deploy queue depth, recent failures, cron runs, backups.", "GET", "/operations", nil, nil, false})
|
||||||
|
|
||||||
|
register(spec{"containr_banners_active", "List active announcement banners shown to users.", "GET", "/banners/active", nil, nil, false})
|
||||||
|
register(spec{"containr_banners_list", "List all announcement banners (admin).", "GET", "/admin/banners", nil, nil, false})
|
||||||
|
register(spec{"containr_banners_create", "Create an announcement banner (admin). Args: title, body, level (info|success|warning|error), active, dismissible, starts_at, ends_at.", "POST", "/admin/banners", nil, []string{"title", "body", "level", "active", "dismissible", "starts_at", "ends_at"}, false})
|
||||||
|
register(spec{"containr_banners_update", "Update an announcement banner (admin). Args: id plus fields to change.", "PATCH", "/admin/banners/{id}", []string{"id"}, []string{"title", "body", "level", "active", "dismissible", "starts_at", "ends_at"}, false})
|
||||||
|
register(spec{"containr_banners_delete", "Delete an announcement banner (admin). Args: id. Requires confirm=true.", "DELETE", "/admin/banners/{id}", []string{"id"}, nil, true})
|
||||||
|
|
||||||
|
register(spec{"containr_branding_get", "Get instance white-label branding (product name, logo, accent, links).", "GET", "/branding", nil, nil, false})
|
||||||
|
register(spec{"containr_settings_update", "Update platform settings (admin). Args: signup_enabled (bool), cloudflare_tunnel_token (string, empty clears), branding (object: product_name, logo_url, favicon_url, accent_color, docs_url, support_url — empty string resets a field).", "PUT", "/settings", nil, []string{"signup_enabled", "cloudflare_tunnel_token", "branding"}, false})
|
||||||
|
|
||||||
|
register(spec{"containr_invites_list", "List team invite links (admin).", "GET", "/admin/invites", nil, nil, false})
|
||||||
|
register(spec{"containr_invites_create", "Create a team invite link (admin). Args: email (optional bind), expires_in_hours (default 168). Returns the one-time token + accept URL.", "POST", "/admin/invites", nil, []string{"email", "expires_in_hours"}, false})
|
||||||
|
register(spec{"containr_invites_revoke", "Revoke a team invite (admin). Args: id. Requires confirm=true.", "DELETE", "/admin/invites/{id}", []string{"id"}, nil, true})
|
||||||
|
register(spec{"containr_admin_impersonate", "Mint a 15-minute bearer token acting as a user (admin, audit-logged). Args: id (user id).", "POST", "/admin/users/{id}/impersonate", []string{"id"}, nil, false})
|
||||||
|
register(spec{"containr_github_app_status", "Show GitHub App provisioning status (admin).", "GET", "/admin/git/github-app", nil, nil, false})
|
||||||
|
register(spec{"containr_github_app_manifest", "Build a GitHub App manifest for self-provisioning (admin). Args: base_url (required, https public URL), name (optional), organization (optional).", "POST", "/admin/git/github-app/manifest", nil, []string{"base_url", "name", "organization"}, false})
|
||||||
|
register(spec{"containr_github_app_convert", "Exchange the GitHub manifest callback code for app credentials (admin). Args: code.", "POST", "/admin/git/github-app/convert", nil, []string{"code"}, false})
|
||||||
|
register(spec{"containr_activity_list", "Activity feed — enriched audit events (severity/category/label). Args: severity, category, resource, project_id, since, page, limit.", "GET", "/activity", nil, []string{"severity", "category", "resource", "project_id", "since", "page", "limit"}, false})
|
||||||
|
register(spec{"containr_project_activity", "Per-project activity timeline. Args: id (project id), page, limit.", "GET", "/projects/{id}/activity", []string{"id"}, []string{"page", "limit"}, false})
|
||||||
|
|
||||||
// Nodes / agent tokens (admin)
|
// Nodes / agent tokens (admin)
|
||||||
register(spec{"containr_nodes_list", "List node agents (admin)", "GET", "/agents", nil, nil, false})
|
register(spec{"containr_nodes_list", "List node agents (admin)", "GET", "/agents", nil, nil, false})
|
||||||
register(spec{"containr_nodes_get", "Get a node agent (admin)", "GET", "/agents/{id}", []string{"id"}, nil, false})
|
register(spec{"containr_nodes_get", "Get a node agent (admin)", "GET", "/agents/{id}", []string{"id"}, nil, false})
|
||||||
register(spec{"containr_agent_tokens_issue", "Issue an agent onboarding token (admin). Args: label.", "POST", "/agent-tokens", nil, []string{"label"}, false})
|
register(spec{"containr_agent_tokens_issue", "Issue an agent onboarding token (admin). Args: label.", "POST", "/agent-tokens", nil, []string{"label"}, false})
|
||||||
register(spec{"containr_agent_tokens_list", "List agent onboarding tokens (admin)", "GET", "/agent-tokens", nil, nil, false})
|
register(spec{"containr_agent_tokens_list", "List agent onboarding tokens (admin)", "GET", "/agent-tokens", nil, nil, false})
|
||||||
|
register(spec{"containr_nodes_update", "Update a node agent (admin). Args: id; optional auto_prune, name.", "PUT", "/agents/{id}", []string{"id"}, []string{"auto_prune", "name"}, false})
|
||||||
|
register(spec{"containr_nodes_prune", "Enqueue a bounded docker system prune on a node (admin). Args: id; optional until (e.g. 168h), volumes (danger, off by default).", "POST", "/agents/{id}/prune", []string{"id"}, []string{"until", "volumes"}, true})
|
||||||
|
register(spec{"containr_nodes_commands", "List recent commands sent to a node agent (admin). Args: id.", "GET", "/agents/{id}/commands", []string{"id"}, nil, false})
|
||||||
|
|
||||||
// Scaling / HA / security
|
// Scaling / HA / security
|
||||||
register(spec{"containr_scaling_status", "Autoscaler status", "GET", "/scaling/status", nil, nil, false})
|
register(spec{"containr_scaling_status", "Autoscaler status", "GET", "/scaling/status", nil, nil, false})
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ type ScanSummary struct {
|
|||||||
// Scanner handles security scanning operations
|
// Scanner handles security scanning operations
|
||||||
type Scanner struct {
|
type Scanner struct {
|
||||||
db *database.DB
|
db *database.DB
|
||||||
|
// OnScanComplete fires after a scan finishes (goroutine context) —
|
||||||
|
// wired by the api layer for notifications. Optional.
|
||||||
|
OnScanComplete func(scan *SecurityScan)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewScanner creates a new security scanner
|
// NewScanner creates a new security scanner
|
||||||
@@ -115,8 +118,11 @@ func (s *Scanner) performScan(scan *SecurityScan) {
|
|||||||
|
|
||||||
// Update scan with results
|
// Update scan with results
|
||||||
completedAt := time.Now()
|
completedAt := time.Now()
|
||||||
|
scan.CompletedAt = &completedAt
|
||||||
|
scan.Summary = summary
|
||||||
|
scan.Status = "completed"
|
||||||
_, err := s.db.Exec(`
|
_, err := s.db.Exec(`
|
||||||
UPDATE security_scans
|
UPDATE security_scans
|
||||||
SET status = $1, completed_at = $2, summary = $3
|
SET status = $1, completed_at = $2, summary = $3
|
||||||
WHERE id = $4
|
WHERE id = $4
|
||||||
`, "completed", completedAt, summaryToJSON(summary), scan.ID)
|
`, "completed", completedAt, summaryToJSON(summary), scan.ID)
|
||||||
@@ -137,6 +143,10 @@ func (s *Scanner) performScan(scan *SecurityScan) {
|
|||||||
log.Printf("Failed to store vulnerability %s: %v", vuln.ID, err)
|
log.Printf("Failed to store vulnerability %s: %v", vuln.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.OnScanComplete != nil {
|
||||||
|
s.OnScanComplete(scan)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// scanDependencies scans for known vulnerable dependencies
|
// scanDependencies scans for known vulnerable dependencies
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ type BuildRequest struct {
|
|||||||
Branch string `json:"branch"` // Git branch
|
Branch string `json:"branch"` // Git branch
|
||||||
Commit string `json:"commit"` // Git commit SHA
|
Commit string `json:"commit"` // Git commit SHA
|
||||||
NoCache bool `json:"no_cache"` // Disable Docker layer cache
|
NoCache bool `json:"no_cache"` // Disable Docker layer cache
|
||||||
|
|
||||||
|
// Registry credentials for pulling a private prebuilt image.
|
||||||
|
PullUsername string `json:"pull_username,omitempty"`
|
||||||
|
PullPassword string `json:"pull_password,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildResponse represents the response from a build operation
|
// BuildResponse represents the response from a build operation
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE services ADD COLUMN IF NOT EXISTS builder varchar(32) NOT NULL DEFAULT 'auto';
|
||||||
|
ALTER TABLE services ADD COLUMN IF NOT EXISTS cpu_reserve varchar(20) NOT NULL DEFAULT '';
|
||||||
|
ALTER TABLE services ADD COLUMN IF NOT EXISTS memory_reserve varchar(20) NOT NULL DEFAULT '';
|
||||||
|
ALTER TABLE services ADD COLUMN IF NOT EXISTS static_build_cmd varchar(255) NOT NULL DEFAULT '';
|
||||||
|
ALTER TABLE services ADD COLUMN IF NOT EXISTS static_dir varchar(255) NOT NULL DEFAULT '';
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE services DROP COLUMN IF EXISTS static_dir;
|
||||||
|
ALTER TABLE services DROP COLUMN IF EXISTS static_build_cmd;
|
||||||
|
ALTER TABLE services DROP COLUMN IF EXISTS memory_reserve;
|
||||||
|
ALTER TABLE services DROP COLUMN IF EXISTS cpu_reserve;
|
||||||
|
ALTER TABLE services DROP COLUMN IF EXISTS builder;
|
||||||
|
-- +goose StatementEnd
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS registries (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
host VARCHAR(255) NOT NULL,
|
||||||
|
username VARCHAR(255) NOT NULL DEFAULT '',
|
||||||
|
password TEXT NOT NULL DEFAULT '',
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
UNIQUE (owner_id, host)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_registries_owner ON registries(owner_id);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
DROP TABLE IF EXISTS registries;
|
||||||
|
-- +goose StatementEnd
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE database_services
|
||||||
|
ADD COLUMN IF NOT EXISTS provider VARCHAR(20) NOT NULL DEFAULT 'managed',
|
||||||
|
ADD COLUMN IF NOT EXISTS external_host VARCHAR(255),
|
||||||
|
ADD COLUMN IF NOT EXISTS external_port INTEGER,
|
||||||
|
ADD COLUMN IF NOT EXISTS external_name VARCHAR(255),
|
||||||
|
ADD COLUMN IF NOT EXISTS external_username VARCHAR(255),
|
||||||
|
ADD COLUMN IF NOT EXISTS external_password TEXT NOT NULL DEFAULT '',
|
||||||
|
ADD COLUMN IF NOT EXISTS external_ssl BOOLEAN NOT NULL DEFAULT false;
|
||||||
|
|
||||||
|
ALTER TABLE database_services
|
||||||
|
DROP CONSTRAINT IF EXISTS database_services_plan_check,
|
||||||
|
ADD CONSTRAINT database_services_plan_check
|
||||||
|
CHECK (plan IN ('hobby', 'starter', 'standard', 'business', 'external'));
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE database_services
|
||||||
|
DROP CONSTRAINT IF EXISTS database_services_plan_check,
|
||||||
|
ADD CONSTRAINT database_services_plan_check
|
||||||
|
CHECK (plan IN ('hobby', 'starter', 'standard', 'business'));
|
||||||
|
|
||||||
|
ALTER TABLE database_services
|
||||||
|
DROP COLUMN IF EXISTS provider,
|
||||||
|
DROP COLUMN IF EXISTS external_host,
|
||||||
|
DROP COLUMN IF EXISTS external_port,
|
||||||
|
DROP COLUMN IF EXISTS external_name,
|
||||||
|
DROP COLUMN IF EXISTS external_username,
|
||||||
|
DROP COLUMN IF EXISTS external_password,
|
||||||
|
DROP COLUMN IF EXISTS external_ssl;
|
||||||
|
-- +goose StatementEnd
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE database_services
|
||||||
|
ADD COLUMN IF NOT EXISTS public_port BOOLEAN NOT NULL DEFAULT false;
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE database_services
|
||||||
|
DROP COLUMN IF EXISTS public_port;
|
||||||
|
-- +goose StatementEnd
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS backup_targets (
|
||||||
|
id VARCHAR(255) PRIMARY KEY,
|
||||||
|
user_id VARCHAR(255) NOT NULL,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
endpoint VARCHAR(512) NOT NULL,
|
||||||
|
bucket VARCHAR(255) NOT NULL,
|
||||||
|
region VARCHAR(100) NOT NULL DEFAULT '',
|
||||||
|
prefix VARCHAR(255) NOT NULL DEFAULT '',
|
||||||
|
access_key TEXT NOT NULL DEFAULT '',
|
||||||
|
secret_key TEXT NOT NULL DEFAULT '',
|
||||||
|
use_tls BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
ALTER TABLE database_services
|
||||||
|
ADD COLUMN IF NOT EXISTS backup_target_id VARCHAR(255);
|
||||||
|
|
||||||
|
ALTER TABLE database_backups
|
||||||
|
ADD COLUMN IF NOT EXISTS remote_key VARCHAR(512);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE database_backups
|
||||||
|
DROP COLUMN IF EXISTS remote_key;
|
||||||
|
|
||||||
|
ALTER TABLE database_services
|
||||||
|
DROP COLUMN IF EXISTS backup_target_id;
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS backup_targets;
|
||||||
|
-- +goose StatementEnd
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE services ADD COLUMN IF NOT EXISTS sleep_enabled BOOLEAN NOT NULL DEFAULT false;
|
||||||
|
ALTER TABLE services ADD COLUMN IF NOT EXISTS sleep_idle_minutes INTEGER NOT NULL DEFAULT 15;
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE services DROP COLUMN IF EXISTS sleep_enabled;
|
||||||
|
ALTER TABLE services DROP COLUMN IF EXISTS sleep_idle_minutes;
|
||||||
|
-- +goose StatementEnd
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- Project-level shared variables (Railway-style). Services reference them via
|
||||||
|
-- ${{shared.KEY}} in environment values; secret entries are encrypted at rest
|
||||||
|
-- and masked in API responses exactly like service variables.
|
||||||
|
CREATE TABLE IF NOT EXISTS project_variables (
|
||||||
|
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
|
||||||
|
project_id uuid NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
||||||
|
key varchar(255) NOT NULL,
|
||||||
|
value text NOT NULL DEFAULT '',
|
||||||
|
is_secret boolean NOT NULL DEFAULT false,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
UNIQUE (project_id, key)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_project_variables_project ON project_variables(project_id);
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP TABLE IF EXISTS project_variables;
|
||||||
Loaded 100 of 178 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user