1
0
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:
Sebastian Malton 2022-01-10 10:48:58 -05:00 committed by GitHub
parent 446eb5ca43
commit 56c15e6b0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 80 deletions

View File

@ -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;

View File

@ -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
zshScript += "export ZDOTDIR=\"$OLD_ZDOTDIR\"\n"; "export ZDOTDIR=\"$OLD_ZDOTDIR\"",
// source all the files // source all the files
zshScript += "test -f \"$OLD_ZDOTDIR/.zshenv\" && . \"$OLD_ZDOTDIR/.zshenv\"\n"; "test -f \"$OLD_ZDOTDIR/.zshenv\" && . \"$OLD_ZDOTDIR/.zshenv\"",
zshScript += "test -f \"$OLD_ZDOTDIR/.zprofile\" && . \"$OLD_ZDOTDIR/.zprofile\"\n"; "test -f \"$OLD_ZDOTDIR/.zprofile\" && . \"$OLD_ZDOTDIR/.zprofile\"",
zshScript += "test -f \"$OLD_ZDOTDIR/.zlogin\" && . \"$OLD_ZDOTDIR/.zlogin\"\n"; "test -f \"$OLD_ZDOTDIR/.zlogin\" && . \"$OLD_ZDOTDIR/.zlogin\"",
zshScript += "test -f \"$OLD_ZDOTDIR/.zshrc\" && . \"$OLD_ZDOTDIR/.zshrc\"\n"; "test -f \"$OLD_ZDOTDIR/.zshrc\" && . \"$OLD_ZDOTDIR/.zshrc\"",
// voodoo to replace any previous occurrences of kubectl path in the PATH // voodoo to replace any previous occurrences of kubectl path in the PATH
zshScript += `kubectlpath="${kubectlPath}"\n`; `kubectlpath="${kubectlPath}"`,
zshScript += `helmpath="${helmPath}"\n`; `helmpath="${helmPath}"`,
zshScript += "p=\":$kubectlpath:\"\n"; "p=\":$kubectlpath:\"",
zshScript += "d=\":$PATH:\"\n"; "d=\":$PATH:\"",
zshScript += `d=\${d//$p/:}\n`; `d=\${d//$p/:}`,
zshScript += `d=\${d/#:/}\n`; `d=\${d/#:/}`,
zshScript += `export PATH="$helmpath:$kubectlpath:\${d/%:/}"\n`; `export PATH="$helmpath:$kubectlpath:\${d/%:/}"`,
zshScript += "export KUBECONFIG=\"$tempkubeconfig\"\n"; "export KUBECONFIG=\"$tempkubeconfig\"",
zshScript += `NO_PROXY=",\${NO_PROXY:-localhost},"\n`; `NO_PROXY=",\${NO_PROXY:-localhost},"`,
zshScript += `NO_PROXY="\${NO_PROXY//,localhost,/,}"\n`; `NO_PROXY="\${NO_PROXY//,localhost,/,}"`,
zshScript += `NO_PROXY="\${NO_PROXY//,127.0.0.1,/,}"\n`; `NO_PROXY="\${NO_PROXY//,127.0.0.1,/,}"`,
zshScript += `NO_PROXY="localhost,127.0.0.1\${NO_PROXY%,}"\n`; `NO_PROXY="localhost,127.0.0.1\${NO_PROXY%,}"`,
zshScript += "export NO_PROXY\n"; "export NO_PROXY",
zshScript += "unset tempkubeconfig\n"; "unset tempkubeconfig",
zshScript += "unset OLD_ZDOTDIR\n"; "unset OLD_ZDOTDIR",
await fsPromises.writeFile(zshScriptPath, zshScript.toString(), { mode: 0o644 }); ].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;
} }
} }