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

Bundle helm3 binary (#220)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2020-04-06 18:18:18 +03:00 committed by GitHub
parent 2593f70184
commit f23da9ccad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 33 deletions

View File

@ -27,6 +27,7 @@ module.exports = {
}, },
{ {
files: [ files: [
"build/*.ts",
"src/**/*.ts", "src/**/*.ts",
"spec/**/*.ts" "spec/**/*.ts"
], ],

3
build/download_helm.ts Normal file
View File

@ -0,0 +1,3 @@
import { helmCli } from "../src/main/helm-cli"
helmCli.ensureBinary()

View File

@ -11,7 +11,8 @@
"version": "3.2.0-rc.1", "version": "3.2.0-rc.1",
"main": "main.ts", "main": "main.ts",
"config": { "config": {
"bundledKubectlVersion": "1.17.3" "bundledKubectlVersion": "1.17.3",
"bundledHelmVersion": "3.1.2"
}, },
"engines": { "engines": {
"node": ">=12.0 <13.0" "node": ">=12.0 <13.0"
@ -41,6 +42,10 @@
{ {
"from": "binaries/client/linux/x64/kubectl", "from": "binaries/client/linux/x64/kubectl",
"to": "./x64/kubectl" "to": "./x64/kubectl"
},
{
"from": "binaries/client/helm3/helm3",
"to": "./helm3/helm3"
} }
] ]
}, },
@ -53,6 +58,10 @@
{ {
"from": "binaries/client/darwin/x64/kubectl", "from": "binaries/client/darwin/x64/kubectl",
"to": "./x64/kubectl" "to": "./x64/kubectl"
},
{
"from": "binaries/client/helm3/helm3",
"to": "./helm3/helm3"
} }
] ]
}, },
@ -68,6 +77,10 @@
{ {
"from": "binaries/client/windows/ia32/kubectl.exe", "from": "binaries/client/windows/ia32/kubectl.exe",
"to": "./ia32/kubectl.exe" "to": "./ia32/kubectl.exe"
},
{
"from": "binaries/client/helm3/helm3.exe",
"to": "./helm3/helm3.exe"
} }
] ]
}, },
@ -94,7 +107,7 @@
"dev": "concurrently -n app,dash \"yarn dev-electron\" \"yarn dev-dashboard\"", "dev": "concurrently -n app,dash \"yarn dev-electron\" \"yarn dev-dashboard\"",
"dev-dashboard": "cd dashboard && yarn dev", "dev-dashboard": "cd dashboard && yarn dev",
"dev-electron": "electron-webpack dev", "dev-electron": "electron-webpack dev",
"compile": "yarn download:kubectl && electron-webpack", "compile": "yarn download:bins && electron-webpack",
"build:linux": "yarn compile && electron-builder --linux --dir", "build:linux": "yarn compile && electron-builder --linux --dir",
"build:mac": "yarn compile && electron-builder --mac --dir", "build:mac": "yarn compile && electron-builder --mac --dir",
"build:win": "yarn compile && electron-builder --win --dir", "build:win": "yarn compile && electron-builder --win --dir",
@ -105,7 +118,9 @@
"lint-dashboard": "eslint $@ --ext js,ts,tsx --max-warnings=0 dashboard/client dashboard/server", "lint-dashboard": "eslint $@ --ext js,ts,tsx --max-warnings=0 dashboard/client dashboard/server",
"postinstall": "patch-package", "postinstall": "patch-package",
"test": "node_modules/.bin/jest", "test": "node_modules/.bin/jest",
"download:kubectl": "yarn run ts-node build/download_kubectl.ts" "download:bins": "concurrently \"yarn download:kubectl\" \"yarn download:helm\"",
"download:kubectl": "yarn run ts-node build/download_kubectl.ts",
"download:helm": "yarn run ts-node build/download_helm.ts"
}, },
"dependencies": { "dependencies": {
"@hapi/call": "^6.0.1", "@hapi/call": "^6.0.1",

View File

@ -1,40 +1,28 @@
import * as path from "path" import * as path from "path"
import { LensBinary, LensBinaryOpts } from "./lens-binary" import { LensBinary, LensBinaryOpts } from "./lens-binary"
import { userStore } from "../common/user-store"
const helmVersion = "3.1.2"
const packageMirrors: Map<string, string> = new Map([
["default", "https://get.helm.sh"],
["china", "https://mirror.azure.cn/kubernetes/helm"]
])
export class HelmCli extends LensBinary { export class HelmCli extends LensBinary {
public constructor(version: string) { public constructor(baseDir: string, version: string) {
const opts: LensBinaryOpts = { const opts: LensBinaryOpts = {
version, version,
baseDir: baseDir,
originalBinaryName: "helm", originalBinaryName: "helm",
newBinaryName: "helm3" newBinaryName: "helm3"
} }
super(opts) super(opts)
} }
protected getTarName(): string|null { protected getTarName(): string|null {
return `${this.binaryName}-v${this.binaryVersion}-${this.platformName}-${this.arch}.tar.gz` return `${this.binaryName}-v${this.binaryVersion}-${this.platformName}-${this.arch}.tar.gz`
} }
protected getUrl() { protected getUrl() {
return `${this.getDownloadMirror()}/helm-v${this.binaryVersion}-${this.platformName}-${this.arch}.tar.gz` return `https://get.helm.sh/helm-v${this.binaryVersion}-${this.platformName}-${this.arch}.tar.gz`
}
protected getDownloadMirror() {
const mirror = packageMirrors.get(userStore.getPreferences().downloadMirror)
if (mirror) { return mirror }
return packageMirrors.get("default")
} }
protected getBinaryPath() { protected getBinaryPath() {
return path.join(this.dirname, this.platformName+"-"+this.arch, this.binaryName) return path.join(this.dirname, this.binaryName)
} }
protected getOriginalBinaryPath() { protected getOriginalBinaryPath() {
@ -42,4 +30,15 @@ export class HelmCli extends LensBinary {
} }
} }
export const helmCli = new HelmCli(helmVersion) const helmVersion = require("../../package.json").config.bundledHelmVersion
const isDevelopment = process.env.NODE_ENV !== "production"
let baseDir: string = null
if(isDevelopment) {
baseDir = path.join(process.cwd(), "binaries", "client")
} else {
baseDir = path.join(process.resourcesPath)
}
export const helmCli = new HelmCli(baseDir, helmVersion)

View File

@ -100,9 +100,6 @@ async function main() {
}) })
}, },
}, promiseIpc) }, promiseIpc)
// download helm cli
helmCli.ensureBinary()
} }
app.on("ready", main) app.on("ready", main)

