<commit_before>'use strict';

var util = require('util'),
    EventEmitter = require('events').EventEmitter,
    Docker = require('dockerode'),
    Promise = require('bluebird'),
    uuid = require('uuid'),
    debug = require('debug')('docker-runner');


var DockerExec = function (options) {

    // linux
    options = options ||  {
        socketPath: '/var/run/docker.sock'
    };

    this.docker = new Docker(options);

    this.container = null;
};

util.inherits(DockerExec, EventEmitter);

/**
 * Creates a new docker container
 */
DockerExec.prototype.createContainer = function (options) {
    debug('create a new container');

    var docker = this.docker;
    return new Promise(function (resolve, reject) {
        docker.createContainer(options, function handler(err, container) {
            if (err) {
                reject(err);
            } else {
                resolve(container);
            }
        });
    });
};

/**
 * attaches a stream to a docker container
 */
DockerExec.prototype.attachContainer = function (container) {
    debug('attach to a container');

    var self = this;

    var attach_opts = {
        stream: true,
        stdin: true,
        stdout: true,
        stderr: true
    };

    return new Promise(function (resolve, reject) {
        container.attach(attach_opts, function handler(err, stream) {
            if (err) {
                reject(err);
            } else {

                // attach stream to stdout
                // stream.pipe(process.stdout);

                /*stream.on('data', function(data){
                    debug(data.toString('utf8'));
                });*/

                self.emit('container:attach');
                resolve(stream);
            }
        });
    });
};

/**
 * starts a docker container
 */
DockerExec.prototype.startContainer = function (container) {
    debug('start a new container');

    var self = this;

    return new Promise(function (resolve, reject) {
        container.start({}, function handler(err, data) {
            if (err) {
                reject(err);
            } else {
                self.emit('container:start');
                resolve(data);
            }
        });
    });
};

/**
 * register for container end
 */
DockerExec.prototype.waitContainer = function (container) {
    debug('register wait event for a new container');

    var self = this;
    container.wait(function handler(err) {
        if (err) {
            self.emit('error', err);
            self.emit('container:stop');
            debug(err);
        } else {
            // emit stream end event;
            self.emit('container:stop');
        }
    });

};

DockerExec.prototype.pullContainer = function (image) {
    debug('download operating system');

    var docker = this.docker;

    return new Promise(function (resolve, reject) {
        docker.pull(image, function (err, stream) {
            if(err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

DockerExec.prototype.streamEnd = function () {
    if (this.stream) {
        this.stream.end();
    }
};

/*
 * Starts a new docker container
 */
DockerExec.prototype.start = function () {
    debug('start a new container');

    var optsc = {
        'Hostname': '',
        'User': '',
        'AttachStdin': true,
        'AttachStdout': true,
        'AttachStderr': true,
        'Tty': true,
        'OpenStdin': true,
        'StdinOnce': false,
        'Env': null,
        'Cmd': ['bash'], // run a bash shell
        'Dns': ['8.8.8.8', '8.8.4.4'], // use google dns
        'Image': 'ubuntu:14.04', // TODO make os configurable
        'Volumes': {},
        'VolumesFrom': ''
    };

    var self = this;

    return new Promise(function (resolve, reject) {<commit_msg>allow more options at startup<commit_after>'use strict';

var util = require('util'),
    EventEmitter = require('events').EventEmitter,
    Docker = require('dockerode'),
    Promise = require('bluebird'),
    uuid = require('uuid'),
    debug = require('debug')('docker-runner');


var DockerExec = function (options) {

    // linux
    options = options ||  {
        socketPath: '/var/run/docker.sock'
    };

    this.docker = new Docker(options);

    this.container = null;
};

util.inherits(DockerExec, EventEmitter);

/**
 * Creates a new docker container
 */
DockerExec.prototype.createContainer = function (options) {
    debug('create a new container');

    var docker = this.docker;
    return new Promise(function (resolve, reject) {
        docker.createContainer(options, function handler(err, container) {
            if (err) {
                reject(err);
            } else {
                resolve(container);
            }
        });
    });
};

/**
 * attaches a stream to a docker container
 */
DockerExec.prototype.attachContainer = function (container) {
    debug('attach to a container');

    var self = this;

    var attach_opts = {
        stream: true,
        stdin: true,
        stdout: true,
        stderr: true
    };

    return new Promise(function (resolve, reject) {
        container.attach(attach_opts, function handler(err, stream) {
            if (err) {
                reject(err);
            } else {

                // attach stream to stdout
                // stream.pipe(process.stdout);

                /*stream.on('data', function(data){
                    debug(data.toString('utf8'));
                });*/

                self.emit('container:attach');
                resolve(stream);
            }
        });
    });
};

/**
 * starts a docker container
 */
DockerExec.prototype.startContainer = function (container) {
    debug('start a new container');

    var self = this;

    return new Promise(function (resolve, reject) {
        container.start({}, function handler(err, data) {
            if (err) {
                reject(err);
            } else {
                self.emit('container:start');
                resolve(data);
            }
        });
    });
};

/**
 * register for container end
 */
DockerExec.prototype.waitContainer = function (container) {
    debug('register wait event for a new container');

    var self = this;
    container.wait(function handler(err) {
        if (err) {
            self.emit('error', err);
            self.emit('container:stop');
            debug(err);
        } else {
            // emit stream end event;
            self.emit('container:stop');
        }
    });

};

DockerExec.prototype.pullContainer = function (image) {
    debug('download operating system');

    var docker = this.docker;

    return new Promise(function (resolve, reject) {
        docker.pull(image, function (err, stream) {
            if(err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

DockerExec.prototype.streamEnd = function () {
    if (this.stream) {
        this.stream.end();
    }
};

/*
 * Starts a new docker container
 */
DockerExec.prototype.start = function (options) {
    debug('start a new container');

    options = options || {};

    var optsc = {
        'Hostname': '',
        'User': '',
        'AttachStdin': true,
        'AttachStdout': true,
        'AttachStderr': true,
        'Tty': true,
        'OpenStdin': true,
        'StdinOnce': false,
        'Env': null,
        'Cmd': ['bash'], // run a bash shell
        'Dns': ['8.8.8.8', '8.8.4.4'], // use google dns
        'Image': options.Image || 'ubuntu:14.04', // TODO make os configurable
        'Volumes': options.Volumes || {},
        'VolumesFrom': options.VolumesFrom || ''
    };

    var self = this;

    return new Promise(function (resolve, reject) {
{'fraction_non_alphanumeric': 0.14048416173062064, 'fraction_numerical': 0.003347926860674736, 'mean_word_length': 1.8766666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2836340', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2125, 'n_words': 518}