service: Fix device name env vars by injecting and filtering in the interface with docker

Change-Type: patch
Signed-off-by: Pablo Carranza Velez <pablo@balena.io>
This commit is contained in:
Pablo Carranza Velez 2018-10-18 14:23:58 +02:00
parent 88b3682d51
commit 220f4b5810
3 changed files with 13 additions and 10 deletions

View File

@ -100,8 +100,6 @@ module.exports = class ServiceManager extends EventEmitter
.then (existingService) => .then (existingService) =>
return @docker.getContainer(existingService.containerId) return @docker.getContainer(existingService.containerId)
.catch NotFoundError, => .catch NotFoundError, =>
conf = service.toDockerContainer()
nets = service.extraNetworksToJoin()
@config.get('name') @config.get('name')
.then (deviceName) => .then (deviceName) =>
@ -111,9 +109,8 @@ module.exports = class ServiceManager extends EventEmitter
'Please fix the device name.' 'Please fix the device name.'
) )
# TODO: Don't mutate service like this, use an interface conf = service.toDockerContainer({ deviceName })
service.config.environment['RESIN_DEVICE_NAME_AT_INIT'] = deviceName nets = service.extraNetworksToJoin()
service.config.environment['BALENA_DEVICE_NAME_AT_INIT'] = deviceName
@logger.logSystemEvent(logTypes.installService, { service }) @logger.logSystemEvent(logTypes.installService, { service })
@reportNewStatus(mockContainerId, service, 'Installing') @reportNewStatus(mockContainerId, service, 'Installing')

View File

@ -425,7 +425,10 @@ export class Service {
entrypoint: container.Config.Entrypoint || '', entrypoint: container.Config.Entrypoint || '',
volumes: _.concat(container.HostConfig.Binds || [], _.keys(container.Config.Volumes || { })), volumes: _.concat(container.HostConfig.Binds || [], _.keys(container.Config.Volumes || { })),
image: container.Config.Image, 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, privileged: container.HostConfig.Privileged || false,
labels: ComposeUtils.normalizeLabels(container.Config.Labels || { }), labels: ComposeUtils.normalizeLabels(container.Config.Labels || { }),
running: container.State.Running, running: container.State.Running,
@ -483,7 +486,7 @@ export class Service {
return svc; return svc;
} }
public toDockerContainer(): Dockerode.ContainerCreateOptions { public toDockerContainer(opts: { deviceName: string }): Dockerode.ContainerCreateOptions {
const { binds, volumes } = this.getBindsAndVolumes(); const { binds, volumes } = this.getBindsAndVolumes();
const { exposedPorts, portBindings } = this.generateExposeAndPorts(); const { exposedPorts, portBindings } = this.generateExposeAndPorts();
@ -503,7 +506,10 @@ export class Service {
Volumes: volumes, Volumes: volumes,
// Typings are wrong here, the docker daemon accepts a string or string[], // Typings are wrong here, the docker daemon accepts a string or string[],
Entrypoint: this.config.entrypoint as 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, ExposedPorts: exposedPorts,
Image: this.config.image, Image: this.config.image,
Labels: this.config.labels, Labels: this.config.labels,

View File

@ -305,7 +305,7 @@ describe 'compose/service', ->
"balena": { "balena": {
"ipv4Address": "1.2.3.4" "ipv4Address": "1.2.3.4"
} }
}).toDockerContainer().NetworkingConfig).to.deep.equal({ }).toDockerContainer({ deviceName: 'foo' }).NetworkingConfig).to.deep.equal({
EndpointsConfig: { EndpointsConfig: {
"123456_balena": { "123456_balena": {
IPAMConfig: { IPAMConfig: {
@ -323,7 +323,7 @@ describe 'compose/service', ->
ipv6Address: '5.6.7.8' ipv6Address: '5.6.7.8'
linkLocalIps: [ '123.123.123' ] linkLocalIps: [ '123.123.123' ]
} }
}).toDockerContainer().NetworkingConfig).to.deep.equal({ }).toDockerContainer({ deviceName: 'foo' }).NetworkingConfig).to.deep.equal({
EndpointsConfig: { EndpointsConfig: {
"123456_balena": { "123456_balena": {
IPAMConfig: { IPAMConfig: {