Cleanup containers as well as images, this stops issues with having containers hanging around on an old image, stopping that image from being deleted.

This commit is contained in:
Pagan Gazzard 2014-10-08 15:41:59 +01:00 committed by Pablo Carranza Vélez
parent 5e869f50a3
commit 7e3c9707cb

View File

@ -268,7 +268,7 @@ exports.update = update = ->
updateDeviceInfo(status: 'Cleaning old images') updateDeviceInfo(status: 'Cleaning old images')
# We cleanup here as we want a point when we have a consistent apps/images state, rather than potentially at a # We cleanup here as we want a point when we have a consistent apps/images state, rather than potentially at a
# point where we might clean up an image we still want. # point where we might clean up an image we still want.
cleanupImages() cleanupContainersAndImages()
.catch (err) -> .catch (err) ->
failedUpdates++ failedUpdates++
if currentlyUpdating is 2 if currentlyUpdating is 2
@ -312,25 +312,34 @@ exports.updateDeviceInfo = updateDeviceInfo = (body) ->
apikey: apiKey apikey: apiKey
) )
cleanupImages = -> cleanupContainersAndImages = ->
knex('app').select() knex('app').select()
.map (app) ->
app.imageId + ':latest'
.then (apps) -> .then (apps) ->
apps = apps.map((app) -> app.imageId + ':latest')
# Make sure not to delete the supervisor image! # Make sure not to delete the supervisor image!
apps.push(config.localImage + ':latest') apps.push(config.localImage + ':latest')
apps.push(config.remoteImage + ':latest') apps.push(config.remoteImage + ':latest')
docker.listImagesAsync() # Cleanup containers first, so that they don't block image removal.
.then (images) -> docker.listContainersAsync(all: true)
Promise.all( .filter (containerInfo) ->
images.filter (image) -> !_.contains(apps, containerInfo.Image)
!_.any image.RepoTags, (imageId) -> .map (containerInfo) ->
_.contains(apps, imageId) docker.getContainer(containerInfo.Id).removeAsync()
.map (image) -> .then ->
# TODO: Remove old supervisor containers cleanly so we don't have to force remove images. console.log('Deleted container:', containerInfo.Id, containerInfo.Image)
docker.getImage(image.Id).removeAsync(force: true) .catch (err) ->
.then -> console.log('Error deleting container:', containerInfo.Id, image.Image, err)
console.log('Deleted image:', image.Id, image.RepoTags) .then ->
.catch (err) -> # And then clean up the images.
console.log('Error deleting image:', image.Id, image.RepoTags, err) docker.listImagesAsync()
) .filter (image) ->
!_.any image.RepoTags, (imageId) ->
_.contains(apps, imageId)
.map (image) ->
docker.getImage(image.Id).removeAsync()
.then ->
console.log('Deleted image:', image.Id, image.RepoTags)
.catch (err) ->
console.log('Error deleting image:', image.Id, image.RepoTags, err)