-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathNodegroups.js
More file actions
455 lines (384 loc) · 16.1 KB
/
Copy pathNodegroups.js
File metadata and controls
455 lines (384 loc) · 16.1 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
/* global */
import {Character} from "../Character.js";
import {DropDownMenu} from "../DropDown.js";
import {Panel} from "./Panel.js";
import {Utils} from "../Utils.js";
export class NodegroupsPanel extends Panel {
constructor () {
super("nodegroups");
this.addTitle("Nodegroups");
this.addPanelMenu();
this._addMenuItemStateApplyMinion(this.panelMenu, "*");
this._addMenuItemStateApplyTestMinion(this.panelMenu, "*");
this.addSearchButton();
this.addPlayPauseButton();
this.addWarningField();
this.addTable(["-menu-", "Minion", "Status", "Salt version", "OS version"]);
this.setTableClickable("cmd");
this.addMsg();
}
onShow () {
this.nrMinions = 0;
const useCacheGrains = Utils.getStorageItemBoolean("session", "use_cache_for_grains", false);
this.setWarningText("info", useCacheGrains ? "the content of this screen is based on cached grains info, minion status or grain info may not be accurate" : "");
const wheelKeyListAllPromise = this.api.getWheelKeyListAll();
const localGrainsItemsPromise = useCacheGrains ? this.api.getRunnerCacheGrains(null) : this.api.getLocalGrainsItems(null);
this.loadMinionsTxt();
this._addNodegroupsRows();
this.updateFooter();
this.setPlayPauseButton("play");
wheelKeyListAllPromise.then((pWheelKeyListAllData) => {
this._handleNodegroupsWheelKeyListAll(pWheelKeyListAllData);
localGrainsItemsPromise.then((pLocalGrainsItemsData) => {
this.updateMinions(pLocalGrainsItemsData);
this.nodeGroupTimeout = window.setTimeout(() => {
this.nodeGroupTimeout = null;
this._handleStep(pWheelKeyListAllData.return[0].data.return);
}, 100);
return true;
}, (pLocalGrainsItemsMsg) => {
const allNodegroupsErr = Utils.msgPerMinion(pWheelKeyListAllData.return[0].data.return.minions, JSON.stringify(pLocalGrainsItemsMsg));
this.updateMinions({"return": [allNodegroupsErr]});
return false;
});
return true;
}, (pWheelKeyListAllMsg) => {
this._handleNodegroupsWheelKeyListAll(JSON.stringify(pWheelKeyListAllMsg));
Utils.ignorePromise(localGrainsItemsPromise);
return false;
});
}
onHide () {
if (this.nodeGroupTimeout) {
// stop the timer when nobody is looking
window.clearTimeout(this.nodeGroupTimeout);
this.nodeGroupTimeout = null;
}
}
updateFooter () {
const nodegroups = Utils.getStorageItemObject("session", "nodegroups");
const txt = ", " + Utils.txtZeroOneMany(Object.keys(nodegroups).length,
"no nodegroups", "{0} nodegroup", "{0} nodegroups");
super.updateFooter(txt);
}
updateOfflineMinion (pMinionId, pMinionsDict) {
super.updateOfflineMinion(pMinionId, pMinionsDict, false);
const minionTr = this.table.querySelector("#" + Utils.getIdFromMinionId(pMinionId));
// force same columns on all rows
minionTr.appendChild(Utils.createTd("saltversion"));
minionTr.appendChild(Utils.createTd("os"));
minionTr.offline = true;
}
_moveMinionToNodegroup (pMinionId, pNodegroup, pWheelKeyListAllSimpleData) {
const minionTrId = Utils.getIdFromMinionId(pMinionId);
let minionTr = this.table.querySelector("#" + minionTrId);
const nodegroupTrId = "ng-" + pNodegroup;
const nodegroupTr = this.table.querySelector("#" + nodegroupTrId);
if (minionTr === null) {
// unknown minion is probably not in accepted state
// or totally unknown and still included in the nodegroup
const minionSpan = Utils.createSpan("minion-id", pMinionId);
const minionTd = Utils.createTd();
minionTd.appendChild(minionSpan);
let status;
if (pWheelKeyListAllSimpleData.minions.indexOf(pMinionId) >= 0) {
// strange, should have found this TR
status = Utils.createTd(["status", "error"], "error");
} else if (pWheelKeyListAllSimpleData.minions_pre.indexOf(pMinionId) >= 0) {
status = Utils.createTd(["status", "unaccepted"], "unaccepted");
this.unaccepted += 1;
} else if (pWheelKeyListAllSimpleData.minions_rejected.indexOf(pMinionId) >= 0) {
status = Utils.createTd(["status", "rejected"], "rejected");
this.rejected += 1;
} else if (pWheelKeyListAllSimpleData.minions_denied.indexOf(pMinionId) >= 0) {
status = Utils.createTd(["status", "denied"], "denied");
this.denied += 1;
} else {
status = Utils.createTd(["status", "keyunknown"], "unknown");
Panel.addPrefixIcon(minionSpan, Character.WARNING_SIGN);
Utils.addToolTip(minionSpan, "This minion is listed for this nodegroup,\nbut the minion is unknown", "bottom-left");
this.unknown += 1;
}
minionTr = this.getElement(Utils.getIdFromMinionId(pMinionId), false);
minionTr.appendChild(minionTd);
minionTr.appendChild(status);
minionTr.appendChild(Utils.createTd());
minionTr.appendChild(Utils.createTd());
this._addMenuItemShowKeys(minionTr.dropdownmenu);
minionTr.offline = true;
}
if (minionTr.dataset.moved) {
// this minion is already part of a group
// duplicate it to put it in its 2nd (3rd,etc) group
const minionTr2 = minionTr.cloneNode(true);
nodegroupTr.parentNode.insertBefore(minionTr2, nodegroupTr.nextSibling);
// fix the click-to-copy-logic
const addressSpan = minionTr2.querySelector("td.address span");
if (addressSpan) {
addressSpan.addEventListener("click", (pClickEvent) => {
Panel._copyAddress(addressSpan, pClickEvent.ctrlKey || pClickEvent.altKey);
pClickEvent.stopPropagation();
});
addressSpan.addEventListener("mouseout", () => {
Panel.restoreClickToCopy(addressSpan);
});
Panel.restoreClickToCopy(addressSpan);
}
// fix the row menu
const oldMenuButton = minionTr2.querySelector("td div.run-command-button");
const minionIsOk = minionTr2.dataset.saltversion !== undefined;
if (oldMenuButton) {
oldMenuButton.parentElement.remove();
const newMenuButton = Utils.createTd();
minionTr2.insertBefore(newMenuButton, minionTr2.firstChild);
minionTr2.dropdownmenu = new DropDownMenu(newMenuButton, "smaller");
if (minionIsOk) {
this._addMenuItemStateApplyMinion(minionTr2.dropdownmenu, pMinionId);
this._addMenuItemStateApplyTestMinion(minionTr2.dropdownmenu, pMinionId);
this._addMenuItemShowGrains(minionTr2.dropdownmenu, pMinionId);
this._addMenuItemShowPillars(minionTr2.dropdownmenu, pMinionId);
this._addMenuItemShowSchedules(minionTr2.dropdownmenu, pMinionId);
this._addMenuItemShowBeacons(minionTr2.dropdownmenu, pMinionId);
}
}
if (minionIsOk) {
// fix the row as needed
minionTr2.addEventListener("click", (pClickEvent) => {
const cmdArr = ["state.apply"];
this.runCommand("", pMinionId, cmdArr);
pClickEvent.stopPropagation();
});
}
} else {
// move the row to its proper place
nodegroupTr.parentNode.insertBefore(minionTr, nodegroupTr.nextSibling);
minionTr.dataset.moved = true;
}
}
_handleStepGroup (pWheelKeyListAllSimpleData) {
const nodegroup = this.todoNodegroups.shift();
// test group membership with function that is typically hidden
// note: uses full_data=true
const localTestVersion = this.api.getLocalTestVersion(nodegroup);
localTestVersion.then((pLocalTestVersionData) => {
const retdata = pLocalTestVersionData.return[0];
// handle the list in reverse order
const nodelist = Object.keys(retdata).sort().
reverse();
for (const minionId of nodelist) {
this._moveMinionToNodegroup(minionId, nodegroup, pWheelKeyListAllSimpleData);
}
const titleElement = this.table.querySelector("#ng-" + nodegroup + " td:nth-child(2)");
const cnt = nodelist.length;
let txt = Utils.txtZeroOneMany(cnt, "no minions", cnt + " minion", cnt + " minions");
let online = 0;
let offline = 0;
let problems = 0;
for (const minionId of nodelist) {
const minionData = retdata[minionId];
if (minionData === false) {
offline += 1;
} else if (minionData.retcode === 0) {
online += 1;
} else {
// that's an error message
// e.g. unaccepted minion or unknown minion
problems += 1;
}
}
if (online !== cnt) {
txt += ", " + online + " online";
}
if (offline !== 0) {
txt += ", " + offline + " offline";
}
if (problems !== 0) {
txt += ", " + Utils.txtZeroOneMany(problems, "no problems", "{0} problem", "{0} problems");
}
titleElement.innerHTML = titleElement.innerHTML.replace("(loading)", txt);
// try again for more
this.nodeGroupTimeout = window.setTimeout(() => {
this.nodeGroupTimeout = null;
this._handleStep(pWheelKeyListAllSimpleData);
}, 100);
}, (pLocalTestVersionMsg) => {
this.showErrorRowInstead(pLocalTestVersionMsg.toString());
});
}
_handleStepNoGroup () {
this.setPlayPauseButton("none");
this.todoNodegroups = null;
const titleElement = this.table.querySelectorAll("#ng-" + null + " td")[1];
const cnt = this.table.rows.length - titleElement.parentElement.rowIndex - 1;
if (cnt === 0) {
// remove the title of the empty group "not in any nodegroup"
titleElement.parentElement.remove();
return;
}
let txt = Utils.txtZeroOneMany(cnt, "no minions", cnt + " minion", cnt + " minions");
let online = 0;
let offline = 0;
for (let row = titleElement.parentElement.rowIndex + 1; row < this.table.rows.length; row++) {
const tr = this.table.rows[row];
if (tr.online === true) {
online += 1;
}
if (tr.offline === true) {
offline += 1;
}
}
if (online !== cnt) {
txt += ", " + online + " online";
}
if (offline !== 0) {
txt += ", " + offline + " offline";
}
titleElement.innerHTML = titleElement.innerHTML.replace("(loading)", txt);
}
_handleStep (pWheelKeyListAllSimpleData) {
// user can decide
// system can decide to remove the play/pause button
if (this.playOrPause !== "play") {
// try again later for more
this.nodeGroupTimeout = window.setTimeout(() => {
this.nodeGroupTimeout = null;
this._handleStep(pWheelKeyListAllSimpleData);
}, 100);
return;
}
if (this.todoNodegroups.length === 0) {
this._handleStepNoGroup();
return;
}
this._handleStepGroup(pWheelKeyListAllSimpleData);
}
_addNodegroupsRows () {
const nodegroups = Utils.getStorageItemObject("session", "nodegroups");
this.allNodegroups = Object.keys(nodegroups).sort();
this.todoNodegroups = Object.keys(nodegroups).sort();
for (const nodegroup of this.allNodegroups) {
this._addNodegroupRow(nodegroup);
}
this._addNodegroupRow(null, this.allNodegroups);
}
_addNodegroupRow (pNodegroup, pAllNodegroups) {
const tr = Utils.createTr("no-search", null, "ng-" + pNodegroup);
tr.style.borderTop = "4px double var(--color-border-default)";
const menuTd = Utils.createTd();
tr.dropdownmenu = new DropDownMenu(menuTd, "smaller");
tr.appendChild(menuTd);
const titleTd = Utils.createTd();
if (pNodegroup) {
titleTd.innerHTML = Character.EM_DASH + " nodegroup <b>" + pNodegroup + "</b> " + Character.EM_DASH + " (loading) " + Character.EM_DASH;
} else {
titleTd.innerText = Character.EM_DASH + " not in any nodegroup " + Character.EM_DASH + " (loading) " + Character.EM_DASH;
}
titleTd.colSpan = 99;
tr.append(titleTd);
this._addMenuItemStateApplyGroup(tr.dropdownmenu, pNodegroup, pAllNodegroups);
this._addMenuItemStateApplyTestGroup(tr.dropdownmenu, pNodegroup, pAllNodegroups);
tr.addEventListener("click", (pClickEvent) => {
const cmdArr = ["state.apply"];
this.runCommand("", NodegroupsPanel._getGroupTarget(pNodegroup, pAllNodegroups), cmdArr);
pClickEvent.stopPropagation();
});
this.table.tBodies[0].appendChild(tr);
}
_handleNodegroupsWheelKeyListAll (pWheelKeyListAll) {
if (this.showErrorRowInstead(pWheelKeyListAll)) {
return;
}
const keys = pWheelKeyListAll.return[0].data.return;
this.nrMinions = keys.minions.length;
this.nrUnaccepted = keys.minions_pre.length;
const minionIds = keys.minions.sort();
for (const minionId of minionIds) {
const minionTr = this.addMinion(minionId, false);
// preliminary dropdown menu
this._addMenuItemStateApplyMinion(minionTr.dropdownmenu, minionId);
this._addMenuItemStateApplyTestMinion(minionTr.dropdownmenu, minionId);
this._addMenuItemShowGrains(minionTr.dropdownmenu, minionId);
this._addMenuItemShowPillars(minionTr.dropdownmenu, minionId);
this._addMenuItemShowSchedules(minionTr.dropdownmenu, minionId);
this._addMenuItemShowBeacons(minionTr.dropdownmenu, minionId);
}
this.updateFooter();
}
updateMinion (pMinionData, pMinionId, pAllNodegroupsGrains) {
super.updateMinion(pMinionData, pMinionId, pAllNodegroupsGrains, false);
const minionTr = this.table.querySelector("#" + Utils.getIdFromMinionId(pMinionId));
this._addMenuItemStateApplyMinion(minionTr.dropdownmenu, pMinionId);
this._addMenuItemStateApplyTestMinion(minionTr.dropdownmenu, pMinionId);
this._addMenuItemShowGrains(minionTr.dropdownmenu, pMinionId);
this._addMenuItemShowPillars(minionTr.dropdownmenu, pMinionId);
this._addMenuItemShowSchedules(minionTr.dropdownmenu, pMinionId);
this._addMenuItemShowBeacons(minionTr.dropdownmenu, pMinionId);
minionTr.addEventListener("click", (pClickEvent) => {
const cmdArr = ["state.apply"];
this.runCommand("", pMinionId, cmdArr);
pClickEvent.stopPropagation();
});
minionTr.online = true;
}
static _getGroupTarget (pNodegroup, pAllNodegroups) {
if (pNodegroup) {
// could have use the '#' notation, but this is more native
return "N@" + pNodegroup;
}
let ret = "";
for (const nodegroup of pAllNodegroups) {
ret += " and not N@" + nodegroup;
}
// strip first " and "
return ret.substring(5);
}
_addMenuItemStateApplyGroup (pMenu, pNodegroup, pAllNodegroups) {
pMenu.addMenuItem("Apply state...", () => {
const cmdArr = ["state.apply"];
this.runCommand("", NodegroupsPanel._getGroupTarget(pNodegroup, pAllNodegroups), cmdArr);
});
}
_addMenuItemStateApplyTestGroup (pMenu, pNodegroup, pAllNodegroups) {
pMenu.addMenuItem("Test state...", () => {
const cmdArr = ["state.apply", "test=", true];
this.runCommand("", NodegroupsPanel._getGroupTarget(pNodegroup, pAllNodegroups), cmdArr);
});
}
_addMenuItemStateApplyMinion (pMenu, pMinionId) {
pMenu.addMenuItem("Apply state...", () => {
const cmdArr = ["state.apply"];
this.runCommand("", pMinionId, cmdArr);
});
}
_addMenuItemStateApplyTestMinion (pMenu, pMinionId) {
pMenu.addMenuItem("Test state...", () => {
const cmdArr = ["state.apply", "test=", true];
this.runCommand("", pMinionId, cmdArr);
});
}
_addMenuItemShowKeys (pMenu) {
pMenu.addMenuItem("Show keys", (pClickEvent) => {
this.router.goTo("keys", undefined, undefined, pClickEvent);
});
}
_addMenuItemShowGrains (pMenu, pMinionId) {
pMenu.addMenuItem("Show grains", (pClickEvent) => {
this.router.goTo("grains-minion", {"minionid": pMinionId}, undefined, pClickEvent);
});
}
_addMenuItemShowSchedules (pMenu, pMinionId) {
pMenu.addMenuItem("Show schedules", (pClickEvent) => {
this.router.goTo("schedules-minion", {"minionid": pMinionId}, undefined, pClickEvent);
});
}
_addMenuItemShowPillars (pMenu, pMinionId) {
pMenu.addMenuItem("Show pillars", (pClickEvent) => {
this.router.goTo("pillars-minion", {"minionid": pMinionId}, undefined, pClickEvent);
});
}
_addMenuItemShowBeacons (pMenu, pMinionId) {
pMenu.addMenuItem("Show beacons", (pClickEvent) => {
this.router.goTo("beacons-minion", {"minionid": pMinionId}, undefined, pClickEvent);
});
}
}