|
| 1 | +#!/usr/bin/env pwsh |
| 2 | + |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | +Executes ImageBuilder with the specified args. |
| 6 | +
|
| 7 | +.PARAMETER ImageBuilderArgs |
| 8 | +The args to pass to ImageBuilder. |
| 9 | +
|
| 10 | +.PARAMETER ReuseImageBuilderImage |
| 11 | +Indicates that a previously built ImageBuilder image is presumed to exist locally and that |
| 12 | +it should be used for this execution of the script. This allows some optimization when |
| 13 | +multiple calls are being made to this script that don't require a fresh image (i.e. the |
| 14 | +repo contents in the image don't need to be or should not be updated with each call to |
| 15 | +this script). |
| 16 | +
|
| 17 | +.PARAMETER OnCommandExecuted |
| 18 | +A ScriptBlock that will be invoked after the ImageBuilder command has been executed. |
| 19 | +This allows the caller to execute extra logic in the context of the ImageBuilder while |
| 20 | +its container is still running. |
| 21 | +The ScriptBlock is passed the following argument values: |
| 22 | + 1. Container name |
| 23 | +#> |
| 24 | +[cmdletbinding()] |
| 25 | +param( |
| 26 | + [string] |
| 27 | + $ImageBuilderArgs, |
| 28 | + |
| 29 | + [switch] |
| 30 | + $ReuseImageBuilderImage, |
| 31 | + |
| 32 | + [scriptblock] |
| 33 | + $OnCommandExecuted |
| 34 | +) |
| 35 | + |
| 36 | +Set-StrictMode -Version Latest |
| 37 | +$ErrorActionPreference = 'Stop' |
| 38 | + |
| 39 | +function Log { |
| 40 | + param ([string] $Message) |
| 41 | + |
| 42 | + Write-Output $Message |
| 43 | +} |
| 44 | + |
| 45 | +function Exec { |
| 46 | + param ([string] $Cmd) |
| 47 | + |
| 48 | + Log "Executing: '$Cmd'" |
| 49 | + Invoke-Expression $Cmd |
| 50 | + if ($LASTEXITCODE -ne 0) { |
| 51 | + $host.SetShouldExit($LASTEXITCODE) |
| 52 | + exit $LASTEXITCODE |
| 53 | + throw "Failed: '$Cmd'" |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +$imageBuilderContainerName = "ImageBuilder-$(Get-Date -Format yyyyMMddhhmmss)" |
| 58 | +$containerCreated = $false |
| 59 | + |
| 60 | +pushd $PSScriptRoot/../../ |
| 61 | +try { |
| 62 | + $activeOS = docker version -f "{{ .Server.Os }}" |
| 63 | + if ($activeOS -eq "linux") { |
| 64 | + # On Linux, ImageBuilder is run within a container. |
| 65 | + $imageBuilderImageName = "microsoft-dotnet-imagebuilder-withrepo" |
| 66 | + if ($ReuseImageBuilderImage -ne $True) { |
| 67 | + & ./eng/docker-tools/Get-ImageBuilder.ps1 |
| 68 | + Exec ("docker build -t $imageBuilderImageName --build-arg " ` |
| 69 | + + "IMAGE=${imageNames.imageBuilderName} -f eng/docker-tools/Dockerfile.WithRepo .") |
| 70 | + } |
| 71 | + |
| 72 | + $imageBuilderCmd = "docker run --name $imageBuilderContainerName -v /var/run/docker.sock:/var/run/docker.sock $imageBuilderImageName" |
| 73 | + $containerCreated = $true |
| 74 | + } |
| 75 | + else { |
| 76 | + # On Windows, ImageBuilder is run locally due to limitations with running Docker client within a container. |
| 77 | + # Remove when https://github.com/dotnet/docker-tools/issues/159 is resolved |
| 78 | + $imageBuilderFolder = ".Microsoft.DotNet.ImageBuilder" |
| 79 | + $imageBuilderCmd = [System.IO.Path]::Combine($imageBuilderFolder, "Microsoft.DotNet.ImageBuilder.exe") |
| 80 | + if (-not (Test-Path -Path "$imageBuilderCmd" -PathType Leaf)) { |
| 81 | + & ./eng/docker-tools/Get-ImageBuilder.ps1 |
| 82 | + Exec "docker create --name $imageBuilderContainerName ${imageNames.imageBuilderName}" |
| 83 | + $containerCreated = $true |
| 84 | + if (Test-Path -Path $imageBuilderFolder) |
| 85 | + { |
| 86 | + Remove-Item -Recurse -Force -Path $imageBuilderFolder |
| 87 | + } |
| 88 | + |
| 89 | + Exec "docker cp ${imageBuilderContainerName}:/image-builder $imageBuilderFolder" |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Exec "$imageBuilderCmd $ImageBuilderArgs" |
| 94 | + |
| 95 | + if ($OnCommandExecuted) { |
| 96 | + Invoke-Command $OnCommandExecuted -ArgumentList $imageBuilderContainerName |
| 97 | + } |
| 98 | +} |
| 99 | +finally { |
| 100 | + if ($containerCreated) { |
| 101 | + Exec "docker container rm -f $imageBuilderContainerName" |
| 102 | + } |
| 103 | + |
| 104 | + popd |
| 105 | +} |
0 commit comments