<commit_before>#!/usr/bin/env node

var _ = require('underscore');
_.mixin(require('underscore.string').exports());

var fs = require('fs');
var cp = require('child_process');

var coreos_image_ids = {
  'stable': '2b171e93f07c4903bcad35bda10acf22__CoreOS-Stable-522.6.0',
};

var AzureCli = require('azure-cli');
var cli = new AzureCli();

process.argv.shift(2);

var weave_salt = function make_weave_salt () {
  var crypto = require('crypto');
  var shasum = crypto.createHash('sha256');
  shasum.update(crypto.randomBytes(256));
  return shasum.digest('hex');
}();

var hostname = function (n) {
  return _.template("<%= pre %>-<%= seq %>")({
    pre: 'core',
    seq: _.pad(n, 2, '0'),
  });
};

var write_cloud_config_data = function (env_files) {
  try {
    var yaml = require('js-yaml');
    var cloud_config = yaml.safeLoad(fs.readFileSync('./weave-cluster.yml', 'utf8'));
    cloud_config.write_files = env_files;
    fs.writeFileSync('./cloud-config.yml', [
      '#cloud-config',
      yaml.safeDump(cloud_config),
    ].join("\n"));
  } catch (e) {
    console.log(e);
  }
};

var env_file_template = {
  permissions: '0644',
  owner: 'root',
  content: _.template([
    'WEAVE_PEERS="<%= peers %>"',
    'WEAVEDNS_ADDR="<%= dns_addr_base %>.<%= dns_addr_node %>/<%= dns_addr_cidr %>"',
    'WEAVE_PASSWORD="<%= <PASSWORD> %>"',
  ].join("\n")),
  path: _.template("/etc/weave.<%= name %>.env"),
};

var make_node_config = function (n) {
  var weave_env = {
    name: hostname(n),
    dns_addr_cidr: 24,
    dns_addr_node: 10+n,
    dns_addr_base: '10.10.1',
    salt: weave_salt,
  };

  elected_node = 0;
  if (n === elected_node) {
    weave_env.peers = "";
  } else {
    weave_env.peers = hostname(elected_node);
  }

  var env_file = _.clone(env_file_template);
  env_file.path = env_file.path(weave_env);
  env_file.content = env_file.content(weave_env);
  
  return env_file;
};

var node_count = 3;

write_cloud_config_data(_(node_count).times(make_node_config));


var vm_name_arg = _.template("--vm-name=<%= name %>")
var vm_ssh_port = _.template("--ssh=<%= port %>")


var tasks = {
  todo: _(node_count).times(function (n) {
    return ['vm', 'create'].concat([
        '--custom-data=./cloud-config.yml',
        '--no-ssh-password',
        '--ssh-cert=../azure-linux/coreos/cluster/ssh-cert.pem',
        '--connect=weave-testing-1', '--location=West Europe',
        coreos_image_ids['stable'], 'core',
        vm_name_arg({ name: hostname(n) }),
        vm_ssh_port({ port: 2200 + n }),
      ]);
  }),
  done: [],
};

var pop_task = function() {
  var ret = {};
  ret.current = tasks.todo.shift();
  ret.remaining = tasks.todo.length;
  console.log(tasks);
  return ret;
};

(function iter (task) {
  if (task.current === undefined) {
    return;
  } else {
    cp.fork('node_modules/azure-cli/bin/azure', task.current)
      .on('exit', function (code, signal) {
        tasks.done.push({
          code: code,
          signal: signal,
          what: task.current.join(' '),
          remaining: task.remaining,
        });
        iter(pop_task());
    });
  }
})(pop_task());
<commit_msg>Fix one leaking var, more cleanup<commit_after>#!/usr/bin/env node

var _ = require('underscore');
_.mixin(require('underscore.string').exports());

var fs = require('fs');
var cp = require('child_process');

var coreos_image_ids = {
  'stable': '2b171e93f07c4903bcad35bda10acf22__CoreOS-Stable-522.6.0',
};

var AzureCli = require('azure-cli');
var cli = new AzureCli();

var weave_salt = function make_weave_salt () {
  var crypto = require('crypto');
  var shasum = crypto.createHash('sha256');
  shasum.update(crypto.randomBytes(256));
  return shasum.digest('hex');
}();

var hostname = function (n) {
  return _.template("<%= pre %>-<%= seq %>")({
    pre: 'core',
    seq: _.pad(n, 2, '0'),
  });
};

var write_cloud_config_data = function (env_files) {
  try {
    var yaml = require('js-yaml');
    var cloud_config = yaml.safeLoad(fs.readFileSync('./weave-cluster.yml', 'utf8'));
    cloud_config.write_files = env_files;
    fs.writeFileSync('./cloud-config.yml', [
      '#cloud-config',
      yaml.safeDump(cloud_config),
    ].join("\n"));
  } catch (e) {
    console.log(e);
  }
};

var env_file_template = {
  permissions: '0644',
  owner: 'root',
  content: _.template([
    'WEAVE_PEERS="<%= peers %>"',
    'WEAVEDNS_ADDR="<%= dns_addr_base %>.<%= dns_addr_node %>/<%= dns_addr_cidr %>"',
    'WEAVE_PASSWORD="<%= <PASSWORD> %>"',
  ].join("\n")),
  path: _.template("/etc/weave.<%= name %>.env"),
};

var make_node_config = function (n) {
  var weave_env = {
    name: hostname(n),
    dns_addr_cidr: 24,
    dns_addr_node: 10+n,
    dns_addr_base: '10.10.1',
    salt: weave_salt,
  };

  var elected_node = 0;
  if (n === elected_node) {
    weave_env.peers = "";
  } else {
    weave_env.peers = hostname(elected_node);
  }

  var env_file = _.clone(env_file_template);
  env_file.path = env_file.path(weave_env);
  env_file.content = env_file.content(weave_env);
  
  return env_file;
};

var node_count = 3;

write_cloud_config_data(_(node_count).times(make_node_config));

var vm_name_arg = _.template("--vm-name=<%= name %>")
var vm_ssh_port = _.template("--ssh=<%= port %>")

var tasks = {
  todo: _(node_count).times(function (n) {
    return ['vm', 'create'].concat([
        '--custom-data=./cloud-config.yml',
        '--no-ssh-password',
        '--ssh-cert=../azure-linux/coreos/cluster/ssh-cert.pem',
        '--connect=weave-testing-1', '--location=West Europe',
        coreos_image_ids['stable'], 'core',
        vm_name_arg({ name: hostname(n) }),
        vm_ssh_port({ port: 2200 + n }),
      ]);
  }),
  done: [],
};

var pop_task = function() {
  var ret = {};
  ret.current = tasks.todo.shift();
  ret.remaining = tasks.todo.length;
  console.log(tasks);
  return ret;
};

(function iter (task) {
  if (task.current === undefined) {
    return;
  } else {
    cp.fork('node_modules/azure-cli/bin/azure', task.current)
      .on('exit', function (code, signal) {
        tasks.done.push({
          code: code,
          signal: signal,
          what: task.current.join(' '),
          remaining: task.remaining,
        });
        iter(pop_task());
    });
  }
})(pop_task());

{'fraction_non_alphanumeric': 0.1939820742637644, 'fraction_numerical': 0.01680537772087068, 'mean_word_length': 3.265529010238908, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '99544', 'n_tokens_mistral': 2465, 'n_tokens_neox': 2343, 'n_words': 403}