Skip to content

Commit baf51fe

Browse files
Update common Docker engineering infrastructure with latest
1 parent 9ce73ba commit baf51fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3669
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Use this Dockerfile to create an ImageBuilder image
2+
ARG IMAGE
3+
FROM $IMAGE
4+
5+
WORKDIR /repo
6+
COPY . .

eng/docker-tools/Dockerfile.syft

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ARG SYFT_IMAGE_NAME
2+
ARG TARGET_IMAGE_NAME
3+
4+
FROM ${SYFT_IMAGE_NAME} AS syft
5+
FROM ${TARGET_IMAGE_NAME} AS scan-image
6+
7+
FROM syft AS run-scan
8+
ARG TARGET_IMAGE_NAME
9+
ENV SYFT_CHECK_FOR_APP_UPDATE=0 \
10+
SYFT_SOURCE_NAME=${TARGET_IMAGE_NAME}
11+
USER root
12+
RUN --mount=from=scan-image,source=/,target=/rootfs \
13+
["/syft", "scan", "/rootfs/", "--select-catalogers", "image", "--output", "spdx-json=/manifest.spdx.json"]
14+
15+
FROM scratch AS output
16+
COPY --from=run-scan /manifest.spdx.json /manifest.spdx.json
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env pwsh
2+
3+
<#
4+
.SYNOPSIS
5+
Outputs the status of external base images referenced in the Dockerfiles.
6+
#>
7+
[cmdletbinding()]
8+
param(
9+
# Path to the manifest file to use
10+
[string]
11+
$Manifest = "manifest.json",
12+
13+
# Architecture to filter Dockerfiles to
14+
[string]
15+
$Architecture = "*",
16+
17+
# A value indicating whether to run the script continously
18+
[switch]
19+
$Continuous,
20+
21+
# Number of seconds to wait between each iteration
22+
[int]
23+
$ContinuousDelay = 10
24+
)
25+
26+
Set-StrictMode -Version Latest
27+
28+
$imageBuilderArgs = "getBaseImageStatus --manifest $Manifest --architecture $Architecture"
29+
if ($Continuous) {
30+
$imageBuilderArgs += " --continuous --continuous-delay $ContinuousDelay"
31+
}
32+
33+
& "$PSScriptRoot/Invoke-ImageBuilder.ps1" -ImageBuilderArgs $imageBuilderArgs
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env pwsh
2+
3+
# Load common image names
4+
$imageNameVars = & $PSScriptRoot/Get-ImageNameVars.ps1
5+
foreach ($varName in $imageNameVars.Keys) {
6+
Set-Variable -Name $varName -Value $imageNameVars[$varName] -Scope Global
7+
}
8+
9+
& docker inspect ${imageNames.imagebuilderName} | Out-Null
10+
if (-not $?) {
11+
Write-Output "Pulling"
12+
& $PSScriptRoot/Invoke-WithRetry.ps1 "docker pull ${imageNames.imagebuilderName}"
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Returns a hashtable of variable name-to-value mapping representing the image name variables
2+
# used by the common build infrastructure.
3+
4+
$vars = @{}
5+
Get-Content $PSScriptRoot/templates/variables/docker-images.yml |
6+
Where-Object { $_.Trim().Length -gt 0 -and $_.Trim() -notlike 'variables:' -and $_.Trim() -notlike '# *' } |
7+
ForEach-Object {
8+
$parts = $_.Split(':', 2)
9+
$vars[$parts[0].Trim()] = $parts[1].Trim()
10+
}
11+
12+
return $vars
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env pwsh
2+
#
3+
# Copyright (c) .NET Foundation and contributors. All rights reserved.
4+
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
5+
#
6+
7+
<#
8+
.SYNOPSIS
9+
Install the .NET Core SDK at the specified path.
10+
11+
.PARAMETER InstallPath
12+
The path where the .NET Core SDK is to be installed.
13+
14+
.PARAMETER Channel
15+
The version of the .NET Core SDK to be installed.
16+
17+
#>
18+
[cmdletbinding()]
19+
param(
20+
[string]
21+
$InstallPath,
22+
[string]
23+
$Channel = "9.0"
24+
)
25+
26+
Set-StrictMode -Version Latest
27+
$ErrorActionPreference = 'Stop'
28+
29+
if (!(Test-Path "$InstallPath")) {
30+
mkdir "$InstallPath" | Out-Null
31+
}
32+
33+
$IsRunningOnUnix = $PSVersionTable.contains("Platform") -and $PSVersionTable.Platform -eq "Unix"
34+
if ($IsRunningOnUnix) {
35+
$DotnetInstallScript = "dotnet-install.sh"
36+
}
37+
else {
38+
$DotnetInstallScript = "dotnet-install.ps1"
39+
}
40+
41+
$DotnetInstallScriptPath = Join-Path -Path $InstallPath -ChildPath $DotnetInstallScript
42+
43+
if (!(Test-Path $DotnetInstallScriptPath)) {
44+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
45+
& "$PSScriptRoot/Invoke-WithRetry.ps1" "Invoke-WebRequest 'https://builds.dotnet.microsoft.com/dotnet/scripts/v1/$DotnetInstallScript' -OutFile $DotnetInstallScriptPath"
46+
}
47+
48+
$DotnetChannel = $Channel
49+
50+
$InstallFailed = $false
51+
if ($IsRunningOnUnix) {
52+
& chmod +x $DotnetInstallScriptPath
53+
& "$PSScriptRoot/Invoke-WithRetry.ps1" "$DotnetInstallScriptPath --channel $DotnetChannel --install-dir $InstallPath" -Retries 5
54+
$InstallFailed = ($LASTEXITCODE -ne 0)
55+
}
56+
else {
57+
& "$PSScriptRoot/Invoke-WithRetry.ps1" "$DotnetInstallScriptPath -Channel $DotnetChannel -InstallDir $InstallPath" -Retries 5
58+
$InstallFailed = (-not $?)
59+
}
60+
61+
# See https://github.com/NuGet/NuGet.Client/pull/4259
62+
$Env:NUGET_EXPERIMENTAL_CHAIN_BUILD_RETRY_POLICY = "6,1500"
63+
64+
if ($InstallFailed) { throw "Failed to install the .NET Core SDK" }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Set-StrictMode -Version Latest
2+
$ErrorActionPreference = 'Stop'
3+
4+
docker ps -a -q | ForEach-Object { docker rm -f $_ }
5+
6+
docker volume prune -f
7+
8+
# Preserve the tagged Windows base images and the common eng infra images (e.g. ImageBuilder)
9+
# to avoid the expense of having to repull continuously.
10+
$imageNameVars = & $PSScriptRoot/Get-ImageNameVars.ps1
11+
12+
docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" |
13+
Where-Object {
14+
$localImage = $_
15+
$localImage.Contains(":<none> ")`
16+
-Or -Not ($localImage.StartsWith("mcr.microsoft.com/windows")`
17+
-Or ($imageNameVars.Values.Where({ $localImage.StartsWith($_) }, 'First').Count -gt 0)) } |
18+
ForEach-Object { $_.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)[1] } |
19+
Select-Object -Unique |
20+
ForEach-Object { docker rmi -f $_ }
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env pwsh
2+
3+
# Executes a command and retries if it fails.
4+
[cmdletbinding()]
5+
param (
6+
[Parameter(Mandatory = $true)][string]$Cmd,
7+
[int]$Retries = 2,
8+
[int]$WaitFactor = 6
9+
)
10+
11+
Set-StrictMode -Version Latest
12+
$ErrorActionPreference = 'Stop'
13+
14+
$count = 0
15+
$completed = $false
16+
17+
Write-Output "Executing '$Cmd'"
18+
19+
while (-not $completed) {
20+
try {
21+
Invoke-Expression $Cmd
22+
if (-not $(Test-Path variable:LASTEXITCODE) -or $LASTEXITCODE -eq 0) {
23+
$completed = $true
24+
continue
25+
}
26+
}
27+
catch {
28+
}
29+
30+
$count++
31+
32+
if ($count -lt $Retries) {
33+
$wait = [Math]::Pow($WaitFactor, $count - 1)
34+
Write-Output "Retry $count/$Retries, retrying in $wait seconds..."
35+
Start-Sleep $wait
36+
}
37+
else {
38+
Write-Output "Retry $count/$Retries, no more retries left."
39+
throw "Failed to execute '$Cmd'"
40+
}
41+
}

eng/docker-tools/Pull-Image.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env pwsh
2+
3+
[cmdletbinding()]
4+
param(
5+
[Parameter(Mandatory = $true, Position = 0)]
6+
[string]$Image,
7+
8+
[Parameter(Mandatory = $false)]
9+
[int]$Retries = 2,
10+
11+
[Parameter(Mandatory = $false)]
12+
[int]$WaitFactor = 6
13+
)
14+
15+
Set-StrictMode -Version Latest
16+
$ErrorActionPreference = 'Stop'
17+
18+
& "$PSScriptRoot/Invoke-WithRetry.ps1" "docker pull $Image" -Retries $Retries -WaitFactor $WaitFactor

0 commit comments

Comments
 (0)