Allow all config variables to take truthy or falsy values

Config variables now use a checkTruthy validation function,
and can be "1", "on", "true" or true to be considered true, or
"0", "off", "false" or false to be considered false.

Change-Type: minor
Signed-off-by: Pablo Carranza Velez <pablo@resin.io>
This commit is contained in:
Pablo Carranza Velez 2017-03-08 10:19:52 -03:00 committed by Pablo Carranza Vélez
parent 9ae4f02bc5
commit 538e384442
4 changed files with 18 additions and 8 deletions

View File

@ -15,7 +15,7 @@ TypedError = require 'typed-error'
fs = Promise.promisifyAll(require('fs'))
JSONStream = require 'JSONStream'
proxyvisor = require './proxyvisor'
{ checkInt } = require './lib/validation'
{ checkInt, checkTruthy } = require './lib/validation'
deviceConfig = require './device-config'
class UpdatesLockedError extends TypedError
@ -405,7 +405,7 @@ apiPollInterval = (val) ->
application.poll()
setLocalMode = (val) ->
mode = (val == '1')
mode = checkTruthy(val) ? false
Promise.try ->
if mode and !application.localMode
logSystemMessage('Entering local mode, app will be forcefully stopped', {}, 'Enter local mode')

View File

@ -10,6 +10,7 @@ configPath = '/boot/config.json'
execAsync = Promise.promisify(require('child_process').exec)
fs = Promise.promisifyAll(require('fs'))
bootstrap = require './bootstrap'
{ checkTruthy } = require './lib/validation'
memoizePromise = _.partial(memoizee, _, promise: true)
@ -78,7 +79,7 @@ exports.setHostConfig = (env, oldEnv, logMessage) ->
setLogToDisplay = (env, oldEnv, logMessage) ->
if env['RESIN_HOST_LOG_TO_DISPLAY']?
enable = env['RESIN_HOST_LOG_TO_DISPLAY'] != '0'
enable = checkTruthy(env['RESIN_HOST_LOG_TO_DISPLAY']) ? true
utils.gosuper.postAsync('/v1/set-log-to-display', { json: true, body: Enable: enable })
.spread (response, body) ->
if response.statusCode != 200

View File

@ -14,4 +14,11 @@ exports.checkString = (s) ->
# This might happen if the parsing of config.json on the host using jq is wrong (it is buggy in some versions).
if !s? or s == 'null' or s == 'undefined' or s == ''
return
return s
return s
exports.checkTruthy = (v) ->
if v == '1' or v == 'true' or v == true or v == 'on'
return true
if v == '0' or v == 'false' or v == false or v == 'off'
return false
return

View File

@ -13,6 +13,7 @@ logger = require './lib/logger'
TypedError = require 'typed-error'
execAsync = Promise.promisify(require('child_process').exec)
device = require './device'
{ checkTruthy } = require './lib/validation'
# Parses package.json and returns resin-supervisor's version
version = require('../package.json').version
@ -179,13 +180,14 @@ exports.extendEnvVars = (env, uuid, appId, appName, commit) ->
# Callback function to enable/disable tcp pings
exports.enableConnectivityCheck = (val) ->
bool = val is 'false'
disableCheck(bool)
enabled = checkTruthy(val) ? true
disableCheck(!enabled)
console.log("Connectivity check enabled: #{not bool}")
# Callback function to enable/disable logs
exports.resinLogControl = (val) ->
logger.disableLogPublishing(val == 'false')
logEnabled = checkTruthy(val) ? true
logger.disableLogPublishing(!logEnabled)
console.log('Logs enabled: ' + val)
emptyHostRequest = request.defaults({ headers: Host: '' })
@ -208,7 +210,7 @@ exports.gosuper = gosuper =
# Callback function to enable/disable VPN
exports.vpnControl = (val) ->
enable = val != 'false'
enable = checkTruthy(val) ? true
gosuper.postAsync('/v1/vpncontrol', { json: true, body: Enable: enable })
.spread (response, body) ->
if response.statusCode == 202