From 6145c8a09fa8754276ad681da44ab7a6caf6f4cb Mon Sep 17 00:00:00 2001 From: antho1404 Date: Sun, 5 Apr 2020 15:23:44 +0700 Subject: [PATCH] improve configuration management --- packages/cli/src/commands/dev.ts | 12 ++--- packages/cli/src/commands/process/dev.ts | 10 ++-- packages/cli/src/commands/service/dev.ts | 8 +-- packages/cli/src/utils/config.ts | 55 +++++++++++++++------ packages/cli/src/utils/environment-tasks.ts | 17 ++----- 5 files changed, 60 insertions(+), 42 deletions(-) diff --git a/packages/cli/src/commands/dev.ts b/packages/cli/src/commands/dev.ts index 57884c4a..90f4a050 100644 --- a/packages/cli/src/commands/dev.ts +++ b/packages/cli/src/commands/dev.ts @@ -67,7 +67,7 @@ export default class Dev extends Command { title: `Creating service "${dir.name}"`, task: async (ctx) => { const definition = await Service.compile(join(args.PATH, 'services', dir.name), this.ipfsClient) - const service = await Service.create(this.lcd, definition, ctx.mnemonic) + const service = await Service.create(this.lcd, definition, ctx.config.mnemonic) this.services.push(service) } } @@ -81,8 +81,8 @@ export default class Dev extends Command { tasks.add({ title: `Creating process "${file.name}"`, task: async (ctx) => { - const compilation = await Process.compile(join(args.PATH, file.name), this.ipfsClient, this.lcd, this.lcdEndpoint, ctx.mnemonic, env) - const deployedProcess = await Process.create(this.lcd, compilation.definition, ctx.mnemonic) + const compilation = await Process.compile(join(args.PATH, file.name), this.ipfsClient, this.lcd, this.lcdEndpoint, ctx.config.mnemonic, env) + const deployedProcess = await Process.create(this.lcd, compilation.definition, ctx.config.mnemonic) this.processes.push(deployedProcess) this.runners = [ ...this.runners, @@ -106,7 +106,7 @@ export default class Dev extends Command { } }) - const { mnemonic } = await tasks.run({ + const { config } = await tasks.run({ configDir: this.config.dataDir, endpoint: this.lcdEndpoint, pull: flags.pull, @@ -144,7 +144,7 @@ export default class Dev extends Command { task: async () => { const uniqueRunners = this.runners.filter((item, i, self) => i === self.indexOf(item)) for (const runner of uniqueRunners) { - await Runner.stop(this.lcdEndpoint, mnemonic, runner.hash) + await Runner.stop(this.lcdEndpoint, config.mnemonic, runner.hash) } } }, @@ -152,7 +152,7 @@ export default class Dev extends Command { title: 'Deleting processes', task: async () => { for (const process of this.processes) { - await Process.remove(this.lcd, process, mnemonic) + await Process.remove(this.lcd, process, config.mnemonic) } } }, diff --git a/packages/cli/src/commands/process/dev.ts b/packages/cli/src/commands/process/dev.ts index 6f0c1b59..95455bff 100644 --- a/packages/cli/src/commands/process/dev.ts +++ b/packages/cli/src/commands/process/dev.ts @@ -52,13 +52,13 @@ export default class Dev extends Command { { title: 'Compiling process', task: async ctx => { - compilation = await Process.compile(args.PROCESS_FILE, this.ipfsClient, this.lcd, this.lcdEndpoint, ctx.mnemonic, flags.env) + compilation = await Process.compile(args.PROCESS_FILE, this.ipfsClient, this.lcd, this.lcdEndpoint, ctx.config.mnemonic, flags.env) } }, { title: 'Creating process', task: async ctx => { - deployedProcess = await Process.create(this.lcd, compilation.definition, ctx.mnemonic) + deployedProcess = await Process.create(this.lcd, compilation.definition, ctx.config.mnemonic) } }, { @@ -75,7 +75,7 @@ export default class Dev extends Command { } } ]) - const { mnemonic } = await tasks.run({ + const { config } = await tasks.run({ configDir: this.config.dataDir, pull: flags.pull, version: flags.version, @@ -110,14 +110,14 @@ export default class Dev extends Command { { title: 'Deleting process', task: async () => { - if (deployedProcess) await Process.remove(this.lcd, deployedProcess, mnemonic) + if (deployedProcess) await Process.remove(this.lcd, deployedProcess, config.mnemonic) } }, { title: 'Stopping services', task: async () => { for (const runner of compilation.runners) { - await Runner.stop(this.lcdEndpoint, mnemonic, runner.hash) + await Runner.stop(this.lcdEndpoint, config.mnemonic, runner.hash) } } }, diff --git a/packages/cli/src/commands/service/dev.ts b/packages/cli/src/commands/service/dev.ts index 47f4fc9b..59b14979 100644 --- a/packages/cli/src/commands/service/dev.ts +++ b/packages/cli/src/commands/service/dev.ts @@ -67,13 +67,13 @@ export default class Dev extends Command { { title: 'Creating service', task: async ctx => { - service = await Service.create(this.lcd, definition, ctx.mnemonic) + service = await Service.create(this.lcd, definition, ctx.config.mnemonic) } }, { title: 'Starting service', task: async ctx => { - runner = await Runner.create(this.lcdEndpoint, ctx.mnemonic, service.hash, flags.env) + runner = await Runner.create(this.lcdEndpoint, ctx.config.mnemonic, service.hash, flags.env) } }, { @@ -118,7 +118,7 @@ export default class Dev extends Command { ]) } ]) - const res = await tasks.run({ + const { config } = await tasks.run({ configDir: this.config.dataDir, pull: flags.pull, version: flags.version, @@ -156,7 +156,7 @@ export default class Dev extends Command { title: 'Stopping service', skip: () => !service && !runner, task: async () => { - return Runner.stop(this.lcdEndpoint, res.mnemonic, runner.hash) + return Runner.stop(this.lcdEndpoint, config.mnemonic, runner.hash) } }, Environment.stop, diff --git a/packages/cli/src/utils/config.ts b/packages/cli/src/utils/config.ts index 7311283f..c15a2584 100644 --- a/packages/cli/src/utils/config.ts +++ b/packages/cli/src/utils/config.ts @@ -1,29 +1,54 @@ -import { existsSync, readFileSync, rmdirSync, writeFileSync } from "fs" +import { existsSync, readFileSync, rmdirSync, writeFileSync, unlinkSync } from "fs" import { safeLoad, safeDump } from "js-yaml" import { join } from "path" import Account from "@mesg/api/lib/account-lcd" -import merge from "lodash.merge" -const FILE = 'config.yml' +export type Config = { + engine: { + account: { + mnemonic: string + } + } + mnemonic: string +} + +const ENGINE_FILE = 'config.yml' +const CLI_FILE = '.mesgrc' export const clear = (path: string): void => { - (rmdirSync as any)(path, { recursive: true }) + const rm = rmdirSync as any + rm(join(path, 'cosmos'), { recursive: true }) + rm(join(path, 'tendermint'), { recursive: true }) + unlinkSync(join(path, ENGINE_FILE)) + unlinkSync(join(path, CLI_FILE)) } -export const read = (path: string): any => { - return existsSync(join(path, FILE)) - ? safeLoad(readFileSync(join(path, FILE)).toString()) +const read = (filepath: string): Config => { + return existsSync(filepath) + ? safeLoad(readFileSync(filepath).toString()) : {} } -export const write = (path: string, config: any) => { - const newConfigs = merge({}, read(path), config) - writeFileSync(join(path, FILE), safeDump(newConfigs)) +const write = (path: string, config: Config): Config => { + writeFileSync(join(path, CLI_FILE), safeDump(config)) + writeFileSync(join(path, ENGINE_FILE), safeDump(config.engine)) + return config +} + +export const hasTestAccount = (path: string): boolean => { + const config = read(join(path, CLI_FILE)) + return !!config.mnemonic && !!config.engine.account.mnemonic } -export const getOrGenerateAccount = (path: string): string => { - const configAccount = read(path).account || {} - return configAccount.mnemonic - ? configAccount.mnemonic - : Account.generateMnemonic() +export const generateConfig = (path: string): Config => { + const mnemonic = Account.generateMnemonic() + const config = { + engine: { + account: { + mnemonic: Account.generateMnemonic() + } + }, + mnemonic + } + return write(path, config) } \ No newline at end of file diff --git a/packages/cli/src/utils/environment-tasks.ts b/packages/cli/src/utils/environment-tasks.ts index 837e4e66..707866e3 100644 --- a/packages/cli/src/utils/environment-tasks.ts +++ b/packages/cli/src/utils/environment-tasks.ts @@ -2,7 +2,7 @@ import Listr, { ListrTask } from "listr" import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" import { join } from "path" import { hasImage, fetchImageTag, createContainer, listContainers, findNetwork, engineLabel, engineName } from "./docker" -import { getOrGenerateAccount, write, clear } from "./config" +import { generateConfig, clear, Config, hasTestAccount } from "./config" import fetch from "node-fetch" const pidFilename = 'pid.json' @@ -72,7 +72,7 @@ export const stop: ListrTask = { ]) } -export type IStart = { configDir: string, pull: boolean, version: string, endpoint: string, mnemonic?: string } +export type IStart = { configDir: string, pull: boolean, version: string, endpoint: string, config?: Config } export const start: ListrTask = { title: 'Starting environment', task: () => new Listr([ @@ -87,8 +87,8 @@ export const start: ListrTask = { }, { title: 'Generating test account', - skip: ctx => ctx.mnemonic, - task: ctx => ctx.mnemonic = getOrGenerateAccount(ctx.configDir) + skip: ctx => hasTestAccount(ctx.configDir), + task: ctx => ctx.config = generateConfig(ctx.configDir) }, { title: 'Updating the Engine image', @@ -98,14 +98,7 @@ export const start: ListrTask = { { title: 'Starting the Engine', skip: async () => (await listContainers({ label: [engineLabel] })).length > 0, - task: ctx => { - write(ctx.configDir, { - account: { - mnemonic: ctx.mnemonic - } - }) - return createContainer(`${image}:${ctx.version}`, ctx.configDir) - } + task: ctx => createContainer(`${image}:${ctx.version}`, ctx.configDir) }, { title: 'Waiting for the Engine to be ready',