@celljs/ta-gateway 4.19.0 → 4.21.0
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/esm/node/docker-engine.d.ts +14 -0
- package/esm/node/docker-engine.js +21 -2
- package/esm/node/docker-sandbox.d.ts +22 -4
- package/esm/node/docker-sandbox.js +104 -9
- package/esm/node/environments.d.ts +25 -2
- package/esm/node/environments.js +106 -13
- package/esm/node/gateway-files.d.ts +18 -0
- package/esm/node/gateway-files.js +36 -0
- package/esm/node/gateway-policy.d.ts +5 -1
- package/esm/node/gateway-policy.js +9 -0
- package/esm/node/gateway-server.d.ts +5 -11
- package/esm/node/gateway-server.js +22 -27
- package/esm/node/gateway-service-util.js +12 -0
- package/esm/node/gateway-service.d.ts +8 -1
- package/esm/node/gateway-service.js +37 -2
- package/esm/node/sandbox-provider.d.ts +24 -0
- package/esm/node/sandbox-updates.d.ts +56 -0
- package/esm/node/sandbox-updates.js +104 -0
- package/esm/node/tenant-store.d.ts +2 -0
- package/lib/node/docker-engine.d.ts +14 -0
- package/lib/node/docker-engine.js +21 -2
- package/lib/node/docker-sandbox.d.ts +22 -4
- package/lib/node/docker-sandbox.js +104 -9
- package/lib/node/environments.d.ts +25 -2
- package/lib/node/environments.js +106 -13
- package/lib/node/gateway-files.d.ts +18 -0
- package/lib/node/gateway-files.js +36 -0
- package/lib/node/gateway-policy.d.ts +5 -1
- package/lib/node/gateway-policy.js +9 -0
- package/lib/node/gateway-server.d.ts +5 -11
- package/lib/node/gateway-server.js +22 -27
- package/lib/node/gateway-service-util.js +12 -0
- package/lib/node/gateway-service.d.ts +8 -1
- package/lib/node/gateway-service.js +37 -2
- package/lib/node/sandbox-provider.d.ts +24 -0
- package/lib/node/sandbox-updates.d.ts +57 -0
- package/lib/node/sandbox-updates.js +108 -0
- package/lib/node/tenant-store.d.ts +2 -0
- package/package.json +5 -5
|
@@ -95,6 +95,17 @@ var GatewayServerUtil;
|
|
|
95
95
|
return given;
|
|
96
96
|
}
|
|
97
97
|
GatewayServerUtil.deploymentEnv = deploymentEnv;
|
|
98
|
+
/**
|
|
99
|
+
* What makes a sandbox a Ta in the cloud of its account (TaAccountService.cloud): where it reaches this gateway's
|
|
100
|
+
* accounts, and the address devices know the gateway by, the only account it joins.
|
|
101
|
+
*/
|
|
102
|
+
function accountEnv(env, url) {
|
|
103
|
+
var _a;
|
|
104
|
+
var _b;
|
|
105
|
+
const service = (_a = ((_b = env.TA_GATEWAY_URL) !== null && _b !== void 0 ? _b : env.TA_PUBLIC_URL)) === null || _a === void 0 ? void 0 : _a.trim().replace(/\/+$/, '');
|
|
106
|
+
return { TA_ACCOUNT_KIND: 'cloud', TA_ACCOUNT_URL: url, ...(service ? { TA_ACCOUNT_SERVICE: service } : {}) };
|
|
107
|
+
}
|
|
108
|
+
GatewayServerUtil.accountEnv = accountEnv;
|
|
98
109
|
/** Where another Ta reaches the exec server of its account's sandbox. */
|
|
99
110
|
GatewayServerUtil.EXEC_PATH = '/api/exec';
|
|
100
111
|
/** What a page is told when its Ta does not start for its account's limits; TaGatewayInfo.quota says it in the page's words. */
|
|
@@ -125,25 +136,6 @@ var GatewayServerUtil;
|
|
|
125
136
|
return (0, node_path_1.join)(root, 'index.html');
|
|
126
137
|
}
|
|
127
138
|
GatewayServerUtil.file = file;
|
|
128
|
-
/**
|
|
129
|
-
* Where a request of `/api/files` goes in the Ta of its tenant: undefined for one whose path, once its dots
|
|
130
|
-
* are resolved (`/api/files/../../x`, `%2e%2e`), is not of `/api/files` there, which would reach any
|
|
131
|
-
* path of that address, and of any address a tenant names as its own Ta service.
|
|
132
|
-
*/
|
|
133
|
-
function filesTarget(requestUrl, endpoint) {
|
|
134
|
-
const target = new URL(requestUrl, endpoint);
|
|
135
|
-
return target.origin === new URL(endpoint).origin && target.pathname.startsWith(`${ta_protocol_1.TA_FILES_PATH}/`) ? target : undefined;
|
|
136
|
-
}
|
|
137
|
-
GatewayServerUtil.filesTarget = filesTarget;
|
|
138
|
-
/**
|
|
139
|
-
* The headers of an answer of the Ta of a tenant, as the browser gets them: without cookies, which a Ta
|
|
140
|
-
* service of the user's (or what runs in a sandbox) would otherwise set on the gateway's origin.
|
|
141
|
-
*/
|
|
142
|
-
function filesHeaders(headers) {
|
|
143
|
-
const { 'set-cookie': _cookies, ...rest } = headers;
|
|
144
|
-
return rest;
|
|
145
|
-
}
|
|
146
|
-
GatewayServerUtil.filesHeaders = filesHeaders;
|
|
147
139
|
})(GatewayServerUtil || (exports.GatewayServerUtil = GatewayServerUtil = {}));
|
|
148
140
|
/**
|
|
149
141
|
* `ta gateway`: one sign-in for many people, each with a Ta of their own. An account that signs in is a
|
|
@@ -175,7 +167,11 @@ class GatewayServer {
|
|
|
175
167
|
this.flow = new sign_in_flow_1.SignInFlow(sign_in_provider_1.SignInProviders.fromEnv(env), new access_store_1.AccessStore((0, node_path_1.join)(options.dir, 'access.json'), allowed), sign_in_provider_1.SignInProviders.publicUrl(env), sign_in_provider_1.SignInProviders.polled(env));
|
|
176
168
|
this.tenants = new tenant_store_1.TenantStore(options.dir);
|
|
177
169
|
// The devices of each account (docs/design/ta-account.md 3), its tokens those of this gateway's sign-in.
|
|
178
|
-
this.accounts = new node_1.RelayServerAccounts(options.dir, {
|
|
170
|
+
this.accounts = new node_1.RelayServerAccounts(options.dir, {
|
|
171
|
+
authenticate: async (token) => GatewayServerUtil.accountOf(this.caller(token)),
|
|
172
|
+
// A computer the user scanned joins their account with a sign-in of its own (docs/design/ta-me.md 4).
|
|
173
|
+
issue: async (account) => this.flow.store.signIn({ id: account.id, name: account.name }, 'A computer of the account'),
|
|
174
|
+
});
|
|
179
175
|
this.links = new sign_in_links_1.SignInLinks(options.dir);
|
|
180
176
|
this.environments = new environments_1.Environments(providers, this.tenants, {
|
|
181
177
|
default: config.default,
|
|
@@ -198,13 +194,10 @@ class GatewayServer {
|
|
|
198
194
|
this.quotas.egressOf = tenant => (this.egress.enabled ? this.egress.refused(tenant.id) : undefined);
|
|
199
195
|
this.environments.envOf = (tenant, provider, fresh) => {
|
|
200
196
|
var _a, _b, _c;
|
|
201
|
-
const deployment = GatewayServerUtil.deploymentEnv(env);
|
|
202
|
-
if (!this.models.enabled && !this.egress.enabled) {
|
|
203
|
-
return Object.keys(deployment).length ? deployment : undefined;
|
|
204
|
-
}
|
|
205
197
|
const base = (_a = provider.gatewayUrl) !== null && _a !== void 0 ? _a : GatewayServerUtil.reach(provider.type, (_c = (_b = this.port) !== null && _b !== void 0 ? _b : options.port) !== null && _c !== void 0 ? _c : 4100, sandbox_provider_1.SandboxProviders.publicBase(env));
|
|
206
198
|
const url = base.replace(/\/+$/, '');
|
|
207
|
-
|
|
199
|
+
// The sandbox is a device of its account (docs/design/ta-me.md 2): the accounts of this gateway, from within.
|
|
200
|
+
const given = { ...GatewayServerUtil.deploymentEnv(env), ...GatewayServerUtil.accountEnv(env, url) };
|
|
208
201
|
if (this.models.enabled) {
|
|
209
202
|
Object.assign(given, { TA_MODEL_PROXY_URL: url, TA_MODEL_PROXY_KEY: this.tenants.modelKey(tenant.id) });
|
|
210
203
|
}
|
|
@@ -279,6 +272,7 @@ class GatewayServer {
|
|
|
279
272
|
this.server = server;
|
|
280
273
|
this.ticker = setInterval(() => void this.tick().catch(error => console.warn(`ta gateway: counting minutes failed: ${error.message}`)), TICK_MS);
|
|
281
274
|
(_b = (_a = this.ticker).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
275
|
+
this.service.updates.start();
|
|
282
276
|
return new Promise((resolve, reject) => {
|
|
283
277
|
var _a, _b;
|
|
284
278
|
server.once('error', reject);
|
|
@@ -291,6 +285,7 @@ class GatewayServer {
|
|
|
291
285
|
}
|
|
292
286
|
async close() {
|
|
293
287
|
clearInterval(this.ticker);
|
|
288
|
+
this.service.updates.dispose();
|
|
294
289
|
this.flow.stopPolling();
|
|
295
290
|
await this.tick().catch(() => undefined);
|
|
296
291
|
for (const socket of this.sockets) {
|
|
@@ -468,12 +463,12 @@ class GatewayServer {
|
|
|
468
463
|
return;
|
|
469
464
|
}
|
|
470
465
|
const endpoint = await this.environments.ensure(caller.tenant);
|
|
471
|
-
const target =
|
|
466
|
+
const target = gateway_files_1.GatewayFiles.target((_b = request.url) !== null && _b !== void 0 ? _b : '/', endpoint.url);
|
|
472
467
|
if (!target) {
|
|
473
468
|
sign_in_pages_1.SignInPageUtil.end(response, 404);
|
|
474
469
|
return;
|
|
475
470
|
}
|
|
476
|
-
await gateway_files_1.GatewayFiles.forward(request, response, target,
|
|
471
|
+
await gateway_files_1.GatewayFiles.forward(request, response, target, gateway_files_1.GatewayFiles.headers);
|
|
477
472
|
}
|
|
478
473
|
page(request, response, path) {
|
|
479
474
|
var _a;
|
|
@@ -26,6 +26,7 @@ var GatewayServiceUtil;
|
|
|
26
26
|
getGateway: 'anyone',
|
|
27
27
|
setEnvironment: 'anyone',
|
|
28
28
|
restartEnvironment: 'anyone',
|
|
29
|
+
updateEnvironment: 'anyone',
|
|
29
30
|
listTenants: 'admin',
|
|
30
31
|
getConsole: 'admin',
|
|
31
32
|
getTenantAgents: 'admin',
|
|
@@ -37,6 +38,10 @@ var GatewayServiceUtil;
|
|
|
37
38
|
startTenant: 'admin',
|
|
38
39
|
stopTenant: 'admin',
|
|
39
40
|
restartTenant: 'admin',
|
|
41
|
+
updateTenant: 'admin',
|
|
42
|
+
updateTenants: 'admin',
|
|
43
|
+
rollbackTenant: 'admin',
|
|
44
|
+
checkUpdates: 'admin',
|
|
40
45
|
createSignInLink: 'admin',
|
|
41
46
|
setTenantLimits: 'admin',
|
|
42
47
|
grantMinutes: 'admin',
|
|
@@ -44,6 +49,7 @@ var GatewayServiceUtil;
|
|
|
44
49
|
setDefaultLimits: 'owner',
|
|
45
50
|
setRole: 'owner',
|
|
46
51
|
setEgressHosts: 'owner',
|
|
52
|
+
setUpdatePolicy: 'owner',
|
|
47
53
|
createAccessToken: 'anyone',
|
|
48
54
|
},
|
|
49
55
|
TaAccessServer: {
|
|
@@ -60,6 +66,10 @@ var GatewayServiceUtil;
|
|
|
60
66
|
'startTenant',
|
|
61
67
|
'stopTenant',
|
|
62
68
|
'restartTenant',
|
|
69
|
+
'updateTenant',
|
|
70
|
+
'updateTenants',
|
|
71
|
+
'rollbackTenant',
|
|
72
|
+
'setUpdatePolicy',
|
|
63
73
|
'getTenantLogs',
|
|
64
74
|
'getAgentActivity',
|
|
65
75
|
'createSignInLink',
|
|
@@ -171,6 +181,8 @@ var GatewayServiceUtil;
|
|
|
171
181
|
return second ? 'suspended' : 'let go';
|
|
172
182
|
case 'setRole':
|
|
173
183
|
return String(second);
|
|
184
|
+
case 'setUpdatePolicy':
|
|
185
|
+
return String(params[0]);
|
|
174
186
|
case 'setEgressHosts':
|
|
175
187
|
return Array.isArray(params[0]) ? params[0].join(', ') || 'any public host' : undefined;
|
|
176
188
|
default:
|
|
@@ -7,6 +7,7 @@ import { type GatewayPolicy } from './gateway-policy';
|
|
|
7
7
|
import type { GatewayQuotas } from './gateway-quotas';
|
|
8
8
|
import { type GatewayUsage } from './gateway-usage';
|
|
9
9
|
import { type ModelProxy } from './model-proxy';
|
|
10
|
+
import { SandboxUpdates } from './sandbox-updates';
|
|
10
11
|
import type { SignInLinks } from './sign-in-links';
|
|
11
12
|
import type { Tenant, TenantStore } from './tenant-store';
|
|
12
13
|
/** Who calls: the browser signed in as `account`, with the token `token`, and what that account is on the gateway. */
|
|
@@ -59,13 +60,16 @@ export declare class GatewayService {
|
|
|
59
60
|
protected readonly meta: GatewayMeta;
|
|
60
61
|
protected readonly models?: ModelProxy;
|
|
61
62
|
protected readonly egress?: EgressProxy;
|
|
63
|
+
/** Moving the sandboxes to a newer version of Ta: started with the gateway (GatewayServer.listen). */
|
|
64
|
+
readonly updates: SandboxUpdates;
|
|
62
65
|
constructor(parts: GatewayParts);
|
|
63
66
|
call(caller: GatewayCaller, method: string, params: unknown[]): Promise<unknown>;
|
|
64
67
|
protected handle(caller: GatewayCaller, service: string, name: string, params: unknown[]): Promise<unknown>;
|
|
65
68
|
protected record(caller: GatewayCaller, action: string, params: unknown[]): void;
|
|
66
69
|
info(caller: GatewayCaller): TaGatewayInfo;
|
|
67
70
|
protected setEnvironment(caller: GatewayCaller, value: unknown): Promise<TaGatewayInfo>;
|
|
68
|
-
|
|
71
|
+
/** `update`: it starts on the latest version as one connects again, even one it went back from. */
|
|
72
|
+
protected restart(caller: GatewayCaller, update?: boolean): Promise<TaGatewayInfo>;
|
|
69
73
|
protected tenantInfo(tenant: Tenant): TaGatewayTenant;
|
|
70
74
|
/** The tenants with what their sandboxes take and their agents do, read together: a slow one holds the console a second, not a second each. */
|
|
71
75
|
protected console(caller: GatewayCaller): Promise<TaGatewayConsole>;
|
|
@@ -77,6 +81,9 @@ export declare class GatewayService {
|
|
|
77
81
|
protected startTenant(caller: GatewayCaller, id: unknown): Promise<void>;
|
|
78
82
|
protected stopTenant(caller: GatewayCaller, id: unknown): Promise<void>;
|
|
79
83
|
protected restartTenant(caller: GatewayCaller, id: unknown): Promise<void>;
|
|
84
|
+
protected updateTenant(caller: GatewayCaller, id: unknown): Promise<void>;
|
|
85
|
+
protected rollbackTenant(caller: GatewayCaller, id: unknown): Promise<void>;
|
|
86
|
+
protected setUpdatePolicy(mode: unknown): TaGatewayPolicy;
|
|
80
87
|
/** What its sandbox wrote, its secrets hidden: tokens and keys printed by what ran there do not reach the page. */
|
|
81
88
|
protected logs(id: unknown, lines: unknown): Promise<string>;
|
|
82
89
|
protected activity(id: unknown, sessionId: unknown): Promise<TaAgentActivity[]>;
|
|
@@ -9,6 +9,7 @@ const gateway_policy_1 = require("./gateway-policy");
|
|
|
9
9
|
const gateway_service_util_1 = require("./gateway-service-util");
|
|
10
10
|
const gateway_usage_1 = require("./gateway-usage");
|
|
11
11
|
const model_proxy_1 = require("./model-proxy");
|
|
12
|
+
const sandbox_updates_1 = require("./sandbox-updates");
|
|
12
13
|
const DAY = 24 * 3600000;
|
|
13
14
|
/**
|
|
14
15
|
* The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
|
|
@@ -28,6 +29,7 @@ class GatewayService {
|
|
|
28
29
|
this.audit = parts.audit;
|
|
29
30
|
this.hooks = parts.hooks;
|
|
30
31
|
this.meta = parts.meta;
|
|
32
|
+
this.updates = new sandbox_updates_1.SandboxUpdates({ ...parts, reset: id => parts.hooks.reset(id) });
|
|
31
33
|
}
|
|
32
34
|
async call(caller, method, params) {
|
|
33
35
|
const [service, name] = method.split('/');
|
|
@@ -50,9 +52,10 @@ class GatewayService {
|
|
|
50
52
|
const [first, second] = params;
|
|
51
53
|
const handlers = service === 'TaGatewayServer'
|
|
52
54
|
? {
|
|
53
|
-
getGateway: () => this.info(caller),
|
|
55
|
+
getGateway: () => this.environments.readVersions(caller.tenant).then(() => this.info(caller)),
|
|
54
56
|
setEnvironment: () => this.setEnvironment(caller, first),
|
|
55
57
|
restartEnvironment: () => this.restart(caller),
|
|
58
|
+
updateEnvironment: () => this.restart(caller, true),
|
|
56
59
|
listTenants: () => this.tenants.list().map(tenant => this.tenantInfo(tenant)),
|
|
57
60
|
getConsole: () => this.console(caller),
|
|
58
61
|
getTenantAgents: () => this.environments.agents(this.tenant(first)),
|
|
@@ -64,6 +67,11 @@ class GatewayService {
|
|
|
64
67
|
startTenant: () => this.startTenant(caller, first),
|
|
65
68
|
stopTenant: () => this.stopTenant(caller, first),
|
|
66
69
|
restartTenant: () => this.restartTenant(caller, first),
|
|
70
|
+
updateTenant: () => this.updateTenant(caller, first),
|
|
71
|
+
updateTenants: () => this.updates.check().then(behind => this.updates.updateAll(behind)),
|
|
72
|
+
rollbackTenant: () => this.rollbackTenant(caller, first),
|
|
73
|
+
checkUpdates: () => this.updates.check(true).then(behind => behind.length),
|
|
74
|
+
setUpdatePolicy: () => this.setUpdatePolicy(first),
|
|
67
75
|
createSignInLink: () => this.signInLink(first, second),
|
|
68
76
|
setTenantLimits: () => this.setTenantLimits(caller, first, second),
|
|
69
77
|
grantMinutes: () => this.grantMinutes(caller, first, second),
|
|
@@ -110,6 +118,7 @@ class GatewayService {
|
|
|
110
118
|
admin: caller.admin,
|
|
111
119
|
role: caller.role,
|
|
112
120
|
quota: this.quotas.state(tenant),
|
|
121
|
+
updates: this.policy.updates,
|
|
113
122
|
};
|
|
114
123
|
}
|
|
115
124
|
async setEnvironment(caller, value) {
|
|
@@ -123,7 +132,11 @@ class GatewayService {
|
|
|
123
132
|
this.hooks.reset(next.id);
|
|
124
133
|
return this.info(caller);
|
|
125
134
|
}
|
|
126
|
-
|
|
135
|
+
/** `update`: it starts on the latest version as one connects again, even one it went back from. */
|
|
136
|
+
async restart(caller, update = false) {
|
|
137
|
+
if (update) {
|
|
138
|
+
caller.tenant = this.tenants.update(caller.tenant.id, { skipVersion: undefined });
|
|
139
|
+
}
|
|
127
140
|
await this.environments.stop(caller.tenant.id).catch(() => undefined);
|
|
128
141
|
this.hooks.reset(caller.tenant.id);
|
|
129
142
|
return this.info(caller);
|
|
@@ -152,6 +165,7 @@ class GatewayService {
|
|
|
152
165
|
const [usages, agents] = await Promise.all([
|
|
153
166
|
Promise.all(tenants.map(tenant => this.environments.usage(tenant).catch(() => undefined))),
|
|
154
167
|
Promise.all(tenants.map(tenant => this.environments.agentSummary(tenant).catch(() => undefined))),
|
|
168
|
+
this.updates.check(),
|
|
155
169
|
]);
|
|
156
170
|
const counts = new Map();
|
|
157
171
|
for (const tenant of tenants) {
|
|
@@ -185,6 +199,7 @@ class GatewayService {
|
|
|
185
199
|
api: provider.api,
|
|
186
200
|
models: provider.models.length,
|
|
187
201
|
})),
|
|
202
|
+
...(this.updates.progress ? { updating: { ...this.updates.progress } } : {}),
|
|
188
203
|
};
|
|
189
204
|
}
|
|
190
205
|
policyView() {
|
|
@@ -232,6 +247,26 @@ class GatewayService {
|
|
|
232
247
|
this.hooks.reset(tenant.id);
|
|
233
248
|
await this.environments.ensure(tenant);
|
|
234
249
|
}
|
|
250
|
+
async updateTenant(caller, id) {
|
|
251
|
+
const wentBack = await this.updates.update(this.manage(caller, id));
|
|
252
|
+
if (wentBack) {
|
|
253
|
+
throw new gateway_service_util_1.GatewayCallError(wentBack, ta_protocol_1.TaErrorCode.COMMAND_FAILED, 'Its log tells why the newer version did not start.');
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async rollbackTenant(caller, id) {
|
|
257
|
+
var _a;
|
|
258
|
+
const tenant = this.manage(caller, id);
|
|
259
|
+
await this.environments.rollback(tenant);
|
|
260
|
+
this.hooks.reset(tenant.id);
|
|
261
|
+
await this.environments.ensure((_a = this.tenants.get(tenant.id)) !== null && _a !== void 0 ? _a : tenant);
|
|
262
|
+
}
|
|
263
|
+
setUpdatePolicy(mode) {
|
|
264
|
+
if (mode !== 'idle' && mode !== 'manual') {
|
|
265
|
+
throw new gateway_service_util_1.GatewayCallError('Updates are idle or manual.', ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'idle: once nothing works in a sandbox; manual: when told to.');
|
|
266
|
+
}
|
|
267
|
+
this.policy.setUpdates(mode);
|
|
268
|
+
return this.policyView();
|
|
269
|
+
}
|
|
235
270
|
/** What its sandbox wrote, its secrets hidden: tokens and keys printed by what ran there do not reach the page. */
|
|
236
271
|
async logs(id, lines) {
|
|
237
272
|
return new redactor_1.Redactor().text(await this.environments.logs(this.tenant(id), gateway_service_util_1.GatewayServiceUtil.lines(lines)));
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { TaSandboxVersion } from '@celljs/ta-protocol';
|
|
1
2
|
import type { EgressConfig } from './egress-proxy';
|
|
2
3
|
/** A tenant, as a provider of sandboxes sees it: `token` is the one its Ta service must take (TA_SERVE_TOKEN). */
|
|
3
4
|
export interface SandboxTenant {
|
|
@@ -10,11 +11,24 @@ export interface SandboxTenant {
|
|
|
10
11
|
* gateway. A provider that makes a sandbox once remakes it when they change.
|
|
11
12
|
*/
|
|
12
13
|
env?: Record<string, string>;
|
|
14
|
+
/** The id of a version not to move to on its own: the one the sandbox was moved back from. */
|
|
15
|
+
skip?: string;
|
|
13
16
|
}
|
|
14
17
|
/** Where the Ta service of a tenant answers: `url` is its http(s) address, the gateway connects to `/api/ws` there. */
|
|
15
18
|
export interface SandboxEndpoint {
|
|
16
19
|
url: string;
|
|
17
20
|
token: string;
|
|
21
|
+
/** Made again on the latest version just now, the one before kept: moved back when it does not answer. */
|
|
22
|
+
updated?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export type SandboxVersion = TaSandboxVersion;
|
|
25
|
+
/** The versions of Ta of a tenant's sandbox: what a new one runs, what it runs, and what it ran before (since its update). */
|
|
26
|
+
export interface SandboxVersions {
|
|
27
|
+
latest?: SandboxVersion;
|
|
28
|
+
current?: SandboxVersion;
|
|
29
|
+
previous?: SandboxVersion & {
|
|
30
|
+
since: number;
|
|
31
|
+
};
|
|
18
32
|
}
|
|
19
33
|
export type SandboxState = 'running' | 'stopped' | 'missing';
|
|
20
34
|
/** What a running sandbox takes: CPU in percent of one CPU, memory in bytes. */
|
|
@@ -49,6 +63,16 @@ export interface SandboxProvider {
|
|
|
49
63
|
usage?(tenant: SandboxTenant): Promise<SandboxUsage | undefined>;
|
|
50
64
|
/** The last `lines` its sandbox wrote (its Ta service's output), for the console. */
|
|
51
65
|
logs?(tenant: SandboxTenant, lines: number): Promise<string>;
|
|
66
|
+
/**
|
|
67
|
+
* Its versions, for a provider whose sandboxes move to a newer version of Ta (docs/design/ta-sandbox-update.md):
|
|
68
|
+
* `start` makes a stopped one on an older version again on the latest, unless that is `skip`, keeping the one
|
|
69
|
+
* before, and says so (`updated`). `refresh`: look for a newer one where it is published (a registry).
|
|
70
|
+
*/
|
|
71
|
+
versions?(tenant: SandboxTenant, refresh?: boolean): Promise<SandboxVersions>;
|
|
72
|
+
/** Back to the one it ran before its update, stopped: the id of the version it leaves. Throws when there is none. */
|
|
73
|
+
rollback?(tenant: SandboxTenant): Promise<string | undefined>;
|
|
74
|
+
/** The one it ran before is dropped. */
|
|
75
|
+
forget?(tenant: SandboxTenant): Promise<void>;
|
|
52
76
|
/** The gateway stops: what runs in this process ends (`local`). */
|
|
53
77
|
dispose?(): Promise<void>;
|
|
54
78
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { TaSessionInfo } from '@celljs/ta-protocol';
|
|
2
|
+
import { type Environments } from './environments';
|
|
3
|
+
import type { GatewayAudit } from './gateway-audit';
|
|
4
|
+
import type { GatewayPolicy } from './gateway-policy';
|
|
5
|
+
import type { Tenant, TenantStore } from './tenant-store';
|
|
6
|
+
export declare namespace SandboxUpdatesUtil {
|
|
7
|
+
/** How often the gateway looks for a newer version and the sandboxes that may move to it. */
|
|
8
|
+
const EVERY_MS: number;
|
|
9
|
+
/** How long nothing is to have happened in a Ta for it to be idle. */
|
|
10
|
+
const QUIET_MS: number;
|
|
11
|
+
/** Who the audit says did what the gateway did on its own. */
|
|
12
|
+
const ACTOR = "ta gateway";
|
|
13
|
+
/**
|
|
14
|
+
* Whether nothing works in a Ta: no turn, no dialog or question waiting, and no conversation touched for a while.
|
|
15
|
+
* Connections do not count: a phone stays connected all day.
|
|
16
|
+
*/
|
|
17
|
+
function idle(sessions: TaSessionInfo[] | undefined, now: number): boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface SandboxUpdatesParts {
|
|
20
|
+
environments: Environments;
|
|
21
|
+
tenants: TenantStore;
|
|
22
|
+
policy: GatewayPolicy;
|
|
23
|
+
audit: GatewayAudit;
|
|
24
|
+
/** The connections of the tenant close, to connect again to its sandbox. */
|
|
25
|
+
reset(tenantId: string): void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Moving the sandboxes of tenants to a newer version of Ta (docs/design/ta-sandbox-update.md): a stopped one does as it
|
|
29
|
+
* starts (its provider); a running one when told to, or once nothing works in it (`idle` of the policy), one after
|
|
30
|
+
* another, the gateway stopping at the first that went back to its version before.
|
|
31
|
+
*/
|
|
32
|
+
export declare class SandboxUpdates {
|
|
33
|
+
protected readonly parts: SandboxUpdatesParts;
|
|
34
|
+
/** While sandboxes move one after another. */
|
|
35
|
+
progress?: {
|
|
36
|
+
done: number;
|
|
37
|
+
total: number;
|
|
38
|
+
current?: string;
|
|
39
|
+
};
|
|
40
|
+
protected timer?: ReturnType<typeof setInterval>;
|
|
41
|
+
constructor(parts: SandboxUpdatesParts);
|
|
42
|
+
start(): void;
|
|
43
|
+
dispose(): void;
|
|
44
|
+
/** The versions of every sandbox read again (`refresh`: a newer one pulled); those running an older one. */
|
|
45
|
+
check(refresh?: boolean): Promise<Tenant[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Its sandbox stops (what runs in it is paused, to go on after) and starts on the latest version, even one it went
|
|
48
|
+
* back from: why it went back to the one before when it did, else undefined.
|
|
49
|
+
*/
|
|
50
|
+
update(tenant: Tenant): Promise<string | undefined>;
|
|
51
|
+
/** Each running sandbox on an older version moves, one after another, in the background: how many. */
|
|
52
|
+
updateAll(tenants: Tenant[], auto?: boolean): number;
|
|
53
|
+
/** Every few minutes: what was kept for a week is dropped; idle sandboxes move, when the policy says so. */
|
|
54
|
+
tick(now?: number): Promise<void>;
|
|
55
|
+
protected record(tenant: Tenant, action: string, detail: string): void;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=sandbox-updates.d.ts.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxUpdates = exports.SandboxUpdatesUtil = void 0;
|
|
4
|
+
const environments_1 = require("./environments");
|
|
5
|
+
var SandboxUpdatesUtil;
|
|
6
|
+
(function (SandboxUpdatesUtil) {
|
|
7
|
+
/** How often the gateway looks for a newer version and the sandboxes that may move to it. */
|
|
8
|
+
SandboxUpdatesUtil.EVERY_MS = 10 * 60000;
|
|
9
|
+
/** How long nothing is to have happened in a Ta for it to be idle. */
|
|
10
|
+
SandboxUpdatesUtil.QUIET_MS = 10 * 60000;
|
|
11
|
+
/** Who the audit says did what the gateway did on its own. */
|
|
12
|
+
SandboxUpdatesUtil.ACTOR = 'ta gateway';
|
|
13
|
+
/**
|
|
14
|
+
* Whether nothing works in a Ta: no turn, no dialog or question waiting, and no conversation touched for a while.
|
|
15
|
+
* Connections do not count: a phone stays connected all day.
|
|
16
|
+
*/
|
|
17
|
+
function idle(sessions, now) {
|
|
18
|
+
return (sessions === null || sessions === void 0 ? void 0 : sessions.every(session => !session.isStreaming && !session.waiting && !session.asking && now - session.updatedAt > SandboxUpdatesUtil.QUIET_MS)) === true;
|
|
19
|
+
}
|
|
20
|
+
SandboxUpdatesUtil.idle = idle;
|
|
21
|
+
})(SandboxUpdatesUtil || (exports.SandboxUpdatesUtil = SandboxUpdatesUtil = {}));
|
|
22
|
+
/**
|
|
23
|
+
* Moving the sandboxes of tenants to a newer version of Ta (docs/design/ta-sandbox-update.md): a stopped one does as it
|
|
24
|
+
* starts (its provider); a running one when told to, or once nothing works in it (`idle` of the policy), one after
|
|
25
|
+
* another, the gateway stopping at the first that went back to its version before.
|
|
26
|
+
*/
|
|
27
|
+
class SandboxUpdates {
|
|
28
|
+
constructor(parts) {
|
|
29
|
+
this.parts = parts;
|
|
30
|
+
parts.environments.rolledBack = (tenant, from, reason) => this.record(tenant, 'rollbackTenant', `${environments_1.EnvironmentsUtil.label(from ? { id: from } : undefined)}: ${reason}`);
|
|
31
|
+
}
|
|
32
|
+
start() {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
this.timer = setInterval(() => void this.tick().catch(error => console.warn(`ta gateway: updating sandboxes failed: ${error.message}`)), SandboxUpdatesUtil.EVERY_MS);
|
|
35
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
36
|
+
}
|
|
37
|
+
dispose() {
|
|
38
|
+
clearInterval(this.timer);
|
|
39
|
+
}
|
|
40
|
+
/** The versions of every sandbox read again (`refresh`: a newer one pulled); those running an older one. */
|
|
41
|
+
async check(refresh = false) {
|
|
42
|
+
const { environments, tenants } = this.parts;
|
|
43
|
+
const list = tenants.list();
|
|
44
|
+
await Promise.all(list.map(tenant => environments.readVersions(tenant, refresh).catch(() => undefined)));
|
|
45
|
+
return list.filter(tenant => environments.info(tenant).latest !== undefined);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Its sandbox stops (what runs in it is paused, to go on after) and starts on the latest version, even one it went
|
|
49
|
+
* back from: why it went back to the one before when it did, else undefined.
|
|
50
|
+
*/
|
|
51
|
+
async update(tenant) {
|
|
52
|
+
const { environments, tenants } = this.parts;
|
|
53
|
+
const next = tenants.update(tenant.id, { skipVersion: undefined });
|
|
54
|
+
await environments.stop(tenant.id).catch(() => undefined);
|
|
55
|
+
this.parts.reset(tenant.id);
|
|
56
|
+
await environments.ensure(next);
|
|
57
|
+
return environments.info(next).error;
|
|
58
|
+
}
|
|
59
|
+
/** Each running sandbox on an older version moves, one after another, in the background: how many. */
|
|
60
|
+
updateAll(tenants, auto = false) {
|
|
61
|
+
const list = tenants.filter(tenant => this.parts.environments.info(tenant).status === 'running');
|
|
62
|
+
if (this.progress || list.length === 0) {
|
|
63
|
+
return this.progress ? this.progress.total - this.progress.done : 0;
|
|
64
|
+
}
|
|
65
|
+
const progress = { done: 0, total: list.length };
|
|
66
|
+
this.progress = progress;
|
|
67
|
+
void (async () => {
|
|
68
|
+
for (const tenant of list) {
|
|
69
|
+
progress.current = tenant.account;
|
|
70
|
+
const from = this.parts.environments.info(tenant);
|
|
71
|
+
const wentBack = await this.update(tenant).catch((error) => error.message);
|
|
72
|
+
progress.done++;
|
|
73
|
+
if (auto) {
|
|
74
|
+
this.record(tenant, 'updateTenant', `${environments_1.EnvironmentsUtil.label(from.version)} → ${environments_1.EnvironmentsUtil.label(from.latest)}`);
|
|
75
|
+
}
|
|
76
|
+
if (wentBack) {
|
|
77
|
+
console.warn(`ta gateway: ${tenant.account} did not move to the latest version, the others wait: ${wentBack}`);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
})().finally(() => {
|
|
82
|
+
this.progress = undefined;
|
|
83
|
+
});
|
|
84
|
+
return list.length;
|
|
85
|
+
}
|
|
86
|
+
/** Every few minutes: what was kept for a week is dropped; idle sandboxes move, when the policy says so. */
|
|
87
|
+
async tick(now = Date.now()) {
|
|
88
|
+
await this.parts.environments.forgetPrevious(now);
|
|
89
|
+
if (this.parts.policy.updates !== 'idle' || this.progress) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const found = await this.check(true);
|
|
93
|
+
await Promise.all(found.map(tenant => this.parts.environments.adopt(tenant)));
|
|
94
|
+
const behind = found.filter(tenant => this.parts.environments.info(tenant).status === 'running');
|
|
95
|
+
const sessions = await Promise.all(behind.map(tenant => this.parts.environments.sessions(tenant)));
|
|
96
|
+
this.updateAll(behind.filter((_, index) => SandboxUpdatesUtil.idle(sessions[index], now)), true);
|
|
97
|
+
}
|
|
98
|
+
record(tenant, action, detail) {
|
|
99
|
+
try {
|
|
100
|
+
this.parts.audit.add({ actor: SandboxUpdatesUtil.ACTOR, action, target: tenant.account, detail });
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.warn(`ta gateway: the audit could not be written: ${error.message}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.SandboxUpdates = SandboxUpdates;
|
|
108
|
+
//# sourceMappingURL=sandbox-updates.js.map
|
|
@@ -37,6 +37,8 @@ export interface Tenant {
|
|
|
37
37
|
epoch: number;
|
|
38
38
|
secret: string;
|
|
39
39
|
};
|
|
40
|
+
/** The version of Ta its sandbox went back from: it does not move to it again on its own (docs/design/ta-sandbox-update.md). */
|
|
41
|
+
skipVersion?: string;
|
|
40
42
|
}
|
|
41
43
|
/** Files written whole or not at all, readable by this user only. */
|
|
42
44
|
export declare function writePrivate(file: string, data: unknown): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-gateway",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.21.0",
|
|
4
4
|
"description": "ta gateway: one Ta per account, each in a sandbox of its own, behind one sign-in",
|
|
5
5
|
"homepage": "https://github.com/cellbang/ta",
|
|
6
6
|
"bugs": {
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@celljs/rpc": "4.
|
|
33
|
-
"@celljs/ta-host": "4.
|
|
34
|
-
"@celljs/ta-protocol": "4.
|
|
35
|
-
"@celljs/ta-relay": "4.
|
|
32
|
+
"@celljs/rpc": "4.21.0",
|
|
33
|
+
"@celljs/ta-host": "4.21.0",
|
|
34
|
+
"@celljs/ta-protocol": "4.21.0",
|
|
35
|
+
"@celljs/ta-relay": "4.21.0"
|
|
36
36
|
},
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|