dce-dev-wizard
Version:
Wizard for managing development apps at Harvard DCE.
160 lines • 9.71 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(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 };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var clear_1 = __importDefault(require("clear"));
// Import helpers
var showChooser_1 = __importDefault(require("../helpers/showChooser"));
var print_1 = __importDefault(require("../helpers/print"));
var exec_1 = __importDefault(require("../helpers/exec"));
var config_1 = __importDefault(require("../helpers/config"));
/**
* Show and/or modify related cluster status
* @author Gabe Abrams
* @param deployment the currently selected deployment
*/
var relatedClusters = function (deployment) { return __awaiter(void 0, void 0, void 0, function () {
var relatedClusters_1, relatedClustersWithStatus, i, relatedCluster, statusOutput_1, isOnline, options, option, cluster, statusOutput, nowIsOnline, newStatus, script;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!true) return [3 /*break*/, 5];
// Show loading indicator
(0, clear_1.default)();
console.log('Checking status of related instances...\n');
relatedClusters_1 = config_1.default.relatedClusters;
if (!relatedClusters_1 || relatedClusters_1.length === 0) {
(0, clear_1.default)();
console.log('No related clusters. In your dceConfig.json file, add a "relatedClusters" array.');
return [2 /*return*/];
}
relatedClustersWithStatus = [];
for (i = 0; i < relatedClusters_1.length; i += 1) {
relatedCluster = relatedClusters_1[i];
// Check if the related cluster is valid
if (!relatedCluster.cluster || !relatedCluster.name) {
console.log('Related clusters must be of form { name, cluster } where name is a human-readable name, cluster is the AWS cluster name.');
process.exit(0);
}
statusOutput_1 = (0, exec_1.default)("aws autoscaling describe-auto-scaling-groups --query \"AutoScalingGroups[?contains(AutoScalingGroupName, '".concat(relatedCluster.cluster, "')] | [].Instances[] | length(@)\""), false);
// Check if the output is a number
if (Number.isNaN(Number.parseInt(statusOutput_1, 10))) {
console.log("Error getting status of ".concat(relatedCluster.name, " (").concat(relatedCluster.cluster, ")"));
console.log("Got output: ".concat(statusOutput_1, " but expected a number."));
process.exit(0);
}
isOnline = Number.parseInt(statusOutput_1, 10) >= 2;
// Write to related cluster list
relatedClustersWithStatus.push(__assign(__assign({}, relatedCluster), { isOnline: isOnline }));
}
// Show status
(0, clear_1.default)();
options = relatedClustersWithStatus.map(function (cluster) {
return {
description: "".concat(cluster.name, " (").concat(cluster.isOnline ? 'ONLINE' : 'Offline', ")"),
};
});
// Option to refresh
options.push({
description: '[Refresh Status]',
tag: 'R',
});
// Option to go back to main menu
options.push({
description: '[Back to Main Menu]',
tag: 'B',
});
option = (0, showChooser_1.default)({
question: 'Choose a cluster to turn on/off:',
options: options,
});
if (option.tag === 'B') {
// Back to main menu
return [2 /*return*/];
}
if (option.tag === 'R') {
// Refresh status
return [3 /*break*/, 0];
}
cluster = relatedClustersWithStatus[option.index];
statusOutput = (0, exec_1.default)("aws autoscaling describe-auto-scaling-groups --query \"AutoScalingGroups[?contains(AutoScalingGroupName, '".concat(cluster.cluster, "')] | [].Instances[] | length(@)\""), false);
nowIsOnline = Number.parseInt(statusOutput, 10) >= 2;
if (!(nowIsOnline !== cluster.isOnline)) return [3 /*break*/, 2];
console.log('Status is out of date. Please continue to refresh.');
return [4 /*yield*/, print_1.default.enterToContinue()];
case 1:
_a.sent();
return [3 /*break*/, 0];
case 2:
newStatus = (cluster.isOnline ? 'off' : 'on');
script = "\n ON_OR_OFF=\"".concat(newStatus, "\"\n CLUSTER_NAME=\"").concat(cluster.cluster, "\"\n\n if ! command -v -- \"aws\" >/dev/null 2>&1; then\n echo \"AWS CLI is not installed. Please install it first.\"\n exit 1\n fi\n\n desiredCapacity=$([ \"$ON_OR_OFF\" == \"on\" ] && echo 1 || echo 0)\n\n asgs=$(\n aws autoscaling describe-auto-scaling-groups --query \"AutoScalingGroups[?contains(AutoScalingGroupName, '$CLUSTER_NAME')].AutoScalingGroupName\" --output text\n )\n\n for asg in $asgs; do\n echo \"Turning $ON_OR_OFF auto scaling group $asg\"\n aws autoscaling set-desired-capacity --auto-scaling-group-name $asg --desired-capacity $desiredCapacity --no-honor-cooldown\n done\n ");
(0, clear_1.default)();
print_1.default.title("Turning ".concat(newStatus));
console.log("\nTurning ".concat(newStatus, " ").concat(cluster.name, "...\n"));
return [4 /*yield*/, (0, exec_1.default)(script, true)];
case 3:
_a.sent();
return [4 /*yield*/, new Promise(function (r) {
setTimeout(r, 3000);
})];
case 4:
_a.sent();
(0, clear_1.default)();
print_1.default.title('Cluster Updated');
console.log("\n".concat(cluster.name, " is ").concat(cluster.isOnline ? 'turning off' : 'turning on', ". This will take up to 15min.\n"));
print_1.default.enterToContinue();
return [3 /*break*/, 0];
case 5: return [2 /*return*/];
}
});
}); };
exports.default = relatedClusters;
//# sourceMappingURL=relatedClusters.js.map