-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathmsdo-installer.js
More file actions
156 lines (156 loc) · 6.84 KB
/
Copy pathmsdo-installer.js
File metadata and controls
156 lines (156 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.install = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const process = __importStar(require("process"));
const core = __importStar(require("@actions/core"));
const common = __importStar(require("./msdo-common"));
const nuget = __importStar(require("./msdo-nuget-client"));
function install(cliVersion) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`Installing Microsoft Security DevOps Cli version: ${cliVersion}`);
if (process.env.MSDO_FILEPATH) {
console.log(`MSDO CLI File Path overriden by %MSDO_FILEPATH%: ${process.env.MSDO_FILEPATH}`);
return;
}
if (process.env.MSDO_DIRECTORY) {
console.log(`MSDO CLI Directory overriden by %MSDO_DIRECTORY%: ${process.env.MSDO_DIRECTORY}`);
let msdoFilePath = path.join(process.env.MSDO_DIRECTORY, 'guardian');
core.debug(`msdoFilePath = ${msdoFilePath}`);
process.env.MSDO_FILEPATH = msdoFilePath;
return;
}
let packageName = resolvePackageName();
let agentDirectory = path.resolve(path.join(process.env.GITHUB_WORKSPACE, '../../_msdo'));
core.debug(`agentDirectory = ${agentDirectory}`);
common.ensureDirectory(agentDirectory);
let agentPackagesDirectory = process.env.MSDO_PACKAGES_DIRECTORY;
if (!agentPackagesDirectory) {
agentPackagesDirectory = path.join(agentDirectory, 'packages');
core.debug(`agentPackagesDirectory = ${agentPackagesDirectory}`);
common.ensureDirectory(agentPackagesDirectory);
process.env.MSDO_PACKAGES_DIRECTORY = agentPackagesDirectory;
}
let agentVersionsDirectory = path.join(agentDirectory, 'versions');
core.debug(`agentVersionsDirectory = ${agentVersionsDirectory}`);
common.ensureDirectory(agentVersionsDirectory);
if (isInstalled(agentVersionsDirectory, packageName, cliVersion)) {
return;
}
let failed = false;
let attempts = 0;
let maxAttempts = 2;
let serviceIndexUrl = "https://api.nuget.org/v3/index.json";
let response;
do {
failed = false;
try {
response = yield nuget.install(serviceIndexUrl, packageName, cliVersion, agentVersionsDirectory);
}
catch (error) {
core.debug(error);
failed = true;
attempts += 1;
if (attempts > maxAttempts) {
break;
}
}
} while (failed);
if (response && response.success) {
if (response.inCache == true) {
console.log(`${packageName} version ${response.resolvedVersion} already installed`);
}
else {
console.log(`Installed ${packageName} version ${response.resolvedVersion}`);
}
}
else {
throw new Error('Failed to install the MSDO CLI nuget package.');
}
setVariables(agentVersionsDirectory, packageName, response.resolvedVersion, true);
});
}
exports.install = install;
function resolvePackageName() {
let packageName;
if (process.env.MSDO_DOTNETDEPENDENTPACKAGE) {
packageName = 'Microsoft.Security.Devops.Cli';
}
else if (process.platform == 'win32') {
packageName = 'Microsoft.Security.Devops.Cli.win-x64';
}
else if (process.platform == 'linux') {
if (process.arch == 'arm64') {
packageName = 'Microsoft.Security.Devops.Cli.linux-arm64';
}
else {
packageName = 'Microsoft.Security.Devops.Cli.linux-x64';
}
}
else {
packageName = 'Microsoft.Security.Devops.Cli';
}
core.debug(`packageName = ${packageName}`);
return packageName;
}
function isInstalled(packagesDirectory, packageName, cliVersion) {
let installed = false;
if (common.isLatest(cliVersion)) {
core.debug(`MSDO CLI version contains a latest quantifier: ${cliVersion}. Continuing with install...`);
return installed;
}
installed = setVariables(packagesDirectory, packageName, cliVersion);
if (installed) {
console.log(`MSDO CLI v${cliVersion} already installed.`);
}
return installed;
}
function setVariables(packagesDirectory, packageName, cliVersion, validate = false) {
let packageDirectory = path.join(packagesDirectory, `${packageName}.${cliVersion}`);
core.debug(`packageDirectory = ${packageDirectory}`);
let msdoDirectory = path.join(packageDirectory, 'tools');
core.debug(`msdoDirectory = ${msdoDirectory}`);
let msdoFilePath = path.join(msdoDirectory, 'guardian');
core.debug(`msdoFilePath = ${msdoFilePath}`);
process.env.MSDO_DIRECTORY = msdoDirectory;
process.env.MSDO_FILEPATH = msdoFilePath;
process.env.MSDO_INSTALLEDVERSION = cliVersion;
let exists = fs.existsSync(process.env.MSDO_FILEPATH);
if (validate && !exists) {
throw new Error(`MSDO CLI v${cliVersion} was not found after installation. Expected location: ${msdoFilePath}`);
}
return exists;
}