@opscompass/opscompass-cli 1.0.7 → 1.0.10

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.
@@ -0,0 +1,52 @@
1
+ const { getOpsCompassClient } = require("../opscompass.js");
2
+ const { getCredentials } = require("../credentials.js");
3
+
4
+ async function transferOracleDatabaseMetadata(args) {
5
+ const callParams = {};
6
+ for (let i = 0; i < args.length - 1; i++) {
7
+ switch (args[i].toLowerCase()) {
8
+ case "--filepath":
9
+ callParams.filePath = args[++i];
10
+ break;
11
+ case "--company":
12
+ callParams.company = args[++i];
13
+ break;
14
+ }
15
+ }
16
+
17
+
18
+ const client = await getOpsCompassClient();
19
+ const response = await client.compliance.transferOracleDatabaseMetadata(callParams.company, callParams.filePath);
20
+
21
+ console.log(JSON.stringify(response, null, 2));
22
+ }
23
+
24
+
25
+ async function printHelp() {
26
+ console.log("Actions available for compliance:");
27
+ console.log();
28
+ console.log("transfer-oracle-database-metadata", "Transfers OpsCompass Oracle Database metadata report into OpsCompass for processing");
29
+ console.log("Parameters:");
30
+ console.log("--company [company_slug]");
31
+ console.log("--filePath [path] Path of the file to transfer to OpsCompass for processing.");
32
+ console.log();
33
+ }
34
+
35
+
36
+ const complianceModule = (action, ...args) => {
37
+ switch (action?.toLowerCase() ?? "") {
38
+ case "transfer-oracle-database-metadata":
39
+ return transferOracleDatabaseMetadata(args);
40
+ case "help":
41
+ case "":
42
+ return printHelp();
43
+ default:
44
+ console.log("Unknown command:", action);
45
+ return printHelp();
46
+ }
47
+ };
48
+
49
+ complianceModule.requiresLogin = true;
50
+ complianceModule.description = "Provides access to details about resource Compliance in OpsCompass.";
51
+
52
+ module.exports = complianceModule;
@@ -0,0 +1,166 @@
1
+ const { getOpsCompassClient } = require("../opscompass.js");
2
+ const { ScriptType, cliCommand } = require("../environment/current.js");
3
+ const { downloadScriptsToHome, findOrCreateScriptTypeFolder, SRC_FOLDER_NAME, BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH, getInstalledScriptVersion } = require("../lib/homeInteraction.js");
4
+ const { canReadFile } = require("../lib/cliHelpers.js");
5
+ const semver = require("semver");
6
+ const path = require("path");
7
+ const fs = require("fs-extra");
8
+ const util = require('util');
9
+ const exec = util.promisify(require('child_process').exec);
10
+
11
+ /**
12
+ * Downloads data gathering scripts based on the given scriptType.
13
+ */
14
+ async function downloadScripts(args) {
15
+ let companySlug = undefined;
16
+ let scriptType = undefined;
17
+ let scriptVersion = undefined
18
+ for (let i = 0; i < args.length - 1; i++) {
19
+ switch (args[i].toLowerCase()) {
20
+ case "--company":
21
+ companySlug = args[++i];
22
+ break;
23
+ case "--script-type":
24
+ case "--scripttype":
25
+ scriptType = args[++i];
26
+ break;
27
+ case "--script-version":
28
+ case "--scriptversion":
29
+ scriptVersion = args[++i];
30
+ break;
31
+ }
32
+ }
33
+
34
+ // If script type is not supported, we do not want to continue.
35
+ const scriptTypes = Object.values(ScriptType);
36
+ if (!scriptTypes.includes(scriptType)) {
37
+ console.log("Unsupported script type...");
38
+ return;
39
+ }
40
+
41
+ try {
42
+ const client = await getOpsCompassClient();
43
+
44
+ await downloadScriptsToHome(scriptType, scriptVersion, companySlug, client);
45
+
46
+ console.log("Download Successful!");
47
+ } catch (err) {
48
+ console.log("Download failed...");
49
+ console.log(err);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Checks the installed scripts version against the latest scripts version for a given scriptType.
55
+ */
56
+ async function checkScriptUpdates(args) {
57
+ let companySlug = undefined;
58
+ let scriptType = undefined;
59
+ for (let i = 0; i < args.length - 1; i++) {
60
+ switch (args[i].toLowerCase()) {
61
+ case "--company":
62
+ companySlug = args[++i];
63
+ break;
64
+ case "--script-type":
65
+ case "--scripttype":
66
+ scriptType = args[++i];
67
+ break;
68
+ }
69
+ }
70
+
71
+ // If script type is not supported, we do not want to continue.
72
+ const scriptTypes = Object.values(ScriptType);
73
+ if (!scriptTypes.includes(scriptType)) {
74
+ console.log("Unsupported script type...");
75
+ return;
76
+ }
77
+
78
+ const client = await getOpsCompassClient();
79
+ const currentScriptVersion = await getInstalledScriptVersion(scriptType);
80
+
81
+ if (!currentScriptVersion) {
82
+ console.log(`Local scripts for type ${scriptType} not found. Please make sure they are installed.`);
83
+ console.log("If the issue persists, please contact your OpsCompass rep.");
84
+ return;
85
+ }
86
+
87
+ const latestScriptVersion = await client.dataGatheringScripts.checkScriptUpdates(companySlug, scriptType);
88
+
89
+ //Semver returns true if the latest version is higher than the currentScriptVersion.
90
+ if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
91
+ console.log(yellowText, `Warning! You are using version ${currentScriptVersion} of the the ${scriptType} scripts, but the latest version is ${latestScriptVersion.scriptVersion}.`);
92
+ console.log(yellowText, `To update to the latest version, please run: ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptType} `);
93
+ } else {
94
+ console.log(greenText, `You are using the latest version of ${scriptType} script, ${latestScriptVersion.scriptVersion}.`);
95
+ console.log();
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Manually runs the installed scripts. The base scripts has a runall.sh file.
101
+ * This file runs all installed modules.
102
+ */
103
+ async function manuallyRunScripts() {
104
+ try {
105
+ const baseScriptFolderPath = findOrCreateScriptTypeFolder(ScriptType.baseScripts);
106
+ const runAllFilePath = path.join(baseScriptFolderPath, SRC_FOLDER_NAME, BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH);
107
+
108
+ if (canReadFile(runAllFilePath)) {
109
+ const runAllScript = await fs.readFile(
110
+ runAllFilePath,
111
+ { encoding: "utf-8" }
112
+ );
113
+
114
+ const { stdout, stderr } = await exec(runAllScript);
115
+ if (!!stderr) {
116
+ console.log(`Error: ${stderr}`);
117
+ } else {
118
+ console.log(stdout);
119
+ }
120
+ } else {
121
+ throw "Cannot manually run scripts. Make sure the scripts are downloaded.\nIf the issue persists, contact your OpsCompass rep.";
122
+ }
123
+ } catch (err) {
124
+ console.log(err);
125
+ }
126
+ }
127
+
128
+ async function printHelp() {
129
+ console.log("Actions available for data-gathering-scripts:");
130
+ console.log();
131
+ console.log("download-scripts", "Downloads scripts for the passed script type.");
132
+ console.log("Parameters:");
133
+ console.log("--company [company_slug]");
134
+ console.log("--script-type ['oracle-cis']");
135
+ console.log("--script-version (optional) [version] if not provided, the latest script will be downloaded");
136
+ console.log();
137
+ console.log("check-script-updates", "Checks locally installed script version against the latest published version for the passed script type.");
138
+ console.log("Parameters:");
139
+ console.log("--company [company_slug]");
140
+ console.log("--script-type ['oracle-cis']");
141
+ console.log();
142
+ console.log("manually-run-scripts", "Do not run this unless you know what you are doing. This manually runs all the scripts.");
143
+ console.log();
144
+ }
145
+
146
+ const dataGatheringScriptsModule = (action, ...args) => {
147
+ switch (action?.toLowerCase() ?? "") {
148
+ case "download-scripts":
149
+ return downloadScripts(args);
150
+ case "check-script-updates":
151
+ return checkScriptUpdates(args);
152
+ case "manually-run-scripts":
153
+ return manuallyRunScripts()
154
+ case "help":
155
+ case "":
156
+ return printHelp();
157
+ default:
158
+ console.log("Unknown command:", action);
159
+ return printHelp();
160
+ }
161
+ };
162
+
163
+ dataGatheringScriptsModule.requiresLogin = true;
164
+ dataGatheringScriptsModule.description = "Provides access to OpsCompass data gathering scripts.";
165
+
166
+ module.exports = dataGatheringScriptsModule;