From c35bc93fe6e24d459fce1ae93842e4f9686d4b79 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Tue, 18 Jul 2023 12:24:07 +0200 Subject: [PATCH] feat(cli): convert flux (#1204) --- src/cli/install-tool/index.ts | 2 + src/cli/tools/flux/index.ts | 77 ++++++++++++++++++++ src/usr/local/containerbase/tools/v2/flux.sh | 24 ------ 3 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 src/cli/tools/flux/index.ts delete mode 100644 src/usr/local/containerbase/tools/v2/flux.sh diff --git a/src/cli/install-tool/index.ts b/src/cli/install-tool/index.ts index 4b56b2afe..bbff59e80 100644 --- a/src/cli/install-tool/index.ts +++ b/src/cli/install-tool/index.ts @@ -2,6 +2,7 @@ import { Container } from 'inversify'; import { rootContainer } from '../services'; import { InstallDartService } from '../tools/dart'; import { InstallDockerService } from '../tools/docker'; +import { InstallFluxService } from '../tools/flux'; import { logger } from '../utils'; import { InstallLegacyToolService } from './install-legacy-tool.service'; import { INSTALL_TOOL_TOKEN, InstallToolService } from './install-tool.service'; @@ -18,6 +19,7 @@ function prepareContainer(): Container { // tool services container.bind(INSTALL_TOOL_TOKEN).to(InstallDockerService); container.bind(INSTALL_TOOL_TOKEN).to(InstallDartService); + container.bind(INSTALL_TOOL_TOKEN).to(InstallFluxService); logger.trace('preparing container done'); return container; diff --git a/src/cli/tools/flux/index.ts b/src/cli/tools/flux/index.ts new file mode 100644 index 000000000..8da447c9f --- /dev/null +++ b/src/cli/tools/flux/index.ts @@ -0,0 +1,77 @@ +import fs from 'node:fs/promises'; +import { join } from 'node:path'; +import { execa } from 'execa'; +import { inject, injectable } from 'inversify'; +import { InstallToolBaseService } from '../../install-tool/install-tool-base.service'; +import { + CompressionService, + EnvService, + HttpService, + PathService, +} from '../../services'; + +@injectable() +export class InstallFluxService extends InstallToolBaseService { + readonly name = 'flux'; + + private get arch(): string { + return this.envSvc.arch; + } + + constructor( + @inject(EnvService) envSvc: EnvService, + @inject(PathService) pathSvc: PathService, + @inject(HttpService) private http: HttpService, + @inject(CompressionService) private compress: CompressionService + ) { + super(pathSvc, envSvc); + } + + override async install(version: string): Promise { + const baseUrl = `https://github.com/fluxcd/flux2/releases/download/v${version}/`; + const filename = `flux_${version}_linux_${this.arch}.tar.gz`; + + const checksumFile = await this.http.download({ + url: `${baseUrl}flux_${version}_checksums.txt`, + }); + const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8')) + .split('\n') + .find((l) => l.includes(filename)) + ?.split(' ')[0]; + + const file = await this.http.download({ + url: `${baseUrl}${filename}`, + checksumType: 'sha256', + expectedChecksum, + }); + + // TODO: create recursive + if (!(await this.pathSvc.findToolPath(this.name))) { + await this.pathSvc.createToolPath(this.name); + } + + const path = join( + await this.pathSvc.createVersionedToolPath(this.name, version), + 'bin' + ); + await fs.mkdir(path); + await this.compress.extract({ + file, + cwd: path, + }); + } + + override async link(version: string): Promise { + const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin'); + + await this.shellwrapper({ srcDir: src }); + } + + override needsPrepare(): boolean { + return false; + } + + override async test(_version: string): Promise { + await execa('flux', ['--version'], { stdio: 'inherit' }); + } +} diff --git a/src/usr/local/containerbase/tools/v2/flux.sh b/src/usr/local/containerbase/tools/v2/flux.sh deleted file mode 100644 index eba41cda1..000000000 --- a/src/usr/local/containerbase/tools/v2/flux.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -function install_tool () { - local versioned_tool_path - local file - local arch=linux_amd64 - - if [[ "$(uname -p)" = "aarch64" ]]; then - arch=linux_arm64 - fi - - file=$(get_from_url "https://github.com/fluxcd/flux2/releases/download/v${TOOL_VERSION}/flux_${TOOL_VERSION}_${arch}.tar.gz") - - versioned_tool_path=$(create_versioned_tool_path) - tar -C "${versioned_tool_path}" -zxvf "${file}" -} - -function link_tool () { - local versioned_tool_path - versioned_tool_path=$(find_versioned_tool_path) - - shell_wrapper "${TOOL_NAME}" "${versioned_tool_path}" - flux -v -}