diff options
| author | Clawd <ai@clawd.bot> | 2026-02-15 18:28:35 -0800 |
|---|---|---|
| committer | Clawd <ai@clawd.bot> | 2026-02-15 18:28:35 -0800 |
| commit | f1d6907f098e1fe66a6b2f9e89abfe70707b53a9 (patch) | |
| tree | cbef9aa79458749dc7d3550486d68c510c96a457 | |
| parent | 738113867cb591b03c6dcfb4a80826c1a884fd2b (diff) | |
docs: add comprehensive technical specification
| -rw-r--r-- | SPEC.md | 499 |
1 files changed, 499 insertions, 0 deletions
| @@ -0,0 +1,499 @@ | |||
| 1 | # Ship v2 Technical Specification | ||
| 2 | |||
| 3 | ## Overview | ||
| 4 | |||
| 5 | Ship deploys code to a VPS. Input: path to code. Output: JSON with URL. | ||
| 6 | |||
| 7 | ```bash | ||
| 8 | ship ./myproject | ||
| 9 | # {"status":"ok","name":"ship-a1b2c3","url":"https://ship-a1b2c3.example.com","type":"docker","took_ms":8200} | ||
| 10 | ``` | ||
| 11 | |||
| 12 | ## CLI Interface | ||
| 13 | |||
| 14 | ### Primary Command | ||
| 15 | |||
| 16 | ``` | ||
| 17 | ship [PATH] [FLAGS] | ||
| 18 | ``` | ||
| 19 | |||
| 20 | **PATH**: File or directory to deploy. Defaults to `.` (current directory). | ||
| 21 | |||
| 22 | **FLAGS**: | ||
| 23 | | Flag | Type | Default | Description | | ||
| 24 | |------|------|---------|-------------| | ||
| 25 | | `--name` | string | auto-generated | Deploy name (becomes subdomain) | | ||
| 26 | | `--host` | string | default host | VPS host (SSH config name or user@host) | | ||
| 27 | | `--health` | string | `/` for static, none for apps | Health check endpoint | | ||
| 28 | | `--ttl` | duration | none | Auto-delete after duration (e.g., `1h`, `7d`) | | ||
| 29 | | `--env` | string[] | none | Environment variables (`KEY=VALUE`) | | ||
| 30 | | `--env-file` | string | none | Path to .env file | | ||
| 31 | | `--pretty` | bool | false | Human-readable output | | ||
| 32 | |||
| 33 | ### Other Commands | ||
| 34 | |||
| 35 | ``` | ||
| 36 | ship list [--host HOST] | ||
| 37 | ship status NAME [--host HOST] | ||
| 38 | ship logs NAME [--lines N] [--host HOST] | ||
| 39 | ship remove NAME [--host HOST] | ||
| 40 | ship host init USER@HOST --domain DOMAIN | ||
| 41 | ship host status [--host HOST] | ||
| 42 | ``` | ||
| 43 | |||
| 44 | All commands output JSON unless `--pretty` is set. | ||
| 45 | |||
| 46 | ## Output Schema | ||
| 47 | |||
| 48 | ### Success Response | ||
| 49 | |||
| 50 | ```typescript | ||
| 51 | { | ||
| 52 | status: "ok", | ||
| 53 | name: string, | ||
| 54 | url: string, | ||
| 55 | type: "static" | "docker" | "binary", | ||
| 56 | took_ms: number, | ||
| 57 | health?: { | ||
| 58 | endpoint: string, | ||
| 59 | status: number, | ||
| 60 | latency_ms: number | ||
| 61 | }, | ||
| 62 | expires?: string // ISO 8601, only if --ttl set | ||
| 63 | } | ||
| 64 | ``` | ||
| 65 | |||
| 66 | ### Error Response | ||
| 67 | |||
| 68 | ```typescript | ||
| 69 | { | ||
| 70 | status: "error", | ||
| 71 | code: string, | ||
| 72 | message: string, | ||
| 73 | name?: string, | ||
| 74 | url?: string | ||
| 75 | } | ||
| 76 | ``` | ||
| 77 | |||
| 78 | ### Exit Codes | ||
| 79 | |||
| 80 | | Code | Meaning | | ||
| 81 | |------|---------| | ||
| 82 | | 0 | Success | | ||
| 83 | | 1 | Deploy failed | | ||
| 84 | | 2 | Invalid arguments | | ||
| 85 | | 3 | SSH connection failed | | ||
| 86 | | 4 | Health check failed | | ||
| 87 | |||
| 88 | ## Error Codes | ||
| 89 | |||
| 90 | | Code | Description | | ||
| 91 | |------|-------------| | ||
| 92 | | `INVALID_PATH` | Path doesn't exist or isn't readable | | ||
| 93 | | `UNKNOWN_PROJECT_TYPE` | Can't detect how to deploy | | ||
| 94 | | `SSH_CONNECT_FAILED` | Can't establish SSH connection | | ||
| 95 | | `SSH_AUTH_FAILED` | SSH key rejected | | ||
| 96 | | `UPLOAD_FAILED` | SCP/rsync failed | | ||
| 97 | | `BUILD_FAILED` | Docker build or compilation failed | | ||
| 98 | | `SERVICE_FAILED` | systemd unit failed to start | | ||
| 99 | | `CADDY_FAILED` | Caddy reload failed | | ||
| 100 | | `HEALTH_CHECK_FAILED` | App didn't respond in time | | ||
| 101 | | `HEALTH_CHECK_TIMEOUT` | Health check timed out | | ||
| 102 | | `NOT_FOUND` | Named deploy doesn't exist | | ||
| 103 | | `CONFLICT` | Name already in use (if --no-update) | | ||
| 104 | | `HOST_NOT_CONFIGURED` | No default host, --host required | | ||
| 105 | | `INVALID_TTL` | Can't parse TTL duration | | ||
| 106 | |||
| 107 | ## Auto-Detection Logic | ||
| 108 | |||
| 109 | When given a path, ship determines deploy type: | ||
| 110 | |||
| 111 | ``` | ||
| 112 | is_file(path)? | ||
| 113 | → is_executable(path)? | ||
| 114 | → BINARY | ||
| 115 | → ERROR: "Not an executable file" | ||
| 116 | |||
| 117 | is_directory(path)? | ||
| 118 | → has_file("Dockerfile")? | ||
| 119 | → DOCKER | ||
| 120 | → has_file("index.html") OR has_file("index.htm")? | ||
| 121 | → STATIC | ||
| 122 | → has_file("go.mod") AND NOT has_file("Dockerfile")? | ||
| 123 | → ERROR: "Go project without Dockerfile. Add a Dockerfile or build a binary." | ||
| 124 | → has_file("package.json") AND NOT has_file("Dockerfile")? | ||
| 125 | → ERROR: "Node project without Dockerfile. Add a Dockerfile." | ||
| 126 | → is_empty(path)? | ||
| 127 | → ERROR: "Directory is empty" | ||
| 128 | → ERROR: "Can't detect project type. Add a Dockerfile or index.html." | ||
| 129 | ``` | ||
| 130 | |||
| 131 | ## Name Generation | ||
| 132 | |||
| 133 | If `--name` not provided: | ||
| 134 | |||
| 135 | ``` | ||
| 136 | name = "ship-" + random_alphanumeric(6) | ||
| 137 | ``` | ||
| 138 | |||
| 139 | Names must match: `^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$` | ||
| 140 | |||
| 141 | Invalid names return `INVALID_NAME` error. | ||
| 142 | |||
| 143 | ## Port Allocation | ||
| 144 | |||
| 145 | Ports are allocated server-side. | ||
| 146 | |||
| 147 | ### Server Files | ||
| 148 | |||
| 149 | ``` | ||
| 150 | /etc/ship/ports/<name> # Contains allocated port number | ||
| 151 | ``` | ||
| 152 | |||
| 153 | ### Allocation Algorithm | ||
| 154 | |||
| 155 | ```python | ||
| 156 | def allocate_port(name): | ||
| 157 | port_file = f"/etc/ship/ports/{name}" | ||
| 158 | |||
| 159 | if exists(port_file): | ||
| 160 | return int(read(port_file)) | ||
| 161 | |||
| 162 | used_ports = [int(read(f)) for f in glob("/etc/ship/ports/*")] | ||
| 163 | next_port = 9000 | ||
| 164 | while next_port in used_ports: | ||
| 165 | next_port += 1 | ||
| 166 | |||
| 167 | write(port_file, str(next_port)) | ||
| 168 | return next_port | ||
| 169 | ``` | ||
| 170 | |||
| 171 | ### Port Range | ||
| 172 | |||
| 173 | - Start: 9000 | ||
| 174 | - Max: 9999 | ||
| 175 | - Error if exhausted: `PORT_EXHAUSTED` | ||
| 176 | |||
| 177 | ## Server File Layout | ||
| 178 | |||
| 179 | ``` | ||
| 180 | /etc/ship/ | ||
| 181 | ports/<name> # Port allocation (contains port number) | ||
| 182 | env/<name>.env # Environment variables | ||
| 183 | ttl/<name> # TTL expiry timestamp (Unix epoch) | ||
| 184 | |||
| 185 | /var/www/<name>/ # Static site files | ||
| 186 | |||
| 187 | /var/lib/<name>/ | ||
| 188 | src/ # Docker build context (checked out source) | ||
| 189 | data/ # Persistent data volume | ||
| 190 | |||
| 191 | /etc/systemd/system/<name>.service # systemd unit | ||
| 192 | /etc/caddy/sites-enabled/<name>.caddy # Caddy config | ||
| 193 | ``` | ||
| 194 | |||
| 195 | ## Deploy Flows | ||
| 196 | |||
| 197 | ### Static Site | ||
| 198 | |||
| 199 | 1. Validate path is directory with index.html | ||
| 200 | 2. Generate name if needed | ||
| 201 | 3. SSH: Create `/var/www/<name>/` | ||
| 202 | 4. rsync: Upload directory contents to `/var/www/<name>/` | ||
| 203 | 5. SSH: Set ownership to `www-data` | ||
| 204 | 6. Generate Caddyfile | ||
| 205 | 7. SSH: Write to `/etc/caddy/sites-enabled/<name>.caddy` | ||
| 206 | 8. SSH: Reload Caddy | ||
| 207 | 9. Health check: `GET https://<name>.<domain>/` | ||
| 208 | 10. If TTL: Write expiry to `/etc/ship/ttl/<name>` | ||
| 209 | 11. Return JSON | ||
| 210 | |||
| 211 | ### Docker | ||
| 212 | |||
| 213 | 1. Validate path is directory with Dockerfile | ||
| 214 | 2. Generate name if needed | ||
| 215 | 3. Allocate port (server-side) | ||
| 216 | 4. rsync: Upload directory to `/var/lib/<name>/src/` | ||
| 217 | 5. SSH: `docker build -t <name> /var/lib/<name>/src/` | ||
| 218 | 6. Generate systemd unit (runs `docker run`) | ||
| 219 | 7. SSH: Write to `/etc/systemd/system/<name>.service` | ||
| 220 | 8. SSH: Write env file to `/etc/ship/env/<name>.env` | ||
| 221 | 9. SSH: `systemctl daemon-reload && systemctl restart <name>` | ||
| 222 | 10. Generate Caddyfile (reverse proxy to port) | ||
| 223 | 11. SSH: Write to `/etc/caddy/sites-enabled/<name>.caddy` | ||
| 224 | 12. SSH: Reload Caddy | ||
| 225 | 13. Health check: `GET https://<name>.<domain><health_endpoint>` | ||
| 226 | 14. If TTL: Write expiry to `/etc/ship/ttl/<name>` | ||
| 227 | 15. Return JSON | ||
| 228 | |||
| 229 | ### Binary | ||
| 230 | |||
| 231 | 1. Validate path is executable file | ||
| 232 | 2. Generate name if needed | ||
| 233 | 3. Allocate port (server-side) | ||
| 234 | 4. SCP: Upload binary to `/usr/local/bin/<name>` | ||
| 235 | 5. SSH: `chmod +x /usr/local/bin/<name>` | ||
| 236 | 6. Generate systemd unit | ||
| 237 | 7. SSH: Write to `/etc/systemd/system/<name>.service` | ||
| 238 | 8. SSH: Write env file to `/etc/ship/env/<name>.env` | ||
| 239 | 9. SSH: `systemctl daemon-reload && systemctl restart <name>` | ||
| 240 | 10. Generate Caddyfile (reverse proxy to port) | ||
| 241 | 11. SSH: Write to `/etc/caddy/sites-enabled/<name>.caddy` | ||
| 242 | 12. SSH: Reload Caddy | ||
| 243 | 13. Health check: `GET https://<name>.<domain><health_endpoint>` | ||
| 244 | 14. If TTL: Write expiry to `/etc/ship/ttl/<name>` | ||
| 245 | 15. Return JSON | ||
| 246 | |||
| 247 | ## Health Checks | ||
| 248 | |||
| 249 | ### Behavior | ||
| 250 | |||
| 251 | After deploy, verify the app is responding: | ||
| 252 | |||
| 253 | 1. Wait 2 seconds (let app start) | ||
| 254 | 2. `GET https://<name>.<domain><health_endpoint>` | ||
| 255 | 3. If 2xx or 3xx: success | ||
| 256 | 4. If error or timeout: retry after 2s | ||
| 257 | 5. Max retries: 15 (total 30s) | ||
| 258 | 6. If all retries fail: return `HEALTH_CHECK_FAILED` | ||
| 259 | |||
| 260 | ### Health Endpoint | ||
| 261 | |||
| 262 | | Deploy Type | Default | Override | | ||
| 263 | |-------------|---------|----------| | ||
| 264 | | static | `/` | `--health` | | ||
| 265 | | docker | none (skip) | `--health` | | ||
| 266 | | binary | none (skip) | `--health` | | ||
| 267 | |||
| 268 | When no health endpoint: skip health check, return success after service starts. | ||
| 269 | |||
| 270 | ## TTL (Auto-Expiry) | ||
| 271 | |||
| 272 | ### Setting TTL | ||
| 273 | |||
| 274 | ```bash | ||
| 275 | ship ./site --name preview --ttl 24h | ||
| 276 | ``` | ||
| 277 | |||
| 278 | Supported formats: `30m`, `1h`, `24h`, `7d` | ||
| 279 | |||
| 280 | ### Server-Side Cleanup | ||
| 281 | |||
| 282 | A systemd timer runs hourly: | ||
| 283 | |||
| 284 | ```ini | ||
| 285 | # /etc/systemd/system/ship-cleanup.timer | ||
| 286 | [Timer] | ||
| 287 | OnCalendar=hourly | ||
| 288 | Persistent=true | ||
| 289 | |||
| 290 | # /etc/systemd/system/ship-cleanup.service | ||
| 291 | [Service] | ||
| 292 | Type=oneshot | ||
| 293 | ExecStart=/usr/local/bin/ship-cleanup | ||
| 294 | ``` | ||
| 295 | |||
| 296 | Cleanup script: | ||
| 297 | |||
| 298 | ```bash | ||
| 299 | #!/bin/bash | ||
| 300 | now=$(date +%s) | ||
| 301 | for f in /etc/ship/ttl/*; do | ||
| 302 | name=$(basename "$f") | ||
| 303 | expires=$(cat "$f") | ||
| 304 | if [ "$now" -gt "$expires" ]; then | ||
| 305 | ship remove "$name" | ||
| 306 | fi | ||
| 307 | done | ||
| 308 | ``` | ||
| 309 | |||
| 310 | ### TTL File Format | ||
| 311 | |||
| 312 | ``` | ||
| 313 | /etc/ship/ttl/<name> | ||
| 314 | ``` | ||
| 315 | |||
| 316 | Contents: Unix timestamp (seconds since epoch) | ||
| 317 | |||
| 318 | ## Templates | ||
| 319 | |||
| 320 | ### systemd Unit (Docker) | ||
| 321 | |||
| 322 | ```ini | ||
| 323 | [Unit] | ||
| 324 | Description=ship: {{.Name}} | ||
| 325 | After=docker.service | ||
| 326 | Requires=docker.service | ||
| 327 | |||
| 328 | [Service] | ||
| 329 | Restart=always | ||
| 330 | RestartSec=5 | ||
| 331 | EnvironmentFile=/etc/ship/env/{{.Name}}.env | ||
| 332 | ExecStartPre=-/usr/bin/docker stop {{.Name}} | ||
| 333 | ExecStartPre=-/usr/bin/docker rm {{.Name}} | ||
| 334 | ExecStart=/usr/bin/docker run --rm --name {{.Name}} \ | ||
| 335 | --env-file /etc/ship/env/{{.Name}}.env \ | ||
| 336 | -p 127.0.0.1:{{.Port}}:{{.Port}} \ | ||
| 337 | -v /var/lib/{{.Name}}/data:/data \ | ||
| 338 | {{.Name}} | ||
| 339 | ExecStop=/usr/bin/docker stop {{.Name}} | ||
| 340 | |||
| 341 | [Install] | ||
| 342 | WantedBy=multi-user.target | ||
| 343 | ``` | ||
| 344 | |||
| 345 | ### systemd Unit (Binary) | ||
| 346 | |||
| 347 | ```ini | ||
| 348 | [Unit] | ||
| 349 | Description=ship: {{.Name}} | ||
| 350 | After=network.target | ||
| 351 | |||
| 352 | [Service] | ||
| 353 | Type=simple | ||
| 354 | Restart=always | ||
| 355 | RestartSec=5 | ||
| 356 | User={{.Name}} | ||
| 357 | EnvironmentFile=/etc/ship/env/{{.Name}}.env | ||
| 358 | ExecStart=/usr/local/bin/{{.Name}} | ||
| 359 | WorkingDirectory=/var/lib/{{.Name}} | ||
| 360 | |||
| 361 | [Install] | ||
| 362 | WantedBy=multi-user.target | ||
| 363 | ``` | ||
| 364 | |||
| 365 | ### Caddyfile (Reverse Proxy) | ||
| 366 | |||
| 367 | ``` | ||
| 368 | {{.Domain}} { | ||
| 369 | reverse_proxy localhost:{{.Port}} | ||
| 370 | } | ||
| 371 | ``` | ||
| 372 | |||
| 373 | ### Caddyfile (Static) | ||
| 374 | |||
| 375 | ``` | ||
| 376 | {{.Domain}} { | ||
| 377 | root * /var/www/{{.Name}} | ||
| 378 | file_server | ||
| 379 | encode gzip | ||
| 380 | } | ||
| 381 | ``` | ||
| 382 | |||
| 383 | ## Environment Variables | ||
| 384 | |||
| 385 | Always set by ship: | ||
| 386 | |||
| 387 | | Variable | Value | | ||
| 388 | |----------|-------| | ||
| 389 | | `PORT` | Allocated port number | | ||
| 390 | | `SHIP_NAME` | Deploy name | | ||
| 391 | | `SHIP_URL` | Full URL | | ||
| 392 | |||
| 393 | User variables from `--env` or `--env-file` are merged. | ||
| 394 | |||
| 395 | ## Host Initialization | ||
| 396 | |||
| 397 | ```bash | ||
| 398 | ship host init user@vps.example.com --domain example.com | ||
| 399 | ``` | ||
| 400 | |||
| 401 | ### Steps | ||
| 402 | |||
| 403 | 1. SSH: Check connectivity | ||
| 404 | 2. SSH: Install packages (`apt install -y caddy docker.io`) | ||
| 405 | 3. SSH: Enable services (`systemctl enable --now caddy docker`) | ||
| 406 | 4. SSH: Create directories (`/etc/ship/ports`, `/etc/ship/env`, `/etc/ship/ttl`) | ||
| 407 | 5. SSH: Install cleanup timer | ||
| 408 | 6. SSH: Configure Caddy base | ||
| 409 | 7. Update local state with host info | ||
| 410 | |||
| 411 | ### Output | ||
| 412 | |||
| 413 | ```json | ||
| 414 | { | ||
| 415 | "status": "ok", | ||
| 416 | "host": "vps.example.com", | ||
| 417 | "domain": "example.com", | ||
| 418 | "installed": ["caddy", "docker"] | ||
| 419 | } | ||
| 420 | ``` | ||
| 421 | |||
| 422 | ## Local State | ||
| 423 | |||
| 424 | Stored at `~/.config/ship/state.json`: | ||
| 425 | |||
| 426 | ```json | ||
| 427 | { | ||
| 428 | "default_host": "vps", | ||
| 429 | "hosts": { | ||
| 430 | "vps": { | ||
| 431 | "ssh": "user@vps.example.com", | ||
| 432 | "domain": "example.com" | ||
| 433 | } | ||
| 434 | } | ||
| 435 | } | ||
| 436 | ``` | ||
| 437 | |||
| 438 | Local state is minimal — server is source of truth for ports, deploys, TTLs. | ||
| 439 | |||
| 440 | ## Pretty Output | ||
| 441 | |||
| 442 | When `--pretty` is set (or `SHIP_PRETTY=1`): | ||
| 443 | |||
| 444 | ### Deploy Success | ||
| 445 | |||
| 446 | ``` | ||
| 447 | ✓ Deployed to https://myapp.example.com (4.2s) | ||
| 448 | ``` | ||
| 449 | |||
| 450 | ### Deploy Failure | ||
| 451 | |||
| 452 | ``` | ||
| 453 | ✗ Deploy failed: health check timed out after 30s | ||
| 454 | ``` | ||
| 455 | |||
| 456 | ### List | ||
| 457 | |||
| 458 | ``` | ||
| 459 | NAME URL TYPE STATUS | ||
| 460 | api https://api.example.com docker running | ||
| 461 | preview-x7k https://preview-x7k.example.com static running (expires in 23h) | ||
| 462 | ``` | ||
| 463 | |||
| 464 | ## Implementation Notes | ||
| 465 | |||
| 466 | ### SSH Execution | ||
| 467 | |||
| 468 | Use a single SSH connection with multiplexing: | ||
| 469 | |||
| 470 | ```bash | ||
| 471 | ssh -o ControlMaster=auto -o ControlPath=/tmp/ship-%r@%h:%p -o ControlPersist=60 ... | ||
| 472 | ``` | ||
| 473 | |||
| 474 | ### Concurrency | ||
| 475 | |||
| 476 | Ship is not designed for concurrent deploys of the same name. Behavior is undefined. | ||
| 477 | |||
| 478 | Different names can deploy concurrently. | ||
| 479 | |||
| 480 | ### Rollback (Future) | ||
| 481 | |||
| 482 | Not in v2.0. Future consideration: keep last N versions, `ship rollback <name>`. | ||
| 483 | |||
| 484 | --- | ||
| 485 | |||
| 486 | ## Appendix: Example Session | ||
| 487 | |||
| 488 | ```bash | ||
| 489 | $ mkdir -p /tmp/hello && echo '<h1>Hello</h1>' > /tmp/hello/index.html | ||
| 490 | |||
| 491 | $ ship /tmp/hello --name hello | ||
| 492 | {"status":"ok","name":"hello","url":"https://hello.example.com","type":"static","took_ms":3200,"health":{"endpoint":"/","status":200,"latency_ms":45}} | ||
| 493 | |||
| 494 | $ ship list | ||
| 495 | {"status":"ok","deploys":[{"name":"hello","url":"https://hello.example.com","type":"static","running":true}]} | ||
| 496 | |||
| 497 | $ ship remove hello | ||
| 498 | {"status":"ok","name":"hello","removed":true} | ||
| 499 | ``` | ||
