UNPKG

dce-dev-wizard

Version:

Wizard for managing development apps at Harvard DCE.

237 lines 14.2 kB
"use strict"; 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 os_1 = __importDefault(require("os")); var clear_1 = __importDefault(require("clear")); var child_process_1 = require("child_process"); // 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")); var prompt_1 = __importDefault(require("../helpers/prompt")); // Get home directory var homeDir = os_1.default.homedir(); /** * Connect/tunnel into a db * @author Gabe Abrams * @param deployment the currently selected deployment */ var connectToDatabase = function (deployment) { return __awaiter(void 0, void 0, void 0, function () { var dbName, name, app, profile, stackName, bastionInstanceId, dbEndpoint, dbPasswordSecret, dbPassword, session_args, tag, collectionName, outFilename, collectionName, jsonPath, collectionName, jsonPath; return __generator(this, function (_a) { switch (_a.label) { case 0: dbName = config_1.default.dbName; // Make sure the deployment has a db if (!dbName) { (0, clear_1.default)(); print_1.default.title('No Database!'); console.log(''); console.log('This deployment does not have a database.'); console.log(''); print_1.default.enterToContinue(); return [2 /*return*/]; } name = deployment.name, app = deployment.app, profile = deployment.profile; // Kill apps (0, clear_1.default)(); print_1.default.title('Connect to database'); console.log('\nEnter sudo to cleanup existing sessions:'); (0, exec_1.default)('sudo lsof -ti tcp:27018 | xargs -r kill', true); (0, clear_1.default)(); print_1.default.title('Connect to database'); console.log(''); stackName = "CacclDeploy-".concat(app); bastionInstanceId = (0, exec_1.default)("aws cloudformation describe-stacks --stack-name ".concat(stackName, " --query \"Stacks[].Outputs[?ExportName=='").concat(stackName, "-bastion-host-id'].OutputValue\" --profile ").concat(profile, " --output text"), false).trim(); console.log("Bastion Instance Id: ".concat(bastionInstanceId)); dbEndpoint = (0, exec_1.default)("aws cloudformation describe-stacks --stack-name ".concat(stackName, " --query \"Stacks[].Outputs[?ExportName=='").concat(stackName, "-db-cluster-endpoint'].OutputValue\" --profile ").concat(profile, " --output text"), false).trim(); console.log("DB Endpoint: ".concat(dbEndpoint)); dbPasswordSecret = (0, exec_1.default)("aws cloudformation describe-stacks --stack-name ".concat(stackName, " --query \"Stacks[].Outputs[?ExportName=='").concat(stackName, "-db-password-secret-arn'].OutputValue\" --profile ").concat(profile, " --output text"), false).trim(); console.log("DB Password Secret: ".concat(dbPasswordSecret)); dbPassword = (0, exec_1.default)("aws secretsmanager get-secret-value --secret-id ".concat(dbPasswordSecret, " --query 'SecretString' --profile ").concat(profile, " --output text"), false).trim(); console.log("DB Password: ".concat(dbPassword)); session_args = [ 'ssm', 'start-session', '--target', bastionInstanceId, '--document-name', 'AWS-StartPortForwardingSessionToRemoteHost', '--parameters', "{\"portNumber\":[\"27017\"],\"localPortNumber\":[\"27018\"],\"host\":[\"".concat(dbEndpoint.split(':')[0], "\"]}"), '--profile', profile, ]; (0, child_process_1.spawn)('aws', session_args, { stdio: 'ignore', detached: true, }); console.log('SSM Session started'); // Finished console.log('\nFinished! Scroll up for errors or more details.'); tag = (0, showChooser_1.default)({ question: 'Operation:', options: [ { description: 'Robo 3T Setup Instructions', tag: 'R', }, { description: 'Studio 3T Setup Instructions', tag: 'S', }, { description: 'Export Collection', tag: 'E', }, { description: 'Insert Items in Collection', tag: 'I', }, { description: 'Upsert Items in Collection', tag: 'U', }, { description: 'Back to Main Menu', tag: 'B', }, ], title: 'Choose an operation:', }).tag; // Robo 3T if (tag === 'R') { (0, clear_1.default)(); print_1.default.title('Robo 3T Setup Instructions'); console.log([ '1. Open Robo 3T, create a new connection', '2. Fill out the "Connection" tab:', " Name = ".concat(name), " Address = localhost", ' Port = 27018', '3. Fill out the "Authentication" tab:', ' Check the "Perform authentication" box', " Database = ".concat(dbName), " Username = root", " Password = ".concat(dbPassword), '4. Fill out the "SSL" tab:', ' Check the "Use SSL protocol" box', ' Authentication Method = Self-signed certificate', ' Check the "Advanced Options" box', ' Invalid Hostnames = Allowed', '5. Click "Test"', '6. If the test is successful, click "Save"', ].join('\n')); console.log(''); print_1.default.enterToContinue(); } if (!(tag === 'S')) return [3 /*break*/, 2]; (0, clear_1.default)(); print_1.default.title('Studio 3T Setup Instructions'); console.log([ '1. Open Studio 3T, create a new connection', '2. Choose "Manually configure my connection settings", then click "Next"', "3. Set the \"Connection name\" to ".concat(name), '4. Fill out the "Server" tab:', ' Connection Type = Standalone', ' Address = localhost', ' Port = 27018', '5. Fill out the "Authentication" tab:', ' Authentication Mode = Legacy', " Username = root", " Password = ".concat(dbPassword), " Authentication DB = ".concat(dbName), '6. Fill out the "SSL" tab:', ' Check the "Use SSL protocol to connect" box', ' Check the "Allow invalid hostnames" box', '5. Click "Test Connection"', '6. If the test is successful, click "Save"', ].join('\n')); console.log(''); return [4 /*yield*/, print_1.default.enterToContinue()]; case 1: _a.sent(); _a.label = 2; case 2: if (!(tag === 'E')) return [3 /*break*/, 4]; (0, clear_1.default)(); print_1.default.title('Export Collection'); collectionName = (0, prompt_1.default)('Collection Name: '); outFilename = "".concat(collectionName, "-").concat(new Date().toISOString(), ".json"); console.log("\nExporting collection to ".concat(outFilename, "\n")); (0, exec_1.default)("mongoexport --collection=".concat(collectionName, " --host=\"localhost:27018\" --db=").concat(dbName, " --out=\"").concat(outFilename, "\" --username=root --password=").concat(dbPassword, " --ssl --sslAllowInvalidHostnames --sslAllowInvalidCertificates"), true); console.log("\nExported to ".concat(outFilename, "\n")); return [4 /*yield*/, print_1.default.enterToContinue()]; case 3: _a.sent(); _a.label = 4; case 4: if (!(tag === 'I')) return [3 /*break*/, 6]; (0, clear_1.default)(); print_1.default.title('Insert Items in Collection'); collectionName = (0, prompt_1.default)('Collection Name: '); console.log('\nCreate a JSON file (one entry per line), drop the file here, then press enter.'); jsonPath = (0, prompt_1.default)('Path: ').trim(); console.log("\nInserting items into collection \"".concat(collectionName, "\"\n")); (0, exec_1.default)("mongoimport --collection=".concat(collectionName, " --host=\"localhost:27018\" --db=").concat(dbName, " --username=root --password=").concat(dbPassword, " --ssl --sslAllowInvalidHostnames --sslAllowInvalidCertificates --file=\"").concat(jsonPath, "\""), true); print_1.default.title('Inserted Successfully'); return [4 /*yield*/, print_1.default.enterToContinue()]; case 5: _a.sent(); _a.label = 6; case 6: if (!(tag === 'U')) return [3 /*break*/, 8]; (0, clear_1.default)(); print_1.default.title('Upsert Items in Collection'); collectionName = (0, prompt_1.default)('Collection Name: '); console.log('\nCreate a JSON file (one entry per line), drop the file here, then press enter.'); jsonPath = (0, prompt_1.default)('Path: ').trim(); console.log("\nUpserting items into collection \"".concat(collectionName, "\"\n")); (0, exec_1.default)("mongoimport --upsert --collection=".concat(collectionName, " --host=\"localhost:27018\" --db=").concat(dbName, " --username=root --password=").concat(dbPassword, " --ssl --sslAllowInvalidHostnames --sslAllowInvalidCertificates --file=\"").concat(jsonPath, "\""), true); print_1.default.title('Upsert Successful'); return [4 /*yield*/, print_1.default.enterToContinue()]; case 7: _a.sent(); _a.label = 8; case 8: return [2 /*return*/]; } }); }); }; exports.default = connectToDatabase; //# sourceMappingURL=connectToDatabase.js.map