Skip to content

Commit

Permalink
chore: starshipjs cleanup (#447)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Anmol1696 committed May 22, 2024
1 parent 70e28d7 commit 4f0e8ad
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 95 deletions.
34 changes: 12 additions & 22 deletions clients/js/packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand Down
73 changes: 44 additions & 29 deletions clients/js/packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 =`
Expand All @@ -77,13 +96,9 @@ Configuration File:
Command-line options will override settings from this file if both are provided.
Command-line Options:
--helmFile <path> Specify the path to the Helm file.
--helmName <name> Specify the Helm release name.
--helmRepo <repo> Specify the Helm repository.
--helmRepoUrl <url> Specify the Helm repository URL.
--helmChart <chart> Specify the Helm chart.
--helmVersion <ver> Specify the version of the Helm chart.
--kindCluster <name> Specify the name of the Kubernetes kind cluster.
--helmFile <path> Specify the path to the Helm file, the actual config file. Required.
--helmName <name> Specify the Helm release name, default: starship.
--helmVersion <ver> Specify the version of the Helm chart, default: v0.2.0.
Examples:
$ starship setup
Expand Down
58 changes: 19 additions & 39 deletions clients/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +29,7 @@ export const defaultStarshipContext: Partial<StarshipContext> = {
helmRepo: 'starship',
helmRepoUrl: 'https://cosmology-tech.github.io/starship/',
helmChart: 'devnet',
helmVersion: 'v0.1.38'
helmVersion: 'v0.2.1-rc0'
};

export interface PodPorts {
Expand Down Expand Up @@ -220,7 +220,13 @@ export class StarshipClient implements StarshipClientI {

public clean(): void {
this.undeploy();
this.cleanKind();
}

public async start(): Promise<void> {
this.setup();
this.deploy();
await this.waitForPods(); // Ensure waitForPods completes before starting port forwarding
this.startPortForward();
}

public setupHelm(): void {
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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<void> {
const podNames = this.getPodNames();

// Check the status of each pod retrieved
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
}
}
}
6 changes: 5 additions & 1 deletion clients/js/packages/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions clients/js/packages/client/src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4f0e8ad

Please sign in to comment.