<commit_before>var express = require('express');
var bodyParser = require('body-parser');
var spawn = require('child_process').spawn;
var request = require('request');

var dropletId = process.env.DROPLET_ID;
var dropletDomain = process.env.DROPLET_DOMAIN;
var sshIdentity = process.env.SSH_IDENTITY;
var sshUsername = process.env.SSH_USERNAME;
var preResetCommand = process.env.PRE_RESET_COMMAND;
var preResetCommandTimeout = process.env.PRE_RESET_COMMAND_TIMEOUT;

var defaultPreResetCommand =
  'echo "The system is going down for reset NOW!" | wall';

var sshCommand = [
  'temp=`mktemp`',
  'printf "%s" "$1" >> "$temp"',
  'timeout $5 ' +
    'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' +
      '-i "$temp" "$2@$3" -- "$4"',
  'rm "$temp"'].join('\n');

var api_endpoint = "https://api.digitalocean.com/v2/droplets/" +
  dropletId + "/actions";

var app = express();

app.set('dropletDomain',dropletDomain);

app.use(express.static(__dirname + '/static'));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));

app.get('/', function(req, res) {
  res.render('index.jade');
});

function sendCommand(req, res, next) {
  var sshProc = spawn('bash', ['-c', sshCommand, 'sshCommand',
    sshIdentity+'\n',
    sshUsername || 'root',
    dropletDomain,
    preResetCommand || defaultPreResetCommand,
    preResetCommandTimeout || 5]);

  sshProc.stdout.on('data', function (data) {
    console.log('ssh stdout: ' + data);
  });
  sshProc.stderr.on('data', function (data) {
    console.log('ssh stderr: ' + data);
  });
  sshProc.on('close', function (code) {
    next();
  });
}

function sendReset(req, res, next) {
  request.post(api_endpoint,{
    body: {type: 'restore', image: process.env.RESTORE_IMAGE_ID},
    auth: {bearer: process.env.DIGITALOCEAN_API_TOKEN}, json: true},
    function(err, response, body) {
      if (err) return next(err);
      if (!/^2/.test(response.statusCode))
        return next(body.message ? new Error(body.message) : body);
      res.redirect('/status/' + body.action.id);
    });
}

var postStack = [sendReset];
if (sshIdentity && dropletDomain) postStack.unshift(sendCommand);
app.post('/', postStack);

app.get('/status/:action', function(req, res, next) {
  request.get(api_endpoint + '/' + req.params.action, {
    auth: {bearer: process.env.DIGITALOCEAN_API_TOKEN}, json: true},
    function(err, response, body) {
      if (err) return next(err);<commit_msg>Don't use the `timeout` command to enforce the timeout<commit_after>var express = require('express');
var bodyParser = require('body-parser');
var spawn = require('child_process').spawn;
var request = require('request');

var dropletId = process.env.DROPLET_ID;
var dropletDomain = process.env.DROPLET_DOMAIN;
var sshIdentity = process.env.SSH_IDENTITY;
var sshUsername = process.env.SSH_USERNAME;
var preResetCommand = process.env.PRE_RESET_COMMAND;
var preResetCommandTimeout = process.env.PRE_RESET_COMMAND_TIMEOUT;

var defaultPreResetCommand =
  'echo "The system is going down for reset NOW!" | wall';

var sshCommand = [
  'temp=`mktemp`',
  'printf "%s" "$1" >> "$temp"',
  'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' +
    '-i "$temp" "$2@$3" -- "$4"',
  'rm "$temp"'].join('\n');

var api_endpoint = "https://api.digitalocean.com/v2/droplets/" +
  dropletId + "/actions";

var app = express();

app.set('dropletDomain',dropletDomain);

app.use(express.static(__dirname + '/static'));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));

app.get('/', function(req, res) {
  res.render('index.jade');
});

function sendCommand(req, res, next) {
  var sshProc = spawn('bash', ['-c', sshCommand, 'sshCommand',
    sshIdentity+'\n',
    sshUsername || 'root',
    dropletDomain,
    preResetCommand || defaultPreResetCommand,
    preResetCommandTimeout || 5]);

  sshProc.stdout.on('data', function (data) {
    console.log('ssh stdout: ' + data);
  });
  sshProc.stderr.on('data', function (data) {
    console.log('ssh stderr: ' + data);
  });
  sshProc.on('close', function (code) {
    if (sshProc) {
      sshProc = null;
      next();
    }
  });

  function sshProcTimeout() {
    if (sshProc) {
      sshProc.kill();
      sshProc.disconnect();
      sshProc = null;
      next();
    }
  }

  setTimeout(sshProcTimeout,
    preResetCommandTimeout ? preResetCommandTimeout * 1000 : 5000);
}

function sendReset(req, res, next) {
  request.post(api_endpoint,{
    body: {type: 'restore', image: process.env.RESTORE_IMAGE_ID},
    auth: {bearer: process.env.DIGITALOCEAN_API_TOKEN}, json: true},
    function(err, response, body) {
      if (err) return next(err);
      if (!/^2/.test(response.statusCode))
        return next(body.message ? new Error(body.message) : body);
      res.redirect('/status/' + body.action.id);
    });
}

var postStack = [sendReset];
if (sshIdentity && dropletDomain) postStack.unshift(sendCommand);
app.post('/', postStack);

app.get('/status/:action', function(req, res, next) {
  request.get(api_endpoint + '/' + req.params.action, {
    auth: {bearer: process.env.DIGITALOCEAN_API_TOKEN}, json: true},
    function(err, response, body) {
      if (err) return next(err);
{'fraction_non_alphanumeric': 0.1707920792079208, 'fraction_numerical': 0.004379284082254379, 'mean_word_length': 4.211309523809524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '698230', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1767, 'n_words': 369}