|
| 1 | +/** |
| 2 | + * The per-stack state a compute driver records on the machine running deploys. |
| 3 | + * |
| 4 | + * Every SSH-style driver has to remember which box a stack lives on: Hetzner |
| 5 | + * pins a server id so CI can find the existing box instead of provisioning a |
| 6 | + * duplicate, and the ssh driver pins the host it adopted plus what it did to |
| 7 | + * it. One file per stack, one reader and one writer, so a driver, the |
| 8 | + * dashboard and the CLI cannot disagree about where that file is. |
| 9 | + * |
| 10 | + * ## Where the file lives |
| 11 | + * |
| 12 | + * Driver state predates the configurable state directory and has a legacy |
| 13 | + * home of its own, `storage/cloud/state/` (the Stacks storage convention), |
| 14 | + * which is meant to be COMMITTED: unlike dashboard credentials it holds |
| 15 | + * nothing secret, and a checkout without it re-provisions. A project that |
| 16 | + * configured `stateDir` (or set `TS_CLOUD_STATE_DIR`) keeps driver state |
| 17 | + * under it instead, in `<stateDir>/state/`. A Stacks application sets |
| 18 | + * `stateDir: 'storage/cloud'`, so for it the two spellings name the same |
| 19 | + * directory and nothing moves. |
| 20 | + */ |
| 21 | +import type { HetznerDriverState } from '../hetzner/state' |
| 22 | +import { mkdir, readFile, rename, writeFile } from 'node:fs/promises' |
| 23 | +import { join } from 'node:path' |
| 24 | +import { isStateDirConfigured, resolveStatePath } from '@ts-cloud/core' |
| 25 | + |
| 26 | +/** The committed driver-state directory used when no state directory is configured. */ |
| 27 | +export const LEGACY_DRIVER_STATE_DIR = 'storage/cloud/state' |
| 28 | + |
| 29 | +/** What the ssh driver remembers about a host it adopted. */ |
| 30 | +export interface SshDriverState { |
| 31 | + provider: 'ssh' |
| 32 | + stackName: string |
| 33 | + /** The hostname or address deploys connect to, exactly as configured. */ |
| 34 | + host: string |
| 35 | + sshUser: string |
| 36 | + sshPort: number |
| 37 | + /** `SHA256:...` of the pinned host key, when the host-key policy pins. */ |
| 38 | + hostKeyFingerprint?: string |
| 39 | + /** The address DNS should point at, when known (configured, or detected). */ |
| 40 | + publicIp?: string |
| 41 | + /** The host's first LAN address, as reported by the preflight. */ |
| 42 | + lanIp?: string |
| 43 | + deployStoragePath?: string |
| 44 | + profile?: 'raspberry-pi' | 'generic' |
| 45 | + /** The bootstrap recipe version last applied to the host. */ |
| 46 | + bootstrapVersion?: number |
| 47 | + /** When that bootstrap ran, ISO 8601. */ |
| 48 | + bootstrappedAt?: string |
| 49 | +} |
| 50 | + |
| 51 | +export type DriverState = HetznerDriverState | SshDriverState |
| 52 | + |
| 53 | +/** |
| 54 | + * The directory driver state files live in, absolute. |
| 55 | + * |
| 56 | + * `<stateDir>/state` when a state directory is configured, the legacy |
| 57 | + * `storage/cloud/state` otherwise. See the module comment for why the legacy |
| 58 | + * path is not simply `.ts-cloud/state`. |
| 59 | + */ |
| 60 | +export function driverStateDir(cwd: string = process.cwd()): string { |
| 61 | + return isStateDirConfigured() ? resolveStatePath(cwd, 'state') : join(cwd, LEGACY_DRIVER_STATE_DIR) |
| 62 | +} |
| 63 | + |
| 64 | +/** Absolute path of the state file for `stackName`. */ |
| 65 | +export function driverStatePath(stackName: string, cwd: string = process.cwd()): string { |
| 66 | + return join(driverStateDir(cwd), `${stackName}.json`) |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Read the state file for `stackName`, or null when there is none (or it is |
| 71 | + * unreadable: a corrupt file is treated as absent, and the atomic write below |
| 72 | + * is what keeps that from ever losing a pin). |
| 73 | + */ |
| 74 | +export async function readDriverState<T extends DriverState = DriverState>( |
| 75 | + stackName: string, |
| 76 | + cwd: string = process.cwd(), |
| 77 | +): Promise<T | null> { |
| 78 | + try { |
| 79 | + const raw = await readFile(driverStatePath(stackName, cwd), 'utf8') |
| 80 | + return JSON.parse(raw) as T |
| 81 | + } catch { |
| 82 | + return null |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Replace the state file for `stackName` atomically: a crash mid-write would |
| 88 | + * corrupt the JSON, and the reader's catch-all would then lose the pinned box |
| 89 | + * (silently re-provisioning a duplicate). Temp file + rename on the same |
| 90 | + * filesystem is atomic. |
| 91 | + */ |
| 92 | +export async function writeDriverState(stackName: string, state: DriverState, cwd: string = process.cwd()): Promise<void> { |
| 93 | + const path = driverStatePath(stackName, cwd) |
| 94 | + await mkdir(driverStateDir(cwd), { recursive: true }) |
| 95 | + const tmp = `${path}.${process.pid}.tmp` |
| 96 | + await writeFile(tmp, `${JSON.stringify(state, null, 2)}\n`, 'utf8') |
| 97 | + await rename(tmp, path) |
| 98 | +} |
0 commit comments