@catladder/cli 0.0.0-main-da5fc4cd → 0.0.0-main-c963a076

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -133,7 +133,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
133
133
  }
134
134
  };
135
135
  exports.__esModule = true;
136
- var child_process_promise_1 = __nccwpck_require__(75763);
136
+ var copyDb_1 = __nccwpck_require__(45772);
137
137
  var startProxy_1 = __nccwpck_require__(63069);
138
138
  exports["default"] = (function (vorpal) { return __awaiter(void 0, void 0, void 0, function () {
139
139
  return __generator(this, function (_a) {
@@ -141,7 +141,7 @@ exports["default"] = (function (vorpal) { return __awaiter(void 0, void 0, void
141
141
  .command("cloud-sql-restore-db", "restore a db from one source to another target")
142
142
  .action(function restoreDb() {
143
143
  return __awaiter(this, void 0, void 0, function () {
144
- var sourceInstance, sourceProxy, targetProxy, sourcePort, targetPort, sourceLocalPort, sourceUsername, sourcePassword, sourceDbName, targetInstance, targetLocalPort, targetUsername, targetPassword, targetDbName, shouldContinue, targetPSQL, copyDBScript;
144
+ var sourceInstance, sourceProxy, targetProxy, sourcePort, targetPort, sourceLocalPort, sourceUsername, sourcePassword, sourceDbName, targetInstance, targetLocalPort, targetUsername, targetPassword, targetDbName, shouldContinue;
145
145
  return __generator(this, function (_a) {
146
146
  switch (_a.label) {
147
147
  case 0: return [4 /*yield*/, this.prompt({
@@ -252,14 +252,19 @@ exports["default"] = (function (vorpal) { return __awaiter(void 0, void 0, void
252
252
  if (!shouldContinue) {
253
253
  return [2 /*return*/];
254
254
  }
255
- targetPSQL = function (command) {
256
- return "PGPASSWORD=".concat(targetPassword, " psql -p ").concat(targetPort, " --host=localhost --user=").concat(targetUsername, " -q ").concat(command);
257
- };
258
- copyDBScript = "\n set -e\n \n \n\n dumptmp=$(mktemp /tmp/dump.XXXXXX)\n\n echo \"Dumping file to $dumptmp\"\n pg_dump --dbname=postgres://".concat(sourceUsername, ":").concat(sourcePassword, "@localhost:").concat(sourcePort, "/").concat(sourceDbName, " --no-owner --no-privileges > $dumptmp\n echo \"dump done\"\n ").concat(targetPSQL("-c 'drop database \"".concat(targetDbName, "\" WITH (FORCE)' 1> /dev/null || true")), "\n ").concat(targetPSQL("-c 'create database \"".concat(targetDbName, "\"' 1> /dev/null")), "\n echo \"Restoring dump...\"\n ").concat(targetPSQL("\"".concat(targetDbName, "\" < $dumptmp 1> /dev/null")), "\n \n\n echo \"Clean up...\"\n set +e\n rm $dumptmp\n echo \"\n\uD83D\uDC31 Done!\"\n ");
259
255
  _a.label = 18;
260
256
  case 18:
261
257
  _a.trys.push([18, , 20, 21]);
262
- return [4 /*yield*/, (0, child_process_promise_1.spawn)(copyDBScript, [], { shell: "bash", stdio: "inherit" })];
258
+ return [4 /*yield*/, (0, copyDb_1.spawnCopyDb)({
259
+ targetPassword: targetPassword,
260
+ targetPort: targetPort,
261
+ targetUsername: targetUsername,
262
+ sourceUsername: sourceUsername,
263
+ sourcePassword: sourcePassword,
264
+ sourcePort: sourcePort,
265
+ sourceDbName: sourceDbName,
266
+ targetDbName: targetDbName
267
+ })];
263
268
  case 19:
264
269
  _a.sent();
265
270
  return [3 /*break*/, 21];
@@ -1400,6 +1405,202 @@ exports.getProjectMongodbAllPodsSortedWithLabel = getProjectMongodbAllPodsSorted
1400
1405
 
1401
1406
  /***/ }),
1402
1407
 
1408
+ /***/ 38831:
1409
+ /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
1410
+
1411
+ "use strict";
1412
+
1413
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1414
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1415
+ return new (P || (P = Promise))(function (resolve, reject) {
1416
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1417
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1418
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1419
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1420
+ });
1421
+ };
1422
+ var __generator = (this && this.__generator) || function (thisArg, body) {
1423
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
1424
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
1425
+ function verb(n) { return function (v) { return step([n, v]); }; }
1426
+ function step(op) {
1427
+ if (f) throw new TypeError("Generator is already executing.");
1428
+ while (_) try {
1429
+ 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;
1430
+ if (y = 0, t) op = [op[0] & 2, t.value];
1431
+ switch (op[0]) {
1432
+ case 0: case 1: t = op; break;
1433
+ case 4: _.label++; return { value: op[1], done: false };
1434
+ case 5: _.label++; y = op[1]; op = [0]; continue;
1435
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
1436
+ default:
1437
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
1438
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
1439
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
1440
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
1441
+ if (t[2]) _.ops.pop();
1442
+ _.trys.pop(); continue;
1443
+ }
1444
+ op = body.call(thisArg, _);
1445
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
1446
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
1447
+ }
1448
+ };
1449
+ exports.__esModule = true;
1450
+ var getProjectConfig_1 = __nccwpck_require__(24146);
1451
+ var startProxy_1 = __nccwpck_require__(63069);
1452
+ var autocompletions_1 = __nccwpck_require__(41373);
1453
+ var connection_string_parser_1 = __nccwpck_require__(71004);
1454
+ var copyDb_1 = __nccwpck_require__(45772);
1455
+ exports["default"] = (function (vorpal) { return __awaiter(void 0, void 0, void 0, function () {
1456
+ return __generator(this, function (_a) {
1457
+ return [2 /*return*/, vorpal
1458
+ .command("project-cloud-sql-restore-db", "restores a project db from one source to another target")
1459
+ .action(function restoreDb() {
1460
+ var _a, _b, _c, _d;
1461
+ return __awaiter(this, void 0, void 0, function () {
1462
+ var envs, sourceEnvAndComponent, _e, sourceEnv, sourceComponent, sourceContext, sourceEnvVars, sourceProxy, sourceDbName, sourceUsername, sourcePassword, targetUsername, targetPassword, targetProxy, sourcePort, targetPort, targetDbName, closeAll, parser, parsersResult, targetEnvAndComponent, _f, targetEnv, targetComponent, targetContext, targetEnvVars, parser, parsersResult, shouldContinue, confirmInstance;
1463
+ return __generator(this, function (_g) {
1464
+ switch (_g.label) {
1465
+ case 0: return [4 /*yield*/, (0, autocompletions_1.envAndComponents)()];
1466
+ case 1:
1467
+ envs = _g.sent();
1468
+ return [4 /*yield*/, this.prompt({
1469
+ type: "list",
1470
+ name: "sourceEnvAndComponent",
1471
+ choices: envs,
1472
+ message: "Source instance (connection string or 'local')? 🤔 "
1473
+ })];
1474
+ case 2:
1475
+ sourceEnvAndComponent = (_g.sent()).sourceEnvAndComponent;
1476
+ _e = sourceEnvAndComponent.split(":"), sourceEnv = _e[0], sourceComponent = _e[1];
1477
+ return [4 /*yield*/, (0, getProjectConfig_1.getPipelineContextByChoice)(sourceEnv, sourceComponent)];
1478
+ case 3:
1479
+ sourceContext = _g.sent();
1480
+ return [4 /*yield*/, (0, getProjectConfig_1.getEnvVarsResolved)(this, sourceContext.environment.shortName, sourceContext.componentName)];
1481
+ case 4:
1482
+ sourceEnvVars = _g.sent();
1483
+ closeAll = function () {
1484
+ sourceProxy === null || sourceProxy === void 0 ? void 0 : sourceProxy.stop();
1485
+ targetProxy === null || targetProxy === void 0 ? void 0 : targetProxy.stop();
1486
+ };
1487
+ if (!(sourceEnv === "local")) return [3 /*break*/, 5];
1488
+ parser = new connection_string_parser_1.ConnectionStringParser({
1489
+ scheme: "postgres",
1490
+ hosts: []
1491
+ });
1492
+ parsersResult = parser.parse(sourceEnvVars.DATABASE_URL);
1493
+ sourcePort = (_b = (_a = parsersResult.hosts) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.port;
1494
+ sourceUsername = parsersResult.username;
1495
+ sourcePassword = parsersResult.password;
1496
+ sourceDbName = parsersResult.endpoint;
1497
+ return [3 /*break*/, 7];
1498
+ case 5:
1499
+ sourcePort = 54399;
1500
+ return [4 /*yield*/, (0, startProxy_1.startCloudSqlProxyInBackground)({
1501
+ instanceName: sourceEnvVars.CLOUD_SQL_INSTANCE_CONNECTION_NAME,
1502
+ localPort: sourcePort
1503
+ })];
1504
+ case 6:
1505
+ sourceProxy = _g.sent();
1506
+ sourceUsername = sourceEnvVars.DB_USER;
1507
+ sourcePassword = sourceEnvVars.DB_PASSWORD;
1508
+ sourceDbName = sourceEnvVars.DB_NAME;
1509
+ _g.label = 7;
1510
+ case 7: return [4 /*yield*/, this.prompt({
1511
+ type: "list",
1512
+ name: "targetEnvAndComponent",
1513
+ choices: envs,
1514
+ message: "target env? 🤔 "
1515
+ })];
1516
+ case 8:
1517
+ targetEnvAndComponent = (_g.sent()).targetEnvAndComponent;
1518
+ _f = targetEnvAndComponent.split(":"), targetEnv = _f[0], targetComponent = _f[1];
1519
+ return [4 /*yield*/, (0, getProjectConfig_1.getPipelineContextByChoice)(targetEnv, targetComponent)];
1520
+ case 9:
1521
+ targetContext = _g.sent();
1522
+ return [4 /*yield*/, (0, getProjectConfig_1.getEnvVarsResolved)(this, targetContext.environment.shortName, targetContext.componentName)];
1523
+ case 10:
1524
+ targetEnvVars = _g.sent();
1525
+ if (!(targetEnv === "local")) return [3 /*break*/, 11];
1526
+ parser = new connection_string_parser_1.ConnectionStringParser({
1527
+ scheme: "postgres",
1528
+ hosts: []
1529
+ });
1530
+ parsersResult = parser.parse(targetEnvVars.DATABASE_URL);
1531
+ targetPort = (_d = (_c = parsersResult.hosts) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.port;
1532
+ targetUsername = parsersResult.username;
1533
+ targetPassword = parsersResult.password;
1534
+ targetDbName = parsersResult.endpoint;
1535
+ return [3 /*break*/, 13];
1536
+ case 11:
1537
+ targetPort = 54499;
1538
+ return [4 /*yield*/, (0, startProxy_1.startCloudSqlProxyInBackground)({
1539
+ instanceName: targetEnvVars.CLOUD_SQL_INSTANCE_CONNECTION_NAME,
1540
+ localPort: targetPort
1541
+ })];
1542
+ case 12:
1543
+ targetProxy = _g.sent();
1544
+ targetUsername = targetEnvVars.DB_USER;
1545
+ targetPassword = targetEnvVars.DB_PASSWORD;
1546
+ targetDbName = sourceEnvVars.DB_NAME;
1547
+ _g.label = 13;
1548
+ case 13: return [4 /*yield*/, this.prompt({
1549
+ type: "confirm",
1550
+ name: "shouldContinue",
1551
+ message: "This will drop ".concat(targetEnv, "/").concat(targetDbName, " and replace it with ").concat(sourceEnv, "/").concat(sourceDbName, ". Continue? \uD83E\uDD14 ")
1552
+ })];
1553
+ case 14:
1554
+ shouldContinue = (_g.sent()).shouldContinue;
1555
+ if (!shouldContinue) {
1556
+ this.log("abort");
1557
+ closeAll();
1558
+ return [2 /*return*/];
1559
+ }
1560
+ if (!(targetContext.environment.envType === "prod")) return [3 /*break*/, 16];
1561
+ this.log("\n\uD83D\uDEA8 You are overriding a production environment. Please type in ".concat(targetEnvVars.CLOUD_SQL_INSTANCE_CONNECTION_NAME, " to continue\n\n"));
1562
+ return [4 /*yield*/, this.prompt({
1563
+ type: "input",
1564
+ name: "confirmInstance",
1565
+ message: "confirm: "
1566
+ })];
1567
+ case 15:
1568
+ confirmInstance = (_g.sent()).confirmInstance;
1569
+ if (confirmInstance !== targetEnvVars.CLOUD_SQL_INSTANCE_CONNECTION_NAME) {
1570
+ this.log("abort");
1571
+ closeAll();
1572
+ return [2 /*return*/];
1573
+ }
1574
+ _g.label = 16;
1575
+ case 16:
1576
+ _g.trys.push([16, , 18, 19]);
1577
+ return [4 /*yield*/, (0, copyDb_1.spawnCopyDb)({
1578
+ targetPassword: targetPassword,
1579
+ targetPort: targetPort,
1580
+ targetUsername: targetUsername,
1581
+ sourceUsername: sourceUsername,
1582
+ sourcePassword: sourcePassword,
1583
+ sourcePort: sourcePort,
1584
+ sourceDbName: sourceDbName,
1585
+ targetDbName: targetDbName
1586
+ })];
1587
+ case 17:
1588
+ _g.sent();
1589
+ return [3 /*break*/, 19];
1590
+ case 18:
1591
+ closeAll();
1592
+ return [7 /*endfinally*/];
1593
+ case 19: return [2 /*return*/];
1594
+ }
1595
+ });
1596
+ });
1597
+ })];
1598
+ });
1599
+ }); });
1600
+ //# sourceMappingURL=commandProjectRestoreDb.js.map
1601
+
1602
+ /***/ }),
1603
+
1403
1604
  /***/ 23526:
1404
1605
  /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
1405
1606
 
@@ -4136,6 +4337,7 @@ var commandPortForward_1 = __importDefault(__nccwpck_require__(3415));
4136
4337
  var commandTriggerCronjob_1 = __importDefault(__nccwpck_require__(51827));
4137
4338
  var commandOpenGrafanaPod_1 = __importDefault(__nccwpck_require__(55848));
4138
4339
  var commandSecretsClearBackups_1 = __importDefault(__nccwpck_require__(41863));
4340
+ var commandProjectRestoreDb_1 = __importDefault(__nccwpck_require__(38831));
4139
4341
  exports["default"] = (function (vorpal) { return __awaiter(void 0, void 0, void 0, function () {
4140
4342
  return __generator(this, function (_a) {
4141
4343
  (0, commandSetup_1["default"])(vorpal);
@@ -4150,6 +4352,7 @@ exports["default"] = (function (vorpal) { return __awaiter(void 0, void 0, void
4150
4352
  (0, commandOpenGrafana_1["default"])(vorpal);
4151
4353
  (0, commandOpenGrafanaPod_1["default"])(vorpal);
4152
4354
  (0, commandCloudSqlProxy_1["default"])(vorpal);
4355
+ (0, commandProjectRestoreDb_1["default"])(vorpal);
4153
4356
  (0, commandOpenGit_1["default"])(vorpal);
4154
4357
  (0, commandOpenEnv_1["default"])(vorpal);
4155
4358
  (0, commandTriggerCronjob_1["default"])(vorpal);
@@ -6672,6 +6875,77 @@ exports.openGoogleCloudRunDashboard = openGoogleCloudRunDashboard;
6672
6875
 
6673
6876
  /***/ }),
6674
6877
 
6878
+ /***/ 45772:
6879
+ /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
6880
+
6881
+ "use strict";
6882
+
6883
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6884
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6885
+ return new (P || (P = Promise))(function (resolve, reject) {
6886
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6887
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6888
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
6889
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
6890
+ });
6891
+ };
6892
+ var __generator = (this && this.__generator) || function (thisArg, body) {
6893
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
6894
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
6895
+ function verb(n) { return function (v) { return step([n, v]); }; }
6896
+ function step(op) {
6897
+ if (f) throw new TypeError("Generator is already executing.");
6898
+ while (_) try {
6899
+ 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;
6900
+ if (y = 0, t) op = [op[0] & 2, t.value];
6901
+ switch (op[0]) {
6902
+ case 0: case 1: t = op; break;
6903
+ case 4: _.label++; return { value: op[1], done: false };
6904
+ case 5: _.label++; y = op[1]; op = [0]; continue;
6905
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
6906
+ default:
6907
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
6908
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
6909
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
6910
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
6911
+ if (t[2]) _.ops.pop();
6912
+ _.trys.pop(); continue;
6913
+ }
6914
+ op = body.call(thisArg, _);
6915
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
6916
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
6917
+ }
6918
+ };
6919
+ exports.__esModule = true;
6920
+ exports.spawnCopyDb = void 0;
6921
+ var child_process_promise_1 = __nccwpck_require__(75763);
6922
+ var spawnCopyDb = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
6923
+ var script;
6924
+ return __generator(this, function (_a) {
6925
+ switch (_a.label) {
6926
+ case 0:
6927
+ script = createCopyDbScript(opts);
6928
+ return [4 /*yield*/, (0, child_process_promise_1.spawn)(script, [], {
6929
+ shell: "bash",
6930
+ stdio: "inherit"
6931
+ })];
6932
+ case 1: return [2 /*return*/, _a.sent()];
6933
+ }
6934
+ });
6935
+ }); };
6936
+ exports.spawnCopyDb = spawnCopyDb;
6937
+ var createCopyDbScript = function (_a) {
6938
+ var targetPassword = _a.targetPassword, targetPort = _a.targetPort, targetUsername = _a.targetUsername, sourceUsername = _a.sourceUsername, sourcePassword = _a.sourcePassword, sourcePort = _a.sourcePort, sourceDbName = _a.sourceDbName, targetDbName = _a.targetDbName;
6939
+ var targetPSQL = function (command) {
6940
+ return "PGPASSWORD=".concat(targetPassword, " psql -p ").concat(targetPort, " --host=localhost --user=").concat(targetUsername, " -q ").concat(command);
6941
+ };
6942
+ var copyDBScript = "\n set -e\n \n \n\n dumptmp=$(mktemp /tmp/dump.XXXXXX)\n\n echo \"Dumping file to $dumptmp\"\n pg_dump --dbname=postgres://".concat(sourceUsername, ":").concat(sourcePassword, "@localhost:").concat(sourcePort, "/").concat(sourceDbName, " --no-owner --no-privileges > $dumptmp\n echo \"dump done\"\n ").concat(targetPSQL("-c 'drop database \"".concat(targetDbName, "\" WITH (FORCE)' 1> /dev/null || true")), "\n ").concat(targetPSQL("-c 'create database \"".concat(targetDbName, "\"' 1> /dev/null")), "\n echo \"Restoring dump...\"\n ").concat(targetPSQL("\"".concat(targetDbName, "\" < $dumptmp 1> /dev/null")), "\n \n\n echo \"Clean up...\"\n set +e\n rm $dumptmp\n echo \"\n\uD83D\uDC31 Done!\"\n ");
6943
+ return copyDBScript;
6944
+ };
6945
+ //# sourceMappingURL=copyDb.js.map
6946
+
6947
+ /***/ }),
6948
+
6675
6949
  /***/ 63069:
6676
6950
  /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
6677
6951
 
@@ -135473,6 +135747,152 @@ var isArray = Array.isArray || function (xs) {
135473
135747
  };
135474
135748
 
135475
135749
 
135750
+ /***/ }),
135751
+
135752
+ /***/ 71004:
135753
+ /***/ ((module, exports) => {
135754
+
135755
+ // MIT License
135756
+ (function (factory) {
135757
+ if ( true && typeof module.exports === "object") {
135758
+ var v = factory(require, exports);
135759
+ if (v !== undefined) module.exports = v;
135760
+ }
135761
+ else if (typeof define === "function" && define.amd) {
135762
+ define(["require", "exports"], factory);
135763
+ }
135764
+ })(function () {
135765
+ "use strict";
135766
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
135767
+ exports.ConnectionStringParser = void 0;
135768
+ class ConnectionStringParser {
135769
+ constructor(options) {
135770
+ this.scheme = (options && options.scheme) || ConnectionStringParser.DEFAULT_SCHEME;
135771
+ }
135772
+ /**
135773
+ * Takes a connection string object and returns a URI string of the form:
135774
+ *
135775
+ * scheme://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[endpoint]][?options]
135776
+ * @param connectionStringObject The object that describes connection string parameters
135777
+ */
135778
+ format(connectionStringObject) {
135779
+ if (!connectionStringObject) {
135780
+ return this.scheme + "://localhost";
135781
+ }
135782
+ if (this.scheme && connectionStringObject.scheme && this.scheme !== connectionStringObject.scheme) {
135783
+ throw new Error(`Scheme not supported: ${connectionStringObject.scheme}`);
135784
+ }
135785
+ let uri = (this.scheme || connectionStringObject.scheme || ConnectionStringParser.DEFAULT_SCHEME) + "://";
135786
+ if (connectionStringObject.username) {
135787
+ uri += encodeURIComponent(connectionStringObject.username);
135788
+ // Allow empty passwords
135789
+ if (connectionStringObject.password) {
135790
+ uri += ":" + encodeURIComponent(connectionStringObject.password);
135791
+ }
135792
+ uri += "@";
135793
+ }
135794
+ uri += this._formatAddress(connectionStringObject);
135795
+ // Only put a slash when there is an endpoint
135796
+ if (connectionStringObject.endpoint) {
135797
+ uri += "/" + encodeURIComponent(connectionStringObject.endpoint);
135798
+ }
135799
+ if (connectionStringObject.options && Object.keys(connectionStringObject.options).length > 0) {
135800
+ uri += "?" + Object.keys(connectionStringObject.options)
135801
+ .map((option) => encodeURIComponent(option) + "=" + encodeURIComponent(connectionStringObject.options[option]))
135802
+ .join("&");
135803
+ }
135804
+ return uri;
135805
+ }
135806
+ /**
135807
+ * Takes a connection string URI of form:
135808
+ *
135809
+ * scheme://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[endpoint]][?options]
135810
+ *
135811
+ * and returns an object of form:
135812
+ *
135813
+ * {
135814
+ * scheme: string,
135815
+ * username?: string,
135816
+ * password?: string,
135817
+ * hosts: [ { host: string, port?: number }, ... ],
135818
+ * endpoint?: string,
135819
+ * options?: object
135820
+ * }
135821
+ *
135822
+ * Where scheme and hosts will always be present. Other fields will only be present in the result if they were
135823
+ * present in the input.
135824
+ * @param uri The connection string URI
135825
+ */
135826
+ parse(uri) {
135827
+ const connectionStringParser = new RegExp("^\\s*" + // Optional whitespace padding at the beginning of the line
135828
+ "([^:]+)://" + // Scheme (Group 1)
135829
+ "(?:([^:@,/?=&]+)(?::([^:@,/?=&]+))?@)?" + // User (Group 2) and Password (Group 3)
135830
+ "([^@/?=&]+)" + // Host address(es) (Group 4)
135831
+ "(?:/([^:@,/?=&]+)?)?" + // Endpoint (Group 5)
135832
+ "(?:\\?([^:@,/?]+)?)?" + // Options (Group 6)
135833
+ "\\s*$", // Optional whitespace padding at the end of the line
135834
+ "gi");
135835
+ const connectionStringObject = {};
135836
+ if (!uri.includes("://")) {
135837
+ throw new Error(`No scheme found in URI ${uri}`);
135838
+ }
135839
+ const tokens = connectionStringParser.exec(uri);
135840
+ if (Array.isArray(tokens)) {
135841
+ connectionStringObject.scheme = tokens[1];
135842
+ if (this.scheme && this.scheme !== connectionStringObject.scheme) {
135843
+ throw new Error(`URI must start with '${this.scheme}://'`);
135844
+ }
135845
+ connectionStringObject.username = tokens[2] ? decodeURIComponent(tokens[2]) : tokens[2];
135846
+ connectionStringObject.password = tokens[3] ? decodeURIComponent(tokens[3]) : tokens[3];
135847
+ connectionStringObject.hosts = this._parseAddress(tokens[4]);
135848
+ connectionStringObject.endpoint = tokens[5] ? decodeURIComponent(tokens[5]) : tokens[5];
135849
+ connectionStringObject.options = tokens[6] ? this._parseOptions(tokens[6]) : tokens[6];
135850
+ }
135851
+ return connectionStringObject;
135852
+ }
135853
+ /**
135854
+ * Formats the address portion of a connection string
135855
+ * @param connectionStringObject The object that describes connection string parameters
135856
+ */
135857
+ _formatAddress(connectionStringObject) {
135858
+ return connectionStringObject.hosts
135859
+ .map((address) => encodeURIComponent(address.host) + (address.port ? ":" + encodeURIComponent(address.port.toString(10)) : ""))
135860
+ .join(",");
135861
+ }
135862
+ /**
135863
+ * Parses an address
135864
+ * @param addresses The address(es) to process
135865
+ */
135866
+ _parseAddress(addresses) {
135867
+ return addresses.split(",")
135868
+ .map((address) => {
135869
+ const i = address.indexOf(":");
135870
+ return (i >= 0 ?
135871
+ { host: decodeURIComponent(address.substring(0, i)), port: +address.substring(i + 1) } :
135872
+ { host: decodeURIComponent(address) });
135873
+ });
135874
+ }
135875
+ /**
135876
+ * Parses options
135877
+ * @param options The options to process
135878
+ */
135879
+ _parseOptions(options) {
135880
+ const result = {};
135881
+ options.split("&")
135882
+ .forEach((option) => {
135883
+ const i = option.indexOf("=");
135884
+ if (i >= 0) {
135885
+ result[decodeURIComponent(option.substring(0, i))] = decodeURIComponent(option.substring(i + 1));
135886
+ }
135887
+ });
135888
+ return result;
135889
+ }
135890
+ }
135891
+ exports.ConnectionStringParser = ConnectionStringParser;
135892
+ ConnectionStringParser.DEFAULT_SCHEME = "db";
135893
+ });
135894
+
135895
+
135476
135896
  /***/ }),
135477
135897
 
135478
135898
  /***/ 10968:
@@ -354990,7 +355410,7 @@ exports.readConfigSync = readConfigSync;
354990
355410
 
354991
355411
  exports.__esModule = true;
354992
355412
  exports.DOCKER_REGISTRY = exports.PIPELINE_IMAGE_TAG = void 0;
354993
- exports.PIPELINE_IMAGE_TAG = "main-da5fc4cd" || 0;
355413
+ exports.PIPELINE_IMAGE_TAG = "main-c963a076" || 0;
354994
355414
  exports.DOCKER_REGISTRY = "git.panter.ch:5001/catladder/catladder" || 0;
354995
355415
 
354996
355416
  /***/ }),
@@ -362799,7 +363219,7 @@ module.exports = underscoreNodeF._;
362799
363219
  /***/ ((module) => {
362800
363220
 
362801
363221
  "use strict";
362802
- module.exports = JSON.parse('{"name":"@catladder/cli","description":"Panter cli tool for Kubernetes","author":"Marco Wettstein <maw@panter.ch>","homepage":"https://git.panter.ch/catladder/catladder/#readme","bugs":"https://git.panter.ch/catladder/catladder/issues","repository":{"type":"git","url":"git+https://git.panter.ch/catladder/catladder.git","directory":"cli"},"keywords":["GitLab","cloud","cli","gitlab-ci","ci/cd","pipeline","deployment","devops","automation","typescript"],"contributors":[{"name":"Marco Wettstein","email":"maw@panter.ch"},{"name":"Michael Leu","email":"leu@panter.ch"},{"name":"Luboš Matejčík","email":"lma@panter.ch"},{"name":"Kaspar Vollenweider","email":"vok@panter.ch"},{"name":"Silvan Adrian","email":"sia@panter.ch"},{"name":"Stefan Bakocs","email":"stb@panter.ch"},{"name":"Lukas Brezina","email":"brl@panter.ch"}],"license":"MIT","version":"0.0.0","scripts":{"lint":"eslint \\"src/**/*.ts\\"","lint:fix":"eslint \\"src/**/*.ts\\" --fix","test":"yarn run lint","build":"yarn build:compile && yarn build:bundle","build:bundle":"./scripts/bundle","build:compile":"yarn tsc","build:watch":"yarn tsc -w"},"bin":{"catenv":"./bin/catenv","catladder":"./bin/catladder"},"dependencies":{"ts-node":"^10.9.1"},"engines":{"node":">=12.0.0"},"devDependencies":{"@catladder/pipeline":"0.0.0","@kubernetes/client-node":"^0.16.2","@tsconfig/node14":"^1.0.1","@types/common-tags":"^1.8.0","@types/fs-extra":"^5.0.4","@types/js-yaml":"^4.0.5","@types/lodash":"^4.14.185","@types/memoizee":"^0.4.2","@types/node-fetch":"^2.1.4","@types/request":"^2.48.1","@types/tmp":"^0.2.3","@types/update-notifier":"^2.5.0","@types/vorpal":"^1.12.0","@types/websocket":"^0.0.40","@types/yargs":"^13.0.0","child-process-promise":"^2.2.1","clipboardy":"^2.0.0","command-exists-promise":"^2.0.2","common-tags":"^1.8.0","dayjs":"^1.10.4","eslint":"^8.7.0","fs-extra":"^7.0.1","gitlab":"^4.4.1","js-yaml":"^4.1.0","lodash":"^4.17.21","memoizee":"^0.4.14","node-fetch":"^2.3.0","open":"^8.4.0","prettier":"^2.5.1","tmp-promise":"^2.0.2","typescript":"^4.5.4","vorpal":"^1.12.0"}}');
363222
+ module.exports = JSON.parse('{"name":"@catladder/cli","description":"Panter cli tool for Kubernetes","author":"Marco Wettstein <maw@panter.ch>","homepage":"https://git.panter.ch/catladder/catladder/#readme","bugs":"https://git.panter.ch/catladder/catladder/issues","repository":{"type":"git","url":"git+https://git.panter.ch/catladder/catladder.git","directory":"cli"},"keywords":["GitLab","cloud","cli","gitlab-ci","ci/cd","pipeline","deployment","devops","automation","typescript"],"contributors":[{"name":"Marco Wettstein","email":"maw@panter.ch"},{"name":"Michael Leu","email":"leu@panter.ch"},{"name":"Luboš Matejčík","email":"lma@panter.ch"},{"name":"Kaspar Vollenweider","email":"vok@panter.ch"},{"name":"Silvan Adrian","email":"sia@panter.ch"},{"name":"Stefan Bakocs","email":"stb@panter.ch"},{"name":"Lukas Brezina","email":"brl@panter.ch"}],"license":"MIT","version":"0.0.0","scripts":{"lint":"eslint \\"src/**/*.ts\\"","lint:fix":"eslint \\"src/**/*.ts\\" --fix","test":"yarn run lint","build":"yarn build:compile && yarn build:bundle","build:bundle":"./scripts/bundle","build:compile":"yarn tsc","build:watch":"yarn tsc -w"},"bin":{"catenv":"./bin/catenv","catladder":"./bin/catladder"},"dependencies":{"ts-node":"^10.9.1"},"engines":{"node":">=12.0.0"},"devDependencies":{"@catladder/pipeline":"0.0.0","@kubernetes/client-node":"^0.16.2","@tsconfig/node14":"^1.0.1","@types/common-tags":"^1.8.0","@types/fs-extra":"^5.0.4","@types/js-yaml":"^4.0.5","@types/lodash":"^4.14.185","@types/memoizee":"^0.4.2","@types/node-fetch":"^2.1.4","@types/request":"^2.48.1","@types/tmp":"^0.2.3","@types/update-notifier":"^2.5.0","@types/vorpal":"^1.12.0","@types/websocket":"^0.0.40","@types/yargs":"^13.0.0","child-process-promise":"^2.2.1","clipboardy":"^2.0.0","command-exists-promise":"^2.0.2","common-tags":"^1.8.0","connection-string-parser":"^1.0.4","dayjs":"^1.10.4","eslint":"^8.7.0","fs-extra":"^7.0.1","gitlab":"^4.4.1","js-yaml":"^4.1.0","lodash":"^4.17.21","memoizee":"^0.4.14","node-fetch":"^2.3.0","open":"^8.4.0","prettier":"^2.5.1","tmp-promise":"^2.0.2","typescript":"^4.5.4","vorpal":"^1.12.0"}}');
362803
363223
 
362804
363224
  /***/ }),
362805
363225
 
@@ -0,0 +1,12 @@
1
+ declare type Opts = {
2
+ targetPassword: string;
3
+ targetPort: number;
4
+ targetUsername: string;
5
+ sourceUsername: string;
6
+ sourcePassword: string;
7
+ sourcePort: number;
8
+ sourceDbName: string;
9
+ targetDbName: string;
10
+ };
11
+ export declare const spawnCopyDb: (opts: Opts) => Promise<unknown>;
12
+ export {};
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
39
+ exports.spawnCopyDb = void 0;
40
+ var child_process_promise_1 = require("child-process-promise");
41
+ var spawnCopyDb = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
42
+ var script;
43
+ return __generator(this, function (_a) {
44
+ switch (_a.label) {
45
+ case 0:
46
+ script = createCopyDbScript(opts);
47
+ return [4 /*yield*/, (0, child_process_promise_1.spawn)(script, [], {
48
+ shell: "bash",
49
+ stdio: "inherit"
50
+ })];
51
+ case 1: return [2 /*return*/, _a.sent()];
52
+ }
53
+ });
54
+ }); };
55
+ exports.spawnCopyDb = spawnCopyDb;
56
+ var createCopyDbScript = function (_a) {
57
+ var targetPassword = _a.targetPassword, targetPort = _a.targetPort, targetUsername = _a.targetUsername, sourceUsername = _a.sourceUsername, sourcePassword = _a.sourcePassword, sourcePort = _a.sourcePort, sourceDbName = _a.sourceDbName, targetDbName = _a.targetDbName;
58
+ var targetPSQL = function (command) {
59
+ return "PGPASSWORD=".concat(targetPassword, " psql -p ").concat(targetPort, " --host=localhost --user=").concat(targetUsername, " -q ").concat(command);
60
+ };
61
+ var copyDBScript = "\n set -e\n \n \n\n dumptmp=$(mktemp /tmp/dump.XXXXXX)\n\n echo \"Dumping file to $dumptmp\"\n pg_dump --dbname=postgres://".concat(sourceUsername, ":").concat(sourcePassword, "@localhost:").concat(sourcePort, "/").concat(sourceDbName, " --no-owner --no-privileges > $dumptmp\n echo \"dump done\"\n ").concat(targetPSQL("-c 'drop database \"".concat(targetDbName, "\" WITH (FORCE)' 1> /dev/null || true")), "\n ").concat(targetPSQL("-c 'create database \"".concat(targetDbName, "\"' 1> /dev/null")), "\n echo \"Restoring dump...\"\n ").concat(targetPSQL("\"".concat(targetDbName, "\" < $dumptmp 1> /dev/null")), "\n \n\n echo \"Clean up...\"\n set +e\n rm $dumptmp\n echo \"\n\uD83D\uDC31 Done!\"\n ");
62
+ return copyDBScript;
63
+ };
64
+ //# sourceMappingURL=copyDb.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copyDb.js","sourceRoot":"","sources":["../../../src/gcloud/cloudSql/copyDb.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAA8C;AAWvC,IAAM,WAAW,GAAG,UAAO,IAAU;;;;;gBACpC,MAAM,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC;gBAEjC,qBAAM,IAAA,6BAAK,EAAC,MAAM,EAAE,EAAE,EAAE;wBAC7B,KAAK,EAAE,MAAM;wBACb,KAAK,EAAE,SAAS;qBACjB,CAAC,EAAA;oBAHF,sBAAO,SAGL,EAAC;;;KACJ,CAAC;AAPW,QAAA,WAAW,eAOtB;AACF,IAAM,kBAAkB,GAAG,UAAC,EASrB;QARL,cAAc,oBAAA,EACd,UAAU,gBAAA,EACV,cAAc,oBAAA,EACd,cAAc,oBAAA,EACd,cAAc,oBAAA,EACd,UAAU,gBAAA,EACV,YAAY,kBAAA,EACZ,YAAY,kBAAA;IAEZ,IAAM,UAAU,GAAG,UAAC,OAAe;QACjC,OAAA,qBAAc,cAAc,sBAAY,UAAU,sCAA4B,cAAc,iBAAO,OAAO,CAAE;IAA5G,CAA4G,CAAC;IAE/G,IAAM,YAAY,GAAG,gKAQa,cAAc,cAAI,cAAc,wBAAc,UAAU,cAAI,YAAY,qFAEpG,UAAU,CACV,8BAAsB,YAAY,0CAAsC,CACzE,qBACC,UAAU,CAAC,gCAAwB,YAAY,qBAAiB,CAAC,2DAEjE,UAAU,CAAC,YAAI,YAAY,+BAA2B,CAAC,qHAOxD,CAAC;IACN,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC"}