<commit_before>import { _, t, config, path, async, Q, fs, utils } from 'azk';
import { Image } from 'azk/images';
import { net } from 'azk/utils';
import { Run } from 'azk/system/run';
import { Scale } from 'azk/system/scale';
import { Balancer } from 'azk/system/balancer';

var XRegExp = require('xregexp').XRegExp;
var regex_port = new XRegExp(
  "(?<private>[0-9]{1,})(:(?<public>[0-9]{1,})){0,1}(/(?<protocol>tcp|udp)){0,1}", "x"
);

export class System {
  constructor(manifest, name, image, options = {}) {
    this.manifest  = manifest;
    this.name      = name;
    this.image     = new Image(image);

    // Options
    this.__options = {}
    this.options   = _.merge({}, this.default_options, options);
    this.options   = this._expand_template(this.options);
  }

  set options(values) {
    this.__options = values;
  }

  get options() {
    return this.__options;
  }

  get default_options() {
    return {
      shell    : "/bin/sh",
      depends  : [],
      envs     : {},
      scalable : false,
    }
  }

  // System run operations
  runShell(...args) { return Run.runShell(this, ...args); }
  runDaemon(...args) { return Run.runDaemon(this, ...args); }
  runProvision(...args) { return Run.runProvision(this, ...args); }
  stop(...args) { return Run.stop(this, ...args); }
  instances(...args) { return Run.instances(this, ...args); }
  throwRunError(...args) { return Run.throwRunError(this, ...args); }

  // Scale operations
  start(...args) { return Scale.start(this, ...args); }
  scale(...args) { return Scale.scale(this, ...args); }
  killAll(...args) { return Scale.killAll(this, ...args); }
  checkDependsAndReturnEnvs(...args) { return Scale.checkDependsAndReturnEnvs(this, ...args); }

  // Save provision info
  get provision_steps() {
    var steps = this.options.provision || [];
    if (!_.isArray(steps)) steps = [];
    return steps;
  }

  get provisioned() {
    var key  = this.name + ":provisioned";
    var date = this.manifest.getMeta(key);
    return date ? new Date(date) : null;
  }

  set provisioned(value) {
    var key  = this.name + ":provisioned";
    return this.manifest.setMeta(key, value);
  }

  // Options with default
  get raw_command() { return this.options.command; }
  get command() {
    var command = this.options.command;
    if (_.isEmpty(command)) {
      var msg = t("system.cmd_not_set", {system: this.name});
      command = [this.shell, "-c", `echo "${msg}"; exit 1`];
    } else {
      command = [this.shell, "-c", command];
    }

    return command;
  }

  get workdir() {
    return this.options.workdir || "/";
  }

  // Get options
  get shell()             { return this.options.shell };
  get namespace() {
    return this.manifest.namespace + '-sys.' + this.name;
  }

  // Scale options
  get scalable() {
    var _scalable = this.options.scalable;

    if(_.isNumber(_scalable)) {
      _scalable = { default: _scalable };
    } else if (!_.isObject(_scalable)) {
      _scalable = _scalable ? { } : { limit: 1 };
    }

    return _.defaults(_scalable, {
      default: 1, limit: -1
    });
  }

  get disabled() {
    return this.scalable.default == 0 && this.scalable.limit == 0;
  }

  get wait_scale() {
    var wait = this.options.wait;
    return _.isEmpty(wait) && wait != false ? true : wait;
  }

  // Ports and host
  get http()  { return this.options.http || {} };
  get hosts() {
    var hostnames = this.http.domains || [config('agent:balancer:host')];

    // v0.5.1 support
    if (!_.isEmpty(this.http.hostname)) {
      hostnames = [this.http.hostname];
    }

    return hostnames;
  }

  get hostname() {
    return this.hosts[0];
  }

  get balanceable() {
    var ports = this.ports;
    return ports.http && !_.isEmpty(this.http);
  }

  get url() {
    var host = this.hostname;
    var port = parseInt(config('agent:balancer:port'));
    return `http://${host}${ port == 80 ? '' : ':' + port }`;
  }

  backends()  { return Balancer.list(this); }

  get http_port() {
    var ports = this._parse_ports(this.ports);
    return ports.http.private;
  }

