From 4f0e8ad892b185c02a905a1667500f21af09c670 Mon Sep 17 00:00:00 2001 From: Anmol Date: Wed, 22 May 2024 12:03:02 +0400 Subject: [PATCH] chore: starshipjs cleanup (#447) * update helm default version * remove kind commands from starship cli * remove kind from installer * some more fixes * update client and config * add async commands to wait-for-pods, add start command * remove some more unused commands * upgrade all packages to version 2.0 * undo package upgrade versions --- clients/js/packages/cli/src/index.ts | 34 ++++------ clients/js/packages/cli/src/utils.ts | 73 +++++++++++++-------- clients/js/packages/client/src/client.ts | 58 ++++++---------- clients/js/packages/client/src/config.ts | 6 +- clients/js/packages/client/src/installer.ts | 4 -- 5 files changed, 80 insertions(+), 95 deletions(-) diff --git a/clients/js/packages/cli/src/index.ts b/clients/js/packages/cli/src/index.ts index d1fa76ceb..e9403a02d 100755 --- a/clients/js/packages/cli/src/index.ts +++ b/clients/js/packages/cli/src/index.ts @@ -3,7 +3,7 @@ import { StarshipClient } from '@starship-ci/client'; // Adjust the import path import { Inquirerer, type Question } from 'inquirerer'; import minimist from 'minimist'; -import { displayUsage, displayVersion, loadConfig, usageText } from './utils'; +import { displayUsage, displayVersion, loadConfig, usageText, params } from './utils'; const argv = minimist(process.argv.slice(2), { alias: { @@ -24,14 +24,7 @@ const prompter = new Inquirerer({ noTty: !argv.tty }); -const questions: Question[] = [ - 'helmName', - 'helmFile', - 'helmRepo', - 'helmRepoUrl', - 'helmChart', - 'helmVersion' -].map(name => ({ name, type: 'text' })); +const questions: Question[] = params.map(name => ({ name, type: 'text' })); // Main function to run the application async function main() { @@ -55,6 +48,12 @@ async function main() { // Execute command based on input switch (command) { + case 'start': + client.start().catch((err: any) => { + console.error('An error occurred during start:', err); + process.exit(1); + }); + break; case 'deploy': client.deploy(); break; @@ -68,7 +67,10 @@ async function main() { client.getPods(); break; case 'wait-for-pods': - client.waitForPods(); + client.waitForPods().catch((err: any) => { + console.error('An error occurred during wait-for-pods:', err); + process.exit(1); + }); break; case 'port-pids': client.printForwardPids(); @@ -79,27 +81,15 @@ async function main() { case 'teardown': client.teardown(); break; - case 'upgrade': - client.upgrade(); - break; case 'undeploy': client.undeploy(); break; - case 'clean-kind': - client.cleanKind(); - break; case 'delete-helm': client.deleteHelm(); break; case 'remove-helm': client.removeHelm(); break; - case 'setup-kind': - client.setupKind(); - break; - case 'clean': - client.clean(); - break; default: console.log(`Unknown command: ${command}`); displayUsage(); diff --git a/clients/js/packages/cli/src/utils.ts b/clients/js/packages/cli/src/utils.ts index 760474054..ffc5630b9 100644 --- a/clients/js/packages/cli/src/utils.ts +++ b/clients/js/packages/cli/src/utils.ts @@ -1,11 +1,11 @@ -import { defaultStarshipContext, StarshipContext } from '@starship-ci/client'; // Adjust the import path as necessary -import { StarshipConfig } from '@starship-ci/client'; +import {defaultStarshipContext, StarshipConfig, StarshipContext} from '@starship-ci/client'; // Adjust the import path as necessary +import { type Question } from 'inquirerer'; import chalk from 'chalk'; -import { readFileSync } from 'fs'; +import {readFileSync} from 'fs'; import * as yaml from 'js-yaml'; -import { dirname, resolve } from 'path'; +import {dirname, resolve} from 'path'; -import { readAndParsePackageJson } from './package'; +import {readAndParsePackageJson} from './package'; import deepmerge from 'deepmerge'; // Function to display the version information @@ -31,27 +31,46 @@ export interface Config { starship: StarshipConfig } +export const params: string[] = [ + 'helmName', + 'helmFile', + 'helmRepo', + 'helmRepoUrl', + 'helmChart', + 'helmVersion', +] + export const loadConfig = (argv: any): Config => { + console.log("argv: ", argv); + console.log("argv.config: ", argv.config); + let context: StarshipContext = { ...defaultStarshipContext } as StarshipContext; + let starship: StarshipConfig = {} as StarshipConfig; + if (argv.config) { - const context = deepmerge(defaultStarshipContext, loadYaml(argv.config)) as StarshipContext - if (context.helmFile) { - const dir = dirname(argv.config); - const configPath = resolve(resolvePath(dir), context.helmFile); - context.helmFile = configPath; - const starship = loadYaml(configPath) as StarshipConfig - - return { - context, - starship - } - } + context = deepmerge(defaultStarshipContext, loadYaml(argv.config)) as StarshipContext } - return { - // @ts-ignore - context: {}, - // @ts-ignore - starship: {} + + console.log("context", context); + + // Override context with command-line arguments dynamically based on StarshipContext keys + params.forEach(key => { + if (argv[key] !== undefined) { + console.log("key: ", key, " argv[key]: ", argv[key]); + // @ts-ignore + context[key] = argv[key]; + } + }); + + if (context.helmFile) { + const dir = dirname(argv.config || process.cwd()); + context.helmFile = resolve(resolvePath(dir), context.helmFile); } + + starship = loadYaml(context.helmFile) as StarshipConfig + + console.log("starship: ", starship); + + return {context, starship}; } export const usageText =` @@ -77,13 +96,9 @@ Configuration File: Command-line options will override settings from this file if both are provided. Command-line Options: - --helmFile Specify the path to the Helm file. - --helmName Specify the Helm release name. - --helmRepo Specify the Helm repository. - --helmRepoUrl Specify the Helm repository URL. - --helmChart Specify the Helm chart. - --helmVersion Specify the version of the Helm chart. - --kindCluster Specify the name of the Kubernetes kind cluster. + --helmFile Specify the path to the Helm file, the actual config file. Required. + --helmName Specify the Helm release name, default: starship. + --helmVersion Specify the version of the Helm chart, default: v0.2.0. Examples: $ starship setup diff --git a/clients/js/packages/client/src/client.ts b/clients/js/packages/client/src/client.ts index 27f25c992..57f470461 100644 --- a/clients/js/packages/client/src/client.ts +++ b/clients/js/packages/client/src/client.ts @@ -13,12 +13,12 @@ import { dependencies as defaultDependencies, Dependency } from "./deps"; import { readAndParsePackageJson } from './package'; export interface StarshipContext { - helmName: string; + helmName?: string; helmFile: string; - helmRepo: string; - helmRepoUrl: string; - helmChart: string; - helmVersion: string; + helmRepo?: string; + helmRepoUrl?: string; + helmChart?: string; + helmVersion?: string; kindCluster?: string; verbose?: boolean; curdir?: string; @@ -29,7 +29,7 @@ export const defaultStarshipContext: Partial = { helmRepo: 'starship', helmRepoUrl: 'https://cosmology-tech.github.io/starship/', helmChart: 'devnet', - helmVersion: 'v0.1.38' + helmVersion: 'v0.2.1-rc0' }; export interface PodPorts { @@ -220,7 +220,13 @@ export class StarshipClient implements StarshipClientI { public clean(): void { this.undeploy(); - this.cleanKind(); + } + + public async start(): Promise { + this.setup(); + this.deploy(); + await this.waitForPods(); // Ensure waitForPods completes before starting port forwarding + this.startPortForward(); } public setupHelm(): void { @@ -274,21 +280,6 @@ export class StarshipClient implements StarshipClientI { this.log("Run \"starship get-pods\" to check the status of the cluster"); } - public upgrade(): void { - this.ensureFileExists(this.ctx.helmFile); - this.exec([ - 'helm', - 'upgrade', - '--debug', - '-f', - this.ctx.helmFile, - this.ctx.helmName, - `${this.ctx.helmRepo}/${this.ctx.helmChart}`, - '--version', - this.ctx.helmVersion - ]); - } - public debug(): void { this.ensureFileExists(this.ctx.helmFile); this.exec([ @@ -367,8 +358,8 @@ export class StarshipClient implements StarshipClientI { // setTimeout(() => this.checkPodStatus(podName), 2500); // check every 2.5 seconds } } - - public waitForPods(): void { + + public async waitForPods(): Promise { const podNames = this.getPodNames(); // Check the status of each pod retrieved @@ -379,7 +370,8 @@ export class StarshipClient implements StarshipClientI { this.displayPodStatuses(); if (!this.areAllPodsRunning()) { - setTimeout(() => this.waitForPods(), 2500) + await new Promise(resolve => setTimeout(resolve, 2500)); + await this.waitForPods(); // Recursive call } } @@ -432,8 +424,8 @@ export class StarshipClient implements StarshipClientI { this.log("Starting new port forwarding..."); this.config.chains.forEach(chain => { - // TODO Talk to Anmol about chain.name and chain.type, seems to be opposite of intuition using chainReg as concept - const chainPodPorts = this.podPorts.chains[chain.type] || this.podPorts.chains.defaultPorts; + // TODO Talk to Anmol about chain.name and chain.name, seems to be opposite of intuition using chainReg as concept + const chainPodPorts = this.podPorts.chains[chain.name] || this.podPorts.chains.defaultPorts; if (chain.ports.rpc) this.forwardPort(chain, chain.ports.rpc, chainPodPorts.rpc); if (chain.ports.rest) this.forwardPort(chain, chain.ports.rest, chainPodPorts.rest); @@ -479,16 +471,4 @@ export class StarshipClient implements StarshipClientI { console.log(pid); }); } - - public setupKind(): void { - if (this.ctx.kindCluster) { - this.exec(['kind', 'create', 'cluster', '--name', this.ctx.kindCluster]); - } - } - - public cleanKind(): void { - if (this.ctx.kindCluster) { - this.exec(['kind', 'delete', 'cluster', '--name', this.ctx.kindCluster]); - } - } } diff --git a/clients/js/packages/client/src/config.ts b/clients/js/packages/client/src/config.ts index 33cdc677e..a8e18bf02 100644 --- a/clients/js/packages/client/src/config.ts +++ b/clients/js/packages/client/src/config.ts @@ -5,23 +5,27 @@ export interface Ports { exposer?: number; grpc?: number; } + export interface Resources { cpu: number; memory: string; } + export interface FaucetConfig { enabled: boolean; type: string; } + export interface Chain { + id: string; name: string; - type: string; image: string; numValidators: number; ports: Ports; faucet?: FaucetConfig; resources?: Resources; } + export interface Relayer { name: string; type: string; diff --git a/clients/js/packages/client/src/installer.ts b/clients/js/packages/client/src/installer.ts index 93251d9d8..11c1bcfdf 100644 --- a/clients/js/packages/client/src/installer.ts +++ b/clients/js/packages/client/src/installer.ts @@ -21,10 +21,6 @@ export class StarshipInstaller { mac: 'brew install helm', linux: 'curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash' }, - kind: { - mac: 'brew install kind', - linux: `curl -Lks https://kind.sigs.k8s.io/dl/v0.18.0/kind-linux-amd64 > ~/.local/bin/kind && chmod +x ~/.local/bin/kind` - } }; async checkAndInstallBinary(binaryName: string) {