From 503e043c4cfc3c54badee0654467a602bd6cb0ae Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Tue, 4 Oct 2016 17:22:30 -0300 Subject: [PATCH] In cleanup, normalize all image tags for comparison --- CHANGELOG.md | 1 + src/docker-utils.coffee | 36 ++++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 970f348f..e48b85fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +* In cleanup, normalize all image tags for comparison [Pablo] * Use getRegistryAndName from docker-toolbelt 1.2.0 [Pablo] # v2.5.0 diff --git a/src/docker-utils.coffee b/src/docker-utils.coffee index a17d6f5b..2ea7ab93 100644 --- a/src/docker-utils.coffee +++ b/src/docker-utils.coffee @@ -101,27 +101,32 @@ do -> Promise.using readLockImages(), -> dockerProgress.pull(image, onProgress) - supervisorTag = config.supervisorImage - if !/:/g.test(supervisorTag) - # If there is no tag then mark it as latest - supervisorTag += ':latest' + normalizeRepoTag = (image) -> + getRepoAndTag(image) + .then ({ repo, tag }) -> + buildRepoTag(repo, tag) + + supervisorTagPromise = normalizeRepoTag(config.supervisorImage) + exports.cleanupContainersAndImages = -> Promise.using writeLockImages(), -> Promise.join( knex('image').select('repoTag') - .map (image) -> - # Docker sometimes prepends 'docker.io/' to official images - return [ image.repoTag, 'docker.io/' + image.repoTag ] - .then(_.flatten) + .map ({ repoTag }) -> + normalizeRepoTag(repoTag) knex('app').select() .map ({ imageId }) -> - imageId + ':latest' + normalizeRepoTag(imageId) knex('dependentApp').select() .map ({ imageId }) -> - imageId + ':latest' + normalizeRepoTag(imageId) + supervisorTagPromise docker.listImagesAsync() - (locallyCreatedTags, apps, dependentApps, images) -> - imageTags = _.map(images, 'RepoTags') + .map (image) -> + image.NormalizedRepoTags = Promise.map(image.RepoTags, normalizeRepoTag) + Promise.props(image) + (locallyCreatedTags, apps, dependentApps, supervisorTag, images) -> + imageTags = _.map(images, 'NormalizedRepoTags') supervisorTags = _.filter imageTags, (tags) -> _.contains(tags, supervisorTag) appTags = _.filter imageTags, (tags) -> @@ -137,9 +142,8 @@ do -> docker.listContainersAsync(all: true) .filter (containerInfo) -> # Do not remove user apps. - getRepoAndTag(containerInfo.Image) - .then ({ repo, tag }) -> - repoTag = buildRepoTag(repo, tag) + normalizeRepoTag(containerInfo.Image) + .then (repoTag) -> if _.contains(appTags, repoTag) return false if _.contains(locallyCreatedTags, repoTag) @@ -154,7 +158,7 @@ do -> .catch(_.noop) .then -> imagesToClean = _.reject images, (image) -> - _.any image.RepoTags, (tag) -> + _.any image.NormalizedRepoTags, (tag) -> return _.contains(appTags, tag) or _.contains(supervisorTags, tag) or _.contains(locallyCreatedTags, tag) Promise.map imagesToClean, (image) -> Promise.map image.RepoTags.concat(image.Id), (tag) ->