dce-dev-wizard
Version:
Wizard for managing development apps at Harvard DCE.
139 lines (135 loc) • 6.31 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const clear_1 = __importDefault(require("clear"));
// Import helpers
const showChooser_1 = __importDefault(require("../helpers/showChooser"));
const print_1 = __importDefault(require("../helpers/print"));
const exec_1 = __importDefault(require("../helpers/exec"));
const config_1 = __importDefault(require("../helpers/config"));
/**
* Show and/or modify related cluster status
* @author Gabe Abrams
* @param deployment the currently selected deployment
*/
const relatedClusters = (deployment) => __awaiter(void 0, void 0, void 0, function* () {
while (true) {
// Show loading indicator
(0, clear_1.default)();
console.log('Checking status of related instances...\n');
// Get related clusters
const { relatedClusters } = config_1.default;
if (!relatedClusters || relatedClusters.length === 0) {
(0, clear_1.default)();
console.log('No related clusters. In your dceConfig.json file, add a "relatedClusters" array.');
return;
}
// Get the status on related clusters
const relatedClustersWithStatus = [];
for (let i = 0; i < relatedClusters.length; i += 1) {
const relatedCluster = relatedClusters[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);
}
// Execute status command via aws cli
const statusOutput = (0, exec_1.default)(`aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?contains(AutoScalingGroupName, '${relatedCluster.cluster}')] | [].Instances[] | length(@)"`, false);
// Check if the output is a number
if (Number.isNaN(Number.parseInt(statusOutput, 10))) {
console.log(`Error getting status of ${relatedCluster.name} (${relatedCluster.cluster})`);
console.log(`Got output: ${statusOutput} but expected a number.`);
process.exit(0);
}
const isOnline = Number.parseInt(statusOutput, 10) >= 2;
// Write to related cluster list
relatedClustersWithStatus.push(Object.assign(Object.assign({}, relatedCluster), { isOnline }));
}
// Show status
(0, clear_1.default)();
const options = relatedClustersWithStatus.map((cluster) => {
return {
description: `${cluster.name} (${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',
});
// Show chooser
const option = (0, showChooser_1.default)({
question: 'Choose a cluster to turn on/off:',
options,
});
if (option.tag === 'B') {
// Back to main menu
return;
}
if (option.tag === 'R') {
// Refresh status
continue;
}
const cluster = relatedClustersWithStatus[option.index];
// Re-check status
const statusOutput = (0, exec_1.default)(`aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?contains(AutoScalingGroupName, '${cluster.cluster}')] | [].Instances[] | length(@)"`, false);
const nowIsOnline = Number.parseInt(statusOutput, 10) >= 2;
if (nowIsOnline !== cluster.isOnline) {
console.log('Status is out of date. Please continue to refresh.');
yield print_1.default.enterToContinue();
continue;
}
// Ready to toggle!
const newStatus = (cluster.isOnline ? 'off' : 'on');
const script = `
ON_OR_OFF="${newStatus}"
CLUSTER_NAME="${cluster.cluster}"
if ! command -v -- "aws" >/dev/null 2>&1; then
echo "AWS CLI is not installed. Please install it first."
exit 1
fi
desiredCapacity=$([ "$ON_OR_OFF" == "on" ] && echo 1 || echo 0)
asgs=$(
aws autoscaling describe-auto-scaling-groups \
--query "AutoScalingGroups[?contains(AutoScalingGroupName, '$CLUSTER_NAME')].AutoScalingGroupName" \
--output text
)
for asg in $asgs; do
echo "Turning $ON_OR_OFF auto scaling group $asg"
aws autoscaling set-desired-capacity \
--auto-scaling-group-name $asg \
--desired-capacity $desiredCapacity \
--no-honor-cooldown
done
`;
(0, clear_1.default)();
print_1.default.title(`Turning ${newStatus}`);
console.log(`\nTurning ${newStatus} ${cluster.name}...\n`);
yield (0, exec_1.default)(script, true);
yield new Promise((r) => {
setTimeout(r, 3000);
});
(0, clear_1.default)();
print_1.default.title('Cluster Updated');
console.log(`\n${cluster.name} is ${cluster.isOnline ? 'turning off' : 'turning on'}. This will take up to 15min.\n`);
print_1.default.enterToContinue();
}
});
exports.default = relatedClusters;
//# sourceMappingURL=relatedClusters.js.map