From 220f4b58109f158b59412d54177957999441a7f6 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Thu, 18 Oct 2018 14:23:58 +0200 Subject: [PATCH] service: Fix device name env vars by injecting and filtering in the interface with docker Change-Type: patch Signed-off-by: Pablo Carranza Velez --- src/compose/service-manager.coffee | 7 ++----- src/compose/service.ts | 12 +++++++++--- test/04-service.spec.coffee | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/compose/service-manager.coffee b/src/compose/service-manager.coffee index 62e0a7f8..104c72c4 100644 --- a/src/compose/service-manager.coffee +++ b/src/compose/service-manager.coffee @@ -100,8 +100,6 @@ module.exports = class ServiceManager extends EventEmitter .then (existingService) => return @docker.getContainer(existingService.containerId) .catch NotFoundError, => - conf = service.toDockerContainer() - nets = service.extraNetworksToJoin() @config.get('name') .then (deviceName) => @@ -111,9 +109,8 @@ module.exports = class ServiceManager extends EventEmitter 'Please fix the device name.' ) - # TODO: Don't mutate service like this, use an interface - service.config.environment['RESIN_DEVICE_NAME_AT_INIT'] = deviceName - service.config.environment['BALENA_DEVICE_NAME_AT_INIT'] = deviceName + conf = service.toDockerContainer({ deviceName }) + nets = service.extraNetworksToJoin() @logger.logSystemEvent(logTypes.installService, { service }) @reportNewStatus(mockContainerId, service, 'Installing') diff --git a/src/compose/service.ts b/src/compose/service.ts index a53c2634..f6021114 100644 --- a/src/compose/service.ts +++ b/src/compose/service.ts @@ -425,7 +425,10 @@ export class Service { entrypoint: container.Config.Entrypoint || '', volumes: _.concat(container.HostConfig.Binds || [], _.keys(container.Config.Volumes || { })), image: container.Config.Image, - environment: conversions.envArrayToObject(container.Config.Env || [ ]), + environment: _.omit(conversions.envArrayToObject(container.Config.Env || [ ]), [ + 'RESIN_DEVICE_NAME_AT_INIT', + 'BALENA_DEVICE_NAME_AT_INIT', + ]), privileged: container.HostConfig.Privileged || false, labels: ComposeUtils.normalizeLabels(container.Config.Labels || { }), running: container.State.Running, @@ -483,7 +486,7 @@ export class Service { return svc; } - public toDockerContainer(): Dockerode.ContainerCreateOptions { + public toDockerContainer(opts: { deviceName: string }): Dockerode.ContainerCreateOptions { const { binds, volumes } = this.getBindsAndVolumes(); const { exposedPorts, portBindings } = this.generateExposeAndPorts(); @@ -503,7 +506,10 @@ export class Service { Volumes: volumes, // Typings are wrong here, the docker daemon accepts a string or string[], Entrypoint: this.config.entrypoint as string, - Env: conversions.envObjectToArray(this.config.environment), + Env: conversions.envObjectToArray(_.assign({ + RESIN_DEVICE_NAME_AT_INIT: opts.deviceName, + BALENA_DEVICE_NAME_AT_INIT: opts.deviceName, + }, this.config.environment)), ExposedPorts: exposedPorts, Image: this.config.image, Labels: this.config.labels, diff --git a/test/04-service.spec.coffee b/test/04-service.spec.coffee index cf3e54bf..9d4edb06 100644 --- a/test/04-service.spec.coffee +++ b/test/04-service.spec.coffee @@ -305,7 +305,7 @@ describe 'compose/service', -> "balena": { "ipv4Address": "1.2.3.4" } - }).toDockerContainer().NetworkingConfig).to.deep.equal({ + }).toDockerContainer({ deviceName: 'foo' }).NetworkingConfig).to.deep.equal({ EndpointsConfig: { "123456_balena": { IPAMConfig: { @@ -323,7 +323,7 @@ describe 'compose/service', -> ipv6Address: '5.6.7.8' linkLocalIps: [ '123.123.123' ] } - }).toDockerContainer().NetworkingConfig).to.deep.equal({ + }).toDockerContainer({ deviceName: 'foo' }).NetworkingConfig).to.deep.equal({ EndpointsConfig: { "123456_balena": { IPAMConfig: {