UNPKG

gce-elastic-docker

Version:

A package to help setup Elasticsearch / Kibana clusters on Google Compute Engine.

251 lines (250 loc) 9.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var gce_1 = require("../gce"); var image_1 = require("../image"); var utils_1 = require("../utils"); var ged_label = 'ged'; exports.ged_label = ged_label; var BaseNode = /** @class */ (function () { function BaseNode(v) { this._set_cluster_name(v); this._set_data(v); this._set_dsize(v); this._set_dtype(v); this.env = {}; this.set_env(v.env); this.set_hsize(v.hsize); this.set_khsize(v.khsize); this._set_image(v); this._set_ingest(v); this._set_kibana(v); this._set_labels(v.labels); this._set_master(v); this._set_max_map_count(v); this._set_zone(v); // must set zone b4 mtype. this._set_mtype(v); this._set_name(v); this._set_service_account(v); this.region = this.zone.slice(0, -2); this.short_region = gce_1.short_regions[this.region]; } // returns the keys of all env vars to remove aka that are null. BaseNode.prototype.get_env_to_remove = function () { var _this = this; return Object.keys(this.env).filter(function (e) { return _this.env[e] === null; }); }; // returns all creatable or updatable env keys/values BaseNode.prototype.get_merged_env = function () { var copy = JSON.parse(JSON.stringify(this)); // remove keys set to null from the copies env. for (var k in copy.env) { if (copy.env[k] === null) { delete copy.env[k]; } } var base = { ES_JAVA_OPTS: "-Xms" + this.hsize + "m -Xmx" + this.hsize + "m", NODE_OPTIONS: "--max-old-space-size=" + this.khsize, 'bootstrap.memory_lock': true, 'cluster.name': this.cluster_name, ged: Buffer.from(JSON.stringify(copy)).toString('base64'), kibana_users: undefined, 'network.host': '0.0.0.0', 'node.data': this.data, 'node.ingest': this.ingest, 'node.master': this.master, 'node.name': this.name }; delete base[image_1.kibana_users_env_var]; Object.keys(copy.env).forEach(function (k) { return base[k] = "" + copy.env[k]; }); return base; }; BaseNode.prototype.get_merged_labels = function () { var _this = this; var base = { ged: 'true' }; Object.keys(this.labels).forEach(function (k) { return base[k] = _this.labels[k]; }); return base; }; BaseNode.prototype.set_env = function (v) { var _this = this; var reserved = { ES_JAVA_OPTS: true, NODE_OPTIONS: true, 'bootstrap.memory_lock': true, 'cluster.name': true, ged: true, kibana_users: true, 'network.host': true, 'node.data': true, 'node.ingest': true, 'node.master': true, 'node.name': true }; if (v) { Object.keys(v).map(function (k) { if (!utils_1.Utils.is_string(v[k]) && !utils_1.Utils.is_null(v[k]) && !utils_1.Utils.is_number(v[k]) && !utils_1.Utils.is_bool(v[k])) { throw Error('an environment value must be a string|null|number|bool.'); } else if (reserved[k]) { throw Error(k + " is a reserved env variable this package sets."); } else { _this.env[k] = v[k]; } }); } }; BaseNode.prototype.set_hsize = function (v) { if (!utils_1.Utils.is_integer(v) || (v < 100) || (v > 31000)) { throw Error("es heap size of " + v + " must be an integer from [100, 31000]"); } this.hsize = v; }; BaseNode.prototype.set_khsize = function (v) { var val = v; if (utils_1.Utils.is_defined(val)) { if (!utils_1.Utils.is_integer(val)) { throw Error('kibana heap size not an integer'); } else if (val < 100) { throw Error('kibana heap size too small.'); } } this.khsize = val ? val : 512; }; BaseNode.prototype._set_cluster_name = function (v) { if (!utils_1.Utils.is_string(v.cluster_name) || !v.cluster_name || / /.test(v.cluster_name)) { throw Error(v.cluster_name + " is not a valid cluster name"); } this.cluster_name = v.cluster_name; }; BaseNode.prototype._set_data = function (v) { if (utils_1.Utils.is_bool(v.data)) { this.data = v.data; } else if (utils_1.Utils.is_defined(v.data)) { throw Error('not a boolean'); } else { this.data = true; } }; BaseNode.prototype._set_dsize = function (v) { if (!utils_1.Utils.is_integer(v.dsize) || (v.dsize < 10) || (v.dsize > 6400)) { throw Error("disk size of " + v.dsize + " must be an integer from [10, 6400]"); } this.dsize = v.dsize; }; BaseNode.prototype._set_dtype = function (v) { if ((v.dtype !== 'pd-ssd') && v.dtype !== ('pd-standard')) { throw Error('disk type must be pd-ssd|pd-standard'); } this.dtype = v.dtype; }; BaseNode.prototype._set_image = function (v) { if (!utils_1.Utils.is_valid_image_name(v.image)) { throw Error(v.image + " is an invalid image name. Make sure it looks like: " + ("{" + gce_1.registries.join(' | ') + "}/{gcloud-project-id}/{image_name}")); } this.image = v.image; }; BaseNode.prototype._set_ingest = function (v) { if (utils_1.Utils.is_bool(v.ingest)) { this.ingest = v.ingest; } else if (utils_1.Utils.is_defined(v.ingest)) { throw Error('not a boolean'); } else { this.ingest = false; } }; BaseNode.prototype._set_kibana = function (v) { if (utils_1.Utils.is_bool(v.kibana)) { this.kibana = v.kibana; } else if (utils_1.Utils.is_defined(v.kibana)) { throw Error('not a boolean'); } else { this.kibana = false; } }; BaseNode.prototype._set_labels = function (v) { var _this = this; var reserved = { ged: true }; this.labels = {}; if (v) { Object.keys(v).map(function (k) { if (!utils_1.Utils.is_string(v[k]) || !v[k]) { throw Error('a labels value must be a nonempty string.'); } else if (!/^[a-z]/.test(k)) { throw Error('a label must start with lowercase letter.'); } else if (!/^[a-z0-9_-]*$/.test(k)) { throw Error('a label can contain only a-z0-9_-'); } else if (!/^[a-z0-9_-]*$/.test(v[k])) { throw Error('a labels value can contain only a-z0-9_-'); } else if (reserved[k]) { throw Error(k + " is a reserved label this package sets."); } else { _this.labels[k] = v[k]; } }); } }; BaseNode.prototype._set_master = function (v) { if (utils_1.Utils.is_bool(v.master)) { this.master = v.master; } else if (utils_1.Utils.is_defined(v.master)) { throw Error('not a boolean'); } else { this.master = true; } }; BaseNode.prototype._set_max_map_count = function (v) { if (utils_1.Utils.is_defined(v.max_map_count) && (!utils_1.Utils.is_integer(v.max_map_count) || (v.max_map_count <= 0))) { throw Error('max map count must be an integer > 0'); } this.max_map_count = v.max_map_count ? v.max_map_count : 262144; }; BaseNode.prototype._set_mtype = function (v) { if (gce_1.m_types[this.zone].indexOf(v.mtype) === -1) { throw Error("mtype of " + v.mtype + " is an invalid gce machine " + ("type for zone " + this.zone)); } this.mtype = v.mtype; }; BaseNode.prototype._set_name = function (v) { if (!utils_1.Utils.is_string(v.name) || !v.name || / /.test(v.name)) { throw Error(v.name + " is not a valid name."); } this.name = v.name; }; BaseNode.prototype._set_service_account = function (v) { if (!utils_1.Utils.is_string(v.service_account) || !/compute@developer/.test(v.service_account)) { throw Error('must provide a valid default gce service account.'); } this.service_account = v.service_account; }; BaseNode.prototype._set_zone = function (v) { if (gce_1.zones.indexOf(v.zone) === -1) { throw Error(v + " is an invalid gce zone."); } this.zone = v.zone; }; return BaseNode; }()); exports.BaseNode = BaseNode;