my-durable 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +7 -0
- package/LICENSE +21 -0
- package/README.md +328 -2
- package/ai_docs/durable_pi/announcement.md +860 -0
- package/ai_docs/durable_pi/changelog.md +51 -0
- package/ai_docs/durable_pi/chord-delta-findings.md +362 -0
- package/ai_docs/durable_pi/coding-agent-integration.md +70 -0
- package/ai_docs/durable_pi/pico-v5-chord-usage.md +391 -0
- package/ai_docs/durable_pi/pico-v5-handoff.md +921 -0
- package/ai_docs/durable_pi/readme.md +617 -0
- package/ai_docs/durable_pi/sources.md +22 -0
- package/ai_docs/durable_pi/spec.md +4696 -0
- package/ai_docs/durable_pi/vacation-planner.md +57 -0
- package/ai_docs/pi/chord.md +268 -0
- package/ai_docs/pi/cli-integration.md +110 -0
- package/ai_docs/pi/cli.md +325 -0
- package/ai_docs/pi/codemode.md +211 -0
- package/ai_docs/pi/compaction.md +467 -0
- package/ai_docs/pi/configuration.md +51 -0
- package/ai_docs/pi/containerization.md +187 -0
- package/ai_docs/pi/custom-provider.md +177 -0
- package/ai_docs/pi/environment-variables.md +102 -0
- package/ai_docs/pi/extensions.md +278 -0
- package/ai_docs/pi/how-pi-works.md +53 -0
- package/ai_docs/pi/index.md +43 -0
- package/ai_docs/pi/json.md +230 -0
- package/ai_docs/pi/keybindings.md +198 -0
- package/ai_docs/pi/llama-cpp.md +118 -0
- package/ai_docs/pi/mcp.md +274 -0
- package/ai_docs/pi/message-types.md +265 -0
- package/ai_docs/pi/models.md +209 -0
- package/ai_docs/pi/overview.md +166 -0
- package/ai_docs/pi/packages.md +133 -0
- package/ai_docs/pi/pi-agent-core.md +568 -0
- package/ai_docs/pi/pi-ai.md +1942 -0
- package/ai_docs/pi/prompt-templates.md +63 -0
- package/ai_docs/pi/providers.md +204 -0
- package/ai_docs/pi/quickstart.md +140 -0
- package/ai_docs/pi/readme.md +99 -0
- package/ai_docs/pi/rpc-commands.md +864 -0
- package/ai_docs/pi/rpc-extension-ui.md +204 -0
- package/ai_docs/pi/rpc.md +197 -0
- package/ai_docs/pi/sdk.md +151 -0
- package/ai_docs/pi/security.md +103 -0
- package/ai_docs/pi/session-format.md +297 -0
- package/ai_docs/pi/sessions.md +70 -0
- package/ai_docs/pi/settings.md +173 -0
- package/ai_docs/pi/shell-aliases.md +97 -0
- package/ai_docs/pi/skills.md +97 -0
- package/ai_docs/pi/slash-commands.md +64 -0
- package/ai_docs/pi/sources.md +57 -0
- package/ai_docs/pi/terminal-setup.md +223 -0
- package/ai_docs/pi/termux.md +121 -0
- package/ai_docs/pi/themes.md +140 -0
- package/ai_docs/pi/tmux.md +59 -0
- package/ai_docs/pi/toolkit.md +168 -0
- package/ai_docs/pi/tui.md +129 -0
- package/ai_docs/pi/usage.md +98 -0
- package/ai_docs/pi/virtual-models.md +118 -0
- package/ai_docs/pi/windows.md +71 -0
- package/apps/dpi-backend/.nvmrc +1 -0
- package/apps/dpi-backend/API.md +99 -0
- package/apps/dpi-backend/README.md +119 -0
- package/apps/dpi-backend/deploy/backend.env.example +9 -0
- package/apps/dpi-backend/deploy/dpi-backend.plist +26 -0
- package/apps/dpi-backend/deploy/dpi-backend.service +22 -0
- package/apps/dpi-backend/package.json +33 -0
- package/apps/dpi-backend/prompts/one_sentence.md +3 -0
- package/apps/dpi-backend/prompts/smart_ass_opus_fix.md +169 -0
- package/apps/dpi-backend/scripts/check-contract.mjs +151 -0
- package/apps/dpi-backend/scripts/migrate-playground.ts +66 -0
- package/apps/dpi-backend/scripts/record-nested-fixture.mts +154 -0
- package/apps/dpi-backend/scripts/token.ts +57 -0
- package/apps/dpi-backend/src/addons.ts +139 -0
- package/apps/dpi-backend/src/auth.ts +20 -0
- package/apps/dpi-backend/src/config.ts +54 -0
- package/apps/dpi-backend/src/contract-check.ts +118 -0
- package/apps/dpi-backend/src/errors.ts +50 -0
- package/apps/dpi-backend/src/extensions.ts +510 -0
- package/apps/dpi-backend/src/faux.ts +80 -0
- package/apps/dpi-backend/src/history.ts +165 -0
- package/apps/dpi-backend/src/idempotency.ts +20 -0
- package/apps/dpi-backend/src/jev.ts +43 -0
- package/apps/dpi-backend/src/jobs.ts +790 -0
- package/apps/dpi-backend/src/main.ts +76 -0
- package/apps/dpi-backend/src/model-catalog.ts +180 -0
- package/apps/dpi-backend/src/pairing-files.ts +142 -0
- package/apps/dpi-backend/src/pairing.ts +459 -0
- package/apps/dpi-backend/src/routes.ts +217 -0
- package/apps/dpi-backend/src/shared-state.ts +85 -0
- package/apps/dpi-backend/src/store.ts +1240 -0
- package/apps/dpi-backend/src/streams.ts +256 -0
- package/apps/dpi-backend/src/tunnel.ts +955 -0
- package/apps/dpi-backend/tsconfig.json +13 -0
- package/apps/dpi-playground/README.md +83 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-CFGc7lHT.js +77 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-DFdKpwm_.css +1 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/anthropic.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/deepseek.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/durable-pi.svg +14 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/gemini.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/grok.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/kimi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/lab.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/meta.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/minimax.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/openai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/qwen.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/xiaomi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/zai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/index.html +16 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/index.html +15 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/package.json +5 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/anthropic.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/deepseek.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/durable-pi.svg +14 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/gemini.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/grok.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/kimi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/lab.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/meta.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/minimax.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/openai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/qwen.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/xiaomi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/zai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/App.vue +180 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/commands.ts +119 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/composables.ts +129 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/hub.ts +40 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/icon-path.ts +7 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/ids.ts +2 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/jobs.ts +80 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/link-policy.ts +16 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/markdown-policy.ts +115 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/markdown.ts +60 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/pair-close.ts +23 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/pair-handoff.ts +56 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/pair.ts +166 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/qrcode.ts +323 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/signature.ts +146 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/store.ts +373 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/stream.ts +183 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/AgentView.vue +342 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Composer.vue +198 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/ContextBar.vue +254 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/DeviceCredentialsDialog.vue +40 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/DeviceIcon.vue +56 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/DirBrowser.vue +95 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/ExtensionsPanel.vue +96 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/HelpDialog.vue +69 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Inspector.vue +425 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/JobCard.vue +221 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/MobileNav.vue +39 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/ModelPicker.vue +71 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/NetworkSidebar.vue +236 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/NewSessionDialog.vue +114 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/OfflinePanel.vue +128 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/PairDeviceWizard.vue +433 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/PromptDialog.vue +89 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/StateEditor.vue +179 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TasksPanel.vue +280 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Toast.vue +11 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TokenManager.vue +175 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TopBar.vue +114 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TranscriptItem.vue +152 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TreeRow.vue +195 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TunnelStatusBar.vue +107 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/extensions.css +105 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/jobs.css +655 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/main.ts +22 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/network.css +675 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/style.css +3203 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/tsconfig.json +16 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/vite.config.ts +14 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/package.json +5 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/audit.ts +406 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/client-ip.ts +72 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/credentials.ts +265 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/guard.ts +43 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/http-errors.ts +156 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/identity.ts +60 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/limiter.ts +165 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/main.ts +460 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/monitor.ts +320 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/mux.ts +277 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/pairing.ts +816 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/proxy.ts +128 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/rotation.ts +583 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/seed.ts +11 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/transport.ts +108 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel-token.ts +11 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel-transport.ts +560 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel.ts +873 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/tsconfig.json +13 -0
- package/apps/dpi-playground/package.json +37 -0
- package/apps/dpi-sdk/README.md +74 -0
- package/apps/dpi-sdk/package.json +20 -0
- package/apps/dpi-sdk/scripts/net.ts +73 -0
- package/apps/dpi-sdk/src/api.ts +631 -0
- package/apps/dpi-sdk/src/app-state.ts +111 -0
- package/apps/dpi-sdk/src/client.ts +330 -0
- package/apps/dpi-sdk/src/errors.ts +16 -0
- package/apps/dpi-sdk/src/jobs.ts +99 -0
- package/apps/dpi-sdk/src/models.ts +472 -0
- package/apps/dpi-sdk/src/node/chunk.ts +488 -0
- package/apps/dpi-sdk/src/node/config.ts +175 -0
- package/apps/dpi-sdk/src/node/drivers.ts +402 -0
- package/apps/dpi-sdk/src/node/ops.ts +114 -0
- package/apps/dpi-sdk/src/node/private-dir.ts +48 -0
- package/apps/dpi-sdk/src/node/registry-migration.ts +549 -0
- package/apps/dpi-sdk/src/node/registry.ts +390 -0
- package/apps/dpi-sdk/src/node/sse-writer.ts +151 -0
- package/apps/dpi-sdk/src/protocol/backoff.ts +31 -0
- package/apps/dpi-sdk/src/protocol/limits.ts +199 -0
- package/apps/dpi-sdk/src/protocol/pairing.ts +311 -0
- package/apps/dpi-sdk/src/protocol/rest-routes.ts +105 -0
- package/apps/dpi-sdk/src/protocol/tunnel.ts +331 -0
- package/apps/dpi-sdk/src/sse.ts +117 -0
- package/apps/dpi-sdk/src/transcript.ts +291 -0
- package/apps/dpi-sdk/tsconfig.json +13 -0
- package/apps/dpi-sdk/w +20 -0
- package/apps/dpi-tui/README.md +54 -0
- package/apps/dpi-tui/bin/dpi-tui.mjs +5 -0
- package/apps/dpi-tui/package.json +20 -0
- package/apps/dpi-tui/src/app.ts +433 -0
- package/apps/dpi-tui/src/commands.ts +151 -0
- package/apps/dpi-tui/src/main.ts +122 -0
- package/apps/dpi-tui/src/pairing.ts +287 -0
- package/apps/dpi-tui/src/pickers.ts +238 -0
- package/apps/dpi-tui/src/render.ts +164 -0
- package/apps/dpi-tui/src/theme.ts +57 -0
- package/apps/dpi-tui/tsconfig.json +14 -0
- package/bin/device.mjs +5 -0
- package/bin/hub.mjs +5 -0
- package/bin/tui.mjs +5 -0
- package/deploy/Caddyfile +120 -0
- package/docs/verification.md +102 -0
- package/dpi-network.example.yaml +62 -0
- package/images/01_durable_agent_network.svg +185 -0
- package/images/02_pi_made_durable.svg +204 -0
- package/images/03_restart_mid_tool.svg +168 -0
- package/images/07_one_api_every_client.svg +123 -0
- package/images/08_device_session_agent.svg +123 -0
- package/images/09_compact_keep_history.svg +143 -0
- package/images/10_shared_app_state.svg +177 -0
- package/images/11_durable_jobs_wait.svg +141 -0
- package/images/12_tools_change_mid_run.svg +231 -0
- package/images/13_hook_blocks_tool.svg +122 -0
- package/images/14_fork_at_any_entry.svg +119 -0
- package/images/15_web_client.png +0 -0
- package/images/16_terminal_client.svg +43 -0
- package/images/17_devices_are_data.svg +182 -0
- package/images/18_should_every_agent_be_durable.svg +118 -0
- package/images/19_sqlite_on_every_device.svg +155 -0
- package/images/20_admission_and_effects.svg +134 -0
- package/justfile +33 -0
- package/package.json +41 -4
- package/scripts/verify.mjs +210 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// One driver per device kind. Drivers know kinds and services, never machines:
|
|
2
|
+
// every machine-specific fact comes from dpi-network.yaml.
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { createInterface } from "node:readline/promises";
|
|
7
|
+
import type { DeviceKind } from "../api.js";
|
|
8
|
+
import { DpiClient } from "../client.js";
|
|
9
|
+
import { syncConfigToRegistry, type NetDevice, type NetworkConfig } from "./config.js";
|
|
10
|
+
import { NetworkRegistry } from "./registry.js";
|
|
11
|
+
import { exeDev, q, remote, rsync, run, shPath, sleep } from "./ops.js";
|
|
12
|
+
|
|
13
|
+
export interface DriverContext {
|
|
14
|
+
config: NetworkConfig;
|
|
15
|
+
registry: NetworkRegistry;
|
|
16
|
+
args: string[];
|
|
17
|
+
log: (line: string) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type Verb =
|
|
21
|
+
| "up"
|
|
22
|
+
| "create"
|
|
23
|
+
| "provision"
|
|
24
|
+
| "deploy"
|
|
25
|
+
| "env"
|
|
26
|
+
| "service"
|
|
27
|
+
| "expose"
|
|
28
|
+
| "register"
|
|
29
|
+
| "restart"
|
|
30
|
+
| "down"
|
|
31
|
+
| "logs"
|
|
32
|
+
| "destroy"
|
|
33
|
+
| "rotate";
|
|
34
|
+
|
|
35
|
+
export type Driver = Partial<Record<Verb, (device: NetDevice, ctx: DriverContext) => Promise<void>>>;
|
|
36
|
+
|
|
37
|
+
export const SERVICE_LABEL = "dev.dpi.backend";
|
|
38
|
+
export const SYSTEMD_UNIT = "dpi-backend";
|
|
39
|
+
const NVM_INSTALL = "https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh";
|
|
40
|
+
|
|
41
|
+
/** Run a script locally (kind local) or on the device over ssh. */
|
|
42
|
+
function exec(device: NetDevice, script: string, options: { echo?: boolean; timeoutMs?: number } = {}) {
|
|
43
|
+
if (device.kind === "local")
|
|
44
|
+
return run("sh", ["-s"], {
|
|
45
|
+
...options,
|
|
46
|
+
stdin: `set -e\nexport NVM_DIR="$HOME/.nvm"\n[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" >/dev/null 2>&1 || true\n${script}`,
|
|
47
|
+
});
|
|
48
|
+
return remote(device, script, options);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function localEnv(config: NetworkConfig): Record<string, string> {
|
|
52
|
+
const file = join(config.root, ".env");
|
|
53
|
+
const out: Record<string, string> = {};
|
|
54
|
+
if (!existsSync(file)) return out;
|
|
55
|
+
for (const line of readFileSync(file, "utf8").split("\n")) {
|
|
56
|
+
const m = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/);
|
|
57
|
+
if (!m) continue;
|
|
58
|
+
out[m[1]] = m[2].replace(/^(['"])(.*)\1$/, "$2");
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ------------------------------------------------------------------ shared steps
|
|
64
|
+
|
|
65
|
+
async function facts(device: NetDevice) {
|
|
66
|
+
const out = (
|
|
67
|
+
await exec(device, `echo "HOME=$HOME"; echo "USER=$(id -un)"; echo "NODE=$(nvm which ${device.node} 2>/dev/null || command -v node || true)"`)
|
|
68
|
+
).stdout;
|
|
69
|
+
const get = (k: string) => out.match(new RegExp(`^${k}=(.*)$`, "m"))?.[1]?.trim() ?? "";
|
|
70
|
+
return { home: get("HOME"), user: get("USER"), node: get("NODE") };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const abs = (p: string, home: string) => (p === "~" ? home : p.startsWith("~/") ? `${home}/${p.slice(2)}` : p);
|
|
74
|
+
|
|
75
|
+
async function provision(device: NetDevice, ctx: DriverContext) {
|
|
76
|
+
ctx.log(`provision ${device.id}: nvm + node ${device.node}`);
|
|
77
|
+
const apt =
|
|
78
|
+
device.kind === "exe-dev"
|
|
79
|
+
? `if command -v apt-get >/dev/null; then sudo -n apt-get update -qq >/dev/null && sudo -n DEBIAN_FRONTEND=noninteractive apt-get install -y -qq git rsync curl build-essential ripgrep >/dev/null; fi\n`
|
|
80
|
+
: "";
|
|
81
|
+
const result = await exec(
|
|
82
|
+
device,
|
|
83
|
+
`${apt}if [ ! -s "$NVM_DIR/nvm.sh" ]; then curl -fsSL ${NVM_INSTALL} | PROFILE=/dev/null bash >/dev/null; fi
|
|
84
|
+
. "$NVM_DIR/nvm.sh"
|
|
85
|
+
nvm install ${device.node} >/dev/null 2>&1
|
|
86
|
+
echo "node: $(nvm which ${device.node})"`,
|
|
87
|
+
{ timeoutMs: 900_000 },
|
|
88
|
+
);
|
|
89
|
+
ctx.log(result.stdout.trim());
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function deploy(device: NetDevice, ctx: DriverContext) {
|
|
93
|
+
const code = device.codeDir.replace(/^~\//, "");
|
|
94
|
+
ctx.log(`deploy ${device.id}: sync apps/dpi-backend + apps/dpi-sdk → ${device.codeDir}/apps`);
|
|
95
|
+
await exec(
|
|
96
|
+
device,
|
|
97
|
+
`mkdir -p "${shPath(device.codeDir)}/apps" "${shPath(device.codeDir)}/e2e" "${shPath(device.homeDir)}" && chmod 700 "${shPath(device.homeDir)}"`,
|
|
98
|
+
);
|
|
99
|
+
await rsync(device, ["apps/dpi-backend", "apps/dpi-sdk"], `${code}/apps/`, ctx.config.root);
|
|
100
|
+
ctx.log(`deploy ${device.id}: npm ci`);
|
|
101
|
+
await exec(
|
|
102
|
+
device,
|
|
103
|
+
`cd "${shPath(device.codeDir)}/apps/dpi-backend" && nvm exec ${device.node} npm ci --no-audit --no-fund --loglevel=error >/dev/null && echo installed`,
|
|
104
|
+
{ timeoutMs: 900_000 },
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Write <home>/backend.env, keeping an existing token unless rotating. Secrets travel on stdin. */
|
|
109
|
+
async function writeEnv(device: NetDevice, ctx: DriverContext, rotate = false) {
|
|
110
|
+
const local = localEnv(ctx.config);
|
|
111
|
+
const missing = device.envKeys.filter((k) => !(local[k] ?? process.env[k]));
|
|
112
|
+
if (missing.length) ctx.log(`warning: ${missing.join(", ")} not found in ${join(ctx.config.root, ".env")} or the environment`);
|
|
113
|
+
const home = shPath(device.homeDir);
|
|
114
|
+
const body = [
|
|
115
|
+
`DPI_DEVICE_NAME=${device.id}`,
|
|
116
|
+
`DPI_PORT=${device.port}`,
|
|
117
|
+
`DPI_HOST=${device.host}`,
|
|
118
|
+
`DPI_START_DIRS=${device.startDirs.join(":")}`,
|
|
119
|
+
...device.envKeys.flatMap((k) => {
|
|
120
|
+
const v = local[k] ?? process.env[k];
|
|
121
|
+
return v && !/[\n\r]/.test(v) ? [`${k}=${v}`] : [];
|
|
122
|
+
}),
|
|
123
|
+
...Object.entries(device.env).map(([k, v]) => `${k}=${v}`),
|
|
124
|
+
].join("\n");
|
|
125
|
+
const mkStartDirs = device.startDirs.map((r) => `mkdir -p "${shPath(r)}"`).join("\n");
|
|
126
|
+
await exec(
|
|
127
|
+
device,
|
|
128
|
+
`umask 077
|
|
129
|
+
mkdir -p "${home}"
|
|
130
|
+
F="${home}/backend.env"
|
|
131
|
+
TOKEN=$(grep '^DPI_TOKEN=' "$F" 2>/dev/null | head -1 | cut -d= -f2- || true)
|
|
132
|
+
if [ -z "$TOKEN" ] || [ "${rotate ? 1 : 0}" = 1 ]; then TOKEN="dpi_$(head -c 32 /dev/urandom | base64 | tr '+/' '-_' | tr -d '=\\n')"; fi
|
|
133
|
+
printf 'DPI_TOKEN=%s\\n' "$TOKEN" > "$F.tmp"
|
|
134
|
+
cat >> "$F.tmp" <<'DPI_ENV_EOF'
|
|
135
|
+
${body}
|
|
136
|
+
DPI_ENV_EOF
|
|
137
|
+
mv "$F.tmp" "$F"
|
|
138
|
+
chmod 600 "$F"
|
|
139
|
+
${mkStartDirs}
|
|
140
|
+
echo "env ok"`,
|
|
141
|
+
);
|
|
142
|
+
ctx.log(`env ${device.id}: ${device.homeDir}/backend.env written (${rotate ? "token rotated" : "token kept or created"})`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function fill(template: string, values: Record<string, string>) {
|
|
146
|
+
return template.replace(/__([A-Z_]+)__/g, (m, key: string) => {
|
|
147
|
+
if (!(key in values)) throw Error(`Template placeholder ${m} has no value`);
|
|
148
|
+
return values[key];
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function installService(device: NetDevice, ctx: DriverContext) {
|
|
153
|
+
if (!device.service) throw Error(`${device.id} has no service in dpi-network.yaml`);
|
|
154
|
+
const f = await facts(device);
|
|
155
|
+
if (!f.node) throw Error(`${device.id}: node ${device.node} not found; run: just net step provision ${device.id}`);
|
|
156
|
+
const app = device.kind === "local" ? join(ctx.config.root, "apps/dpi-backend") : `${abs(device.codeDir, f.home)}/apps/dpi-backend`;
|
|
157
|
+
const dpiHome = abs(device.homeDir, f.home);
|
|
158
|
+
const nodeDir = f.node.replace(/\/node$/, "");
|
|
159
|
+
const values = {
|
|
160
|
+
LABEL: SERVICE_LABEL,
|
|
161
|
+
UNIT: SYSTEMD_UNIT,
|
|
162
|
+
NODE: f.node,
|
|
163
|
+
APP: app,
|
|
164
|
+
HOME: f.home,
|
|
165
|
+
USER: f.user,
|
|
166
|
+
DPI_HOME: dpiHome,
|
|
167
|
+
LOG: `${dpiHome}/backend.log`,
|
|
168
|
+
PATH: `${nodeDir}:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`,
|
|
169
|
+
};
|
|
170
|
+
const deployDir = join(ctx.config.root, "apps/dpi-backend/deploy");
|
|
171
|
+
if (device.service === "launchd") {
|
|
172
|
+
const plist = fill(readFileSync(join(deployDir, "dpi-backend.plist"), "utf8"), values);
|
|
173
|
+
await exec(
|
|
174
|
+
device,
|
|
175
|
+
`P="$HOME/Library/LaunchAgents/${SERVICE_LABEL}.plist"
|
|
176
|
+
mkdir -p "$HOME/Library/LaunchAgents"
|
|
177
|
+
cat > "$P" <<'DPI_PLIST_EOF'
|
|
178
|
+
${plist}
|
|
179
|
+
DPI_PLIST_EOF
|
|
180
|
+
launchctl bootout gui/$(id -u)/${SERVICE_LABEL} 2>/dev/null || true
|
|
181
|
+
for i in 1 2 3 4 5; do launchctl bootstrap gui/$(id -u) "$P" 2>/dev/null && break; sleep 1; done
|
|
182
|
+
launchctl print gui/$(id -u)/${SERVICE_LABEL} >/dev/null && echo "launchd ok"`,
|
|
183
|
+
);
|
|
184
|
+
} else {
|
|
185
|
+
const unit = fill(readFileSync(join(deployDir, "dpi-backend.service"), "utf8"), values);
|
|
186
|
+
await exec(
|
|
187
|
+
device,
|
|
188
|
+
`sudo -n tee /etc/systemd/system/${SYSTEMD_UNIT}.service >/dev/null <<'DPI_UNIT_EOF'
|
|
189
|
+
${unit}
|
|
190
|
+
DPI_UNIT_EOF
|
|
191
|
+
sudo -n systemctl daemon-reload
|
|
192
|
+
sudo -n systemctl enable ${SYSTEMD_UNIT} >/dev/null 2>&1
|
|
193
|
+
sudo -n systemctl restart ${SYSTEMD_UNIT}
|
|
194
|
+
echo "systemd ok"`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
ctx.log(`service ${device.id}: ${device.service} installed and started`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function serviceCommand(device: NetDevice, action: "restart" | "down") {
|
|
201
|
+
if (device.service === "launchd")
|
|
202
|
+
return exec(
|
|
203
|
+
device,
|
|
204
|
+
action === "restart"
|
|
205
|
+
? `launchctl kickstart -k gui/$(id -u)/${SERVICE_LABEL}`
|
|
206
|
+
: `launchctl bootout gui/$(id -u)/${SERVICE_LABEL} 2>/dev/null || true`,
|
|
207
|
+
);
|
|
208
|
+
if (device.service === "systemd")
|
|
209
|
+
return exec(device, `sudo -n systemctl ${action === "restart" ? "restart" : "stop"} ${SYSTEMD_UNIT}`);
|
|
210
|
+
throw Error(`${device.id} has no service; it runs with \`just backend run\``);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function register(device: NetDevice, ctx: DriverContext) {
|
|
214
|
+
syncConfigToRegistry(ctx.config, ctx.registry);
|
|
215
|
+
const token = (await exec(device, `grep '^DPI_TOKEN=' "${shPath(device.homeDir)}/backend.env" | head -1 | cut -d= -f2-`)).stdout.trim();
|
|
216
|
+
if (!token) throw Error(`${device.id}: no DPI_TOKEN in ${device.homeDir}/backend.env; run: just net step env ${device.id}`);
|
|
217
|
+
ctx.registry.setToken(device.id, token);
|
|
218
|
+
ctx.registry.forgetUid(device.id);
|
|
219
|
+
ctx.log(`register ${device.id}: token stored in ${ctx.registry.path}`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export async function waitOnline(device: NetDevice, ctx: DriverContext, timeoutMs = 90_000) {
|
|
223
|
+
const token = ctx.registry.get(device.id)?.token;
|
|
224
|
+
if (!token) throw Error(`${device.id} has no token; run: just net register ${device.id}`);
|
|
225
|
+
const client = new DpiClient(device.url, token);
|
|
226
|
+
const end = Date.now() + timeoutMs;
|
|
227
|
+
let last = "";
|
|
228
|
+
while (Date.now() < end) {
|
|
229
|
+
try {
|
|
230
|
+
const info = await client.device({ timeoutMs: 5000 });
|
|
231
|
+
ctx.log(`online ${device.id}: ${device.url} (${info.name}, uid ${info.uid}, ${info.models.length} models)`);
|
|
232
|
+
return info;
|
|
233
|
+
} catch (e) {
|
|
234
|
+
last = (e as Error).message;
|
|
235
|
+
await sleep(2000);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
throw Error(`${device.id} did not come online at ${device.url}: ${last}`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function logs(device: NetDevice, ctx: DriverContext) {
|
|
242
|
+
const n = Number(ctx.args[0] ?? 80) || 80;
|
|
243
|
+
const out = await exec(device, `tail -n ${n} "${shPath(device.homeDir)}/backend.log" 2>/dev/null || echo "(no log yet at ${device.homeDir}/backend.log)"`);
|
|
244
|
+
process.stdout.write(out.stdout);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ------------------------------------------------------------------ drivers
|
|
248
|
+
|
|
249
|
+
const sshDriver: Driver = {
|
|
250
|
+
provision,
|
|
251
|
+
deploy,
|
|
252
|
+
env: (d, ctx) => writeEnv(d, ctx),
|
|
253
|
+
rotate: async (d, ctx) => {
|
|
254
|
+
await writeEnv(d, ctx, true);
|
|
255
|
+
await serviceCommand(d, "restart");
|
|
256
|
+
await register(d, ctx);
|
|
257
|
+
},
|
|
258
|
+
service: installService,
|
|
259
|
+
register,
|
|
260
|
+
restart: async (d, ctx) => {
|
|
261
|
+
await serviceCommand(d, "restart");
|
|
262
|
+
ctx.log(`restart ${d.id}: done`);
|
|
263
|
+
},
|
|
264
|
+
down: async (d, ctx) => {
|
|
265
|
+
await serviceCommand(d, "down");
|
|
266
|
+
ctx.log(`down ${d.id}: stopped`);
|
|
267
|
+
},
|
|
268
|
+
logs,
|
|
269
|
+
async up(d, ctx) {
|
|
270
|
+
await provision(d, ctx);
|
|
271
|
+
await deploy(d, ctx);
|
|
272
|
+
await writeEnv(d, ctx);
|
|
273
|
+
await installService(d, ctx);
|
|
274
|
+
await register(d, ctx);
|
|
275
|
+
await waitOnline(d, ctx);
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
async function exeVms(): Promise<string[]> {
|
|
280
|
+
const out = await exeDev<{ vms?: { vm_name?: string; name?: string; dns_name?: string }[] }>(["ls"]);
|
|
281
|
+
return (out.vms ?? []).map((v) => v.vm_name ?? v.name ?? (v.dns_name ?? "").replace(/\.exe\.xyz$/, ""));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function exeCreate(device: NetDevice, ctx: DriverContext) {
|
|
285
|
+
const vm = device.vm!;
|
|
286
|
+
if ((await exeVms()).includes(vm)) {
|
|
287
|
+
ctx.log(`create ${device.id}: VM ${vm} already exists`);
|
|
288
|
+
} else {
|
|
289
|
+
const c = device.create ?? {};
|
|
290
|
+
ctx.log(`create ${device.id}: ssh exe.dev new --name=${vm}`);
|
|
291
|
+
await exeDev(
|
|
292
|
+
[
|
|
293
|
+
"new",
|
|
294
|
+
`--name=${vm}`,
|
|
295
|
+
...(c.cpu ? [`--cpu=${c.cpu}`] : []),
|
|
296
|
+
...(c.memory ? [`--memory=${c.memory}`] : []),
|
|
297
|
+
...(c.disk ? [`--disk=${c.disk}`] : []),
|
|
298
|
+
],
|
|
299
|
+
300_000,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
for (let i = 0; i < 40; i++) {
|
|
303
|
+
const ok = await run("ssh", ["-o", "BatchMode=yes", "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=accept-new", device.ssh!, "echo READY"], {
|
|
304
|
+
allowFail: true,
|
|
305
|
+
timeoutMs: 20_000,
|
|
306
|
+
});
|
|
307
|
+
if (ok.stdout.includes("READY")) return ctx.log(`create ${device.id}: ssh ${device.ssh} ready`);
|
|
308
|
+
await sleep(3000);
|
|
309
|
+
}
|
|
310
|
+
throw Error(`${device.id}: ${device.ssh} never accepted ssh`);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async function exeExpose(device: NetDevice, ctx: DriverContext) {
|
|
314
|
+
await exeDev(["share", "port", device.vm!, String(device.port)]);
|
|
315
|
+
await exeDev(["share", "set-public", device.vm!]);
|
|
316
|
+
ctx.log(`expose ${device.id}: ${device.url} → :${device.port}, public (the bearer token is the gate)`);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async function exeDestroy(device: NetDevice, ctx: DriverContext) {
|
|
320
|
+
const vm = device.vm!;
|
|
321
|
+
if (!(await exeVms()).includes(vm)) return ctx.log(`destroy ${device.id}: VM ${vm} does not exist`);
|
|
322
|
+
if (!ctx.args.includes("--yes")) {
|
|
323
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
324
|
+
const answer = await rl.question(`Destroy exe.dev VM ${vm} and its disk? Type the VM name to confirm: `);
|
|
325
|
+
rl.close();
|
|
326
|
+
if (answer.trim() !== vm) return ctx.log("destroy: cancelled");
|
|
327
|
+
}
|
|
328
|
+
await exeDev(["rm", vm], 120_000);
|
|
329
|
+
ctx.registry.forgetUid(device.id);
|
|
330
|
+
ctx.log(`destroy ${device.id}: VM ${vm} removed`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const exeDevDriver: Driver = {
|
|
334
|
+
...sshDriver,
|
|
335
|
+
create: exeCreate,
|
|
336
|
+
expose: exeExpose,
|
|
337
|
+
destroy: exeDestroy,
|
|
338
|
+
async up(d, ctx) {
|
|
339
|
+
await exeCreate(d, ctx);
|
|
340
|
+
await provision(d, ctx);
|
|
341
|
+
await deploy(d, ctx);
|
|
342
|
+
await writeEnv(d, ctx);
|
|
343
|
+
await installService(d, ctx);
|
|
344
|
+
await exeExpose(d, ctx);
|
|
345
|
+
await register(d, ctx);
|
|
346
|
+
await waitOnline(d, ctx);
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const localDriver: Driver = {
|
|
351
|
+
env: (d, ctx) => writeEnv(d, ctx),
|
|
352
|
+
register,
|
|
353
|
+
logs,
|
|
354
|
+
rotate: async (d, ctx) => {
|
|
355
|
+
await writeEnv(d, ctx, true);
|
|
356
|
+
await register(d, ctx);
|
|
357
|
+
ctx.log("restart the local backend to use the new token");
|
|
358
|
+
},
|
|
359
|
+
service: installService,
|
|
360
|
+
restart: async (d, ctx) => {
|
|
361
|
+
await serviceCommand(d, "restart");
|
|
362
|
+
ctx.log(`restart ${d.id}: done`);
|
|
363
|
+
},
|
|
364
|
+
down: async (d, ctx) => {
|
|
365
|
+
await serviceCommand(d, "down");
|
|
366
|
+
ctx.log(`down ${d.id}: stopped`);
|
|
367
|
+
},
|
|
368
|
+
async up(d, ctx) {
|
|
369
|
+
await writeEnv(d, ctx);
|
|
370
|
+
await register(d, ctx);
|
|
371
|
+
if (d.service) {
|
|
372
|
+
await installService(d, ctx);
|
|
373
|
+
await waitOnline(d, ctx);
|
|
374
|
+
} else ctx.log(`up ${d.id}: env ready. Start it with: just backend run (it reads ${d.homeDir}/backend.env)`);
|
|
375
|
+
},
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const urlDriver: Driver = {
|
|
379
|
+
async register(d, ctx) {
|
|
380
|
+
syncConfigToRegistry(ctx.config, ctx.registry);
|
|
381
|
+
const i = ctx.args.indexOf("--token");
|
|
382
|
+
const token = i >= 0 ? ctx.args[i + 1] : d.tokenEnv ? process.env[d.tokenEnv] : undefined;
|
|
383
|
+
if (!token) throw Error(`${d.id} is a url device: pass --token <token>${d.tokenEnv ? ` or set ${d.tokenEnv}` : ""}`);
|
|
384
|
+
ctx.registry.setToken(d.id, token);
|
|
385
|
+
ctx.registry.forgetUid(d.id);
|
|
386
|
+
ctx.log(`register ${d.id}: token stored`);
|
|
387
|
+
},
|
|
388
|
+
async up(d, ctx) {
|
|
389
|
+
await urlDriver.register!(d, ctx);
|
|
390
|
+
await waitOnline(d, ctx);
|
|
391
|
+
},
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
export const drivers: Record<DeviceKind, Driver> = {
|
|
395
|
+
local: localDriver,
|
|
396
|
+
ssh: sshDriver,
|
|
397
|
+
"exe-dev": exeDevDriver,
|
|
398
|
+
url: urlDriver,
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
export const defaultHome = () => homedir();
|
|
402
|
+
export { q };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Process helpers for the network ops CLI: local commands, remote shell scripts over ssh
|
|
2
|
+
// (scripts and secrets travel on stdin, never argv), rsync and the exe.dev control plane.
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import type { NetDevice } from "./config.js";
|
|
5
|
+
|
|
6
|
+
export interface RunResult {
|
|
7
|
+
code: number;
|
|
8
|
+
stdout: string;
|
|
9
|
+
stderr: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RunOptions {
|
|
13
|
+
stdin?: string;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
/** Stream output to this process's stdout/stderr while capturing it. */
|
|
16
|
+
echo?: boolean;
|
|
17
|
+
cwd?: string;
|
|
18
|
+
env?: NodeJS.ProcessEnv;
|
|
19
|
+
allowFail?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function run(cmd: string, args: string[], options: RunOptions = {}): Promise<RunResult> {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const child = spawn(cmd, args, {
|
|
25
|
+
cwd: options.cwd,
|
|
26
|
+
env: options.env ?? process.env,
|
|
27
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
28
|
+
});
|
|
29
|
+
let stdout = "";
|
|
30
|
+
let stderr = "";
|
|
31
|
+
const timer = options.timeoutMs
|
|
32
|
+
? setTimeout(() => {
|
|
33
|
+
child.kill("SIGTERM");
|
|
34
|
+
stderr += `\n[timeout after ${options.timeoutMs}ms]`;
|
|
35
|
+
}, options.timeoutMs)
|
|
36
|
+
: undefined;
|
|
37
|
+
child.stdout.on("data", (chunk: Buffer) => {
|
|
38
|
+
stdout += chunk;
|
|
39
|
+
if (options.echo) process.stdout.write(chunk);
|
|
40
|
+
});
|
|
41
|
+
child.stderr.on("data", (chunk: Buffer) => {
|
|
42
|
+
stderr += chunk;
|
|
43
|
+
if (options.echo) process.stderr.write(chunk);
|
|
44
|
+
});
|
|
45
|
+
child.on("error", (e) => {
|
|
46
|
+
if (timer) clearTimeout(timer);
|
|
47
|
+
reject(e);
|
|
48
|
+
});
|
|
49
|
+
child.on("close", (code) => {
|
|
50
|
+
if (timer) clearTimeout(timer);
|
|
51
|
+
const result = { code: code ?? 1, stdout, stderr };
|
|
52
|
+
if (result.code !== 0 && !options.allowFail)
|
|
53
|
+
reject(Object.assign(Error(`${cmd} ${args.slice(0, 3).join(" ")} … failed (exit ${result.code}): ${stderr.trim().slice(-800)}`), { result }));
|
|
54
|
+
else resolve(result);
|
|
55
|
+
});
|
|
56
|
+
child.stdin.end(options.stdin ?? "");
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const SSH_OPTS = ["-o", "BatchMode=yes", "-o", "ConnectTimeout=15", "-o", "StrictHostKeyChecking=accept-new", "-o", "ServerAliveInterval=15"];
|
|
61
|
+
|
|
62
|
+
/** "~/x" → "$HOME/x" for use inside double-quoted remote shell strings. */
|
|
63
|
+
export const shPath = (p: string) => (p === "~" ? "$HOME" : p.startsWith("~/") ? `$HOME/${p.slice(2)}` : p);
|
|
64
|
+
/** Single-quote a value for a POSIX shell. */
|
|
65
|
+
export const q = (s: string) => `'${s.replaceAll("'", `'\\''`)}'`;
|
|
66
|
+
|
|
67
|
+
const PRELUDE = `set -e
|
|
68
|
+
export NVM_DIR="$HOME/.nvm"
|
|
69
|
+
export PATH="/opt/homebrew/bin:/usr/local/bin:$HOME/.local/bin:$PATH"
|
|
70
|
+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" >/dev/null 2>&1 || true
|
|
71
|
+
`;
|
|
72
|
+
|
|
73
|
+
export function sshTarget(device: NetDevice) {
|
|
74
|
+
if (!device.ssh) throw Error(`${device.id} (kind ${device.kind}) has no ssh target`);
|
|
75
|
+
return device.ssh;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Run a POSIX shell script on the device. The script (and anything embedded in it) goes over stdin. */
|
|
79
|
+
export function remote(device: NetDevice, script: string, options: Omit<RunOptions, "stdin"> = {}) {
|
|
80
|
+
return run("ssh", [...SSH_OPTS, sshTarget(device), "sh -s"], { ...options, stdin: PRELUDE + script });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function rsync(device: NetDevice, sources: string[], dest: string, cwd: string) {
|
|
84
|
+
return run(
|
|
85
|
+
"rsync",
|
|
86
|
+
[
|
|
87
|
+
"-a",
|
|
88
|
+
"--delete",
|
|
89
|
+
"--exclude", "node_modules",
|
|
90
|
+
"--exclude", ".data",
|
|
91
|
+
"--exclude", ".durable-pi",
|
|
92
|
+
"--exclude", "dist",
|
|
93
|
+
"--exclude", "*.log",
|
|
94
|
+
"--exclude", ".DS_Store",
|
|
95
|
+
"-e", `ssh ${SSH_OPTS.join(" ")}`,
|
|
96
|
+
...sources,
|
|
97
|
+
`${sshTarget(device)}:${dest}`,
|
|
98
|
+
],
|
|
99
|
+
{ cwd, timeoutMs: 600_000 },
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The exe.dev control plane (`ssh exe.dev <verb> --json`). */
|
|
104
|
+
export async function exeDev<T = unknown>(args: string[], timeoutMs = 60_000): Promise<T> {
|
|
105
|
+
const result = await run("ssh", ["-o", "BatchMode=yes", "-o", "ConnectTimeout=15", "exe.dev", ...args, "--json"], { timeoutMs });
|
|
106
|
+
const text = result.stdout.trim();
|
|
107
|
+
try {
|
|
108
|
+
return (text ? JSON.parse(text) : {}) as T;
|
|
109
|
+
} catch {
|
|
110
|
+
return { raw: text } as T;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Directories that hold secrets (registry database, device keypair, pairing file, backend env).
|
|
3
|
+
*
|
|
4
|
+
* `mkdir` applies its mode only to directories it creates. One that already exists — restored from
|
|
5
|
+
* a backup, copied with the wrong umask, made by hand — keeps whatever mode it had, and anyone who
|
|
6
|
+
* can write to it can unlink the 0600 files inside and put their own in their place. So the
|
|
7
|
+
* directory itself is checked every time, not just created.
|
|
8
|
+
*/
|
|
9
|
+
import { chmodSync, mkdirSync, statSync } from "node:fs";
|
|
10
|
+
|
|
11
|
+
export type PrivateDirOutcome = "created" | "private" | "tightened";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Make `dir` usable only by this account: create it (and missing parents) 0700, or take group/other
|
|
15
|
+
* access away from an existing one. Throws when the directory belongs to another account (their
|
|
16
|
+
* access cannot be removed by us and must not be trusted) or when the mode cannot be enforced.
|
|
17
|
+
* A symlink is followed; the target is what gets judged.
|
|
18
|
+
*/
|
|
19
|
+
export function ensurePrivateDir(dir: string): PrivateDirOutcome {
|
|
20
|
+
const existing = statOrUndefined(dir);
|
|
21
|
+
if (existing && !existing.isDirectory()) throw new Error(`${dir} is not a directory`);
|
|
22
|
+
if (!existing) mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
23
|
+
// POSIX owner/mode bits do not exist on Windows; ACLs are the platform's job there.
|
|
24
|
+
if (process.platform === "win32") return existing ? "private" : "created";
|
|
25
|
+
|
|
26
|
+
const uid = process.getuid?.();
|
|
27
|
+
const st = statSync(dir);
|
|
28
|
+
if (uid !== undefined && st.uid !== uid) {
|
|
29
|
+
throw new Error(`${dir} is owned by uid ${st.uid}, not by this account (uid ${uid}); it cannot be made private. Choose another location or chown it.`);
|
|
30
|
+
}
|
|
31
|
+
if ((st.mode & 0o077) === 0) return existing ? "private" : "created";
|
|
32
|
+
|
|
33
|
+
chmodSync(dir, 0o700);
|
|
34
|
+
const after = statSync(dir).mode & 0o777;
|
|
35
|
+
if ((after & 0o077) !== 0) {
|
|
36
|
+
throw new Error(`${dir} is mode ${after.toString(8)} and chmod 700 did not take effect; refusing to keep secrets there`);
|
|
37
|
+
}
|
|
38
|
+
return existing ? "tightened" : "created";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function statOrUndefined(path: string): ReturnType<typeof statSync> | undefined {
|
|
42
|
+
try {
|
|
43
|
+
return statSync(path);
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if ((e as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
48
|
+
}
|