summaryrefslogtreecommitdiffstats
path: root/SPEC.md
blob: 50b54c26931b489a780817ecd37284120e9377c8 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# Ship v2 Technical Specification

## Overview

Ship deploys code to a VPS. Input: path to code. Output: JSON with URL.

```bash
ship ./myproject
# {"status":"ok","name":"ship-a1b2c3","url":"https://ship-a1b2c3.example.com","type":"docker","took_ms":8200}
```

## CLI Interface

### Primary Command

```
ship [PATH] [FLAGS]
```

**PATH**: File or directory to deploy. Defaults to `.` (current directory).

**FLAGS**:
| Flag | Type | Default | Description |
|------|------|---------|-------------|
| `--name` | string | auto-generated | Deploy name (becomes subdomain) |
| `--host` | string | default host | VPS host (SSH config name or user@host) |
| `--health` | string | `/` for static, none for apps | Health check endpoint |
| `--ttl` | duration | none | Auto-delete after duration (e.g., `1h`, `7d`) |
| `--env` | string[] | none | Environment variables (`KEY=VALUE`) |
| `--env-file` | string | none | Path to .env file |
| `--pretty` | bool | false | Human-readable output |

### Other Commands

```
ship list [--host HOST]
ship status NAME [--host HOST]
ship logs NAME [--lines N] [--host HOST]
ship remove NAME [--host HOST]
ship host init USER@HOST --domain DOMAIN
ship host status [--host HOST]
```

All commands output JSON unless `--pretty` is set.

## Output Schema

### Success Response

```typescript
{
  status: "ok",
  name: string,
  url: string,
  type: "static" | "docker" | "binary",
  took_ms: number,
  health?: {
    endpoint: string,
    status: number,
    latency_ms: number
  },
  expires?: string  // ISO 8601, only if --ttl set
}
```

### Error Response

```typescript
{
  status: "error",
  code: string,
  message: string,
  name?: string,
  url?: string
}
```

### Exit Codes

| Code | Meaning |
|------|---------|
| 0 | Success |
| 1 | Deploy failed |
| 2 | Invalid arguments |
| 3 | SSH connection failed |
| 4 | Health check failed |

## Error Codes

| Code | Description |
|------|-------------|
| `INVALID_PATH` | Path doesn't exist or isn't readable |
| `UNKNOWN_PROJECT_TYPE` | Can't detect how to deploy |
| `SSH_CONNECT_FAILED` | Can't establish SSH connection |
| `SSH_AUTH_FAILED` | SSH key rejected |
| `UPLOAD_FAILED` | SCP/rsync failed |
| `BUILD_FAILED` | Docker build or compilation failed |
| `SERVICE_FAILED` | systemd unit failed to start |
| `CADDY_FAILED` | Caddy reload failed |
| `HEALTH_CHECK_FAILED` | App didn't respond in time |
| `HEALTH_CHECK_TIMEOUT` | Health check timed out |
| `NOT_FOUND` | Named deploy doesn't exist |
| `CONFLICT` | Name already in use (if --no-update) |
| `HOST_NOT_CONFIGURED` | No default host, --host required |
| `INVALID_TTL` | Can't parse TTL duration |

## Auto-Detection Logic

When given a path, ship determines deploy type:

```
is_file(path)?
  → is_executable(path)?
    → BINARY
    → ERROR: "Not an executable file"
  
is_directory(path)?
  → has_file("Dockerfile")?
    → DOCKER
  → has_file("index.html") OR has_file("index.htm")?
    → STATIC
  → has_file("go.mod") AND NOT has_file("Dockerfile")?
    → ERROR: "Go project without Dockerfile. Add a Dockerfile or build a binary."
  → has_file("package.json") AND NOT has_file("Dockerfile")?
    → ERROR: "Node project without Dockerfile. Add a Dockerfile."
  → is_empty(path)?
    → ERROR: "Directory is empty"
  → ERROR: "Can't detect project type. Add a Dockerfile or index.html."
```

## Name Generation

If `--name` not provided:

```
name = "ship-" + random_alphanumeric(6)
```

Names must match: `^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$`

Invalid names return `INVALID_NAME` error.

## Port Allocation

Ports are allocated server-side.

### Server Files

```
/etc/ship/ports/<name>     # Contains allocated port number
```

### Allocation Algorithm

