Avoid stopping the VPN until a remote target state has been fetched, and retry applying config variables when they fail

Change-Type: patch
Signed-off-by: Pablo Carranza Velez <pablo@resin.io>
This commit is contained in:
Pablo Carranza Velez 2017-12-08 00:16:34 -08:00
parent 481888fc8b
commit 8fc1a0935b
2 changed files with 34 additions and 12 deletions

View File

@ -466,6 +466,7 @@ apiPollInterval = (val) ->
console.log('New API poll interval: ' + val)
clearInterval(updateStatus.intervalHandle)
application.poll()
return true
setLocalMode = (val) ->
mode = checkTruthy(val) ? false
@ -490,6 +491,7 @@ setLocalMode = (val) ->
unlockAndStart(app)
.then ->
application.localMode = mode
.return(true)
specialActionConfigVars = [
[ 'RESIN_SUPERVISOR_LOCAL_MODE', setLocalMode ]
@ -501,15 +503,19 @@ specialActionConfigVars = [
executedSpecialActionConfigVars = {}
executeSpecialActionsAndHostConfig = (conf, oldConf) ->
executeSpecialActionsAndHostConfig = (conf, oldConf, opts) ->
updatedValues = _.clone(oldConf)
needsReboot = false
Promise.mapSeries specialActionConfigVars, ([ key, specialActionCallback ]) ->
if (conf[key]? or oldConf[key]?) and specialActionCallback?
# This makes the Special Action Envs only trigger their functions once.
if executedSpecialActionConfigVars[key] != conf[key]
logSpecialAction(key, conf[key])
Promise.try ->
specialActionCallback(conf[key])
.then ->
specialActionCallback(conf[key], logSystemMessage, opts)
.then (updated) ->
if updated
updatedValues[key] = conf[key]
executedSpecialActionConfigVars[key] = conf[key]
logSpecialAction(key, conf[key], true)
.then ->
@ -519,14 +525,22 @@ executeSpecialActionsAndHostConfig = (conf, oldConf) ->
return _.startsWith(key, device.hostConfigConfigVarPrefix)
if !_.isEqual(hostConfigVars, oldHostConfigVars)
device.setHostConfig(hostConfigVars, oldHostConfigVars, logSystemMessage)
.then (changedHostConfig) ->
needsReboot = changedHostConfig
if changedHostConfig
_.forEach oldHostConfigVars, (val, key) ->
delete updatedValues[key]
_.assign(updatedValues, hostConfigVars)
.then ->
return { updatedValues, needsReboot }
getAndApplyDeviceConfig = ->
getAndApplyDeviceConfig = ({ initial = false } = {}) ->
deviceConfig.get()
.then ({ values, targetValues }) ->
executeSpecialActionsAndHostConfig(targetValues, values)
.tap ->
deviceConfig.set({ values: targetValues }) if !_.isEqual(values, targetValues)
.then (needsReboot) ->
executeSpecialActionsAndHostConfig(targetValues, values, { initial })
.tap ({ updatedValues }) ->
deviceConfig.set({ values: updatedValues }) if !_.isEqual(values, updatedValues)
.then ({ needsReboot }) ->
if needsReboot
logSystemMessage('Rebooting', {}, 'Reboot')
Promise.delay(1000)
@ -922,7 +936,7 @@ application.initialize = ->
listenToEvents()
migrateContainerIdApps()
.then ->
getAndApplyDeviceConfig()
getAndApplyDeviceConfig(initial: true)
.then ->
knex('app').whereNot(markedForDeletion: true).orWhereNull('markedForDeletion').select()
.map (app) ->

View File

@ -184,12 +184,14 @@ exports.enableConnectivityCheck = (val) ->
enabled = checkTruthy(val) ? true
disableCheck(!enabled)
console.log("Connectivity check enabled: #{enabled}")
return true
# Callback function to enable/disable logs
exports.resinLogControl = (val) ->
logEnabled = checkTruthy(val) ? true
logger.disableLogPublishing(!logEnabled)
console.log('Logs enabled: ' + val)
return true
emptyHostRequest = request.defaults({ headers: Host: '' })
gosuperRequest = (method, endpoint, options = {}, callback) ->
@ -210,14 +212,20 @@ exports.gosuper = gosuper =
getAsync: Promise.promisify(gosuperGet, multiArgs: true)
# Callback function to enable/disable VPN
exports.vpnControl = (val) ->
exports.vpnControl = (val, logMessage, { initial = false } = {}) ->
enable = checkTruthy(val) ? true
# If it's the initial run, we always want the VPN enabled, so we ignore calls to disable it
if initial and !enable
return Promise.resolve(false)
gosuper.postAsync('/v1/vpncontrol', { json: true, body: Enable: enable })
.spread (response, body) ->
if response.statusCode == 202
console.log('VPN enabled: ' + enable)
return true
else
console.log('Error: ' + body + ' response:' + response.statusCode)
logMessage("Error (#{response.statusCode}) toggling VPN: #{body}", {}, 'Toggle VPN error')
return false
.catchReturn(false)
exports.AppNotFoundError = class AppNotFoundError extends TypedError