@opscompass/opscompass-cli 1.3.5 → 1.3.7

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.
package/lib/utils.js CHANGED
@@ -72,8 +72,29 @@ function cleanupOldConfigBackups(dirPath, prefix, extension, maxAgeSeconds) {
72
72
  /**
73
73
  * Process standard arguments (eg company) and remove them
74
74
  * @param {string[]} args
75
+ * @returns {boolean}
75
76
  */
76
77
  function processStandardArgs(args) {
78
+ for (let i = 0; i < args.length; i++) {
79
+ if (
80
+ typeof args[i] === "string" &&
81
+ args[i].toLowerCase() === "--account"
82
+ ) {
83
+ const account = args[i + 1];
84
+ if (
85
+ account === undefined ||
86
+ account === null ||
87
+ (typeof account === "string" &&
88
+ (account.startsWith("--") || account.trim() === ""))
89
+ ) {
90
+ console.error(
91
+ "ERROR: --account is required (or set a default account).",
92
+ );
93
+ return false;
94
+ }
95
+ }
96
+ }
97
+
77
98
  // There should be global values from opscompassHome/defaults.json for these
78
99
  // - They are picked up by index.js
79
100
  // - Override them here from the command line
@@ -94,6 +115,7 @@ function processStandardArgs(args) {
94
115
  break;
95
116
  }
96
117
  }
118
+ return true;
97
119
  }
98
120
 
