forked from windmill-labs/windmill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.ts
More file actions
571 lines (526 loc) · 16 KB
/
Copy pathsettings.ts
File metadata and controls
571 lines (526 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import process from "node:process";
import { colors, Confirm, log, yamlParseFile, yamlStringify } from "./deps.ts";
import * as wmill from "./gen/services.gen.ts";
import { AIConfig, Config, GlobalSetting } from "./gen/types.gen.ts";
import { compareInstanceObjects, InstanceSyncOptions } from "./instance.ts";
import { isSuperset } from "./types.ts";
import { deepEqual } from "./utils.ts";
import { removeWorkerPrefix } from "./worker_groups.ts";
export interface SimplifiedSettings {
// slack_team_id?: string;
// slack_name?: string;
// slack_command_script?: string;
// slack_email?: string;
auto_invite_enabled: boolean;
auto_invite_as: string;
auto_invite_mode: string;
webhook?: string;
deploy_to?: string;
error_handler?: string;
error_handler_extra_args?: any;
error_handler_muted_on_cancel?: boolean;
ai_config?: AIConfig;
large_file_storage?: any;
git_sync?: any;
default_app?: string;
default_scripts?: any;
name: string;
mute_critical_alerts?: boolean;
color?: string;
operator_settings?: any;
}
const INSTANCE_SETTINGS_PATH = "instance_settings.yaml";
let instanceSettingsPath = INSTANCE_SETTINGS_PATH;
async function checkInstanceSettingsPath(opts: InstanceSyncOptions) {
if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) {
instanceSettingsPath = `${opts.prefix}/${INSTANCE_SETTINGS_PATH}`;
}
}
const INSTANCE_CONFIGS_PATH = "instance_configs.yaml";
let instanceConfigsPath = INSTANCE_CONFIGS_PATH;
async function checkInstanceConfigPath(opts: InstanceSyncOptions) {
if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) {
instanceConfigsPath = `${opts.prefix}/${INSTANCE_CONFIGS_PATH}`;
}
}
export async function pushWorkspaceSettings(
workspace: string,
_path: string,
settings: SimplifiedSettings | undefined,
localSettings: SimplifiedSettings
) {
try {
const remoteSettings = await wmill.getSettings({
workspace,
});
const workspaceName = await wmill.getWorkspaceName({
workspace,
});
settings = {
// slack_team_id: remoteSettings.slack_team_id,
// slack_name: remoteSettings.slack_name,
// slack_command_script: remoteSettings.slack_command_script,
// slack_email: remoteSettings.slack_email,
auto_invite_enabled: remoteSettings.auto_invite_domain !== null,
auto_invite_as: remoteSettings.auto_invite_operator
? "operator"
: "developer",
auto_invite_mode: remoteSettings.auto_add ? "add" : "invite",
webhook: remoteSettings.webhook,
deploy_to: remoteSettings.deploy_to,
error_handler: remoteSettings.error_handler,
error_handler_extra_args: remoteSettings.error_handler_extra_args,
error_handler_muted_on_cancel:
remoteSettings.error_handler_muted_on_cancel,
ai_config: remoteSettings.ai_config,
large_file_storage: remoteSettings.large_file_storage,
git_sync: remoteSettings.git_sync,
default_app: remoteSettings.default_app,
default_scripts: remoteSettings.default_scripts,
name: workspaceName,
mute_critical_alerts: remoteSettings.mute_critical_alerts,
color: remoteSettings.color,
operator_settings: remoteSettings.operator_settings,
};
} catch (err) {
throw new Error(`Failed to get workspace settings: ${err}`);
}
if (isSuperset(localSettings, settings)) {
log.debug(`Workspace settings are up to date`);
return;
}
log.debug(`Workspace settings are not up-to-date, updating...`);
if (localSettings.webhook !== settings.webhook) {
log.debug(`Updateing webhook...`);
await wmill.editWebhook({
workspace,
requestBody: {
webhook: localSettings.webhook,
},
});
}
if (
localSettings.auto_invite_as !== settings.auto_invite_as ||
localSettings.auto_invite_enabled !== settings.auto_invite_enabled ||
localSettings.auto_invite_mode !== settings.auto_invite_mode
) {
log.debug(`Updating auto invite...`);
if (!["operator", "developer"].includes(settings.auto_invite_as)) {
throw new Error(
`Invalid value for auto_invite_as. Valid values are "operator" and "developer"`
);
}
if (!["add", "invite"].includes(settings.auto_invite_mode)) {
throw new Error(
`Invalid value for auto_invite_mode. Valid values are "invite" and "add"`
);
}
try {
await wmill.editAutoInvite({
workspace,
requestBody: localSettings.auto_invite_enabled
? {
operator: localSettings.auto_invite_as === "operator",
invite_all: true,
auto_add: localSettings.auto_invite_mode === "add",
}
: {},
});
} catch (_) {
// on cloud
log.debug(
`Auto invite is not possible on cloud, only auto-inviting same domain...`
);
await wmill.editAutoInvite({
workspace,
requestBody: localSettings.auto_invite_enabled
? {
operator: localSettings.auto_invite_as === "operator",
invite_all: false,
auto_add: localSettings.auto_invite_mode === "add",
}
: {},
});
}
}
if (!deepEqual(localSettings.ai_config, settings.ai_config)) {
log.debug(`Updating copilot settings...`);
await wmill.editCopilotConfig({
workspace,
requestBody: localSettings.ai_config ?? {},
});
}
if (
localSettings.error_handler != settings.error_handler ||
!deepEqual(
localSettings.error_handler_extra_args,
settings.error_handler_extra_args
) ||
localSettings.error_handler_muted_on_cancel !=
settings.error_handler_muted_on_cancel
) {
log.debug(`Updating error handler...`);
await wmill.editErrorHandler({
workspace,
requestBody: {
error_handler: localSettings.error_handler,
error_handler_extra_args: localSettings.error_handler_extra_args,
error_handler_muted_on_cancel:
localSettings.error_handler_muted_on_cancel,
},
});
}
if (localSettings.deploy_to != settings.deploy_to) {
log.debug(`Updating deploy to...`);
await wmill.editDeployTo({
workspace,
requestBody: {
deploy_to: localSettings.deploy_to,
},
});
}
if (
!deepEqual(localSettings.large_file_storage, settings.large_file_storage)
) {
log.debug(`Updating large file storage...`);
await wmill.editLargeFileStorageConfig({
workspace,
requestBody: {
large_file_storage: localSettings.large_file_storage,
},
});
}
if (!deepEqual(localSettings.git_sync, settings.git_sync)) {
log.debug(`Updating git sync...`);
await wmill.editWorkspaceGitSyncConfig({
workspace,
requestBody: {
git_sync_settings: localSettings.git_sync,
},
});
}
if (!deepEqual(localSettings.default_scripts, settings.default_scripts)) {
log.debug(`Updating default scripts...`);
await wmill.editDefaultScripts({
workspace,
requestBody: localSettings.default_scripts,
});
}
if (localSettings.default_app != settings.default_app) {
log.debug(`Updating default app...`);
await wmill.editWorkspaceDefaultApp({
workspace,
requestBody: {
default_app_path: localSettings.default_app,
},
});
}
if (localSettings.name != settings.name) {
log.debug(`Updating workspace name...`);
await wmill.changeWorkspaceName({
workspace,
requestBody: {
new_name: localSettings.name,
},
});
}
if (localSettings.mute_critical_alerts != settings.mute_critical_alerts) {
log.debug(`Updating mute critical alerts...`);
await wmill.workspaceMuteCriticalAlertsUi({
workspace,
requestBody: {
mute_critical_alerts: localSettings.mute_critical_alerts,
},
});
}
if (localSettings.color != settings.color) {
log.debug(`Updating workspace color...`);
await wmill.changeWorkspaceColor({
workspace,
requestBody: {
color: localSettings.color,
},
});
}
if (localSettings.operator_settings != settings.operator_settings) {
log.debug(`Updating operator settings...`);
await wmill.updateOperatorSettings({
workspace,
requestBody: localSettings.operator_settings,
});
}
}
export async function pushWorkspaceKey(
workspace: string,
_path: string,
key: string | undefined,
localKey: string
) {
try {
key = await wmill
.getWorkspaceEncryptionKey({
workspace,
})
.then((r) => r.key);
} catch (err) {
throw new Error(`Failed to get workspace encryption key: ${err}`);
}
if (localKey && key !== localKey) {
const confirm = await Confirm.prompt({
message:
"The local workspace encryption key does not match the remote. Do you want to reencrypt all your secrets on the remote with the new key?\nSay 'no' if your local secrets are already encrypted with the new key (e.g. workspace/instance migration)\nOtherwise, say 'yes' and pull the secrets after the reencryption.\n",
default: true,
});
log.debug(`Updating workspace encryption key...`);
await wmill.setWorkspaceEncryptionKey({
workspace,
requestBody: {
new_key: localKey,
skip_reencrypt: !confirm,
},
});
} else {
log.debug(`Workspace encryption key is up to date`);
}
}
export async function readInstanceSettings(opts: InstanceSyncOptions) {
let localSettings: GlobalSetting[] = [];
await checkInstanceSettingsPath(opts);
try {
localSettings = (await yamlParseFile(
instanceSettingsPath
)) as GlobalSetting[];
} catch {
log.warn(`No ${instanceSettingsPath} found`);
}
return localSettings;
}
import { decrypt, encrypt } from "./local_encryption.ts";
const SENSITIVE_FIELD: string[] = ["license_key", "jwt_secret"];
async function processInstanceSettings(
settings: GlobalSetting[],
mode: "encode" | "decode"
): Promise<GlobalSetting[]> {
const encKey = process.env.WMILL_INSTANCE_LOCAL_ENCRYPTION_KEY;
if (encKey) {
const res: GlobalSetting[] = [];
for (const s of settings) {
if (SENSITIVE_FIELD.includes(s.name) && typeof s.value === "string") {
res.push(
(await processField(s, "value", encKey, mode)) as GlobalSetting
);
} else if (s.name == "oauths") {
if (typeof s.value === "object") {
const oauths = s.value as { [key: string]: any };
for (const [k, v] of Object.entries(oauths)) {
oauths[k] = await processField(v, "secret", encKey, mode);
}
res.push(s);
} else {
log.warn(`Unexpected oauths value type: ${typeof s.value}`);
res.push(s);
}
} else {
res.push(s);
}
}
return res;
} else {
log.warn(
"No encryption key found, skipping encryption. Recommend setting WMILL_INSTANCE_LOCAL_ENCRYPTION_KEY"
);
}
return settings;
}
async function processField(
obj: { [key: string]: any },
field: string,
encKey: string,
mode: "encode" | "decode"
): Promise<{ [key: string]: any }> {
return {
...obj,
[field]:
mode === "encode"
? await encrypt(obj[field], encKey)
: ((await decrypt(obj[field], encKey)) as any),
};
}
export async function pullInstanceSettings(
opts: InstanceSyncOptions,
preview = false
) {
const remoteSettings = await wmill.listGlobalSettings();
await checkInstanceSettingsPath(opts);
if (preview) {
const localSettings: GlobalSetting[] = await readInstanceSettings(opts);
const processedSettings = await processInstanceSettings(
remoteSettings,
"encode"
);
return compareInstanceObjects(
processedSettings,
localSettings,
"name",
"setting"
);
} else {
log.info("Pulling settings from instance");
const processedSettings = await processInstanceSettings(
remoteSettings,
"encode"
);
await Deno.writeTextFile(
instanceSettingsPath,
yamlStringify(processedSettings)
);
log.info(colors.green(`Settings written to ${instanceSettingsPath}`));
}
}
export async function pushInstanceSettings(
opts: InstanceSyncOptions,
preview: boolean = false
) {
const remoteSettings = await wmill.listGlobalSettings();
let localSettings: GlobalSetting[] = await readInstanceSettings(opts);
localSettings = await processInstanceSettings(localSettings, "decode");
if (opts.baseUrl) {
localSettings = localSettings.filter((s) => s.name !== "base_url");
localSettings.push({
name: "base_url",
//@ts-ignore
value: opts.baseUrl,
});
}
if (preview) {
return compareInstanceObjects(
localSettings,
remoteSettings,
"name",
"setting"
);
} else {
for (const setting of localSettings) {
const remoteMatch = remoteSettings.find((s) => s.name === setting.name);
if (remoteMatch && deepEqual(remoteMatch, setting)) {
continue;
}
try {
await wmill.setGlobal({
key: setting.name,
requestBody: {
value: setting.value,
},
});
} catch (err) {
log.error(`Failed to set setting ${setting.name}: ${err}`);
}
}
for (const remoteSetting of remoteSettings) {
const localMatch = localSettings.find(
(s) => s.name === remoteSetting.name
);
if (!localMatch) {
try {
await wmill.setGlobal({
key: remoteSetting.name,
requestBody: {
value: null,
},
});
} catch (err) {
log.error(`Failed to delete setting ${remoteSetting.name}: ${err}`);
}
}
}
log.info(colors.green("Settings pushed to instance"));
}
}
export async function readLocalConfigs(opts: InstanceSyncOptions) {
let localConfigs: Config[] = [];
await checkInstanceConfigPath(opts);
try {
localConfigs = (await yamlParseFile(instanceConfigsPath)) as Config[];
} catch {
log.warn(`No ${instanceConfigsPath} found`);
}
return localConfigs;
}
export async function pullInstanceConfigs(
opts: InstanceSyncOptions,
preview = false
) {
const remoteConfigs = (await wmill.listConfigs()).map((x) => {
return {
...x,
name: removeWorkerPrefix(x.name),
};
});
if (preview) {
const localConfigs: Config[] = await readLocalConfigs(opts);
return compareInstanceObjects(
remoteConfigs,
localConfigs,
"name",
"config"
);
} else {
log.info("Pulling configs from instance");
await Deno.writeTextFile(
instanceConfigsPath,
yamlStringify(remoteConfigs as any)
);
log.info(colors.green(`Configs written to ${instanceConfigsPath}`));
}
}
export async function pushInstanceConfigs(
opts: InstanceSyncOptions,
preview: boolean = false
) {
const remoteConfigs = (await wmill.listConfigs()).map((x) => {
return {
...x,
name: removeWorkerPrefix(x.name),
};
});
const localConfigs = await readLocalConfigs(opts);
if (preview) {
return compareInstanceObjects(
localConfigs,
remoteConfigs,
"name",
"config"
);
} else {
log.info("Pushing configs to instance");
for (const config of localConfigs) {
const remoteMatch = remoteConfigs.find((c) => c.name === config.name);
if (remoteMatch && deepEqual(remoteMatch, config)) {
continue;
}
try {
await wmill.updateConfig({
name: config.name.startsWith("worker__")
? config.name
: `worker__${config.name}`,
requestBody: config.config,
});
} catch (err) {
log.error(`Failed to set config ${config.name}: ${err}`);
}
}
for (const removeConfig of remoteConfigs) {
const localMatch = localConfigs.find((c) => c.name === removeConfig.name);
if (!localMatch) {
try {
await wmill.deleteConfig({
name: removeConfig.name,
});
} catch (err) {
log.error(`Failed to delete config ${removeConfig.name}: ${err}`);
}
}
}
log.info(colors.green("Configs pushed to instance"));
}
}