@opscompass/opscompass-cli 1.0.8 → 1.0.11
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/colors.js +11 -0
- package/credentials.js +16 -38
- package/environment/current.js +7 -0
- package/environment/demo.js +6 -0
- package/environment/dev.js +6 -0
- package/environment/local.js +6 -0
- package/environment/production.js +7 -0
- package/index.js +5 -0
- package/lib/cliHelpers.js +229 -0
- package/lib/homeInteraction.js +211 -0
- package/license.txt +33 -0
- package/modules/compliance.js +184 -0
- package/modules/data-gathering-scripts.js +211 -0
- package/modules/license-manager.js +617 -0
- package/modules.js +7 -0
- package/package.json +7 -4
- package/updates.js +33 -0
package/colors.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* Global color variables for console output. Note %s is where in the string (the second argument) gets injected. \x1b[0m resets the terminal color so it doesn't continue to be the chosen color anymore after this point.
|
|
2
|
+
Colors are coded with ANSI Escape Codes - Select Graphic Rendition subset. */
|
|
3
|
+
|
|
4
|
+
global.yellowText = "\x1b[33m%s\x1b[0m";
|
|
5
|
+
global.greenText = "\x1b[32m%s\x1b[0m";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
yellowText,
|
|
10
|
+
greenText
|
|
11
|
+
};
|
package/credentials.js
CHANGED
|
@@ -2,51 +2,29 @@ const fs = require("fs");
|
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const { redeemRefreshToken } = require("./oauth");
|
|
4
4
|
const { opscompassHome } = require("./environment/current.js");
|
|
5
|
+
const {
|
|
6
|
+
writeFileToHome
|
|
7
|
+
} = require("./lib/homeInteraction.js");
|
|
8
|
+
const {
|
|
9
|
+
canReadFile, FilePermissions
|
|
10
|
+
} = require("./lib/cliHelpers.js");
|
|
5
11
|
|
|
6
12
|
function canReadStoredCredentials() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
);
|
|
14
|
-
return true;
|
|
15
|
-
} catch {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
13
|
+
return canReadFile(
|
|
14
|
+
path.join(
|
|
15
|
+
opscompassHome,
|
|
16
|
+
"tokens.json"
|
|
17
|
+
)
|
|
18
|
+
);
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
async function storeCredentials(data) {
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
} catch {
|
|
25
|
-
// mode 700: only the user can interact with the directory
|
|
26
|
-
// and can do everything in that directory (read, write, list)
|
|
27
|
-
fs.mkdirSync(opscompassHome, { mode: "700" });
|
|
22
|
+
// calculate the expires_at from the expires_in
|
|
23
|
+
if (data.expires_in) {
|
|
24
|
+
data.expires_at = new Date(Date.now() + (data.expires_in * 1000));
|
|
28
25
|
}
|
|
29
26
|
|
|
30
|
-
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
try {
|
|
33
|
-
// calculate the expires_at from the expires_in
|
|
34
|
-
if (data.expires_in) {
|
|
35
|
-
data.expires_at = new Date(Date.now() + (data.expires_in * 1000));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// write the token data to the file
|
|
39
|
-
fs.writeFile(
|
|
40
|
-
path.join(opscompassHome, 'tokens.json'),
|
|
41
|
-
JSON.stringify(data),
|
|
42
|
-
// mode 600: the user can read/write (and not execute)
|
|
43
|
-
// but only the user can interact with the file
|
|
44
|
-
{ mode: "600" },
|
|
45
|
-
() => resolve());
|
|
46
|
-
} catch (ex) {
|
|
47
|
-
reject(ex);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
27
|
+
return writeFileToHome('tokens.json', data, 0o600);
|
|
50
28
|
}
|
|
51
29
|
|
|
52
30
|
async function getCredentials() {
|
package/environment/current.js
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
// production: OpsCompassClient is referenced from npm package
|
|
2
2
|
|
|
3
|
+
|
|
3
4
|
const {
|
|
4
5
|
OpsCompassClient,
|
|
6
|
+
ScriptType,
|
|
5
7
|
OAuthHandler
|
|
6
8
|
} = require("@opscompass/opscompass-api");
|
|
7
9
|
|
|
8
10
|
const path = require("path");
|
|
9
11
|
const { homedir } = require("os");
|
|
10
12
|
const opscompassHome = path.join(homedir(), '.opscompass');
|
|
13
|
+
const cliCommand = "opscompass";
|
|
11
14
|
|
|
12
15
|
const clientId = "vOakDaOv4L26f3uuI4sp3KC1Sov3nsW2";
|
|
13
16
|
// default authDomain
|
|
14
17
|
// default audience
|
|
15
18
|
const oAuthClient = new OAuthHandler(clientId);
|
|
19
|
+
const isLocal = false
|
|
16
20
|
|
|
17
21
|
module.exports = {
|
|
18
22
|
OpsCompassClient,
|
|
23
|
+
ScriptType,
|
|
19
24
|
oAuthClient,
|
|
20
25
|
opscompassHome,
|
|
26
|
+
isLocal,
|
|
27
|
+
cliCommand
|
|
21
28
|
};
|
package/environment/demo.js
CHANGED
|
@@ -2,20 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
4
|
OpsCompassClient,
|
|
5
|
+
ScriptType,
|
|
5
6
|
OAuthHandler
|
|
6
7
|
} = require("@opscompass/opscompass-api-demo");
|
|
7
8
|
|
|
8
9
|
const path = require("path");
|
|
9
10
|
const { homedir } = require("os");
|
|
10
11
|
const opscompassHome = path.join(homedir(), '.opscompassdemo');
|
|
12
|
+
const cliCommand = "opscompassdemo";
|
|
11
13
|
|
|
12
14
|
const clientId = "XOPT3HDcwhskfVjc9IMjLXV1pov79blV";
|
|
13
15
|
const authDomain = "auth-dev.opscompass.com";
|
|
14
16
|
const audience = "https://opscompass-api-demo.azure-api.net";
|
|
15
17
|
const oAuthClient = new OAuthHandler(clientId, authDomain, audience);
|
|
18
|
+
const isLocal = false
|
|
16
19
|
|
|
17
20
|
module.exports = {
|
|
18
21
|
OpsCompassClient,
|
|
22
|
+
ScriptType,
|
|
19
23
|
oAuthClient,
|
|
20
24
|
opscompassHome,
|
|
25
|
+
isLocal,
|
|
26
|
+
cliCommand
|
|
21
27
|
};
|
package/environment/dev.js
CHANGED
|
@@ -2,20 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
4
|
OpsCompassClient,
|
|
5
|
+
ScriptType,
|
|
5
6
|
OAuthHandler
|
|
6
7
|
} = require("@opscompass/opscompass-api-dev");
|
|
7
8
|
|
|
8
9
|
const path = require("path");
|
|
9
10
|
const { homedir } = require("os");
|
|
10
11
|
const opscompassHome = path.join(homedir(), '.opscompassdev');
|
|
12
|
+
const cliCommand = "opscompassdev";
|
|
11
13
|
|
|
12
14
|
const clientId = "XOPT3HDcwhskfVjc9IMjLXV1pov79blV";
|
|
13
15
|
const authDomain = "auth-dev.opscompass.com";
|
|
14
16
|
const audience = "https://fa-sampleapi-cus-dev.azurewebsites.net";
|
|
15
17
|
const oAuthClient = new OAuthHandler(clientId, authDomain, audience);
|
|
18
|
+
const isLocal = false;
|
|
16
19
|
|
|
17
20
|
module.exports = {
|
|
18
21
|
OpsCompassClient,
|
|
22
|
+
ScriptType,
|
|
19
23
|
oAuthClient,
|
|
20
24
|
opscompassHome,
|
|
25
|
+
isLocal,
|
|
26
|
+
cliCommand
|
|
21
27
|
};
|
package/environment/local.js
CHANGED
|
@@ -3,20 +3,26 @@
|
|
|
3
3
|
|
|
4
4
|
const {
|
|
5
5
|
OpsCompassClient,
|
|
6
|
+
ScriptType,
|
|
6
7
|
OAuthHandler
|
|
7
8
|
} = require("@opscompass/opscompass-api");
|
|
8
9
|
|
|
9
10
|
const path = require("path");
|
|
10
11
|
const { homedir } = require("os");
|
|
11
12
|
const opscompassHome = path.join(homedir(), '.opscompassdev');
|
|
13
|
+
const cliCommand = "opscompassdev";
|
|
12
14
|
|
|
13
15
|
const clientId = "XOPT3HDcwhskfVjc9IMjLXV1pov79blV";
|
|
14
16
|
const authDomain = "auth-dev.opscompass.com";
|
|
15
17
|
const audience = "https://fa-sampleapi-cus-dev.azurewebsites.net";
|
|
16
18
|
const oAuthClient = new OAuthHandler(clientId, authDomain, audience);
|
|
19
|
+
const isLocal = true
|
|
17
20
|
|
|
18
21
|
module.exports = {
|
|
19
22
|
OpsCompassClient,
|
|
23
|
+
ScriptType,
|
|
20
24
|
oAuthClient,
|
|
21
25
|
opscompassHome,
|
|
26
|
+
isLocal,
|
|
27
|
+
cliCommand
|
|
22
28
|
};
|
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
// production: OpsCompassClient is referenced from npm package
|
|
2
2
|
|
|
3
|
+
|
|
3
4
|
const {
|
|
4
5
|
OpsCompassClient,
|
|
6
|
+
ScriptType,
|
|
5
7
|
OAuthHandler
|
|
6
8
|
} = require("@opscompass/opscompass-api");
|
|
7
9
|
|
|
8
10
|
const path = require("path");
|
|
9
11
|
const { homedir } = require("os");
|
|
10
12
|
const opscompassHome = path.join(homedir(), '.opscompass');
|
|
13
|
+
const cliCommand = "opscompass";
|
|
11
14
|
|
|
12
15
|
const clientId = "vOakDaOv4L26f3uuI4sp3KC1Sov3nsW2";
|
|
13
16
|
// default authDomain
|
|
14
17
|
// default audience
|
|
15
18
|
const oAuthClient = new OAuthHandler(clientId);
|
|
19
|
+
const isLocal = false
|
|
16
20
|
|
|
17
21
|
module.exports = {
|
|
18
22
|
OpsCompassClient,
|
|
23
|
+
ScriptType,
|
|
19
24
|
oAuthClient,
|
|
20
25
|
opscompassHome,
|
|
26
|
+
isLocal,
|
|
27
|
+
cliCommand
|
|
21
28
|
};
|
package/index.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
const modules = require("./modules.js");
|
|
4
4
|
const handleError = require('./error.js');
|
|
5
5
|
const { canReadStoredCredentials } = require("./credentials.js");
|
|
6
|
+
const { versionCheck } = require("./updates.js");
|
|
7
|
+
const { isLocal } = require("./environment/current.js");
|
|
6
8
|
|
|
7
9
|
function requiresLogin() {
|
|
8
10
|
console.log("You aren't logged in. Please run `opscompass login` to log in.");
|
|
@@ -28,6 +30,9 @@ function help() {
|
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
(async function() {
|
|
33
|
+
if (!isLocal) {
|
|
34
|
+
versionCheck();
|
|
35
|
+
}
|
|
31
36
|
const [proc, command, commandModule, ...args] = process.argv;
|
|
32
37
|
const moduleName = commandModule?.toLowerCase() ?? "";
|
|
33
38
|
if (moduleName === "" || moduleName === "help") {
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const https = require('https');
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const AdmZip = require('adm-zip');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Users need to be able to enter array data into the CLI. They do this by submitting identical argument names with each element value.
|
|
8
|
+
* This method consolidates all data associated with the specified argument names, and creates an array object.
|
|
9
|
+
* @param {string[]} argumentNames an array of the argument names to consolidate on
|
|
10
|
+
* @param {string[]} args the raw arguments to use when consolidating the arrays
|
|
11
|
+
* @returns the arguments with the array arguments consolidated into arrays.
|
|
12
|
+
*/
|
|
13
|
+
function setArgumentArrays(argumentNames, args) {
|
|
14
|
+
const indexesToRemove = [];
|
|
15
|
+
|
|
16
|
+
// iterate over each expected array argument name
|
|
17
|
+
argumentNames.forEach((argumentName) => {
|
|
18
|
+
let capturingArgument = false;
|
|
19
|
+
|
|
20
|
+
// Filter args down to array of values that match the current argument name
|
|
21
|
+
const argumentArray = args.filter(function(value, index) {
|
|
22
|
+
// The args array elements first have the argument name, and the element following is the value.
|
|
23
|
+
if (value === argumentName) {
|
|
24
|
+
// we want to mark this element for future deletion because it is a matching argument name,
|
|
25
|
+
// and set capturingArgument to true so it captures the next element in the array
|
|
26
|
+
indexesToRemove.push(index);
|
|
27
|
+
capturingArgument = true;
|
|
28
|
+
} else if (capturingArgument) {
|
|
29
|
+
// we want to mark this value for future deletion, set capturingArgument to false (since we just want to capture one array element),
|
|
30
|
+
// and then return true to return the argument value in the end array.
|
|
31
|
+
indexesToRemove.push(index);
|
|
32
|
+
capturingArgument = false;
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Appending the original argument name and the finalized argument array
|
|
38
|
+
// to the end of the args array will prevent it from being deleted.
|
|
39
|
+
if (argumentArray.length) {
|
|
40
|
+
args.push(argumentName);
|
|
41
|
+
args.push(argumentArray);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Now that we are done, we want to delete all elements that match the CLI array argument structure
|
|
46
|
+
args = args.filter(function(value, index) {
|
|
47
|
+
return indexesToRemove.indexOf(index) == -1;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return args;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Writes provided data to the specified file.
|
|
55
|
+
* @param {string} newFilePath the path of the new file
|
|
56
|
+
* @param {Object} data the data to write to the file
|
|
57
|
+
* @param {number} mode sets the file permissions
|
|
58
|
+
* @returns when the file write is complete
|
|
59
|
+
*/
|
|
60
|
+
async function writeFile(newFilePath, data, mode) {
|
|
61
|
+
try {
|
|
62
|
+
await fs.writeFile(newFilePath, JSON.stringify(data), { mode });
|
|
63
|
+
} catch (err) {
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Deletes a file based on the passed parameters.
|
|
70
|
+
* @param {string} parentDirectoryPath the path to the parent directory of the file to delete
|
|
71
|
+
* @param {string} deleteFileName the name of the file to delete
|
|
72
|
+
*/
|
|
73
|
+
function deleteFile(deleteFilePath) {
|
|
74
|
+
try {
|
|
75
|
+
fs.unlinkSync(deleteFilePath);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.log(`Something went wrong during deletion. ${err}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Deletes a directory based on the passed parameters.
|
|
83
|
+
* Recursively force deletes contents.
|
|
84
|
+
* @param {string} directoryPath the path to the directory to delete
|
|
85
|
+
*/
|
|
86
|
+
function deleteDirectory(directoryPath) {
|
|
87
|
+
try {
|
|
88
|
+
fs.rmSync(directoryPath, { recursive: true, force: true });
|
|
89
|
+
} catch (err) {
|
|
90
|
+
console.log(`Something went wrong while deleting directory: ${err}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Downloads a file from the provided url to the given path with the given file name.
|
|
96
|
+
* @param {string} downloadUrl the download source
|
|
97
|
+
* @param {string} downloadFilePath the download destination
|
|
98
|
+
* @param {string} downloadName the downloaded file name
|
|
99
|
+
* @returns {Promise<void>} Returns once the download is complete
|
|
100
|
+
*/
|
|
101
|
+
async function downloadFile(downloadUrl, downloadFilePath, downloadName) {
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
// Download the file
|
|
104
|
+
https.get(downloadUrl, (res) => {
|
|
105
|
+
|
|
106
|
+
// Open file in local filesystem
|
|
107
|
+
const file = fs.createWriteStream(path.join(downloadFilePath, downloadName));
|
|
108
|
+
|
|
109
|
+
// Write data into local file
|
|
110
|
+
res.pipe(file);
|
|
111
|
+
|
|
112
|
+
// Close the file
|
|
113
|
+
file.on('finish', () => {
|
|
114
|
+
file.close();
|
|
115
|
+
resolve();
|
|
116
|
+
});
|
|
117
|
+
}).on("error", (err) => {
|
|
118
|
+
console.log("downloadFile failed...");
|
|
119
|
+
reject(err);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Unzips a file based on the passed parameters. Saves the unziped contents in the directory
|
|
126
|
+
* containing the .zip.
|
|
127
|
+
* @param {string} unzipFileLocation the path to the directory to store the unzipped contents
|
|
128
|
+
* @param {string} unzipFileName the name of the unzipped file
|
|
129
|
+
*/
|
|
130
|
+
async function unzipFile(unzipFileLocation, unzipFileName) {
|
|
131
|
+
const zipFileLocation = path.join(unzipFileLocation, unzipFileName);
|
|
132
|
+
try {
|
|
133
|
+
const zip = new AdmZip(zipFileLocation);
|
|
134
|
+
zip.extractAllTo(unzipFileLocation);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
deleteFile(zipFileLocation);
|
|
137
|
+
throw("Something went wrong during unzipping.\nIf you specified a script version, confirm it is correct.\nIf the error continues, contact your OpsCompass rep.");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* We need to be able to get all file paths for a given directory. This returns all file paths in all sub folders as well.
|
|
143
|
+
* @param {string} dirPath the path to the directory to return all file paths for
|
|
144
|
+
* @returns {string[]} all file paths recursively for the passed in directory path
|
|
145
|
+
*/
|
|
146
|
+
function getAllFilesInFolder(dirPath) {
|
|
147
|
+
files = fs.readdirSync(dirPath);
|
|
148
|
+
|
|
149
|
+
var arrayOfFiles = arrayOfFiles || [];
|
|
150
|
+
|
|
151
|
+
files.forEach(function(file) {
|
|
152
|
+
const filePathToCheck = path.join(dirPath, file);
|
|
153
|
+
if (fs.lstatSync(filePathToCheck).isDirectory() ) {
|
|
154
|
+
const folderFiles = getAllFilesInFolder(filePathToCheck);
|
|
155
|
+
arrayOfFiles = arrayOfFiles.concat(folderFiles);
|
|
156
|
+
} else {
|
|
157
|
+
arrayOfFiles.push(filePathToCheck);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return arrayOfFiles
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Moves a directory and contents from one location to another.
|
|
166
|
+
* @param {string} sourcePath the directory to move
|
|
167
|
+
* @param {string} destinationPath where to move the directory to
|
|
168
|
+
*/
|
|
169
|
+
async function moveFolder(sourcePath, destinationPath) {
|
|
170
|
+
try {
|
|
171
|
+
await fs.move(sourcePath, destinationPath);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
throw (err);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* See if the .opscompass(dev|demo) directory contains passed folder; if not, try to create it.
|
|
179
|
+
* @param {string} folderPath the path to the folder to find or create
|
|
180
|
+
* @returns {string} The path to the found or created folder
|
|
181
|
+
*/
|
|
182
|
+
function findOrCreateFolder(folderPath) {
|
|
183
|
+
try {
|
|
184
|
+
fs.accessSync(folderPath);
|
|
185
|
+
} catch {
|
|
186
|
+
// mode 700: only the user can interact with the directory
|
|
187
|
+
// and can do everything in that directory (read, write, list)
|
|
188
|
+
fs.mkdirSync(folderPath, { mode: "700" });
|
|
189
|
+
}
|
|
190
|
+
return path.join(folderPath);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Checks to see if the current user can read the file at the provided file path.
|
|
195
|
+
* @param {string} filePath the path to the file to check read permission
|
|
196
|
+
* @returns {boolean} if the user can read the file
|
|
197
|
+
*/
|
|
198
|
+
function canReadFile(filePath) {
|
|
199
|
+
try {
|
|
200
|
+
fs.accessSync(filePath, fs.constants.R_OK);
|
|
201
|
+
return true;
|
|
202
|
+
} catch {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Object that makes file permissions a bit more user friendly.
|
|
209
|
+
*
|
|
210
|
+
* mode 600: the user can read/write (and not execute)
|
|
211
|
+
* but only the user can interact with the file
|
|
212
|
+
*/
|
|
213
|
+
const FilePermissions = {
|
|
214
|
+
userReadWrite: 600
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
module.exports = {
|
|
218
|
+
setArgumentArrays,
|
|
219
|
+
writeFile,
|
|
220
|
+
deleteFile,
|
|
221
|
+
deleteDirectory,
|
|
222
|
+
downloadFile,
|
|
223
|
+
unzipFile,
|
|
224
|
+
findOrCreateFolder,
|
|
225
|
+
canReadFile,
|
|
226
|
+
moveFolder,
|
|
227
|
+
getAllFilesInFolder,
|
|
228
|
+
FilePermissions
|
|
229
|
+
};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
const fs = require("fs-extra");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { opscompassHome, ScriptType, OpsCompassClient } = require("../environment/current.js");
|
|
4
|
+
const {
|
|
5
|
+
findOrCreateFolder,
|
|
6
|
+
deleteDirectory,
|
|
7
|
+
downloadFile,
|
|
8
|
+
unzipFile,
|
|
9
|
+
deleteFile,
|
|
10
|
+
moveFolder,
|
|
11
|
+
writeFile,
|
|
12
|
+
canReadFile,
|
|
13
|
+
FilePermissions,
|
|
14
|
+
getAllFilesInFolder
|
|
15
|
+
} = require("./cliHelpers.js");
|
|
16
|
+
|
|
17
|
+
const SRC_FOLDER_NAME = "src";
|
|
18
|
+
const METADATA_FILE_NAME = "metadata.json";
|
|
19
|
+
const BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH = "scripts/runall.sh";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Writes provided data to the specified file in the home directory.
|
|
23
|
+
* @param {string} fileName the name of the file to write to
|
|
24
|
+
* @param {Object} data object to write to the file
|
|
25
|
+
* @param {number} mode sets the file permissions
|
|
26
|
+
* @returns when the file write is complete
|
|
27
|
+
*/
|
|
28
|
+
async function writeFileToHome(fileName, data, mode) {
|
|
29
|
+
findOrCreateFolder(opscompassHome);
|
|
30
|
+
|
|
31
|
+
// promisify the callback of fs.writeFile
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
try {
|
|
34
|
+
// write the token data to the file
|
|
35
|
+
fs.writeFile(
|
|
36
|
+
path.join(opscompassHome, fileName),
|
|
37
|
+
JSON.stringify(data),
|
|
38
|
+
{ mode: mode },
|
|
39
|
+
() => resolve());
|
|
40
|
+
} catch (ex) {
|
|
41
|
+
reject(ex);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Finds script type folder. If it doesn't exist, it will be created.
|
|
48
|
+
* @returns path to specific folder for script type
|
|
49
|
+
*/
|
|
50
|
+
function findOrCreateScriptTypeFolder(scriptType) {
|
|
51
|
+
findOrCreateScriptsFolder();
|
|
52
|
+
const folderPath = scriptTypeFolderPath(scriptType)
|
|
53
|
+
findOrCreateFolder(folderPath);
|
|
54
|
+
return folderPath;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Finds the scripts folder. If it doesn't exist, it will be created.
|
|
59
|
+
* @returns path to the scripts folder
|
|
60
|
+
*/
|
|
61
|
+
function findOrCreateScriptsFolder() {
|
|
62
|
+
findOrCreateFolder(opscompassHome);
|
|
63
|
+
return findOrCreateFolder(`${opscompassHome}/scripts`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Checks the installed script version currently installed for the specified type
|
|
68
|
+
* @param {string} scriptType the type of script to version check
|
|
69
|
+
* @returns the version installed for the specified script version
|
|
70
|
+
*/
|
|
71
|
+
async function getInstalledScriptVersion(scriptType) {
|
|
72
|
+
const scriptMetadata = await getScriptMetadata(scriptType);
|
|
73
|
+
return scriptMetadata?.version;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Fetch the contents of the script metadata file.
|
|
78
|
+
* @param {string} scriptType the type of the script to fetch metadata for
|
|
79
|
+
* @returns a metadata object
|
|
80
|
+
*/
|
|
81
|
+
async function getScriptMetadata(scriptType) {
|
|
82
|
+
const scriptsPath = findOrCreateScriptsFolder();
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const metadataPath = path.join(scriptsPath, scriptType, METADATA_FILE_NAME);
|
|
86
|
+
|
|
87
|
+
// Check to make sure scripts have been downloaded
|
|
88
|
+
if (!canReadFile(metadataPath)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const fileContents = await fs.readFile(
|
|
93
|
+
metadataPath,
|
|
94
|
+
{ encoding: "utf-8" }
|
|
95
|
+
);
|
|
96
|
+
return JSON.parse(fileContents);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
throw err;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Removes the currently installed script for the specified script type
|
|
104
|
+
* @param {string} scriptType the script type to remove
|
|
105
|
+
*/
|
|
106
|
+
function removeCurrentlyInstalledScript(scriptType) {
|
|
107
|
+
const scriptsPath = findOrCreateScriptsFolder();
|
|
108
|
+
deleteDirectory(path.join(scriptsPath, scriptType, SRC_FOLDER_NAME));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Returns folder path for passed script type.
|
|
113
|
+
* @param {string} scriptType script type to use when generating folder path
|
|
114
|
+
* @returns {string} folder path for script type
|
|
115
|
+
*/
|
|
116
|
+
function scriptTypeFolderPath(scriptType) {
|
|
117
|
+
return `${opscompassHome}/scripts/${scriptType}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* @param {string} scriptType script type to use when downloading scripts
|
|
123
|
+
* @param {string} scriptVersion script version to use when downloading scripts. If undefined, downloads latest.
|
|
124
|
+
* @param {string} companySlug company slug to use when downloading scripts
|
|
125
|
+
* @param {OpsCompassClient} client client to use to call OpsCompass api
|
|
126
|
+
*/
|
|
127
|
+
async function downloadScriptsToHome(scriptType, scriptVersion, companySlug, client) {
|
|
128
|
+
// Ensure base scripts are downloaded/updated
|
|
129
|
+
if (scriptType !== ScriptType.baseScripts) {
|
|
130
|
+
await downloadScriptsToHome(ScriptType.baseScripts, undefined, companySlug, client);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Download specific script
|
|
134
|
+
const scriptDownloadInfo = await client.dataGatheringScripts.getScriptsUrl(companySlug, scriptType, scriptVersion);
|
|
135
|
+
const scriptTypeFolderPath = findOrCreateScriptTypeFolder(scriptType);
|
|
136
|
+
const fileName = scriptDownloadInfo.fileName;
|
|
137
|
+
scriptVersion = fileName.replace(".zip", "");
|
|
138
|
+
|
|
139
|
+
removeCurrentlyInstalledScript(scriptType);
|
|
140
|
+
await downloadFile(scriptDownloadInfo.downloadUrl, scriptTypeFolderPath, fileName);
|
|
141
|
+
await unzipFile(scriptTypeFolderPath, fileName);
|
|
142
|
+
|
|
143
|
+
// Move contents from script version folder to src
|
|
144
|
+
const destinationPath = path.join(scriptTypeFolderPath, SRC_FOLDER_NAME);
|
|
145
|
+
await moveFolder(path.join(scriptTypeFolderPath, scriptVersion), destinationPath);
|
|
146
|
+
|
|
147
|
+
// Set all files to mode 600, if they are .sh set to 700.
|
|
148
|
+
const allFilePaths = getAllFilesInFolder(destinationPath);
|
|
149
|
+
allFilePaths.forEach(function(filePath) {
|
|
150
|
+
var fileExtension = path.extname(filePath);
|
|
151
|
+
|
|
152
|
+
if (fileExtension === '.sh') {
|
|
153
|
+
fs.chmodSync(filePath, 0o700);
|
|
154
|
+
} else {
|
|
155
|
+
fs.chmodSync(filePath, 0o600);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Set the script's metadata
|
|
160
|
+
await setScriptMetadata(scriptTypeFolderPath, scriptVersion);
|
|
161
|
+
|
|
162
|
+
deleteFile(path.join(scriptTypeFolderPath, fileName));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Returns boolean indicating if the script type has been downloaded previously, or if this is the first time.
|
|
167
|
+
* @param {string} scriptType the script type to check
|
|
168
|
+
* @return {boolean} indicates if script type has been previously downloaded
|
|
169
|
+
*/
|
|
170
|
+
function previousScriptsDownloaded(scriptType) {
|
|
171
|
+
const folderPath = scriptTypeFolderPath(scriptType);
|
|
172
|
+
try {
|
|
173
|
+
return fs.existsSync(folderPath);
|
|
174
|
+
} catch (err) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Creates/updates a file that contains a specific script's metadata.
|
|
181
|
+
* @param {string} scriptFolderPath path to the script folder
|
|
182
|
+
* @param {string} scriptVersion the version of the downloaded script
|
|
183
|
+
*/
|
|
184
|
+
async function setScriptMetadata(scriptFolderPath, scriptVersion) {
|
|
185
|
+
const scriptMetadata = {
|
|
186
|
+
version: scriptVersion
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const metadataFilePath = path.join(scriptFolderPath, METADATA_FILE_NAME);
|
|
190
|
+
|
|
191
|
+
// Clear out old metadata if present
|
|
192
|
+
if (canReadFile(metadataFilePath)) {
|
|
193
|
+
deleteFile(metadataFilePath);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
await writeFile(
|
|
197
|
+
metadataFilePath,
|
|
198
|
+
scriptMetadata
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
module.exports = {
|
|
203
|
+
SRC_FOLDER_NAME,
|
|
204
|
+
BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH,
|
|
205
|
+
writeFileToHome,
|
|
206
|
+
findOrCreateScriptTypeFolder,
|
|
207
|
+
getInstalledScriptVersion,
|
|
208
|
+
removeCurrentlyInstalledScript,
|
|
209
|
+
downloadScriptsToHome,
|
|
210
|
+
previousScriptsDownloaded
|
|
211
|
+
};
|