@tsplc/tmp-agent 0.0.0-stage → 0.6.3
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/codesys.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
builderImageStatus,
|
|
4
|
+
captureDocker,
|
|
5
|
+
gatewayImage,
|
|
6
|
+
setupReason,
|
|
7
|
+
} from "@tsplc/build";
|
|
8
|
+
import { gatewayPort, resolveConfig } from "@tsplc/config/build";
|
|
9
|
+
import { AgentError, errorMessage } from "./errors";
|
|
10
|
+
import type { Credentials, Target } from "./protocol";
|
|
11
|
+
import { loadRelease, manifestFile, type StoredRelease } from "./releases";
|
|
12
|
+
import type { AgentState } from "./state";
|
|
13
|
+
|
|
14
|
+
/** How the agent runs CODESYS, whichever release it deploys. */
|
|
15
|
+
export interface CodesysSettings {
|
|
16
|
+
/** Downloads without a baseline import their POUs in one PLCopenXML import; experimental. */
|
|
17
|
+
bulkImport: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The gateway reaches controllers through this Docker network. */
|
|
21
|
+
const network = "tsplc-agent";
|
|
22
|
+
/**
|
|
23
|
+
* Builder containers reach the gateway by name on this internal network, which has no internet.
|
|
24
|
+
* With internet, CODESYS asks a certificate authority about each DLL it loads, without caching
|
|
25
|
+
* the answer, and opening a project takes minutes instead of seconds.
|
|
26
|
+
*/
|
|
27
|
+
const codesysNetwork = "tsplc-agent-codesys";
|
|
28
|
+
const gateway = { host: "tsplc-agent-gateway", port: gatewayPort };
|
|
29
|
+
export const loginContainer = "tsplc-agent-login";
|
|
30
|
+
/** Keeps CODESYS running between deployments, so each skips starting it. */
|
|
31
|
+
export const deployContainer = "tsplc-agent-deploy";
|
|
32
|
+
/** Every Docker container and network the agent creates: one agent per Docker server uses them. */
|
|
33
|
+
export const agentDockerResources = {
|
|
34
|
+
containers: [gateway.host, deployContainer, loginContainer],
|
|
35
|
+
networks: [network, codesysNetwork],
|
|
36
|
+
};
|
|
37
|
+
/** CODESYS reads the controller user from these for one operation; the agent never stores it. */
|
|
38
|
+
const credentialNames = {
|
|
39
|
+
usernameEnv: "TSPLC_DEVICE_USERNAME",
|
|
40
|
+
passwordEnv: "TSPLC_DEVICE_PASSWORD",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Network and gateway commands return at once; one still running after this has hung. */
|
|
44
|
+
const dockerTimeout = 60_000;
|
|
45
|
+
|
|
46
|
+
const docker = async (...args: string[]) =>
|
|
47
|
+
(await captureDocker(args, dockerTimeout)).stdout.trim();
|
|
48
|
+
|
|
49
|
+
const ensureNetwork = async (name: string, ...options: string[]) => {
|
|
50
|
+
if (
|
|
51
|
+
!(await docker("network", "ls", "--quiet", "--filter", `name=^${name}$`))
|
|
52
|
+
) {
|
|
53
|
+
await docker("network", "create", ...options, name);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Why the images can't serve a deployment or login, or null when they can. Containers run the
|
|
59
|
+
* installed tsPLC's code, so only a new image recipe or a missing image needs setup.
|
|
60
|
+
*/
|
|
61
|
+
const setupNeeded = async () => {
|
|
62
|
+
const status = await builderImageStatus();
|
|
63
|
+
const reason =
|
|
64
|
+
setupReason(status) ??
|
|
65
|
+
((await docker("image", "ls", "--quiet", gatewayImage))
|
|
66
|
+
? null
|
|
67
|
+
: `the CODESYS gateway image ${gatewayImage} is missing`);
|
|
68
|
+
return { status, reason };
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Checks that the images can serve a deployment or login, naming what to run on the plant PC
|
|
73
|
+
* when they can't, and returns the builder image's status.
|
|
74
|
+
*/
|
|
75
|
+
export const requireBuilder = async () => {
|
|
76
|
+
const { status, reason } = await setupNeeded().catch((cause: unknown) => {
|
|
77
|
+
throw new AgentError(
|
|
78
|
+
`Docker doesn't answer on the plant PC; start Docker Desktop there: ${errorMessage(cause)}`,
|
|
79
|
+
503,
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
if (reason) {
|
|
83
|
+
throw new AgentError(
|
|
84
|
+
`The plant PC needs tsplc-agent setup: ${reason}. Run tsplc-agent setup there; the line's server keeps running meanwhile`,
|
|
85
|
+
409,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return status;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** What the agent says about its images when it starts. */
|
|
92
|
+
export const describeBuilder = async () => {
|
|
93
|
+
const { status, reason } = await setupNeeded();
|
|
94
|
+
if (reason) {
|
|
95
|
+
return `Deployments and logins need tsplc-agent setup first: ${reason}`;
|
|
96
|
+
}
|
|
97
|
+
if (status.kind === "unverified") {
|
|
98
|
+
return `Warning: ${status.reason}. Deployments use it; run tsplc-agent setup once to rebuild and label it`;
|
|
99
|
+
}
|
|
100
|
+
return "The CODESYS images match this tsPLC";
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/** Removes container `name`, which need not exist. */
|
|
104
|
+
export const removeContainer = (name: string) => docker("rm", "--force", name);
|
|
105
|
+
|
|
106
|
+
/** The gateway container stays up between operations, as a CODESYS gateway service would. */
|
|
107
|
+
export const ensureGateway = async () => {
|
|
108
|
+
await ensureNetwork(network);
|
|
109
|
+
await ensureNetwork(codesysNetwork, "--internal");
|
|
110
|
+
if (
|
|
111
|
+
!(await docker(
|
|
112
|
+
"container",
|
|
113
|
+
"ls",
|
|
114
|
+
"--quiet",
|
|
115
|
+
"--filter",
|
|
116
|
+
`name=^${gateway.host}$`,
|
|
117
|
+
))
|
|
118
|
+
) {
|
|
119
|
+
await docker("rm", "--force", gateway.host);
|
|
120
|
+
await docker(
|
|
121
|
+
"run",
|
|
122
|
+
"--detach",
|
|
123
|
+
"--restart",
|
|
124
|
+
"unless-stopped",
|
|
125
|
+
"--platform",
|
|
126
|
+
"linux/amd64",
|
|
127
|
+
"--network",
|
|
128
|
+
network,
|
|
129
|
+
"--name",
|
|
130
|
+
gateway.host,
|
|
131
|
+
gatewayImage,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
// Gateways started by earlier agents are only on `network`.
|
|
135
|
+
const networks = await docker(
|
|
136
|
+
"inspect",
|
|
137
|
+
"--format",
|
|
138
|
+
"{{json .NetworkSettings.Networks}}",
|
|
139
|
+
gateway.host,
|
|
140
|
+
);
|
|
141
|
+
if (!(codesysNetwork in JSON.parse(networks))) {
|
|
142
|
+
await docker("network", "connect", codesysNetwork, gateway.host);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** The controller user as the variables one operation's container passes to CODESYS. */
|
|
147
|
+
export const credentialEnvironment = (credentials: Credentials) => ({
|
|
148
|
+
[credentialNames.usernameEnv]: credentials.username,
|
|
149
|
+
[credentialNames.passwordEnv]: credentials.password,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The release's build config, aimed at `target` through the agent's gateway, with the agent's own
|
|
154
|
+
* CODESYS settings. The download baseline in `controllerDirectory` outlives releases: an online
|
|
155
|
+
* change compares against the last download.
|
|
156
|
+
*/
|
|
157
|
+
export const controllerConfig = (
|
|
158
|
+
release: StoredRelease,
|
|
159
|
+
target: Target,
|
|
160
|
+
controllerDirectory: string,
|
|
161
|
+
settings: CodesysSettings,
|
|
162
|
+
) => ({
|
|
163
|
+
...resolveConfig(
|
|
164
|
+
{
|
|
165
|
+
...release.manifest.config,
|
|
166
|
+
...(settings.bulkImport && { bulkImport: true }),
|
|
167
|
+
online: {
|
|
168
|
+
network: codesysNetwork,
|
|
169
|
+
gateway,
|
|
170
|
+
device: target,
|
|
171
|
+
credentials: credentialNames,
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
join(release.directory, manifestFile),
|
|
175
|
+
),
|
|
176
|
+
outDir: controllerDirectory,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
/** The deployed release's build config for the selected controller, or null before a deployment to one. */
|
|
180
|
+
export const deployedControllerConfig = async (
|
|
181
|
+
state: AgentState,
|
|
182
|
+
settings: CodesysSettings,
|
|
183
|
+
) => {
|
|
184
|
+
const deployed = await state.release();
|
|
185
|
+
const target = await state.target();
|
|
186
|
+
if (!deployed || !target) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
return controllerConfig(
|
|
190
|
+
await loadRelease(state.paths.releases, deployed.id),
|
|
191
|
+
target,
|
|
192
|
+
state.paths.controller,
|
|
193
|
+
settings,
|
|
194
|
+
);
|
|
195
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { basename, dirname, resolve } from "node:path";
|
|
3
|
+
import { envName } from "@tsplc/config/build";
|
|
4
|
+
import { dashboardHeader } from "@tsplc/debug/api";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { auditLimits } from "../audit";
|
|
7
|
+
import { errorMessage } from "../errors";
|
|
8
|
+
import {
|
|
9
|
+
agentVersionHeader,
|
|
10
|
+
allows,
|
|
11
|
+
cancelableStates,
|
|
12
|
+
debuggerMount,
|
|
13
|
+
deploymentPagePath,
|
|
14
|
+
deploymentsApiPath,
|
|
15
|
+
finalStates,
|
|
16
|
+
type Role,
|
|
17
|
+
roles,
|
|
18
|
+
statusPath,
|
|
19
|
+
} from "../protocol";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The pages a browser loads, each with the paths under it, and the role its link shows to; `/` is the
|
|
23
|
+
* first. They hold no data, which they fetch from `/api` as whoever is signed in.
|
|
24
|
+
*/
|
|
25
|
+
const pages = [
|
|
26
|
+
{ path: "/deployments", label: "Deployments", role: "viewer" },
|
|
27
|
+
{ path: "/environment", label: "Environment", role: "viewer" },
|
|
28
|
+
{ path: "/audit", label: "Audit", role: "viewer" },
|
|
29
|
+
{ path: "/access", label: "Access", role: "admin" },
|
|
30
|
+
] as const satisfies readonly { path: string; label: string; role: Role }[];
|
|
31
|
+
|
|
32
|
+
/** Where the pages' script and stylesheet are served; like the pages, they hold no data. */
|
|
33
|
+
export const dashboardAssets = "/dashboard-assets/";
|
|
34
|
+
|
|
35
|
+
/** The browser's local storage key for the chosen theme, read before the page first paints. */
|
|
36
|
+
const themeKey = "tsplc-theme";
|
|
37
|
+
|
|
38
|
+
const page = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>tsPLC</title><script>document.documentElement.dataset.theme=localStorage.getItem(${JSON.stringify(themeKey)})||""</script><link rel="stylesheet" href="${dashboardAssets}dashboard.css"><script type="module" src="${dashboardAssets}main.js"></script></head><body><main id="app"></main></body></html>`;
|
|
39
|
+
|
|
40
|
+
/** The config's rule for an environment variable's name, as an input's `pattern`. */
|
|
41
|
+
const envNamePattern = () => {
|
|
42
|
+
const { pattern } = z.toJSONSchema(envName);
|
|
43
|
+
if (pattern === undefined) {
|
|
44
|
+
throw new Error("The config's environment variable name has no pattern");
|
|
45
|
+
}
|
|
46
|
+
return pattern;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const dashboardContract = () => ({
|
|
50
|
+
roles,
|
|
51
|
+
permissions: Object.fromEntries(
|
|
52
|
+
roles.map((role) => [
|
|
53
|
+
role,
|
|
54
|
+
Object.fromEntries(
|
|
55
|
+
roles.map((required) => [required, allows(role, required)]),
|
|
56
|
+
),
|
|
57
|
+
]),
|
|
58
|
+
),
|
|
59
|
+
pages,
|
|
60
|
+
themeKey,
|
|
61
|
+
auditLimits,
|
|
62
|
+
finalStates,
|
|
63
|
+
cancelableStates,
|
|
64
|
+
envNamePattern: envNamePattern(),
|
|
65
|
+
dashboardHeader,
|
|
66
|
+
agentVersionHeader,
|
|
67
|
+
debuggerMount,
|
|
68
|
+
statusPath,
|
|
69
|
+
deploymentsApiPath,
|
|
70
|
+
/** `deploymentPagePath`'s prefix, which the deployment's id follows. */
|
|
71
|
+
deploymentPages: deploymentPagePath(""),
|
|
72
|
+
});
|
|
73
|
+
export type DashboardContract = ReturnType<typeof dashboardContract>;
|
|
74
|
+
|
|
75
|
+
interface Asset {
|
|
76
|
+
text: string;
|
|
77
|
+
type: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const bundle = async () => {
|
|
81
|
+
// Resolved through the package, so a published `dist/` bundle finds the shipped sources.
|
|
82
|
+
const sources = resolve(
|
|
83
|
+
dirname(createRequire(import.meta.url).resolve("@tsplc/agent")),
|
|
84
|
+
"../src/dashboard/ui",
|
|
85
|
+
);
|
|
86
|
+
const result = await Bun.build({
|
|
87
|
+
entrypoints: [
|
|
88
|
+
resolve(sources, "main.ts"),
|
|
89
|
+
resolve(sources, "dashboard.css"),
|
|
90
|
+
],
|
|
91
|
+
target: "browser",
|
|
92
|
+
// Workspace packages resolve to their sources, as they do for the host, so no build is needed first.
|
|
93
|
+
conditions: ["bun"],
|
|
94
|
+
minify: true,
|
|
95
|
+
naming: "[name].[ext]",
|
|
96
|
+
define: { TSPLC_CONTRACT: JSON.stringify(dashboardContract()) },
|
|
97
|
+
});
|
|
98
|
+
if (!result.success) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Bundling the dashboard failed:\n${result.logs.map(String).join("\n")}`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const assets = new Map<string, Asset>();
|
|
104
|
+
for (const output of result.outputs) {
|
|
105
|
+
assets.set(basename(output.path), {
|
|
106
|
+
text: await output.text(),
|
|
107
|
+
type: output.type,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return assets;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const isPage = (pathname: string) =>
|
|
114
|
+
pathname === "/" ||
|
|
115
|
+
pages.some(
|
|
116
|
+
({ path }) => pathname === path || pathname.startsWith(`${path}/`),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The agent's dashboard: its pages, answered for any path under them, and their script and
|
|
121
|
+
* stylesheet, bundled on first request and then kept.
|
|
122
|
+
*/
|
|
123
|
+
export const createDashboard = () => {
|
|
124
|
+
let assets: Promise<Map<string, Asset>> | undefined;
|
|
125
|
+
return {
|
|
126
|
+
handle: async (request: Request): Promise<Response | undefined> => {
|
|
127
|
+
const { pathname } = new URL(request.url);
|
|
128
|
+
if (request.method !== "GET") {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
if (isPage(pathname)) {
|
|
132
|
+
return new Response(page, {
|
|
133
|
+
headers: {
|
|
134
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
135
|
+
"Cache-Control": "no-cache",
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (!pathname.startsWith(dashboardAssets)) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
assets ??= bundle();
|
|
143
|
+
try {
|
|
144
|
+
const asset = (await assets).get(
|
|
145
|
+
pathname.slice(dashboardAssets.length),
|
|
146
|
+
);
|
|
147
|
+
if (!asset) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
return new Response(asset.text, {
|
|
151
|
+
headers: { "Content-Type": asset.type, "Cache-Control": "no-cache" },
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {
|
|
154
|
+
assets = undefined;
|
|
155
|
+
return new Response(errorMessage(error), { status: 500 });
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
};
|
|
@@ -0,0 +1,191 @@
|
|
|
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 { contract } 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
|
+
...contract.roles.map((role) =>
|
|
22
|
+
h("option", { value: role, selected: role === selected }, role),
|
|
23
|
+
),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const chosenRole = (select: HTMLSelectElement) =>
|
|
27
|
+
contract.roles.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
|
+
const element = h(
|
|
33
|
+
"div",
|
|
34
|
+
{ class: "page" },
|
|
35
|
+
pageHeader(
|
|
36
|
+
"Access",
|
|
37
|
+
"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.",
|
|
38
|
+
),
|
|
39
|
+
holder,
|
|
40
|
+
);
|
|
41
|
+
if (!app.can("admin")) {
|
|
42
|
+
fill(holder, empty("Only an admin can see and change who has which role."));
|
|
43
|
+
return { element };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const render = (access: AccessConfig) => {
|
|
47
|
+
const logins = Object.entries(access.roles).sort(([a], [b]) =>
|
|
48
|
+
a.localeCompare(b),
|
|
49
|
+
);
|
|
50
|
+
const rows = logins.map(([login, role]) => ({
|
|
51
|
+
login,
|
|
52
|
+
select: roleSelect(role, { none: null }),
|
|
53
|
+
}));
|
|
54
|
+
const tailnet = roleSelect(access.tailnetDefault, { none: "No access" });
|
|
55
|
+
const newLogin = h("input", {
|
|
56
|
+
class: "input",
|
|
57
|
+
type: "email",
|
|
58
|
+
placeholder: "someone@example.com",
|
|
59
|
+
autocomplete: "off",
|
|
60
|
+
});
|
|
61
|
+
const newRole = roleSelect("viewer", { none: null });
|
|
62
|
+
const save = async (next: AccessConfig) => {
|
|
63
|
+
const saved = await attempt(() => api.saveAccess(next), {
|
|
64
|
+
done: "Saved access",
|
|
65
|
+
});
|
|
66
|
+
if (saved) {
|
|
67
|
+
render(saved);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const current = (): AccessConfig => ({
|
|
71
|
+
roles: Object.fromEntries(
|
|
72
|
+
rows.flatMap(({ login, select }) => {
|
|
73
|
+
const role = chosenRole(select);
|
|
74
|
+
return role ? [[login, role]] : [];
|
|
75
|
+
}),
|
|
76
|
+
),
|
|
77
|
+
tailnetDefault: chosenRole(tailnet),
|
|
78
|
+
});
|
|
79
|
+
fill(
|
|
80
|
+
holder,
|
|
81
|
+
h(
|
|
82
|
+
"section",
|
|
83
|
+
{ class: "card" },
|
|
84
|
+
h("h3", {}, "Tailnet default"),
|
|
85
|
+
h(
|
|
86
|
+
"p",
|
|
87
|
+
{ class: "muted" },
|
|
88
|
+
"The role of a member of the tailnet without a role of their own.",
|
|
89
|
+
),
|
|
90
|
+
tailnet,
|
|
91
|
+
),
|
|
92
|
+
h(
|
|
93
|
+
"section",
|
|
94
|
+
{ class: "card" },
|
|
95
|
+
h("h3", {}, "Roles by login"),
|
|
96
|
+
h(
|
|
97
|
+
"p",
|
|
98
|
+
{ class: "muted" },
|
|
99
|
+
"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.",
|
|
100
|
+
),
|
|
101
|
+
rows.length
|
|
102
|
+
? h(
|
|
103
|
+
"table",
|
|
104
|
+
{ class: "table" },
|
|
105
|
+
h(
|
|
106
|
+
"thead",
|
|
107
|
+
{},
|
|
108
|
+
h(
|
|
109
|
+
"tr",
|
|
110
|
+
{},
|
|
111
|
+
h("th", {}, "Tailscale login"),
|
|
112
|
+
h("th", {}, "Role"),
|
|
113
|
+
h("th", {}, ""),
|
|
114
|
+
),
|
|
115
|
+
),
|
|
116
|
+
h(
|
|
117
|
+
"tbody",
|
|
118
|
+
{},
|
|
119
|
+
...rows.map(({ login, select }) =>
|
|
120
|
+
h(
|
|
121
|
+
"tr",
|
|
122
|
+
{},
|
|
123
|
+
h("td", { class: "mono" }, login),
|
|
124
|
+
h("td", {}, select),
|
|
125
|
+
h(
|
|
126
|
+
"td",
|
|
127
|
+
{ class: "row-actions" },
|
|
128
|
+
h(
|
|
129
|
+
"button",
|
|
130
|
+
{
|
|
131
|
+
class: "button small danger-text",
|
|
132
|
+
onclick: () => {
|
|
133
|
+
const next = current();
|
|
134
|
+
void save({
|
|
135
|
+
...next,
|
|
136
|
+
roles: Object.fromEntries(
|
|
137
|
+
Object.entries(next.roles).filter(
|
|
138
|
+
([name]) => name !== login,
|
|
139
|
+
),
|
|
140
|
+
),
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
"Remove",
|
|
145
|
+
),
|
|
146
|
+
),
|
|
147
|
+
),
|
|
148
|
+
),
|
|
149
|
+
),
|
|
150
|
+
)
|
|
151
|
+
: empty("No login has a role yet."),
|
|
152
|
+
h(
|
|
153
|
+
"form",
|
|
154
|
+
{
|
|
155
|
+
class: "inline-form",
|
|
156
|
+
onsubmit: (event) => {
|
|
157
|
+
event.preventDefault();
|
|
158
|
+
const login = newLogin.value.trim();
|
|
159
|
+
const role = chosenRole(newRole);
|
|
160
|
+
if (!login || !role) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const next = current();
|
|
164
|
+
void save({ ...next, roles: { ...next.roles, [login]: role } });
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
newLogin,
|
|
168
|
+
newRole,
|
|
169
|
+
h("button", { class: "button", type: "submit" }, "Add"),
|
|
170
|
+
),
|
|
171
|
+
),
|
|
172
|
+
h(
|
|
173
|
+
"div",
|
|
174
|
+
{ class: "form-actions" },
|
|
175
|
+
h(
|
|
176
|
+
"button",
|
|
177
|
+
{ class: "button primary", onclick: () => void save(current()) },
|
|
178
|
+
"Save changes",
|
|
179
|
+
),
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
void api
|
|
185
|
+
.access()
|
|
186
|
+
.then(render, (error: unknown) =>
|
|
187
|
+
fill(holder, h("p", { class: "error-text" }, errorMessage(error))),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
return { element };
|
|
191
|
+
};
|