View File

@ -1,16 +1,16 @@
import { app, remote } from "electron"
import * as path from "path" import * as path from "path"
import * as fs from "fs" import * as fs from "fs"
import * as request from "request" import * as request from "request"
import logger from "./logger" import logger from "./logger"
import { ensureDir, pathExists } from "fs-extra" import { ensureDir, pathExists } from "fs-extra"
import * as tar from "tar" import * as tar from "tar"
import { globalRequestOpts} from "../common/request"
export type LensBinaryOpts = { export type LensBinaryOpts = {
version: string; version: string;
baseDir: string;
originalBinaryName: string; originalBinaryName: string;
newBinaryName?: string; newBinaryName?: string;
requestOpts?: request.Options;
} }
export class LensBinary { export class LensBinary {
@ -24,12 +24,14 @@ export class LensBinary {
protected platformName: string protected platformName: string
protected arch: string protected arch: string
protected originalBinaryName: string protected originalBinaryName: string
protected requestOpts: request.Options
constructor(opts: LensBinaryOpts) { constructor(opts: LensBinaryOpts) {
const baseDir = opts.baseDir
this.originalBinaryName = opts.originalBinaryName this.originalBinaryName = opts.originalBinaryName
this.binaryName = opts.newBinaryName || opts.originalBinaryName this.binaryName = opts.newBinaryName || opts.originalBinaryName
this.binaryVersion = opts.version this.binaryVersion = opts.version
this.requestOpts = opts.requestOpts
let arch = null let arch = null
@ -41,20 +43,24 @@ export class LensBinary {
arch = process.arch arch = process.arch
} }
this.arch = arch this.arch = arch
const binaryDir = path.join((app || remote.app).getPath("userData"), "binaries", this.binaryName)
this.platformName = process.platform === "win32" ? "windows" : process.platform this.platformName = process.platform === "win32" ? "windows" : process.platform
if (process.platform === "win32") { if (process.platform === "win32") {
this.binaryName = this.binaryName+".exe" this.binaryName = this.binaryName+".exe"
this.originalBinaryName = this.originalBinaryName+".exe" this.originalBinaryName = this.originalBinaryName+".exe"
} }
this.dirname = path.normalize(path.join(binaryDir, this.binaryVersion))
this.dirname = path.normalize(path.join(baseDir, this.binaryName))
const tarName = this.getTarName() const tarName = this.getTarName()
if (tarName) { if (tarName) {
this.tarPath = path.join(this.dirname, tarName) this.tarPath = path.join(this.dirname, tarName)
} }
} }
protected binaryDir() {
throw new Error("binaryDir not implemented")
}
public async binaryPath() { public async binaryPath() {
await this.ensureBinary() await this.ensureBinary()
return this.getBinaryPath() return this.getBinaryPath()
@ -101,6 +107,7 @@ export class LensBinary {
await this.downloadBinary().catch((error) => { logger.error(error) }); await this.downloadBinary().catch((error) => { logger.error(error) });
if (this.tarPath) await this.untarBinary() if (this.tarPath) await this.untarBinary()
if(this.originalBinaryName != this.binaryName ) await this.renameBinary() if(this.originalBinaryName != this.binaryName ) await this.renameBinary()
logger.info(`${this.originalBinaryName} has been downloaded to ${this.getBinaryPath()}`)
} }
} }
@ -139,13 +146,14 @@ export class LensBinary {
logger.info(`Downloading ${this.originalBinaryName} ${this.binaryVersion} from ${url} to ${binaryPath}`) logger.info(`Downloading ${this.originalBinaryName} ${this.binaryVersion} from ${url} to ${binaryPath}`)
const requestOpts: request.UriOptions & request.CoreOptions = { const requestOpts: request.UriOptions & request.CoreOptions = {
uri: url, uri: url,
gzip: true gzip: true,
...this.requestOpts
} }
const stream = request(globalRequestOpts(requestOpts)) const stream = request(requestOpts)
stream.on("complete", () => { stream.on("complete", () => {
logger.info(`${this.originalBinaryName} binary download finished`) logger.info(`Download of ${this.originalBinaryName} finished`)
file.end(() => {}) file.end(() => {})
}) })

View File

@ -30,7 +30,7 @@
<h2>Download Mirror</h2> <h2>Download Mirror</h2>
<b-form-group <b-form-group
label="Download mirror for kubectl/helm:" label="Download mirror for kubectl:"
label-for="input-download-mirror" label-for="input-download-mirror"
> >
<b-form-select <b-form-select