<commit_before>
/*
 * GET docker index.
 */

exports.index = function(req, res){
  res.render('index', { title: 'Docker' });
};

var resWrite = function(req, res, input){
  // res.writeHead(200, {
  //   'Content-Type': 'application/json'
  // });

  if(typeof input !== 'string'){
    input = JSON.stringify(input);
  }

  res.write(input);
  res.end();
};

//docker
//todo
//refactor to a different file
var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
var pexec = require('../docker_util/pexec').pexec;

var createContainer = function(){
  docker.run('2947c9301082', [], process.stdout, function(err, data, container) {
    console.log(data.StatusCode);
  });
};

var listContainers = function(){
  docker.listContainers(function(err, containers) {
    console.log('list', err, containers);
  });
};

var stopAll = function(cb){
  docker.listContainers(function(err, containers) {
    // console.log('list', err, containers);
    containers.forEach(function(containerInfo) {
      docker.getContainer(containerInfo.Id).stop(cb);
    });
  });
};


var runContainer = function(externalPort){
  var cmd = 'docker run -d -p '+ externalPort  +':3131 howtox/c9_ng'
  pexec(cmd);
};

var getCommand = function(gitTag){
    var maxPort = Math.pow(2,16); //http://stackoverflow.com/questions/113224/what-is-the-largest-tcp-ip-network-port-number-allowable-for-ipv4
    var minPort = 1024;
    var randomPort = Math.floor(Math.random() * (maxPort - minPort + 1)) + minPort;

    var command = 'docker run -d '+ 
            ' -p ' + randomPort +':3131'+ 
            ' -p ' + (randomPort+1) +':8000 '+ 
            ' -e TAG=' + gitTag +
            ' 66cfb37f9cb4';
    return {
        command: command,
        port: randomPort
    };
};

exports.create = function(req, res){
  var gitTag = req.body && req.body.tag;
  var commandObj;
  switch(gitTag){
    case 'step-0':
        console.log('0');
        commandObj = getCommand(gitTag);
        pexec(commandObj.command).then(function(){
            resWrite(req, res, {port:commandObj.port});
        });
        break;
    case 'step-1':
        console.log('1');
        command = getCommand(gitTag);
        pexec(command).then(function(){
            resWrite(req, res, {port:randomPort});
        });
        break;        
    case '2':
        console.log('2');
        command = getCommand(gitTag);
        pexec(command).then(function(){
            resWrite(req, res, {port:randomPort});
        });
        break;        
    case '3':
        console.log('3');
        command = getCommand(gitTag);
        pexec(command).then(function(){
            resWrite(req, res, {port:randomPort});
        });
        break;      
    case '99':
        console.log('99');
        stopAll(function(){
            console.log('STOP ALL');
        });
        break;
    default:
        console.log('default');
        break;
  }
//   runContainer(9877);

//   resWrite(req, res, {msg:200});
};

exports.stop = function(req, res){
  stopAll(function(){<commit_msg>Fix typo<commit_after>
/*
 * GET docker index.
 */

exports.index = function(req, res){
  res.render('index', { title: 'Docker' });
};

var resWrite = function(req, res, input){
  // res.writeHead(200, {
  //   'Content-Type': 'application/json'
  // });

  if(typeof input !== 'string'){
    input = JSON.stringify(input);
  }

  res.write(input);
  res.end();
};

//docker
//todo
//refactor to a different file
var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
var pexec = require('../docker_util/pexec').pexec;

var createContainer = function(){
  docker.run('2947c9301082', [], process.stdout, function(err, data, container) {
    console.log(data.StatusCode);
  });
};

var listContainers = function(){
  docker.listContainers(function(err, containers) {
    console.log('list', err, containers);
  });
};

var stopAll = function(cb){
  docker.listContainers(function(err, containers) {
    // console.log('list', err, containers);
    containers.forEach(function(containerInfo) {
      docker.getContainer(containerInfo.Id).stop(cb);
    });
  });
};


var runContainer = function(externalPort){
  var cmd = 'docker run -d -p '+ externalPort  +':3131 howtox/c9_ng'
  pexec(cmd);
};

var getCommand = function(gitTag){
    var maxPort = Math.pow(2,16); //http://stackoverflow.com/questions/113224/what-is-the-largest-tcp-ip-network-port-number-allowable-for-ipv4
    var minPort = 1024;
    var randomPort = Math.floor(Math.random() * (maxPort - minPort + 1)) + minPort;

    var command = 'docker run -d '+ 
            ' -p ' + randomPort +':3131'+ 
            ' -p ' + (randomPort+1) +':8000 '+ 
            ' -e TAG=' + gitTag +
            ' 66cfb37f9cb4';
    return {
        command: command,
        port: randomPort
    };
};

exports.create = function(req, res){
  var gitTag = req.body && req.body.tag;
  var commandObj;
  switch(gitTag){
    case 'step-0':
        console.log('0');
        commandObj = getCommand(gitTag);
        pexec(commandObj.command).then(function(){
            resWrite(req, res, {port:commandObj.port});
        });
        break;
    case 'step-1':
        console.log('1');
        commandObj = getCommand(gitTag);
        pexec(commandObj.command).then(function(){
            resWrite(req, res, {port:commandObj.port});
        });
        break;        
    case 'step-2':
        console.log('2');
        commandObj = getCommand(gitTag);
        pexec(commandObj.command).then(function(){
            resWrite(req, res, {port:commandObj.port});
        });
        break;        
    case 'step-3':
        console.log('3');
        commandObj = getCommand(gitTag);
        pexec(commandObj.command).then(function(){
            resWrite(req, res, {port:commandObj.port});
        });
        break;      
    case '99':
        console.log('99');
        stopAll(function(){
            console.log('STOP ALL');
        });
        break;
    default:
        console.log('default');
        break;
  }
//   runContainer(9877);

//   resWrite(req, res, {msg:200});
};

exports.stop = function(req, res){
  stopAll(function(){
{'fraction_non_alphanumeric': 0.1708656330749354, 'fraction_numerical': 0.021963824289405683, 'mean_word_length': 2.5429061784897025, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2528126', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1986, 'n_words': 376}