From 5caace400fe6d7c6fafbc9e5c6fad07c51daae45 Mon Sep 17 00:00:00 2001 From: Petros Angelatos Date: Fri, 9 May 2014 04:42:04 +0100 Subject: [PATCH] Send logs through pubnub --- package.json | 3 ++- src/application.coffee | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 47247334..ec37b733 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/application.coffee b/src/application.coffee index b8051ff9..d2f4ab59 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -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)