@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.
- 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 +205 -0
- package/lib/homeInteraction.js +182 -0
- package/license.txt +33 -0
- package/modules/compliance.js +52 -0
- package/modules/data-gathering-scripts.js +166 -0
- package/modules/license-manager.js +607 -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);
|
|
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,205 @@
|
|
|
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
|
+
* Moves a directory and contents from one location to another.
|
|
143
|
+
* @param {string} sourcePath the directory to move
|
|
144
|
+
* @param {string} destinationPath where to move the directory to
|
|
145
|
+
*/
|
|
146
|
+
async function moveFolder(sourcePath, destinationPath) {
|
|
147
|
+
try {
|
|
148
|
+
await fs.move(sourcePath, destinationPath);
|
|
149
|
+
} catch (err) {
|
|
150
|
+
throw (err);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* See if the .opscompass(dev|demo) directory contains passed folder; if not, try to create it.
|
|
156
|
+
* @param {string} folderPath the path to the folder to find or create
|
|
157
|
+
* @returns {string} The path to the found or created folder
|
|
158
|
+
*/
|
|
159
|
+
function findOrCreateFolder(folderPath) {
|
|
160
|
+
try {
|
|
161
|
+
fs.accessSync(folderPath);
|
|
162
|
+
} catch {
|
|
163
|
+
// mode 700: only the user can interact with the directory
|
|
164
|
+
// and can do everything in that directory (read, write, list)
|
|
165
|
+
fs.mkdirSync(folderPath, { mode: "700" });
|
|
166
|
+
}
|
|
167
|
+
return path.join(folderPath);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Checks to see if the current user can read the file at the provided file path.
|
|
172
|
+
* @param {string} filePath the path to the file to check read permission
|
|
173
|
+
* @returns {boolean} if the user can read the file
|
|
174
|
+
*/
|
|
175
|
+
function canReadFile(filePath) {
|
|
176
|
+
try {
|
|
177
|
+
fs.accessSync(filePath, fs.constants.R_OK);
|
|
178
|
+
return true;
|
|
179
|
+
} catch {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Object that makes file permissions a bit more user friendly.
|
|
186
|
+
*
|
|
187
|
+
* mode 600: the user can read/write (and not execute)
|
|
188
|
+
* but only the user can interact with the file
|
|
189
|
+
*/
|
|
190
|
+
const FilePermissions = {
|
|
191
|
+
userReadWrite: 600
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
module.exports = {
|
|
195
|
+
setArgumentArrays,
|
|
196
|
+
writeFile,
|
|
197
|
+
deleteFile,
|
|
198
|
+
deleteDirectory,
|
|
199
|
+
downloadFile,
|
|
200
|
+
unzipFile,
|
|
201
|
+
findOrCreateFolder,
|
|
202
|
+
canReadFile,
|
|
203
|
+
moveFolder,
|
|
204
|
+
FilePermissions
|
|
205
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
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
|
+
} = require("./cliHelpers.js");
|
|
15
|
+
|
|
16
|
+
const SRC_FOLDER_NAME = "src";
|
|
17
|
+
const METADATA_FILE_NAME = "metadata.json";
|
|
18
|
+
const BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH = "scripts/runall.sh";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Writes provided data to the specified file in the home directory.
|
|
22
|
+
* @param {string} fileName the name of the file to write to
|
|
23
|
+
* @param {Object} data object to write to the file
|
|
24
|
+
* @param {number} mode sets the file permissions
|
|
25
|
+
* @returns when the file write is complete
|
|
26
|
+
*/
|
|
27
|
+
async function writeFileToHome(fileName, data, mode) {
|
|
28
|
+
findOrCreateFolder(opscompassHome);
|
|
29
|
+
|
|
30
|
+
// promisify the callback of fs.writeFile
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
try {
|
|
33
|
+
// write the token data to the file
|
|
34
|
+
fs.writeFile(
|
|
35
|
+
path.join(opscompassHome, fileName),
|
|
36
|
+
JSON.stringify(data),
|
|
37
|
+
{ mode: mode },
|
|
38
|
+
() => resolve());
|
|
39
|
+
} catch (ex) {
|
|
40
|
+
reject(ex);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Finds script type folder. If it doesn't exist, it will be created.
|
|
47
|
+
* @returns path to specific folder for script type
|
|
48
|
+
*/
|
|
49
|
+
function findOrCreateScriptTypeFolder(scriptType) {
|
|
50
|
+
findOrCreateScriptsFolder();
|
|
51
|
+
const folderPath = scriptTypeFolderPath(scriptType)
|
|
52
|
+
findOrCreateFolder(folderPath);
|
|
53
|
+
return folderPath;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Finds the scripts folder. If it doesn't exist, it will be created.
|
|
58
|
+
* @returns path to the scripts folder
|
|
59
|
+
*/
|
|
60
|
+
function findOrCreateScriptsFolder() {
|
|
61
|
+
findOrCreateFolder(opscompassHome);
|
|
62
|
+
return findOrCreateFolder(`${opscompassHome}/scripts`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Checks the installed script version currently installed for the specified type
|
|
67
|
+
* @param {string} scriptType the type of script to version check
|
|
68
|
+
* @returns the version installed for the specified script version
|
|
69
|
+
*/
|
|
70
|
+
async function getInstalledScriptVersion(scriptType) {
|
|
71
|
+
const scriptMetadata = await getScriptMetadata(scriptType);
|
|
72
|
+
return scriptMetadata?.version;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Fetch the contents of the script metadata file.
|
|
77
|
+
* @param {string} scriptType the type of the script to fetch metadata for
|
|
78
|
+
* @returns a metadata object
|
|
79
|
+
*/
|
|
80
|
+
async function getScriptMetadata(scriptType) {
|
|
81
|
+
const scriptsPath = findOrCreateScriptsFolder();
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const metadataPath = path.join(scriptsPath, scriptType, METADATA_FILE_NAME);
|
|
85
|
+
|
|
86
|
+
// Check to make sure scripts have been downloaded
|
|
87
|
+
if (!canReadFile(metadataPath)) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const fileContents = await fs.readFile(
|
|
92
|
+
metadataPath,
|
|
93
|
+
{ encoding: "utf-8" }
|
|
94
|
+
);
|
|
95
|
+
return JSON.parse(fileContents);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Removes the currently installed script for the specified script type
|
|
103
|
+
* @param {string} scriptType the script type to remove
|
|
104
|
+
*/
|
|
105
|
+
function removeCurrentlyInstalledScript(scriptType) {
|
|
106
|
+
const scriptsPath = findOrCreateScriptsFolder();
|
|
107
|
+
deleteDirectory(path.join(scriptsPath, scriptType, SRC_FOLDER_NAME));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Returns folder path for passed script type.
|
|
112
|
+
* @param {string} scriptType script type to use when generating folder path
|
|
113
|
+
* @returns {string} folder path for script type
|
|
114
|
+
*/
|
|
115
|
+
function scriptTypeFolderPath(scriptType) {
|
|
116
|
+
return `${opscompassHome}/scripts/${scriptType}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
*
|
|
121
|
+
* @param {string} scriptType script type to use when downloading scripts
|
|
122
|
+
* @param {string} scriptVersion script version to use when downloading scripts. If undefined, downloads latest.
|
|
123
|
+
* @param {string} companySlug company slug to use when downloading scripts
|
|
124
|
+
* @param {OpsCompassClient} client client to use to call OpsCompass api
|
|
125
|
+
*/
|
|
126
|
+
async function downloadScriptsToHome(scriptType, scriptVersion, companySlug, client) {
|
|
127
|
+
// Ensure base scripts are downloaded/updated
|
|
128
|
+
if (scriptType !== ScriptType.baseScripts) {
|
|
129
|
+
await downloadScriptsToHome(ScriptType.baseScripts, undefined, companySlug, client);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Download specific script
|
|
133
|
+
const scriptDownloadInfo = await client.dataGatheringScripts.getScriptsUrl(companySlug, scriptType, scriptVersion);
|
|
134
|
+
const scriptTypeFolderPath = findOrCreateScriptTypeFolder(scriptType);
|
|
135
|
+
const fileName = scriptDownloadInfo.fileName;
|
|
136
|
+
scriptVersion = fileName.replace(".zip", "");
|
|
137
|
+
|
|
138
|
+
removeCurrentlyInstalledScript(scriptType);
|
|
139
|
+
await downloadFile(scriptDownloadInfo.downloadUrl, scriptTypeFolderPath, fileName);
|
|
140
|
+
await unzipFile(scriptTypeFolderPath, fileName);
|
|
141
|
+
|
|
142
|
+
// Move contents from script version folder to src
|
|
143
|
+
await moveFolder(path.join(scriptTypeFolderPath, scriptVersion), path.join(scriptTypeFolderPath, SRC_FOLDER_NAME));
|
|
144
|
+
|
|
145
|
+
// Set the script's metadata
|
|
146
|
+
await setScriptMetadata(scriptTypeFolderPath, scriptVersion);
|
|
147
|
+
|
|
148
|
+
deleteFile(path.join(scriptTypeFolderPath, fileName));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Creates/updates a file that contains a specific script's metadata.
|
|
153
|
+
* @param {string} scriptFolderPath path to the script folder
|
|
154
|
+
* @param {string} scriptVersion the version of the downloaded script
|
|
155
|
+
*/
|
|
156
|
+
async function setScriptMetadata(scriptFolderPath, scriptVersion) {
|
|
157
|
+
const scriptMetadata = {
|
|
158
|
+
version: scriptVersion
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const metadataFilePath = path.join(scriptFolderPath, METADATA_FILE_NAME);
|
|
162
|
+
|
|
163
|
+
// Clear out old metadata if present
|
|
164
|
+
if (canReadFile(metadataFilePath)) {
|
|
165
|
+
deleteFile(metadataFilePath);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
await writeFile(
|
|
169
|
+
metadataFilePath,
|
|
170
|
+
scriptMetadata
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
module.exports = {
|
|
175
|
+
SRC_FOLDER_NAME,
|
|
176
|
+
BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH,
|
|
177
|
+
writeFileToHome,
|
|
178
|
+
findOrCreateScriptTypeFolder,
|
|
179
|
+
getInstalledScriptVersion,
|
|
180
|
+
removeCurrentlyInstalledScript,
|
|
181
|
+
downloadScriptsToHome
|
|
182
|
+
};
|
package/license.txt
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
OPCOMPASS™ COMMAND LINE INTERFACE
|
|
2
|
+
LICENSE AGREEMENT
|
|
3
|
+
|
|
4
|
+
IMPORTANT! READ CAREFULLY: THIS IS A LEGAL AGREEMENT. BY DOWNLOADING, INSTALLING, COPYING, SAVING ON YOUR COMPUTER, OR OTHERWISE USING THIS TOOL, YOU (LICENSEE, AS DEFINED BELOW) ARE BECOMING A PARTY TO THIS AGREEMENT AND YOU ARE CONSENTING TO BE BOUND BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
|
|
5
|
+
IF YOU DO NOT AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT, YOU SHOULD NOT DOWNLOAD, INSTALL, OR USE THE TOOL.
|
|
6
|
+
|
|
7
|
+
1. PARTIES
|
|
8
|
+
(a) "Licensor" means OpsCompass, Inc., having its principal place of business at 11422 Miracle Hills Dr #300, Omaha, NE 68154.
|
|
9
|
+
(b) "Licensee" means an individual or a legal entity entering into and exercising rights under this Agreement or future versions thereof. Licensee is also referred to herein as "You".
|
|
10
|
+
|
|
11
|
+
2. DEFINITIONS
|
|
12
|
+
"Tool" means the software program known as the OpsCompass™ Command Line Interface in its delivered form, including its source code and documentation (if any).
|
|
13
|
+
|
|
14
|
+
3. OWNERSHIP
|
|
15
|
+
The Tool is the property of Licensor. The Tool is licensed, not sold. Title and copyrights to the Tool, in whole and in part and all copies thereof, and all modifications, enhancements, derivatives and other alterations of the Tool regardless of who made any modifications, if any, are, and will remain, the sole and exclusive property of Licensor.
|
|
16
|
+
|
|
17
|
+
4. GRANT OF LICENSE
|
|
18
|
+
Subject to the terms, conditions, and limitations set forth in this Agreement, including any amendments thereto, Licensor hereby grants to Licensee a limited, non-exclusive, perpetual license to install and use the Tool in accordance with the Tool documentation and pursuant to the terms and conditions of this Agreement.
|
|
19
|
+
|
|
20
|
+
5. FEEDBACK
|
|
21
|
+
Licensee may, but is not obligated to, provide to Licensor or its subsidiaries suggestions, comments and feedback regarding the Tool, including but not limited to usability, bug reports and test results (collectively, "Feedback"). All right, title, and interest in the Feedback shall belong to Licensor for its unlimited use and Licensee agrees to perform any reasonable tasks necessary to document Licensor’s ownership in the same.
|
|
22
|
+
|
|
23
|
+
6. NO WARRANTY
|
|
24
|
+
THE TOOL IS PROVIDED TO LICENSEE "AS IS" AND WITHOUT WARRANTIES. LICENSOR MAKES NO WARRANTY AS TO ITS USE OR PERFORMANCE. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, LICENSOR AND ITS AFFILIATES, SUPPLIERS, AND RESELLERS, DISCLAIM ALL OTHER WARRANTIES AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE TOOL, AND THE PROVISION OF OR FAILURE TO PROVIDE SUPPORT SERVICES.
|
|
25
|
+
|
|
26
|
+
7. DISCLAIMER OF DAMAGES
|
|
27
|
+
(a) TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR OR ITS AFFILIATES, LICENSORS, SUPPLIERS OR RESELLERS BE LIABLE TO LICENSEE UNDER ANY THEORY FOR ANY DAMAGES SUFFERED BY LICENSEE OR ANY USER OF THE TOOL, OR FOR ANY SPECIAL, INCIDENTAL, INDIRECT, CONSEQUENTIAL, OR SIMILAR DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS OR CONFIDENTIAL OR OTHER INFORMATION, FOR BUSINESS INTERRUPTION, FOR PERSONAL INJURY, FOR LOSS OF PRIVACY, FOR FAILURE TO MEET ANY DUTY INCLUDING OF GOOD FAITH OR OF REASONABLE CARE, FOR NEGLIGENCE, AND FOR ANY OTHER PECUNIARY OR OTHER LOSS WHATSOEVER) ARISING OUT OF THE USE OR INABILITY TO USE THE TOOL, OR THE PROVISION OF OR FAILURE TO PROVIDE SUPPORT SERVICES, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, AND REGARDLESS OF THE LEGAL OR EQUITABLE THEORY (CONTRACT, TORT OR OTHERWISE) UPON WHICH THE CLAIM IS BASED.
|
|
28
|
+
(b) IN ANY CASE, LICENSOR'S ENTIRE LIABILITY UNDER ANY PROVISION OF THIS AGREEMENT WILL BE LIMITED TO THE AMOUNT ACTUALLY PAID BY LICENSEE FOR THE TOOL UNDER THIS AGREEMENT OR FIVE (5) USD, WHICHEVER IS GREATER.
|
|
29
|
+
|
|
30
|
+
8. GENERAL
|
|
31
|
+
(a) Licensor reserves the right at any time to cease the support of the Tool and to alter features, specifications, capabilities, functions, licensing terms, release dates, general availability or other characteristics of the Tool.
|
|
32
|
+
(b) This Agreement constitutes the entire agreement between the parties concerning Licensee's use of the Tool, and supersedes any and all prior or contemporaneous oral or written representations, communications, or advertising with respect to the Tool.
|
|
33
|
+
(c) This Agreement will be governed by the laws of Nebraska, in the United States, without reference to conflict of laws principles. Licensee agrees that any litigation relating to this Agreement may only be brought in, and will be subject to the jurisdiction of, the state, local, and federal courts located in Douglas County, Nebraska.
|