From 4cfcd39524a598e670e00efb5fee4ff67b25f768 Mon Sep 17 00:00:00 2001 From: Page Date: Mon, 16 Jun 2014 21:25:06 +0100 Subject: [PATCH] Enable periodic update checks, and add logic to only switch to the new image if it is different. --- src/app.coffee | 7 ++++++ src/supervisor-update.coffee | 43 +++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/app.coffee b/src/app.coffee index eeec6c2f..08b63aa6 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -15,6 +15,7 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) -> .then -> api = require './api' application = require './application' + supervisor = require './supervisor-update' console.log('Starting OpenVPN..') openvpn = spawn('openvpn', ['client.conf'], cwd: '/data') @@ -45,3 +46,9 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) -> , 5 * 60 * 1000) # Every 5 mins application.update() + console.log('Starting periodic check for supervisor updates..') + setInterval(-> + supervisor.update() + , 5 * 60 * 1000) # Every 5 mins + supervisor.update() + diff --git a/src/supervisor-update.coffee b/src/supervisor-update.coffee index 781675e0..55ac3a84 100644 --- a/src/supervisor-update.coffee +++ b/src/supervisor-update.coffee @@ -13,11 +13,15 @@ Promise.promisifyAll(docker.getContainer().__proto__) localImage = 'resin/rpi-supervisor' remoteImage = config.registryEndpoint + '/' + localImage +# Docker sets the HOSTNAME as the container's short id. +currentSupervisorImage = docker.getContainer(process.env.HOSTNAME).inspectAsync().then (info) -> + return info.Image + supervisorUpdating = Promise.resolve() exports.update = -> # Make sure only one attempt to update the full supervisor is running at a time, ignoring any errors from previous update attempts supervisorUpdating = supervisorUpdating.catch(->).then -> - console.log('Fetching updated supervisor:', remoteImage) + console.log('Fetching supervisor:', remoteImage) docker.createImageAsync(fromImage: remoteImage) .then (stream) -> return new Promise (resolve, reject) -> @@ -29,12 +33,21 @@ exports.update = -> stream.on('end', resolve) .then -> - console.log('Tagging updated supervisor:', remoteImage) + console.log('Tagging supervisor:', remoteImage) docker.getImage(remoteImage).tagAsync( repo: localImage force: true ) .then -> + console.log('Inspecting newly tagged supervisor:', localImage) + Promise.all([ + docker.getImage(localImage).inspectAsync() + currentSupervisorImage + ]) + .spread (localImageInfo, currentSupervisorImage) -> + if localImageInfo.id == currentSupervisorImage + console.log('Supervisor is up to date') + return console.log('Creating updated supervisor container:', localImage) docker.createContainerAsync( Image: localImage @@ -47,19 +60,19 @@ exports.update = -> for envVar in config.expectedEnvVars envVar + '=' + process.env[envVar] ) - .then (container) -> - console.log('Starting updated supervisor container:', localImage) - container.startAsync( - Privileged: true - Binds: [ - '/mnt/mmcblk0p1/config.json:/boot/config.json' - '/var/run/docker.sock:/run/docker.sock' - '/var/lib/docker/data:/data' - ] - ) - .then -> - # We've started the new container, so we're done here! #pray - process.exit() + .then (container) -> + console.log('Starting updated supervisor container:', localImage) + container.startAsync( + Privileged: true + Binds: [ + '/mnt/mmcblk0p1/config.json:/boot/config.json' + '/var/run/docker.sock:/run/docker.sock' + '/var/lib/docker/data:/data' + ] + ) + .then -> + # We've started the new container, so we're done here! #pray + process.exit() .catch (err) -> console.error('Error updating supervisor:', err) throw err