From 3975939181ee8a1116837342bd97fce531638f32 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Thu, 14 Aug 2014 13:35:05 +0100 Subject: [PATCH] Cleanup unneeded images after a successful update. --- src/application.coffee | 27 +++++++++++++++++++++++++++ src/config.coffee | 3 +++ src/supervisor-update.coffee | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/application.coffee b/src/application.coffee index 54aabecd..789290c9 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -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) + ) diff --git a/src/config.coffee b/src/config.coffee index 21a9664c..4081a8b1 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -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': {} diff --git a/src/supervisor-update.coffee b/src/supervisor-update.coffee index d44fa541..2f0f4df8 100644 --- a/src/supervisor-update.coffee +++ b/src/supervisor-update.coffee @@ -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)