From afd7a9703068af5d340d9025465a8d7db98a4342 Mon Sep 17 00:00:00 2001 From: Jim Ehrismann Date: Fri, 19 Jun 2020 14:13:35 -0400 Subject: [PATCH] first crack at updating Kubectl.ts Signed-off-by: Jim Ehrismann --- build/download_kubectl.ts | 4 ++-- src/main/kubectl.ts | 31 ++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/build/download_kubectl.ts b/build/download_kubectl.ts index 0cd47c51c5..682b93874e 100644 --- a/build/download_kubectl.ts +++ b/build/download_kubectl.ts @@ -43,7 +43,7 @@ class KubectlDownloader { if (exists) { let oldEtag = "" try { - oldEtag = fs.readFileSync(this.pathEtag, "utf8") + oldEtag = await fs.promises.readFile(this.pathEtag, "utf8") } catch(err) { // treat any error here as a bad check @@ -74,7 +74,7 @@ class KubectlDownloader { try { const etag = await this.urlEtag() - fs.writeFileSync(this.pathEtag, etag, 'utf8') + await fs.promises.writeFile(this.pathEtag, etag, 'utf8') } catch(err) { console.log(`error (${err.message}) saving kubectl etag, this may incur further downloads`) diff --git a/src/main/kubectl.ts b/src/main/kubectl.ts index f72bcb5336..ff7a79291e 100644 --- a/src/main/kubectl.ts +++ b/src/main/kubectl.ts @@ -5,7 +5,6 @@ import * as request from "request" import * as requestPromise from "request-promise-native" import logger from "./logger" import { ensureDir, pathExists } from "fs-extra" -import * as md5File from "md5-file" import { globalRequestOpts } from "../common/request" import * as lockFile from "proper-lockfile" import { helmCli } from "./helm-cli" @@ -51,6 +50,8 @@ export class Kubectl { protected directory: string protected url: string protected path: string + protected pathEtag: string; + protected etag: string; protected dirname: string public static readonly kubectlDir = path.join((app || remote.app).getPath("userData"), "binaries", "kubectl") @@ -94,6 +95,8 @@ export class Kubectl { this.dirname = path.normalize(path.join(Kubectl.kubectlDir, this.kubectlVersion)) this.path = path.join(this.dirname, binaryName) + this.pathEtag = this.path + ".etag"; + this.etag = ""; } public async kubectlPath(): Promise { @@ -138,19 +141,29 @@ export class Kubectl { if (!checkTag) { return true } - const hash = md5File.sync(this.path) + let oldEtag = "" + try { + oldEtag = await fs.promises.readFile(this.pathEtag, "utf8") + } + catch(err) { + // treat any error here as a bad check + logger.debug(`Error reading saved etag for local kubectl (${err.message})`) + return false + } + const etag = await this.urlEtag() if (etag === "") { logger.debug("Cannot resolve kubectl remote etag") return true } - if (hash == etag) { - logger.debug("Kubectl md5sum matches the remote etag") + if(oldEtag == etag) { + logger.debug("Local kubectl etag matches the remote etag") return true } - logger.error("Kubectl md5sum " + hash + " does not match the remote etag " + etag + ", unlinking and downloading again") + logger.error(`Local kubectl etag ${oldEtag} does not match the remote etag ${etag}, unlinking`) await fs.promises.unlink(this.path) + await fs.promises.unlink(this.pathEtag) } return false @@ -163,6 +176,7 @@ export class Kubectl { if (!exist) { await fs.promises.copyFile(Kubectl.bundledKubectlPath, this.path) await fs.promises.chmod(this.path, 0o755) + await fs.promises.copyFile(Kubectl.bundledKubectlPath + ".etag", this.pathEtag) } return true } catch(err) { @@ -199,6 +213,13 @@ export class Kubectl { logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`) return new Promise((resolve, reject) => { + try { + const etag = this.urlEtag() + fs.writeFileSync(this.pathEtag, etag) + } + catch(err) { + logger.debug(`error (${err.message}) saving kubectl etag, this may incur further downloads`) + } const stream = request({ gzip: true, ...this.getRequestOpts()