Skip to content

Commit 27e0620

Browse files
authored
Merge pull request #136 from edge/develop
v1.8.5
2 parents 4a4ad56 + 6ec8ffd commit 27e0620

File tree

9 files changed

+115
-15
lines changed

9 files changed

+115
-15
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@edge/cli",
3-
"version": "1.8.4",
3+
"version": "1.8.5",
44
"description": "Command line interface for the Edge network",
55
"private": true,
66
"author": "Edge Network <[email protected]>",

src/cli/docker.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import * as sg from '@edge/stargate-utils'
66
import { Command } from 'commander'
7-
import { Context } from '../main'
87
import config from '../config'
98
import dotenv from 'dotenv'
109
import { AuthConfig, DockerOptions } from 'dockerode'
10+
import { Context, Network } from '../main'
1111
import { existsSync, readFileSync } from 'fs'
1212

1313
/** Docker environment (env) options. */
@@ -22,6 +22,21 @@ export type EnvFileOption = {
2222
envFile?: string
2323
}
2424

25+
/** Docker extra hosts options. */
26+
export type ExtraHostsOption = {
27+
/** Extra hosts configuration for a Docker container. */
28+
extraHosts: string[]
29+
}
30+
31+
/** Docker Gateway options. */
32+
export type GatewayOption = {
33+
/**
34+
* Gateway host - a specialised extra host configuration.
35+
* See `ExtraHostsOption`
36+
*/
37+
gateway?: string
38+
}
39+
2540
/** Docker networking options. */
2641
export type NetworksOption = {
2742
/** Docker networks for a container to join. */
@@ -34,6 +49,15 @@ export type PrefixOption = {
3449
prefix?: string
3550
}
3651

52+
/** Docker Stargate options. */
53+
export type StargateOption = {
54+
/**
55+
* Stargate host - a specialised extra host configuration.
56+
* See `ExtraHostsOption`
57+
*/
58+
stargate?: string
59+
}
60+
3761
/** Docker image tag options. */
3862
export type TargetOption = {
3963
/** Target tag of image to use. This is the `:version` component of the full tag. */
@@ -56,11 +80,25 @@ export const configureEnv = (cmd: Command): void => {
5680
cmd.option('-e, --env <var...>', 'set environment variable(s) for node')
5781
}
5882

83+
/**
84+
* Configure a command with extra hosts for Docker.
85+
* This option breaks the naming pattern within this file as `add-host` is more understandable and consistent with
86+
* the equivalent usage in `docker run`.
87+
*/
88+
export const configureExtraHosts = (cmd: Command): void => {
89+
cmd.option('--add-host <host...>', 'configure /etc/hosts within node')
90+
}
91+
5992
/** Configure a command with a Docker env file. */
6093
export const configureEnvFile = (cmd: Command): void => {
6194
cmd.option('--env-file <path>', 'set environment variables file for node')
6295
}
6396

97+
/** Configure a command with a Docker Gateway host. */
98+
export const configureGateway = (cmd: Command): void => {
99+
cmd.option('--gateway <host>', 'set Gateway host for node (if applicable)')
100+
}
101+
64102
/** Configure a command with Docker networking options. */
65103
export const configureNetworks = (cmd: Command): void => {
66104
cmd.option('--network <var...>', 'set network(s) for node')
@@ -71,6 +109,11 @@ export const configurePrefix = (cmd: Command): void => {
71109
cmd.option('--prefix <prefix>', 'Docker entity prefix')
72110
}
73111

112+
/** Configure a command with a Docker Stargate host. */
113+
export const configureStargate = (cmd: Command): void => {
114+
cmd.option('--stargate <host>', 'set Stargate host for node (if applicable)')
115+
}
116+
74117
/** Configure a command with Docker image tag options. */
75118
export const configureTarget = (cmd: Command): void => {
76119
cmd.option('--target <version>', 'node target version')
@@ -99,6 +142,16 @@ export const readAllEnv = (cmd: Command): EnvOption => {
99142
return { env }
100143
}
101144

145+
/** Read **all** extra host options for the Docker container from a command, including Gateway and Stargate hosts. */
146+
export const readAllExtraHosts = (cmd: Command, network: Network): ExtraHostsOption => {
147+
const { extraHosts } = readExtraHosts(cmd)
148+
const { gateway } = readGateway(cmd)
149+
if (gateway) extraHosts.push(`${network.gateway.host}:${gateway}`)
150+
const { stargate } = readStargate(cmd)
151+
if (stargate) extraHosts.push(`${network.stargate.host}:${stargate}`)
152+
return { extraHosts }
153+
}
154+
102155
/** Read Docker registry authentication options from a command. */
103156
export const readAuth = (cmd: Command): AuthConfig | undefined => {
104157
const opts = cmd.opts()
@@ -144,6 +197,20 @@ export const readEnvFile = (cmd: Command): EnvFileOption => {
144197
return { envFile: undefined }
145198
}
146199

200+
/** Read Docker extra hosts option from a command. */
201+
export const readExtraHosts = (cmd: Command): ExtraHostsOption => {
202+
const opts = cmd.opts()
203+
return {
204+
extraHosts: opts.addHost !== undefined ? opts.addHost : []
205+
}
206+
}
207+
208+
/** Read Docker Gateway option from a command. */
209+
export const readGateway = (cmd: Command): GatewayOption => {
210+
const opts = cmd.opts()
211+
return { gateway: opts.gateway }
212+
}
213+
147214
/** Read Docker network options from a command. */
148215
export const readNetworks = (cmd: Command): NetworksOption => {
149216
const opts = cmd.opts()
@@ -160,10 +227,16 @@ export const readPrefix = (cmd: Command): PrefixOption => {
160227
return { prefix: opts.prefix }
161228
}
162229

230+
/** Read Docker Stargate option from a command. */
231+
export const readStargate = (cmd: Command): StargateOption => {
232+
const opts = cmd.opts()
233+
return { stargate: opts.stargate }
234+
}
235+
163236
/** Read Docker image tag options from a command. */
164237
export const readTarget = async ({ cmd, network }: Context, name: string): Promise<TargetOption> => {
165238
const opts = cmd.opts()
166239
return {
167-
target: opts.target || (await sg.service.get(network.stargate.host, name)).version
240+
target: opts.target || (await sg.service.get(network.stargate.url, name)).version
168241
}
169242
}

src/device/cli/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a GNU GPL-style license
33
// that can be found in the LICENSE.md file. All rights reserved.
44

5+
import * as cli from '../../cli'
56
import * as xeUtils from '@edge/xe-utils'
67
import { Command } from 'commander'
78
import { Context } from '../../main'
@@ -23,15 +24,21 @@ type NodeInfo = {
2324
stake: xeUtils.stake.Stake
2425
}
2526

27+
type Options =
28+
cli.docker.EnvOption &
29+
cli.docker.ExtraHostsOption &
30+
cli.docker.GatewayOption &
31+
cli.docker.NetworksOption &
32+
cli.docker.PrefixOption &
33+
cli.docker.StargateOption
34+
2635
/**
2736
* Build a container options object for a new Docker container.
2837
*/
2938
export const createContainerOptions = (
3039
node: NodeInfo,
3140
tag: string,
32-
env: string[] | undefined,
33-
prefix?: string | undefined,
34-
networks?: string[]
41+
{ env, extraHosts, network, prefix }: Options
3542
): ContainerCreateOptions => {
3643
const containerName = [
3744
'edge',
@@ -58,6 +65,7 @@ export const createContainerOptions = (
5865
'/var/run/docker.sock:/var/run/docker.sock',
5966
`${volumeName}:/data`
6067
],
68+
ExtraHosts: extraHosts.length > 0 ? extraHosts : undefined,
6169
RestartPolicy: { Name: 'unless-stopped' }
6270
}
6371
}
@@ -72,9 +80,9 @@ export const createContainerOptions = (
7280
'443/tcp': {}
7381
}
7482
}
75-
if (networks?.length) {
83+
if (network?.length) {
7684
opts.NetworkingConfig = {
77-
EndpointsConfig: networks.reduce((e, n) => {
85+
EndpointsConfig: network.reduce((e, n) => {
7886
e[n] = {}
7987
return e
8088
}, <EndpointsConfig>{})

src/device/cli/start.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Context, Network } from '../../main'
2121
export const action = (ctx: Context) => async (): Promise<void> => {
2222
const opts = {
2323
...cli.docker.readAllEnv(ctx.cmd),
24+
...cli.docker.readAllExtraHosts(ctx.cmd, ctx.network),
2425
...cli.docker.readNetworks(ctx.cmd),
2526
...cli.docker.readPrefix(ctx.cmd)
2627
}
@@ -46,7 +47,7 @@ export const action = (ctx: Context) => async (): Promise<void> => {
4647
const { debug } = cli.debug.read(ctx.parent)
4748
await image.pullVisible(docker, targetImage, authconfig, debug)
4849

49-
const containerOptions = createContainerOptions(node, target, opts.env, opts.prefix, opts.network)
50+
const containerOptions = createContainerOptions(node, target, opts)
5051
log.debug('creating container', { containerOptions })
5152
const container = await docker.createContainer(containerOptions)
5253
log.debug('starting container')
@@ -62,8 +63,11 @@ export const command = (ctx: Context): Command => {
6263
cli.docker.configureAuth(cmd)
6364
cli.docker.configureEnv(cmd)
6465
cli.docker.configureEnvFile(cmd)
66+
cli.docker.configureExtraHosts(cmd)
67+
cli.docker.configureGateway(cmd)
6568
cli.docker.configureNetworks(cmd)
6669
cli.docker.configurePrefix(cmd)
70+
cli.docker.configureStargate(cmd)
6771
cli.docker.configureTarget(cmd)
6872
cmd.action(errorHandler(ctx, checkVersionHandler(ctx, action({ ...ctx, cmd }))))
6973
return cmd

src/device/cli/update.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ export const action = (ctx: Context) => async (): Promise<void> => {
7373
log.debug('removing container', { id: containerInspect?.Id })
7474
await container.remove()
7575

76-
const containerOptions = createContainerOptions(node, target, containerInspect?.Config.Env)
76+
const containerOptions = createContainerOptions(node, target, {
77+
env: containerInspect?.Config.Env || [],
78+
extraHosts: containerInspect?.HostConfig.ExtraHosts || []
79+
})
7780
if (containerInspect && Object.keys(containerInspect.NetworkSettings.Networks).length > 0) {
7881
const endpoints: EndpointsConfig = {}
7982
Object.keys(containerInspect.NetworkSettings.Networks).forEach(n => {

src/main-mainnet.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ main(process.argv, {
2727
flags: {
2828
onboarding: true
2929
},
30+
gateway: {
31+
host: 'gateway.edge.network'
32+
},
3033
index: {
3134
host: 'https://index.xe.network'
3235
},
3336
registry: {
3437
imageName: (app, arch) => `${config.docker.registry.address}/${app}/mainnet-${arch}`
3538
},
3639
stargate: {
37-
host: 'https://stargate.edge.network'
40+
host: 'stargate.edge.network',
41+
url: 'https://stargate.edge.network'
3842
},
3943
wallet: {
4044
defaultFile: `${homedir}${sep}.edge${sep}wallet${sep}mainnet.json`

src/main-testnet.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ main(process.argv, {
2727
flags: {
2828
onboarding: true
2929
},
30+
gateway: {
31+
host: 'gateway.test.network'
32+
},
3033
index: {
3134
host: 'https://index.test.network'
3235
},
3336
registry: {
3437
imageName: (app, arch) => `${config.docker.registry.address}/${app}/testnet-${arch}`
3538
},
3639
stargate: {
37-
host: 'https://stargate.test.network'
40+
host: 'stargate.test.network',
41+
url: 'https://stargate.test.network'
3842
},
3943
wallet: {
4044
defaultFile: `${homedir}${sep}.edge${sep}wallet${sep}testnet.json`

src/main.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ export type Network = {
6161
latestVersionURL: (os: string, arch: string) => string
6262
}
6363
flags: Record<string, boolean>
64+
gateway: {
65+
host: string
66+
}
6467
index: {
6568
host: string
6669
}
6770
registry: {
6871
imageName: (app: string, arch: string) => string
6972
}
7073
stargate: {
71-
host: sg.Host
74+
host: string
75+
url: sg.Host
7276
}
7377
wallet: {
7478
defaultFile: string

0 commit comments

Comments
 (0)