Merge pull request #289 from mook-as/win-handle-no-wsl

WSL: Provide better error message when WSL is not installed.
This commit is contained in:
Matt Farina
2021-05-13 14:45:17 -07:00
committed by GitHub
5 changed files with 69 additions and 5 deletions

View File

@@ -88,6 +88,19 @@ Electron.app.whenReady().then(async() => {
console.log(cfg);
tray.emit('settings-update', cfg);
k8smanager = newK8sManager(cfg.kubernetes);
// Check if there are any reasons that would mean it makes no sense to
// continue starting the app.
const invalidReason = await k8smanager.getBackendInvalidReason();
if (invalidReason) {
handleFailure(invalidReason);
gone = true;
Electron.app.quit();
return;
}
imageManager = new Kim();
interface KimImage {
imageName: string,
@@ -540,14 +553,20 @@ async function linkResource(name: string, state: boolean): Promise<Error | null>
}
function handleFailure(payload: any) {
let { errorCode, message, context: titlePart } = payload;
let titlePart = 'Starting Kubernetes';
let message = 'There was an unknown error starting Kubernetes';
if (typeof (payload) === 'number') {
errorCode = payload;
if (payload instanceof K8s.KubernetesError) {
({ name: titlePart, message } = payload);
} else if (payload instanceof Error) {
message += `: ${ payload }`;
} 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 with exit code: ${ errorCode }`);
titlePart = titlePart || 'Starting Kubernetes';
console.log(`Kubernetes was unable to start:`, payload);
Electron.dialog.showErrorBox(`Error ${ titlePart }`, message);
}

View File

@@ -254,6 +254,10 @@ export default class HyperkitBackend extends events.EventEmitter implements K8s.
})();
}
getBackendInvalidReason(): Promise<K8s.KubernetesError | null> {
return Promise.resolve(null);
}
async start(): Promise<void> {
const desiredVersion = await this.desiredVersion;

View File

@@ -39,6 +39,13 @@ export interface VersionLister {
on(event: 'versions-updated', callback: ()=>void): void;
}
export class KubernetesError extends Error {
constructor(name: string, message: string) {
super(message);
this.name = name;
}
}
export interface KubernetesBackend extends events.EventEmitter {
state: State;
@@ -57,6 +64,13 @@ export interface KubernetesBackend extends events.EventEmitter {
/** The amount of memory in the VM, in MiB, or 0 if the VM is not running. */
memory: Promise<number>;
/**
* Check if the current backend is valid.
* @returns Null if the backend is valid, otherwise an error describing why
* the backend is invalid that can be shown to the user.
*/
getBackendInvalidReason(): Promise<KubernetesError | null>;
/** Start the Kubernetes cluster. */
start(): Promise<void>;

View File

@@ -38,6 +38,10 @@ class OSNotImplemented extends events.EventEmitter {
return Promise.reject(new Error('not implemented'));
}
getBackendInvalidReason() {
return Promise.resolve(null);
}
start() {
this.#notified = displayError(this.#notified);

View File

@@ -143,6 +143,29 @@ export default class WSLBackend extends events.EventEmitter implements K8s.Kuber
}
}
async getBackendInvalidReason(): Promise<K8s.KubernetesError | null> {
// Check if wsl.exe is available
try {
await this.isDistroRegistered();
} catch (ex) {
if ((ex as NodeJS.ErrnoException).code === 'ENOENT') {
console.log('Error launching WSL: it does not appear to be installed.');
const message = `
Windows Subsystem for Linux does not appear to be installed.
Please install it manually:
https://docs.microsoft.com/en-us/windows/wsl/install-win10
`.replace(/[ \t]{2,}/g, '');
return new K8s.KubernetesError('WSL Not Installed', message);
}
throw ex;
}
return null;
}
async start(): Promise<void> {
try {
if (this.process) {