From f39b51885dccec51e0c4a1851a0a62857ad18a05 Mon Sep 17 00:00:00 2001 From: Lorenzo Stoakes Date: Mon, 15 Sep 2014 12:31:14 +0100 Subject: [PATCH] Syntax cleanup pass. --- src/app.coffee | 10 +++++----- src/application.coffee | 23 ++++++++++++----------- src/config.coffee | 2 +- src/supervisor-update.coffee | 7 ++++--- src/supervisor.coffee | 3 ++- src/tty.coffee | 4 ++-- src/utils.coffee | 16 +++++++++------- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/app.coffee b/src/app.coffee index 23836bc0..7cb844ef 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -11,7 +11,7 @@ utils.mixpanelTrack('Supervisor start') connectivityState = true # Used to prevent multiple messages when disconnected -ensureConnected = (continuous=false) -> +ensureConnected = (continuous = false) -> utils.checkConnectivity() .then (connected) -> if not connected @@ -34,7 +34,7 @@ ensureConnected = (continuous=false) -> , 10 * 1000) # Every 10 seconds perform this check. -knex('config').select('value').where(key: 'uuid').then ([uuid]) -> +knex('config').select('value').where(key: 'uuid').then ([ uuid ]) -> if not uuid?.value console.log('New device detected. Bootstrapping..') ensureConnected().then -> @@ -51,7 +51,7 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) -> supervisor = require './supervisor-update' console.log('Starting OpenVPN..') - openvpn = spawn('openvpn', ['client.conf'], cwd: '/data') + openvpn = spawn('openvpn', [ 'client.conf' ], cwd: '/data') # Prefix and log all OpenVPN output openvpn.stdout.on 'data', (data) -> @@ -71,7 +71,7 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) -> .then (apps) -> Promise.all(apps.map(application.start)) .catch (error) -> - console.error("Error starting apps:", error) + console.error('Error starting apps:', error) .then -> console.log('Starting periodic check for updates..') setInterval(-> @@ -98,4 +98,4 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) -> ensureConnected(true) # Let the previous supervisor know that we started successfully - console.log(config.successMessage) + console.log(config.successMessage) diff --git a/src/application.coffee b/src/application.coffee index 94a89612..9118ea26 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -27,13 +27,13 @@ pubnub = PUBNUB.init(config.pubnub) publish = do -> publishQueue = [] - knex('config').select('value').where(key: 'uuid').then ([uuid]) -> + knex('config').select('value').where(key: 'uuid').then ([ uuid ]) -> uuid = uuid.value channel = "device-#{uuid}-logs" # Redefine original function publish = (message) -> - pubnub.publish({channel, message}) + pubnub.publish({ channel, message }) # Replay queue now that we have initialised the publish function publish(args...) for args in publishQueue @@ -111,7 +111,7 @@ exports.start = start = (app) -> stream.on('end', resolve) .then -> - console.log("Creating container:", app.imageId) + console.log('Creating container:', app.imageId) updateDeviceInfo(status: 'Starting') ports = {} if portList? @@ -120,7 +120,7 @@ exports.start = start = (app) -> docker.createContainerAsync( Image: app.imageId - Cmd: ['/bin/bash', '-c', '/start'] + Cmd: [ '/bin/bash', '-c', '/start' ] Tty: true Volumes: '/data': {} @@ -182,7 +182,7 @@ exports.update = update = -> knex('config').select('value').where(key: 'uuid') knex('app').select() ]) - .then ([[apiKey], [uuid], apps]) -> + .then ([ [ apiKey ], [ uuid ], apps ]) -> apiKey = apiKey.value uuid = uuid.value resinAPI.get( @@ -195,7 +195,7 @@ exports.update = update = -> apikey: apiKey ) .then (remoteApps) -> - console.log("Remote apps") + console.log('Remote apps') remoteApps = _.filter(remoteApps, 'commit') remoteApps = _.map remoteApps, (app) -> env = @@ -217,21 +217,22 @@ exports.update = update = -> remoteImages = _.keys(remoteApps) console.log(remoteImages) - console.log("Local apps") + console.log('Local apps') apps = _.indexBy(apps, 'imageId') - localApps = _.mapValues(apps, (app) -> _.pick(app, ['appId', 'commit', 'imageId', 'env'])) + localApps = _.mapValues apps, (app) -> + _.pick(app, [ 'appId', 'commit', 'imageId', 'env' ]) localImages = _.keys(localApps) console.log(localImages) - console.log("Apps to be removed") + console.log('Apps to be removed') toBeRemoved = _.difference(localImages, remoteImages) console.log(toBeRemoved) - console.log("Apps to be installed") + console.log('Apps to be installed') toBeInstalled = _.difference(remoteImages, localImages) console.log(toBeInstalled) - console.log("Apps to be updated") + console.log('Apps to be updated') toBeUpdated = _.intersection(remoteImages, localImages) toBeUpdated = _.filter toBeUpdated, (imageId) -> return !_.isEqual(remoteApps[imageId], localApps[imageId]) diff --git a/src/config.coffee b/src/config.coffee index a354c48c..bde38d3f 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -14,7 +14,7 @@ module.exports = config = config.remoteImage = config.registryEndpoint + '/' + config.localImage config.supervisorContainer = - Volumes: + Volumes: '/boot/config.json': {} '/data': {} '/run/docker.sock': {} diff --git a/src/supervisor-update.coffee b/src/supervisor-update.coffee index 62c33ce0..a4572643 100644 --- a/src/supervisor-update.coffee +++ b/src/supervisor-update.coffee @@ -16,7 +16,7 @@ remoteImage = config.remoteImage getContainerId = -> fs.readFileAsync( '/proc/1/cgroup' ) .then (data) -> - data.toString().match( /:cpu:\/docker\/(.*)$/m )[ 1 ] + data.toString().match( /:cpu:\/docker\/(.*)$/m )[1] .catch (err) -> return process.env.HOSTNAME @@ -28,7 +28,7 @@ startNewSupervisor = (currentSupervisor) -> console.log('Creating supervisor container:', localImage) docker.createContainerAsync( Image: localImage - Cmd: ['/start'] + Cmd: [ '/start' ] Volumes: config.supervisorContainer.Volumes Env: currentSupervisor.Config.Env ) @@ -86,7 +86,8 @@ exports.initialised = currentSupervisor.then (currentSupervisor) -> 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 + # 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 -> utils.mixpanelTrack('Supervisor update check') console.log('Fetching supervisor:', remoteImage) diff --git a/src/supervisor.coffee b/src/supervisor.coffee index eb7f54e2..5c1653bb 100755 --- a/src/supervisor.coffee +++ b/src/supervisor.coffee @@ -3,7 +3,8 @@ process.on 'uncaughtException', (e) -> supervisor = require './supervisor-update' -# Make sure the supervisor-update has initialised before we continue, as it will handle restarting to add mounts if necessary. +# Make sure the supervisor-update has initialised before we continue, as it will +# handle restarting to add mounts if necessary. supervisor.initialised.then -> knex = require './db' diff --git a/src/tty.coffee b/src/tty.coffee index c17f6838..9b98adc8 100644 --- a/src/tty.coffee +++ b/src/tty.coffee @@ -12,14 +12,14 @@ exports.start = (appId) -> return apps[appId] = apps[appId].catch -> port = nextPort++ knex('app').select().where({appId}) - .then ([app]) -> + .then ([ app ]) -> if !app? throw new Error('App not found') tty.createServer shell: './src/enterContainer.sh' shellArgs: do -> i = 0 - return (session) -> [app.containerId, session.id, i++] + return (session) -> [ app.containerId, session.id, i++ ] .listenAsync(port, null) .then -> ngrok.connectAsync(port) diff --git a/src/utils.coffee b/src/utils.coffee index 9045f322..3e5b9711 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -17,7 +17,7 @@ mixpanelClient = mixpanel.init(config.mixpanelToken) exports.mixpanelProperties = mixpanelProperties = username: require('/boot/config.json').username -exports.mixpanelTrack = (event, properties={}) -> +exports.mixpanelTrack = (event, properties = {}) -> console.log('Event:', event, JSON.stringify(properties)) # Mutation is bad, and it should feel bad properties = _.assign(_.cloneDeep(properties), mixpanelProperties) @@ -35,7 +35,9 @@ exports.findIpAddrs = -> # We only care about LOCAL routes (not UNICAST or BROADCAST) if line.match(/LOCAL$/) - # Then we make sure the previous line was an ending branch (and hence contains an IP - 127.0.0.0 has BROADCAST and LOCAL entries) + # Then we make sure the previous line was an ending branch (and + # hence contains an IP - 127.0.0.0 has BROADCAST and LOCAL + # entries) if prevLine.match(/^\|--/) # Then we remove the ending branch bit maybeAddr = prevLine.replace(/^\|--/, '').trim() @@ -56,8 +58,8 @@ exports.blink = (ms = 200) -> # Helps in checking connectivity by pinging the given site. exports.checkConnectivity = (host = '8.8.8.8') -> - ping.sys.promise_probe(host, - timeout: 1 - extra: ["-c 1"] - ).then (res) -> - return res.alive + ping.sys.promise_probe(host, + timeout: 1 + extra: [ '-c 1' ] + ).then (res) -> + return res.alive