@opscompass/opscompass-cli 1.0.23 → 1.0.25

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/index.js CHANGED
@@ -11,29 +11,28 @@ const path = require("path");
11
11
  let defaultParams = JSON.parse("{}");
12
12
 
13
13
  function checkDefaults() {
14
- const defaultsPath = path.join(opscompassHome, 'defaults.json');
14
+ const defaultsPath = path.join(opscompassHome, "defaults.json");
15
15
  let rawdata;
16
16
  try {
17
17
  rawdata = fs.readFileSync(defaultsPath);
18
18
  defaultParams = JSON.parse(rawdata);
19
19
  } catch (err) {
20
20
  // We just want to catch the error, no logging necessary
21
- }
22
- /**
23
- * When an individual string is passed to the API, default param will be provided if it exists.
24
- * Default value is overwritten when explicitly included in CLI command.
25
- */
21
+ }
22
+ /**
23
+ * When an individual string is passed to the API, default param will be provided if it exists.
24
+ * Default value is overwritten when explicitly included in CLI command.
25
+ */
26
26
  companySlug = defaultParams.company;
27
27
  company = defaultParams.company;
28
28
  account = defaultParams.account;
29
29
  monitoringConfig = defaultParams.monitoringConfiguration;
30
30
 
31
- /**
32
- * When a callParams object is passed to the API rather than one of the above strings, default values will be used unless explicitly replaced.
31
+ /**
32
+ * When a callParams object is passed to the API rather than one of the above strings, default values will be used unless explicitly replaced.
33
33
  * Other properties can still be added to the callParams object. Unneeded default parameters may be included, but are ignored by the API.
34
- */
34
+ */
35
35
  callParams = defaultParams;
36
-
37
36
  }
38
37
 
