Skip to content

Commit

Permalink
improve configuration management
Browse files Browse the repository at this point in the history
  • Loading branch information
antho1404 committed Apr 5, 2020
1 parent 95010ca commit 6145c8a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
12 changes: 6 additions & 6 deletions packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -144,15 +144,15 @@ 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)
}
}
},
{
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)
}
}
},
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/commands/process/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
{
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
}
},
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/service/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 40 additions & 15 deletions packages/cli/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 5 additions & 12 deletions packages/cli/src/utils/environment-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -72,7 +72,7 @@ export const stop: ListrTask<IStop> = {
])
}

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<IStart> = {
title: 'Starting environment',
task: () => new Listr([
Expand All @@ -87,8 +87,8 @@ export const start: ListrTask<IStart> = {
},
{
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',
Expand All @@ -98,14 +98,7 @@ export const start: ListrTask<IStart> = {
{
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',
Expand Down

0 comments on commit 6145c8a

Please sign in to comment.