```python
def allocate_port(name):
    port_file = f"/etc/ship/ports/{name}"
    
    if exists(port_file):
        return int(read(port_file))
    
    used_ports = [int(read(f)) for f in glob("/etc/ship/ports/*")]
    next_port = 9000
    while next_port in used_ports:
        next_port += 1
    
    write(port_file, str(next_port))
    return next_port
```

### Port Range

- Start: 9000
- Max: 9999
- Error if exhausted: `PORT_EXHAUSTED`

## Server File Layout

```
/etc/ship/
  ports/<name>              # Port allocation (contains port number)
  env/<name>.env            # Environment variables
  ttl/<name>                # TTL expiry timestamp (Unix epoch)

/var/www/<name>/            # Static site files

/var/lib/<name>/
  src/                      # Docker build context (checked out source)
  data/                     # Persistent data volume

/etc/systemd/system/<name>.service    # systemd unit
/etc/caddy/sites-enabled/<name>.caddy # Caddy config
```

## Deploy Flows

### Static Site

1. Validate path is directory with index.html
2. Generate name if needed
3. SSH: Create `/var/www/<name>/`
4. rsync: Upload directory contents to `/var/www/<name>/`
5. SSH: Set ownership to `www-data`
6. Generate Caddyfile
7. SSH: Write to `/etc/caddy/sites-enabled/<name>.caddy`
8. SSH: Reload Caddy
9. Health check: `GET https://<name>.<domain>/`
10. If TTL: Write expiry to `/etc/ship/ttl/<name>`
11. Return JSON

### Docker

1. Validate path is directory with Dockerfile
2. Generate name if needed
3. Allocate port (server-side)
4. rsync: Upload directory to `/var/lib/<name>/src/`
5. SSH: `docker build -t <name> /var/lib/<name>/src/`
6. Generate systemd unit (runs `docker run`)
7. SSH: Write to `/etc/systemd/system/<name>.service`
8. SSH: Write env file to `/etc/ship/env/<name>.env`
9. SSH: `systemctl daemon-reload && systemctl restart <name>`
10. Generate Caddyfile (reverse proxy to port)
11. SSH: Write to `/etc/caddy/sites-enabled/<name>.caddy`
12. SSH: Reload Caddy
13. Health check: `GET https://<name>.<domain><health_endpoint>`
14. If TTL: Write expiry to `/etc/ship/ttl/<name>`
15. Return JSON

### Binary

1. Validate path is executable file
2. Generate name if needed
3. Allocate port (server-side)
4. SCP: Upload binary to `/usr/local/bin/<name>`
5. SSH: `chmod +x /usr/local/bin/<name>`
6. Generate systemd unit
7. SSH: Write to `/etc/systemd/system/<name>.service`
8. SSH: Write env file to `/etc/ship/env/<name>.env`
9. SSH: `systemctl daemon-reload && systemctl restart <name>`
10. Generate Caddyfile (reverse proxy to port)
11. SSH: Write to `/etc/caddy/sites-enabled/<name>.caddy`
12. SSH: Reload Caddy
13. Health check: `GET https://<name>.<domain><health_endpoint>`
14. If TTL: Write expiry to `/etc/ship/ttl/<name>`
15. Return JSON

## Health Checks

### Behavior

After deploy, verify the app is responding:

1. Wait 2 seconds (let app start)
2. `GET https://<name>.<domain><health_endpoint>`
3. If 2xx or 3xx: success
4. If error or timeout: retry after 2s
5. Max retries: 15 (total 30s)
6. If all retries fail: return `HEALTH_CHECK_FAILED`

### Health Endpoint

| Deploy Type | Default | Override |
|-------------|---------|----------|
| static | `/` | `--health` |
| docker | none (skip) | `--health` |
| binary | none (skip) | `--health` |

When no health endpoint: skip health check, return success after service starts.

## TTL (Auto-Expiry)

### Setting TTL

```bash
ship ./site --name preview --ttl 24h
```

Supported formats: `30m`, `1h`, `24h`, `7d`

### Server-Side Cleanup

A systemd timer runs hourly:

```ini
# /etc/systemd/system/ship-cleanup.timer
[Timer]
OnCalendar=hourly
Persistent=true

# /etc/systemd/system/ship-cleanup.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/ship-cleanup
```

Cleanup script:

```bash
#!/bin/bash
now=$(date +%s)
for f in /etc/ship/ttl/*; do
  name=$(basename "$f")
  expires=$(cat "$f")
  if [ "$now" -gt "$expires" ]; then
    ship remove "$name"
  fi
done
```

