|
1 | 1 | import { ApiClient, requireProject } from '../api.js' |
2 | 2 | import { info, printJson, handleApproval } from '../util.js' |
| 3 | +import { resolveComputeServiceId } from './services.js' |
3 | 4 |
|
4 | 5 | type Opts = { branch?: string; group?: string; json?: boolean } |
5 | 6 |
|
@@ -41,3 +42,32 @@ function printDomain(r: any, json?: boolean): void { |
41 | 42 | } |
42 | 43 | if (!r.configured) info(' once DNS propagates, Fly issues the cert — re-check with `insta compute check-domain`') |
43 | 44 | } |
| 45 | + |
| 46 | +// ---- lifecycle (start/stop/suspend/status) ---- |
| 47 | + |
| 48 | +type LifeOpts = { json?: boolean } |
| 49 | + |
| 50 | +async function lifecycle(verb: 'start' | 'stop' | 'suspend', serviceName: string | undefined, opts: LifeOpts): Promise<void> { |
| 51 | + const api = await ApiClient.load() |
| 52 | + const p = await requireProject() |
| 53 | + const { services } = await api.request('GET', `/projects/${p.projectId}/services`) |
| 54 | + const id = resolveComputeServiceId(services, serviceName) |
| 55 | + const res = await api.rawRequest('POST', `/projects/${p.projectId}/services/${id}/${verb}`) |
| 56 | + if (handleApproval(res)) return |
| 57 | + if (opts.json) return printJson(res.body) |
| 58 | + info(`compute ${res.body.service?.name ?? id}: ${verb} → desired=${res.body.service?.desired_state} (live: ${res.body.state})`) |
| 59 | +} |
| 60 | + |
| 61 | +export const computeStart = (service: string | undefined, opts: LifeOpts) => lifecycle('start', service, opts) |
| 62 | +export const computeStop = (service: string | undefined, opts: LifeOpts) => lifecycle('stop', service, opts) |
| 63 | +export const computeSuspend = (service: string | undefined, opts: LifeOpts) => lifecycle('suspend', service, opts) |
| 64 | + |
| 65 | +export async function computeStatus(serviceName: string | undefined, opts: LifeOpts): Promise<void> { |
| 66 | + const api = await ApiClient.load() |
| 67 | + const p = await requireProject() |
| 68 | + const { services } = await api.request('GET', `/projects/${p.projectId}/services`) |
| 69 | + const id = resolveComputeServiceId(services, serviceName) |
| 70 | + const r = await api.request('GET', `/projects/${p.projectId}/services/${id}/state`) |
| 71 | + if (opts.json) return printJson(r) |
| 72 | + info(`compute ${serviceName ?? id}: desired=${r.desiredState} live=${r.state}`) |
| 73 | +} |
0 commit comments