mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-31 16:35:23 +00:00
Deduplicate knex app fetching logic.
This commit is contained in:
parent
4278b6baf1
commit
a371e35d5c
@ -1,6 +1,5 @@
|
||||
Promise = require 'bluebird'
|
||||
utils = require './utils'
|
||||
knex = require './db'
|
||||
express = require 'express'
|
||||
bodyParser = require 'body-parser'
|
||||
bufferEq = require 'buffer-equal-constant-time'
|
||||
@ -58,10 +57,8 @@ module.exports = (application) ->
|
||||
if !appId?
|
||||
return res.status(400).send('Missing app id')
|
||||
Promise.using application.lockUpdates(appId, true), ->
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new Error('App not found')
|
||||
utils.getKnexApp(appId)
|
||||
.then (app) ->
|
||||
application.kill(app)
|
||||
.then ->
|
||||
new Promise (resolve, reject) ->
|
||||
@ -71,6 +68,8 @@ module.exports = (application) ->
|
||||
.pipe(res)
|
||||
.finally ->
|
||||
application.start(app)
|
||||
.catch utils.AppNotFoundError, (e) ->
|
||||
return res.status(400).send(e.message)
|
||||
.catch (err) ->
|
||||
res.status(503).send(err?.message or err or 'Unknown error')
|
||||
|
||||
@ -89,15 +88,15 @@ module.exports = (application) ->
|
||||
if !appId?
|
||||
return res.status(400).send('Missing app id')
|
||||
Promise.using application.lockUpdates(appId, force), ->
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new Error('App not found')
|
||||
utils.getKnexApp(appId)
|
||||
.then (app) ->
|
||||
application.kill(app)
|
||||
.then ->
|
||||
application.start(app)
|
||||
.then ->
|
||||
res.status(200).send('OK')
|
||||
.catch utils.AppNotFoundError, (e) ->
|
||||
return res.status(400).send(e.message)
|
||||
.catch (err) ->
|
||||
res.status(503).send(err?.message or err or 'Unknown error')
|
||||
|
||||
@ -108,13 +107,13 @@ module.exports = (application) ->
|
||||
if !appId?
|
||||
return res.status(400).send('Missing app id')
|
||||
Promise.using application.lockUpdates(appId, force), ->
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new Error('App not found')
|
||||
utils.getKnexApp(appId)
|
||||
.tap (app) ->
|
||||
application.kill(app, true, false)
|
||||
.then ->
|
||||
res.json(_.pick(app, 'containerId'))
|
||||
.then (app) ->
|
||||
res.json(_.pick(app, 'containerId'))
|
||||
.catch utils.AppNotFoundError, (e) ->
|
||||
return res.status(400).send(e.message)
|
||||
.catch (err) ->
|
||||
res.status(503).send(err?.message or err or 'Unknown error')
|
||||
|
||||
@ -124,13 +123,13 @@ module.exports = (application) ->
|
||||
if !appId?
|
||||
return res.status(400).send('Missing app id')
|
||||
Promise.using application.lockUpdates(appId), ->
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new Error('App not found')
|
||||
utils.getKnexApp(appId)
|
||||
.tap (app) ->
|
||||
application.start(app)
|
||||
.then ->
|
||||
res.json(_.pick(app, 'containerId'))
|
||||
.then (app) ->
|
||||
res.json(_.pick(app, 'containerId'))
|
||||
.catch utils.AppNotFoundError, (e) ->
|
||||
return res.status(400).send(e.message)
|
||||
.catch (err) ->
|
||||
res.status(503).send(err?.message or err or 'Unknown error')
|
||||
|
||||
@ -141,14 +140,14 @@ module.exports = (application) ->
|
||||
return res.status(400).send('Missing app id')
|
||||
Promise.using application.lockUpdates(appId, true), ->
|
||||
columns = [ 'appId', 'containerId', 'commit', 'imageId', 'env' ]
|
||||
knex('app').select(columns).where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new Error('App not found')
|
||||
utils.getKnexApp(appId, columns)
|
||||
.then (app) ->
|
||||
# Don't return keys on the endpoint
|
||||
app.env = _.omit(JSON.parse(app.env), config.privateAppEnvVars)
|
||||
# Don't return data that will be of no use to the user
|
||||
res.json(app)
|
||||
.catch utils.AppNotFoundError, (e) ->
|
||||
return res.status(400).send(e.message)
|
||||
.catch (err) ->
|
||||
res.status(503).send(err?.message or err or 'Unknown error')
|
||||
|
||||
@ -181,15 +180,16 @@ module.exports = (application) ->
|
||||
onStatus = (status) ->
|
||||
status = JSON.stringify(status) if _.isObject(status)
|
||||
res.write(status)
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
return res.status(400).send('App not found') if !app?
|
||||
utils.getKnexApp(appId)
|
||||
.then (app) ->
|
||||
res.status(200)
|
||||
compose.up(application.composePath(appId), onStatus)
|
||||
.catch (err) ->
|
||||
console.log('Error on compose up:', err, err.stack)
|
||||
.finally ->
|
||||
res.end()
|
||||
.catch utils.AppNotFoundError, (e) ->
|
||||
return res.status(400).send(e.message)
|
||||
.catch (err) ->
|
||||
res.status(503).send(err?.message or err or 'Unknown error')
|
||||
|
||||
@ -198,15 +198,16 @@ module.exports = (application) ->
|
||||
onStatus = (status) ->
|
||||
status = JSON.stringify(status) if _.isObject(status)
|
||||
res.write(status)
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
return res.status(400).send('App not found') if !app?
|
||||
utils.getKnexApp(appId)
|
||||
.then (app) ->
|
||||
res.status(200)
|
||||
compose.down(application.composePath(appId), onStatus)
|
||||
.catch (err) ->
|
||||
console.log('Error on compose down:', err, err.stack)
|
||||
.finally ->
|
||||
res.end()
|
||||
.catch utils.AppNotFoundError, (e) ->
|
||||
return res.status(400).send(e.message)
|
||||
.catch (err) ->
|
||||
res.status(503).send(err?.message or err or 'Unknown error')
|
||||
|
||||
|
@ -392,13 +392,6 @@ wrapAsError = (err) ->
|
||||
return err if _.isError(err)
|
||||
return new Error(err.message ? err)
|
||||
|
||||
select = (appId) ->
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new Error('App not found')
|
||||
return app
|
||||
|
||||
# Wait for app to signal it's ready to die, or timeout to complete.
|
||||
# timeout defaults to 1 minute.
|
||||
waitToKill = (app, timeout) ->
|
||||
@ -435,7 +428,7 @@ updateStrategies =
|
||||
.then ->
|
||||
Promise.using lockUpdates(localApp, force), ->
|
||||
logSystemEvent(logTypes.updateApp, app) if localApp.imageId == app.imageId
|
||||
select(localApp.appId)
|
||||
utils.getKnexApp(localApp.appId)
|
||||
.then(kill)
|
||||
.then ->
|
||||
start(app)
|
||||
@ -445,7 +438,7 @@ updateStrategies =
|
||||
'kill-then-download': ({ localApp, app, needsDownload, force }) ->
|
||||
Promise.using lockUpdates(localApp, force), ->
|
||||
logSystemEvent(logTypes.updateApp, app) if localApp.imageId == app.imageId
|
||||
select(localApp.appId)
|
||||
utils.getKnexApp(localApp.appId)
|
||||
.then(kill)
|
||||
.then ->
|
||||
fetch(app) if needsDownload
|
||||
@ -456,7 +449,7 @@ updateStrategies =
|
||||
throw err
|
||||
'hand-over': ({ localApp, app, needsDownload, force, timeout }) ->
|
||||
Promise.using lockUpdates(localApp, force), ->
|
||||
select(localApp.appId)
|
||||
utils.getKnexApp(localApp.appId)
|
||||
.then (localApp) ->
|
||||
Promise.try ->
|
||||
fetch(app) if needsDownload
|
||||
@ -577,10 +570,8 @@ application.update = update = (force) ->
|
||||
# If an env var shouldn't cause a restart but requires an action, we should still
|
||||
# save the new env to the DB
|
||||
if !_.includes(toBeUpdated, appId) and !_.includes(toBeInstalled, appId)
|
||||
knex('app').select().where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new Error('App not found')
|
||||
utils.getKnexApp(appId)
|
||||
.then (app) ->
|
||||
app.env = JSON.stringify(remoteAppEnvs[appId])
|
||||
knex('app').update(app).where({ appId })
|
||||
.then (needsReboot) ->
|
||||
@ -595,7 +586,7 @@ application.update = update = (force) ->
|
||||
Promise.using lockUpdates(localApps[appId], force), ->
|
||||
# We get the app from the DB again in case someone restarted it
|
||||
# (which would have changed its containerId)
|
||||
select(appId)
|
||||
utils.getKnexApp(appId)
|
||||
.then(kill)
|
||||
.then ->
|
||||
knex('app').where('appId', appId).delete()
|
||||
|
@ -10,6 +10,7 @@ url = require 'url'
|
||||
randomHexString = require './lib/random-hex-string'
|
||||
request = Promise.promisifyAll require 'request'
|
||||
logger = require './lib/logger'
|
||||
TypedError = require 'typed-error'
|
||||
|
||||
# Parses package.json and returns resin-supervisor's version
|
||||
version = require('../package.json').version
|
||||
@ -180,3 +181,13 @@ exports.vpnControl = (val) ->
|
||||
console.log('VPN enabled: ' + enable)
|
||||
else
|
||||
console.log('Error: ' + body + ' response:' + response.statusCode)
|
||||
|
||||
exports.AppNotFoundError = class AppNotFoundError extends TypedError
|
||||
|
||||
exports.getKnexApp = (appId, columns) ->
|
||||
knex('app').select(columns).where({ appId })
|
||||
.then ([ app ]) ->
|
||||
if !app?
|
||||
throw new AppNotFoundError('App not found')
|
||||
return app
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user