### TTL File Format

```
/etc/ship/ttl/<name>
```

Contents: Unix timestamp (seconds since epoch)

## Templates

### systemd Unit (Docker)

```ini
[Unit]
Description=ship: {{.Name}}
After=docker.service
Requires=docker.service

[Service]
Restart=always
RestartSec=5
EnvironmentFile=/etc/ship/env/{{.Name}}.env
ExecStartPre=-/usr/bin/docker stop {{.Name}}
ExecStartPre=-/usr/bin/docker rm {{.Name}}
ExecStart=/usr/bin/docker run --rm --name {{.Name}} \
  --env-file /etc/ship/env/{{.Name}}.env \
  -p 127.0.0.1:{{.Port}}:{{.Port}} \
  -v /var/lib/{{.Name}}/data:/data \
  {{.Name}}
ExecStop=/usr/bin/docker stop {{.Name}}

[Install]
WantedBy=multi-user.target
```

### systemd Unit (Binary)

```ini
[Unit]
Description=ship: {{.Name}}
After=network.target

[Service]
Type=simple
Restart=always
RestartSec=5
User={{.Name}}
EnvironmentFile=/etc/ship/env/{{.Name}}.env
ExecStart=/usr/local/bin/{{.Name}}
WorkingDirectory=/var/lib/{{.Name}}

[Install]
WantedBy=multi-user.target
```

### Caddyfile (Reverse Proxy)

```
{{.Domain}} {
    reverse_proxy localhost:{{.Port}}
}
```

### Caddyfile (Static)

```
{{.Domain}} {
    root * /var/www/{{.Name}}
    file_server
    encode gzip
}
```

## Environment Variables

Always set by ship:

| Variable | Value |
|----------|-------|
| `PORT` | Allocated port number |
| `SHIP_NAME` | Deploy name |
| `SHIP_URL` | Full URL |

User variables from `--env` or `--env-file` are merged.

## Host Initialization

```bash
ship host init user@vps.example.com --domain example.com
```

### Steps

1. SSH: Check connectivity
2. SSH: Install packages (`apt install -y caddy docker.io`)
3. SSH: Enable services (`systemctl enable --now caddy docker`)
4. SSH: Create directories (`/etc/ship/ports`, `/etc/ship/env`, `/etc/ship/ttl`)
5. SSH: Install cleanup timer
6. SSH: Configure Caddy base
7. Update local state with host info

### Output

```json
{
  "status": "ok",
  "host": "vps.example.com",
  "domain": "example.com",
  "installed": ["caddy", "docker"]
}
```

## Local State

Stored at `~/.config/ship/state.json`:

```json
{
  "default_host": "vps",
  "hosts": {
    "vps": {
      "ssh": "user@vps.example.com",
      "domain": "example.com"
    }
  }
}
```

Local state is minimal — server is source of truth for ports, deploys, TTLs.

## Pretty Output

When `--pretty` is set (or `SHIP_PRETTY=1`):

### Deploy Success

```
✓ Deployed to https://myapp.example.com (4.2s)
```

### Deploy Failure

```
✗ Deploy failed: health check timed out after 30s
```

### List

```
NAME          URL                              TYPE    STATUS
api           https://api.example.com          docker  running
preview-x7k   https://preview-x7k.example.com  static  running (expires in 23h)
```

## Implementation Notes

### SSH Execution

Use a single SSH connection with multiplexing:

```bash
ssh -o ControlMaster=auto -o ControlPath=/tmp/ship-%r@%h:%p -o ControlPersist=60 ...
```

### Concurrency

Ship is not designed for concurrent deploys of the same name. Behavior is undefined.

Different names can deploy concurrently.

### Rollback (Future)

Not in v2.0. Future consideration: keep last N versions, `ship rollback <name>`.

---

## Appendix: Example Session

```bash
$ mkdir -p /tmp/hello && echo '<h1>Hello</h1>' > /tmp/hello/index.html

$ ship /tmp/hello --name hello
{"status":"ok","name":"hello","url":"https://hello.example.com","type":"static","took_ms":3200,"health":{"endpoint":"/","status":200,"latency_ms":45}}

$ ship list
{"status":"ok","deploys":[{"name":"hello","url":"https://hello.example.com","type":"static","running":true}]}

$ ship remove hello
{"status":"ok","name":"hello","removed":true}
```