@opscompass/opscompass-cli 1.0.21 → 1.0.23

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/cliHelpers.js CHANGED
@@ -1,12 +1,14 @@
1
- const fs = require('fs-extra');
2
- const https = require('https');
1
+ const fs = require("fs-extra");
2
+ const https = require("https");
3
3
  const path = require("path");
4
- const AdmZip = require('adm-zip');
4
+ const AdmZip = require("adm-zip");
5
5
 
6
6
  let httpsProxyAgent;
7
7
  if (process.env.https_proxy || process.env.HTTPS_PROXY) {
8
8
  const createHttpsProxyAgent = require("https-proxy-agent");
9
- httpsProxyAgent = createHttpsProxyAgent(process.env.https_proxy || process.env.HTTPS_PROXY);
9
+ httpsProxyAgent = createHttpsProxyAgent(
10
+ process.env.https_proxy || process.env.HTTPS_PROXY
11
+ );
10
12
  }
11
13
 
12
14
  /**
@@ -24,7 +26,7 @@ function setArgumentArrays(argumentNames, args) {
24
26
  let capturingArgument = false;
25
27
 
26
28
  // Filter args down to array of values that match the current argument name
27
- const argumentArray = args.filter(function(value, index) {
29
+ const argumentArray = args.filter(function (value, index) {
28
30
  // The args array elements first have the argument name, and the element following is the value.
29
31
  if (value === argumentName) {
30
32
  // we want to mark this element for future deletion because it is a matching argument name,
@@ -49,7 +51,7 @@ function setArgumentArrays(argumentNames, args) {
49
51
  });
50
52
 
51
53
  // Now that we are done, we want to delete all elements that match the CLI array argument structure
52
- args = args.filter(function(value, index) {
54
+ args = args.filter(function (value, index) {
53
55
  return indexesToRemove.indexOf(index) == -1;
54
56
  });
55
57
 
@@ -85,7 +87,7 @@ function deleteFile(deleteFilePath) {
85
87
  }
86
88
 
87
89
  /**
88
- * Deletes a directory based on the passed parameters.
90
+ * Deletes a directory based on the passed parameters.
89
91
  * Recursively force deletes contents.
90
92
  * @param {string} directoryPath the path to the directory to delete
91
93
  */
@@ -113,23 +115,26 @@ async function downloadFile(downloadUrl, downloadFilePath, downloadName) {
113
115
  }
114
116
 
115
117
  // Download the file
116
- https.get(downloadUrl, getOptions, (res) => {
117
-
118
- // Open file in local filesystem
119
- const file = fs.createWriteStream(path.join(downloadFilePath, downloadName));
120
-
121
- // Write data into local file
122
- res.pipe(file);
123
-
124
- // Close the file
125
- file.on('finish', () => {
118
+ https
119
+ .get(downloadUrl, getOptions, (res) => {
120
+ // Open file in local filesystem
121
+ const file = fs.createWriteStream(
122
+ path.join(downloadFilePath, downloadName)
123
+ );
124
+
125
+ // Write data into local file
126
+ res.pipe(file);
127
+
128
+ // Close the file
129
+ file.on("finish", () => {
126
130
  file.close();
127
131
  resolve();
128
- });
129
- }).on("error", (err) => {
132
+ });
133
+ })
134
+ .on("error", (err) => {
130
135
  console.log("downloadFile failed...");
131
136
  reject(err);
132
- });
137
+ });
133
138
  });
134
139
  }
135
140
 
@@ -143,10 +148,11 @@ async function unzipFile(unzipFileLocation, unzipFileName) {
143
148
  const zipFileLocation = path.join(unzipFileLocation, unzipFileName);
144
149
  try {
145
150
  const zip = new AdmZip(zipFileLocation);
146
- zip.extractAllTo(unzipFileLocation);
151
+ // Overwrite and keepOriginalPermissions
152
+ zip.extractAllTo(unzipFileLocation, true, true);
147
153
  } catch (err) {
148
154
  deleteFile(zipFileLocation);
149
- throw("Something went wrong during unzipping.\nIf you specified a script version, confirm it is correct.\nIf the error continues, contact your OpsCompass rep.");
155
+ throw "Something went wrong during unzipping.\nIf you specified a script version, confirm it is correct.\nIf the error continues, contact your OpsCompass rep.";
150
156
  }
151
157
  }
