@opscompass/opscompass-cli 1.3.6 → 1.3.8
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/README.md +39 -0
- package/lib/cliHelpers.js +10 -6
- package/lib/dataGathering/sendDg.js +18 -0
- package/lib/homeInteraction.js +276 -68
- package/lib/utils.js +22 -0
- package/modules/data-gathering-scripts.js +191 -54
- package/modules/data-gathering.js +22 -0
- package/modules/license-manager/assessment.js +87 -0
- package/modules/license-manager/microsoft.js +49 -29
- package/modules/license-manager/oracle.js +87 -14
- package/modules/license-manager.js +58 -8
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -104,3 +104,42 @@ Here, the OpsCompass ID of the resource is 1234567. When you have the OpsCompass
|
|
|
104
104
|
```
|
|
105
105
|
opscompass resources get --company your_company_id --id 1234567
|
|
106
106
|
```
|
|
107
|
+
|
|
108
|
+
## Persisted Oracle and MSSQL assessments
|
|
109
|
+
|
|
110
|
+
These commands read periodically persisted licensing results, not live configuration or current-entitlement inventory. No call recalculates an assessment, looks up another account, or fetches every page automatically.
|
|
111
|
+
|
|
112
|
+
| UI tab | Oracle command | MSSQL command | JSON section |
|
|
113
|
+
| --- | --- | --- | --- |
|
|
114
|
+
| Compliance | `get-oracle-assessment-summary` | `get-mssql-assessment-summary` | `assessmentSummary` |
|
|
115
|
+
| Entitlements | `get-oracle-assessment-entitlements` | `get-mssql-assessment-entitlements` | `assessmentEntitlements` |
|
|
116
|
+
| License usage map | `get-oracle-assessment-license-usage-summary` | `get-mssql-assessment-license-usage-summary` | `licenseUsageSummary` |
|
|
117
|
+
| Deployment details | `get-oracle-assessment-deployment-details` | `get-mssql-assessment-deployment-details` | `deploymentDetails` |
|
|
118
|
+
|
|
119
|
+
Run each under `license-manager`, for example:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
opscompass license-manager get-oracle-assessment-summary --company acme --account 42 --limit 100 --offset 0
|
|
123
|
+
opscompass license-manager get-oracle-assessment-entitlements --company acme --account 42 --assessment-id 123 --limit 100 --offset 0
|
|
124
|
+
opscompass license-manager get-oracle-assessment-summary --company acme --account 42 --mlss-assessment-id 55
|
|
125
|
+
opscompass license-manager get-mssql-assessment-summary --company acme --account sql-account --limit 100 --offset 0
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- `--company` and `--account` use existing saved defaults when omitted. The backend resolves account by trying company-scoped positive numeric row ID first, then exact `cloudSubscriptionId` match; account accepts any nonblank opaque string (leading/trailing whitespace trimmed), so numeric strings such as `'0'` or oversized integers are valid client-side and resolved server-side within the company. Oracle also accepts `--account-id` / `--accountid`, overriding the current account.
|
|
129
|
+
- `--assessment-id` is an optional positive safe integer; when omitted and no `--mlss-assessment-id` is supplied, selects the current persisted assessment. Pin the returned `assessmentId` for subsequent pages and other sections to prevent current-selection rollover. This does not make rows immutable if ingestion edits them under the same ID.
|
|
130
|
+
- Oracle-only: `--mlss-assessment-id` selects by MLSS assessment ID instead of local row ID. MLSS and local assessment IDs are separate namespaces; when both selectors are omitted, the current persisted assessment is selected. `--assessment-id` and `--mlss-assessment-id` are mutually exclusive; supplying both is an error. The returned `assessmentId` is always the local row ID. MSSQL commands reject `--mlss-assessment-id`.
|
|
131
|
+
- `--limit` accepts integers 1 through 500 (default 100). `--offset` accepts nonnegative safe integers (default 0). Increment offset by limit and stop when the selected section array contains fewer than limit rows; an exact final full page is followed by an empty page.
|
|
132
|
+
- Pagination counts top-level rows only. Entitlement, usage and deployment modal details remain nested and complete for the selected rows; child arrays are not limited to 500.
|
|
133
|
+
- Each success prints one JSON object containing assessment metadata, effective `pagination`, and exactly one section. Missing assessments are errors; valid empty sections/pages retain metadata. Invalid, blank, missing or unknown flags fail with a nonzero exit and no request. API failures also exit nonzero, without a fake success document.
|
|
134
|
+
|
|
135
|
+
Oracle retains metric labels, decimal-string prices, negative availability, comments and nulls. Its physical-machine/deployment identifiers are MLSS/OLM IDs, not OpsCompass inventory resource IDs. MSSQL preserves stored entitlement prices, including zero/null, and real optional inventory resource links. MSSQL reads the stored layout used by v3 and shared with v2, with no v1 calculation fallback. Current results are known to have v3 provenance only after verified producer regeneration; pinned historical v2/v3 provenance is unknown.
|
|
136
|
+
|
|
137
|
+
### Breaking release: assessment entitlements
|
|
138
|
+
|
|
139
|
+
`get-mssql-assessment-entitlements` deliberately replaces the old assessment-ID-only request and bare-array response. Use company/account scope and optional `--assessment-id` instead of `--id` / `--assessmentId`; read `assessmentEntitlements` from the page envelope. There is no legacy alias or dual response schema. Coordinate backend/API and client distribution; older installed clients do not accept this contract.
|
|
140
|
+
|
|
141
|
+
The SDK uses the same single-page contract. Oracle methods are on `client.licenseManager`; MSSQL methods are on `client.licenseManager.mssql`. Method names match the commands in camelCase (for example `getOracleAssessmentSummary` and `getMssqlAssessmentEntitlements`). All four Oracle methods take `(companySlug: string, accountId: string | number, options?: OracleAssessmentReadOptions)` with optional `assessmentId`, `mlssAssessmentId`, `limit`, and `offset`. The four MSSQL methods take `(companySlug: string, accountId: string | number, options?: AssessmentReadOptions)` with optional `assessmentId`, `limit`, and `offset`. Both reject on HTTP failures. Use the returned ID and offsets yourself; the SDK does not fan out or rename sections.
|
|
142
|
+
|
|
143
|
+
### Operator rollout order
|
|
144
|
+
|
|
145
|
+
The MLSS source-selection feature requires staged deployment: MLSS service first, then the customer-facing API, then the SDK/CLI client. Do not release client support before the backend accepts the new query parameter.
|
package/lib/cliHelpers.js
CHANGED
|
@@ -158,17 +158,21 @@ async function downloadFile(downloadUrl, downloadFilePath, downloadName) {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
/**
|
|
161
|
-
* Unzips a file
|
|
162
|
-
* containing the
|
|
163
|
-
* @param {string}
|
|
164
|
-
* @param {string}
|
|
161
|
+
* Unzips a file to the requested extraction directory.
|
|
162
|
+
* @param {string} unzipFileLocation directory containing the zip archive
|
|
163
|
+
* @param {string} unzipFileName archive file name
|
|
164
|
+
* @param {string} extractionDestination directory receiving extracted contents
|
|
165
165
|
*/
|
|
166
|
-
async function unzipFile(
|
|
166
|
+
async function unzipFile(
|
|
167
|
+
unzipFileLocation,
|
|
168
|
+
unzipFileName,
|
|
169
|
+
extractionDestination = unzipFileLocation,
|
|
170
|
+
) {
|
|
167
171
|
const zipFileLocation = path.join(unzipFileLocation, unzipFileName);
|
|
168
172
|
try {
|
|
169
173
|
const zip = new AdmZip(zipFileLocation);
|
|
170
174
|
// Overwrite and keepOriginalPermissions
|
|
171
|
-
zip.extractAllTo(
|
|
175
|
+
zip.extractAllTo(extractionDestination, true, true);
|
|
172
176
|
} catch (err) {
|
|
173
177
|
deleteFile(zipFileLocation);
|
|
174
178
|
throw "Something went wrong during unzipping.\nIf you specified a script version, confirm it is correct.\nIf the error continues, contact your OpsCompass rep.";
|
|
@@ -691,6 +691,21 @@ function isArchiveFile(filePath) {
|
|
|
691
691
|
const lower = filePath.toLowerCase();
|
|
692
692
|
return lower.endsWith(".tgz") || lower.endsWith(".tar.gz");
|
|
693
693
|
}
|
|
694
|
+
|
|
695
|
+
function canonicalizeEncryptedArchiveForUpload(filePath) {
|
|
696
|
+
if (!filePath.toLowerCase().endsWith(".tar.gz.gpg")) {
|
|
697
|
+
return { filePath };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "opscompass-send-dg-upload-"));
|
|
701
|
+
const uploadPath = path.join(
|
|
702
|
+
tempDir,
|
|
703
|
+
`${path.basename(filePath).slice(0, -".tar.gz.gpg".length)}.tgz.gpg`,
|
|
704
|
+
);
|
|
705
|
+
fs.copyFileSync(filePath, uploadPath);
|
|
706
|
+
return { filePath: uploadPath, tempDir };
|
|
707
|
+
}
|
|
708
|
+
|
|
694
709
|
function logCheckpointKey(company, account, logDirectory) {
|
|
695
710
|
return `${company}\u0000${account || ""}\u0000${path.resolve(logDirectory)}`;
|
|
696
711
|
}
|
|
@@ -1013,6 +1028,9 @@ async function sendDgAction(args = [], logger = { emit: () => {} }) {
|
|
|
1013
1028
|
if (!isEncryptedFile(filePath)) {
|
|
1014
1029
|
encrypted = await encryptFile(filePath, params.account);
|
|
1015
1030
|
}
|
|
1031
|
+
const uploadArchive = canonicalizeEncryptedArchiveForUpload(encrypted);
|
|
1032
|
+
encrypted = uploadArchive.filePath;
|
|
1033
|
+
if (uploadArchive.tempDir) stableOutputTempDirs.add(uploadArchive.tempDir);
|
|
1016
1034
|
|
|
1017
1035
|
const response = await sendToOc(client, params.metadataType, params.fileType, encrypted, params.company);
|
|
1018
1036
|
if (!transferSucceeded(response)) {
|
package/lib/homeInteraction.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from "fs-extra";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import {
|
|
4
5
|
Verify,
|
|
@@ -12,11 +13,9 @@ import {
|
|
|
12
13
|
} from "../environment/current.js";
|
|
13
14
|
import {
|
|
14
15
|
findOrCreateFolder,
|
|
15
|
-
deleteDirectory,
|
|
16
16
|
downloadFile,
|
|
17
17
|
unzipFile,
|
|
18
18
|
deleteFile,
|
|
19
|
-
moveFolder,
|
|
20
19
|
writeFile,
|
|
21
20
|
canReadFile,
|
|
22
21
|
getAllFilesInFolder,
|
|
@@ -111,13 +110,148 @@ async function getScriptMetadata(scriptType) {
|
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
function assertDirectChildSemver(version, description) {
|
|
114
|
+
if (
|
|
115
|
+
typeof version !== "string" ||
|
|
116
|
+
path.posix.basename(version) !== version ||
|
|
117
|
+
path.win32.basename(version) !== version ||
|
|
118
|
+
!semver.valid(version)
|
|
119
|
+
) {
|
|
120
|
+
throw new Error(`${description} must be a direct-child semantic version.`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return version;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function versionFromArchiveName(fileName) {
|
|
127
|
+
if (
|
|
128
|
+
typeof fileName !== "string" ||
|
|
129
|
+
path.posix.basename(fileName) !== fileName ||
|
|
130
|
+
path.win32.basename(fileName) !== fileName ||
|
|
131
|
+
!fileName.endsWith(".zip")
|
|
132
|
+
) {
|
|
133
|
+
throw new Error("Script archive name must be a direct-child .zip file.");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return fileName.slice(0, -".zip".length);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function validateInstalledMetadata(scriptType, scriptTypePath, srcPath) {
|
|
140
|
+
const metadataPath = path.join(scriptTypePath, METADATA_FILE_NAME);
|
|
141
|
+
if (!fs.existsSync(metadataPath)) {
|
|
142
|
+
if (fs.existsSync(srcPath)) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Cannot replace ${scriptType} scripts because ${METADATA_FILE_NAME} does not contain the installed version.`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
const scriptMetadata = await getScriptMetadata(scriptType);
|
|
152
|
+
assertDirectChildSemver(
|
|
153
|
+
scriptMetadata?.version,
|
|
154
|
+
`Installed ${METADATA_FILE_NAME} version`,
|
|
155
|
+
);
|
|
156
|
+
} catch (err) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
`Cannot replace ${scriptType} scripts because ${METADATA_FILE_NAME} is unreadable or lacks a valid direct-child semantic version.`,
|
|
159
|
+
{ cause: err },
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function normalizeScriptPermissions(destinationPath) {
|
|
165
|
+
const allFilePaths = getAllFilesInFolder(destinationPath);
|
|
166
|
+
allFilePaths.forEach(function (filePath) {
|
|
167
|
+
// Skip files we don't care about ({hidden}, bat, txt, ...)
|
|
168
|
+
if (path.basename(filePath).match(/^\./)) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const fileExtension = path.extname(filePath);
|
|
173
|
+
if (fileExtension.match(/bat|txt|pl/)) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const fileStat = fs.lstatSync(filePath);
|
|
178
|
+
if (fileStat.mode & 0o007) {
|
|
179
|
+
fs.chmodSync(filePath, fileStat.mode & 0o770);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function removeInstallArtifact(artifactPath, output, description) {
|
|
185
|
+
try {
|
|
186
|
+
await fs.rm(artifactPath, { recursive: true, force: true });
|
|
187
|
+
} catch (err) {
|
|
188
|
+
output.warn?.(`Unable to remove ${description} ${artifactPath}: ${err}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function cleanupOldBackups(backupRoot, keepBackupPath, output) {
|
|
193
|
+
if (!fs.existsSync(backupRoot)) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let entries;
|
|
198
|
+
try {
|
|
199
|
+
entries = await fs.readdir(backupRoot, { withFileTypes: true });
|
|
200
|
+
} catch (err) {
|
|
201
|
+
output.warn?.(`Unable to inspect script backups ${backupRoot}: ${err}`);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const backupEntries = entries.filter((entry) =>
|
|
206
|
+
entry.name.startsWith("backup-"),
|
|
207
|
+
);
|
|
208
|
+
if (!keepBackupPath && backupEntries.length > 0) {
|
|
209
|
+
let newestModifiedTime = -Infinity;
|
|
210
|
+
try {
|
|
211
|
+
for (const entry of backupEntries) {
|
|
212
|
+
const backupPath = path.join(backupRoot, entry.name);
|
|
213
|
+
const modifiedTime = (await fs.stat(backupPath)).mtimeMs;
|
|
214
|
+
if (modifiedTime >= newestModifiedTime) {
|
|
215
|
+
newestModifiedTime = modifiedTime;
|
|
216
|
+
keepBackupPath = backupPath;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
} catch (err) {
|
|
220
|
+
output.warn?.(`Unable to inspect script backups ${backupRoot}: ${err}`);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
for (const entry of backupEntries) {
|
|
226
|
+
const backupPath = path.join(backupRoot, entry.name);
|
|
227
|
+
if (backupPath === keepBackupPath) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
await removeInstallArtifact(backupPath, output, "old script backup");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function restorePreviousSource({
|
|
235
|
+
srcPath,
|
|
236
|
+
stagedPayloadPath,
|
|
237
|
+
backupPath,
|
|
238
|
+
previousSourceBackedUp,
|
|
239
|
+
payloadPromoted,
|
|
240
|
+
installError,
|
|
241
|
+
}) {
|
|
242
|
+
try {
|
|
243
|
+
if (payloadPromoted) {
|
|
244
|
+
await fs.rename(srcPath, stagedPayloadPath);
|
|
245
|
+
}
|
|
246
|
+
if (previousSourceBackedUp) {
|
|
247
|
+
await fs.rename(backupPath, srcPath);
|
|
248
|
+
}
|
|
249
|
+
} catch (rollbackError) {
|
|
250
|
+
throw new Error(
|
|
251
|
+
`Script install failed (${installError}); rollback also failed (${rollbackError}). Live source: ${srcPath}. Previous source backup: ${backupPath || "none"}. Staged payload: ${stagedPayloadPath}.`,
|
|
252
|
+
{ cause: installError },
|
|
253
|
+
);
|
|
254
|
+
}
|
|
121
255
|
}
|
|
122
256
|
|
|
123
257
|
/**
|
|
@@ -199,7 +333,7 @@ async function downloadScriptsToHome(
|
|
|
199
333
|
);
|
|
200
334
|
const scriptTypePath = findOrCreateScriptTypeFolder(scriptType);
|
|
201
335
|
const fileName = scriptDownloadInfo.fileName;
|
|
202
|
-
scriptVersion = fileName
|
|
336
|
+
scriptVersion = versionFromArchiveName(fileName);
|
|
203
337
|
|
|
204
338
|
await downloadFile(
|
|
205
339
|
scriptDownloadInfo.downloadUrl,
|
|
@@ -208,68 +342,132 @@ async function downloadScriptsToHome(
|
|
|
208
342
|
);
|
|
209
343
|
|
|
210
344
|
scriptDownloadInfo.filePath = path.join(scriptTypePath, fileName);
|
|
211
|
-
|
|
212
|
-
|
|
345
|
+
let stagingDirectory;
|
|
346
|
+
let stagedPayloadPath;
|
|
347
|
+
let backupArtifactPath;
|
|
348
|
+
let backupPath;
|
|
349
|
+
let preserveDiagnosticPaths = false;
|
|
213
350
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
throw new Error(
|
|
218
|
-
`Script bundle verification failed for ${scriptType}: ${verification.reason || "invalid signature"}`,
|
|
219
|
-
);
|
|
220
|
-
}
|
|
351
|
+
try {
|
|
352
|
+
const verification =
|
|
353
|
+
(await Verify(scriptDownloadInfo, output)) || SCRIPT_VERIFICATION_FALLBACK;
|
|
221
354
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
355
|
+
// Signature present but invalid must fail closed and not install.
|
|
356
|
+
if (verification.status === SCRIPT_BUNDLE_VERIFICATION_STATUS.invalid) {
|
|
357
|
+
throw new Error(
|
|
358
|
+
`Script bundle verification failed for ${scriptType}: ${verification.reason || "invalid signature"}`,
|
|
359
|
+
);
|
|
360
|
+
}
|
|
229
361
|
|
|
230
|
-
|
|
362
|
+
if (verification.status !== SCRIPT_BUNDLE_VERIFICATION_STATUS.verified) {
|
|
363
|
+
output.warn?.(
|
|
364
|
+
"Downloaded %s bundle is unverified (%s)",
|
|
365
|
+
scriptType,
|
|
366
|
+
verification.reason || "unknown",
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
assertDirectChildSemver(scriptVersion, "Script archive version");
|
|
231
370
|
|
|
232
|
-
|
|
371
|
+
const stagingRoot = path.join(scriptTypePath, ".src-staging");
|
|
372
|
+
await fs.ensureDir(stagingRoot);
|
|
373
|
+
stagingDirectory = await fs.mkdtemp(path.join(stagingRoot, "install-"));
|
|
374
|
+
await unzipFile(scriptTypePath, fileName, stagingDirectory);
|
|
233
375
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
376
|
+
stagedPayloadPath = path.join(stagingDirectory, scriptVersion);
|
|
377
|
+
let stagedPayloadStat;
|
|
378
|
+
try {
|
|
379
|
+
stagedPayloadStat = await fs.lstat(stagedPayloadPath);
|
|
380
|
+
} catch (err) {
|
|
381
|
+
throw new Error(
|
|
382
|
+
`Script archive does not contain the expected ${scriptVersion} directory.`,
|
|
383
|
+
{ cause: err },
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
if (!stagedPayloadStat.isDirectory()) {
|
|
387
|
+
throw new Error(
|
|
388
|
+
`Script archive does not contain the expected ${scriptVersion} directory.`,
|
|
389
|
+
);
|
|
390
|
+
}
|
|
240
391
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
392
|
+
normalizeScriptPermissions(stagedPayloadPath);
|
|
393
|
+
|
|
394
|
+
const srcPath = path.join(scriptTypePath, SRC_FOLDER_NAME);
|
|
395
|
+
await validateInstalledMetadata(scriptType, scriptTypePath, srcPath);
|
|
396
|
+
|
|
397
|
+
let previousSourceBackedUp = false;
|
|
398
|
+
let payloadPromoted = false;
|
|
399
|
+
|
|
400
|
+
// WHY: NFS clients can keep live script files open, so recursive replacement is not a safe cutover.
|
|
401
|
+
// DECISION: validate installed metadata before any live rename so an unsafe recovery state fails while src is untouched; use only native same-filesystem renames for the live transition.
|
|
402
|
+
// DECISION: Leave legacy direct-child semver folders untouched because an interrupted older installer may have left its only recoverable payload there.
|
|
403
|
+
// TRADEOFF: Keep the newest hidden backup with its validated metadata beside src after a successful cutover, accepting its disk-space cost for a self-describing recovery point.
|
|
404
|
+
try {
|
|
405
|
+
if (fs.existsSync(srcPath)) {
|
|
406
|
+
const backupRoot = path.join(scriptTypePath, ".src-backups");
|
|
407
|
+
await fs.ensureDir(backupRoot);
|
|
408
|
+
backupArtifactPath = await fs.mkdtemp(
|
|
409
|
+
path.join(backupRoot, "backup-"),
|
|
410
|
+
);
|
|
411
|
+
backupPath = path.join(backupArtifactPath, SRC_FOLDER_NAME);
|
|
412
|
+
await fs.copy(
|
|
413
|
+
path.join(scriptTypePath, METADATA_FILE_NAME),
|
|
414
|
+
path.join(backupArtifactPath, METADATA_FILE_NAME),
|
|
415
|
+
);
|
|
416
|
+
await fs.rename(srcPath, backupPath);
|
|
417
|
+
previousSourceBackedUp = true;
|
|
252
418
|
}
|
|
253
|
-
}
|
|
254
419
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
420
|
+
await fs.rename(stagedPayloadPath, srcPath);
|
|
421
|
+
payloadPromoted = true;
|
|
422
|
+
|
|
423
|
+
await setScriptMetadata(scriptTypePath, scriptVersion, verification);
|
|
424
|
+
} catch (installError) {
|
|
425
|
+
try {
|
|
426
|
+
await restorePreviousSource({
|
|
427
|
+
srcPath,
|
|
428
|
+
stagedPayloadPath,
|
|
429
|
+
backupPath,
|
|
430
|
+
previousSourceBackedUp,
|
|
431
|
+
payloadPromoted,
|
|
432
|
+
installError,
|
|
433
|
+
});
|
|
434
|
+
} catch (rollbackError) {
|
|
435
|
+
preserveDiagnosticPaths = true;
|
|
436
|
+
throw rollbackError;
|
|
437
|
+
}
|
|
438
|
+
throw installError;
|
|
263
439
|
}
|
|
264
|
-
});
|
|
265
440
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
441
|
+
await cleanupOldBackups(
|
|
442
|
+
path.join(scriptTypePath, ".src-backups"),
|
|
443
|
+
backupArtifactPath,
|
|
444
|
+
output,
|
|
445
|
+
);
|
|
270
446
|
|
|
271
|
-
|
|
272
|
-
|
|
447
|
+
return scriptVersion;
|
|
448
|
+
} finally {
|
|
449
|
+
if (fs.existsSync(scriptDownloadInfo.filePath)) {
|
|
450
|
+
deleteFile(scriptDownloadInfo.filePath);
|
|
451
|
+
}
|
|
452
|
+
if (stagingDirectory && !preserveDiagnosticPaths) {
|
|
453
|
+
await removeInstallArtifact(
|
|
454
|
+
stagingDirectory,
|
|
455
|
+
output,
|
|
456
|
+
"script staging directory",
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
if (
|
|
460
|
+
backupArtifactPath &&
|
|
461
|
+
!preserveDiagnosticPaths &&
|
|
462
|
+
!fs.existsSync(backupPath)
|
|
463
|
+
) {
|
|
464
|
+
await removeInstallArtifact(
|
|
465
|
+
backupArtifactPath,
|
|
466
|
+
output,
|
|
467
|
+
"empty script backup",
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
273
471
|
}
|
|
274
472
|
|
|
275
473
|
/**
|
|
@@ -287,7 +485,7 @@ function previousScriptsDownloaded(scriptType) {
|
|
|
287
485
|
}
|
|
288
486
|
|
|
289
487
|
/**
|
|
290
|
-
*
|
|
488
|
+
* Atomically creates or updates a script's metadata.
|
|
291
489
|
* @param {string} scriptFolderPath path to the script folder
|
|
292
490
|
* @param {string} scriptVersion the version of the downloaded script
|
|
293
491
|
*/
|
|
@@ -296,6 +494,7 @@ async function setScriptMetadata(
|
|
|
296
494
|
scriptVersion,
|
|
297
495
|
verification = SCRIPT_VERIFICATION_FALLBACK,
|
|
298
496
|
) {
|
|
497
|
+
assertDirectChildSemver(scriptVersion, "Installed metadata version");
|
|
299
498
|
const scriptMetadata = {
|
|
300
499
|
version: scriptVersion,
|
|
301
500
|
verification: {
|
|
@@ -309,13 +508,23 @@ async function setScriptMetadata(
|
|
|
309
508
|
};
|
|
310
509
|
|
|
311
510
|
const metadataFilePath = path.join(scriptFolderPath, METADATA_FILE_NAME);
|
|
511
|
+
const pendingMetadataPath = path.join(
|
|
512
|
+
scriptFolderPath,
|
|
513
|
+
`.metadata-${randomUUID()}.tmp`,
|
|
514
|
+
);
|
|
312
515
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
516
|
+
try {
|
|
517
|
+
await writeFile(pendingMetadataPath, scriptMetadata);
|
|
518
|
+
await fs.rename(pendingMetadataPath, metadataFilePath);
|
|
519
|
+
} finally {
|
|
520
|
+
if (fs.existsSync(pendingMetadataPath)) {
|
|
521
|
+
try {
|
|
522
|
+
await fs.rm(pendingMetadataPath, { force: true });
|
|
523
|
+
} catch {
|
|
524
|
+
// The original write/rename failure is more useful than temp cleanup.
|
|
525
|
+
}
|
|
526
|
+
}
|
|
316
527
|
}
|
|
317
|
-
|
|
318
|
-
await writeFile(metadataFilePath, scriptMetadata);
|
|
319
528
|
}
|
|
320
529
|
|
|
321
530
|
async function getScriptVerificationState(scriptType) {
|
|
@@ -355,7 +564,6 @@ export {
|
|
|
355
564
|
getInstalledScriptVersion,
|
|
356
565
|
getScriptVerificationState,
|
|
357
566
|
assertScriptBundleVerified,
|
|
358
|
-
removeCurrentlyInstalledScript,
|
|
359
567
|
downloadScriptsToHome,
|
|
360
568
|
previousScriptsDownloaded,
|
|
361
569
|
};
|
package/lib/utils.js
CHANGED
|
@@ -72,8 +72,29 @@ function cleanupOldConfigBackups(dirPath, prefix, extension, maxAgeSeconds) {
|
|
|
72
72
|
/**
|
|
73
73
|
* Process standard arguments (eg company) and remove them
|
|
74
74
|
* @param {string[]} args
|
|
75
|
+
* @returns {boolean}
|
|
75
76
|
*/
|
|
76
77
|
function processStandardArgs(args) {
|
|
78
|
+
for (let i = 0; i < args.length; i++) {
|
|
79
|
+
if (
|
|
80
|
+
typeof args[i] === "string" &&
|
|
81
|
+
args[i].toLowerCase() === "--account"
|
|
82
|
+
) {
|
|
83
|
+
const account = args[i + 1];
|
|
84
|
+
if (
|
|
85
|
+
account === undefined ||
|
|
86
|
+
account === null ||
|
|
87
|
+
(typeof account === "string" &&
|
|
88
|
+
(account.startsWith("--") || account.trim() === ""))
|
|
89
|
+
) {
|
|
90
|
+
console.error(
|
|
91
|
+
"ERROR: --account is required (or set a default account).",
|
|
92
|
+
);
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
77
98
|
// There should be global values from opscompassHome/defaults.json for these
|
|
78
99
|
// - They are picked up by index.js
|
|
79
100
|
// - Override them here from the command line
|
|
@@ -94,6 +115,7 @@ function processStandardArgs(args) {
|
|
|
94
115
|
break;
|
|
95
116
|
}
|
|
96
117
|
}
|
|
118
|
+
return true;
|
|
97
119
|
}
|
|
98
120
|
|
|
99
121
|
/**
|