From 60f0cd2fcb3b31afbd51f7a24a471ff405f7e304 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Mon, 8 Jan 2018 14:18:00 -0300 Subject: [PATCH] Change all labels to use hyphens instead of underscores, and fix some instances of using split instead of a regex Signed-off-by: Pablo Carranza Velez --- src/application-manager.coffee | 2 +- src/compose/networks.coffee | 2 +- src/compose/service-manager.coffee | 6 ++-- src/compose/service.coffee | 28 +++++++++++-------- src/compose/volumes.coffee | 2 +- src/lib/validation.coffee | 2 +- .../20171129064057_multicontainer.js | 8 +++--- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/application-manager.coffee b/src/application-manager.coffee index d0c4877e..0d7fbcec 100644 --- a/src/application-manager.coffee +++ b/src/application-manager.coffee @@ -629,7 +629,7 @@ module.exports = class ApplicationManager extends EventEmitter validStrategies = [ 'download-then-kill', 'kill-then-download', 'delete-then-download', 'hand-over' ] if !_.includes(validStrategies, strategy) strategy = 'download-then-kill' - timeout = checkInt(target.labels['io.resin.update.handover_timeout']) + timeout = checkInt(target.labels['io.resin.update.handover-timeout']) return @_strategySteps[strategy](current, target, needsDownload, dependenciesMetForStart, dependenciesMetForKill, needsSpecialKill, timeout) _nextStepsForAppUpdate: (currentApp, targetApp, availableImages = [], stepsInProgress = []) => diff --git a/src/compose/networks.coffee b/src/compose/networks.coffee index d95e722a..a05a61ae 100644 --- a/src/compose/networks.coffee +++ b/src/compose/networks.coffee @@ -10,7 +10,7 @@ module.exports = class Networks # TODO: parse supported config fields format: (network) -> - m = /^([0-9]+)_(.+)$/.match(network.Name) + m = network.Name.match(/^([0-9]+)_(.+)$/) appId = checkInt(m[1]) name = m[2] return { diff --git a/src/compose/service-manager.coffee b/src/compose/service-manager.coffee index 36555fa8..c710b8f4 100644 --- a/src/compose/service-manager.coffee +++ b/src/compose/service-manager.coffee @@ -82,7 +82,7 @@ module.exports = class ServiceManager extends EventEmitter return service getAllByAppId: (appId) => - @getAll("io.resin.app_id=#{appId}") + @getAll("io.resin.app-id=#{appId}") stopAllByAppId: (appId) => Promise.map @getAllByAppId(appId), (service) => @@ -164,7 +164,7 @@ module.exports = class ServiceManager extends EventEmitter # Returns an array with the container(s) matching a service by appId, commit, image and environment get: (service) => - @getAll("io.resin.service_id=#{service.serviceId}") + @getAll("io.resin.service-id=#{service.serviceId}") .filter((currentService) -> currentService.isSameContainer(service)) .get(0) @@ -219,7 +219,7 @@ module.exports = class ServiceManager extends EventEmitter .then => @start(targetService) .then => - @waitToKill(currentService, targetService.labels['io.resin.update.handover_timeout']) + @waitToKill(currentService, targetService.labels['io.resin.update.handover-timeout']) .then => @kill(currentService) diff --git a/src/compose/service.coffee b/src/compose/service.coffee index 50b13a23..1e7f4270 100644 --- a/src/compose/service.coffee +++ b/src/compose/service.coffee @@ -276,16 +276,16 @@ module.exports = class Service addFeaturesFromLabels: (opts) => if checkTruthy(@labels['io.resin.features.dbus']) @volumes.push('/run/dbus:/host/run/dbus') - if checkTruthy(@labels['io.resin.features.kernel_modules']) + if checkTruthy(@labels['io.resin.features.kernel-modules']) @volumes.push('/lib/modules:/lib/modules') if checkTruthy(@labels['io.resin.features.firmware']) @volumes.push('/lib/firmware:/lib/firmware') - if checkTruthy(@labels['io.resin.features.supervisor_api']) + if checkTruthy(@labels['io.resin.features.supervisor-api']) @_addSupervisorApi(opts) else # We ensure the user hasn't added "supervisor0" to the service's networks delete @networks[constants.supervisorNetworkInterface] - if checkTruthy(@labels['io.resin.features.resin_api']) + if checkTruthy(@labels['io.resin.features.resin-api']) @environment['RESIN_API_KEY'] = opts.deviceApiKey extendEnvVars: ({ imageInfo, uuid, appName, name, version, deviceType, osVersion }) => @@ -312,9 +312,9 @@ module.exports = class Service @labels = _.clone(@labels) _.defaults(@labels, imageInfo?.Config?.Labels ? {}) @labels['io.resin.supervised'] = 'true' - @labels['io.resin.app_id'] = @appId.toString() - @labels['io.resin.service_id'] = @serviceId.toString() - @labels['io.resin.service_name'] = @serviceName + @labels['io.resin.app-id'] = @appId.toString() + @labels['io.resin.service-id'] = @serviceId.toString() + @labels['io.resin.service-name'] = @serviceName return @labels extendAndSanitiseExposedPorts: (imageInfo) => @@ -333,10 +333,13 @@ module.exports = class Service for vol in @volumes isBind = /:/.test(vol) if isBind - [ bindSource, bindDest ] = vol.split(':') + [ bindSource, bindDest, mode ] = vol.split(':') if !path.isAbsolute(bindSource) # Rewrite named volumes to namespace by appId - volumes.push("#{@appId}_#{bindSource}:#{bindDest}") + volDefinition = "#{@appId}_#{bindSource}:#{bindDest}" + if mode? + volDefinition += ":#{mode}" + volumes.push(volDefinition) else console.log("Ignoring invalid bind mount #{vol}") else @@ -353,7 +356,8 @@ module.exports = class Service return null bindSource = vol.split(':')[0] if !path.isAbsolute(bindSource) - return bindSource.split('_')[1] + m = bindSource.match(/[0-9]+_(.+)/) + return m[1] else return null return _.filter(validVolumes, (v) -> !_.isNull(v)) @@ -395,9 +399,9 @@ module.exports = class Service if containerPort? and !_.includes(boundContainerPorts, containerPort) expose.push(containerPort) - appId = checkInt(container.Config.Labels['io.resin.app_id']) - serviceId = checkInt(container.Config.Labels['io.resin.service_id']) - serviceName = container.Config.Labels['io.resin.service_name'] + appId = checkInt(container.Config.Labels['io.resin.app-id']) + serviceId = checkInt(container.Config.Labels['io.resin.service-id']) + serviceName = container.Config.Labels['io.resin.service-name'] nameComponents = container.Name.match(/.*_(\d+)_(\d+)$/) imageId = checkInt(nameComponents?[1]) releaseId = checkInt(nameComponents?[2]) diff --git a/src/compose/volumes.coffee b/src/compose/volumes.coffee index eddb6637..2a617ff7 100644 --- a/src/compose/volumes.coffee +++ b/src/compose/volumes.coffee @@ -12,7 +12,7 @@ module.exports = class Volumes constructor: ({ @docker, @logger }) -> format: (volume) => - m = /^([0-9]+)_(.+)$/.match(volume.Name) + m = volume.Name.match(/^([0-9]+)_(.+)$/) appId = checkInt(m[1]) name = m[2] return { diff --git a/src/lib/validation.coffee b/src/lib/validation.coffee index ac030c71..ddcbfe65 100644 --- a/src/lib/validation.coffee +++ b/src/lib/validation.coffee @@ -32,7 +32,7 @@ exports.isValidEnv = isValidEnv = (obj) -> exports.isValidLabelsObject = isValidLabelsObject = (obj) -> _.isObject(obj) and _.every obj, (val, key) -> - isValidShortText(key) and /^[a-zA-Z_]+[a-zA-Z0-9_.]*$/.test(key) and _.isString(val) + isValidShortText(key) and /^[a-zA-Z_]+[a-zA-Z0-9\.\-]*$/.test(key) and _.isString(val) undefinedOrValidEnv = (val) -> if val? and !isValidEnv(val) diff --git a/src/migrations/20171129064057_multicontainer.js b/src/migrations/20171129064057_multicontainer.js index 8539db45..caae4f52 100644 --- a/src/migrations/20171129064057_multicontainer.js +++ b/src/migrations/20171129064057_multicontainer.js @@ -60,13 +60,13 @@ var singleToMulticontainerApp = function (app, appId) { `${defaultVolume}:/data` ], labels: { - 'io.resin.features.kernel_modules': '1', + 'io.resin.features.kernel-modules': '1', 'io.resin.features.firmware': '1', 'io.resin.features.dbus': '1', - 'io.resin.features.supervisor_api': '1', - 'io.resin.features.resin_api': '1', + 'io.resin.features.supervisor-api': '1', + 'io.resin.features.resin-api': '1', 'io.resin.update.strategy': updateStrategy, - 'io.resin.update.handover_timeout': handoverTimeout + 'io.resin.update.handover-timeout': handoverTimeout }, environment: environment, restart: restartPolicy,