mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Minor cleanup of Kubectl's downloadKubectl and writeInitScripts (#4556)
This commit is contained in:
parent
446eb5ca43
commit
56c15e6b0f
@ -34,7 +34,9 @@ const electronRemote = (() => {
|
|||||||
if (ipcRenderer) {
|
if (ipcRenderer) {
|
||||||
try {
|
try {
|
||||||
return require("@electron/remote");
|
return require("@electron/remote");
|
||||||
} catch {}
|
} catch {
|
||||||
|
// ignore temp
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@ -27,12 +27,14 @@ import { ensureDir, pathExists } from "fs-extra";
|
|||||||
import * as lockFile from "proper-lockfile";
|
import * as lockFile from "proper-lockfile";
|
||||||
import { helmCli } from "./helm/helm-cli";
|
import { helmCli } from "./helm/helm-cli";
|
||||||
import { UserStore } from "../common/user-store";
|
import { UserStore } from "../common/user-store";
|
||||||
import { customRequest } from "../common/request";
|
|
||||||
import { getBundledKubectlVersion } from "../common/utils/app-version";
|
import { getBundledKubectlVersion } from "../common/utils/app-version";
|
||||||
import { isDevelopment, isWindows, isTestEnv } from "../common/vars";
|
import { isDevelopment, isWindows, isTestEnv } from "../common/vars";
|
||||||
import { SemVer } from "semver";
|
import { SemVer } from "semver";
|
||||||
import { defaultPackageMirror, packageMirrors } from "../common/user-store/preferences-helpers";
|
import { defaultPackageMirror, packageMirrors } from "../common/user-store/preferences-helpers";
|
||||||
import { AppPaths } from "../common/app-paths";
|
import { AppPaths } from "../common/app-paths";
|
||||||
|
import got from "got/dist/source";
|
||||||
|
import { promisify } from "util";
|
||||||
|
import stream from "stream";
|
||||||
|
|
||||||
const bundledVersion = getBundledKubectlVersion();
|
const bundledVersion = getBundledKubectlVersion();
|
||||||
const kubectlMap: Map<string, string> = new Map([
|
const kubectlMap: Map<string, string> = new Map([
|
||||||
@ -53,7 +55,7 @@ const kubectlMap: Map<string, string> = new Map([
|
|||||||
["1.21", bundledVersion],
|
["1.21", bundledVersion],
|
||||||
]);
|
]);
|
||||||
let bundledPath: string;
|
let bundledPath: string;
|
||||||
const initScriptVersionString = "# lens-initscript v3\n";
|
const initScriptVersionString = "# lens-initscript v3";
|
||||||
|
|
||||||
export function bundledKubectlPath(): string {
|
export function bundledKubectlPath(): string {
|
||||||
if (bundledPath) { return bundledPath; }
|
if (bundledPath) { return bundledPath; }
|
||||||
@ -309,99 +311,86 @@ export class Kubectl {
|
|||||||
|
|
||||||
logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`);
|
logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`);
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
const downloadStream = got.stream({ url: this.url, decompress: true });
|
||||||
const stream = customRequest({
|
const fileWriteStream = fs.createWriteStream(this.path, { mode: 0o755 });
|
||||||
url: this.url,
|
const pipeline = promisify(stream.pipeline);
|
||||||
gzip: true,
|
|
||||||
});
|
|
||||||
const file = fs.createWriteStream(this.path);
|
|
||||||
|
|
||||||
stream.on("complete", () => {
|
await pipeline(downloadStream, fileWriteStream);
|
||||||
logger.debug("kubectl binary download finished");
|
logger.debug("kubectl binary download finished");
|
||||||
file.end();
|
|
||||||
});
|
|
||||||
stream.on("error", (error) => {
|
|
||||||
logger.error(error);
|
|
||||||
fs.unlink(this.path, () => {
|
|
||||||
// do nothing
|
|
||||||
});
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
file.on("close", () => {
|
|
||||||
logger.debug("kubectl binary download closed");
|
|
||||||
fs.chmod(this.path, 0o755, (err) => {
|
|
||||||
if (err) reject(err);
|
|
||||||
});
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
stream.pipe(file);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async writeInitScripts() {
|
protected async writeInitScripts() {
|
||||||
const kubectlPath = UserStore.getInstance().downloadKubectlBinaries ? this.dirname : path.dirname(this.getPathFromPreferences());
|
const kubectlPath = UserStore.getInstance().downloadKubectlBinaries
|
||||||
|
? this.dirname
|
||||||
|
: path.dirname(this.getPathFromPreferences());
|
||||||
const helmPath = helmCli.getBinaryDir();
|
const helmPath = helmCli.getBinaryDir();
|
||||||
const fsPromises = fs.promises;
|
|
||||||
const bashScriptPath = path.join(this.dirname, ".bash_set_path");
|
const bashScriptPath = path.join(this.dirname, ".bash_set_path");
|
||||||
let bashScript = `${initScriptVersionString}`;
|
const bashScript = [
|
||||||
|
initScriptVersionString,
|
||||||
bashScript += "tempkubeconfig=\"$KUBECONFIG\"\n";
|
"tempkubeconfig=\"$KUBECONFIG\"",
|
||||||
bashScript += "test -f \"/etc/profile\" && . \"/etc/profile\"\n";
|
"test -f \"/etc/profile\" && . \"/etc/profile\"",
|
||||||
bashScript += "if test -f \"$HOME/.bash_profile\"; then\n";
|
"if test -f \"$HOME/.bash_profile\"; then",
|
||||||
bashScript += " . \"$HOME/.bash_profile\"\n";
|
" . \"$HOME/.bash_profile\"",
|
||||||
bashScript += "elif test -f \"$HOME/.bash_login\"; then\n";
|
"elif test -f \"$HOME/.bash_login\"; then",
|
||||||
bashScript += " . \"$HOME/.bash_login\"\n";
|
" . \"$HOME/.bash_login\"",
|
||||||
bashScript += "elif test -f \"$HOME/.profile\"; then\n";
|
"elif test -f \"$HOME/.profile\"; then",
|
||||||
bashScript += " . \"$HOME/.profile\"\n";
|
" . \"$HOME/.profile\"",
|
||||||
bashScript += "fi\n";
|
"fi",
|
||||||
bashScript += `export PATH="${helmPath}:${kubectlPath}:$PATH"\n`;
|
`export PATH="${helmPath}:${kubectlPath}:$PATH"`,
|
||||||
bashScript += "export KUBECONFIG=\"$tempkubeconfig\"\n";
|
'export KUBECONFIG="$tempkubeconfig"',
|
||||||
|
`NO_PROXY=",\${NO_PROXY:-localhost},"`,
|
||||||
bashScript += `NO_PROXY=",\${NO_PROXY:-localhost},"\n`;
|
`NO_PROXY="\${NO_PROXY//,localhost,/,}"`,
|
||||||
bashScript += `NO_PROXY="\${NO_PROXY//,localhost,/,}"\n`;
|
`NO_PROXY="\${NO_PROXY//,127.0.0.1,/,}"`,
|
||||||
bashScript += `NO_PROXY="\${NO_PROXY//,127.0.0.1,/,}"\n`;
|
`NO_PROXY="localhost,127.0.0.1\${NO_PROXY%,}"`,
|
||||||
bashScript += `NO_PROXY="localhost,127.0.0.1\${NO_PROXY%,}"\n`;
|
"export NO_PROXY",
|
||||||
bashScript += "export NO_PROXY\n";
|
"unset tempkubeconfig",
|
||||||
bashScript += "unset tempkubeconfig\n";
|
].join("\n");
|
||||||
await fsPromises.writeFile(bashScriptPath, bashScript.toString(), { mode: 0o644 });
|
|
||||||
|
|
||||||
const zshScriptPath = path.join(this.dirname, ".zlogin");
|
const zshScriptPath = path.join(this.dirname, ".zlogin");
|
||||||
let zshScript = `${initScriptVersionString}`;
|
const zshScript = [
|
||||||
|
initScriptVersionString,
|
||||||
|
"tempkubeconfig=\"$KUBECONFIG\"",
|
||||||
|
|
||||||
zshScript += "tempkubeconfig=\"$KUBECONFIG\"\n";
|
// restore previous ZDOTDIR
|
||||||
// restore previous ZDOTDIR
|
"export ZDOTDIR=\"$OLD_ZDOTDIR\"",
|
||||||
zshScript += "export ZDOTDIR=\"$OLD_ZDOTDIR\"\n";
|
|
||||||
// source all the files
|
|
||||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zshenv\" && . \"$OLD_ZDOTDIR/.zshenv\"\n";
|
|
||||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zprofile\" && . \"$OLD_ZDOTDIR/.zprofile\"\n";
|
|
||||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zlogin\" && . \"$OLD_ZDOTDIR/.zlogin\"\n";
|
|
||||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zshrc\" && . \"$OLD_ZDOTDIR/.zshrc\"\n";
|
|
||||||
|
|
||||||
// voodoo to replace any previous occurrences of kubectl path in the PATH
|
// source all the files
|
||||||
zshScript += `kubectlpath="${kubectlPath}"\n`;
|
"test -f \"$OLD_ZDOTDIR/.zshenv\" && . \"$OLD_ZDOTDIR/.zshenv\"",
|
||||||
zshScript += `helmpath="${helmPath}"\n`;
|
"test -f \"$OLD_ZDOTDIR/.zprofile\" && . \"$OLD_ZDOTDIR/.zprofile\"",
|
||||||
zshScript += "p=\":$kubectlpath:\"\n";
|
"test -f \"$OLD_ZDOTDIR/.zlogin\" && . \"$OLD_ZDOTDIR/.zlogin\"",
|
||||||
zshScript += "d=\":$PATH:\"\n";
|
"test -f \"$OLD_ZDOTDIR/.zshrc\" && . \"$OLD_ZDOTDIR/.zshrc\"",
|
||||||
zshScript += `d=\${d//$p/:}\n`;
|
|
||||||
zshScript += `d=\${d/#:/}\n`;
|
// voodoo to replace any previous occurrences of kubectl path in the PATH
|
||||||
zshScript += `export PATH="$helmpath:$kubectlpath:\${d/%:/}"\n`;
|
`kubectlpath="${kubectlPath}"`,
|
||||||
zshScript += "export KUBECONFIG=\"$tempkubeconfig\"\n";
|
`helmpath="${helmPath}"`,
|
||||||
zshScript += `NO_PROXY=",\${NO_PROXY:-localhost},"\n`;
|
"p=\":$kubectlpath:\"",
|
||||||
zshScript += `NO_PROXY="\${NO_PROXY//,localhost,/,}"\n`;
|
"d=\":$PATH:\"",
|
||||||
zshScript += `NO_PROXY="\${NO_PROXY//,127.0.0.1,/,}"\n`;
|
`d=\${d//$p/:}`,
|
||||||
zshScript += `NO_PROXY="localhost,127.0.0.1\${NO_PROXY%,}"\n`;
|
`d=\${d/#:/}`,
|
||||||
zshScript += "export NO_PROXY\n";
|
`export PATH="$helmpath:$kubectlpath:\${d/%:/}"`,
|
||||||
zshScript += "unset tempkubeconfig\n";
|
"export KUBECONFIG=\"$tempkubeconfig\"",
|
||||||
zshScript += "unset OLD_ZDOTDIR\n";
|
`NO_PROXY=",\${NO_PROXY:-localhost},"`,
|
||||||
await fsPromises.writeFile(zshScriptPath, zshScript.toString(), { mode: 0o644 });
|
`NO_PROXY="\${NO_PROXY//,localhost,/,}"`,
|
||||||
|
`NO_PROXY="\${NO_PROXY//,127.0.0.1,/,}"`,
|
||||||
|
`NO_PROXY="localhost,127.0.0.1\${NO_PROXY%,}"`,
|
||||||
|
"export NO_PROXY",
|
||||||
|
"unset tempkubeconfig",
|
||||||
|
"unset OLD_ZDOTDIR",
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
fs.promises.writeFile(bashScriptPath, bashScript, { mode: 0o644 }),
|
||||||
|
fs.promises.writeFile(zshScriptPath, zshScript, { mode: 0o644 }),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getDownloadMirror(): string {
|
protected getDownloadMirror(): string {
|
||||||
// MacOS packages are only available from default
|
// MacOS packages are only available from default
|
||||||
|
|
||||||
const mirror = packageMirrors.get(UserStore.getInstance().downloadMirror)
|
const { url } = packageMirrors.get(UserStore.getInstance().downloadMirror)
|
||||||
?? packageMirrors.get(defaultPackageMirror);
|
?? packageMirrors.get(defaultPackageMirror);
|
||||||
|
|
||||||
return mirror.url;
|
return url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user