Enable periodic update checks, and add logic to only switch to the new image if it is different.

This commit is contained in:
Page 2014-06-16 21:25:06 +01:00 committed by Pablo Carranza Vélez
parent 0d2d372cb9
commit 4cfcd39524
2 changed files with 35 additions and 15 deletions

View File

@ -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()

View File

@ -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