forked from windmill-labs/windmill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_groups.ts
More file actions
138 lines (117 loc) · 3.67 KB
/
Copy pathworker_groups.ts
File metadata and controls
138 lines (117 loc) · 3.67 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
import { Command, Confirm, setClient, Table } from "./deps.ts";
import { log } from "./deps.ts";
import { allInstances, getActiveInstance, Instance, InstanceSyncOptions, pickInstance } from "./instance.ts";
import * as wmill from "./gen/services.gen.ts";
import { pullInstanceConfigs, pushInstanceConfigs } from "./settings.ts";
type GlobalOptions = {
instance?: string;
baseUrl?: string;
};
export async function getInstance(opts: GlobalOptions) {
const instances = await allInstances();
const instanceName = await getActiveInstance(opts);
const instance = instances.find((i) => i.name === instanceName);
if (instance) {
setClient(
instance.token,
instance.remote.slice(0, instance.remote.length - 1)
);
}
return instance;
}
export function removeWorkerPrefix(name: string) {
if (name.startsWith("worker__")) {
return name.substring(8);
}
return name;
}
export async function displayWorkerGroups(opts: void) {
log.info("2 actions available, pull and push.");
const activeInstance = await getActiveInstance({});
if (activeInstance) {
log.info("Active instance: " + activeInstance);
const instance = await getInstance({});
if (instance) {
const wGroups = await wmill.listWorkerGroups();
new Table()
.header(["name", "config"])
.padding(2)
.border(true)
.body(wGroups.map((x) => [removeWorkerPrefix(x.name), JSON.stringify(x.config, null, 2)]))
.render();
} else {
log.error(`Instance ${activeInstance} not found`);
}
} else {
log.info("No active instance found");
log.info("Use 'wmill instance add' to add a new instance");
}
}
async function pullWorkerGroups(opts: InstanceSyncOptions) {
await pickInstance(opts, true);
const totalChanges = await pullInstanceConfigs(opts, true) ?? 0;
if (totalChanges === 0) {
log.info("No changes to apply");
return;
}
let confirm = true;
if (opts.yes !== true) {
confirm = await Confirm.prompt({
message: `Do you want to pul these ${totalChanges} instance-level changes?`,
default: true,
});
}
if (confirm) {
await pullInstanceConfigs(opts, false);
}
}
async function pushWorkerGroups(opts: InstanceSyncOptions) {
await pickInstance(opts, true);
const totalChanges = await pushInstanceConfigs(opts, true) ?? 0;
if (totalChanges === 0) {
log.info("No changes to apply");
return;
}
let confirm = true;
if (opts.yes !== true) {
confirm = await Confirm.prompt({
message: `Do you want to apply these ${totalChanges} instance-level changes?`,
default: true,
});
}
if (confirm) {
await pushInstanceConfigs(opts, false);
}
}
const command = new Command()
.description("display worker groups, pull and push worker groups configs")
.action(displayWorkerGroups)
.command("pull")
.description(
"Pull worker groups (similar to `wmill instance pull --skip-users --skip-settings --skip-groups`)"
)
.option(
"--instance",
"Name of the instance to push to, override the active instance"
)
.option(
"--base-url",
"Base url to be passed to the instance settings instead of the local one"
)
.option("--yes", "Pull without needing confirmation")
.action(pullWorkerGroups as any)
.command("push")
.description(
"Push instance settings, users, configs, group and overwrite remote"
)
.option(
"--instance [instance]",
"Name of the instance to push to, override the active instance"
)
.option(
"--base-url [baseUrl]",
"If used with --token, will be used as the base url for the instance"
)
.option("--yes", "Push without needing confirmation")
.action(pushWorkerGroups as any);
export default command;