<commit_before>'use strict';

var GCE = require('./index');
var Auth = require('./Auth');
var fs = require('fs');
var uuid = require('uuid');
var path = require('path');
var ursa = require('ursa');

var buildTypes = {
  slave: {
    instance: fs.readFileSync(path.join(__dirname, 'instances', 'slave.json'), 'utf8')
  },
  github: {
    instance: fs.readFileSync(path.join(__dirname, 'instances', 'github.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'instances', 'github_startup.sh'), 'utf8')
  },
  ui: {
    instance: fs.readFileSync(path.join(__dirname, 'instances', 'ui.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'instances', 'ui_startup.sh'), 'utf8')
  },
  'slave-snapshot': {
    instance: fs.readFileSync(path.join(__dirname, 'images', 'slave.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'images', 'slave_startup.sh'), 'utf8')
  },
  'github-snapshot': {
    instance: fs.readFileSync(path.join(__dirname, 'images', 'github.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'images', 'github_startup.sh'), 'utf8')
  }
};

function Instance(projectId, zone, instanceName, type) {
  this.projectId = projectId;
  this.instanceName = instanceName;
  this.zone = zone;
  this.gce = new GCE(projectId, zone);

  if (type) {
    this.type = type;
    this.instanceBuildInfo = JSON.parse(buildTypes[this.type].instance);
    this.diskName = this.instanceBuildInfo.disks[0].deviceName;
    this.instanceBuildInfo.name = this.instanceName;
  }
}

// TODO(trostler): only export this function
Instance.Factory = function(type, xtra) {
  switch (type) {
    case 'slave':
      return Instance.Slave(xtra);
      break;
    case 'github':
      return Instance.GithubListener();
      break;
    case 'ui':
      return Instance.UI();
      break;
    case 'slave-snapshot':
      return Instance.SlaveSnapshot();
      break;
    case 'github-snapshot':
      return Instance.GithubSnapshot();
      break;
    default:
      throw new Error('I do not know instance type ' + type);
      break;
  }
};

Instance.Slave = function(iName) {
  // some krazy random name
  var instanceName = iName || ['x', new Date().getTime(), uuid.v4()].join('-').slice(0, 63);
  var newSlave = new Instance(Auth.projectId, Auth.zone, instanceName, 'slave');
  if (!iName) {
    // Generate a new pub/priv key pair
    var keys = ursa.generatePrivateKey();
    var pubSsh = keys.toPublicSsh('base64');
    newSlave.instanceBuildInfo.metadata.items.push({
      key: 'sshKeys',
      value: [ 'sivart:ssh-rsa', pubSsh, 'sivart'].join(' ')
    });
    newSlave.privateKey = keys.toPrivatePem('base64');
  }
  return newSlave;
};

Instance.SlaveSnapshot = function() {
  return new Instance(Auth.projectId, Auth.zone, 'slave-snapshot', 'slave-snapshot');<commit_msg>set up private key correctly<commit_after>'use strict';

var GCE = require('./index');
var Auth = require('./Auth');
var fs = require('fs');
var uuid = require('uuid');
var path = require('path');
var ursa = require('ursa');

var buildTypes = {
  slave: {
    instance: fs.readFileSync(path.join(__dirname, 'instances', 'slave.json'), 'utf8')
  },
  github: {
    instance: fs.readFileSync(path.join(__dirname, 'instances', 'github.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'instances', 'github_startup.sh'), 'utf8')
  },
  ui: {
    instance: fs.readFileSync(path.join(__dirname, 'instances', 'ui.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'instances', 'ui_startup.sh'), 'utf8')
  },
  'slave-snapshot': {
    instance: fs.readFileSync(path.join(__dirname, 'images', 'slave.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'images', 'slave_startup.sh'), 'utf8')
  },
  'github-snapshot': {
    instance: fs.readFileSync(path.join(__dirname, 'images', 'github.json'), 'utf8'),
    script: fs.readFileSync(path.join(__dirname, 'images', 'github_startup.sh'), 'utf8')
  }
};

function Instance(projectId, zone, instanceName, type) {
  this.projectId = projectId;
  this.instanceName = instanceName;
  this.zone = zone;
  this.gce = new GCE(projectId, zone);

  if (type) {
    this.type = type;
    this.instanceBuildInfo = JSON.parse(buildTypes[this.type].instance);
    this.diskName = this.instanceBuildInfo.disks[0].deviceName;
    this.instanceBuildInfo.name = this.instanceName;
  }
}

// TODO(trostler): only export this function
Instance.Factory = function(type, xtra) {
  switch (type) {
    case 'slave':
      return Instance.Slave(xtra);
      break;
    case 'github':
      return Instance.GithubListener();
      break;
    case 'ui':
      return Instance.UI();
      break;
    case 'slave-snapshot':
      return Instance.SlaveSnapshot();
      break;
    case 'github-snapshot':
      return Instance.GithubSnapshot();
      break;
    default:
      throw new Error('I do not know instance type ' + type);
      break;
  }
};

Instance.Slave = function(iName) {
  // some krazy random name
  var instanceName = iName || ['x', new Date().getTime(), uuid.v4()].join('-').slice(0, 63);
  var newSlave = new Instance(Auth.projectId, Auth.zone, instanceName, 'slave');
  if (!iName) {
    // Generate a new pub/priv key pair
    var keys = ursa.generatePrivateKey();
    var pubSsh = keys.toPublicSsh('base64');
    newSlave.instanceBuildInfo.metadata.items.push({
      key: 'sshKeys',
      value: [ 'sivart:ssh-rsa', pubSsh, 'sivart'].join(' ')
    });

    // Now set up private key - up to caller to store this somewhere
    var privPem = keys.toPrivatePem();
    var priv = ursa.createPrivateKey(privPem);
    newSlave.privateKey = priv.toPrivatePem().toString();
  }
  return newSlave;
};

Instance.SlaveSnapshot = function() {
  return new Instance(Auth.projectId, Auth.zone, 'slave-snapshot', 'slave-snapshot');
{'fraction_non_alphanumeric': 0.16597938144329896, 'fraction_numerical': 0.00584192439862543, 'mean_word_length': 3.818708609271523, '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': '2580747', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1960, 'n_words': 415}