<commit_before>_ = require 'lodash'
es = require 'event-stream'
url = require 'url'
knex = require './db'
path = require 'path'
Docker = require 'dockerode'
Promise = require 'bluebird'
JSONStream = require 'JSONStream'
PlatformAPI = require 'resin-platform-api/request'

DOCKER_SOCKET = '/run/docker.sock'
PLATFORM_ENDPOINT = url.resolve(process.env.API_ENDPOINT, '/ewa/')
resinAPI = new PlatformAPI(PLATFORM_ENDPOINT)

docker = Promise.promisifyAll(new Docker(socketPath: DOCKER_SOCKET))
# Hack dockerode to promisify internal classes' prototypes
Promise.promisifyAll(docker.getImage().__proto__)
Promise.promisifyAll(docker.getContainer().__proto__)

exports.kill = kill = (app) ->
	docker.listContainersAsync(all: 1)
	.then (containers) ->
		Promise.all(
			containers
			.filter (container) -> container.Image is "#{app.imageId}: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.imageId).inspectAsync()
	.catch (error) ->
		console.log("Pulling image:", app.imageId)
		docker.createImageAsync(fromImage: app.imageId)
		.then (stream) ->
			deferred = Promise.defer()

			if stream.headers['content-type'] is 'application/json'
				stream.pipe(JSONStream.parse('error'))
				.pipe es.mapSync (error) ->
					deferred.reject(error)
			else
				stream.pipe es.wait (error, text) -> deferred.reject(text)

			stream.on 'end', -> deferred.resolve()

			return deferred.promise
	.then ->
		console.log("Creating container:", app.imageId)
		ports = {}
		# Parse the env vars before trying to access them, that's because they have to be stringified for knex..
		env = JSON.parse(app.env)
		if env.PORT?
			maybePort = parseInt(env.PORT, 10)
			if parseFloat(env.PORT) is maybePort and maybePort > 0 and maybePort < 65535
				port = maybePort
		if port?
			ports[port + '/tcp'] = {}<commit_msg>Switch to using the Promise constructor rather than deferred, it allows for slightly nicer code.  Also add some parens in appropriate places.<commit_after>_ = require 'lodash'
es = require 'event-stream'
url = require 'url'
knex = require './db'
path = require 'path'
Docker = require 'dockerode'
Promise = require 'bluebird'
JSONStream = require 'JSONStream'
PlatformAPI = require 'resin-platform-api/request'

DOCKER_SOCKET = '/run/docker.sock'
PLATFORM_ENDPOINT = url.resolve(process.env.API_ENDPOINT, '/ewa/')
resinAPI = new PlatformAPI(PLATFORM_ENDPOINT)

docker = Promise.promisifyAll(new Docker(socketPath: DOCKER_SOCKET))
# Hack dockerode to promisify internal classes' prototypes
Promise.promisifyAll(docker.getImage().__proto__)
Promise.promisifyAll(docker.getContainer().__proto__)

exports.kill = kill = (app) ->
	docker.listContainersAsync(all: 1)
	.then (containers) ->
		Promise.all(
			containers
			.filter (container) -> container.Image is "#{app.imageId}: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.imageId).inspectAsync()
	.catch (error) ->
		console.log("Pulling image:", app.imageId)
		docker.createImageAsync(fromImage: app.imageId)
		.then (stream) ->
			return new Promise (resolve, reject) ->
				if stream.headers['content-type'] is 'application/json'
					stream.pipe(JSONStream.parse('error'))
					.pipe(es.mapSync(reject))
				else
					stream.pipe(es.wait((error, text) -> reject(text))

				stream.on('end', resolve)
	.then ->
		console.log("Creating container:", app.imageId)
		ports = {}
		# Parse the env vars before trying to access them, that's because they have to be stringified for knex..
		env = JSON.parse(app.env)
		if env.PORT?
			maybePort = parseInt(env.PORT, 10)
			if parseFloat(env.PORT) is maybePort and maybePort > 0 and maybePort < 65535
				port = maybePort
		if port?
			ports[port + '/tcp'] = {}
{'fraction_non_alphanumeric': 0.13380453326834024, 'fraction_numerical': 0.004387033877650499, 'mean_word_length': 4.181818181818182, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3673801', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1303, 'n_words': 328}