-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: download helm binary based on os and platform architecture (#128)
Signed-off-by: Lenin Mehedy <[email protected]>
- Loading branch information
1 parent
d890974
commit e64e9a2
Showing
21 changed files
with
493 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the ""License""); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an ""AS IS"" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
import os from 'os' | ||
import { FullstackTestingError, MissingArgumentError } from '../errors.mjs' | ||
import { ShellRunner } from '../shell_runner.mjs' | ||
|
||
export class DependencyManager extends ShellRunner { | ||
constructor (logger, depManagerMap) { | ||
super(logger) | ||
if (!depManagerMap) throw new MissingArgumentError('A map of dependency managers are required') | ||
this.depManagerMap = depManagerMap | ||
} | ||
|
||
/** | ||
* Check if the required dependency is installed or not | ||
* @param dep is the name of the program | ||
* @param shouldInstall Whether or not install the dependency if not installed | ||
* @returns {Promise<boolean>} | ||
*/ | ||
async checkDependency (dep, shouldInstall = true) { | ||
this.logger.debug(`Checking for dependency: ${dep}`) | ||
|
||
let status = false | ||
const manager = this.depManagerMap.get(dep) | ||
if (manager) { | ||
status = await manager.checkVersion(shouldInstall) | ||
} | ||
|
||
if (!status) { | ||
throw new FullstackTestingError(`Dependency '${dep}' is not found`) | ||
} | ||
|
||
this.logger.debug(`Dependency '${dep}' is found`) | ||
return true | ||
} | ||
|
||
taskCheckDependencies (deps = []) { | ||
const subTasks = [] | ||
deps.forEach(dep => { | ||
subTasks.push({ | ||
title: `Check dependency: ${dep} [OS: ${os.platform()}, Release: ${os.release()}, Arch: ${os.arch()}]`, | ||
task: () => this.checkDependency(dep) | ||
}) | ||
}) | ||
|
||
return subTasks | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/core/dependency_managers/helm_dependency_manager.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the ""License""); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an ""AS IS"" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
import fs from 'fs' | ||
import os from 'os' | ||
import path from 'path' | ||
import * as util from 'util' | ||
import { MissingArgumentError } from '../errors.mjs' | ||
import * as helpers from '../helpers.mjs' | ||
import { constants, Templates } from '../index.mjs' | ||
import * as version from '../../../version.mjs' | ||
import { ShellRunner } from '../shell_runner.mjs' | ||
|
||
// constants required by HelmDependencyManager | ||
const HELM_RELEASE_BASE_URL = 'https://get.helm.sh' | ||
const HELM_ARTIFACT_TEMPLATE = 'helm-%s-%s-%s.%s' | ||
const HELM_ARTIFACT_EXT = new Map() | ||
.set(constants.OS_DARWIN, 'tar.gz') | ||
.set(constants.OS_LINUX, 'tar.gz') | ||
.set(constants.OS_WINDOWS, 'zip') | ||
|
||
/** | ||
* Helm dependency manager installs or uninstalls helm client at SOLO_HOME_DIR/bin directory | ||
*/ | ||
export class HelmDependencyManager extends ShellRunner { | ||
constructor ( | ||
downloader, | ||
zippy, | ||
logger, | ||
installationDir = path.join(constants.SOLO_HOME_DIR, 'bin'), | ||
osPlatform = os.platform(), | ||
osArch = os.arch(), | ||
helmVersion = version.HELM_VERSION | ||
) { | ||
super(logger) | ||
|
||
if (!downloader) throw new MissingArgumentError('An instance of core/PackageDownloader is required') | ||
if (!zippy) throw new MissingArgumentError('An instance of core/Zippy is required') | ||
if (!installationDir) throw new MissingArgumentError('installation directory is required') | ||
|
||
this.downloader = downloader | ||
this.zippy = zippy | ||
this.installationDir = installationDir | ||
this.osPlatform = osPlatform | ||
this.osArch = ['x64', 'x86-64'].includes(osArch) ? 'amd64' : osArch | ||
this.helmVersion = helmVersion | ||
this.helmPath = Templates.installationPath(constants.HELM, this.installationDir, this.osPlatform, this.osArch) | ||
|
||
const fileExt = HELM_ARTIFACT_EXT.get(this.osPlatform) | ||
this.artifactName = util.format(HELM_ARTIFACT_TEMPLATE, this.helmVersion, this.osPlatform, this.osArch, fileExt) | ||
this.helmURL = `${HELM_RELEASE_BASE_URL}/${this.artifactName}` | ||
} | ||
|
||
getHelmPath () { | ||
return this.helmPath | ||
} | ||
|
||
isInstalled () { | ||
return fs.existsSync(this.helmPath) | ||
} | ||
|
||
/** | ||
* Uninstall helm from solo bin folder | ||
* @return {Promise<void>} | ||
*/ | ||
async uninstall () { | ||
if (this.isInstalled()) { | ||
fs.rmSync(this.helmPath) | ||
} | ||
} | ||
|
||
async install () { | ||
const tmpDir = helpers.getTmpDir() | ||
const extractedDir = path.join(tmpDir, 'extracted') | ||
const tmpFile = path.join(tmpDir, this.artifactName) | ||
let helmSrc = path.join(extractedDir, `${this.osPlatform}-${this.osArch}`, constants.HELM) | ||
|
||
await this.downloader.fetchFile(this.helmURL, tmpFile) | ||
if (this.osPlatform === constants.OS_WINDOWS) { | ||
await this.zippy.unzip(tmpFile, extractedDir) | ||
// append .exe for windows | ||
helmSrc = path.join(extractedDir, `${this.osPlatform}-${this.osArch}`, `${constants.HELM}.exe`) | ||
} else { | ||
await this.zippy.untar(tmpFile, extractedDir) | ||
} | ||
|
||
if (!fs.existsSync(this.installationDir)) { | ||
fs.mkdirSync(this.installationDir) | ||
} | ||
|
||
// install new helm | ||
await this.uninstall() | ||
this.helmPath = Templates.installationPath(constants.HELM, this.installationDir, this.osPlatform) | ||
fs.cpSync(helmSrc, this.helmPath) | ||
|
||
return this.isInstalled() | ||
} | ||
|
||
async checkVersion (shouldInstall = true) { | ||
if (!this.isInstalled()) { | ||
if (shouldInstall) { | ||
await this.install() | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
const output = await this.run(`${this.helmPath} version --short`) | ||
const parts = output[0].split('+') | ||
this.logger.debug(`Found ${constants.HELM}:${parts[0]}`) | ||
return helpers.compareVersion(version.HELM_VERSION, parts[0]) >= 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the ""License""); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an ""AS IS"" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
import { DependencyManager } from './dependency_manager.mjs' | ||
import { HelmDependencyManager } from './helm_dependency_manager.mjs' | ||
|
||
export { HelmDependencyManager, DependencyManager } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.