@tsplc/tmp-agent 0.0.0-stage → 0.6.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/README.md +26 -2
- package/dist/cli.js +4888 -0
- package/dist/cli.js.map +53 -0
- package/dist/client.js +514 -0
- package/dist/client.js.map +12 -0
- package/dist/index.js +3680 -0
- package/dist/index.js.map +41 -0
- package/package.json +61 -4
- package/src/access.ts +260 -0
- package/src/agent.ts +461 -0
- package/src/audit.ts +74 -0
- package/src/cli.ts +290 -0
- package/src/client.ts +407 -0
- package/src/codesys.ts +195 -0
- package/src/dashboard/index.ts +159 -0
- package/src/dashboard/ui/access.ts +191 -0
- package/src/dashboard/ui/actions.ts +207 -0
- package/src/dashboard/ui/api.ts +138 -0
- package/src/dashboard/ui/app.ts +41 -0
- package/src/dashboard/ui/approval.ts +131 -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 +431 -0
- package/src/dashboard/ui/deployments.ts +319 -0
- package/src/dashboard/ui/details.ts +214 -0
- package/src/dashboard/ui/dom.ts +87 -0
- package/src/dashboard/ui/environment.ts +345 -0
- package/src/dashboard/ui/format.ts +124 -0
- package/src/dashboard/ui/logs.ts +126 -0
- package/src/dashboard/ui/main.ts +88 -0
- package/src/dashboard/ui/overlay.ts +144 -0
- package/src/dashboard/ui/shell.ts +185 -0
- package/src/dashboard/ui/source.ts +144 -0
- package/src/debugging.ts +142 -0
- package/src/deploy.ts +203 -0
- package/src/deployments/health.ts +98 -0
- package/src/deployments/retention.ts +44 -0
- package/src/deployments/routes.ts +281 -0
- package/src/deployments/runner.ts +900 -0
- package/src/deployments/store.ts +209 -0
- package/src/deployments/submission.ts +78 -0
- package/src/discovery.ts +90 -0
- package/src/environment.ts +214 -0
- package/src/errors.ts +12 -0
- package/src/index.ts +36 -0
- package/src/instance.ts +51 -0
- package/src/login.ts +267 -0
- package/src/logs.ts +115 -0
- package/src/maintenance.ts +303 -0
- package/src/operations.ts +149 -0
- package/src/processes.ts +41 -0
- package/src/protocol.ts +557 -0
- package/src/releases.ts +414 -0
- package/src/restarts.ts +96 -0
- package/src/routes.ts +128 -0
- package/src/secrets.ts +174 -0
- package/src/server-process.ts +264 -0
- package/src/service.ts +194 -0
- package/src/sessions.ts +58 -0
- package/src/state.ts +126 -0
- package/src/tailscale.ts +112 -0
- package/src/terminal.ts +57 -0
- package/src/transfer.ts +94 -0
- package/src/upgrade.ts +153 -0
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
import {
|
|
2
|
+
controllerCredentialsSchema,
|
|
3
|
+
controllerIdsSchema,
|
|
4
|
+
loginStateSchema,
|
|
5
|
+
onlineChangeFindingSchema,
|
|
6
|
+
rollbackSchema,
|
|
7
|
+
} from "@tsplc/build";
|
|
8
|
+
import { plainValue, secretValue, tsplcConfigSchema } from "@tsplc/config";
|
|
9
|
+
import { envName, releaseConfigSchema, tcpPort } from "@tsplc/config/build";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { version } from "../package.json";
|
|
12
|
+
|
|
13
|
+
/** The tsPLC version a client and the agent it deploys through must share. */
|
|
14
|
+
export const tsplcVersion = version;
|
|
15
|
+
|
|
16
|
+
/** The controller the agent deploys to, as its runtime's TCP block driver address. */
|
|
17
|
+
export const targetSchema = z.strictObject({
|
|
18
|
+
host: z.string().nonempty(),
|
|
19
|
+
port: tcpPort,
|
|
20
|
+
});
|
|
21
|
+
export type Target = z.infer<typeof targetSchema>;
|
|
22
|
+
|
|
23
|
+
export const networkInterfaceSchema = z.strictObject({
|
|
24
|
+
name: z.string(),
|
|
25
|
+
address: z.ipv4(),
|
|
26
|
+
prefixLength: z.number().int().min(0).max(32),
|
|
27
|
+
});
|
|
28
|
+
export type NetworkInterface = z.infer<typeof networkInterfaceSchema>;
|
|
29
|
+
|
|
30
|
+
/** A host on the scanned subnet that accepts connections on the CODESYS runtime port. */
|
|
31
|
+
export const discoveredControllerSchema = z.strictObject({
|
|
32
|
+
address: z.ipv4(),
|
|
33
|
+
port: z.number().int(),
|
|
34
|
+
hostname: z.string().nullable(),
|
|
35
|
+
});
|
|
36
|
+
export type DiscoveredController = z.infer<typeof discoveredControllerSchema>;
|
|
37
|
+
|
|
38
|
+
/** `POST /discover`: the network interface to scan for controllers. */
|
|
39
|
+
export const discoverSchema = z.strictObject({ name: z.string() });
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The controller's user, which the agent passes to CODESYS for one operation. A request sends
|
|
43
|
+
* null instead to use the one stored on the agent's PC.
|
|
44
|
+
*/
|
|
45
|
+
export type Credentials = z.infer<typeof controllerCredentialsSchema>;
|
|
46
|
+
|
|
47
|
+
/** A published leaf of the deployed program, as CODESYS expressions name it. */
|
|
48
|
+
export const variableSchema = z.strictObject({
|
|
49
|
+
path: z.string(),
|
|
50
|
+
type: z.string(),
|
|
51
|
+
});
|
|
52
|
+
export type Variable = z.infer<typeof variableSchema>;
|
|
53
|
+
|
|
54
|
+
/** A target's plain values, by variable name. */
|
|
55
|
+
const targetValuesSchema = z.record(envName, z.string());
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* What `tsplc deploy` uploads beside its files. `config` is the build config with paths
|
|
59
|
+
* relative to the release: `st/`, `template.project` and `libraries/`.
|
|
60
|
+
*/
|
|
61
|
+
export const releaseManifestSchema = z.strictObject({
|
|
62
|
+
// Checked against the owner's schema but kept as uploaded: the release id hashes the manifest.
|
|
63
|
+
config: z.record(z.string(), z.unknown()).superRefine((config, context) => {
|
|
64
|
+
const release = releaseConfigSchema.safeParse(config);
|
|
65
|
+
if (!release.success) {
|
|
66
|
+
context.addIssue({
|
|
67
|
+
code: "custom",
|
|
68
|
+
message: `Invalid release config: ${z.prettifyError(release.error)}`,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
variables: z.array(variableSchema),
|
|
73
|
+
/** The server executable's path in the release, when the project builds one. */
|
|
74
|
+
server: z.string().nonempty().optional(),
|
|
75
|
+
/** The project's `debug` section; the program's debug manifest is the release's `debug.json`. */
|
|
76
|
+
debug: tsplcConfigSchema.shape.debug,
|
|
77
|
+
/** The variable in the agent's environment holding the server's bearer token, for its telemetry and request trace. */
|
|
78
|
+
serverTokenEnv: z.string().nonempty().optional(),
|
|
79
|
+
/** The project's `secrets`, whose placeholders in the ST the agent fills in from Bun.secrets. */
|
|
80
|
+
secrets: tsplcConfigSchema.shape.secrets,
|
|
81
|
+
/** The values of the target it was built for, which its server starts with; none in a release from before 0.5.12. */
|
|
82
|
+
values: targetValuesSchema.default({}),
|
|
83
|
+
});
|
|
84
|
+
export type ReleaseManifest = z.infer<typeof releaseManifestSchema>;
|
|
85
|
+
|
|
86
|
+
/** How `tsplc deploy` compresses each file of a multipart upload, the only form agents without `POST /releases/compare` accept. */
|
|
87
|
+
export const uploadEncoding = "gzip";
|
|
88
|
+
|
|
89
|
+
/** The commit a deployment was built from, when `tsplc deploy` ran in a git checkout. */
|
|
90
|
+
export const gitSourceSchema = z.strictObject({
|
|
91
|
+
commit: z.string().nonempty(),
|
|
92
|
+
branch: z.string().nullable(),
|
|
93
|
+
/** Uncommitted changes went into the build. */
|
|
94
|
+
dirty: z.boolean(),
|
|
95
|
+
/** The commit's subject line. */
|
|
96
|
+
message: z.string().nullable(),
|
|
97
|
+
});
|
|
98
|
+
export type GitSource = z.infer<typeof gitSourceSchema>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* How a deployment applies: `apply` runs it as soon as it is its turn; `review` builds it and
|
|
102
|
+
* checks what an online change would do, then waits for someone to approve it.
|
|
103
|
+
*/
|
|
104
|
+
export const deploymentModeSchema = z.enum(["apply", "review"]);
|
|
105
|
+
export type DeploymentMode = z.infer<typeof deploymentModeSchema>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* What `tsplc deploy` sends with a release to `POST /api/deployments`, in the upload's `options`
|
|
109
|
+
* beside the existing deploy options.
|
|
110
|
+
*/
|
|
111
|
+
export const deploymentRequestSchema = z.strictObject({
|
|
112
|
+
mode: deploymentModeSchema,
|
|
113
|
+
git: gitSourceSchema.nullable(),
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
export const deployOptionsSchema = z
|
|
117
|
+
.strictObject({
|
|
118
|
+
credentials: controllerCredentialsSchema.nullable(),
|
|
119
|
+
/** A full download stops the application; without it only an online change is applied. */
|
|
120
|
+
download: z.boolean(),
|
|
121
|
+
initializeUser: z.boolean(),
|
|
122
|
+
/** How `POST /api/deployments` queues it; `POST /deploy` runs it at once and ignores this. */
|
|
123
|
+
deployment: deploymentRequestSchema.optional(),
|
|
124
|
+
})
|
|
125
|
+
.refine(
|
|
126
|
+
(options) => options.credentials || !options.initializeUser,
|
|
127
|
+
"Initializing the controller's user needs the user's credentials.",
|
|
128
|
+
);
|
|
129
|
+
export type DeployOptions = z.infer<typeof deployOptionsSchema>;
|
|
130
|
+
|
|
131
|
+
/** How many buckets a release's files fall into for `POST /releases/compare`. */
|
|
132
|
+
export const releaseBuckets = 256;
|
|
133
|
+
|
|
134
|
+
/** A release id, or the start of a SHA-256 of content: 16 lowercase hex digits. */
|
|
135
|
+
const shortHex = z.string().regex(/^[a-f0-9]{16}$/);
|
|
136
|
+
|
|
137
|
+
/** `POST /releases/compare`: the digest of each bucket of the release about to be deployed. */
|
|
138
|
+
export const compareRequestSchema = z.strictObject({
|
|
139
|
+
buckets: z.array(shortHex).length(releaseBuckets),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Its answer: the stored release the upload can build on, and that release's files, by path and
|
|
144
|
+
* digest, in each bucket that differs. Without a base the whole release is uploaded.
|
|
145
|
+
*/
|
|
146
|
+
export const comparisonSchema = z.strictObject({
|
|
147
|
+
base: shortHex.nullable(),
|
|
148
|
+
changed: z.array(
|
|
149
|
+
z.strictObject({
|
|
150
|
+
bucket: z
|
|
151
|
+
.number()
|
|
152
|
+
.int()
|
|
153
|
+
.min(0)
|
|
154
|
+
.max(releaseBuckets - 1),
|
|
155
|
+
files: z.record(z.string(), shortHex),
|
|
156
|
+
}),
|
|
157
|
+
),
|
|
158
|
+
});
|
|
159
|
+
export type Comparison = z.infer<typeof comparisonSchema>;
|
|
160
|
+
|
|
161
|
+
/** A `POST /deploy` body in this form is one release upload rather than a multipart form. */
|
|
162
|
+
export const releaseUploadType = "application/vnd.tsplc.release+zstd";
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* A release upload's header. The release is `base`'s files without the `remove` paths, with the
|
|
166
|
+
* uploaded files added or replacing theirs; `buckets` are the digests it must then have.
|
|
167
|
+
*/
|
|
168
|
+
export const releaseUploadSchema = z.strictObject({
|
|
169
|
+
manifest: releaseManifestSchema,
|
|
170
|
+
options: deployOptionsSchema,
|
|
171
|
+
base: shortHex.nullable(),
|
|
172
|
+
buckets: compareRequestSchema.shape.buckets,
|
|
173
|
+
remove: z.array(z.string()),
|
|
174
|
+
});
|
|
175
|
+
export type ReleaseUpload = z.infer<typeof releaseUploadSchema>;
|
|
176
|
+
|
|
177
|
+
/** Long operations stream one of these per line. */
|
|
178
|
+
export const eventSchema = z.discriminatedUnion("kind", [
|
|
179
|
+
z.strictObject({ kind: z.literal("progress"), message: z.string() }),
|
|
180
|
+
z.strictObject({ kind: z.literal("done"), result: z.unknown() }),
|
|
181
|
+
z.strictObject({
|
|
182
|
+
kind: z.literal("error"),
|
|
183
|
+
message: z.string(),
|
|
184
|
+
/** The controller has no application from this agent yet, so only a full download can deploy. */
|
|
185
|
+
downloadRequired: z.boolean(),
|
|
186
|
+
/**
|
|
187
|
+
* What became of the controller after a download failed mid-transfer. Sent only to clients that
|
|
188
|
+
* send their version, since earlier clients reject an event with fields they don't know.
|
|
189
|
+
*/
|
|
190
|
+
rollback: rollbackSchema.optional(),
|
|
191
|
+
}),
|
|
192
|
+
]);
|
|
193
|
+
export type AgentEvent = z.infer<typeof eventSchema>;
|
|
194
|
+
|
|
195
|
+
const deployOperationSchema = z.enum(["download", "online-change"]);
|
|
196
|
+
|
|
197
|
+
export const releaseSchema = z.strictObject({
|
|
198
|
+
id: z.string(),
|
|
199
|
+
deployedAt: z.iso.datetime(),
|
|
200
|
+
operation: deployOperationSchema,
|
|
201
|
+
controllerIds: controllerIdsSchema,
|
|
202
|
+
/** The running server executable, relative to the releases directory. */
|
|
203
|
+
server: z.string().optional(),
|
|
204
|
+
});
|
|
205
|
+
export type Release = z.infer<typeof releaseSchema>;
|
|
206
|
+
|
|
207
|
+
export const statusSchema = z.strictObject({
|
|
208
|
+
target: targetSchema.nullable(),
|
|
209
|
+
release: releaseSchema.nullable(),
|
|
210
|
+
server: z
|
|
211
|
+
.strictObject({ executable: z.string(), running: z.boolean() })
|
|
212
|
+
.nullable(),
|
|
213
|
+
/** The open login's application state and forced expressions, or why it can't report them. */
|
|
214
|
+
login: z
|
|
215
|
+
.union([loginStateSchema, z.strictObject({ error: z.string() })])
|
|
216
|
+
.nullable(),
|
|
217
|
+
/** A controller user is stored on the agent's PC, so requests may leave theirs out. */
|
|
218
|
+
credentialsStored: z.boolean(),
|
|
219
|
+
});
|
|
220
|
+
export type AgentStatus = z.infer<typeof statusSchema>;
|
|
221
|
+
|
|
222
|
+
/** The agent's status, which names the agent's version even to a request it refuses. */
|
|
223
|
+
export const statusPath = "/status";
|
|
224
|
+
|
|
225
|
+
/** `POST` holds the agent for an upgrade, so it takes no operations; `DELETE` lets it take them again. */
|
|
226
|
+
export const agentHoldPath = "/agent/hold";
|
|
227
|
+
|
|
228
|
+
/** `POST`: a held agent exits, as `exitSchema` asks. */
|
|
229
|
+
export const agentExitPath = "/agent/exit";
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* How a held agent exits: leaving the line's server running for the next agent or stopping it,
|
|
233
|
+
* and whether whatever started it should start it again.
|
|
234
|
+
*/
|
|
235
|
+
export const exitSchema = z.strictObject({
|
|
236
|
+
server: z.enum(["leave", "stop"]),
|
|
237
|
+
restart: z.boolean(),
|
|
238
|
+
});
|
|
239
|
+
export type AgentExit = z.infer<typeof exitSchema>;
|
|
240
|
+
|
|
241
|
+
/** A logout's result: why the login couldn't log out, when its container had to be removed instead. */
|
|
242
|
+
export const logoutSchema = z.strictObject({ failure: z.string().optional() });
|
|
243
|
+
|
|
244
|
+
/** Who ran a command, and with which tsPLC, as the client reports it in these headers. */
|
|
245
|
+
export const clientHeaders = {
|
|
246
|
+
user: "x-tsplc-user",
|
|
247
|
+
host: "x-tsplc-host",
|
|
248
|
+
version: "x-tsplc-version",
|
|
249
|
+
/** The client's `workerScriptsDigest`, which tells a local build from a release of its version. */
|
|
250
|
+
scripts: "x-tsplc-scripts",
|
|
251
|
+
} as const;
|
|
252
|
+
|
|
253
|
+
/** The header each agent response names the agent's tsPLC version in; agents before 0.3.0 send none. */
|
|
254
|
+
export const agentVersionHeader = "x-tsplc-agent-version";
|
|
255
|
+
|
|
256
|
+
/** Whether `version` is at least `minimum`, comparing major, minor and patch. */
|
|
257
|
+
const atLeast = (version: string, minimum: [number, number, number]) => {
|
|
258
|
+
const parts = version.split(/[.+-]/).slice(0, 3).map(Number);
|
|
259
|
+
for (const [index, wanted] of minimum.entries()) {
|
|
260
|
+
const part = parts[index] ?? 0;
|
|
261
|
+
if (part !== wanted) {
|
|
262
|
+
return part > wanted;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return true;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/** Whether an agent of `version` has `tsplc-agent upgrade`, which arrived in 0.5.0. */
|
|
269
|
+
export const upgradesItself = (version: string | null) =>
|
|
270
|
+
version !== null && atLeast(version, [0, 5, 0]);
|
|
271
|
+
|
|
272
|
+
/** Why a client and an agent of different tsPLC versions don't deploy, and what to do about it. */
|
|
273
|
+
export const versionMismatch = (versions: {
|
|
274
|
+
agent: string | null;
|
|
275
|
+
client: string;
|
|
276
|
+
/** The agent has `tsplc-agent upgrade`. */
|
|
277
|
+
agentUpgrades: boolean;
|
|
278
|
+
}) =>
|
|
279
|
+
`The plant agent runs ${versions.agent ? `tsPLC ${versions.agent}` : "a tsPLC older than 0.3.0"}, but this deployment comes from tsPLC ${versions.client}. A release is compiled by the agent's own CODESYS scripts, so the two must match: on the plant PC, run ${versions.agentUpgrades ? `tsplc-agent upgrade ${versions.client}` : `bun add --global @tsplc/agent@${versions.client}, then restart tsplc-agent serve`}${versions.agent ? `, or deploy with tsPLC ${versions.agent}` : ""}`;
|
|
280
|
+
|
|
281
|
+
/** Who sent a request, as the client reports it. */
|
|
282
|
+
export const clientIdentity = (request: Request) => {
|
|
283
|
+
const header = (name: string) =>
|
|
284
|
+
request.headers.get(name)?.slice(0, 200) || undefined;
|
|
285
|
+
return { user: header(clientHeaders.user), host: header(clientHeaders.host) };
|
|
286
|
+
};
|
|
287
|
+
export type ClientIdentity = ReturnType<typeof clientIdentity>;
|
|
288
|
+
|
|
289
|
+
/** The tsPLC a request's client runs, as it reports it; clients before 0.3.0 report none. */
|
|
290
|
+
export const clientRelease = (request: Request) => ({
|
|
291
|
+
version:
|
|
292
|
+
request.headers.get(clientHeaders.version)?.slice(0, 50) || undefined,
|
|
293
|
+
scripts:
|
|
294
|
+
request.headers.get(clientHeaders.scripts)?.slice(0, 100) || undefined,
|
|
295
|
+
});
|
|
296
|
+
export type ClientRelease = ReturnType<typeof clientRelease>;
|
|
297
|
+
|
|
298
|
+
/** A line of the agent's `deployments.jsonl`. */
|
|
299
|
+
export const deploymentSchema = z.strictObject({
|
|
300
|
+
at: z.iso.datetime(),
|
|
301
|
+
release: z.string(),
|
|
302
|
+
/** The debug manifest's build id, when the release has one. */
|
|
303
|
+
build: z.string().optional(),
|
|
304
|
+
operation: deployOperationSchema,
|
|
305
|
+
/** Whether the request sent the controller user or the agent used its stored one; lines from before 0.2.18 lack it. */
|
|
306
|
+
credentials: z.enum(["provided", "stored"]).optional(),
|
|
307
|
+
user: z.string().optional(),
|
|
308
|
+
host: z.string().optional(),
|
|
309
|
+
});
|
|
310
|
+
export type Deployment = z.infer<typeof deploymentSchema>;
|
|
311
|
+
|
|
312
|
+
/** A line of the agent's `audit.jsonl`: a login command that changed the controller. */
|
|
313
|
+
export const auditSchema = z.strictObject({
|
|
314
|
+
at: z.iso.datetime(),
|
|
315
|
+
/** As typed after `tsplc`, such as `force` or `reset cold`. */
|
|
316
|
+
command: z.string(),
|
|
317
|
+
/** Expressions written or forced, with their values; unforced expressions map to "". */
|
|
318
|
+
values: z.record(z.string(), z.string()),
|
|
319
|
+
/** The values read just before the command, when the read succeeded. */
|
|
320
|
+
previous: z.record(z.string(), z.string()).optional(),
|
|
321
|
+
user: z.string().optional(),
|
|
322
|
+
host: z.string().optional(),
|
|
323
|
+
release: z.string().optional(),
|
|
324
|
+
error: z.string().optional(),
|
|
325
|
+
});
|
|
326
|
+
export type Audit = z.infer<typeof auditSchema>;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Who may do what on the agent's dashboard and API, each role including the ones before it: a
|
|
330
|
+
* viewer reads deployments, logs, the debugger and the audit log; a deployer also deploys,
|
|
331
|
+
* approves, cancels, redeploys, rolls back and restarts the server; an admin also manages the
|
|
332
|
+
* environment's secrets and who has which role.
|
|
333
|
+
*/
|
|
334
|
+
export const roles = ["viewer", "deployer", "admin"] as const;
|
|
335
|
+
export const roleSchema = z.enum(roles);
|
|
336
|
+
export type Role = z.infer<typeof roleSchema>;
|
|
337
|
+
|
|
338
|
+
export const allows = (role: Role, required: Role) =>
|
|
339
|
+
roles.indexOf(role) >= roles.indexOf(required);
|
|
340
|
+
|
|
341
|
+
/** Who made a request, and how the agent knows. */
|
|
342
|
+
export const identitySchema = z.strictObject({
|
|
343
|
+
/** The Tailscale login, or null for a token or a request from the agent's own PC. */
|
|
344
|
+
login: z.string().nullable(),
|
|
345
|
+
/** A name to show: the Tailscale display name, or how the token or PC is known. */
|
|
346
|
+
name: z.string(),
|
|
347
|
+
role: roleSchema,
|
|
348
|
+
/** `server`: the deployed server, with the token the agent started it with. */
|
|
349
|
+
via: z.enum(["tailscale", "token", "local", "server"]),
|
|
350
|
+
});
|
|
351
|
+
export type Identity = z.infer<typeof identitySchema>;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Where a deployment is. `awaitingApproval` waits on a person: a review to apply, or a full
|
|
355
|
+
* download that would stop the line. A finished deployment is `ready`, `error` or `canceled`.
|
|
356
|
+
*/
|
|
357
|
+
export const deploymentStateSchema = z.enum([
|
|
358
|
+
"queued",
|
|
359
|
+
"building",
|
|
360
|
+
"awaitingApproval",
|
|
361
|
+
"applying",
|
|
362
|
+
"starting",
|
|
363
|
+
"verifying",
|
|
364
|
+
"ready",
|
|
365
|
+
"error",
|
|
366
|
+
"canceled",
|
|
367
|
+
]);
|
|
368
|
+
export type DeploymentState = z.infer<typeof deploymentStateSchema>;
|
|
369
|
+
|
|
370
|
+
/** States a deployment never leaves. */
|
|
371
|
+
export const finalStates: readonly DeploymentState[] = [
|
|
372
|
+
"ready",
|
|
373
|
+
"error",
|
|
374
|
+
"canceled",
|
|
375
|
+
];
|
|
376
|
+
|
|
377
|
+
/** States a deployment may still be canceled in: anything before it applies. */
|
|
378
|
+
export const cancelableStates: readonly DeploymentState[] = [
|
|
379
|
+
"queued",
|
|
380
|
+
"building",
|
|
381
|
+
"awaitingApproval",
|
|
382
|
+
];
|
|
383
|
+
|
|
384
|
+
/** The steps a deployment goes through, in order; a deployment skips the ones it doesn't need. */
|
|
385
|
+
export const deploymentPhaseNameSchema = z.enum([
|
|
386
|
+
"queue",
|
|
387
|
+
"build",
|
|
388
|
+
"review",
|
|
389
|
+
"apply",
|
|
390
|
+
"server",
|
|
391
|
+
"health",
|
|
392
|
+
]);
|
|
393
|
+
|
|
394
|
+
const deploymentIdSchema = z.string().regex(/^dpl_[a-z0-9]{16,32}$/);
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* A deployment as the agent keeps it, in `deployments/<id>/record.json` of its state directory.
|
|
398
|
+
* Every record of the last 14 days is kept, and always the running one and the last that was
|
|
399
|
+
* healthy, with their releases.
|
|
400
|
+
*/
|
|
401
|
+
export const deploymentRecordSchema = z.strictObject({
|
|
402
|
+
id: deploymentIdSchema,
|
|
403
|
+
release: z.string(),
|
|
404
|
+
/** The debug manifest's build id, when the release has one. */
|
|
405
|
+
build: z.string().nullable(),
|
|
406
|
+
createdAt: z.iso.datetime(),
|
|
407
|
+
updatedAt: z.iso.datetime(),
|
|
408
|
+
/** An upload from `tsplc deploy`, or a release the agent already had, deployed again. */
|
|
409
|
+
origin: z.discriminatedUnion("kind", [
|
|
410
|
+
z.strictObject({ kind: z.literal("upload") }),
|
|
411
|
+
z.strictObject({ kind: z.literal("redeploy"), from: deploymentIdSchema }),
|
|
412
|
+
z.strictObject({ kind: z.literal("rollback"), from: deploymentIdSchema }),
|
|
413
|
+
]),
|
|
414
|
+
git: gitSourceSchema.nullable(),
|
|
415
|
+
/** Who asked for it: the identity behind the request, with the deploying machine's user and host. */
|
|
416
|
+
requestedBy: z.strictObject({
|
|
417
|
+
identity: identitySchema.omit({ role: true }),
|
|
418
|
+
user: z.string().nullable(),
|
|
419
|
+
host: z.string().nullable(),
|
|
420
|
+
}),
|
|
421
|
+
mode: deploymentModeSchema,
|
|
422
|
+
/** What it applies or applied: an online change unless a full download was asked for or approved. */
|
|
423
|
+
operation: deployOperationSchema,
|
|
424
|
+
state: deploymentStateSchema,
|
|
425
|
+
phases: z.array(
|
|
426
|
+
z.strictObject({
|
|
427
|
+
name: deploymentPhaseNameSchema,
|
|
428
|
+
startedAt: z.iso.datetime(),
|
|
429
|
+
finishedAt: z.iso.datetime().nullable(),
|
|
430
|
+
outcome: z.enum(["running", "ok", "failed", "skipped"]),
|
|
431
|
+
}),
|
|
432
|
+
),
|
|
433
|
+
/** What the online-change check found: refusals, and what the change restarts. */
|
|
434
|
+
findings: z.array(onlineChangeFindingSchema),
|
|
435
|
+
/** What it waits on while `awaitingApproval`. */
|
|
436
|
+
approval: z
|
|
437
|
+
.strictObject({
|
|
438
|
+
reason: z.enum(["review", "downloadRequired"]),
|
|
439
|
+
message: z.string(),
|
|
440
|
+
})
|
|
441
|
+
.nullable(),
|
|
442
|
+
error: z
|
|
443
|
+
.strictObject({
|
|
444
|
+
message: z.string(),
|
|
445
|
+
downloadRequired: z.boolean(),
|
|
446
|
+
rollback: rollbackSchema.optional(),
|
|
447
|
+
})
|
|
448
|
+
.nullable(),
|
|
449
|
+
controllerIds: controllerIdsSchema.nullable(),
|
|
450
|
+
/** What the check after applying found; null until it ran. */
|
|
451
|
+
health: z
|
|
452
|
+
.strictObject({
|
|
453
|
+
controller: z.enum(["ok", "failed"]),
|
|
454
|
+
server: z.enum(["ok", "failed", "none"]),
|
|
455
|
+
plc: z.enum(["ok", "failed", "unknown"]),
|
|
456
|
+
detail: z.string().nullable(),
|
|
457
|
+
})
|
|
458
|
+
.nullable(),
|
|
459
|
+
/** When it became the running deployment, and when another replaced it; null until and while. */
|
|
460
|
+
activeFrom: z.iso.datetime().nullable(),
|
|
461
|
+
activeUntil: z.iso.datetime().nullable(),
|
|
462
|
+
});
|
|
463
|
+
export type DeploymentRecord = z.infer<typeof deploymentRecordSchema>;
|
|
464
|
+
|
|
465
|
+
/** One line of a deployment's log, `deployments/<id>/log.jsonl`. */
|
|
466
|
+
export const deploymentLogLineSchema = z.strictObject({
|
|
467
|
+
/** Counts from 1 within the deployment; `?after=` resumes from it. */
|
|
468
|
+
seq: z.number().int().positive(),
|
|
469
|
+
at: z.iso.datetime(),
|
|
470
|
+
level: z.enum(["info", "warning", "error"]),
|
|
471
|
+
phase: deploymentPhaseNameSchema.nullable(),
|
|
472
|
+
message: z.string(),
|
|
473
|
+
});
|
|
474
|
+
export type DeploymentLogLine = z.infer<typeof deploymentLogLineSchema>;
|
|
475
|
+
|
|
476
|
+
/** A change to a deployment, as its subscribers and its server-sent events carry it. */
|
|
477
|
+
export type DeploymentEvent =
|
|
478
|
+
| { kind: "record"; record: DeploymentRecord }
|
|
479
|
+
| { kind: "log"; line: DeploymentLogLine };
|
|
480
|
+
|
|
481
|
+
/** The deployments API: `POST` queues a release, `GET` lists the deployments. */
|
|
482
|
+
export const deploymentsApiPath = "/api/deployments";
|
|
483
|
+
|
|
484
|
+
/** Deployment `id` in the deployments API; its actions and views are below it. */
|
|
485
|
+
export const deploymentApiPath = (id: string) =>
|
|
486
|
+
`${deploymentsApiPath}/${encodeURIComponent(id)}`;
|
|
487
|
+
|
|
488
|
+
/** Deployment `id`'s page on the dashboard. */
|
|
489
|
+
export const deploymentPagePath = (id: string) =>
|
|
490
|
+
`/deployments/${encodeURIComponent(id)}`;
|
|
491
|
+
|
|
492
|
+
/** Where the agent serves the debugger. */
|
|
493
|
+
export const debuggerMount = "/debug/";
|
|
494
|
+
|
|
495
|
+
/** `POST /api/deployments`'s answer: the queued deployment and the dashboard page that shows it. */
|
|
496
|
+
export const deploymentCreatedSchema = z.strictObject({
|
|
497
|
+
id: deploymentIdSchema,
|
|
498
|
+
url: z.string(),
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
/** `GET /api/deployments`: newest first, with the running and last healthy ones named. */
|
|
502
|
+
export const deploymentListSchema = z.strictObject({
|
|
503
|
+
deployments: z.array(deploymentRecordSchema),
|
|
504
|
+
current: deploymentIdSchema.nullable(),
|
|
505
|
+
lastGood: deploymentIdSchema.nullable(),
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
/** `POST /api/deployments/:id/approve`: apply it, as a full download when one is required. */
|
|
509
|
+
export const approveSchema = z.strictObject({ download: z.boolean() });
|
|
510
|
+
|
|
511
|
+
/** `POST /api/deployments/:id/redeploy`: deploy its release again. */
|
|
512
|
+
export const redeploySchema = z.strictObject({ mode: deploymentModeSchema });
|
|
513
|
+
|
|
514
|
+
/** `GET /api/deployments/:id/source`: the release's files, by path, with their sizes in bytes. */
|
|
515
|
+
export const deploymentSourceSchema = z.strictObject({
|
|
516
|
+
files: z.array(
|
|
517
|
+
z.strictObject({ path: z.string(), size: z.number().int().nonnegative() }),
|
|
518
|
+
),
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
/** A secret the deployed release's config declares, or one stored without a declaration: never its value. */
|
|
522
|
+
const environmentEntrySchema = z.strictObject({
|
|
523
|
+
name: z.string(),
|
|
524
|
+
declared: z.boolean(),
|
|
525
|
+
set: z.boolean(),
|
|
526
|
+
});
|
|
527
|
+
export type EnvironmentEntry = z.infer<typeof environmentEntrySchema>;
|
|
528
|
+
|
|
529
|
+
/** `GET /api/environment`: the plant PC's variables, and what the running release was built with. */
|
|
530
|
+
export const environmentSchema = z.strictObject({
|
|
531
|
+
/** The plain values the running release was built with, from the config's target and this PC. */
|
|
532
|
+
values: targetValuesSchema,
|
|
533
|
+
/** The plain variables stored on this PC, which the next deployment is built with. */
|
|
534
|
+
variables: z.record(envName, z.string()),
|
|
535
|
+
secrets: z.array(environmentEntrySchema),
|
|
536
|
+
});
|
|
537
|
+
export type EnvironmentListing = z.infer<typeof environmentSchema>;
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* `access.json` in the agent's state directory, and the body of `GET` and `PUT /api/access`: the
|
|
541
|
+
* role of each Tailscale login, and the role a tailnet member without one gets, none by default.
|
|
542
|
+
*/
|
|
543
|
+
export const accessConfigSchema = z.strictObject({
|
|
544
|
+
roles: z.record(z.string().nonempty(), roleSchema),
|
|
545
|
+
tailnetDefault: roleSchema.nullable(),
|
|
546
|
+
});
|
|
547
|
+
export type AccessConfig = z.infer<typeof accessConfigSchema>;
|
|
548
|
+
|
|
549
|
+
/** `POST /api/session`: the agent's full or read token, which signs a browser in. */
|
|
550
|
+
export const sessionRequestSchema = z.strictObject({ token: z.string() });
|
|
551
|
+
|
|
552
|
+
/** `PUT /api/environment/:name`: a plain variable's new value, shown and built with, or a secret's. */
|
|
553
|
+
export const environmentValueSchema = z.discriminatedUnion("secret", [
|
|
554
|
+
z.strictObject({ secret: z.literal(false), value: plainValue }),
|
|
555
|
+
z.strictObject({ secret: z.literal(true), value: secretValue }),
|
|
556
|
+
]);
|
|
557
|
+
export type EnvironmentValue = z.infer<typeof environmentValueSchema>;
|