UNPKG

gce-elastic-docker

Version:

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

395 lines (394 loc) 19.1 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var js_yaml_1 = require("js-yaml"); var base_node_1 = require("../base-node"); var node_update_opts_1 = require("../node-update-opts"); var node_updater_1 = require("../node-updater"); var utils_1 = require("../utils"); var Node = /** @class */ (function (_super) { __extends(Node, _super); function Node(v) { var _this = _super.call(this, v) || this; _this._set_created(v); _this._set_ip(v); return _this; } Node.fetch_all = function (verbose) { return __awaiter(this, void 0, void 0, function () { var cmd, vms, _a, _b, ret; return __generator(this, function (_c) { switch (_c.label) { case 0: cmd = 'gcloud compute instances list --format=json ' + ("--filter=\"labels:" + base_node_1.ged_label + "\""); if (verbose) { console.log("fetching all nodes this package has created via\n" + cmd); } _b = (_a = JSON).parse; return [4 /*yield*/, utils_1.Utils.exec(cmd, verbose)]; case 1: vms = _b.apply(_a, [_c.sent()]); ret = []; vms.forEach(function (vm) { // get the internal ip/create time var ip = vm.networkInterfaces[0].networkIP; var created = (new Date(vm.creationTimestamp)).valueOf(); vm.metadata.items.forEach(function (m) { if (m.key === 'gce-container-declaration') { var container_decl = js_yaml_1.safeLoad(m.value); var envs = container_decl.spec.containers[0].env; var envb64 = envs.filter(function (e) { return e.name === base_node_1.ged_label; })[0].value; var env = Buffer.from(envb64, 'base64').toString(); var tmp_node = JSON.parse(env); tmp_node.ip = ip; tmp_node.created = created; // will throw if internal ip/created are not yet set so disregard // those nodes. this will occur only when they are being created. try { ret.push(new Node(tmp_node)); } catch (e) { } } }); }); return [2 /*return*/, ret]; } }); }); }; // returns the standard elasticsearch json response for GET _cluster/health Node.prototype.cluster_health = function (verbose) { return __awaiter(this, void 0, void 0, function () { var cmd, res, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (verbose) { console.log("fetching cluster health for " + this.name); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); cmd = 'curl -s localhost:9200/_cluster/health'; return [4 /*yield*/, this.exec(cmd)]; case 2: res = _a.sent(); return [2 /*return*/, JSON.parse(res)]; case 3: e_1 = _a.sent(); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; // returns green | yellow | red | undefined Node.prototype.cluster_state = function (verbose) { return __awaiter(this, void 0, void 0, function () { var e_2; return __generator(this, function (_a) { switch (_a.label) { case 0: if (verbose) { console.log("fetching cluster state for " + this.name); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.cluster_health()]; case 2: return [2 /*return*/, (_a.sent()).status]; case 3: e_2 = _a.sent(); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; Node.prototype.delete = function (verbose) { return __awaiter(this, void 0, void 0, function () { var cmd; return __generator(this, function (_a) { switch (_a.label) { case 0: cmd = "printf \"y\n\" | gcloud compute instances delete " + this.name + " --zone " + this.zone; if (verbose) { console.log("deleting instance " + this.name + " via " + cmd); } return [4 /*yield*/, utils_1.Utils.exec(cmd, verbose)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; // executes the given command in the container and returns its stdout. // note that the cmd is base64 encoded so we dont have to worry about special // characters like $ or quotes. Node.prototype.exec = function (cmd, verbose) { return __awaiter(this, void 0, void 0, function () { var b64, wrapped_cmd; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!utils_1.Utils.is_string(cmd) || !cmd) { throw Error('command missing!'); } b64 = Buffer.from(cmd).toString('base64'); wrapped_cmd = "gcloud compute ssh " + this.name + " --zone " + this.zone + " " + ("--command \"sudo docker exec -i \\$(sudo docker ps -a | grep " + this.image + " | ") + ("cut -d ' ' -f 1) bash -c 'eval \\$(echo " + b64 + " | base64 --decode)'\""); if (verbose) { console.log("executing the following command\n" + wrapped_cmd); } return [4 /*yield*/, utils_1.Utils.exec(wrapped_cmd, verbose)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Node.prototype.kibana_saved_objects = function (verbose) { return __awaiter(this, void 0, void 0, function () { var cmd, resp; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.kibana) { throw Error(this.name + " isnt a kibana node!"); } cmd = 'curl localhost:5601/api/saved_objects/_find?per_page=10000'; if (verbose) { console.log("fetching kibana saved objects for " + this.name); } return [4 /*yield*/, this.exec(cmd)]; case 1: resp = _a.sent(); return [2 /*return*/, JSON.parse(resp).saved_objects]; } }); }); }; // returns number | undefined Node.prototype.kibana_status = function (verbose) { return __awaiter(this, void 0, void 0, function () { var cmd, res, e_3; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.kibana) { return [2 /*return*/]; } else if (verbose) { console.log("fetching kibana status for " + this.name); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); cmd = 'curl -s -o /dev/null -w "%{http_code}" localhost:5601'; return [4 /*yield*/, this.exec(cmd)]; case 2: res = _a.sent(); return [2 /*return*/, res ? Number(res) : undefined]; case 3: e_3 = _a.sent(); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; Node.prototype.restart = function (verbose) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.stop(verbose)]; case 1: _a.sent(); return [4 /*yield*/, this.start(verbose)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }; Node.prototype.start = function (verbose) { return __awaiter(this, void 0, void 0, function () { var cmd; return __generator(this, function (_a) { switch (_a.label) { case 0: cmd = "gcloud compute instances start " + this.name + " --zone " + this.zone; if (verbose) { console.log("starting instance " + this.name + " via " + cmd); } return [4 /*yield*/, utils_1.Utils.exec(cmd, verbose)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Node.prototype.stop = function (verbose) { return __awaiter(this, void 0, void 0, function () { var cmd; return __generator(this, function (_a) { switch (_a.label) { case 0: cmd = "gcloud compute instances stop " + this.name + " --zone " + this.zone; if (verbose) { console.log("stopping instance " + this.name + " via " + cmd); } return [4 /*yield*/, utils_1.Utils.exec(cmd, verbose)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Node.prototype.update = function (opts) { return (new node_updater_1.NodeUpdater(this, new node_update_opts_1.NodeUpdateOpts(opts))).update(); }; Node.prototype.wait_for_elastic = function (interval, verbose) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!utils_1.Utils.is_number(interval) || (interval < 1000)) { throw Error('gap time between consecutive requests must be >= 1000'); } if (verbose) { console.log("waiting for state >= yellow from elastic for " + this.name); } return [4 /*yield*/, new Promise(function (resolve) { var cnt = 0; var again = function () { setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { var state; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.cluster_state(verbose)]; case 1: state = _a.sent(); /yellow|green/.test(state) ? resolve() : again(); return [2 /*return*/]; } }); }); }, !cnt++ ? Math.random() * interval : interval); }; again(); })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Node.prototype.wait_for_kibana = function (interval, verbose) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.kibana) { throw Error(this.name + " isnt a kibana node! You'll never get a 200 response."); } else if (!utils_1.Utils.is_number(interval) || (interval < 1000)) { throw Error('gap time between consecutive requests must be >= 1000'); } if (verbose) { console.log("waiting for status 200 from kibana for " + this.name); } return [4 /*yield*/, new Promise(function (resolve) { var cnt = 0; var again = function () { setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { var status; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.kibana_status(verbose)]; case 1: status = _a.sent(); status === 200 ? resolve() : again(); return [2 /*return*/]; } }); }); }, !cnt++ ? Math.random() * interval : interval); }; again(); })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Node.prototype._set_created = function (v) { if (!utils_1.Utils.is_integer(v.created) || (v.created <= 0)) { throw Error('invalid value for created.'); } this.created = v.created; }; Node.prototype._set_ip = function (v) { if (!utils_1.Utils.is_string(v.ip) || !v.ip) { throw Error('ip not a valid string'); } this.ip = v.ip; }; return Node; }(base_node_1.BaseNode)); exports.Node = Node;