152
158
 
@@ -160,9 +166,9 @@ function getAllFilesInFolder(dirPath) {
160
166
 
161
167
  var arrayOfFiles = arrayOfFiles || [];
162
168
 
163
- files.forEach(function(file) {
169
+ files.forEach(function (file) {
164
170
  const filePathToCheck = path.join(dirPath, file);
165
- if (fs.lstatSync(filePathToCheck).isDirectory() ) {
171
+ if (fs.lstatSync(filePathToCheck).isDirectory()) {
166
172
  const folderFiles = getAllFilesInFolder(filePathToCheck);
167
173
  arrayOfFiles = arrayOfFiles.concat(folderFiles);
168
174
  } else {
@@ -170,7 +176,7 @@ function getAllFilesInFolder(dirPath) {
170
176
  }
171
177
  });
172
178
 
173
- return arrayOfFiles
179
+ return arrayOfFiles;
174
180
  }
175
181
 
176
182
  /**
@@ -182,7 +188,7 @@ async function moveFolder(sourcePath, destinationPath) {
182
188
  try {
183
189
  await fs.move(sourcePath, destinationPath);
184
190
  } catch (err) {
185
- throw (err);
191
+ throw err;
186
192
  }
187
193
  }
188
194
 
@@ -191,7 +197,7 @@ async function moveFolder(sourcePath, destinationPath) {
191
197
  * @param {string} folderPath the path to the folder to find or create
192
198
  * @returns {string} The path to the found or created folder
193
199
  */
194
- function findOrCreateFolder(folderPath) {
200
+ function findOrCreateFolder(folderPath) {
195
201
  try {
196
202
  fs.accessSync(folderPath);
197
203
  } catch {
@@ -218,12 +224,12 @@ function canReadFile(filePath) {
218
224
 
219
225
  /**
220
226
  * Object that makes file permissions a bit more user friendly.
221
- *
227
+ *
222
228
  * mode 600: the user can read/write (and not execute)
223
229
  * but only the user can interact with the file
224
230
  */
225
231
  const FilePermissions = {
226
- userReadWrite: 600
232
+ userReadWrite: 600,
227
233
  };
228
234
 
229
235
  module.exports = {
@@ -237,5 +243,5 @@ module.exports = {
237
243
  canReadFile,
238
244
  moveFolder,
239
245
  getAllFilesInFolder,
240
- FilePermissions
246
+ FilePermissions,
241
247
  };
@@ -17,6 +17,7 @@ const {
17
17
  FilePermissions,
18
18
  getAllFilesInFolder,
19
19
  } = require("./cliHelpers.js");
20
+ const { nextTick } = require("process");
20
21
 
21
22
  const SRC_FOLDER_NAME = "src";
22
23
  const METADATA_FILE_NAME = "metadata.json";
@@ -134,12 +135,13 @@ async function downloadScriptsToHome(
134
135
  ) {
135
136
  // Ensure base scripts are downloaded/updated
136
137
  if (scriptType !== ScriptType.baseScripts) {
137
- await downloadScriptsToHome(
138
+ const baseVersion = await downloadScriptsToHome(
138
139
  ScriptType.baseScripts,
139
140
  undefined,
140
141
  companySlug,
141
142
  client
142
143
  );
144
+ console.log("Base scripts (v%s)", baseVersion);
143
145
  }
144
146
 
145
147
  // Download specific script
@@ -167,15 +169,28 @@ async function downloadScriptsToHome(
167
169
  destinationPath
168
170
  );
169
171
 
170
- // Set all files to mode 600, if they are .sh set to 700.
172
+ // File permissions should be set by the pipeline that builds the distribution bundle
173
+ // However, remove them if "other" has permissions - typically want just user and group
171
174
  const allFilePaths = getAllFilesInFolder(destinationPath);
172
175
  allFilePaths.forEach(function (filePath) {
173
- var fileExtension = path.extname(filePath);
174
-
175
- if (fileExtension === ".sh") {
176
- fs.chmodSync(filePath, 0o700);
176
+ // Skip files we don't care about ({hidden}, bat, txt, ...)
177
+ if (path.basename(filePath).match(/^\./)) {
178
+ return;
177
179
  } else {
178
- fs.chmodSync(filePath, 0o600);
180
+ var fileExtension = path.extname(filePath);
181
+ if (fileExtension.match(/bat|txt|pl/)) {
182
+ return;
183
+ }
184
+ }
185
+
186
+ var fileStat = fs.lstatSync(filePath);
187
+ if (fileStat.mode & 0o007) {
188
+ // console.error(
189
+ // "Warning: loose permissions (0o%s) on %s",
190
+ // (fileStat.mode & 0o777).toString(8),
191
+ // filePath
192
+ // );
193
+ fs.chmodSync(filePath, fileStat.mode & 0o770);
179
194
  }
180
195
  });
181
196
 
@@ -183,6 +198,9 @@ async function downloadScriptsToHome(
183
198
  await setScriptMetadata(scriptTypeFolderPath, scriptVersion);
184
199
 
185
200
  deleteFile(path.join(scriptTypeFolderPath, fileName));
201
+
202
+ // Return the version downloaded
203
+ return scriptVersion;
186
204
  }
187
205
 
188
206
  /**
@@ -51,6 +51,8 @@ async function listLocalScripts(args) {
51
51
  }
52
52
 
53
53
  if (items.length > 0) {
54
+ // Go with compact output for this display
55
+ //console.log(JSON.stringify(scriptsLocal, null, 2));
54
56
  console.log(scriptsLocal);
55
57
  } else {
56
58
  throw "Cannot list scripts. Make sure the scripts are downloaded.\nIf the issue persists, contact your OpsCompass rep.";
@@ -94,15 +96,47 @@ async function downloadScripts(args) {
94
96
  try {
95
97
  const client = await getOpsCompassClient();
96
98
 
97
- await downloadScriptsToHome(scriptType, scriptVersion, companySlug, client);
99
+ const rcvdVersion = await downloadScriptsToHome(
100
+ scriptType,
101
+ scriptVersion,
102
+ companySlug,
103
+ client
104
+ );
98
105
 
99
- console.log("Download Successful!");
106
+ console.log("Downloaded %s (v%s)", scriptType, rcvdVersion);
100
107
  } catch (err) {
101
108
  console.log("Download failed...");
102
109
  console.log(err);
103
110
  }
104
111
  }
105
112
 
113
+ /**
114
+ * Checks the available script versions from the server
115
+ */
116
+ async function listAvailableScripts(args) {
117
+ for (let i = 0; i < args.length - 1; i++) {
118
+ switch (args[i].toLowerCase()) {
119
+ case "--company":
120
+ companySlug = args[++i];
121
+ break;
122
+ }
123
+ }
124
+
125
+ try {
126
+ const client = await getOpsCompassClient();
127
+
128
+ const scriptsRemote = await client.dataGatheringScripts.getScriptVersions(
129
+ company
130
+ );
131
+ // Go with compact output for this display
132
+ //console.log(JSON.stringify(scriptsRemote, null, 2));
133
+ console.log(scriptsRemote);
134
+ } catch (err) {
135
+ console.log("List available script versions failed...");
136
+ console.log(err);
137
+ }
138
+ }
139
+
106
140
  /**
107
141
  * Checks the installed scripts version against the latest scripts version for a given scriptType.
108
142
  */
@@ -250,6 +284,11 @@ async function manuallyRunScripts() {
250
284
  async function printHelp() {
251
285
  console.log("Actions available for data-gathering-scripts:");
252
286
  console.log();
287
+ console.log(
288
+ "list-available",
289
+ "Lists all script types and versions available for download"
290
+ );
291
+ console.log();
253
292
  console.log(
254
293
  "list-local",
255
294
  "Lists all locally downloaded script types and versions"
@@ -301,6 +340,8 @@ const dataGatheringScriptsModule = (action, ...args) => {
301
340
  return downloadScripts(args);
302
341
  case "check-script-updates":
303
342
  return checkScriptUpdates(args);
343
+ case "list-available":
344
+ return listAvailableScripts(args);
304
345
  case "list-local":
305
346
  return listLocalScripts();
306
347
  case "manually-run-scripts":