@tsplc/agent 0.5.9 → 0.5.12
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/dist/cli.js +2660 -525
- package/dist/cli.js.map +32 -18
- package/dist/client.js +262 -37
- package/dist/client.js.map +4 -4
- package/dist/index.js +2325 -415
- package/dist/index.js.map +27 -14
- package/package.json +6 -5
- package/src/access.ts +276 -0
- package/src/agent.ts +130 -29
- package/src/audit.ts +64 -0
- package/src/cli.ts +81 -5
- package/src/client.ts +204 -36
- package/src/controller.ts +60 -0
- package/src/dashboard/index.ts +118 -0
- package/src/dashboard/ui/access.ts +192 -0
- package/src/dashboard/ui/actions.ts +192 -0
- package/src/dashboard/ui/api.ts +158 -0
- package/src/dashboard/ui/app.ts +41 -0
- package/src/dashboard/ui/approval.ts +132 -0
- package/src/dashboard/ui/audit.ts +115 -0
- package/src/dashboard/ui/contract.ts +19 -0
- package/src/dashboard/ui/dashboard.css +1332 -0
- package/src/dashboard/ui/deployment.ts +432 -0
- package/src/dashboard/ui/deployments.ts +317 -0
- package/src/dashboard/ui/details.ts +240 -0
- package/src/dashboard/ui/dom.ts +91 -0
- package/src/dashboard/ui/environment.ts +326 -0
- package/src/dashboard/ui/format.ts +118 -0
- package/src/dashboard/ui/logs.ts +127 -0
- package/src/dashboard/ui/main.ts +86 -0
- package/src/dashboard/ui/overlay.ts +146 -0
- package/src/dashboard/ui/shell.ts +192 -0
- package/src/dashboard/ui/source.ts +144 -0
- package/src/deploy.ts +174 -144
- package/src/deployments/health.ts +80 -0
- package/src/deployments/retention.ts +45 -0
- package/src/deployments/routes.ts +273 -0
- package/src/deployments/runner.ts +877 -0
- package/src/deployments/store.ts +233 -0
- package/src/environment.ts +284 -0
- package/src/index.ts +4 -0
- package/src/login.ts +17 -0
- package/src/operations.ts +6 -1
- package/src/protocol.ts +259 -2
- package/src/releases.ts +73 -1
- package/src/routes.ts +52 -0
- package/src/secrets.ts +74 -4
- package/src/server-process.ts +33 -16
- package/src/sessions.ts +58 -0
- package/src/state.ts +2 -0
- package/src/tailscale.ts +112 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import {
|
|
4
|
+
appendFile,
|
|
5
|
+
mkdir,
|
|
6
|
+
readdir,
|
|
7
|
+
readFile,
|
|
8
|
+
rename,
|
|
9
|
+
rm,
|
|
10
|
+
writeFile,
|
|
11
|
+
} from "node:fs/promises";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { errorMessage } from "../errors";
|
|
14
|
+
import {
|
|
15
|
+
type DeploymentLogLine,
|
|
16
|
+
type DeploymentRecord,
|
|
17
|
+
type DeploymentState,
|
|
18
|
+
deploymentLogLineSchema,
|
|
19
|
+
deploymentRecordSchema,
|
|
20
|
+
} from "../protocol";
|
|
21
|
+
|
|
22
|
+
/** A change to a deployment, as its subscribers hear it. */
|
|
23
|
+
export type DeploymentEvent =
|
|
24
|
+
| { kind: "record"; record: DeploymentRecord }
|
|
25
|
+
| { kind: "log"; line: DeploymentLogLine };
|
|
26
|
+
|
|
27
|
+
/** States a deployment never leaves. */
|
|
28
|
+
const finalStates: readonly DeploymentState[] = ["ready", "error", "canceled"];
|
|
29
|
+
|
|
30
|
+
export const isFinished = (record: DeploymentRecord) =>
|
|
31
|
+
finalStates.includes(record.state);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The deployment running on the line, which no other has replaced yet, and the healthy one that
|
|
35
|
+
* ran last.
|
|
36
|
+
*/
|
|
37
|
+
export const activeDeployments = (records: readonly DeploymentRecord[]) => {
|
|
38
|
+
const running = records.filter((record) => record.activeFrom !== null);
|
|
39
|
+
const newest = (candidates: DeploymentRecord[]) =>
|
|
40
|
+
candidates.reduce<DeploymentRecord | null>(
|
|
41
|
+
(best, record) =>
|
|
42
|
+
best && (best.activeFrom ?? "") >= (record.activeFrom ?? "")
|
|
43
|
+
? best
|
|
44
|
+
: record,
|
|
45
|
+
null,
|
|
46
|
+
)?.id ?? null;
|
|
47
|
+
return {
|
|
48
|
+
current: newest(running.filter((record) => record.activeUntil === null)),
|
|
49
|
+
lastGood: newest(running.filter((record) => record.state === "ready")),
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const parseJson = (text: string): unknown => {
|
|
54
|
+
try {
|
|
55
|
+
return JSON.parse(text);
|
|
56
|
+
} catch {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** 20 lowercase letters and digits after `dpl_`. */
|
|
62
|
+
const newId = () =>
|
|
63
|
+
`dpl_${[...randomBytes(20)].map((byte) => (byte % 36).toString(36)).join("")}`;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Deployment records and logs under `directory`: `<id>/record.json`, rewritten whole through a
|
|
67
|
+
* temporary file, and `<id>/log.jsonl`, appended. Records are held in memory too, so reading one
|
|
68
|
+
* never waits on the disk; each deployment's writes happen in the order they were made.
|
|
69
|
+
*/
|
|
70
|
+
export const openDeploymentStore = async (options: {
|
|
71
|
+
directory: string;
|
|
72
|
+
now: () => Date;
|
|
73
|
+
log: (message: string) => void;
|
|
74
|
+
}) => {
|
|
75
|
+
const { directory } = options;
|
|
76
|
+
const records = new Map<string, DeploymentRecord>();
|
|
77
|
+
/** The last log line's `seq` for each deployment whose log has been read or written. */
|
|
78
|
+
const sequences = new Map<string, number>();
|
|
79
|
+
const writes = new Map<string, Promise<void>>();
|
|
80
|
+
const listeners = new Map<string, Set<(event: DeploymentEvent) => void>>();
|
|
81
|
+
|
|
82
|
+
await mkdir(directory, { recursive: true });
|
|
83
|
+
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
84
|
+
const file = join(directory, entry.name, "record.json");
|
|
85
|
+
if (!entry.isDirectory() || !existsSync(file)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const parsed = deploymentRecordSchema.safeParse(
|
|
89
|
+
parseJson(await readFile(file, "utf8")),
|
|
90
|
+
);
|
|
91
|
+
if (parsed.success && parsed.data.id === entry.name) {
|
|
92
|
+
records.set(parsed.data.id, parsed.data);
|
|
93
|
+
} else {
|
|
94
|
+
options.log(`Skipping the unreadable deployment record ${file}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const recordDirectory = (id: string) => join(directory, id);
|
|
99
|
+
const logFile = (id: string) => join(recordDirectory(id), "log.jsonl");
|
|
100
|
+
|
|
101
|
+
/** Runs `write` after the deployment's earlier writes, whether or not they failed. */
|
|
102
|
+
const inTurn = <Result>(id: string, write: () => Promise<Result>) => {
|
|
103
|
+
const next = (writes.get(id) ?? Promise.resolve()).then(write);
|
|
104
|
+
writes.set(
|
|
105
|
+
id,
|
|
106
|
+
next.then(
|
|
107
|
+
() => undefined,
|
|
108
|
+
(error: unknown) =>
|
|
109
|
+
options.log(
|
|
110
|
+
`Writing deployment ${id} failed: ${errorMessage(error)}`,
|
|
111
|
+
),
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
return next;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const emit = (id: string, event: DeploymentEvent) => {
|
|
118
|
+
for (const listener of listeners.get(id) ?? []) {
|
|
119
|
+
listener(event);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const readLines = async (id: string) => {
|
|
124
|
+
const file = logFile(id);
|
|
125
|
+
if (!existsSync(file)) {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
return (await readFile(file, "utf8"))
|
|
129
|
+
.split("\n")
|
|
130
|
+
.filter(Boolean)
|
|
131
|
+
.flatMap((line) => {
|
|
132
|
+
// A crash can leave the last line half written; it is skipped.
|
|
133
|
+
const parsed = deploymentLogLineSchema.safeParse(parseJson(line));
|
|
134
|
+
return parsed.success ? [parsed.data] : [];
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const writeRecord = (record: DeploymentRecord) =>
|
|
139
|
+
inTurn(record.id, async () => {
|
|
140
|
+
// The newest state is written, which may already include later changes.
|
|
141
|
+
const latest = records.get(record.id);
|
|
142
|
+
if (!latest) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const file = join(recordDirectory(record.id), "record.json");
|
|
146
|
+
const temporary = `${file}.tmp`;
|
|
147
|
+
await mkdir(recordDirectory(record.id), { recursive: true });
|
|
148
|
+
await writeFile(temporary, JSON.stringify(latest, null, 2));
|
|
149
|
+
await rename(temporary, file);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const existing = (id: string) => {
|
|
153
|
+
const record = records.get(id);
|
|
154
|
+
if (!record) {
|
|
155
|
+
throw new Error(`No deployment ${id}`);
|
|
156
|
+
}
|
|
157
|
+
return record;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
newId,
|
|
162
|
+
get: (id: string) => records.get(id),
|
|
163
|
+
/** Every deployment, newest first. */
|
|
164
|
+
list: () =>
|
|
165
|
+
[...records.values()].sort((left, right) =>
|
|
166
|
+
right.createdAt.localeCompare(left.createdAt),
|
|
167
|
+
),
|
|
168
|
+
create: async (record: DeploymentRecord) => {
|
|
169
|
+
records.set(record.id, record);
|
|
170
|
+
sequences.set(record.id, 0);
|
|
171
|
+
await writeRecord(record);
|
|
172
|
+
return record;
|
|
173
|
+
},
|
|
174
|
+
/** Applies `change` to the deployment's record, stamping `updatedAt`, and writes it. */
|
|
175
|
+
update: async (
|
|
176
|
+
id: string,
|
|
177
|
+
change: (record: DeploymentRecord) => DeploymentRecord,
|
|
178
|
+
) => {
|
|
179
|
+
const record = {
|
|
180
|
+
...change(existing(id)),
|
|
181
|
+
updatedAt: options.now().toISOString(),
|
|
182
|
+
};
|
|
183
|
+
records.set(id, record);
|
|
184
|
+
emit(id, { kind: "record", record });
|
|
185
|
+
await writeRecord(record);
|
|
186
|
+
return record;
|
|
187
|
+
},
|
|
188
|
+
/** Appends a line to the deployment's log; subscribers hear it once it is on disk. */
|
|
189
|
+
log: (id: string, entry: Omit<DeploymentLogLine, "seq" | "at">) =>
|
|
190
|
+
inTurn(id, async () => {
|
|
191
|
+
const last =
|
|
192
|
+
sequences.get(id) ??
|
|
193
|
+
(await readLines(id)).reduce(
|
|
194
|
+
(highest, line) => Math.max(highest, line.seq),
|
|
195
|
+
0,
|
|
196
|
+
);
|
|
197
|
+
const line: DeploymentLogLine = {
|
|
198
|
+
seq: last + 1,
|
|
199
|
+
at: options.now().toISOString(),
|
|
200
|
+
...entry,
|
|
201
|
+
};
|
|
202
|
+
sequences.set(id, line.seq);
|
|
203
|
+
await mkdir(recordDirectory(id), { recursive: true });
|
|
204
|
+
await appendFile(logFile(id), `${JSON.stringify(line)}\n`);
|
|
205
|
+
emit(id, { kind: "log", line });
|
|
206
|
+
return line;
|
|
207
|
+
}),
|
|
208
|
+
/** Waits until the deployment's changes so far are on disk and heard. */
|
|
209
|
+
flush: async (id: string) => {
|
|
210
|
+
await writes.get(id);
|
|
211
|
+
},
|
|
212
|
+
/** The deployment's log lines after `seq` `after`. */
|
|
213
|
+
lines: async (id: string, after: number) => {
|
|
214
|
+
await writes.get(id);
|
|
215
|
+
return (await readLines(id)).filter((line) => line.seq > after);
|
|
216
|
+
},
|
|
217
|
+
subscribe: (id: string, listener: (event: DeploymentEvent) => void) => {
|
|
218
|
+
const subscribed = listeners.get(id) ?? new Set();
|
|
219
|
+
subscribed.add(listener);
|
|
220
|
+
listeners.set(id, subscribed);
|
|
221
|
+
return () => {
|
|
222
|
+
subscribed.delete(listener);
|
|
223
|
+
};
|
|
224
|
+
},
|
|
225
|
+
remove: (id: string) =>
|
|
226
|
+
inTurn(id, async () => {
|
|
227
|
+
records.delete(id);
|
|
228
|
+
sequences.delete(id);
|
|
229
|
+
await rm(recordDirectory(id), { recursive: true, force: true });
|
|
230
|
+
}),
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
export type DeploymentStore = Awaited<ReturnType<typeof openDeploymentStore>>;
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { PlantVariables } from "@tsplc/config";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import type { AuditLog } from "./audit";
|
|
7
|
+
import { AgentError, errorMessage } from "./errors";
|
|
8
|
+
import type { Operations } from "./operations";
|
|
9
|
+
import {
|
|
10
|
+
type EnvironmentEntry,
|
|
11
|
+
type EnvironmentListing,
|
|
12
|
+
environmentValueSchema,
|
|
13
|
+
type Identity,
|
|
14
|
+
variableName,
|
|
15
|
+
} from "./protocol";
|
|
16
|
+
import { loadRelease } from "./releases";
|
|
17
|
+
import type { ApiRoute } from "./routes";
|
|
18
|
+
import { environmentService } from "./secrets";
|
|
19
|
+
import type { ServerSupervisor } from "./server-process";
|
|
20
|
+
import type { AgentState } from "./state";
|
|
21
|
+
|
|
22
|
+
/** `environment.json` in the state directory `state`. */
|
|
23
|
+
export const environmentFile = (state: string) =>
|
|
24
|
+
join(state, "environment.json");
|
|
25
|
+
|
|
26
|
+
/** `variables.json` in the state directory `state`. */
|
|
27
|
+
export const variablesFile = (state: string) => join(state, "variables.json");
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* `environment.json`: the names the dashboard stored by Bun.secrets service, since Bun.secrets
|
|
31
|
+
* can't list a service's names. Never a value.
|
|
32
|
+
*/
|
|
33
|
+
const storedNamesSchema = z.record(z.string(), z.array(variableName));
|
|
34
|
+
|
|
35
|
+
/** `variables.json`: the plain variables set on this PC, by name. */
|
|
36
|
+
const storedVariablesSchema = z.record(variableName, z.string());
|
|
37
|
+
|
|
38
|
+
const readJson = async <T>(file: string, schema: z.ZodType<T>, empty: T) =>
|
|
39
|
+
existsSync(file)
|
|
40
|
+
? schema.parse(JSON.parse(await readFile(file, "utf8")))
|
|
41
|
+
: empty;
|
|
42
|
+
|
|
43
|
+
const writeJson = async (file: string, value: unknown) => {
|
|
44
|
+
await writeFile(`${file}.tmp`, JSON.stringify(value, null, 2));
|
|
45
|
+
await rename(`${file}.tmp`, file);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The plant PC's variables: plain ones, shown and edited here, and secrets, set and replaced but
|
|
50
|
+
* never read back; what the running release was built with; and the server's restart. `tsplc
|
|
51
|
+
* deploy` builds the PLC's program with the plain values and carries them to the server, and the
|
|
52
|
+
* agent fills the secrets in from the Bun.secrets service the release names, so a change reaches
|
|
53
|
+
* both with the next deployment.
|
|
54
|
+
*/
|
|
55
|
+
export const createEnvironment = (context: {
|
|
56
|
+
state: AgentState;
|
|
57
|
+
server: ServerSupervisor;
|
|
58
|
+
operations: Operations;
|
|
59
|
+
audit: AuditLog;
|
|
60
|
+
/** `environment.json`. */
|
|
61
|
+
file: string;
|
|
62
|
+
/** `variables.json`. */
|
|
63
|
+
variables: string;
|
|
64
|
+
log: (message: string) => void;
|
|
65
|
+
}) => {
|
|
66
|
+
const { state, audit } = context;
|
|
67
|
+
|
|
68
|
+
const storedNames = () => readJson(context.file, storedNamesSchema, {});
|
|
69
|
+
const writeStoredNames = async (
|
|
70
|
+
service: string,
|
|
71
|
+
update: (names: string[]) => string[],
|
|
72
|
+
) => {
|
|
73
|
+
const stored = await storedNames();
|
|
74
|
+
await writeJson(context.file, {
|
|
75
|
+
...stored,
|
|
76
|
+
[service]: update(stored[service] ?? []),
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
const storedVariables = () =>
|
|
80
|
+
readJson(context.variables, storedVariablesSchema, {});
|
|
81
|
+
|
|
82
|
+
const deployedManifest = async () => {
|
|
83
|
+
const release = await state.release();
|
|
84
|
+
return release
|
|
85
|
+
? (await loadRelease(state.paths.releases, release.id)).manifest
|
|
86
|
+
: undefined;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** The service the deployed release keeps its secrets in, and the names it reads. */
|
|
90
|
+
const declared = async () => {
|
|
91
|
+
const secrets = (await deployedManifest())?.secrets;
|
|
92
|
+
return secrets
|
|
93
|
+
? { service: secrets.service, names: secrets.names }
|
|
94
|
+
: { service: environmentService, names: [] };
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const isSet = async (service: string, name: string) =>
|
|
98
|
+
(await Bun.secrets.get({ service, name })) !== null;
|
|
99
|
+
|
|
100
|
+
const entries = async (): Promise<EnvironmentEntry[]> => {
|
|
101
|
+
const { service, names } = await declared();
|
|
102
|
+
const stored = ((await storedNames())[service] ?? []).filter(
|
|
103
|
+
(name) => !names.includes(name),
|
|
104
|
+
);
|
|
105
|
+
const undeclared = await Promise.all(
|
|
106
|
+
stored.map(async (name) => ({
|
|
107
|
+
name,
|
|
108
|
+
declared: false,
|
|
109
|
+
set: await isSet(service, name),
|
|
110
|
+
})),
|
|
111
|
+
);
|
|
112
|
+
return [
|
|
113
|
+
...(await Promise.all(
|
|
114
|
+
names.map(async (name) => ({
|
|
115
|
+
name,
|
|
116
|
+
declared: true,
|
|
117
|
+
set: await isSet(service, name),
|
|
118
|
+
})),
|
|
119
|
+
)),
|
|
120
|
+
// A name deleted outside the dashboard is gone.
|
|
121
|
+
...undeclared.filter(({ set }) => set),
|
|
122
|
+
];
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const listing = async (): Promise<EnvironmentListing> => ({
|
|
126
|
+
values: (await deployedManifest())?.values ?? {},
|
|
127
|
+
variables: await storedVariables(),
|
|
128
|
+
secrets: await entries(),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/** What `tsplc deploy` builds with: the plain values, and the secrets set in `service`. */
|
|
132
|
+
const forRelease = async (service: string): Promise<PlantVariables> => {
|
|
133
|
+
const held = (await storedNames())[service] ?? [];
|
|
134
|
+
const set = await Promise.all(
|
|
135
|
+
held.map(async (name) => ((await isSet(service, name)) ? [name] : [])),
|
|
136
|
+
);
|
|
137
|
+
return { service, values: await storedVariables(), secrets: set.flat() };
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** Records a change by the variable's name, with a plain one's value; never a secret's. */
|
|
141
|
+
const record = (
|
|
142
|
+
identity: Identity,
|
|
143
|
+
command: string,
|
|
144
|
+
change: {
|
|
145
|
+
values: Record<string, string>;
|
|
146
|
+
previous: Record<string, string> | null;
|
|
147
|
+
},
|
|
148
|
+
) =>
|
|
149
|
+
audit.record({
|
|
150
|
+
identity,
|
|
151
|
+
command,
|
|
152
|
+
values: change.values,
|
|
153
|
+
previous: change.previous,
|
|
154
|
+
error: null,
|
|
155
|
+
});
|
|
156
|
+
/** A secret's change: its name alone. */
|
|
157
|
+
const nameOnly = { values: {}, previous: null };
|
|
158
|
+
|
|
159
|
+
const withoutVariable = async (name: string) => {
|
|
160
|
+
const { [name]: removed, ...rest } = await storedVariables();
|
|
161
|
+
if (removed !== undefined) {
|
|
162
|
+
await writeJson(context.variables, rest);
|
|
163
|
+
}
|
|
164
|
+
return removed;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const routes: ApiRoute[] = [
|
|
168
|
+
{
|
|
169
|
+
method: "GET",
|
|
170
|
+
pattern: "/api/environment",
|
|
171
|
+
role: "viewer",
|
|
172
|
+
handle: async () => Response.json(await listing()),
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
method: "GET",
|
|
176
|
+
pattern: "/api/environment/release",
|
|
177
|
+
role: "deployer",
|
|
178
|
+
handle: async (request) =>
|
|
179
|
+
Response.json(
|
|
180
|
+
await forRelease(
|
|
181
|
+
new URL(request.url).searchParams.get("service") ||
|
|
182
|
+
environmentService,
|
|
183
|
+
),
|
|
184
|
+
),
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
method: "PUT",
|
|
188
|
+
pattern: "/api/environment/:name",
|
|
189
|
+
role: "admin",
|
|
190
|
+
handle: async (request, { identity, params }) => {
|
|
191
|
+
const name = variableName.parse(params.name);
|
|
192
|
+
const change = environmentValueSchema.parse(await request.json());
|
|
193
|
+
const { service } = await declared();
|
|
194
|
+
if (change.secret) {
|
|
195
|
+
await Bun.secrets.set({ service, name, value: change.value });
|
|
196
|
+
await writeStoredNames(service, (stored) => [
|
|
197
|
+
...stored.filter((other) => other !== name),
|
|
198
|
+
name,
|
|
199
|
+
]);
|
|
200
|
+
await withoutVariable(name);
|
|
201
|
+
await record(identity, `environment set ${name}`, nameOnly);
|
|
202
|
+
return Response.json(await listing());
|
|
203
|
+
}
|
|
204
|
+
const previous = (await storedVariables())[name];
|
|
205
|
+
await writeJson(context.variables, {
|
|
206
|
+
...(await storedVariables()),
|
|
207
|
+
[name]: change.value,
|
|
208
|
+
});
|
|
209
|
+
// A name is a plain variable or a secret, never both.
|
|
210
|
+
if (await Bun.secrets.delete({ service, name })) {
|
|
211
|
+
await writeStoredNames(service, (stored) =>
|
|
212
|
+
stored.filter((other) => other !== name),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
await record(identity, `environment set ${name}`, {
|
|
216
|
+
values: { [name]: change.value },
|
|
217
|
+
previous: previous === undefined ? null : { [name]: previous },
|
|
218
|
+
});
|
|
219
|
+
return Response.json(await listing());
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
method: "DELETE",
|
|
224
|
+
pattern: "/api/environment/:name",
|
|
225
|
+
role: "admin",
|
|
226
|
+
handle: async (_request, { identity, params }) => {
|
|
227
|
+
const name = variableName.parse(params.name);
|
|
228
|
+
const removed = await withoutVariable(name);
|
|
229
|
+
if (removed !== undefined) {
|
|
230
|
+
await record(identity, `environment delete ${name}`, {
|
|
231
|
+
values: {},
|
|
232
|
+
previous: { [name]: removed },
|
|
233
|
+
});
|
|
234
|
+
return Response.json(await listing());
|
|
235
|
+
}
|
|
236
|
+
const { service } = await declared();
|
|
237
|
+
if (!(await Bun.secrets.delete({ service, name }))) {
|
|
238
|
+
throw new AgentError(`${name} isn't set`, 404);
|
|
239
|
+
}
|
|
240
|
+
await writeStoredNames(service, (stored) =>
|
|
241
|
+
stored.filter((other) => other !== name),
|
|
242
|
+
);
|
|
243
|
+
await record(identity, `environment delete ${name}`, nameOnly);
|
|
244
|
+
return Response.json(await listing());
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
method: "POST",
|
|
249
|
+
pattern: "/api/server/restart",
|
|
250
|
+
role: "deployer",
|
|
251
|
+
handle: async (_request, { identity }) => {
|
|
252
|
+
const running = context.server.status();
|
|
253
|
+
if (!running) {
|
|
254
|
+
throw new AgentError("The deployed release runs no server", 409);
|
|
255
|
+
}
|
|
256
|
+
const failure = await context.operations
|
|
257
|
+
.exclusive("a server restart", () =>
|
|
258
|
+
context.server.replace(running.executable),
|
|
259
|
+
)
|
|
260
|
+
.then(
|
|
261
|
+
() => null,
|
|
262
|
+
(error: unknown) => error,
|
|
263
|
+
);
|
|
264
|
+
// Refused before it began: another operation runs.
|
|
265
|
+
if (failure instanceof AgentError) {
|
|
266
|
+
throw failure;
|
|
267
|
+
}
|
|
268
|
+
await audit.record({
|
|
269
|
+
identity,
|
|
270
|
+
command: "server restart",
|
|
271
|
+
values: {},
|
|
272
|
+
previous: null,
|
|
273
|
+
error: failure === null ? null : errorMessage(failure),
|
|
274
|
+
});
|
|
275
|
+
if (failure !== null) {
|
|
276
|
+
context.log(`Restarting the server failed: ${errorMessage(failure)}`);
|
|
277
|
+
throw new AgentError(errorMessage(failure), 500);
|
|
278
|
+
}
|
|
279
|
+
return Response.json(context.server.status());
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
];
|
|
283
|
+
return { routes };
|
|
284
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -4,17 +4,21 @@ export {
|
|
|
4
4
|
type AgentEvent,
|
|
5
5
|
type AgentStatus,
|
|
6
6
|
type Audit,
|
|
7
|
+
agentVersionHeader,
|
|
7
8
|
auditSchema,
|
|
8
9
|
type Credentials,
|
|
9
10
|
clientHeaders,
|
|
10
11
|
controllerSchema,
|
|
11
12
|
credentialsSchema,
|
|
12
13
|
type Deployment,
|
|
14
|
+
type DeploymentMode,
|
|
15
|
+
type DeploymentRecord,
|
|
13
16
|
type DeployOptions,
|
|
14
17
|
type DiscoveredController,
|
|
15
18
|
deploymentSchema,
|
|
16
19
|
deployOptionsSchema,
|
|
17
20
|
eventSchema,
|
|
21
|
+
type GitSource,
|
|
18
22
|
logoutSchema,
|
|
19
23
|
type NetworkInterface,
|
|
20
24
|
networkInterfaceSchema,
|
package/src/login.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
findControllerLogin,
|
|
6
6
|
type LoginCommand,
|
|
7
7
|
openControllerLogin,
|
|
8
|
+
type ResetKind,
|
|
8
9
|
settleWarmController,
|
|
9
10
|
} from "@tsplc/build";
|
|
10
11
|
import { LoginUnavailable, noLogin } from "@tsplc/debug";
|
|
@@ -202,6 +203,22 @@ export const createLogin = (context: {
|
|
|
202
203
|
});
|
|
203
204
|
},
|
|
204
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Resets the application through the open login and starts it again, as `tsplc reset` then
|
|
208
|
+
* `tsplc start` do. Returns the state it runs in.
|
|
209
|
+
*/
|
|
210
|
+
restart: async (reset: ResetKind) => {
|
|
211
|
+
const login = await requireLogin();
|
|
212
|
+
return enqueue(async () => {
|
|
213
|
+
await login.send({ command: "reset", kind: reset });
|
|
214
|
+
const started = await login.send({ command: "start" });
|
|
215
|
+
if (!("state" in started)) {
|
|
216
|
+
throw new Error("The login answered a start without the state");
|
|
217
|
+
}
|
|
218
|
+
return { state: started.state };
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
|
|
205
222
|
/** Whether a login's container runs, open or opening. */
|
|
206
223
|
isOpen: async () =>
|
|
207
224
|
(await findControllerLogin(loginContainer)) !== undefined,
|
package/src/operations.ts
CHANGED
|
@@ -3,7 +3,12 @@ import { AgentError, errorMessage } from "./errors";
|
|
|
3
3
|
import type { AgentEvent } from "./protocol";
|
|
4
4
|
|
|
5
5
|
/** What the agent can be busy with, as its messages name it. */
|
|
6
|
-
export type Operation =
|
|
6
|
+
export type Operation =
|
|
7
|
+
| "a deployment"
|
|
8
|
+
| "a login"
|
|
9
|
+
| "a logout"
|
|
10
|
+
| "a server restart"
|
|
11
|
+
| "a controller restart";
|
|
7
12
|
|
|
8
13
|
/** The agent runs one deployment, login or logout at a time, and none while it is held for an upgrade. */
|
|
9
14
|
export const createOperations = (log: (message: string) => void) => {
|