mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-21 14:37:49 +00:00
Remove a load of unnecessary parens.
This commit is contained in:
parent
3c3e81a892
commit
37212cedbe
@ -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)
|
||||
)
|
||||
|
||||
)
|
||||
|
@ -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)
|
||||
)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user