Cleanup unneeded images after a successful update.

This commit is contained in:
Pagan Gazzard 2014-08-14 13:35:05 +01:00 committed by Pablo Carranza Vélez
parent 4cc3092b73
commit 3975939181
3 changed files with 31 additions and 1 deletions

View File

@ -244,6 +244,10 @@ exports.update = update = ->
Promise.all(installingPromises.concat(updatingPromises))
.then ->
failedUpdates = 0
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 point where we might clean up an image we still want.
cleanupImages()
.catch (err) ->
failedUpdates++
if currentlyUpdating is 2
@ -286,3 +290,26 @@ exports.updateDeviceInfo = updateDeviceInfo = (body) ->
customOptions:
apikey: apiKey
)
cleanupImages = ->
knex('app').select()
.then (apps) ->
apps = apps.map((app) -> app.imageId + ':latest')
# Make sure not to delete the supervisor image!
apps.push(config.localImage + ':latest')
apps.push(config.remoteImage + ':latest')
docker.listImagesAsync()
.then (images) ->
Promise.all(
images.filter (image) ->
!_.any image.RepoTags, (imageId) ->
_.contains(apps, imageId)
.map (image) ->
# TODO: Remove old supervisor containers cleanly so we don't have to force remove images.
docker.getImage(image.Id).removeAsync(force: true)
.then ->
console.log('Deleted image:', image.Id, image.RepoTags)
.catch (err) ->
console.log('Error deleting image:', image.Id, image.RepoTags, err)
)

View File

@ -8,6 +8,9 @@ module.exports = config =
dockerSocket: process.env.DOCKER_SOCKET ? '/run/docker.sock'
localImage: process.env.SUPERVISOR_IMAGE ? 'resin/rpi-supervisor'
configMountPoint: process.env.CONFIG_MOUNT_POINT ? '/mnt/mmcblk0p1/config.json'
config.remoteImage = config.registryEndpoint + '/' + config.localImage
config.supervisorContainer =
Volumes:
'/boot/config.json': {}

View File

@ -9,7 +9,7 @@ Promise.promisifyAll(docker.getImage().__proto__)
Promise.promisifyAll(docker.getContainer().__proto__)
localImage = config.localImage
remoteImage = config.registryEndpoint + '/' + localImage
remoteImage = config.remoteImage
startNewSupervisor = (currentSupervisor) ->
console.log('Creating supervisor container:', localImage)