@zerothreatai/cli 6.0.3 → 6.0.4

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.
@@ -57,6 +57,23 @@ async function runCompose(args) {
57
57
  child.on("close", code => (code === 0 ? resolve() : reject(new Error("compose failed"))));
58
58
  });
59
59
  }
60
+ async function checkSqlSuccess() {
61
+ const containerName = 'a01-archive';
62
+ const timeout = 10 * 60 * 1000; // 10 minutes
63
+ const startTime = Date.now();
64
+ while (Date.now() - startTime < timeout) {
65
+ const containers = await docker.listContainers({ all: true, filters: { name: [containerName] } });
66
+ const container = containers.find(c => c.Names.some(n => n.includes(containerName)));
67
+ if (container && container.State === 'exited') {
68
+ const containerObj = docker.getContainer(container.Id);
69
+ const logs = await containerObj.logs({ stdout: true, stderr: true });
70
+ const logString = logs.toString();
71
+ return logString.includes('published successfully');
72
+ }
73
+ await new Promise(resolve => setTimeout(resolve, 3000));
74
+ }
75
+ return false;
76
+ }
60
77
  async function firstIgnition(licenseKey, emailId) {
61
78
  let token = '';
62
79
  const acrTokenService = new acr_token_service_1.default();
@@ -76,7 +93,10 @@ async function firstIgnition(licenseKey, emailId) {
76
93
  await runCompose(["up", "-d"]);
77
94
  //sleep for 30 seconds to allow sql server to start
78
95
  console.log("Waiting for SQL Server to start...");
79
- await new Promise(resolve => setTimeout(resolve, 30000));
96
+ const SqlSuccess = await checkSqlSuccess();
97
+ if (!SqlSuccess) {
98
+ throw new Error("Unable to complete database setup. Please try again later.");
99
+ }
80
100
  }
81
101
  catch (error) {
82
102
  if (spinner.isSpinning)
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.cleanupContainersFromCompose = cleanupContainersFromCompose;
6
7
  exports.restartService = restartService;
7
8
  const child_process_1 = require("child_process");
8
9
  const util_1 = require("util");
@@ -10,29 +11,110 @@ const license_api_service_1 = __importDefault(require("../services/license-api-s
10
11
  const app_constants_1 = require("../constants/app-constants");
11
12
  const child_process_2 = require("child_process");
12
13
  const chalk_1 = __importDefault(require("chalk"));
14
+ const ora_1 = __importDefault(require("ora"));
15
+ const fs_1 = __importDefault(require("fs"));
16
+ const path_1 = __importDefault(require("path"));
17
+ const js_yaml_1 = __importDefault(require("js-yaml"));
13
18
  const execAsync = (0, util_1.promisify)(child_process_1.exec);
14
19
  const PROJECT = "zerothreat";
15
- async function runCompose(args) {
20
+ async function cleanupContainersFromCompose(composeFilePath) {
21
+ const absPath = path_1.default.resolve(composeFilePath);
22
+ if (!fs_1.default.existsSync(absPath)) {
23
+ throw new Error(`Compose file not found: ${absPath}`);
24
+ }
25
+ const raw = fs_1.default.readFileSync(absPath, "utf8");
26
+ const compose = js_yaml_1.default.load(raw);
27
+ if (!compose.services)
28
+ return;
29
+ const containerNames = Object.values(compose.services)
30
+ .map(s => s.container_name)
31
+ .filter(Boolean);
32
+ for (const name of containerNames) {
33
+ try {
34
+ await execAsync(`docker rm -f ${name}`);
35
+ }
36
+ catch {
37
+ }
38
+ }
39
+ }
40
+ async function runCompose() {
41
+ await cleanupContainersFromCompose(app_constants_1.dockerComposeAcr);
16
42
  return new Promise((resolve, reject) => {
17
- const child = (0, child_process_2.spawn)("docker", ["compose", "-f", app_constants_1.dockerComposeAcr, "-p", PROJECT, ...args], {
18
- stdio: "inherit",
43
+ const child = (0, child_process_2.spawn)("docker", ["compose", "-f", app_constants_1.dockerComposeAcr, "up", "-d"], {
44
+ stdio: ["ignore", "ignore", "pipe"],
19
45
  });
20
46
  child.on("close", code => (code === 0 ? resolve() : reject(new Error("compose failed"))));
21
47
  });
22
48
  }
49
+ async function containerExists(name) {
50
+ try {
51
+ await execAsync(`docker inspect ${name}`);
52
+ return true;
53
+ }
54
+ catch {
55
+ return false;
56
+ }
57
+ }
23
58
  async function restartService() {
24
59
  // Start docker image a02-conduit
25
- await execAsync('docker start a02-conduit');
26
- // Call getSystemUp from license api service
60
+ const primaryContainer = 'a02-conduit';
61
+ const pCointainerExiste = await containerExists(primaryContainer);
62
+ try {
63
+ if (pCointainerExiste) {
64
+ await execAsync(`docker start ${primaryContainer}`);
65
+ }
66
+ else {
67
+ await execAsync(`docker run -d --restart unless-stopped -p 3201:3201 --name ${primaryContainer} --network zerothreat-onprem-nw -v /var/run/docker.sock:/var/run/docker.sock -v zt-license-data:/app/projects/api/administration/zt-license-db ztonpremacr-abhbbthkbyh5e8hu.azurecr.io/${primaryContainer}`);
68
+ }
69
+ await new Promise(r => setTimeout(r, 5000));
70
+ }
71
+ catch (err) {
72
+ console.log(chalk_1.default.red(err));
73
+ return;
74
+ }
75
+ const varifySpinner = (0, ora_1.default)('Verifying your system …');
76
+ const dockerUpSpinner = (0, ora_1.default)('Spinning up containers… 🐳');
27
77
  const licenseService = new license_api_service_1.default();
28
- await licenseService.getSystemUp();
78
+ varifySpinner.start();
79
+ // Call getSystemUp from license api service
80
+ try {
81
+ await licenseService.getSystemUp();
82
+ }
83
+ catch (error) {
84
+ if (varifySpinner.isSpinning)
85
+ varifySpinner.fail(chalk_1.default.red('Verification failed. Please check your system.'));
86
+ console.log(chalk_1.default.red(error));
87
+ return;
88
+ }
89
+ // verifying signature
29
90
  try {
30
91
  await licenseService.verifySignature(app_constants_1.fingerPrint);
31
- await runCompose(["up", "-d"]);
92
+ varifySpinner.succeed('System verified.');
32
93
  }
33
94
  catch (error) {
95
+ if (varifySpinner.isSpinning)
96
+ varifySpinner.fail(chalk_1.default.red('Verification failed. Please check your system.'));
34
97
  console.error(chalk_1.default.red(error));
35
98
  return;
36
99
  }
100
+ // Up the docker images
101
+ try {
102
+ const TIMEOUT_MS = 2 * 60 * 1000;
103
+ const containerTimeout = setTimeout(() => {
104
+ throw new Error('TIMEOUT: Taking more time to restart, Please try again later');
105
+ }, TIMEOUT_MS);
106
+ dockerUpSpinner.start();
107
+ await runCompose();
108
+ clearTimeout(containerTimeout);
109
+ dockerUpSpinner.succeed('Containers are up and running. 🐳🚀');
110
+ console.log(chalk_1.default.gray('➤ You can now continue using ZeroThreat on this url : '));
111
+ console.log(chalk_1.default.bold.blue('http://localhost:3203'));
112
+ }
113
+ catch (error) {
114
+ if (dockerUpSpinner.isSpinning)
115
+ dockerUpSpinner.fail(chalk_1.default.red('Container spin-up failed. 🐳💥'));
116
+ console.log(chalk_1.default.red(error));
117
+ }
118
+ return;
37
119
  }
38
120
  ;
@@ -5,11 +5,13 @@ const child_process_1 = require("child_process");
5
5
  async function executeJS(jsSource) {
6
6
  return new Promise((resolve, reject) => {
7
7
  const child = (0, child_process_1.spawn)(process.execPath, // node
8
- ["-e", jsSource], { stdio: ["ignore", "pipe", "pipe"] });
8
+ ["-"], { stdio: ["pipe", "pipe", "pipe"] });
9
9
  let stdout = "";
10
10
  let stderr = "";
11
11
  child.stdout.on("data", d => (stdout += d.toString()));
12
12
  child.stderr.on("data", d => (stderr += d.toString()));
13
+ child.stdin.write(jsSource);
14
+ child.stdin.end();
13
15
  child.on("close", code => {
14
16
  if (code !== 0) {
15
17
  reject(new Error(stderr || `JS exited with code ${code}`));
@@ -51,5 +51,5 @@ function displayLicenseTable(response) {
51
51
  console.log(chalk_1.default.bold.blue('\n📊 LICENSE DETAILS'));
52
52
  console.log(table.toString());
53
53
  console.log(chalk_1.default.gray('➤ You can now start using ZeroThreat on this url : '));
54
- console.log(chalk_1.default.bold.blue('http://localhost:5173'));
54
+ console.log(chalk_1.default.bold.blue('http://localhost:3203'));
55
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerothreatai/cli",
3
- "version": "6.0.3",
3
+ "version": "6.0.4",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "node build.js",