39
38
  function requiresLogin() {
@@ -77,7 +76,11 @@ function help() {
77
76
  const module = modules[moduleName];
78
77
  if (!module.requiresLogin || loggedIn()) {
79
78
  try {
80
- await module(...args);
79
+ if (typeof module === "function") {
80
+ await module(...args);
81
+ } else {
82
+ console.log(JSON.stringify(module, null, 2));
83
+ }
81
84
  } catch (e) {
82
85
  handleError(e);
83
86
  }
@@ -19,6 +19,8 @@ const {
19
19
  } = require("./cliHelpers.js");
20
20
  const { nextTick } = require("process");
21
21
 
22
+ const semver = require("semver");
23
+
22
24
  const SRC_FOLDER_NAME = "src";
23
25
  const METADATA_FILE_NAME = "metadata.json";
24
26
  const BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH = "scripts/runall.sh";
@@ -127,21 +129,51 @@ function scriptTypeFolderPath(scriptType) {
127
129
  * @param {string} companySlug company slug to use when downloading scripts
128
130
  * @param {OpsCompassClient} client client to use to call OpsCompass api
129
131
  */
132
+ var scriptsAvailable = undefined;
130
133
  async function downloadScriptsToHome(
131
134
  scriptType,
132
135
  scriptVersion,
133
136
  companySlug,
134
137
  client
135
138
  ) {
136
- // Ensure base scripts are downloaded/updated
139
+ // Ensure base scripts are downloaded/updated for all modules
137
140
  if (scriptType !== ScriptType.baseScripts) {
138
- const baseVersion = await downloadScriptsToHome(
139
- ScriptType.baseScripts,
140
- undefined,
141
- companySlug,
142
- client
141
+ const localBaseVersion = await getInstalledScriptVersion(
142
+ ScriptType.baseScripts
143
143
  );
144
- console.log("Base scripts (v%s)", baseVersion);
144
+
145
+ // Determine what is available
146
+ if (!scriptsAvailable) {
147
+ console.log("Fetching remote versions...");
148
+ scriptsAvailable = await client.dataGatheringScripts.getScriptVersions(
149
+ companySlug
150
+ );
151
+ }
152
+
153
+ var availableBase = undefined;
154
+ if (
155
+ Array.isArray(scriptsAvailable?.versions) &&
156
+ scriptsAvailable.versions?.length > 0
157
+ ) {
158
+ availableBase = scriptsAvailable.versions.find(
159
+ (version) => version.scriptType === ScriptType.baseScripts
160
+ );
161
+ }
162
+
163
+ // If base is available and not installed or out of date
164
+ if (
165
+ availableBase &&
166
+ (!localBaseVersion ||
167
+ semver.gt(availableBase.scriptVersion, localBaseVersion))
168
+ ) {
169
+ const baseVersion = await downloadScriptsToHome(
170
+ ScriptType.baseScripts,
171
+ undefined,
172
+ companySlug,
173
+ client
174
+ );
175
+ console.log("Base scripts updated (v%s)", baseVersion);
176
+ }
145
177
  }
146
178
 
147
179
  // Download specific script
@@ -20,9 +20,17 @@ const util = require("util");
20
20
  const exec = util.promisify(require("child_process").exec);
21
21
 
22
22
  /**
23
- * Lists script types that are locally installed
23
+ * Lists script types that are locally downloaded
24
24
  */
25
25
  async function listLocalScripts(args) {
26
+ let quiet = false;
27
+ for (let i = 0; i < args.length; i++) {
28
+ switch (args[i].toLowerCase()) {
29
+ case "--quiet":
30
+ quiet = true;
31
+ }
32
+ }
33
+
26
34
  const scriptTypes = Object.values(ScriptType);
27
35
  let scriptsLocal;
28
36
 
@@ -50,16 +58,19 @@ async function listLocalScripts(args) {
50
58
  }
51
59
  }
52
60
 
53
- if (items.length > 0) {
54
- // Go with compact output for this display
55
- //console.log(JSON.stringify(scriptsLocal, null, 2));
56
- console.log(scriptsLocal);
57
- } else {
61
+ if (items.length <= 0) {
58
62
  throw "Cannot list scripts. Make sure the scripts are downloaded.\nIf the issue persists, contact your OpsCompass rep.";
63
+ } else {
64
+ if (!quiet) {
65
+ // Go with compact output for this display
66
+ // console.log(JSON.stringify(scriptsLocal, null, 2));
67
+ console.log(scriptsLocal);
68
+ }
59
69
  }
60
70
  } catch (err) {
61
71
  console.log(err);
62
72
  }
73
+
63
74
  return scriptsLocal;
64
75
  }
65
76
 
@@ -73,7 +84,7 @@ async function downloadScripts(args) {
73
84
  for (let i = 0; i < args.length - 1; i++) {
74
85
  switch (args[i].toLowerCase()) {
75
86
  case "--company":
76
- companySlug = args[++i];
87
+ company = args[++i];
77
88
  break;
78
89
  case "--script-type":
79
90
  case "--scripttype":
@@ -99,7 +110,7 @@ async function downloadScripts(args) {
99
110
  const rcvdVersion = await downloadScriptsToHome(
100
111
  scriptType,
101
112
  scriptVersion,
102
- companySlug,
113
+ company,
103
114
  client
104
115
  );
105
116
 
@@ -114,11 +125,14 @@ async function downloadScripts(args) {
114
125
  * Checks the available script versions from the server
115
126
  */
116
127
  async function listAvailableScripts(args) {
117
- for (let i = 0; i < args.length - 1; i++) {
128
+ let quiet = false;
129
+ for (let i = 0; i < args.length; i++) {
118
130
  switch (args[i].toLowerCase()) {
119
131
  case "--company":
120
- companySlug = args[++i];
132
+ company = args[++i];
121
133
  break;
134
+ case "--quiet":
135
+ quiet = true;
122
136
  }
123
137
  }
124
138
 
@@ -128,9 +142,13 @@ async function listAvailableScripts(args) {
128
142
  const scriptsRemote = await client.dataGatheringScripts.getScriptVersions(
129
143
  company
130
144
  );
131
- // Go with compact output for this display
132
- //console.log(JSON.stringify(scriptsRemote, null, 2));
133
- console.log(scriptsRemote);
145
+
146
+ if (!quiet) {
147
+ // Go with compact output for this display
148
+ //console.log(JSON.stringify(scriptsRemote, null, 2));
149
+ console.log(scriptsRemote);
150
+ }
151
+ return scriptsRemote;
134
152
  } catch (err) {
135
153
  console.log("List available script versions failed...");
136
154
  console.log(err);
@@ -138,14 +156,20 @@ async function listAvailableScripts(args) {
138
156
  }
139
157
 
140
158
  /**
141
- * Checks the installed scripts version against the latest scripts version for a given scriptType.
159
+ * Checks the downloaded scripts version against the latest scripts version for a given scriptType.
160
+ * or all scriptTypes if not specified
142
161
  */
143
162
  async function checkScriptUpdates(args) {
144
163
  let scriptType = undefined;
145
- for (let i = 0; i < args.length - 1; i++) {
164
+ let autoUpdate = false;
165
+ for (let i = 0; i < args.length; i++) {
146
166
  switch (args[i].toLowerCase()) {
167
+ case "--auto-update":
168
+ case "--autoupdate":
169
+ autoUpdate = true;
170
+ break;
147
171
  case "--company":
148
- companySlug = args[++i];
172
+ company = args[++i];
149
173
  break;
150
174
  case "--script-type":
151
175
  case "--scripttype":
@@ -154,47 +178,145 @@ async function checkScriptUpdates(args) {
154
178
  }
155
179
  }
156
180
 
157
- // If script type is not supported, we do not want to continue.
158
- const scriptTypes = Object.values(ScriptType);
159
- if (!scriptTypes.includes(scriptType)) {
160
- console.log("Unsupported script type...");
161
- return;
162
- }
163
-
164
181
  const client = await getOpsCompassClient();
165
- const currentScriptVersion = await getInstalledScriptVersion(scriptType);
166
182
 
167
- if (!currentScriptVersion) {
168
- console.log(
169
- `Local scripts for type ${scriptType} not found. Please make sure they are installed.`
170
- );
171
- console.log("If the issue persists, please contact your OpsCompass rep.");
172
- return;
173
- }
183
+ // If script type was specified, just check that one
184
+ if (scriptType) {
185
+ // If script type is not supported, we do not want to continue.
186
+ const scriptTypes = Object.values(ScriptType);
187
+ if (!scriptTypes.includes(scriptType)) {
188
+ console.log("Unsupported script type...");
189
+ return;
190
+ }
174
191
 
175
- const latestScriptVersion =
176
- await client.dataGatheringScripts.checkScriptUpdates(
177
- companySlug,
178
- scriptType
179
- );
192
+ const currentScriptVersion = await getInstalledScriptVersion(scriptType);
180
193
 
181
- //Semver returns true if the latest version is higher than the currentScriptVersion.
182
- if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
183
- console.log(
184
- yellowText,
185
- `Warning! You are using version ${currentScriptVersion} of the the ${scriptType} scripts, but the latest version is ${latestScriptVersion.scriptVersion}. To update to the latest version, please run:`
186
- );
187
- console.log(
188
- greenText,
189
- ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptType} `
190
- );
191
- console.log();
194
+ if (!currentScriptVersion) {
195
+ console.log(
196
+ `Local scripts for type ${scriptType} not found. Please make sure they are downloaded.`
197
+ );
198
+ console.log("If the issue persists, please contact your OpsCompass rep.");
199
+ return;
200
+ }
201
+
202
+ console.log("Checking for latest version of module %s...", scriptType);
203
+ const latestScriptVersion =
204
+ await client.dataGatheringScripts.checkScriptUpdates(company, scriptType);
205
+
206
+ //Semver returns true if the latest version is higher than the currentScriptVersion.
207
+ if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
208
+ console.log(
209
+ yellowText,
210
+ `Warning! You are using version ${currentScriptVersion} of the the ${scriptType} scripts, but the latest version is ${latestScriptVersion.scriptVersion}. To update to the latest version, please run:`
211
+ );
212
+ console.log(
213
+ greenText,
214
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${company} --scriptType ${scriptType} `
215
+ );
216
+ if (autoUpdate) {
217
+ console.log("Updating...");
218
+ try {
219
+ // Get the latest version
220
+ const rcvdVersion = await downloadScriptsToHome(
221
+ scriptType,
222
+ undefined,
223
+ company,
224
+ client
225
+ );
226
+
227
+ console.log("Downloaded %s (v%s)", scriptVer.scriptType, rcvdVersion);
228
+ } catch (err) {
229
+ console.log("Download failed...");
230
+ console.log(err);
231
+ }
232
+ }
233
+ console.log();
234
+ } else {
235
+ console.log(
236
+ greenText,
237
+ `You are already using the latest version of ${scriptType} script, ${latestScriptVersion.scriptVersion}.`
238
+ );
239
+ console.log();
240
+ }
192
241
  } else {
193
- console.log(
194
- greenText,
195
- `You are already using the latest version of ${scriptType} script, ${latestScriptVersion.scriptVersion}.`
196
- );
197
- console.log();
242
+ const scriptsLocal = await listLocalScripts([...args, "--quiet"]);
243
+
244
+ if (
245
+ scriptsLocal &&
246
+ Array.isArray(scriptsLocal?.versions) &&
247
+ scriptsLocal.versions?.length > 0
248
+ ) {
249
+ // We have at least one scriptType downloaded
250
+ } else {
251
+ console.error("\nScripts available for download:");
252
+ listAvailableScripts(args);
253
+ return;
254
+ }
255
+
256
+ // Sort by script-type
257
+ scriptsLocal.versions.sort(function (a, b) {
258
+ return a.scriptType < b.scriptType
259
+ ? -1
260
+ : a.scriptType > b.scriptType
261
+ ? 1
262
+ : 0;
263
+ });
264
+
265
+ console.log("\nFetching remote versions...");
266
+ const scriptsRemote = await listAvailableScripts([...args, "--quiet"]);
267
+
268
+ for (const scriptVer of scriptsLocal.versions) {
269
+ const remoteVer = scriptsRemote.versions.find(
270
+ (version) => version.scriptType === scriptVer.scriptType
271
+ );
272
+
273
+ const needsUpdate = semver.gt(
274
+ remoteVer.scriptVersion,
275
+ scriptVer.scriptVersion
276
+ );
277
+
278
+ if (needsUpdate) {
279
+ console.log(
280
+ "\nmodule %s: Local=%s, needs update=%s",
281
+ scriptVer.scriptType,
282
+ scriptVer.scriptVersion,
283
+ remoteVer.scriptVersion
284
+ );
285
+ console.log(
286
+ greenText,
287
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${company} --scriptType ${scriptVer.scriptType} \n`
288
+ );
289
+ if (autoUpdate) {
290
+ console.log("Updating...");
291
+ try {
292
+ // Get the latest version
293
+ const rcvdVersion = await downloadScriptsToHome(
294
+ scriptVer.scriptType,
295
+ undefined,
296
+ company,
297
+ client
298
+ );
299
+
300
+ console.log(
301
+ "Downloaded %s (v%s)",
302
+ scriptVer.scriptType,
303
+ rcvdVersion
304
+ );
305
+ } catch (err) {
306
+ console.log("Download failed...");
307
+ console.log(err);
308
+ }
309
+ }
310
+ console.log();
311
+ } else {
312
+ console.log(
313
+ "\nmodule %s: Local=%s, is up to date",
314
+ scriptVer.scriptType,
315
+ scriptVer.scriptVersion
316
+ );
317
+ }
318
+ }
319
+ //console.log(scriptsRemote);
198
320
  }
199
321
  }
200
322
 
@@ -203,7 +325,7 @@ async function autoCheckScriptUpdates(args) {
203
325
  for (let i = 0; i < args.length - 1; i++) {
204
326
  switch (args[i].toLowerCase()) {
205
327
  case "--company":
206
- companySlug = args[++i];
328
+ company = args[++i];
207
329
  break;
208
330
  case "--script-type":
209
331
  case "--scripttype":
@@ -220,17 +342,14 @@ async function autoCheckScriptUpdates(args) {
220
342
  const currentScriptVersion = await getInstalledScriptVersion(scriptType);
221
343
  if (!currentScriptVersion) {
222
344
  console.log(
223
- `Local scripts for type ${scriptType} not found. Please make sure they are installed.`
345
+ `Local scripts for type ${scriptType} not found. Please make sure they are downloaded.`
224
346
  );
225
347
  console.log("If the issue persists, please contact your OpsCompass rep.");
226
348
  return;
227
349
  }
228
350
 
229
351
  const latestScriptVersion =
230
- await client.dataGatheringScripts.checkScriptUpdates(
231
- companySlug,
232
- scriptType
233
- );
352
+ await client.dataGatheringScripts.checkScriptUpdates(company, scriptType);
234
353
 
235
354
  //Semver returns true if the latest version is higher than the currentScriptVersion.
236
355
  if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
@@ -241,15 +360,15 @@ async function autoCheckScriptUpdates(args) {
241
360
  console.log();
242
361
  console.log(
243
362
  greenText,
244
- ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptType} `
363
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${company} --scriptType ${scriptType} `
245
364
  );
246
365
  console.log();
247
366
  }
248
367
  }
249
368
 
250
369
  /**
251
- * Manually runs the installed scripts. The base scripts has a runall.sh file.
252
- * This file runs all installed modules.
370
+ * Manually runs the configured scripts. The base scripts has a runall.sh file.
371
+ * The runall.sh file runs all configured modules.
253
372
  */
254
373
  async function manuallyRunScripts() {
255
374
  try {
@@ -288,11 +407,16 @@ async function printHelp() {
288
407
  "list-available",
289
408
  "Lists all script types and versions available for download"
290
409
  );
410
+ console.log("Parameters:");
411
+ console.log("--company [company_slug]");
412
+ console.log("--quiet does not generate output on stdout");
291
413
  console.log();
292
414
  console.log(
293
415
  "list-local",
294
416
  "Lists all locally downloaded script types and versions"
295
417
  );
418
+ console.log("Parameters:");
419
+ console.log("--quiet does not generate output on stdout");
296
420
  console.log();
297
421
  console.log(
298
422
  "download-scripts",
@@ -309,9 +433,10 @@ async function printHelp() {
309
433
  console.log();
310
434
  console.log(
311
435
  "check-script-updates",
312
- "Checks locally installed script version against the latest published version for the passed script type."
436
+ "Checks local script version against the latest published version for the passed script type, or all locally downloaded script types if none specified"
313
437
  );
314
438
  console.log("Parameters:");
439
+ console.log("--auto-update automatically downloads any needed updates");
315
440
  console.log("--company [company_slug]");
316
441
  console.log(
317
442
  "--script-type ['oracle-compliance', 'oracle-features', 'vsphere', 'msql-license', 'msql-compliance']"
@@ -343,7 +468,7 @@ const dataGatheringScriptsModule = (action, ...args) => {
343
468
  case "list-available":
344
469
  return listAvailableScripts(args);
345
470
  case "list-local":
346
- return listLocalScripts();
471
+ return listLocalScripts(args);
347
472
  case "manually-run-scripts":
348
473
  return manuallyRunScripts();
349
474
  case "help":
@@ -622,7 +622,7 @@ async function getMssqlEntitlements(args) {
622
622
 
623
623
  async function createOracleDatabaseConfiguration(args) {
624
624
  let name = undefined;
625
- let optionalParams = { };
625
+ let optionalParams = {};
626
626
  for (let i = 0; i < args.length - 1; i++) {
627
627
  switch (args[i].toLowerCase()) {
628
628
  case "--monitoringconfigurationid":
@@ -674,6 +674,14 @@ async function updateMonitoringConfiguration(args) {
674
674
  case "--configuration-name":
675
675
  callParams.configurationName = args[++i];
676
676
  break;
677
+ case "--autoupdateconfigurations":
678
+ case "--auto-update-configurations":
679
+ callParams.autoUpdateConfigurations = args[++i];
680
+ break;
681
+ case "--autoupdatescripts":
682
+ case "--auto-update-scripts":
683
+ callParams.autoUpdateScripts = args[++i];
684
+ break;
677
685
  case "--datafileretaindays":
678
686
  callParams.dataFileRetainDays = args[++i];
679
687
  break;
@@ -1601,6 +1609,8 @@ async function printHelp() {
1601
1609
  console.log("Parameters:");
1602
1610
  console.log("--id [monitoring_configuration id]");
1603
1611
  console.log("--name or --configuration-name (optional) [unique name]");
1612
+ console.log("--autoUpdateConfigurations (optional) [true | false]");
1613
+ console.log("--autoUpdateScripts (optional) [true | false]");
1604
1614
  console.log(
1605
1615
  "--dataFileRetainDays (optional) [determines how many days to retain encrypted output files locally]"
1606
1616
  );
package/modules.js CHANGED
@@ -2,9 +2,19 @@ const resources = require("./modules/resources.js");
2
2
  const accounts = require("./modules/accounts.js");
3
3
  const licenseManager = require("./modules/license-manager.js");
4
4
  const compliance = require("./modules/compliance.js");
5
- const dataGatheringScripts = require("./modules/data-gathering-scripts.js");
6
5
  const defaults = require("./modules/defaults.js");
7
6
 
7
+ // Dynaimcally loaded modules - may be optional
8
+ const fs = require("fs");
9
+ const path = require("path");
10
+ const dataGatheringScripts = fs.existsSync(
11
+ path.join(__dirname, "modules/data-gathering-scripts.js")
12
+ )
13
+ ? require("./modules/data-gathering-scripts.js")
14
+ : {
15
+ requiresLogin: false,
16
+ description: "removed for security",
17
+ };
8
18
 
9
19
  /* login acts like a module, even though it's special */
10
20
  const login = require("./login.js");
@@ -14,7 +24,7 @@ module.exports = {
14
24
  resources,
15
25
  accounts,
16
26
  compliance,
17
- defaults,
27
+ defaults,
18
28
  "license-manager": licenseManager,
19
- "data-gathering-scripts": dataGatheringScripts
29
+ "data-gathering-scripts": dataGatheringScripts,
20
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opscompass/opscompass-cli",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "description": "Calls OpsCompass's APIs",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,7 +15,7 @@
15
15
  "author": "opscompass",
16
16
  "license": "SEE LICENSE IN license.txt",
17
17
  "dependencies": {
18
- "@opscompass/opscompass-api": "^1.0.23",
18
+ "@opscompass/opscompass-api": "^1.0.25",
19
19
  "adm-zip": "^0.5.9",
20
20
  "axios": "^0.21.1",
21
21
  "fs-extra": "^10.1.0",