mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-18 17:00:25 +00:00
Enable periodic update checks, and add logic to only switch to the new image if it is different.
This commit is contained in:
parent
0d2d372cb9
commit
4cfcd39524
@ -15,6 +15,7 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) ->
|
|||||||
.then ->
|
.then ->
|
||||||
api = require './api'
|
api = require './api'
|
||||||
application = require './application'
|
application = require './application'
|
||||||
|
supervisor = require './supervisor-update'
|
||||||
|
|
||||||
console.log('Starting OpenVPN..')
|
console.log('Starting OpenVPN..')
|
||||||
openvpn = spawn('openvpn', ['client.conf'], cwd: '/data')
|
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
|
, 5 * 60 * 1000) # Every 5 mins
|
||||||
application.update()
|
application.update()
|
||||||
|
|
||||||
|
console.log('Starting periodic check for supervisor updates..')
|
||||||
|
setInterval(->
|
||||||
|
supervisor.update()
|
||||||
|
, 5 * 60 * 1000) # Every 5 mins
|
||||||
|
supervisor.update()
|
||||||
|
|
||||||
|
@ -13,11 +13,15 @@ Promise.promisifyAll(docker.getContainer().__proto__)
|
|||||||
localImage = 'resin/rpi-supervisor'
|
localImage = 'resin/rpi-supervisor'
|
||||||
remoteImage = config.registryEndpoint + '/' + localImage
|
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()
|
supervisorUpdating = Promise.resolve()
|
||||||
exports.update = ->
|
exports.update = ->
|
||||||
# Make sure only one attempt to update the full supervisor is running at a time, ignoring any errors from previous update attempts
|
# 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 ->
|
supervisorUpdating = supervisorUpdating.catch(->).then ->
|
||||||
console.log('Fetching updated supervisor:', remoteImage)
|
console.log('Fetching supervisor:', remoteImage)
|
||||||
docker.createImageAsync(fromImage: remoteImage)
|
docker.createImageAsync(fromImage: remoteImage)
|
||||||
.then (stream) ->
|
.then (stream) ->
|
||||||
return new Promise (resolve, reject) ->
|
return new Promise (resolve, reject) ->
|
||||||
@ -29,12 +33,21 @@ exports.update = ->
|
|||||||
|
|
||||||
stream.on('end', resolve)
|
stream.on('end', resolve)
|
||||||
.then ->
|
.then ->
|
||||||
console.log('Tagging updated supervisor:', remoteImage)
|
console.log('Tagging supervisor:', remoteImage)
|
||||||
docker.getImage(remoteImage).tagAsync(
|
docker.getImage(remoteImage).tagAsync(
|
||||||
repo: localImage
|
repo: localImage
|
||||||
force: true
|
force: true
|
||||||
)
|
)
|
||||||
.then ->
|
.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)
|
console.log('Creating updated supervisor container:', localImage)
|
||||||
docker.createContainerAsync(
|
docker.createContainerAsync(
|
||||||
Image: localImage
|
Image: localImage
|
||||||
@ -47,19 +60,19 @@ exports.update = ->
|
|||||||
for envVar in config.expectedEnvVars
|
for envVar in config.expectedEnvVars
|
||||||
envVar + '=' + process.env[envVar]
|
envVar + '=' + process.env[envVar]
|
||||||
)
|
)
|
||||||
.then (container) ->
|
.then (container) ->
|
||||||
console.log('Starting updated supervisor container:', localImage)
|
console.log('Starting updated supervisor container:', localImage)
|
||||||
container.startAsync(
|
container.startAsync(
|
||||||
Privileged: true
|
Privileged: true
|
||||||
Binds: [
|
Binds: [
|
||||||
'/mnt/mmcblk0p1/config.json:/boot/config.json'
|
'/mnt/mmcblk0p1/config.json:/boot/config.json'
|
||||||
'/var/run/docker.sock:/run/docker.sock'
|
'/var/run/docker.sock:/run/docker.sock'
|
||||||
'/var/lib/docker/data:/data'
|
'/var/lib/docker/data:/data'
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
.then ->
|
.then ->
|
||||||
# We've started the new container, so we're done here! #pray
|
# We've started the new container, so we're done here! #pray
|
||||||
process.exit()
|
process.exit()
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
console.error('Error updating supervisor:', err)
|
console.error('Error updating supervisor:', err)
|
||||||
throw err
|
throw err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user