local-elastic-docker
Version:
A package for creating / tearing down local Elasticsearch / Kibana single node clusters via Docker.
381 lines (380 loc) • 18.2 kB
JavaScript
"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 base_container_1 = require("../base-container");
var image_1 = require("../image");
var utils_1 = require("../utils");
var Container = /** @class */ (function (_super) {
__extends(Container, _super);
function Container(v) {
return _super.call(this, v) || this;
}
Container.fetch_all = function (verbose) {
return __awaiter(this, void 0, void 0, function () {
var names_cmd, containers, names, inspect_cmd, configs, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
names_cmd = "docker ps -a --filter \"label=" + image_1.elastic_image_label + "\" " +
'--format " {{ .Names }}"';
containers = [];
if (verbose) {
console.log("fetching all containers this package has created via\n" + names_cmd);
}
return [4 /*yield*/, utils_1.Utils.exec(names_cmd)];
case 1:
names = _c.sent();
names = names.split('\n').join(' ');
if (!names.length) return [3 /*break*/, 3];
inspect_cmd = "docker inspect " + names;
_b = (_a = JSON).parse;
return [4 /*yield*/, utils_1.Utils.exec(inspect_cmd)];
case 2:
configs = _b.apply(_a, [_c.sent()]);
configs.forEach(function (config) {
var labels = config.Config.Labels;
var b64 = labels[image_1.elastic_image_label];
var decoded = JSON.parse(Buffer.from(b64, 'base64').toString());
containers.push(new Container(decoded));
});
_c.label = 3;
case 3: return [2 /*return*/, containers];
}
});
});
};
// resolves w/ the standard elasticsearch json response for GET _cluster/health | undefined
Container.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*/];
}
});
});
};
// resolves w/ green | yellow | red | undefined
Container.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*/];
}
});
});
};
Container.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 = "docker rm -f " + this.name;
if (verbose) {
console.log("deleting container " + 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.
Container.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 = "docker exec -i " + this.name +
(" 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()];
}
});
});
};
Container.prototype.kibana_saved_objects = function (verbose) {
return __awaiter(this, void 0, void 0, function () {
var major, cmd, resp, types, cmd, resp;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.kibana) {
throw Error(this.name + " isnt a kibana node!");
}
major = this._get_major_version();
if (verbose) {
console.log("fetching kibana saved objects for " + this.name);
}
if (!(major === 6)) return [3 /*break*/, 2];
cmd = 'curl -X GET "localhost:5601/api/saved_objects/_find?per_page=10000"';
return [4 /*yield*/, this.exec(cmd)];
case 1:
resp = _a.sent();
return [2 /*return*/, JSON.parse(resp).saved_objects];
case 2:
if (!(major === 7)) return [3 /*break*/, 4];
types = [
'config',
'map',
'canvas-workpad',
'canvas-element',
'index-pattern',
'visualization',
'search',
'dashboard',
'url'
];
cmd = 'curl -X GET "localhost:5601/api/saved_objects/_find?per_page=10000&' +
types.map(function (t) { return "type=" + t; }).join('&') + '"';
return [4 /*yield*/, this.exec(cmd)];
case 3:
resp = _a.sent();
return [2 /*return*/, JSON.parse(resp).saved_objects];
case 4: throw Error('havent added support for es major version ' + major);
}
});
});
};
// resolves w/ number | undefined
Container.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/status';
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*/];
}
});
});
};
Container.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*/];
}
});
});
};
Container.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 = "docker start " + this.name;
if (verbose) {
console.log("starting container " + this.name + " via " + cmd);
}
return [4 /*yield*/, utils_1.Utils.exec(cmd, verbose)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
Container.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 = "docker stop " + this.name;
if (verbose) {
console.log("stopping container " + this.name + " via " + cmd);
}
return [4 /*yield*/, utils_1.Utils.exec(cmd, verbose)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
Container.prototype.wait_for_elastic = function (verbose) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (verbose) {
console.log("waiting for state >= yellow from elastic for " + this.name);
}
return [4 /*yield*/, new Promise(function (resolve) {
var interval = 2000;
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*/];
}
});
}); }, interval);
};
again();
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
Container.prototype.wait_for_kibana = function (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 container! You'll never get a 200 response.");
}
else if (verbose) {
console.log("waiting for status 200 from kibana for " + this.name);
}
return [4 /*yield*/, new Promise(function (resolve) {
var interval = 2000;
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*/];
}
});
}); }, interval);
};
again();
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
return Container;
}(base_container_1.BaseContainer));
exports.Container = Container;