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) =>
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')

View File

@ -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,

View File

@ -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: {