@tsplc/agent 0.5.10 → 0.5.13

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.
@@ -1,11 +1,87 @@
1
1
  import { errorMessage } from "../../errors";
2
- import { api, type EnvironmentEntry } from "./api";
2
+ import type {
3
+ EnvironmentEntry,
4
+ EnvironmentListing,
5
+ EnvironmentValue,
6
+ } from "../../protocol";
7
+ import { api } from "./api";
3
8
  import { type App, attempt } from "./app";
4
9
  import { badge, dot, empty, fill, h, pageHeader, type View } from "./dom";
5
10
  import { confirm, dialog } from "./overlay";
6
11
 
7
- /** Asks for a secret's value, never showing one: the agent stores it and never sends it back. */
8
- const askValue = (name: string | null) => {
12
+ /** A row of the Environment page: a plain variable or a secret, and where it stands. */
13
+ interface Variable {
14
+ name: string;
15
+ secret: boolean;
16
+ /** A plain variable's value on this PC, or the running release's when only it has one. */
17
+ value: string | null;
18
+ /** Whether this PC holds it, so it can be edited and deleted here. */
19
+ stored: boolean;
20
+ status: { tone: string; text: string };
21
+ }
22
+
23
+ const applied = { tone: "ok", text: "Running" };
24
+ const pending = { tone: "waiting", text: "Deploy to apply" };
25
+
26
+ /** A secret stands applied once the running release reads it; the next deployment picks up a new one. */
27
+ const secretStatus = (entry: EnvironmentEntry) => {
28
+ if (!entry.set) {
29
+ return { tone: "error", text: "Not set" };
30
+ }
31
+ return entry.declared ? applied : pending;
32
+ };
33
+
34
+ /** The label of a row's edit button. */
35
+ const editLabel = (variable: Variable) => {
36
+ if (!variable.stored) {
37
+ return "Set";
38
+ }
39
+ return variable.secret ? "Replace" : "Edit";
40
+ };
41
+
42
+ /** The page's rows: this PC's plain variables, what the running release has besides, and the secrets. */
43
+ const variables = ({
44
+ values,
45
+ variables: stored,
46
+ secrets,
47
+ }: EnvironmentListing): Variable[] => {
48
+ const secretNames = new Set(secrets.map(({ name }) => name));
49
+ const plain = [...new Set([...Object.keys(stored), ...Object.keys(values)])]
50
+ .filter((name) => !secretNames.has(name))
51
+ .sort()
52
+ .map((name): Variable => {
53
+ const value = stored[name];
54
+ if (value === undefined) {
55
+ return {
56
+ name,
57
+ secret: false,
58
+ value: values[name] ?? null,
59
+ stored: false,
60
+ status: { tone: "ok", text: "Running, not set on this PC" },
61
+ };
62
+ }
63
+ return {
64
+ name,
65
+ secret: false,
66
+ value,
67
+ stored: true,
68
+ status: values[name] === value ? applied : pending,
69
+ };
70
+ });
71
+ const secret = secrets.map(
72
+ (entry): Variable => ({
73
+ name: entry.name,
74
+ secret: true,
75
+ value: null,
76
+ stored: entry.set,
77
+ status: secretStatus(entry),
78
+ }),
79
+ );
80
+ return [...plain, ...secret];
81
+ };
82
+
83
+ /** Asks for a variable's value, and whether it is a secret, which is never shown again. */
84
+ const askValue = (variable: Variable | null) => {
9
85
  const nameInput = h("input", {
10
86
  class: "input mono",
11
87
  placeholder: "NAME",
@@ -16,22 +92,34 @@ const askValue = (name: string | null) => {
16
92
  });
17
93
  const valueInput = h("input", {
18
94
  class: "input mono",
19
- type: "password",
20
- autocomplete: "new-password",
95
+ type: variable?.secret ? "password" : "text",
96
+ autocomplete: "off",
97
+ spellcheck: "false",
21
98
  placeholder: "Value",
99
+ value: variable && !variable.secret ? (variable.value ?? "") : "",
100
+ });
101
+ const secretInput = h("input", {
102
+ type: "checkbox",
103
+ checked: variable?.secret ?? false,
22
104
  });
23
- return dialog<{ name: string; value: string }>({
24
- title: name ? `Set ${name}` : "Add a variable",
105
+ const note = h("p", { class: "muted" });
106
+ const describe = () => {
107
+ valueInput.type = secretInput.checked ? "password" : "text";
108
+ note.textContent = secretInput.checked
109
+ ? "Kept on this PC with Bun.secrets and never shown again. The PLC's program gets it at each deployment, the server when it starts."
110
+ : "Shown here. The next tsplc deploy builds the PLC's program with it and starts the server with it.";
111
+ };
112
+ secretInput.addEventListener("change", describe);
113
+ describe();
114
+ return dialog<{ name: string; change: EnvironmentValue }>({
115
+ title: variable ? `Set ${variable.name}` : "Add a variable",
25
116
  body: [
26
- name
117
+ variable
27
118
  ? null
28
119
  : h("label", { class: "field" }, h("span", {}, "Name"), nameInput),
29
120
  h("label", { class: "field" }, h("span", {}, "Value"), valueInput),
30
- h(
31
- "p",
32
- { class: "muted" },
33
- "The value is stored on the agent's PC and never shown again. The server reads it when it next starts.",
34
- ),
121
+ h("label", { class: "check" }, secretInput, "Secret"),
122
+ note,
35
123
  ],
36
124
  actions: (close) => [
37
125
  h(
@@ -39,12 +127,15 @@ const askValue = (name: string | null) => {
39
127
  {
40
128
  class: "button primary",
41
129
  onclick: () => {
42
- const chosen = name ?? nameInput.value.trim();
43
- if (!chosen || (!nameInput.checkValidity() && !name)) {
130
+ const name = variable?.name ?? nameInput.value.trim();
131
+ if (!variable && (!name || !nameInput.checkValidity())) {
44
132
  nameInput.reportValidity();
45
133
  return;
46
134
  }
47
- close({ name: chosen, value: valueInput.value });
135
+ close({
136
+ name,
137
+ change: { secret: secretInput.checked, value: valueInput.value },
138
+ });
48
139
  },
49
140
  },
50
141
  "Save",
@@ -53,7 +144,7 @@ const askValue = (name: string | null) => {
53
144
  });
54
145
  };
55
146
 
56
- /** The release's secrets: which are declared and set, never their values. */
147
+ /** This PC's variables, plain and secret, and whether the running release has them. */
57
148
  export const environmentView = (app: App): View => {
58
149
  const admin = app.can("admin");
59
150
  const body = h("tbody");
@@ -67,8 +158,8 @@ export const environmentView = (app: App): View => {
67
158
  "tr",
68
159
  {},
69
160
  h("th", {}, "Name"),
70
- h("th", {}, "Declared"),
71
161
  h("th", {}, "Value"),
162
+ h("th", {}, "Status"),
72
163
  admin ? h("th", {}, "") : null,
73
164
  ),
74
165
  ),
@@ -76,29 +167,31 @@ export const environmentView = (app: App): View => {
76
167
  );
77
168
  const holder = h("div", {}, empty("Loading the environment…"));
78
169
 
79
- const set = async (name: string | null) => {
80
- const entered = await askValue(name);
170
+ const set = async (variable: Variable | null) => {
171
+ const entered = await askValue(variable);
81
172
  if (
82
173
  entered &&
83
174
  (await attempt(
84
- () => api.setEnvironment(entered.name, entered.value),
85
- `Saved ${entered.name}. Restart the server to apply it.`,
175
+ () => api.setEnvironment(entered.name, entered.change),
176
+ entered.change.secret
177
+ ? `Saved ${entered.name}. The next deployment applies it, and Restart server applies it to the server now.`
178
+ : `Saved ${entered.name}. Deploy to apply it to the PLC's program and the server.`,
86
179
  )) !== null
87
180
  ) {
88
181
  await load();
89
182
  }
90
183
  };
91
184
 
92
- const remove = async (entry: EnvironmentEntry) => {
185
+ const remove = async (variable: Variable) => {
93
186
  const confirmed = await confirm({
94
- title: `Delete ${entry.name}?`,
187
+ title: `Delete ${variable.name}?`,
95
188
  body: [
96
189
  h(
97
190
  "p",
98
191
  {},
99
- entry.declared
100
- ? "The release declares it, so its server can't start without it after the next restart."
101
- : "No release declares it.",
192
+ variable.secret
193
+ ? "A release that reads it can't deploy without it, and the server starts without it after the next restart."
194
+ : "The next deployment is built without it, unless tsplc.config.ts sets it.",
102
195
  ),
103
196
  ],
104
197
  confirm: "Delete",
@@ -107,25 +200,34 @@ export const environmentView = (app: App): View => {
107
200
  if (
108
201
  confirmed &&
109
202
  (await attempt(
110
- () => api.deleteEnvironment(entry.name),
111
- `Deleted ${entry.name}`,
203
+ () => api.deleteEnvironment(variable.name),
204
+ `Deleted ${variable.name}`,
112
205
  )) !== null
113
206
  ) {
114
207
  await load();
115
208
  }
116
209
  };
117
210
 
118
- const row = (entry: EnvironmentEntry) =>
211
+ const row = (variable: Variable) =>
119
212
  h(
120
213
  "tr",
121
214
  {},
122
- h("td", { class: "mono" }, entry.name),
123
215
  h(
124
216
  "td",
125
217
  {},
126
- entry.declared
127
- ? badge("Declared", "neutral")
128
- : h("span", { class: "muted" }, "Not declared"),
218
+ h("span", { class: "mono" }, variable.name),
219
+ variable.secret ? h("span", {}, " ", badge("Secret")) : null,
220
+ ),
221
+ h(
222
+ "td",
223
+ { class: "mono" },
224
+ variable.secret
225
+ ? h(
226
+ "span",
227
+ { class: "muted" },
228
+ variable.stored ? "••••••••" : "Not set",
229
+ )
230
+ : variable.value,
129
231
  ),
130
232
  h(
131
233
  "td",
@@ -133,10 +235,8 @@ export const environmentView = (app: App): View => {
133
235
  h(
134
236
  "span",
135
237
  { class: "state" },
136
- dot(entry.set ? "ok" : entry.declared ? "error" : "idle"),
137
- entry.set
138
- ? h("span", { class: "mono muted" }, "••••••••")
139
- : "Not set",
238
+ dot(variable.status.tone),
239
+ variable.status.text,
140
240
  ),
141
241
  ),
142
242
  admin
@@ -145,15 +245,15 @@ export const environmentView = (app: App): View => {
145
245
  { class: "row-actions" },
146
246
  h(
147
247
  "button",
148
- { class: "button small", onclick: () => void set(entry.name) },
149
- entry.set ? "Replace" : "Set",
248
+ { class: "button small", onclick: () => void set(variable) },
249
+ editLabel(variable),
150
250
  ),
151
- entry.set
251
+ variable.stored
152
252
  ? h(
153
253
  "button",
154
254
  {
155
255
  class: "button small danger-text",
156
- onclick: () => void remove(entry),
256
+ onclick: () => void remove(variable),
157
257
  },
158
258
  "Delete",
159
259
  )
@@ -164,13 +264,15 @@ export const environmentView = (app: App): View => {
164
264
 
165
265
  const load = async () => {
166
266
  try {
167
- const entries = await api.environment();
168
- fill(body, ...entries.map(row));
267
+ const rows = variables(await api.environment());
268
+ fill(body, ...rows.map(row));
169
269
  fill(
170
270
  holder,
171
- entries.length
271
+ rows.length
172
272
  ? table
173
- : empty("The deployed release declares no secrets."),
273
+ : empty(
274
+ "No variables yet. Add one, and the next deployment builds with it.",
275
+ ),
174
276
  );
175
277
  } catch (error) {
176
278
  fill(holder, h("p", { class: "error-text" }, errorMessage(error)));
@@ -184,7 +286,7 @@ export const environmentView = (app: App): View => {
184
286
  h(
185
287
  "p",
186
288
  {},
187
- "The line's server stops and starts again with the environment as it is now. The controller keeps running.",
289
+ "The line's server stops and starts again with the running release's values and the secrets as they are now. The controller keeps running.",
188
290
  ),
189
291
  ],
190
292
  confirm: "Restart",
@@ -202,7 +304,7 @@ export const environmentView = (app: App): View => {
202
304
  { class: "page" },
203
305
  pageHeader(
204
306
  "Environment",
205
- "The secrets the release's server reads. Values are write-only, and new ones apply when the server restarts.",
307
+ "The variables the PLC's program and the line's server read. tsplc deploy builds with them, over the values in tsplc.config.ts.",
206
308
  app.can("deployer")
207
309
  ? h(
208
310
  "button",
@@ -1,5 +1,6 @@
1
1
  import { errorMessage } from "../../errors";
2
2
  import type { Identity } from "../../protocol";
3
+ import { restartController } from "./actions";
3
4
  import { agentVersion, api } from "./api";
4
5
  import { type App, attempt } from "./app";
5
6
  import { badge, fill, h, icon, icons } from "./dom";
@@ -34,6 +35,25 @@ const themeButton = () => {
34
35
  return button;
35
36
  };
36
37
 
38
+ /** Resets the application and starts it again, from any page; busy until the controller answers. */
39
+ const restartButton = () => {
40
+ const button = h(
41
+ "button",
42
+ {
43
+ class: "button small",
44
+ title: "Reset the application warm or cold, then start it",
45
+ },
46
+ "Reset and start",
47
+ );
48
+ button.addEventListener("click", () => {
49
+ void restartController((busy) => {
50
+ button.disabled = busy;
51
+ button.textContent = busy ? "Restarting…" : "Reset and start";
52
+ });
53
+ });
54
+ return button;
55
+ };
56
+
37
57
  /** Whether the identity signed in with a cookie, which signing out clears; Tailscale and the agent's PC can't sign out. */
38
58
  const signedInByToken = (identity: Identity) => identity.via === "token";
39
59
 
@@ -100,6 +120,7 @@ export const shell = (app: App) => {
100
120
  ),
101
121
  h("span", { class: "slash" }, "/"),
102
122
  where,
123
+ app.can("deployer") ? restartButton() : null,
103
124
  h(
104
125
  "div",
105
126
  { class: "topbar-end" },
package/src/deploy.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import {
2
2
  controllerBaseline,
3
3
  findControllerLogin,
4
+ type ResetKind,
5
+ restartWarmController,
4
6
  reviewWarmController,
5
7
  settleWarmController,
6
8
  startWarmController,
@@ -163,8 +165,42 @@ export const createDeployments = async (context: {
163
165
  });
164
166
  await runner.start();
165
167
 
168
+ const deployedConfig = async () => {
169
+ const deployed = await state.release();
170
+ const target = await state.target();
171
+ if (!deployed || !target) {
172
+ throw new AgentError("Nothing is deployed to the controller yet", 409);
173
+ }
174
+ return controllerConfig(
175
+ await loadRelease(state.paths.releases, deployed.id),
176
+ target,
177
+ state.paths.controller,
178
+ context.codesys,
179
+ );
180
+ };
181
+
166
182
  return {
167
183
  runner,
184
+ /**
185
+ * Resets the deployed application and starts it again through the CODESYS that stays running
186
+ * for deployments, as the agent's one operation, so it takes seconds and needs no login.
187
+ */
188
+ restart: (reset: ResetKind) =>
189
+ context.operations.exclusive("a controller restart", async () => {
190
+ if (await findControllerLogin(loginContainer)) {
191
+ throw new AgentError(
192
+ "A login opened meanwhile; restart again to use it",
193
+ 409,
194
+ );
195
+ }
196
+ const config = await deployedConfig();
197
+ await requireBuilder();
198
+ await ensureGateway();
199
+ await warming;
200
+ return restartWarmController(config, deployContainer, reset, {
201
+ credentials: await controllerCredentials(null),
202
+ });
203
+ }),
168
204
  /**
169
205
  * Stops the warm container once it has committed the last deployment, so an upgraded agent
170
206
  * starts one running its own code and none keeps the installed package's files in use.