  portName(sought) {
    var ports = this._parse_ports(this.ports);
    var name  = sought;<commit_msg>Making all hostnames lowercase #182<commit_after>import { _, t, config, path, async, Q, fs, utils } from 'azk';
import { Image } from 'azk/images';
import { net } from 'azk/utils';
import { Run } from 'azk/system/run';
import { Scale } from 'azk/system/scale';
import { Balancer } from 'azk/system/balancer';

var XRegExp = require('xregexp').XRegExp;
var regex_port = new XRegExp(
  "(?<private>[0-9]{1,})(:(?<public>[0-9]{1,})){0,1}(/(?<protocol>tcp|udp)){0,1}", "x"
);

export class System {
  constructor(manifest, name, image, options = {}) {
    this.manifest  = manifest;
    this.name      = name;
    this.image     = new Image(image);

    // Options
    this.__options = {}
    this.options   = _.merge({}, this.default_options, options);
    this.options   = this._expand_template(this.options);
  }

  set options(values) {
    this.__options = values;
  }

  get options() {
    return this.__options;
  }

  get default_options() {
    return {
      shell    : "/bin/sh",
      depends  : [],
      envs     : {},
      scalable : false,
    }
  }

  // System run operations
  runShell(...args) { return Run.runShell(this, ...args); }
  runDaemon(...args) { return Run.runDaemon(this, ...args); }
  runProvision(...args) { return Run.runProvision(this, ...args); }
  stop(...args) { return Run.stop(this, ...args); }
  instances(...args) { return Run.instances(this, ...args); }
  throwRunError(...args) { return Run.throwRunError(this, ...args); }

  // Scale operations
  start(...args) { return Scale.start(this, ...args); }
  scale(...args) { return Scale.scale(this, ...args); }
  killAll(...args) { return Scale.killAll(this, ...args); }
  checkDependsAndReturnEnvs(...args) { return Scale.checkDependsAndReturnEnvs(this, ...args); }

  // Save provision info
  get provision_steps() {
    var steps = this.options.provision || [];
    if (!_.isArray(steps)) steps = [];
    return steps;
  }

  get provisioned() {
    var key  = this.name + ":provisioned";
    var date = this.manifest.getMeta(key);
    return date ? new Date(date) : null;
  }

  set provisioned(value) {
    var key  = this.name + ":provisioned";
    return this.manifest.setMeta(key, value);
  }

  // Options with default
  get raw_command() { return this.options.command; }
  get command() {
    var command = this.options.command;
    if (_.isEmpty(command)) {
      var msg = t("system.cmd_not_set", {system: this.name});
      command = [this.shell, "-c", `echo "${msg}"; exit 1`];
    } else {
      command = [this.shell, "-c", command];
    }

    return command;
  }

  get workdir() {
    return this.options.workdir || "/";
  }

  // Get options
  get shell()             { return this.options.shell };
  get namespace() {
    return this.manifest.namespace + '-sys.' + this.name;
  }

  // Scale options
  get scalable() {
    var _scalable = this.options.scalable;

    if(_.isNumber(_scalable)) {
      _scalable = { default: _scalable };
    } else if (!_.isObject(_scalable)) {
      _scalable = _scalable ? { } : { limit: 1 };
    }

    return _.defaults(_scalable, {
      default: 1, limit: -1
    });
  }

  get disabled() {
    return this.scalable.default == 0 && this.scalable.limit == 0;
  }

  get wait_scale() {
    var wait = this.options.wait;
    return _.isEmpty(wait) && wait != false ? true : wait;
  }

  // Ports and host
  get http()  { return this.options.http || {} };
  get hosts() {
    var hostnames = this.http.domains || [config('agent:balancer:host')];

    // v0.5.1 support
    if (!_.isEmpty(this.http.hostname)) {
      hostnames = [this.http.hostname];
    }

    hostnames = _.map(hostnames, function(hostname) {
      return hostname.toLowerCase();
    });

    return hostnames;
  }

  get hostname() {
    return this.hosts[0];
  }

  get balanceable() {
    var ports = this.ports;
    return ports.http && !_.isEmpty(this.http);
  }

  get url() {
    var host = this.hostname;
    var port = parseInt(config('agent:balancer:port'));
    return `http://${host}${ port == 80 ? '' : ':' + port }`;
  }

  backends()  { return Balancer.list(this); }

  get http_port() {
    var ports = this._parse_ports(this.ports);
    return ports.http.private;
  }

  portName(sought) {
    var ports = this._parse_ports(this.ports);
    var name  = sought;
{'fraction_non_alphanumeric': 0.17549042779484755, 'fraction_numerical': 0.005554242495863862, 'mean_word_length': 2.708588957055215, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, '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': '876533', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2838, 'n_words': 634}