AzsBuildUtilities.psm1
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# TODO: Remove buildreq output (or only print on error). # TODO: What happens with CopyOnFailure if there is a build error? We should check that the build stage passed. function WriteMessage( [Parameter(Mandatory=$true)] [string] $Message ) { Write-Host ("[{0}]: {1}" -f (Get-Date), $Message) } function GetBuildTrackerPageWithRetry( [Parameter(Mandatory=$true)] [string] $BuildTrackerUrl ) { $attempt = 1 do { $response = Invoke-WebRequest -Uri $BuildTrackerUrl -UseDefaultCredential $responseCode = $response.StatusCode $attempt += 1 if ($responseCode -ne 200) { WriteMessage ("[WARNING] Unexpected response code from BuildTracker: {0}. Retrying..." -f $responseCode) } } while ($responseCode -ne 200 -and $attempt -lt 3) if ($responseCode -ne 200 -and $attempt -ge 3) { throw "Error getting build tracker page with 3 retries." } return $response.Content } function GetJobStatus( [Parameter(Mandatory=$true)] [string] $BuildTrackerPageContent ) { $statusRegex = [regex]"<B>Status: </B><font color='\w+'><b>(\w+)</b></font>" $match = [regex]::Match($BuildTrackerPageContent, $statusRegex).captures.groups[1].value if ($match -eq $null) { throw "Could not find job status on BuildTracker page" } return $match } function GetReddogPath( [Parameter(Mandatory=$true)] [string] $BuildTrackerPageContent ) { $reddogRegex = [regex]"<BR><B>Drop: </B><a href='(.+)'>" $reddogPath = [regex]::Match($BuildTrackerPageContent, $reddogRegex).captures.groups[1].value if ($reddogPath -eq $null) { throw "Could not find reddog location on BuildTracker page" } return $reddogPath } function CopyBuildToShare( [Parameter(Mandatory=$true)] [string] $ShareRootPath, [Parameter(Mandatory=$true)] [string] $BuildTrackerPageContent ) { WriteMessage "Finding build packages..." $reddogPath = GetReddogPath($BuildTrackerPageContent) $packagesPath = $reddogPath + "\packages" WriteMessage ("reddog packages path: {0}" -f $packagesPath) $shareOutputPath = $packagesPath -replace "\\\\reddog",$ShareRootPath WriteMessage "Generating share output path..." WriteMessage ("Share output path: {0}" -f $shareOutputPath) if (Test-Path $shareOutputPath) { WriteMessage "Share output path already exists! Removing..." Remove-Item -Recurse -Path $shareOutputPath } WriteMessage "Copying to share..." Copy-Item -Recurse -Force -Path $packagesPath -Destination $shareOutputPath return $shareOutputPath } function GetShareLatestPath( [Parameter(Mandatory=$true)] [string] $ShareOutputPath ) { $latestPath = $ShareOutputPath -replace "\\[^\\]+\\packages", "_latest" if ($latestPath -eq $null) { throw "Could not find generate latest path" } return $latestPath } function UpdateLatestPath( [Parameter(Mandatory=$true)] [string] $ShareOutputPath ) { $latestPath = GetShareLatestPath($ShareOutputPath) $latestPackagesPath = $latestPath + "\packages" WriteMessage ("Latest packages path: {0}" -f $latestPackagesPath) if (Test-Path $latestPath) { WriteMessage "Latest path already exists! Removing..." Remove-Item -Recurse -Path $latestPath } WriteMessage "Copying to latest packages path..." Copy-Item -Recurse -Force -Path $ShareOutputPath -Destination $latestPackagesPath return $latestPackagesPath } function IsJobInProgress( [Parameter(Mandatory=$true)] [string] $JobStatus ) { return $JobStatus -eq "Queued" -or $JobStatus -eq "Submitted" -or $JobStatus -eq "Sequenced" -or $JobStatus -eq "Pending" -or $JobStatus -eq "Running" } function IsJobCompleted( [Parameter(Mandatory=$true)] [string] $JobStatus ) { return $JobStatus -eq "Completed" } function IsJobFailed( [Parameter(Mandatory=$true)] [string] $JobStatus ) { return $JobStatus -eq "Failed" -or $JobStatus -eq "Aborted" } function GetBuildTrackerUrl( [Parameter(Mandatory=$true)] [Object[]] $BuildreqOutput ) { $lines = $BuildreqOutput.Split([Environment]::NewLine) $buildUrlRegex = [regex]"Build (http://wabt/BuildTracker/Jobs/LegSummary.aspx\?Id=\d+) started$" # Find the build URL, checking each line of output in reverse for ($i = $lines.Count - 1; $i -ge 0; $i--) { $captures = [regex]::Match($lines[$i], $buildUrlRegex).captures if ($captures -ne $null -and $captures.Groups.Count -ge 2) { $buildUrl = $captures.Groups[1].Value # Greedily take the first match if ($buildUrl -ne $null) { return $buildUrl } } } # If we reach here, we never found a build URL throw "Could not find BuildTracker URL in buildreq output" } function Watch-Build( [Parameter(Mandatory=$true)] [string] $BuildTrackerUrl ) { WriteMessage ("Watching build tracker job @ `"{0}`"..." -f $BuildTrackerUrl) do { $pageContent = GetBuildTrackerPageWithRetry($BuildTrackerUrl) $status = GetJobStatus($pageContent) WriteMessage ("Current job status: {0}" -f $status) $continue = IsJobInProgress($status) if ($continue) { Start-Sleep -Seconds 60 } } while ($continue) if ($(IsJobFailed($status)) -eq $true) { Write-Error "Job failed!" return $false } if ($(IsJobCompleted($status)) -eq $false) { Write-Error "Job did not complete." return $false } return $true } function Watch-BuildAndCopyToShare( [Parameter(Mandatory=$true)] [string] $BuildTrackerUrl, [string] $ShareRootPath = ("\\ecg\teams\Solutions\AzureStack\Data\{0}" -f $env:UserName), [Switch] $ShutdownOnCompletion, [Switch] $CopyOnFailure ) { WriteMessage ("Output share root path: `"{0}`"..." -f $ShareRootPath) if ($(Watch-Build($BuildTrackerUrl)) -eq $false) { if ($CopyOnFailure -eq $false) { WriteMessage "Build failed. Run with -CopyOnFailure to attempt to copy build on failure." return $false } else { WriteMessage "WARNING: Build failed. Attempting to copy anyway as -CopyOnFailure is enabled..." } } else { WriteMessage "Job completed!" } $pageContent = GetBuildTrackerPageWithRetry($BuildTrackerUrl) $outputPath = CopyBuildToShare -ShareRootPath $ShareRootPath -BuildTrackerPageContent $pageContent if ($outputPath -eq $null) { Write-Error ("Error copying to share: {0}!" -f $ShareRootPath) return $false } WriteMessage "Updating latest path..." UpdateLatestPath($outputPath) WriteMessage "Openning share output path..." Start $outputPath if ($ShutdownOnCompletion -eq $true) { WriteMessage "Shutdown on completion is enabled. Shutting down in 60 seconds..." Shutdown /f /s /t 60 } return $true } function Start-BuildAndCopyToShare( [string] $ShareRootPath = ("\\ecg\teams\Solutions\AzureStack\Data\{0}" -f $env:UserName), [Switch] $Publish, [Switch] $Sign, [Switch] $ShutdownOnCompletion, [Switch] $CopyOnFailure ) { WriteMessage ("Building repo...") $buildReqCommand = "buildreq" if ($Sign -eq $true) { WriteMessage "Code signing enabled." $buildReqCommand += " --signing" } else { WriteMessage "Code signing disabled." } if ($Publish -eq $true) { WriteMessage "Code publishing enabled." $buildReqCommand += " --publish" if ($Sign -eq $false) { WriteMessage "[WARNING] Are you sure you want to publish unsigned code?" } } else { WriteMessage "Code publishing disabled." } WriteMessage ("Calling buildreq with command `"{0}`"..." -f $buildReqCommand) $buildReqOutput = Invoke-Expression $buildReqCommand Write-Host $buildReqOutput $buildTrackerUrl = GetBuildTrackerUrl($buildReqOutput) return $(Watch-BuildAndCopyToShare -ShutdownOnCompletion:$ShutdownOnCompletion -CopyOnFailure:$CopyOnFailure -BuildTrackerUrl $buildTrackerUrl -ShareRootPath $ShareRootPath) } Export-ModuleMember Watch-Build, Watch-BuildAndCopyToShare, Start-BuildAndCopyToShare |