99
121
  /**
@@ -24,43 +24,61 @@ async function listLocalScripts(args) {
24
24
  }
25
25
 
26
26
  const scriptTypes = Object.values(ScriptType);
27
- let scriptsLocal;
27
+ const scriptsLocal = { path: undefined, versions: [] };
28
+ const failedScriptTypes = [];
28
29
 
29
30
  try {
30
31
  const baseScriptFolderPath = findOrCreateScriptsFolder();
31
- scriptsLocal = { path: baseScriptFolderPath, versions: [] };
32
+ scriptsLocal.path = baseScriptFolderPath;
32
33
  const items = fs.readdirSync(baseScriptFolderPath, { withFileTypes: true });
33
34
 
34
35
  for (const item of items) {
35
- if (scriptTypes.includes(item.name) && item.isDirectory()) {
36
- let metaFile = path.join(
37
- baseScriptFolderPath,
38
- item.name,
39
- "metadata.json",
40
- );
41
- if (fs.existsSync(metaFile)) {
42
- const data = fs.readFileSync(metaFile);
43
- const metaVersion = JSON.parse(data.toString());
44
-
45
- scriptsLocal.versions.push({
46
- scriptType: item.name,
47
- scriptVersion: metaVersion.version,
48
- });
36
+ if (!scriptTypes.includes(item.name) || !item.isDirectory()) {
37
+ continue;
38
+ }
39
+
40
+ const metaFile = path.join(
41
+ baseScriptFolderPath,
42
+ item.name,
43
+ "metadata.json",
44
+ );
45
+ if (!fs.existsSync(metaFile)) {
46
+ failedScriptTypes.push(item.name);
47
+ continue;
48
+ }
49
+
50
+ try {
51
+ const data = fs.readFileSync(metaFile);
52
+ const metaVersion = JSON.parse(data.toString());
53
+ if (!metaVersion?.version) {
54
+ failedScriptTypes.push(item.name);
55
+ continue;
49
56
  }
57
+
58
+ scriptsLocal.versions.push({
59
+ scriptType: item.name,
60
+ scriptVersion: metaVersion.version,
61
+ });
62
+ } catch (err) {
63
+ console.log(err);
64
+ failedScriptTypes.push(item.name);
50
65
  }
51
66
  }
52
67
 
53
- if (items.length <= 0) {
54
- throw "Cannot list scripts. Make sure the scripts are downloaded.\nIf the issue persists, contact your OpsCompass rep.";
55
- } else {
56
- if (!quiet) {
57
- // Go with compact output for this display
58
- // console.log(JSON.stringify(scriptsLocal, null, 2));
59
- console.log(scriptsLocal);
60
- }
68
+
69
+ if (!quiet) {
70
+ // Go with compact output for this display
71
+ // console.log(JSON.stringify(scriptsLocal, null, 2));
72
+ console.log(scriptsLocal);
61
73
  }
62
74
  } catch (err) {
63
75
  console.log(err);
76
+ scriptsLocal.success = false;
77
+ scriptsLocal.error = err;
78
+ }
79
+ if (failedScriptTypes.length > 0) {
80
+ scriptsLocal.success = false;
81
+ scriptsLocal.failedScriptTypes = failedScriptTypes;
64
82
  }
65
83
 
66
84
  return scriptsLocal;
@@ -92,7 +110,8 @@ async function downloadScripts(args) {
92
110
  const scriptTypes = Object.values(ScriptType);
93
111
  if (!scriptTypes.includes(scriptType)) {
94
112
  console.log("Unsupported script type...");
95
- return;
113
+ process.exitCode = 1;
114
+ return { success: false, scriptType };
96
115
  }
97
116
 
98
117
  try {
@@ -106,9 +125,16 @@ async function downloadScripts(args) {
106
125
  );
107
126
 
108
127
  console.log("Downloaded %s (v%s)", scriptType, rcvdVersion);
128
+ return {
129
+ success: true,
130
+ scriptType,
131
+ downloadedVersion: rcvdVersion,
132
+ };
109
133
  } catch (err) {
110
134
  console.log("Download script type %s failed...", scriptType);
111
135
  console.log(err);
136
+ process.exitCode = 1;
137
+ return { success: false, scriptType, error: err };
112
138
  }
113
139
  }
114
140
 
@@ -142,6 +168,7 @@ async function listAvailableScripts(args) {
142
168
  } catch (err) {
143
169
  console.log("List available script versions failed...");
144
170
  console.log(err);
171
+ return { success: false, error: err };
145
172
  }
146
173
  }
147
174
 
@@ -152,6 +179,21 @@ async function listAvailableScripts(args) {
152
179
  async function checkScriptUpdates(args) {
153
180
  let scriptType = undefined;
154
181
  let autoUpdate = false;
182
+ const updateResult = { success: true, failedScriptTypes: [] };
183
+ const recordUpdateFailure = (failedScriptType) => {
184
+ updateResult.success = false;
185
+ if (
186
+ failedScriptType &&
187
+ !updateResult.failedScriptTypes.includes(failedScriptType)
188
+ ) {
189
+ updateResult.failedScriptTypes.push(failedScriptType);
190
+ }
191
+ process.exitCode = 1;
192
+ return updateResult;
193
+ };
194
+ const isValidVersion = (version) =>
195
+ typeof version === "string" && semver.valid(version) !== null;
196
+
155
197
  for (let i = 0; i < args.length; i++) {
156
198
  switch (args[i].toLowerCase()) {
157
199
  case "--auto-update":
@@ -168,30 +210,58 @@ async function checkScriptUpdates(args) {
168
210
  }
169
211
  }
170
212
 
171
- const client = await getOpsCompassClient();
213
+ const scriptTypes = Object.values(ScriptType);
172
214
 
173
215
  // If script type was specified, just check that one
174
216
  if (scriptType) {
175
217
  // If script type is not supported, we do not want to continue.
176
- const scriptTypes = Object.values(ScriptType);
177
218
  if (!scriptTypes.includes(scriptType)) {
178
219
  console.log("Unsupported script type...");
179
- return;
220
+ return recordUpdateFailure(scriptType);
180
221
  }
181
222
 
182
- const currentScriptVersion = await getInstalledScriptVersion(scriptType);
223
+ let currentScriptVersion;
224
+ try {
225
+ currentScriptVersion = await getInstalledScriptVersion(scriptType);
226
+ } catch (err) {
227
+ console.log("Check local script version for type %s failed...", scriptType);
228
+ console.log(err);
229
+ return recordUpdateFailure(scriptType);
230
+ }
183
231
 
184
232
  if (!currentScriptVersion) {
185
233
  console.log(
186
234
  `Local scripts for type ${scriptType} not found. Please make sure they are downloaded.`,
187
235
  );
188
236
  console.log("If the issue persists, please contact your OpsCompass rep.");
189
- return;
237
+ return recordUpdateFailure(scriptType);
190
238
  }
191
239
 
192
- console.log("Checking for latest version of module %s...", scriptType);
193
- const latestScriptVersion =
194
- await client.dataGatheringScripts.checkScriptUpdates(company, scriptType);
240
+ if (!isValidVersion(currentScriptVersion)) {
241
+ console.log("Local script version for type %s is malformed.", scriptType);
242
+ return recordUpdateFailure(scriptType);
243
+ }
244
+
245
+ let client;
246
+ let latestScriptVersion;
247
+ try {
248
+ client = await getOpsCompassClient();
249
+ console.log("Checking for latest version of module %s...", scriptType);
250
+ latestScriptVersion =
251
+ await client.dataGatheringScripts.checkScriptUpdates(
252
+ company,
253
+ scriptType,
254
+ );
255
+ } catch (err) {
256
+ console.log("Check latest script version for type %s failed...", scriptType);
257
+ console.log(err);
258
+ return recordUpdateFailure(scriptType);
259
+ }
260
+
261
+ if (!isValidVersion(latestScriptVersion?.scriptVersion)) {
262
+ console.log("Remote script version for type %s is malformed.", scriptType);
263
+ return recordUpdateFailure(scriptType);
264
+ }
195
265
 
196
266
  //Semver returns true if the latest version is higher than the currentScriptVersion.
197
267
  if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
@@ -218,6 +288,7 @@ async function checkScriptUpdates(args) {
218
288
  } catch (err) {
219
289
  console.log("Update script type %s failed...", scriptType);
220
290
  console.log(err);
291
+ recordUpdateFailure(scriptType);
221
292
  }
222
293
  }
223
294
  console.log();
@@ -229,37 +300,98 @@ async function checkScriptUpdates(args) {
229
300
  console.log();
230
301
  }
231
302
  } else {
303
+ let client;
304
+ try {
305
+ client = await getOpsCompassClient();
306
+ } catch (err) {
307
+ console.log("Check script updates failed...");
308
+ console.log(err);
309
+ return recordUpdateFailure();
310
+ }
311
+
232
312
  const scriptsLocal = await listLocalScripts([...args, "--quiet"]);
233
313
 
234
- if (
235
- scriptsLocal &&
236
- Array.isArray(scriptsLocal?.versions) &&
237
- scriptsLocal.versions?.length > 0
238
- ) {
239
- // We have at least one scriptType downloaded
240
- } else {
314
+ if (scriptsLocal?.success === false) {
315
+ if (
316
+ Array.isArray(scriptsLocal.failedScriptTypes) &&
317
+ scriptsLocal.failedScriptTypes.length > 0
318
+ ) {
319
+ for (const failedScriptType of scriptsLocal.failedScriptTypes) {
320
+ recordUpdateFailure(failedScriptType);
321
+ }
322
+ } else {
323
+ recordUpdateFailure();
324
+ }
325
+ }
326
+
327
+ if (!scriptsLocal || !Array.isArray(scriptsLocal.versions)) {
328
+ return recordUpdateFailure();
329
+ }
330
+
331
+ // DECISION: Treat a successfully read empty recognized inventory as a normal first-run informational state, but require a successful remote listing before reporting it.
332
+ const noLocalScripts = scriptsLocal.versions.length === 0;
333
+ if (noLocalScripts) {
241
334
  console.error("\nScripts available for download:");
242
- listAvailableScripts(args);
243
- return;
335
+ } else {
336
+ // Sort by script-type
337
+ scriptsLocal.versions.sort(function (a, b) {
338
+ return a.scriptType < b.scriptType
339
+ ? -1
340
+ : a.scriptType > b.scriptType
341
+ ? 1
342
+ : 0;
343
+ });
344
+
345
+ console.log("\nCheck-Updates fetching remote versions...");
244
346
  }
245
347
 
246
- // Sort by script-type
247
- scriptsLocal.versions.sort(function (a, b) {
248
- return a.scriptType < b.scriptType
249
- ? -1
250
- : a.scriptType > b.scriptType
251
- ? 1
252
- : 0;
253
- });
348
+ const scriptsRemote = await listAvailableScripts(
349
+ noLocalScripts ? args : [...args, "--quiet"],
350
+ );
351
+
352
+ if (
353
+ scriptsRemote?.success === false ||
354
+ !Array.isArray(scriptsRemote?.versions)
355
+ ) {
356
+ if (scriptsRemote?.success !== false) {
357
+ console.log("Remote script version response was malformed.");
358
+ }
359
+ return recordUpdateFailure();
360
+ }
254
361
 
255
- console.log("\nCheck-Updates fetching remote versions...");
256
- const scriptsRemote = await listAvailableScripts([...args, "--quiet"]);
362
+ if (noLocalScripts) {
363
+ if (updateResult.success) {
364
+ updateResult.status = "no_local_scripts";
365
+ }
366
+ return updateResult;
367
+ }
257
368
 
258
369
  for (const scriptVer of scriptsLocal.versions) {
370
+ if (
371
+ !scriptVer?.scriptType ||
372
+ !isValidVersion(scriptVer.scriptVersion)
373
+ ) {
374
+ console.log(
375
+ "Local script version for type %s is malformed.",
376
+ scriptVer?.scriptType,
377
+ );
378
+ recordUpdateFailure(scriptVer?.scriptType);
379
+ continue;
380
+ }
381
+
259
382
  const remoteVer = scriptsRemote.versions.find(
260
- (version) => version.scriptType === scriptVer.scriptType,
383
+ (version) => version?.scriptType === scriptVer.scriptType,
261
384
  );
262
385
 
386
+ if (!remoteVer || !isValidVersion(remoteVer.scriptVersion)) {
387
+ console.log(
388
+ "Remote script version for type %s is missing or malformed.",
389
+ scriptVer.scriptType,
390
+ );
391
+ recordUpdateFailure(scriptVer.scriptType);
392
+ continue;
393
+ }
394
+
263
395
  const needsUpdate = semver.gt(
264
396
  remoteVer.scriptVersion,
265
397
  scriptVer.scriptVersion,
@@ -294,8 +426,12 @@ async function checkScriptUpdates(args) {
294
426
  rcvdVersion,
295
427
  );
296
428
  } catch (err) {
297
- console.log("Auto update script type %s failed...", scriptType);
429
+ console.log(
430
+ "Auto update script type %s failed...",
431
+ scriptVer.scriptType,
432
+ );
298
433
  console.log(err);
434
+ recordUpdateFailure(scriptVer.scriptType);
299
435
  }
300
436
  }
301
437
  console.log();
@@ -309,6 +445,7 @@ async function checkScriptUpdates(args) {
309
445
  }
310
446
  //console.log(scriptsRemote);
311
447
  }
448
+ return updateResult;
312
449
  }
313
450
 
314
451
  async function autoCheckScriptUpdates(args) {
@@ -28,6 +28,25 @@ const DATA_GATHERING_OUTPUT_EVENT_TYPE = Object.freeze({
28
28
  stdout: "process.stdout",
29
29
  stderr: "process.stderr",
30
30
  });
31
+ const DATA_GATHERING_CAPABILITIES = Object.freeze({
32
+ sendDg: true,
33
+ logDirectory: true,
34
+ });
35
+
36
+ // WHY: filemon runs independently from the CLI package and needs a stable runtime contract.
37
+ // DECISION: expose capabilities directly instead of making callers infer support from versions or help text.
38
+ function printDataGatheringCapabilities() {
39
+ const capabilities = {
40
+ version: 1,
41
+ sendDg: DATA_GATHERING_CAPABILITIES.sendDg,
42
+ logDirectory: DATA_GATHERING_CAPABILITIES.logDirectory,
43
+ };
44
+ console.log(JSON.stringify(capabilities));
45
+ return {
46
+ exitCode: DATA_GATHERING_EXIT_CODES.success,
47
+ capabilities,
48
+ };
49
+ }
31
50
 
32
51
  function parseDataGatheringGlobalArgs(args) {
33
52
  const options = {
@@ -555,7 +574,7 @@ async function printDataGatheringHelp(ui = createDataGatheringUi()) {
555
574
  ui.blank();
556
575
  ui.section("Actions");
557
576
  ui.item("run", "Run downloaded data-gathering scripts or emit a dry-run plan.");
558
- ui.item("list-modules", "List installed script modules visible to the CLI.");
577
+ ui.item("capabilities", "Print machine-readable data-gathering CLI capabilities.");
559
578
  ui.item("validate-install", "Check required data-gathering dependencies.");
560
579
  ui.item("setup", "Run the data-gathering setup wizard.");
561
580
  ui.item("send-dg", "Package, encrypt, and transfer DG artifacts. Plain-file batches reject duplicate basenames.");
@@ -580,6 +599,19 @@ async function printDataGatheringHelp(ui = createDataGatheringUi()) {
580
599
  ui.item("--parallel <n>", "Surface reserved for future parallel execution.");
581
600
  ui.item("--allow-unverified", "Break-glass: allow execution of unverified bundles.");
582
601
 
602
+ ui.blank();
603
+ ui.section("Send DG Parameters");
604
+ ui.item("<file> [...file]", "One or more DG artifacts. Plain files are archived, encrypted, and uploaded.");
605
+ ui.item("--metadata-type", "sql-server-database | oracle-database | vmware-vsphere | network-discovery | nutanix-prism");
606
+ ui.item("--file-type", "Required for oracle/sql-server: oracle supports ebusiness, licensing, compliance; sql-server supports licensing, compliance.");
607
+ ui.item("--destinations", "Only oc is supported; default oc.");
608
+ ui.item("--company", "Company slug; omitted only when a default company is configured.");
609
+ ui.item("--account", "Account token used for encryption; omitted only when a default account is configured.");
610
+ ui.item("--directory", "Base directory for relative file paths; default current directory.");
611
+ ui.item("--log-directory", "Directory of DG logs to include when eligible; default OpsCompass log directory.");
612
+ ui.item("Accepted archive names", ".tgz, .tar.gz, .tgz.gpg, and .tar.gz.gpg. Encrypted .tar.gz.gpg files upload with a .tgz.gpg-compatible name.");
613
+
614
+
583
615
  ui.blank();
584
616
  ui.section("Examples");
585
617
  ui.command(
@@ -594,9 +626,16 @@ async function printDataGatheringHelp(ui = createDataGatheringUi()) {
594
626
  `${cliCommand} data-gathering run --dry-run --config /path/to/dgConfig.json --module msql-license`,
595
627
  "Preview the execution plan without running collectors.",
596
628
  );
629
+ ui.command(
630
+ `${cliCommand} data-gathering send-dg /path/to/oracle-metadata.tgz.gpg --metadata-type oracle-database --file-type licensing --company <slug>`,
631
+ "Upload a pre-encrypted Oracle licensing artifact.",
632
+ );
597
633
  ui.blank();
634
+ return { exitCode: DATA_GATHERING_EXIT_CODES.success };
598
635
  }
599
636
 
637
+
638
+
600
639
  function getCompletionEventType(action) {
601
640
  return action?.toLowerCase() === "setup" ? "setup.completed" : "run.completed";
602
641
  }
@@ -615,6 +654,8 @@ async function runDataGatheringAction(action, args) {
615
654
  return await runDataGathering(remainingArgs, logger);
616
655
  case "list-modules":
617
656
  return await listModules(logger);
657
+ case "capabilities":
658
+ return printDataGatheringCapabilities();
618
659
  case "validate-install":
619
660
  return await runValidateInstallCommand(remainingArgs, logger, { ui });
620
661
  case "setup":
@@ -635,6 +676,8 @@ async function runDataGatheringAction(action, args) {
635
676
  },
636
677
  );
637
678
  case "help":
679
+ case "--help":
680
+ case "-h":
638
681
  case "":
639
682
  return printDataGatheringHelp(ui);
640
683
  default:
@@ -721,6 +764,7 @@ export {
721
764
  parsePositiveInteger,
722
765
  parseRunArgs,
723
766
  printDataGatheringHelp,
767
+ printDataGatheringCapabilities,
724
768
  readRequiredFlagValue,
725
769
  resolveTargetModules,
726
770
  runDataGathering,
@@ -250,22 +250,46 @@ async function getMssqlAssessmentResources(args) {
250
250
  }
251
251
 
252
252
  async function checkMssqlConfigMetadata(args) {
253
- for (let i = 0; i < args.length - 1; i++) {
253
+ const callParams = {};
254
+ let hasExplicitAccount = false;
255
+ for (let i = 0; i < args.length; i++) {
254
256
  switch (args[i].toLowerCase()) {
255
257
  case "--account-id":
256
258
  case "--accountid":
257
- callParams.accountId = args[++i];
259
+ hasExplicitAccount = true;
260
+ if (
261
+ args[i + 1] !== undefined &&
262
+ !args[i + 1].startsWith("--")
263
+ ) {
264
+ callParams.accountId = args[++i];
265
+ }
258
266
  break;
259
267
  case "--last-collected-before":
260
268
  case "--lastcollectedbefore":
261
- callParams.lastCollectedBefore = args[++i];
269
+ if (args[i + 1] !== undefined) {
270
+ callParams.lastCollectedBefore = args[++i];
271
+ }
262
272
  break;
263
273
  }
264
274
  }
265
275
 
276
+ const selectedAccount = hasExplicitAccount
277
+ ? callParams.accountId
278
+ : global.account;
279
+ if (selectedAccount === undefined || selectedAccount === null) {
280
+ console.error("ERROR: --account is required (or set a default account).");
281
+ return;
282
+ }
283
+
284
+ const normalizedAccount = String(selectedAccount).trim();
285
+ if (normalizedAccount === "") {
286
+ console.error("ERROR: --account is required (or set a default account).");
287
+ return;
288
+ }
289
+
266
290
  const client = await getOpsCompassClient();
267
291
  const response = await client.licenseManager.mssql.checkMssqlConfigMetadata(
268
- callParams.accountId,
292
+ normalizedAccount,
269
293
  global.company,
270
294
  callParams.lastCollectedBefore,
271
295
  );
@@ -469,6 +493,7 @@ async function getMssqlConfiguration(args) {
469
493
  }
470
494
 
471
495
  async function updateMssqlConfiguration(args) {
496
+ let callParams = {};
472
497
  for (let i = 0; i < args.length - 1; i++) {
473
498
  switch (args[i].toLowerCase()) {
474
499
  case "--id":
@@ -834,7 +859,8 @@ async function printMssqlHelp() {
834
859
  "Checks the mssql configuration metadata for a company.",
835
860
  );
836
861
  console.log("Parameters:");
837
- console.log("--accountId [string] The account id for the company.");
862
+ console.log("--account [account-id-or-cloudSubscriptionId] (optional, defaults to current account) The account ID or cloudSubscriptionId for the company.");
863
+ console.log("--account-id, --accountid [account-id-or-cloudSubscriptionId] (optional legacy aliases; override the current account)");
838
864
  console.log(`--company [company_slug] (default: ${global.company})\n`);
839
865
  console.log(
840
866
  "--lastCollectedBefore [string] (optional) Only returns records that have not been collected since the given date. Format: UTC, example: '2022-08-23T17:00:00Z'",