44
55import * as sg from '@edge/stargate-utils'
66import { Command } from 'commander'
7- import { Context } from '../main'
87import config from '../config'
98import dotenv from 'dotenv'
109import { AuthConfig , DockerOptions } from 'dockerode'
10+ import { Context , Network } from '../main'
1111import { 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. */
2641export 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. */
3862export 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. */
6093export 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. */
65103export 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. */
75118export 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. */
103156export 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. */
148215export 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. */
164237export 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}
0 commit comments