|
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 5 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 6 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 7 | + } |
| 8 | + Object.defineProperty(o, k2, desc); |
| 9 | +}) : (function(o, m, k, k2) { |
| 10 | + if (k2 === undefined) k2 = k; |
| 11 | + o[k2] = m[k]; |
| 12 | +})); |
| 13 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 14 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 15 | +}) : function(o, v) { |
| 16 | + o["default"] = v; |
| 17 | +}); |
| 18 | +var __importStar = (this && this.__importStar) || (function () { |
| 19 | + var ownKeys = function(o) { |
| 20 | + ownKeys = Object.getOwnPropertyNames || function (o) { |
| 21 | + var ar = []; |
| 22 | + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; |
| 23 | + return ar; |
| 24 | + }; |
| 25 | + return ownKeys(o); |
| 26 | + }; |
| 27 | + return function (mod) { |
| 28 | + if (mod && mod.__esModule) return mod; |
| 29 | + var result = {}; |
| 30 | + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); |
| 31 | + __setModuleDefault(result, mod); |
| 32 | + return result; |
| 33 | + }; |
| 34 | +})(); |
| 35 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 36 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 37 | +}; |
| 38 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 39 | +exports.AzModuleInstaller = exports.AzModuleSource = void 0; |
| 40 | +const core = __importStar(require("@actions/core")); |
| 41 | +const tc = __importStar(require("@actions/tool-cache")); |
| 42 | +const os = __importStar(require("os")); |
| 43 | +const ArchiveTools_1 = require("./Utilities/ArchiveTools"); |
| 44 | +const FileUtils_1 = __importDefault(require("./Utilities/FileUtils")); |
| 45 | +const Utils_1 = __importDefault(require("./Utilities/Utils")); |
| 46 | +const path_1 = __importDefault(require("path")); |
| 47 | +const Constants_1 = __importDefault(require("./Constants")); |
| 48 | +exports.AzModuleSource = { |
| 49 | + PrivateAgent: "privateAgent", |
| 50 | + Folder: "hostedAgentFolder", |
| 51 | + Zip: "hostedAgentZip", |
| 52 | + GHRelease: "hostedAgentGHRelease", |
| 53 | + PSGallery: "hostedAgentPSGallery" |
| 54 | +}; |
| 55 | +class AzModuleInstaller { |
| 56 | + version; |
| 57 | + githubAuth; |
| 58 | + moduleRoot; |
| 59 | + modulePath; |
| 60 | + moduleZipPath; |
| 61 | + installResult; |
| 62 | + isWin = false; |
| 63 | + constructor(version, githubAuth) { |
| 64 | + this.version = version; |
| 65 | + this.githubAuth = githubAuth; |
| 66 | + this.installResult = { |
| 67 | + moduleSource: "Others", |
| 68 | + isInstalled: false |
| 69 | + }; |
| 70 | + const platform = (process.env.RUNNER_OS || os.type())?.toLowerCase(); |
| 71 | + core.debug(`Platform: ${platform}`); |
| 72 | + this.moduleRoot = Utils_1.default.getDefaultAzInstallFolder(platform); |
| 73 | + if (platform == "windows" || platform == "windows_nt") { |
| 74 | + this.isWin = true; |
| 75 | + } |
| 76 | + this.modulePath = path_1.default.join(this.moduleRoot, `${Constants_1.default.prefix}${this.version}`); |
| 77 | + this.moduleZipPath = `${this.modulePath}.zip`; |
| 78 | + } |
| 79 | + async install() { |
| 80 | + if (Utils_1.default.isHostedAgent(this.moduleRoot)) { |
| 81 | + await this.tryInstallingLatest(); |
| 82 | + await this.tryInstallFromFolder(); |
| 83 | + await this.tryInstallFromZip(); |
| 84 | + await this.tryInstallFromGHRelease(); |
| 85 | + await this.tryInstallFromPSGallery(); |
| 86 | + } |
| 87 | + else { |
| 88 | + core.debug("File layout is not like hosted agent, skippig module install."); |
| 89 | + this.installResult = { |
| 90 | + isInstalled: false, |
| 91 | + moduleSource: exports.AzModuleSource.PrivateAgent |
| 92 | + }; |
| 93 | + } |
| 94 | + return this.installResult; |
| 95 | + } |
| 96 | + async tryInstallingLatest() { |
| 97 | + if (this.installResult.isInstalled) { |
| 98 | + core.debug(`Module already installed skipping tryInstallingLatest`); |
| 99 | + return; |
| 100 | + } |
| 101 | + if (this.version === "latest") { |
| 102 | + core.debug("Latest selected, will use latest Az module available in agent as folder."); |
| 103 | + this.installResult = { |
| 104 | + isInstalled: true, |
| 105 | + moduleSource: exports.AzModuleSource.Folder |
| 106 | + }; |
| 107 | + } |
| 108 | + } |
| 109 | + async tryInstallFromFolder() { |
| 110 | + if (this.installResult.isInstalled) { |
| 111 | + core.debug(`Module already installed skipping tryInstallFromFolder`); |
| 112 | + return; |
| 113 | + } |
| 114 | + if (FileUtils_1.default.pathExists(this.modulePath)) { |
| 115 | + core.debug(`Az ${this.version} present at ${this.modulePath} as folder.`); |
| 116 | + this.installResult = { |
| 117 | + isInstalled: true, |
| 118 | + moduleSource: exports.AzModuleSource.Folder |
| 119 | + }; |
| 120 | + } |
| 121 | + } |
| 122 | + async tryInstallFromZip() { |
| 123 | + if (this.installResult.isInstalled) { |
| 124 | + core.debug(`Module already installed skipping tryInstallFromZip`); |
| 125 | + return; |
| 126 | + } |
| 127 | + if (FileUtils_1.default.pathExists(this.moduleZipPath)) { |
| 128 | + core.debug(`Az ${this.version} present at ${this.moduleZipPath} as zip, expanding it.`); |
| 129 | + await new ArchiveTools_1.ArchiveTools(this.isWin).unzip(this.moduleZipPath, this.moduleRoot); |
| 130 | + await FileUtils_1.default.deleteFile(this.moduleZipPath); |
| 131 | + this.installResult = { |
| 132 | + isInstalled: true, |
| 133 | + moduleSource: exports.AzModuleSource.Zip |
| 134 | + }; |
| 135 | + } |
| 136 | + } |
| 137 | + async tryInstallFromGHRelease() { |
| 138 | + if (this.installResult.isInstalled) { |
| 139 | + core.debug(`Module already installed skipping tryInstallFromGHRelease`); |
| 140 | + return; |
| 141 | + } |
| 142 | + try { |
| 143 | + const downloadUrl = await this.getDownloadUrlFromGHRelease(); |
| 144 | + core.debug(`Downloading Az ${this.version} from GHRelease using url ${downloadUrl}`); |
| 145 | + await tc.downloadTool(downloadUrl, this.moduleZipPath, this.githubAuth); |
| 146 | + core.debug(`Expanding Az ${this.version} downloaded at ${this.moduleZipPath} as zip.`); |
| 147 | + await new ArchiveTools_1.ArchiveTools(this.isWin).unzip(this.moduleZipPath, this.moduleRoot); |
| 148 | + await FileUtils_1.default.deleteFile(this.moduleZipPath); |
| 149 | + this.installResult = { |
| 150 | + isInstalled: true, |
| 151 | + moduleSource: exports.AzModuleSource.GHRelease |
| 152 | + }; |
| 153 | + } |
| 154 | + catch (err) { |
| 155 | + core.debug(err); |
| 156 | + core.info("Download from GHRelease failed, will fallback to PSGallery"); |
| 157 | + } |
| 158 | + } |
| 159 | + async tryInstallFromPSGallery() { |
| 160 | + if (this.installResult.isInstalled) { |
| 161 | + core.debug(`Module already installed skipping tryInstallFromPSGallery`); |
| 162 | + return; |
| 163 | + } |
| 164 | + await Utils_1.default.saveAzModule(this.version, this.modulePath); |
| 165 | + this.installResult = { |
| 166 | + isInstalled: true, |
| 167 | + moduleSource: exports.AzModuleSource.PSGallery |
| 168 | + }; |
| 169 | + } |
| 170 | + async getDownloadUrlFromGHRelease() { |
| 171 | + core.debug("Getting versions manifest from GHRelease."); |
| 172 | + const releases = await tc.getManifestFromRepo("Azure", "az-ps-module-versions", this.githubAuth, "main"); |
| 173 | + core.debug(JSON.stringify(releases)); |
| 174 | + const releaseInfo = releases.filter(release => release.version === this.version)?.[0]; |
| 175 | + if (!releaseInfo || releaseInfo.files.length === 0) { |
| 176 | + throw new Error(`Version ${this.version} not present in versions manifest of GHRelease.`); |
| 177 | + } |
| 178 | + return releaseInfo.files[0].download_url; |
| 179 | + } |
| 180 | +} |
| 181 | +exports.AzModuleInstaller = AzModuleInstaller; |
0 commit comments