-
Notifications
You must be signed in to change notification settings - Fork 12
/
minikube-installer.ts
182 lines (160 loc) · 6.67 KB
/
minikube-installer.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
/**********************************************************************
* Copyright (C) 2023-2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import type { components } from '@octokit/openapi-types';
import { Octokit } from '@octokit/rest';
import * as extensionApi from '@podman-desktop/api';
import { ProgressLocation } from '@podman-desktop/api';
import { installBinaryToSystem } from './util';
const githubOrganization = 'kubernetes';
const githubRepo = 'minikube';
type GitHubRelease = components['schemas']['release'];
export interface AssetInfo {
id: number;
name: string;
}
const WINDOWS_X64_PLATFORM = 'win32-x64';
const LINUX_X64_PLATFORM = 'linux-x64';
const LINUX_ARM64_PLATFORM = 'linux-arm64';
const MACOS_X64_PLATFORM = 'darwin-x64';
const MACOS_ARM64_PLATFORM = 'darwin-arm64';
const WINDOWS_X64_ASSET_NAME = 'minikube-windows-amd64.exe';
const LINUX_X64_ASSET_NAME = 'minikube-linux-amd64';
const LINUX_ARM64_ASSET_NAME = 'minikube-linux-arm64';
const MACOS_X64_ASSET_NAME = 'minikube-darwin-amd64';
const MACOS_ARM64_ASSET_NAME = 'minikube-darwin-arm64';
/**
* @deprecated use {@link MinikubeDownload} instead
*/
export class MinikubeInstaller {
private assetNames = new Map<string, string>();
private assetPromise: Promise<AssetInfo>;
constructor(
private readonly storagePath: string,
private telemetryLogger: extensionApi.TelemetryLogger,
) {
this.assetNames.set(WINDOWS_X64_PLATFORM, WINDOWS_X64_ASSET_NAME);
this.assetNames.set(LINUX_X64_PLATFORM, LINUX_X64_ASSET_NAME);
this.assetNames.set(LINUX_ARM64_PLATFORM, LINUX_ARM64_ASSET_NAME);
this.assetNames.set(MACOS_X64_PLATFORM, MACOS_X64_ASSET_NAME);
this.assetNames.set(MACOS_ARM64_PLATFORM, MACOS_ARM64_ASSET_NAME);
}
findAssetInfo(data: GitHubRelease[], assetName: string): AssetInfo {
for (const release of data) {
for (const asset of release.assets) {
if (asset.name === assetName) {
return {
id: asset.id,
name: assetName,
};
}
}
}
return undefined;
}
async getAssetInfo(): Promise<AssetInfo> {
if (!(await this.assetPromise)) {
const assetName = this.assetNames.get(os.platform().concat('-').concat(os.arch()));
const octokit = new Octokit();
this.assetPromise = octokit.repos
.listReleases({ owner: githubOrganization, repo: githubRepo })
.then(response => this.findAssetInfo(response.data, assetName))
.catch((error: unknown) => {
console.error(error);
return undefined;
});
}
return this.assetPromise;
}
async isAvailable(): Promise<boolean> {
const assetInfo = await this.getAssetInfo();
return assetInfo !== undefined;
}
async performInstall(): Promise<boolean> {
this.telemetryLogger.logUsage('install-minikube-prompt');
const dialogResult = await extensionApi.window.showInformationMessage(
'The minikube binary is required for local Kubernetes development, would you like to download it?',
'Yes',
'Cancel',
);
if (dialogResult === 'Yes') {
this.telemetryLogger.logUsage('install-minikube-prompt-yes');
return extensionApi.window.withProgress(
{ location: ProgressLocation.TASK_WIDGET, title: 'Installing minikube' },
async progress => {
progress.report({ increment: 5 });
try {
const assetInfo = await this.getAssetInfo();
if (assetInfo) {
const octokit = new Octokit();
const asset = await octokit.repos.getReleaseAsset({
owner: githubOrganization,
repo: githubRepo,
asset_id: assetInfo.id,
headers: {
accept: 'application/octet-stream',
},
});
progress.report({ increment: 80 });
if (asset) {
const destFile = path.resolve(this.storagePath, assetInfo.name);
if (!fs.existsSync(this.storagePath)) {
fs.mkdirSync(this.storagePath);
}
fs.appendFileSync(destFile, Buffer.from(asset.data as unknown as ArrayBuffer));
if (!extensionApi.env.isWindows) {
const stat = fs.statSync(destFile);
fs.chmodSync(destFile, stat.mode | fs.constants.S_IXUSR);
}
// Explain to the user that the binary has been successfully installed to the storage path
// prompt and ask if they want to install it system-wide (copied to /usr/bin/, or AppData for Windows)
const result = await extensionApi.window.showInformationMessage(
`minikube binary has been succesfully downloaded to ${destFile}.\n\nWould you like to install it system-wide for accessibility on the command line? This will require administrative privileges.`,
'Yes',
'Cancel',
);
if (result === 'Yes') {
try {
// Move the binary file to the system from destFile and rename to 'minikube'
await installBinaryToSystem(destFile, 'minikube');
await extensionApi.window.showInformationMessage(
'minikube binary has been successfully installed system-wide.',
);
} catch (error) {
console.error(error);
await extensionApi.window.showErrorMessage(`Unable to install minikube binary: ${error}`);
}
}
this.telemetryLogger.logUsage('install-minikube-downloaded');
extensionApi.window.showNotification({ body: 'minikube is successfully installed.' });
return true;
}
}
} finally {
progress.report({ increment: -1 });
}
},
);
} else {
this.telemetryLogger.logUsage('install-minikube-prompt-no');
}
return false;
}
}