Send logs through pubnub

This commit is contained in:
Petros Angelatos 2014-05-09 04:42:04 +01:00 committed by Pablo Carranza Vélez
parent 9325b258ae
commit 5caace400f
2 changed files with 27 additions and 1 deletions

View File

@ -18,7 +18,8 @@
"JSONStream": "~0.7.1",
"event-stream": "~3.0.20",
"sqlite3": "~2.1.19",
"resin-platform-api": "git+ssh://git@bitbucket.org:rulemotion/resin-platform-api.git#v0.2.3"
"resin-platform-api": "git+ssh://git@bitbucket.org:rulemotion/resin-platform-api.git#v0.2.3",
"pubnub": "~3.6.4"
},
"engines": {
"node": "0.10.22"

View File

@ -4,6 +4,7 @@ url = require 'url'
knex = require './db'
path = require 'path'
Docker = require 'dockerode'
PUBNUB = require 'pubnub'
Promise = require 'bluebird'
JSONStream = require 'JSONStream'
PlatformAPI = require 'resin-platform-api/request'
@ -17,6 +18,20 @@ docker = Promise.promisifyAll(new Docker(socketPath: DOCKER_SOCKET))
Promise.promisifyAll(docker.getImage().__proto__)
Promise.promisifyAll(docker.getContainer().__proto__)
pubnub = PUBNUB.init(
subscribe_key: 'sub-c-bananas'
publish_key: 'pub-c-bananas'
)
publish = null
knex('config').select('value').where(key: 'uuid').then ([uuid]) ->
uuid = uuid.value
channel = "device-#{uuid}-logs"
publish = (message) ->
pubnub.publish({channel, message})
exports.kill = kill = (app) ->
docker.listContainersAsync(all: 1)
.then (containers) ->
@ -59,6 +74,7 @@ exports.start = start = (app) ->
docker.createContainerAsync(
Image: app.imageId
Cmd: ['/bin/bash', '-c', '/start']
Tty: true
Volumes:
'/dev': {}
Env: _.map env, (v, k) -> k + '=' + v
@ -77,6 +93,15 @@ exports.start = start = (app) ->
'/var/run/docker.sock:/run/docker.sock'
]
)
.then ->
container.attach {stream: true, stdout: true, stderr: true, tty: true}, (err, stream) ->
es.pipeline(
stream
es.split()
# Remove color escape sequences
es.mapSync((s) -> s.replace(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/g, ''))
es.mapSync(publish)
)
.tap ->
console.log('Started container:', app.imageId)