@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,192 @@
|
|
|
1
|
+
import { errorMessage } from "../../errors";
|
|
2
|
+
import type { AccessConfig, Role } from "../../protocol";
|
|
3
|
+
import { api } from "./api";
|
|
4
|
+
import { type App, attempt } from "./app";
|
|
5
|
+
import { roleNames } from "./contract";
|
|
6
|
+
import { empty, fill, h, pageHeader, type View } from "./dom";
|
|
7
|
+
|
|
8
|
+
const noRole = "";
|
|
9
|
+
|
|
10
|
+
const roleSelect = (selected: Role | null, options: { none: string | null }) =>
|
|
11
|
+
h(
|
|
12
|
+
"select",
|
|
13
|
+
{ class: "input" },
|
|
14
|
+
options.none === null
|
|
15
|
+
? null
|
|
16
|
+
: h(
|
|
17
|
+
"option",
|
|
18
|
+
{ value: noRole, selected: selected === null },
|
|
19
|
+
options.none,
|
|
20
|
+
),
|
|
21
|
+
...roleNames.map((role) =>
|
|
22
|
+
h("option", { value: role, selected: role === selected }, role),
|
|
23
|
+
),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const chosenRole = (select: HTMLSelectElement) =>
|
|
27
|
+
roleNames.find((role) => role === select.value) ?? null;
|
|
28
|
+
|
|
29
|
+
/** Who has which role: by Tailscale login, and the role any other member of the tailnet gets. */
|
|
30
|
+
export const accessView = (app: App): View => {
|
|
31
|
+
const holder = h("div", {}, empty("Loading access…"));
|
|
32
|
+
if (!app.can("admin")) {
|
|
33
|
+
fill(holder, empty("Only an admin can see and change who has which role."));
|
|
34
|
+
return {
|
|
35
|
+
element: h("div", { class: "page" }, pageHeader("Access", null), holder),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const render = (access: AccessConfig) => {
|
|
40
|
+
const logins = Object.entries(access.roles).sort(([a], [b]) =>
|
|
41
|
+
a.localeCompare(b),
|
|
42
|
+
);
|
|
43
|
+
const rows = logins.map(([login, role]) => ({
|
|
44
|
+
login,
|
|
45
|
+
select: roleSelect(role, { none: null }),
|
|
46
|
+
}));
|
|
47
|
+
const tailnet = roleSelect(access.tailnetDefault, { none: "No access" });
|
|
48
|
+
const newLogin = h("input", {
|
|
49
|
+
class: "input",
|
|
50
|
+
type: "email",
|
|
51
|
+
placeholder: "someone@example.com",
|
|
52
|
+
autocomplete: "off",
|
|
53
|
+
});
|
|
54
|
+
const newRole = roleSelect("viewer", { none: null });
|
|
55
|
+
const save = async (next: AccessConfig) => {
|
|
56
|
+
const saved = await attempt(() => api.saveAccess(next), "Saved access");
|
|
57
|
+
if (saved) {
|
|
58
|
+
render(saved);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const current = (): AccessConfig => ({
|
|
62
|
+
roles: Object.fromEntries(
|
|
63
|
+
rows.flatMap(({ login, select }) => {
|
|
64
|
+
const role = chosenRole(select);
|
|
65
|
+
return role ? [[login, role]] : [];
|
|
66
|
+
}),
|
|
67
|
+
),
|
|
68
|
+
tailnetDefault: chosenRole(tailnet),
|
|
69
|
+
});
|
|
70
|
+
fill(
|
|
71
|
+
holder,
|
|
72
|
+
h(
|
|
73
|
+
"section",
|
|
74
|
+
{ class: "card" },
|
|
75
|
+
h("h3", {}, "Tailnet default"),
|
|
76
|
+
h(
|
|
77
|
+
"p",
|
|
78
|
+
{ class: "muted" },
|
|
79
|
+
"The role of a member of the tailnet without a role of their own.",
|
|
80
|
+
),
|
|
81
|
+
tailnet,
|
|
82
|
+
),
|
|
83
|
+
h(
|
|
84
|
+
"section",
|
|
85
|
+
{ class: "card" },
|
|
86
|
+
h("h3", {}, "Roles by login"),
|
|
87
|
+
h(
|
|
88
|
+
"p",
|
|
89
|
+
{ class: "muted" },
|
|
90
|
+
"A viewer reads deployments, logs, the debugger and the audit log. A deployer also deploys, approves, rolls back and restarts the server. An admin also manages the environment's variables and access.",
|
|
91
|
+
),
|
|
92
|
+
rows.length
|
|
93
|
+
? h(
|
|
94
|
+
"table",
|
|
95
|
+
{ class: "table" },
|
|
96
|
+
h(
|
|
97
|
+
"thead",
|
|
98
|
+
{},
|
|
99
|
+
h(
|
|
100
|
+
"tr",
|
|
101
|
+
{},
|
|
102
|
+
h("th", {}, "Tailscale login"),
|
|
103
|
+
h("th", {}, "Role"),
|
|
104
|
+
h("th", {}, ""),
|
|
105
|
+
),
|
|
106
|
+
),
|
|
107
|
+
h(
|
|
108
|
+
"tbody",
|
|
109
|
+
{},
|
|
110
|
+
...rows.map(({ login, select }) =>
|
|
111
|
+
h(
|
|
112
|
+
"tr",
|
|
113
|
+
{},
|
|
114
|
+
h("td", { class: "mono" }, login),
|
|
115
|
+
h("td", {}, select),
|
|
116
|
+
h(
|
|
117
|
+
"td",
|
|
118
|
+
{ class: "row-actions" },
|
|
119
|
+
h(
|
|
120
|
+
"button",
|
|
121
|
+
{
|
|
122
|
+
class: "button small danger-text",
|
|
123
|
+
onclick: () => {
|
|
124
|
+
const next = current();
|
|
125
|
+
void save({
|
|
126
|
+
...next,
|
|
127
|
+
roles: Object.fromEntries(
|
|
128
|
+
Object.entries(next.roles).filter(
|
|
129
|
+
([name]) => name !== login,
|
|
130
|
+
),
|
|
131
|
+
),
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
"Remove",
|
|
136
|
+
),
|
|
137
|
+
),
|
|
138
|
+
),
|
|
139
|
+
),
|
|
140
|
+
),
|
|
141
|
+
)
|
|
142
|
+
: empty("No login has a role yet."),
|
|
143
|
+
h(
|
|
144
|
+
"form",
|
|
145
|
+
{
|
|
146
|
+
class: "inline-form",
|
|
147
|
+
onsubmit: (event) => {
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
const login = newLogin.value.trim();
|
|
150
|
+
const role = chosenRole(newRole);
|
|
151
|
+
if (!login || !role) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const next = current();
|
|
155
|
+
void save({ ...next, roles: { ...next.roles, [login]: role } });
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
newLogin,
|
|
159
|
+
newRole,
|
|
160
|
+
h("button", { class: "button", type: "submit" }, "Add"),
|
|
161
|
+
),
|
|
162
|
+
),
|
|
163
|
+
h(
|
|
164
|
+
"div",
|
|
165
|
+
{ class: "form-actions" },
|
|
166
|
+
h(
|
|
167
|
+
"button",
|
|
168
|
+
{ class: "button primary", onclick: () => void save(current()) },
|
|
169
|
+
"Save changes",
|
|
170
|
+
),
|
|
171
|
+
),
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
void api
|
|
176
|
+
.access()
|
|
177
|
+
.then(render, (error: unknown) =>
|
|
178
|
+
fill(holder, h("p", { class: "error-text" }, errorMessage(error))),
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
element: h(
|
|
183
|
+
"div",
|
|
184
|
+
{ class: "page" },
|
|
185
|
+
pageHeader(
|
|
186
|
+
"Access",
|
|
187
|
+
"Who may view, deploy and administer this agent. The agent's tokens keep their roles: the full token is an admin, the read token a viewer.",
|
|
188
|
+
),
|
|
189
|
+
holder,
|
|
190
|
+
),
|
|
191
|
+
};
|
|
192
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { DeploymentMode, DeploymentRecord } from "../../protocol";
|
|
2
|
+
import { api } from "./api";
|
|
3
|
+
import { type App, attempt, deploymentHref } from "./app";
|
|
4
|
+
import { h } from "./dom";
|
|
5
|
+
import { cancelable, shortId, states } from "./format";
|
|
6
|
+
import { confirm, dialog, type MenuItem, toast } from "./overlay";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Resets the controller's application, warm or cold as chosen, and starts it again: CODESYS's
|
|
10
|
+
* "Reset warm" then "Start", for clearing a state the application got stuck in.
|
|
11
|
+
*/
|
|
12
|
+
export const restartController = async (
|
|
13
|
+
/** Told when the restart is sent and when the agent has answered. */
|
|
14
|
+
running: (busy: boolean) => void,
|
|
15
|
+
) => {
|
|
16
|
+
const reset = await dialog<"warm" | "cold" | null>({
|
|
17
|
+
title: "Reset and start the application?",
|
|
18
|
+
body: [
|
|
19
|
+
h(
|
|
20
|
+
"p",
|
|
21
|
+
{},
|
|
22
|
+
"The application stops, its variables go back to their initial values, and it starts again with the same code. The line stops for a moment.",
|
|
23
|
+
),
|
|
24
|
+
h(
|
|
25
|
+
"p",
|
|
26
|
+
{ class: "muted" },
|
|
27
|
+
"A warm reset keeps retain variables; a cold reset resets them too.",
|
|
28
|
+
),
|
|
29
|
+
],
|
|
30
|
+
actions: (close) => [
|
|
31
|
+
h("button", { class: "button", onclick: () => close(null) }, "Cancel"),
|
|
32
|
+
h(
|
|
33
|
+
"button",
|
|
34
|
+
{ class: "button danger", onclick: () => close("cold") },
|
|
35
|
+
"Reset cold and start",
|
|
36
|
+
),
|
|
37
|
+
h(
|
|
38
|
+
"button",
|
|
39
|
+
{ class: "button primary", onclick: () => close("warm") },
|
|
40
|
+
"Reset warm and start",
|
|
41
|
+
),
|
|
42
|
+
],
|
|
43
|
+
});
|
|
44
|
+
if (!reset) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
running(true);
|
|
48
|
+
try {
|
|
49
|
+
await attempt(
|
|
50
|
+
() => api.restartController({ reset }),
|
|
51
|
+
`Reset ${reset}; the application runs again`,
|
|
52
|
+
);
|
|
53
|
+
} finally {
|
|
54
|
+
running(false);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** Deploys a deployment's release again, applied at once or reviewed first, then opens the new one. */
|
|
59
|
+
export const redeploy = async (app: App, record: DeploymentRecord) => {
|
|
60
|
+
const mode = await dialog<DeploymentMode>({
|
|
61
|
+
title: "Redeploy",
|
|
62
|
+
body: [
|
|
63
|
+
h(
|
|
64
|
+
"p",
|
|
65
|
+
{},
|
|
66
|
+
"Deploy the release of ",
|
|
67
|
+
h("code", {}, shortId(record.id)),
|
|
68
|
+
" again as a new deployment.",
|
|
69
|
+
),
|
|
70
|
+
h(
|
|
71
|
+
"p",
|
|
72
|
+
{ class: "muted" },
|
|
73
|
+
"Apply runs it as soon as it is its turn. Review checks what an online change would do first, and waits for someone to approve it.",
|
|
74
|
+
),
|
|
75
|
+
],
|
|
76
|
+
actions: (close) => [
|
|
77
|
+
h(
|
|
78
|
+
"button",
|
|
79
|
+
{ class: "button", onclick: () => close("review") },
|
|
80
|
+
"Review first",
|
|
81
|
+
),
|
|
82
|
+
h(
|
|
83
|
+
"button",
|
|
84
|
+
{ class: "button primary", onclick: () => close("apply") },
|
|
85
|
+
"Redeploy",
|
|
86
|
+
),
|
|
87
|
+
],
|
|
88
|
+
});
|
|
89
|
+
if (!mode) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const created = await attempt(() => api.redeploy(record.id, mode), null);
|
|
93
|
+
if (created) {
|
|
94
|
+
app.navigate(deploymentHref(created.id));
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/** Deploys a deployment's release again as an online change, after a confirmation, then opens the new one. */
|
|
99
|
+
export const rollback = async (app: App, record: DeploymentRecord) => {
|
|
100
|
+
const confirmed = await confirm({
|
|
101
|
+
title: "Roll back to this deployment?",
|
|
102
|
+
body: [
|
|
103
|
+
h(
|
|
104
|
+
"p",
|
|
105
|
+
{},
|
|
106
|
+
"A new deployment applies the release of ",
|
|
107
|
+
h("code", {}, shortId(record.id)),
|
|
108
|
+
` (${record.release.slice(0, 12)}) to the controller as an online change, and starts its server.`,
|
|
109
|
+
),
|
|
110
|
+
h(
|
|
111
|
+
"p",
|
|
112
|
+
{ class: "muted" },
|
|
113
|
+
"If an online change can't apply it, the deployment waits for approval of a full download rather than stopping the line by itself.",
|
|
114
|
+
),
|
|
115
|
+
],
|
|
116
|
+
confirm: "Roll back",
|
|
117
|
+
tone: "primary",
|
|
118
|
+
});
|
|
119
|
+
if (!confirmed) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const created = await attempt(() => api.rollback(record.id), null);
|
|
123
|
+
if (created) {
|
|
124
|
+
app.navigate(deploymentHref(created.id));
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export const cancel = async (record: DeploymentRecord) => {
|
|
129
|
+
const confirmed = await confirm({
|
|
130
|
+
title: "Cancel this deployment?",
|
|
131
|
+
body: [
|
|
132
|
+
h(
|
|
133
|
+
"p",
|
|
134
|
+
{},
|
|
135
|
+
"The controller is left as it is; nothing of this deployment is applied.",
|
|
136
|
+
),
|
|
137
|
+
],
|
|
138
|
+
confirm: "Cancel deployment",
|
|
139
|
+
tone: "danger",
|
|
140
|
+
});
|
|
141
|
+
if (confirmed) {
|
|
142
|
+
await attempt(() => api.cancel(record.id), "Deployment canceled");
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** A deployment's actions, as a menu offers them: only the ones the signed-in role may take. */
|
|
147
|
+
export const deploymentMenu = (
|
|
148
|
+
app: App,
|
|
149
|
+
record: DeploymentRecord,
|
|
150
|
+
{ current, open }: { current: boolean; open: boolean },
|
|
151
|
+
): MenuItem[] => {
|
|
152
|
+
const deployer = app.can("deployer");
|
|
153
|
+
const finished = states[record.state].finished;
|
|
154
|
+
const items: MenuItem[] = [];
|
|
155
|
+
if (open) {
|
|
156
|
+
items.push({
|
|
157
|
+
label: "Open",
|
|
158
|
+
run: () => app.navigate(deploymentHref(record.id)),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (deployer) {
|
|
162
|
+
items.push(
|
|
163
|
+
{
|
|
164
|
+
label: "Redeploy…",
|
|
165
|
+
run: () => void redeploy(app, record),
|
|
166
|
+
disabled: !finished,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
label: "Rollback to this…",
|
|
170
|
+
run: () => void rollback(app, record),
|
|
171
|
+
// Only a release that once ran healthy, and not the one running now.
|
|
172
|
+
disabled: record.state !== "ready" || current,
|
|
173
|
+
},
|
|
174
|
+
);
|
|
175
|
+
if (cancelable(record.state)) {
|
|
176
|
+
items.push({
|
|
177
|
+
label: "Cancel…",
|
|
178
|
+
run: () => void cancel(record),
|
|
179
|
+
tone: "danger",
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
items.push("separator", {
|
|
184
|
+
label: "Copy id",
|
|
185
|
+
run: () =>
|
|
186
|
+
void navigator.clipboard.writeText(record.id).then(
|
|
187
|
+
() => toast("Copied the deployment id", "ok"),
|
|
188
|
+
() => toast("The browser refused the clipboard", "error"),
|
|
189
|
+
),
|
|
190
|
+
});
|
|
191
|
+
return items;
|
|
192
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ControllerRestart,
|
|
3
|
+
controllerRestartReplySchema,
|
|
4
|
+
} from "@tsplc/server/plant-agent";
|
|
5
|
+
import type { z } from "zod";
|
|
6
|
+
import type {
|
|
7
|
+
AccessConfig,
|
|
8
|
+
AgentStatus,
|
|
9
|
+
Audit,
|
|
10
|
+
DeploymentLogLine,
|
|
11
|
+
DeploymentMode,
|
|
12
|
+
DeploymentRecord,
|
|
13
|
+
deploymentCreatedSchema,
|
|
14
|
+
deploymentListSchema,
|
|
15
|
+
deploymentSourceSchema,
|
|
16
|
+
EnvironmentListing,
|
|
17
|
+
EnvironmentValue,
|
|
18
|
+
environmentEntrySchema,
|
|
19
|
+
Identity,
|
|
20
|
+
} from "../../protocol";
|
|
21
|
+
import { controllerRestartPath } from "./contract";
|
|
22
|
+
|
|
23
|
+
// The answers come from the agent that served this page, built from the same sources, so they are
|
|
24
|
+
// trusted: protocol.ts can't be bundled for a browser to parse them, since it loads @tsplc/build.
|
|
25
|
+
|
|
26
|
+
export type DeploymentList = z.infer<typeof deploymentListSchema>;
|
|
27
|
+
export type DeploymentCreated = z.infer<typeof deploymentCreatedSchema>;
|
|
28
|
+
export type EnvironmentEntry = z.infer<typeof environmentEntrySchema>;
|
|
29
|
+
|
|
30
|
+
export type SourceFile = z.infer<
|
|
31
|
+
typeof deploymentSourceSchema
|
|
32
|
+
>["files"][number];
|
|
33
|
+
|
|
34
|
+
/** A refused or failed request, with the agent's message. */
|
|
35
|
+
export class ApiError extends Error {
|
|
36
|
+
constructor(
|
|
37
|
+
readonly status: number,
|
|
38
|
+
message: string,
|
|
39
|
+
) {
|
|
40
|
+
super(message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const failure = async (response: Response) => {
|
|
45
|
+
const text = await response.text();
|
|
46
|
+
try {
|
|
47
|
+
const body: unknown = JSON.parse(text);
|
|
48
|
+
if (
|
|
49
|
+
typeof body === "object" &&
|
|
50
|
+
body !== null &&
|
|
51
|
+
"error" in body &&
|
|
52
|
+
typeof body.error === "string"
|
|
53
|
+
) {
|
|
54
|
+
return new ApiError(response.status, body.error);
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
// Not JSON: the text is the message.
|
|
58
|
+
}
|
|
59
|
+
return new ApiError(
|
|
60
|
+
response.status,
|
|
61
|
+
text || `${response.status} ${response.statusText}`,
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
let version: string | null = null;
|
|
66
|
+
|
|
67
|
+
/** The tsPLC version of the agent, as its last answer named it. */
|
|
68
|
+
export const agentVersion = () => version;
|
|
69
|
+
|
|
70
|
+
const call = async (
|
|
71
|
+
method: "GET" | "POST" | "PUT" | "DELETE",
|
|
72
|
+
path: string,
|
|
73
|
+
body: { json: unknown } | null,
|
|
74
|
+
) => {
|
|
75
|
+
const response = await fetch(path, {
|
|
76
|
+
method,
|
|
77
|
+
credentials: "same-origin",
|
|
78
|
+
// A signed-in browser must mark what it changes, so another site can't make it send one.
|
|
79
|
+
headers: {
|
|
80
|
+
...(method !== "GET" && { "x-tsplc-dashboard": "1" }),
|
|
81
|
+
...(body && { "Content-Type": "application/json" }),
|
|
82
|
+
},
|
|
83
|
+
body: body ? JSON.stringify(body.json) : null,
|
|
84
|
+
});
|
|
85
|
+
version = response.headers.get("x-tsplc-agent-version") ?? version;
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
throw await failure(response);
|
|
88
|
+
}
|
|
89
|
+
return response;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const get = async (path: string) => (await call("GET", path, null)).json();
|
|
93
|
+
/** A change's answer, which some routes leave empty. */
|
|
94
|
+
const send = async (
|
|
95
|
+
method: "POST" | "PUT" | "DELETE",
|
|
96
|
+
path: string,
|
|
97
|
+
json: unknown,
|
|
98
|
+
) => {
|
|
99
|
+
const text = await (
|
|
100
|
+
await call(method, path, json === null ? null : { json })
|
|
101
|
+
).text();
|
|
102
|
+
return text ? JSON.parse(text) : null;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const deploymentPath = (id: string) =>
|
|
106
|
+
`/api/deployments/${encodeURIComponent(id)}`;
|
|
107
|
+
|
|
108
|
+
export const api = {
|
|
109
|
+
session: (): Promise<Identity> => get("/api/session"),
|
|
110
|
+
signIn: (token: string): Promise<unknown> =>
|
|
111
|
+
send("POST", "/api/session", { token }),
|
|
112
|
+
signOut: (): Promise<unknown> => send("DELETE", "/api/session", null),
|
|
113
|
+
status: (): Promise<AgentStatus> => get("/status"),
|
|
114
|
+
|
|
115
|
+
deployments: (): Promise<DeploymentList> => get("/api/deployments"),
|
|
116
|
+
deployment: (id: string): Promise<DeploymentRecord> =>
|
|
117
|
+
get(deploymentPath(id)),
|
|
118
|
+
logs: async (id: string, after: number): Promise<DeploymentLogLine[]> =>
|
|
119
|
+
(await get(`${deploymentPath(id)}/logs?after=${after}`)).lines,
|
|
120
|
+
approve: (id: string, download: boolean): Promise<DeploymentRecord> =>
|
|
121
|
+
send("POST", `${deploymentPath(id)}/approve`, { download }),
|
|
122
|
+
cancel: (id: string): Promise<DeploymentRecord> =>
|
|
123
|
+
send("POST", `${deploymentPath(id)}/cancel`, null),
|
|
124
|
+
redeploy: (id: string, mode: DeploymentMode): Promise<DeploymentCreated> =>
|
|
125
|
+
send("POST", `${deploymentPath(id)}/redeploy`, { mode }),
|
|
126
|
+
rollback: (id: string): Promise<DeploymentCreated> =>
|
|
127
|
+
send("POST", `${deploymentPath(id)}/rollback`, null),
|
|
128
|
+
source: async (id: string): Promise<SourceFile[]> =>
|
|
129
|
+
(await get(`${deploymentPath(id)}/source`)).files,
|
|
130
|
+
sourceFile: async (id: string, path: string) =>
|
|
131
|
+
(
|
|
132
|
+
await call(
|
|
133
|
+
"GET",
|
|
134
|
+
`${deploymentPath(id)}/source/${path.split("/").map(encodeURIComponent).join("/")}`,
|
|
135
|
+
null,
|
|
136
|
+
)
|
|
137
|
+
).text(),
|
|
138
|
+
/** The URL of a deployment's live events, from the log line after `after`. */
|
|
139
|
+
eventsUrl: (id: string, after: number) =>
|
|
140
|
+
`${deploymentPath(id)}/events?after=${after}`,
|
|
141
|
+
|
|
142
|
+
environment: (): Promise<EnvironmentListing> => get("/api/environment"),
|
|
143
|
+
setEnvironment: (name: string, change: EnvironmentValue): Promise<unknown> =>
|
|
144
|
+
send("PUT", `/api/environment/${encodeURIComponent(name)}`, change),
|
|
145
|
+
deleteEnvironment: (name: string): Promise<unknown> =>
|
|
146
|
+
send("DELETE", `/api/environment/${encodeURIComponent(name)}`, null),
|
|
147
|
+
restartServer: (): Promise<unknown> =>
|
|
148
|
+
send("POST", "/api/server/restart", null),
|
|
149
|
+
restartController: (
|
|
150
|
+
restart: ControllerRestart,
|
|
151
|
+
): Promise<z.infer<typeof controllerRestartReplySchema>> =>
|
|
152
|
+
send("POST", controllerRestartPath, restart),
|
|
153
|
+
|
|
154
|
+
audit: (limit: number): Promise<Audit[]> => get(`/api/audit?limit=${limit}`),
|
|
155
|
+
access: (): Promise<AccessConfig> => get("/api/access"),
|
|
156
|
+
saveAccess: (access: AccessConfig): Promise<AccessConfig> =>
|
|
157
|
+
send("PUT", "/api/access", access),
|
|
158
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { errorMessage } from "../../errors";
|
|
2
|
+
import type { Identity, Role } from "../../protocol";
|
|
3
|
+
import { allows } from "./contract";
|
|
4
|
+
import { toast } from "./overlay";
|
|
5
|
+
|
|
6
|
+
/** What every page is given: who is signed in, and how to move between pages. */
|
|
7
|
+
export interface App {
|
|
8
|
+
identity: Identity;
|
|
9
|
+
/** Whether the signed-in role includes `required`. */
|
|
10
|
+
can(required: Role): boolean;
|
|
11
|
+
navigate(path: string): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const createApp = (
|
|
15
|
+
identity: Identity,
|
|
16
|
+
navigate: (path: string) => void,
|
|
17
|
+
): App => ({
|
|
18
|
+
identity,
|
|
19
|
+
can: (required) => allows(identity.role, required),
|
|
20
|
+
navigate,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** Runs a change, telling the user how it went: `done` on success, the agent's message on failure. */
|
|
24
|
+
export const attempt = async <T>(
|
|
25
|
+
run: () => Promise<T>,
|
|
26
|
+
done: string | null,
|
|
27
|
+
): Promise<T | null> => {
|
|
28
|
+
try {
|
|
29
|
+
const result = await run();
|
|
30
|
+
if (done) {
|
|
31
|
+
toast(done, "ok");
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
toast(errorMessage(error), "error");
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const deploymentHref = (id: string, tab = "") =>
|
|
41
|
+
`/deployments/${encodeURIComponent(id)}${tab ? `/${tab}` : ""}`;
|