1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Read kubectl path and download dir from preferences

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala 2020-09-03 21:59:58 +03:00
parent 935a9e04a5
commit 97aaf79dbc
2 changed files with 61 additions and 50 deletions

View File

@ -97,7 +97,7 @@ export class Kubectl {
this.url = `${this.getDownloadMirror()}/v${this.kubectlVersion}/bin/${platformName}/${arch}/${binaryName}` this.url = `${this.getDownloadMirror()}/v${this.kubectlVersion}/bin/${platformName}/${arch}/${binaryName}`
this.dirname = path.normalize(path.join(Kubectl.kubectlDir, this.kubectlVersion)) this.dirname = path.normalize(path.join(this.getDownloadDir(), this.kubectlVersion))
this.path = path.join(this.dirname, binaryName) this.path = path.join(this.dirname, binaryName)
} }
@ -105,7 +105,19 @@ export class Kubectl {
return Kubectl.bundledKubectlPath return Kubectl.bundledKubectlPath
} }
public getPathFromPreferences() {
return userStore.preferences.kubectlBinariesPath || this.getBundledPath()
}
protected getDownloadDir() {
return userStore.preferences.downloadBinariesPath || Kubectl.kubectlDir
}
public async getPath(bundled = false): Promise<string> { public async getPath(bundled = false): Promise<string> {
if (!userStore.preferences.downloadKubectlBinaries) {
return this.getPathFromPreferences()
}
// return binary name if bundled path is not functional // return binary name if bundled path is not functional
if (!await this.checkBinary(this.getBundledPath(), false)) { if (!await this.checkBinary(this.getBundledPath(), false)) {
Kubectl.invalidBundle = true Kubectl.invalidBundle = true
@ -128,6 +140,7 @@ export class Kubectl {
public async binDir() { public async binDir() {
try { try {
await this.ensureKubectl() await this.ensureKubectl()
await this.writeInitScripts()
return this.dirname return this.dirname
} catch (err) { } catch (err) {
logger.error(err) logger.error(err)
@ -180,6 +193,9 @@ export class Kubectl {
} }
public async ensureKubectl(): Promise<boolean> { public async ensureKubectl(): Promise<boolean> {
if (!userStore.preferences.downloadKubectlBinaries) {
return true
}
if (Kubectl.invalidBundle) { if (Kubectl.invalidBundle) {
logger.error(`Detected invalid bundle binary, returning ...`) logger.error(`Detected invalid bundle binary, returning ...`)
return false return false
@ -203,10 +219,6 @@ export class Kubectl {
release() release()
return false return false
} }
await this.writeInitScripts().catch((error) => {
logger.error("Failed to write init scripts");
logger.error(error)
})
logger.debug(`Releasing lock for ${this.kubectlVersion}`) logger.debug(`Releasing lock for ${this.kubectlVersion}`)
release() release()
return true return true
@ -266,54 +278,51 @@ export class Kubectl {
} }
protected async writeInitScripts() { protected async writeInitScripts() {
const kubectlPath = userStore.preferences.downloadKubectlBinaries ? this.dirname : path.dirname(this.getPathFromPreferences())
const helmPath = helmCli.getBinaryDir() const helmPath = helmCli.getBinaryDir()
const fsPromises = fs.promises; const fsPromises = fs.promises;
const bashScriptPath = path.join(this.dirname, '.bash_set_path') const bashScriptPath = path.join(this.dirname, '.bash_set_path')
const bashScriptIsLatest = await this.scriptIsLatest(bashScriptPath)
if (!bashScriptIsLatest) { let bashScript = "" + initScriptVersionString
let bashScript = "" + initScriptVersionString bashScript += "tempkubeconfig=\"$KUBECONFIG\"\n"
bashScript += "tempkubeconfig=\"$KUBECONFIG\"\n" bashScript += "test -f \"/etc/profile\" && . \"/etc/profile\"\n"
bashScript += "test -f \"/etc/profile\" && . \"/etc/profile\"\n" bashScript += "if test -f \"$HOME/.bash_profile\"; then\n"
bashScript += "if test -f \"$HOME/.bash_profile\"; then\n" bashScript += " . \"$HOME/.bash_profile\"\n"
bashScript += " . \"$HOME/.bash_profile\"\n" bashScript += "elif test -f \"$HOME/.bash_login\"; then\n"
bashScript += "elif test -f \"$HOME/.bash_login\"; then\n" bashScript += " . \"$HOME/.bash_login\"\n"
bashScript += " . \"$HOME/.bash_login\"\n" bashScript += "elif test -f \"$HOME/.profile\"; then\n"
bashScript += "elif test -f \"$HOME/.profile\"; then\n" bashScript += " . \"$HOME/.profile\"\n"
bashScript += " . \"$HOME/.profile\"\n" bashScript += "fi\n"
bashScript += "fi\n" bashScript += `export PATH="${helmPath}:${kubectlPath}:$PATH"\n`
bashScript += `export PATH="${this.dirname}:${helmPath}:$PATH"\n` bashScript += "export KUBECONFIG=\"$tempkubeconfig\"\n"
bashScript += "export KUBECONFIG=\"$tempkubeconfig\"\n" bashScript += "unset tempkubeconfig\n"
bashScript += "unset tempkubeconfig\n" await fsPromises.writeFile(bashScriptPath, bashScript.toString(), { mode: 0o644 })
await fsPromises.writeFile(bashScriptPath, bashScript.toString(), { mode: 0o644 })
}
const zshScriptPath = path.join(this.dirname, '.zlogin') const zshScriptPath = path.join(this.dirname, '.zlogin')
const zshScriptIsLatest = await this.scriptIsLatest(zshScriptPath)
if (!zshScriptIsLatest) {
let zshScript = "" + initScriptVersionString
zshScript += "tempkubeconfig=\"$KUBECONFIG\"\n" let zshScript = "" + initScriptVersionString
// restore previous 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 zshScript += "tempkubeconfig=\"$KUBECONFIG\"\n"
zshScript += `kubectlpath=\"${this.dirname}"\n` // restore previous ZDOTDIR
zshScript += `helmpath=\"${helmPath}"\n` zshScript += "export ZDOTDIR=\"$OLD_ZDOTDIR\"\n"
zshScript += "p=\":$kubectlpath:\"\n" // source all the files
zshScript += "d=\":$PATH:\"\n" zshScript += "test -f \"$OLD_ZDOTDIR/.zshenv\" && . \"$OLD_ZDOTDIR/.zshenv\"\n"
zshScript += "d=${d//$p/:}\n" zshScript += "test -f \"$OLD_ZDOTDIR/.zprofile\" && . \"$OLD_ZDOTDIR/.zprofile\"\n"
zshScript += "d=${d/#:/}\n" zshScript += "test -f \"$OLD_ZDOTDIR/.zlogin\" && . \"$OLD_ZDOTDIR/.zlogin\"\n"
zshScript += "export PATH=\"$kubectlpath:$helmpath:${d/%:/}\"\n" zshScript += "test -f \"$OLD_ZDOTDIR/.zshrc\" && . \"$OLD_ZDOTDIR/.zshrc\"\n"
zshScript += "export KUBECONFIG=\"$tempkubeconfig\"\n"
zshScript += "unset tempkubeconfig\n" // voodoo to replace any previous occurrences of kubectl path in the PATH
zshScript += "unset OLD_ZDOTDIR\n" zshScript += `kubectlpath=\"${kubectlPath}"\n`
await fsPromises.writeFile(zshScriptPath, zshScript.toString(), { mode: 0o644 }) zshScript += `helmpath=\"${helmPath}"\n`
} zshScript += "p=\":$kubectlpath:\"\n"
zshScript += "d=\":$PATH:\"\n"
zshScript += "d=${d//$p/:}\n"
zshScript += "d=${d/#:/}\n"
zshScript += "export PATH=\"$helmpath:$kubectlpath:${d/%:/}\"\n"
zshScript += "export KUBECONFIG=\"$tempkubeconfig\"\n"
zshScript += "unset tempkubeconfig\n"
zshScript += "unset OLD_ZDOTDIR\n"
await fsPromises.writeFile(zshScriptPath, zshScript.toString(), { mode: 0o644 })
} }
protected getDownloadMirror() { protected getDownloadMirror() {

View File

@ -4,12 +4,13 @@ import { EventEmitter } from "events";
import path from "path" import path from "path"
import shellEnv from "shell-env" import shellEnv from "shell-env"
import { app } from "electron" import { app } from "electron"
import { Kubectl } from "./kubectl" import { Kubectl, bundledKubectl } from "./kubectl"
import { Cluster } from "./cluster" import { Cluster } from "./cluster"
import { ClusterPreferences } from "../common/cluster-store"; import { ClusterPreferences } from "../common/cluster-store";
import { helmCli } from "./helm/helm-cli" import { helmCli } from "./helm/helm-cli"
import { isWindows } from "../common/vars"; import { isWindows } from "../common/vars";
import { tracker } from "../common/tracker"; import { tracker } from "../common/tracker";
import { userStore } from "../common/user-store";
export class ShellSession extends EventEmitter { export class ShellSession extends EventEmitter {
static shellEnvs: Map<string, any> = new Map() static shellEnvs: Map<string, any> = new Map()
@ -35,7 +36,8 @@ export class ShellSession extends EventEmitter {
} }
public async open() { public async open() {
this.kubectlBinDir = await this.kubectl.binDir() const pathFromPreferences = userStore.preferences.kubectlBinariesPath || Kubectl.bundledKubectlPath
this.kubectlBinDir = userStore.preferences.downloadKubectlBinaries ? await this.kubectl.binDir() : path.dirname(pathFromPreferences)
this.helmBinDir = helmCli.getBinaryDir() this.helmBinDir = helmCli.getBinaryDir()
const env = await this.getCachedShellEnv() const env = await this.getCachedShellEnv()
const shell = env.PTYSHELL const shell = env.PTYSHELL
@ -67,11 +69,11 @@ export class ShellSession extends EventEmitter {
protected async getShellArgs(shell: string): Promise<Array<string>> { protected async getShellArgs(shell: string): Promise<Array<string>> {
switch(path.basename(shell)) { switch(path.basename(shell)) {
case "powershell.exe": case "powershell.exe":
return ["-NoExit", "-command", `& {Set-Location $Env:USERPROFILE; $Env:PATH="${this.kubectlBinDir};${this.helmBinDir};$Env:PATH"}`] return ["-NoExit", "-command", `& {Set-Location $Env:USERPROFILE; $Env:PATH="${this.helmBinDir};${this.kubectlBinDir};$Env:PATH"}`]
case "bash": case "bash":
return ["--init-file", path.join(this.kubectlBinDir, '.bash_set_path')] return ["--init-file", path.join(this.kubectlBinDir, '.bash_set_path')]
case "fish": case "fish":
return ["--login", "--init-command", `export PATH="${this.kubectlBinDir}:${this.helmBinDir}:$PATH"; export KUBECONFIG="${this.kubeconfigPath}"`] return ["--login", "--init-command", `export PATH="${this.helmBinDir}:${this.kubectlBinDir}:$PATH"; export KUBECONFIG="${this.kubeconfigPath}"`]
case "zsh": case "zsh":
return ["--login"] return ["--login"]
default: default: