From 37212cedbe017f4252cfa77aad90462b981b7124 Mon Sep 17 00:00:00 2001 From: Page Date: Wed, 19 Mar 2014 19:17:07 +0000 Subject: [PATCH] Remove a load of unnecessary parens. --- src/application.coffee | 64 ++++++++++++++++++++---------------------- src/bootstrap.coffee | 21 +++++++------- src/db.coffee | 17 ++++++----- src/utils.coffee | 6 ++-- 4 files changed, 51 insertions(+), 57 deletions(-) diff --git a/src/application.coffee b/src/application.coffee index 227bd261..b4b79b4d 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -15,24 +15,22 @@ Promise.promisifyAll(docker.getImage().__proto__) Promise.promisifyAll(docker.getContainer().__proto__) exports.kill = kill = (app) -> - docker.listContainersAsync(all: 1).then((containers) -> + docker.listContainersAsync(all: 1) + .then (containers) -> Promise.all( containers - .filter((container) -> - return container.Image is "#{app}:latest" - ) - .map((container) -> docker.getContainer(container.Id)) - .map((container) -> - console.log("Stopping and deleting container:", container) - container.stopAsync().then(-> - container.removeAsync() - ) - ) + .filter (container) -> container.Image is "#{app}:latest" + .map (container) -> docker.getContainer(container.Id) + .map (container) -> + console.log("Stopping and deleting container:", container) + container.stopAsync() + .then -> + container.removeAsync() ) - ) exports.start = start = (app) -> - docker.getImage(app).inspectAsync().catch((error) -> + docker.getImage(app).inspectAsync() + .catch (error) -> console.log("Pulling image:", app) deferred = Promise.defer() options = @@ -40,27 +38,25 @@ exports.start = start = (app) -> path: "/v1.8/images/create?fromImage=#{app}" socketPath: '/hostrun/docker.sock' - req = http.request(options, (res) -> + req = http.request options, (res) -> if res.headers['content-type'] is 'application/json' res.pipe(JSONStream.parse('error')) - .pipe(es.mapSync((error) -> - deferred.reject(error) - )) + .pipe es.mapSync (error) -> + deferred.reject(error) else - res.pipe(es.wait((error, text) -> deferred.reject(text))) + res.pipe es.wait (error, text) -> deferred.reject(text) - res.on('end', -> + res.on 'end', -> if res.statusCode is 200 deferred.resolve() else deferred.reject(res.statusCode) - ) - ) + req.end() - req.on('error', (error) -> deferred.reject(error)) + req.on 'error', (error) -> deferred.reject(error) return deferred.promise - ).then(-> + .then -> console.log("Creating container:", app) docker.createContainerAsync( Image: app @@ -68,31 +64,32 @@ exports.start = start = (app) -> Volumes: '/dev': {} ) - ).then((container) -> + .then (container) -> container.startAsync( Privileged: true Binds: ['/dev:/dev'] ) - ) exports.restart = restart = (app) -> - kill(app).then(-> + kill(app) + .then -> start(app) - ) exports.update = -> Promise.all([ knex('config').select('value').where(key: 'apiKey') knex('config').select('value').where(key: 'uuid') knex('app').select() - ]).then(([[apiKey], [uuid], apps]) -> + ]) + .then ([[apiKey], [uuid], apps]) -> apiKey = apiKey.value uuid = uuid.value request( method: 'GET' url: url.resolve(process.env.API_ENDPOINT, "/ewa/application?$filter=device/uuid eq '#{uuid}'&apikey=#{apiKey}") json: true - ).spread((request, body) -> + ) + .spread (request, body) -> console.log("Remote apps") remoteApps = ("registry.resin.io/#{path.basename(app.git_repository, '.git')}/#{app.commit}" for app in body.d when app.commit) console.log(remoteApps) @@ -111,14 +108,13 @@ exports.update = -> # Install the apps and add each to the db as they succeed promises = toBeInstalled.map (app) -> - start(app).then -> + start(app) + .then -> knex('app').insert({imageId: app}) # And delete all the ones to remove in one go promises.push( - Promise.all(toBeRemoved.map(kill)).then -> + Promise.all(toBeRemoved.map(kill)) + .then -> knex('app').whereIn('imageId', toBeRemoved).delete() ) Promise.all(promises) - ) - - ) diff --git a/src/bootstrap.coffee b/src/bootstrap.coffee index d5a217f3..2805b47c 100644 --- a/src/bootstrap.coffee +++ b/src/bootstrap.coffee @@ -25,7 +25,8 @@ module.exports = (uuid) -> division: '' ) - Promise.all([config, keys]).then(([config, keys]) -> + Promise.all([config, keys]) + .then ([config, keys]) -> console.log('UUID:', uuid) console.log('User ID:', config.userId) console.log('User:', config.username) @@ -40,36 +41,34 @@ module.exports = (uuid) -> url: url.resolve(process.env.API_ENDPOINT, 'associate') json: config ) - ).spread((response, body) -> + .spread (response, body) -> if response.statusCode >= 400 throw body console.log('Configuring VPN..') vpnConf = fs.readFileAsync('/supervisor/src/openvpn.conf.tmpl', 'utf8') - .then((tmpl) -> + .then (tmpl) -> fs.writeFileAsync('/supervisor/data/client.conf', _.template(tmpl)(body)) - ) Promise.all([ fs.writeFileAsync('/supervisor/data/ca.crt', body.ca) fs.writeFileAsync('/supervisor/data/client.crt', body.cert) vpnConf ]) - ).then(-> + .then -> console.log('Finishing bootstrapping') Promise.all([ - knex('config').truncate().then(-> - config.then((config) -> + knex('config').truncate() + .then -> + config + .then (config) -> knex('config').insert([ {key: 'uuid', value: uuid} {key: 'apiKey', value: config.apiKey} {key: 'username', value: config.username} {key: 'userId', value: config.userId} ]) - ) - ) knex('app').truncate() ]) - ).catch((error) -> + .catch (error) -> console.log(error) - ) diff --git a/src/db.coffee b/src/db.coffee index 5f6add24..0dd0e2cf 100644 --- a/src/db.coffee +++ b/src/db.coffee @@ -1,5 +1,5 @@ Promise = require 'bluebird' -Knex = require('knex') +Knex = require 'knex' knex = Knex.initialize( client: 'sqlite3' @@ -8,23 +8,22 @@ knex = Knex.initialize( ) knex.init = Promise.all([ - knex.schema.hasTable('config').then((exists) -> + knex.schema.hasTable('config') + .then (exists) -> if not exists - knex.schema.createTable('config', (t) -> + knex.schema.createTable 'config', (t) -> t.string('key').primary() t.string('value') - ) - ) - knex.schema.hasTable('app').then((exists) -> + + knex.schema.hasTable('app') + .then (exists) -> if not exists - knex.schema.createTable('app', (t) -> + knex.schema.createTable 'app', (t) -> t.increments('id').primary() t.string('name') t.string('containerId') t.string('imageId') t.boolean('privileged') - ) - ) ]) module.exports = knex diff --git a/src/utils.coffee b/src/utils.coffee index 98704f14..6d9817b5 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -7,14 +7,14 @@ crypto = require 'crypto' # or the hostname if there isn't a serial number (when run in dev mode) # The uuid is the SHA1 hash of that value. exports.getDeviceUuid = -> - fs.readFileAsync('/proc/cpuinfo', 'utf8').then((cpuinfo) -> + fs.readFileAsync('/proc/cpuinfo', 'utf8') + .then (cpuinfo) -> serial = cpuinfo .split('\n') .filter((line) -> line.indexOf('Serial') isnt -1)[0] ?.split(':')[1] .trim() or os.hostname() - + return crypto.createHash('sha1').update(serial, 'utf8').digest('hex') - ) exports.delay = (ms) -> new Promise (v) -> setTimeout(v, ms)