forked from rancher-sandbox/rancher-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.ts
915 lines (764 loc) · 29 KB
/
background.ts
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
import os from 'os';
import path from 'path';
import util from 'util';
import Electron from 'electron';
import _ from 'lodash';
import K8sFactory from '@/backend/factory';
import { getImageProcessor } from '@/backend/images/imageFactory';
import { ImageProcessor } from '@/backend/images/imageProcessor';
import * as K8s from '@/backend/k8s';
import { Steve } from '@/backend/steve';
import * as settings from '@/config/settings';
import { IntegrationManager, getIntegrationManager } from '@/integrations/integrationManager';
import { removeLegacySymlinks, PermissionError } from '@/integrations/legacy';
import { getPathManagerFor, PathManagementStrategy, PathManager } from '@/integrations/pathManager';
import { CommandWorkerInterface, HttpCommandServer } from '@/main/commandServer/httpCommandServer';
import SettingsValidator from '@/main/commandServer/settingsValidator';
import { HttpCredentialHelperServer } from '@/main/credentialServer/httpCredentialHelperServer';
import { DashboardServer } from '@/main/dashboardServer';
import { DiagnosticsManager, DiagnosticsResultCollection } from '@/main/diagnostics/diagnostics';
import { ImageEventHandler } from '@/main/imageEvents';
import { getIpcMainProxy } from '@/main/ipcMain';
import mainEvents from '@/main/mainEvents';
import buildApplicationMenu from '@/main/mainmenu';
import setupNetworking from '@/main/networking';
import setupTray from '@/main/tray';
import setupUpdate from '@/main/update';
import * as childProcess from '@/utils/childProcess';
import getCommandLineArgs from '@/utils/commandLine';
import DockerDirManager from '@/utils/dockerDirManager';
import Logging, { setLogLevel } from '@/utils/logging';
import paths from '@/utils/paths';
import { setupProtocolHandler, protocolRegistered } from '@/utils/protocols';
import { jsonStringifyWithWhiteSpace } from '@/utils/stringify';
import { RecursivePartial } from '@/utils/typeUtils';
import * as window from '@/window';
import { closeDashboard, openDashboard } from '@/window/dashboard';
import { preferencesSetDirtyFlag } from '@/window/preferences';
Electron.app.setName('Rancher Desktop');
Electron.app.setPath('cache', paths.cache);
Electron.app.setAppLogsPath(paths.logs);
const console = Logging.background;
const ipcMainProxy = getIpcMainProxy(console);
const dockerDirManager = new DockerDirManager(path.join(os.homedir(), '.docker'));
const k8smanager = newK8sManager();
const diagnostics: DiagnosticsManager = new DiagnosticsManager();
let cfg: settings.Settings;
let waitForFirstRunDialogCompletion = true;
let gone = false; // when true indicates app is shutting down
let imageEventHandler: ImageEventHandler|null = null;
let currentContainerEngine = settings.ContainerEngine.NONE;
let currentImageProcessor: ImageProcessor | null = null;
let enabledK8s: boolean;
let pathManager: PathManager;
const integrationManager: IntegrationManager = getIntegrationManager();
let noModalDialogs = false;
/**
* pendingRestartContext is needed because with the CLI it's possible to change
* the state of the system without using the UI. This can push the system out
* of sync, for example setting kubernetes-enabled=true while it's disabled.
* Normally the code restarts the system when processing the SET command, but if
* the backend is currently starting up or shutting down, we have to wait for it
* to finish. This module gets a `state-changed` event when that happens,
* and if this flag is true, a new restart can be triggered.
*/
let pendingRestartContext: CommandWorkerInterface.CommandContext | undefined;
let httpCommandServer: HttpCommandServer|null = null;
const httpCredentialHelperServer = new HttpCredentialHelperServer();
if (!Electron.app.requestSingleInstanceLock()) {
gone = true;
process.exit(201);
}
// Scheme must be registered before the app is ready
Electron.protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } },
]);
process.on('unhandledRejection', (reason: any, promise: any) => {
if (reason.code === 'ECONNREFUSED' && reason.port === cfg.kubernetes.port) {
// Do nothing: a connection to the kubernetes server was broken
} else {
console.error('UnhandledRejectionWarning:', reason);
}
});
// takes care of any propagation of settings we want to do
// when settings change
mainEvents.on('settings-update', async(newSettings) => {
console.log(`mainEvents settings-update: ${ JSON.stringify(newSettings) }`);
if (newSettings.debug) {
setLogLevel('debug');
} else {
setLogLevel('info');
}
k8smanager.debug = newSettings.debug;
if (pathManager.strategy !== newSettings.pathManagementStrategy) {
await pathManager.remove();
pathManager = getPathManagerFor(newSettings.pathManagementStrategy);
await pathManager.enforce();
}
});
mainEvents.handle('settings-fetch', () => {
return Promise.resolve(cfg);
});
Electron.app.whenReady().then(async() => {
try {
const commandLineArgs = getCommandLineArgs();
DashboardServer.getInstance().init();
httpCommandServer = new HttpCommandServer(new BackgroundCommandWorker());
await httpCommandServer.init();
await httpCredentialHelperServer.init();
await setupNetworking();
cfg = settings.load();
if (commandLineArgs.length) {
try {
let transientConfig: settings.TransientSettings;
// Note that transientConfig and the returned cfg are aliases for
// the two global objects defined in `settings`.
[transientConfig, cfg] = settings.updateFromCommandLine(cfg, commandLineArgs);
k8smanager.noModalDialogs = noModalDialogs = transientConfig.noModalDialogs;
} catch (err) {
console.log(`Failed to update command from argument ${ commandLineArgs } `, err);
}
}
pathManager = getPathManagerFor(cfg.pathManagementStrategy);
mainEvents.emit('settings-update', cfg);
// Set up the updater; we may need to quit the app if an update is already
// queued.
if (await setupUpdate(cfg.updater, true)) {
gone = true;
// The update code will trigger a restart; don't do it here, as it may not
// be ready yet.
console.log('Will apply update; skipping startup.');
return;
}
installDevtools();
setupProtocolHandler();
await integrationManager.enforce();
await doFirstRunDialog();
if (gone) {
console.log('User triggered quit during first-run');
return;
}
buildApplicationMenu();
Electron.app.setAboutPanelOptions({
copyright: 'Copyright © 2021-2022 SUSE LLC', // TODO: Update this to 2021-... as dev progresses
applicationName: Electron.app.name,
applicationVersion: `Version ${ await getVersion() }`,
iconPath: path.join(paths.resources, 'icons', 'logo-square-512.png'),
});
setupTray();
window.openMain();
dockerDirManager.ensureCredHelperConfigured();
// Path management strategy will need to be selected after an upgrade
if (!os.platform().startsWith('win') && cfg.pathManagementStrategy === PathManagementStrategy.NotSet) {
if (!noModalDialogs) {
await window.openPathUpdate();
} else {
cfg.pathManagementStrategy = PathManagementStrategy.RcFiles;
}
}
if (os.platform() === 'linux' || os.platform() === 'darwin') {
try {
await removeLegacySymlinks(paths.oldIntegration);
} catch (error) {
if (error instanceof PermissionError) {
await window.openLegacyIntegrations();
} else {
throw error;
}
}
}
diagnostics.runChecks().catch(console.error);
await startBackend(cfg);
} catch (ex) {
console.error('Error starting up:', ex);
gone = true;
Electron.app.quit();
}
});
function installDevtools() {
if (Electron.app.isPackaged) {
return;
}
const { default: installExtension, VUEJS_DEVTOOLS } = require('electron-devtools-installer');
// No need to wait for it to complete, but handle any errors asynchronously
installExtension(VUEJS_DEVTOOLS).catch((err: any) => {
console.log(`Error installing VUEJS_DEVTOOLS: ${ err }`);
});
}
async function doFirstRunDialog() {
if (settings.firstRunDialogNeeded()) {
await window.openFirstRunDialog();
}
waitForFirstRunDialogCompletion = false;
}
/**
* Check if there are any reasons that would mean it makes no sense to continue
* starting the app. Should be invoked before attempting to start the backend.
*/
async function checkBackendValid() {
const invalidReason = await k8smanager.getBackendInvalidReason();
if (invalidReason) {
await handleFailure(invalidReason);
gone = true;
Electron.app.quit();
}
}
/**
* Start the Kubernetes backend.
*
* @precondition cfg.kubernetes.version is set.
*/
async function startBackend(cfg: settings.Settings) {
await checkBackendValid();
try {
await startK8sManager();
} catch (err) {
handleFailure(err);
}
}
/**
* Start the backend.
*
* @note Callers are responsible for handling errors thrown from here.
*/
async function startK8sManager() {
const changedContainerEngine = currentContainerEngine !== cfg.kubernetes.containerEngine;
currentContainerEngine = cfg.kubernetes.containerEngine;
enabledK8s = cfg.kubernetes.enabled;
if (changedContainerEngine) {
setupImageProcessor();
}
await k8smanager.start(cfg.kubernetes);
}
/**
* We need to deactivate the current imageProcessor, if there is one,
* so it stops processing events,
* and also tell the image event-handler about the new image processor.
*
* Some container engines support namespaces, so we need to specify the current namespace
* as well. It should be done here so that the consumers of the `current-engine-changed`
* event will operate in an environment where the image-processor knows the current namespace.
*/
function setupImageProcessor() {
const imageProcessor = getImageProcessor(cfg.kubernetes.containerEngine, k8smanager.executor);
currentImageProcessor?.deactivate();
if (!imageEventHandler) {
imageEventHandler = new ImageEventHandler(imageProcessor);
}
imageEventHandler.imageProcessor = imageProcessor;
currentImageProcessor = imageProcessor;
currentImageProcessor.activate();
currentImageProcessor.namespace = cfg.images.namespace;
window.send('k8s-current-engine', cfg.kubernetes.containerEngine);
}
Electron.app.on('second-instance', async() => {
// Someone tried to run another instance of Rancher Desktop,
// reveal and focus this window instead.
await protocolRegistered;
window.openMain();
});
interface K8sError {
errCode: number | string
}
function isK8sError(object: any): object is K8sError {
return 'errCode' in object;
}
Electron.app.on('before-quit', async(event) => {
if (gone) {
return;
}
event.preventDefault();
httpCommandServer?.closeServer();
httpCredentialHelperServer.closeServer();
try {
await k8smanager?.stop();
console.log(`2: Child exited cleanly.`);
} catch (ex) {
if (isK8sError(ex)) {
console.log(`2: Child exited with code ${ ex.errCode }`);
}
handleFailure(ex);
} finally {
gone = true;
if (process.env['APPIMAGE']) {
await integrationManager.removeSymlinksOnly();
}
Electron.app.quit();
}
});
Electron.app.on('window-all-closed', () => {
// On macOS, hide the dock icon.
Electron.app.dock?.hide();
// On windows and macOS platforms, we only quit via the notification tray / menu bar.
// On Linux we close the application since not all distros support tray menu/icons
if (os.platform() === 'linux' && !settings.firstRunDialogNeeded()) {
Electron.app.quit();
}
});
Electron.app.on('activate', async() => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (waitForFirstRunDialogCompletion) {
console.log('Still processing the first-run dialog: not opening main window');
return;
}
await protocolRegistered;
window.openMain();
});
ipcMainProxy.on('settings-read', (event) => {
event.reply('settings-read', cfg);
});
// This is the synchronous version of the above; we still use
// ipcRenderer.sendSync in some places, so it's required for now.
ipcMainProxy.on('settings-read', (event) => {
console.debug(`event settings-read in main: ${ JSON.stringify(cfg) }`);
event.returnValue = cfg;
});
ipcMainProxy.on('images-namespaces-read', (event) => {
if ([K8s.State.STARTED, K8s.State.DISABLED].includes(k8smanager.state)) {
currentImageProcessor?.relayNamespaces();
}
});
ipcMainProxy.on('dashboard-open', () => {
openDashboard();
});
ipcMainProxy.on('dashboard-close', () => {
closeDashboard();
});
ipcMainProxy.on('preferences-open', () => {
window.openMain(true);
});
ipcMainProxy.on('preferences-close', () => {
window.getWindow('preferences')?.close();
});
ipcMainProxy.on('preferences-set-dirty', (_event, dirtyFlag) => {
preferencesSetDirtyFlag(dirtyFlag);
});
function writeSettings(arg: RecursivePartial<settings.Settings>) {
_.merge(cfg, arg);
settings.save(cfg);
mainEvents.emit('settings-update', cfg);
}
ipcMainProxy.handle('settings-write', (event, arg) => {
writeSettings(arg);
// dashboard requires kubernetes, so we want to close it if kubernetes is disabled
if (arg?.kubernetes?.enabled === false) {
closeDashboard();
}
event.sender.sendToFrame(event.frameId, 'settings-update', cfg);
});
mainEvents.on('settings-write', writeSettings);
ipcMainProxy.on('k8s-state', (event) => {
event.returnValue = k8smanager.state;
});
ipcMainProxy.on('k8s-current-engine', () => {
window.send('k8s-current-engine', currentContainerEngine);
});
ipcMainProxy.on('k8s-current-port', () => {
window.send('k8s-current-port', k8smanager.kubeBackend.desiredPort);
});
ipcMainProxy.on('k8s-reset', async(_, arg) => {
await doK8sReset(arg, { interactive: true });
});
ipcMainProxy.on('api-get-credentials', () => {
mainEvents.emit('api-get-credentials');
});
Electron.ipcMain.handle('api-get-credentials', () => {
return new Promise<void>((resolve) => {
mainEvents.once('api-credentials', resolve);
mainEvents.emit('api-get-credentials');
});
});
mainEvents.on('api-credentials', (credentials) => {
window.send('api-credentials', credentials);
});
function backendIsBusy() {
return [K8s.State.STARTING, K8s.State.STOPPING].includes(k8smanager.state);
}
async function doK8sReset(arg: 'fast' | 'wipe' | 'fullRestart', context: CommandWorkerInterface.CommandContext): Promise<void> {
// If not in a place to restart than skip it
if (backendIsBusy()) {
console.log(`Skipping reset, invalid state ${ k8smanager.state }`);
return;
}
try {
switch (arg) {
case 'fast':
await k8smanager.reset(cfg.kubernetes);
break;
case 'fullRestart':
await k8smanager.stop();
console.log(`Stopped Kubernetes backend cleanly.`);
await startK8sManager();
break;
case 'wipe':
await k8smanager.stop();
console.log(`Stopped Kubernetes backend cleanly.`);
console.log('Deleting VM to reset...');
await k8smanager.del();
console.log(`Deleted VM to reset exited cleanly.`);
await startK8sManager();
break;
}
} catch (ex) {
if (context.interactive) {
handleFailure(ex);
} else {
console.error(ex);
}
}
}
ipcMainProxy.on('k8s-restart', async() => {
if (cfg.kubernetes.port !== k8smanager.kubeBackend.desiredPort) {
// On port change, we need to wipe the VM.
return doK8sReset('wipe', { interactive: true });
} else if (cfg.kubernetes.containerEngine !== currentContainerEngine || cfg.kubernetes.enabled !== enabledK8s) {
return doK8sReset('fullRestart', { interactive: true });
}
try {
switch (k8smanager.state) {
case K8s.State.STOPPED:
case K8s.State.STARTED:
case K8s.State.DISABLED:
// Calling start() will restart the backend, possible switching versions
// as a side-effect.
await startK8sManager();
break;
}
} catch (ex) {
handleFailure(ex);
}
});
ipcMainProxy.on('k8s-versions', async() => {
window.send('k8s-versions', await k8smanager.kubeBackend.availableVersions, await k8smanager.kubeBackend.cachedVersionsOnly());
});
ipcMainProxy.on('k8s-progress', () => {
window.send('k8s-progress', k8smanager.progress);
});
ipcMainProxy.handle('service-fetch', (_, namespace) => {
return k8smanager.kubeBackend.listServices(namespace);
});
ipcMainProxy.handle('service-forward', async(_, service, state) => {
if (state) {
const hostPort = service.listenPort ?? 0;
await k8smanager.kubeBackend.forwardPort(service.namespace, service.name, service.port, hostPort);
} else {
await k8smanager.kubeBackend.cancelForward(service.namespace, service.name, service.port);
}
});
ipcMainProxy.on('k8s-integrations', async() => {
mainEvents.emit('integration-update', await integrationManager.listIntegrations() ?? {});
});
ipcMainProxy.on('k8s-integration-set', (event, name, newState) => {
writeSettings({ kubernetes: { WSLIntegrations: { [name]: newState } } });
});
mainEvents.on('integration-update', (state) => {
window.send('k8s-integrations', state);
});
/**
* Do a factory reset of the application. This will stop the currently running
* cluster (if any), and delete all of its data. This will also remove any
* rancher-desktop data, and restart the application.
*/
async function doFactoryReset(keepSystemImages: boolean) {
await k8smanager.factoryReset(keepSystemImages);
await pathManager.remove();
await integrationManager.remove();
if (os.platform() === 'win32') {
// On Windows, we need to use a helper process in order to ensure we
// delete files in use. Of course, we can't wait for that process to
// return - the whole point is for us to not be running.
childProcess.spawn(path.join(paths.resources, 'win32', 'wsl-helper.exe'),
['factory-reset', `--wait-pid=${ process.pid }`, `--keep-system-images=${ keepSystemImages ? 'true' : 'false' }`],
{ detached: true, windowsHide: true });
Electron.app.quit();
return;
}
// Remove app settings
await settings.clear();
Electron.app.quit();
}
ipcMainProxy.on('factory-reset', (event, keepSystemImages) => {
doFactoryReset(keepSystemImages).catch((err) => {
console.error(err);
});
});
ipcMainProxy.on('troubleshooting/show-logs', async(event) => {
const error = await Electron.shell.openPath(paths.logs);
if (error) {
const browserWindow = Electron.BrowserWindow.fromWebContents(event.sender);
const options = {
message: error,
type: 'error',
title: `Error opening logs`,
detail: `Please manually open ${ paths.logs }`,
};
console.error(`Failed to open logs: ${ error }`);
if (browserWindow) {
await Electron.dialog.showMessageBox(browserWindow, options);
} else {
await Electron.dialog.showMessageBox(options);
}
}
});
ipcMainProxy.on('diagnostics/run', () => {
diagnostics.runChecks();
});
ipcMainProxy.on('get-app-version', async(event) => {
event.reply('get-app-version', await getVersion());
});
ipcMainProxy.handle('show-message-box', (_event, options: Electron.MessageBoxOptions, modal = false): Promise<Electron.MessageBoxReturnValue> => {
return window.showMessageBox(options, modal);
});
Electron.ipcMain.handle('show-message-box-rd', async(_event, options: Electron.MessageBoxOptions, modal = false) => {
const mainWindow = modal ? window.getWindow('main') : null;
const dialog = window.openDialog(
'Dialog',
{
modal,
parent: mainWindow || undefined,
frame: true,
title: options.title,
height: 225,
});
let response: any;
dialog.webContents.on('ipc-message', (_event, channel, args) => {
if (channel === 'dialog/mounted') {
dialog.webContents.send('dialog/options', options);
}
if (channel === 'dialog/close') {
response = args || { response: options.cancelId };
dialog.close();
}
});
dialog.on('close', () => {
if (response) {
return;
}
response = { response: options.cancelId };
});
await (new Promise<void>((resolve) => {
dialog.on('closed', resolve);
}));
return response;
});
function getProductionVersion() {
try {
return Electron.app.getVersion();
} catch (err) {
console.log(`Can't get app version: ${ err }`);
return '?';
}
}
async function getDevVersion() {
try {
const { stdout } = await childProcess.spawnFile('git', ['describe', '--tags'], { stdio: ['ignore', 'pipe', 'inherit'] });
return stdout.trim();
} catch (err) {
console.log(`Can't get app version: ${ err }`);
return '?';
}
}
async function getVersion() {
return process.env.NODE_ENV === 'production' ? getProductionVersion() : await getDevVersion();
}
function showErrorDialog(title: string, message: string, fatal?: boolean) {
Electron.dialog.showErrorBox(title, message);
if (fatal) {
process.exit(0);
}
}
async function handleFailure(payload: any) {
let titlePart = 'Error Starting Kubernetes';
let message = 'There was an unknown error starting Kubernetes';
let secondaryMessage = '';
if (payload instanceof K8s.KubernetesError) {
({ name: titlePart, message } = payload);
} else if (payload instanceof Error) {
secondaryMessage = payload.toString();
} else if (typeof payload === 'number') {
message = `Kubernetes was unable to start with the following exit code: ${ payload }`;
} else if ('errorCode' in payload) {
message = payload.message || message;
titlePart = payload.context || titlePart;
}
console.log(`Kubernetes was unable to start:`, payload);
try {
// getFailureDetails is going to read from existing log files.
// Wait 1 second before reading them to allow recent writes to appear in them.
await util.promisify(setTimeout)(1_000);
const failureDetails: K8s.FailureDetails = await k8smanager.getFailureDetails(payload);
if (failureDetails) {
if (noModalDialogs) {
console.log(titlePart);
console.log(secondaryMessage || message);
console.log(failureDetails);
gone = true;
Electron.app.quit();
} else {
await window.openKubernetesErrorMessageWindow(titlePart, secondaryMessage || message, failureDetails);
}
return;
}
} catch (e) {
console.log(`Failed to get failure details: `, e);
}
if (noModalDialogs) {
console.log(titlePart);
console.log(message);
gone = true;
Electron.app.quit();
} else {
showErrorDialog(titlePart, message, payload instanceof K8s.KubernetesError && payload.fatal);
}
}
function doFullRestart(context: CommandWorkerInterface.CommandContext) {
doK8sReset('fullRestart', context).catch((err: any) => {
console.log(`Error restarting: ${ err }`);
});
}
function newK8sManager() {
const arch = (Electron.app.runningUnderARM64Translation || os.arch() === 'arm64') ? 'aarch64' : 'x86_64';
const mgr = K8sFactory(arch, dockerDirManager);
mgr.kubeBackend.on('state-changed', (state: K8s.State) => {
mainEvents.emit('k8s-check-state', mgr);
window.send('k8s-check-state', state);
if ([K8s.State.STARTED, K8s.State.DISABLED].includes(state)) {
if (!cfg.kubernetes.version) {
writeSettings({ kubernetes: { version: mgr.kubeBackend.version } });
}
currentImageProcessor?.relayNamespaces();
if (enabledK8s) {
Steve.getInstance().start();
}
}
if (state === K8s.State.STOPPING) {
Steve.getInstance().stop();
}
if (pendingRestartContext !== undefined && !backendIsBusy()) {
// If we restart immediately the QEMU process in the VM doesn't always respond to a shutdown messages
setTimeout(doFullRestart, 2_000, pendingRestartContext);
pendingRestartContext = undefined;
}
});
mgr.kubeBackend.on('current-port-changed', (port: number) => {
window.send('k8s-current-port', port);
});
mgr.kubeBackend.on('kim-builder-uninstalled', () => {
writeSettings({ kubernetes: { checkForExistingKimBuilder: false } });
});
mgr.kubeBackend.on('service-changed', (services: K8s.ServiceEntry[]) => {
console.debug(`service-changed: ${ JSON.stringify(services) }`);
window.send('service-changed', services);
});
mgr.kubeBackend.on('service-error', (service: K8s.ServiceEntry, errorMessage: string) => {
console.debug(`service-error: ${ errorMessage }, ${ JSON.stringify(service) }`);
window.send('service-error', service, errorMessage);
});
mgr.kubeBackend.on('progress', () => {
window.send('k8s-progress', mgr.progress);
});
mgr.kubeBackend.on('versions-updated', async() => {
window.send('k8s-versions', await mgr.kubeBackend.availableVersions, await mgr.kubeBackend.cachedVersionsOnly());
});
mgr.kubeBackend.on('show-notification', (notificationOptions: Electron.NotificationConstructorOptions) => {
(new Electron.Notification(notificationOptions)).show();
});
return mgr;
}
/**
* Implement the methods the HttpCommandServer needs to service its requests.
* These methods do two things:
* 1. Verify the semantics of the parameters (the server just checks syntax).
* 2. Provide a thin wrapper over existing functionality in this module.
* Getters, on success, return status 200 and a string that may be JSON or simple.
* Setters, on success, return status 202, possibly with a human-readable status note.
* The `requestShutdown` method is a special case that never returns.
*/
class BackgroundCommandWorker implements CommandWorkerInterface {
protected k8sVersions: string[] = [];
protected settingsValidator = new SettingsValidator();
/**
* Use the settings validator to validate settings after doing any
* initialization.
*/
protected async validateSettings(...args: Parameters<SettingsValidator['validateSettings']>) {
if (this.k8sVersions.length === 0) {
this.k8sVersions = (await k8smanager.kubeBackend.availableVersions).map(entry => entry.version.version);
this.settingsValidator.k8sVersions = this.k8sVersions;
}
return this.settingsValidator.validateSettings(...args);
}
getSettings() {
return jsonStringifyWithWhiteSpace(cfg);
}
getDiagnosticCategories(): string[]|undefined {
return diagnostics.getCategoryNames();
}
getDiagnosticIdsByCategory(category: string): string[]|undefined {
return diagnostics.getIdsForCategory(category);
}
getDiagnosticChecks(category: string|null, checkID: string|null): Promise<DiagnosticsResultCollection> {
return diagnostics.getChecks(category, checkID);
}
runDiagnosticChecks(): Promise<DiagnosticsResultCollection> {
return diagnostics.runChecks();
}
factoryReset(keepSystemImages: boolean) {
doFactoryReset(keepSystemImages).catch((err) => {
console.error(err);
});
}
/**
* Check semantics of SET commands:
* - verify that setting names are recognized, and validate provided values
* - returns an array of two strings:
* 1. a description of the status of the request, if it was valid
* 2. a list of any errors in the request body.
* @param newSettings: a subset of the Settings object, containing the desired values
* @returns [{string} description of final state if no error, {string} error message]
*/
async updateSettings(context: CommandWorkerInterface.CommandContext, newSettings: RecursivePartial<settings.Settings>): Promise<[string, string]> {
const [needToUpdate, errors] = await this.validateSettings(cfg, newSettings);
if (errors.length > 0) {
return ['', `errors in attempt to update settings:\n${ errors.join('\n') }`];
}
if (needToUpdate) {
writeSettings(newSettings);
// cfg is a global, and at this point newConfig has been merged into it :(
window.send('settings-update', cfg);
} else {
// Obviously if there are no settings to update, there's no need to restart.
return ['no changes necessary', ''];
}
// Check if the newly applied preferences demands a restart of the backend.
const restartReasons = await k8smanager.requiresRestartReasons(cfg.kubernetes);
if (Object.keys(restartReasons).length === 0) {
return ['settings updated; no restart required', ''];
}
// Trigger a restart of the backend (possibly delayed).
if (!backendIsBusy()) {
pendingRestartContext = undefined;
setImmediate(doFullRestart, context);
return ['reconfiguring Rancher Desktop to apply changes (this may take a while)', ''];
} else {
// Call doFullRestart once the UI is finished starting or stopping
pendingRestartContext = context;
return ['UI is currently busy, but will eventually be reconfigured to apply requested changes', ''];
}
}
async proposeSettings(context: CommandWorkerInterface.CommandContext, newSettings: RecursivePartial<settings.Settings>): Promise<[string, string]> {
const [, errors] = await this.validateSettings(cfg, newSettings);
if (errors.length > 0) {
return ['', `Errors in proposed settings:\n${ errors.join('\n') }`];
}
const result = await k8smanager?.requiresRestartReasons(newSettings?.kubernetes ?? {}) ?? {};
return [JSON.stringify(result), ''];
}
async requestShutdown() {
httpCommandServer?.closeServer();
httpCredentialHelperServer.closeServer();
await k8smanager.stop();
Electron.app.quit();
}
}