File: winmake.ps1

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (530 lines) | stat: -rw-r--r-- 20,331 bytes parent folder | download | duplicates (2)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530

#!/usr/bin/env powershell
[CmdletBinding(PositionalBinding = $false)]
param (
    [ValidateSet('amd64', 'arm64')]
    [Alias('arch')]
    [string]$architecture = $(
        $defaultArchitecture = 'amd64'
        $arch = try { & go env GOARCH } catch {
            Write-Warning "Failed retriving the host architecture, using default ($defaultArchitecture). Is Go installed?"
            return $defaultArchitecture
        }
        if ($arch -cnotin @('arm64', 'amd64')) {
            Write-Warning "Unsupported architecture $arch. Using default ($defaultArchitecture)."
            return $defaultArchitecture
        }
        return $arch
    ),
    [parameter(ValueFromRemainingArguments)][object[]]$params = @()
)

. ./contrib/cirrus/win-lib.ps1

# Targets
function Podman-Remote {
    New-Item -ItemType Directory -Force -Path './bin/windows'

    $buildInfo = Get-Date -UFormat %s -Millisecond 0
    $buildInfo = "-X github.com/containers/podman/v5/libpod/define.buildInfo=$buildInfo "
    $commit = Git-Commit
    $commit = "-X github.com/containers/podman/v5/libpod/define.gitCommit=$commit "

    $ENV:GOARCH = $architecture
    Run-Command "go build --ldflags `"$commit $buildInfo `" --tags `"$remotetags`" --o ./bin/windows/podman.exe ./cmd/podman/."
}

function Make-Clean {
    $paths = @(
        # Files generated by the `podman` target
        "$PSScriptRoot\bin\windows"
        # Files generated by the `installer` target
        "$PSScriptRoot\test\version\version.exe"
        "$PSScriptRoot\contrib\win-installer\artifacts"
        "$PSScriptRoot\contrib\win-installer\current"
        "$PSScriptRoot\contrib\win-installer\docs"
        "$PSScriptRoot\contrib\win-installer\fetch"
        "$PSScriptRoot\contrib\win-installer\wix\obj"
        "$PSScriptRoot\contrib\win-installer\*.log"
        "$PSScriptRoot\contrib\win-installer\*.msi"
        "$PSScriptRoot\contrib\win-installer\*.wixpdb"
        "$PSScriptRoot\contrib\win-installer\shasums"
        # Files generated by the `installer-legacy` target
        "$PSScriptRoot\contrib\win-installer-legacy\artifacts"
        "$PSScriptRoot\contrib\win-installer-legacy\current"
        "$PSScriptRoot\contrib\win-installer-legacy\docs"
        "$PSScriptRoot\contrib\win-installer-legacy\en-us"
        "$PSScriptRoot\contrib\win-installer-legacy\fetch"
        "$PSScriptRoot\contrib\win-installer-legacy\obj"
        "$PSScriptRoot\contrib\win-installer-legacy\*.log"
        "$PSScriptRoot\contrib\win-installer-legacy\*.exe"
        "$PSScriptRoot\contrib\win-installer-legacy\*.wixpdb"
        # Files generated by the Documentation target
        "$PSScriptRoot\docs\build\remote\podman-*.html"
        "$PSScriptRoot\docs\build\remote\podman-for-windows.html"
    )

    foreach ($path in $paths) {
        if (Test-Path -Path $path -PathType Container) {
            Remove-Item $path -Recurse -Force -Confirm:$false
        }
        elseif (Test-Path -Path $path -PathType Leaf) {
            Remove-Item $path -Force -Confirm:$false
        }
    }
}

function Local-Unit {
    Build-Ginkgo
    $skippackages = 'hack,internal\domain\infra\abi,internal\domain\infra\tunnel,libpod\lock\shm,pkg\api\handlers\libpod,pkg\api\handlers\utils,pkg\bindings,'
    $skippackages += 'pkg\domain\infra\abi,pkg\emulation,pkg\machine\apple,pkg\machine\applehv,pkg\machine\e2e,pkg\machine\libkrun,'
    $skippackages += 'pkg\machine\provider,pkg\machine\proxyenv,pkg\machine\qemu,pkg\specgen\generate,pkg\systemd,test\e2e,test\utils,cmd\rootlessport,'
    $skippackages += 'pkg\pidhandle'
    if ($null -eq $ENV:GINKGOTIMEOUT) { $ENV:GINKGOTIMEOUT = '--timeout=15m' }
    Run-Command "./bin/ginkgo.exe -vv -r --tags `"$remotetags`" ${ENV:GINKGOTIMEOUT} --trace --no-color --skip-package `"$skippackages`""
}

function Local-Machine {
    param (
        [string]$files
    );
    Build-Ginkgo
    if ($files) {
        $files = "--focus-file ""$files"""
    }
    elseif ($FOCUS_FILE) {
        $files = "--focus-file ""$FOCUS_FILE"" --silence-skips"
    }
    if ($FOCUS) {
        $focus = "--focus ""$FOCUS"" --silence-skips"
    }

    if ($null -eq $ENV:GINKGOTIMEOUT) { $ENV:GINKGOTIMEOUT = '--timeout=50m' }
    Run-Command "./bin/ginkgo.exe -vv --tags `"$remotetags`" ${ENV:GINKGOTIMEOUT} --trace --no-color $focus $files pkg/machine/e2e/."
}

# Expect starting directory to be /podman
function Win-SSHProxy {
    param (
        [string]$Version
    );

    New-Item -ItemType Directory -Force -Path './bin/windows'
    if (-Not $Version) {
        $match = Select-String -Path "$PSScriptRoot\go.mod" -Pattern 'github.com/containers/gvisor-tap-vsock\s+(v[\d\.]+)'
        $Version = $match.Matches.Groups[1].Value
    }
    Write-Host "Downloading gvproxy version $version"
    if ($architecture -eq 'amd64') {
        curl.exe -sSL -o './bin/windows/gvproxy.exe' --retry 5 "https://github.com/containers/gvisor-tap-vsock/releases/download/$Version/gvproxy-windowsgui.exe"
        curl.exe -sSL -o './bin/windows/win-sshproxy.exe' --retry 5 "https://github.com/containers/gvisor-tap-vsock/releases/download/$Version/win-sshproxy.exe"
    }
    else {
        curl.exe -sSL -o './bin/windows/gvproxy.exe' --retry 5 "https://github.com/containers/gvisor-tap-vsock/releases/download/$Version/gvproxy-windows-arm64.exe"
        curl.exe -sSL -o './bin/windows/win-sshproxy.exe' --retry 5 "https://github.com/containers/gvisor-tap-vsock/releases/download/$Version/win-sshproxy-arm64.exe"
    }
}

function Installer {
    param (
        [string]$version,
        [string]$suffix = 'dev',
        [string]$installerPath = "$PSScriptRoot\contrib\win-installer"
    );
    Write-Host "Building the windows installer for $architecture"

    # Check for the files to include in the installer
    $requiredArtifacts = @(
        "$PSScriptRoot\bin\windows\podman.exe"
        "$PSScriptRoot\bin\windows\gvproxy.exe"
        "$PSScriptRoot\bin\windows\win-sshproxy.exe"
        "$PSScriptRoot\docs\build\remote\podman-for-windows.html"
    )
    $requiredArtifacts | ForEach-Object {
        if (!(Test-Path -Path $PSItem -PathType Leaf)) {
            Write-Host "$PSItem not found."
            Write-Host "Make 'podman', 'win-gvproxy' and 'docs' (or 'docs-using-podman') before making the installer:"
            Write-Host '   .\winmake.ps1 podman-remote'
            Write-Host '   .\winmake.ps1 win-gvproxy'
            Write-Host '   .\winmake.ps1 docs or .\winmake.ps1 docs-using-podman'
            Exit 1
        }
    }

    # Create the ZIP file with the full client distribution
    $zipFileDest = "$installerPath\current"
    Build-Distribution-Zip-File -destinationPath $zipFileDest

    if (-Not $version) {
        # Get Podman version from local source code
        $version = Get-Podman-Version
    }

    # Run build.ps1
    if ($installerPath -eq "$PSScriptRoot\contrib\win-installer-legacy") {
        $ENV:PODMAN_ARCH = $architecture # This is used by the "build.ps1" script
        Run-Command "$installerPath\build.ps1 $version $suffix `"$zipFileDest`""
    }
    else {
        Run-Command "$installerPath\build.ps1 -Version $version -Architecture $architecture -LocalReleaseDirPath `"$zipFileDest`""
    }
}

function Test-Installer {
    param (
        [string]$version,
        [ValidateSet('wsl', 'hyperv')]
        [string]$provider = 'wsl'
    );

    if (-Not $version) {
        # Get Podman version from local source code
        $version = Get-Podman-Version
    }

    $msiPath = "$PSScriptRoot\contrib\win-installer\podman-${version}.msi"
    if (!(Test-Path -Path $msiPath -PathType Leaf)) {
        Write-Host "MSI executable not found in path $msiPath."
        Write-Host "Make 'installer' before making the installer test:"
        Write-Host '   .\winmake.ps1 installer'
        Exit 1
    }

    $nextMsiPath = "$PSScriptRoot\contrib\win-installer\podman-9.9.9.msi"
    if (!(Test-Path -Path $nextMsiPath -PathType Leaf)) {
        Write-Host 'The automated tests include testing the upgrade from current version to a future version.'
        Write-Host 'That requires a version 9.9.9 of the installer:'
        Write-Host '   .\winmake.ps1 installer 9.9.9'
        Write-Host 'Build it and retry running installertest.'
        Exit 1
    }

    $command = "$PSScriptRoot\contrib\win-installer\test.ps1"
    $command += ' -scenario all'
    $command += " -provider $provider"
    $command += " -msiPath $msiPath"
    $command += " -nextMsiPath $nextMsiPath"
    Run-Command "${command}"
}

function Test-Installer-Legacy {
    param (
        [string]$version,
        [ValidateSet('dev', 'prod')]
        [string]$flavor = 'dev',
        [ValidateSet('wsl', 'hyperv')]
        [string]$provider = 'wsl'
    );

    if (-Not $version) {
        # Get Podman version from local source code
        $version = Get-Podman-Version
    }

    if ($flavor -eq 'prod') {
        $suffix = ''
    }
    else {
        $suffix = '-dev'
    }

    $setupExePath = "$PSScriptRoot\contrib\win-installer-legacy\podman-${version}${suffix}-setup.exe"
    if (!(Test-Path -Path $setupExePath -PathType Leaf)) {
        Write-Host "Setup executable not found in path $setupExePath."
        Write-Host "Make 'installer' before making the installer test:"
        Write-Host '   .\winmake.ps1 installer-legacy'
        Exit 1
    }

    $nextSetupExePath = "$PSScriptRoot\contrib\win-installer-legacy\podman-9.9.9-dev-setup.exe"
    if (!(Test-Path -Path $nextSetupExePath -PathType Leaf)) {
        Write-Host 'The automated tests include testing the upgrade from current version to a future version.'
        Write-Host 'That requires a version 9.9.9 of the installer:'
        Write-Host '   .\winmake.ps1 installer-legacy 9.9.9'
        Write-Host 'Build it and retry running installertest.'
        Exit 1
    }

    $command = "$PSScriptRoot\contrib\win-installer-legacy\test-installer.ps1"
    $command += ' -scenario all'
    $command += " -provider $provider"
    $command += " -setupExePath $setupExePath"
    $command += " -nextSetupExePath $nextSetupExePath"
    Run-Command "${command}"
}

function Documentation {
    Write-Host 'Generating the documentation artifacts'
    # Check that pandoc is installed
    if (!(Get-Command -Name 'pandoc' -ErrorAction SilentlyContinue)) {
        Write-Host 'Pandoc not found. Pandoc is required to convert the documentation Markdown files into HTML files.'
        Write-Host "Alternatively, use '.\winmake docs-using-podman' to use a container to run pandoc and generate the documentation."
        Exit 1
    }
    # Check that the podman client is built
    $podmanClient = "$PSScriptRoot\bin\windows\podman.exe"
    if (!(Test-Path -Path $podmanClient -PathType Leaf)) {
        Write-Host "$podmanClient not found. Make 'podman-remote' before 'documentation'."
        Exit 1
    }
    Run-Command "$PSScriptRoot\docs\make.ps1 $podmanClient"
}

# DocumentationUsingPodman generates documentation with pandoc running in a container.
# This is usefult on Windows arm64 where pandoc is not available.
# It's also useful to generate documentation identical to CI.
# It requires the podman client to be built and a podman machine running.
# The whole podman git repository is bind mounted in the container at /podman.
# The documentation is generated by running the command `make podman-remote-windows-docs`.
# The generated documentation is stored in the directory docs/build/remote.
function DocumentationUsingPodman {
    Write-Host 'Generating documentation artifacts'
    # Check that podman has been built
    $podmanClient = "${PSScriptRoot}\bin\windows\podman.exe"
    if (!(Test-Path -Path $podmanClient -PathType Leaf)) {
        Write-Host "$podmanClient not found. Make 'podman-remote' before 'documentation'."
        Exit 1
    }
    # Check that a podman machine exist
    $currentMachine = (& ${podmanClient} machine info -f json | ConvertFrom-Json).Host.CurrentMachine
    if (!$currentMachine) {
        Write-Host "Podman machine doesn't exist. Initialize and start one before running the validate script."
        Exit 1
    }
    # Check that the podman machine is running
    $state = (& ${podmanClient} machine info -f json | ConvertFrom-Json).Host.MachineState
    if ($state -ne 'Running') {
        Write-Host 'Podman machine is not running. Start the machine before running the validate script.'
        Exit 1
    }

    Write-Host 'Building the image to generate the documentation'
    Run-Command "${podmanClient} build --build-arg TARGET_OS=windows -t podman-docs-generator ${PSScriptRoot}/docs"

    Write-Host 'Starting the container to run the documentation build'
    Run-Command "${podmanClient} run -t --rm -v ${PSScriptRoot}:/podman podman-docs-generator"
}

function Validate {
    $podmanExecutable = 'podman'
    $podmanSrcVolumeMount = "${PSScriptRoot}:/go/src/github.com/containers/podman"
    # All files bind mounted from a Windows host are marked as executable.
    # That makes the pre-commit hook "check-executables-have-shebangs" fail.
    # Setting the environment variable "SKIP=check-executables-have-shebangs"
    # allow to skip that pre-commit hook.
    $podmanEnvVariable = '-e SKIP=check-executables-have-shebangs'
    $podmanRunArgs = "--rm -v $podmanSrcVolumeMount --security-opt label=disable -t -w /go/src/github.com/containers/podman $podmanEnvVariable"
    $validateImage = 'quay.io/libpod/validatepr:latest'
    $validateCommand = 'make .validatepr'

    # Check that podman is installed
    if (!(Get-Command -Name $podmanExecutable -ErrorAction SilentlyContinue)) {
        Write-Host "$podmanExecutable not found. $podmanExecutable is required to run the validate script."
        Exit 1
    }

    # Check that a podman machine exist
    $currentMachine = (podman machine info -f json | ConvertFrom-Json).Host.CurrentMachine
    if (!$currentMachine) {
        Write-Host "Podman machine doesn't exist. Initialize and start one before running the validate script."
        Exit 1
    }

    # Check that the podman machine is running
    $state = (podman machine info -f json | ConvertFrom-Json).Host.MachineState
    if ($state -ne 'Running') {
        Write-Host 'Podman machine is not running. Start the machine before running the validate script.'
        Exit 1
    }

    Run-Command "$podmanExecutable run $podmanRunArgs $validateImage $validateCommand"
}

function Lint {
    # Check that golangci-lint is installed
    if (!(Get-Command -Name 'golangci-lint' -ErrorAction SilentlyContinue)) {
        Write-Host 'The tool "golangci-lint" not found. Install https://golangci-lint.run/ before running the lint script.'
        Exit 1
    }

    # Check that pre-commit is installed
    if (!(Get-Command -Name 'pre-commit' -ErrorAction SilentlyContinue)) {
        Write-Host 'The tool "pre-commit" not found. Install https://pre-commit.com/ before running the lint script.'
        Exit 1
    }

    Run-Command 'pre-commit run --all-files'
    Run-Command "golangci-lint run --timeout=10m --build-tags=`"$remotetags`" $PSScriptRoot\cmd\podman"
}

# Helpers
function Build-Ginkgo {
    if (Test-Path -Path ./bin/ginkgo.exe -PathType Leaf) {
        return
    }
    Write-Host 'Building Ginkgo'
    Run-Command 'go build -o ./bin/ginkgo.exe ./vendor/github.com/onsi/ginkgo/v2/ginkgo'
}

function Git-Commit {
    # git is not installed by default on windows,
    # so if we can't get the commit, we don't include this info
    Get-Command git -ErrorAction SilentlyContinue | out-null
    if (!$?) {
        return
    }
    $commit = git rev-parse HEAD
    $dirty = git status --porcelain --untracked-files=no
    if ($dirty) {
        $commit = "$commit-dirty"
    }
    return $commit
}

function Build-Distribution-Zip-File {
    param (
        [string]$destinationPath
    );
    $binariesFolder = "$PSScriptRoot\bin\windows"
    $documentationFolder = "$PSScriptRoot\docs\build\remote\"
    $zipFile = "$destinationPath\podman-remote-release-windows_$architecture.zip"

    # Create a temporary folder to store the distribution files
    $tempFolder = New-Item -ItemType Directory -Force -Path "$env:TEMP\podman-windows"

    # Copy bin\windows\ content to the temporary folder
    Copy-Item -Recurse -Force -Path "$binariesFolder\*" -Destination "$tempFolder\"

    # Copy docs\build\remote to the temporary folder
    Copy-Item -Recurse -Force -Path "$documentationFolder" -Destination "$tempFolder\docs\"

    # If $destination path doesn't exist, create it
    if (-Not (Test-Path -Path $destinationPath -PathType Container)) {
        New-Item -ItemType Directory -Force -Path $destinationPath
    }

    # Create the ZIP file with the full client distribution
    Compress-Archive -Path "$tempFolder\*" -DestinationPath $zipFile -Force

    # Delete the temporary folder
    Remove-Item -Recurse -Force -Path "$tempFolder"
}

function Get-Podman-Version {
    $versionSrc = "$PSScriptRoot\test\version\"
    $versionBin = "$PSScriptRoot\test\version\version.exe"
    Run-Command "go build --o `"$versionBin`" `"$versionSrc`""
    $version = Invoke-Expression "$versionBin"
    # Remove the '-dev' suffix from the version
    $version = $version -replace '-.*', ''
    return $version
}

# Init script
$target = $params[0]

$remotetags = 'remote exclude_graphdriver_btrfs containers_image_openpgp'

switch ($target) {
    { $_ -in '', 'podman-remote', 'podman' } {
        Podman-Remote
    }
    'localunit' {
        Local-Unit
    }
    'localmachine' {
        if ($params.Count -gt 1) {
            $files = $params[1]
        }
        Local-Machine -files $files
    }
    'clean' {
        Make-Clean
    }
    { $_ -in 'win-sshproxy', 'win-gvproxy' } {
        if ($params.Count -gt 1) {
            $ref = $params[1]
        }
        Win-SSHProxy($ref)
    }
    'installer' {
        if ($params.Count -gt 1) {
            Installer -version $params[1]
        }
        else {
            Installer
        }
    }
    'installertest' {
        if ($params.Count -gt 1) {
            Test-Installer -provider $params[1]
        }
        else {
            Test-Installer
        }
    }
    'installer-legacy' {
        if ($params.Count -gt 1) {
            Installer -version $params[1] -installerPath $PSScriptRoot\contrib\win-installer-legacy
        }
        else {
            Installer -installerPath $PSScriptRoot\contrib\win-installer-legacy
        }
    }
    'installertest-legacy' {
        if ($params.Count -gt 1) {
            Test-Installer-Legacy -provider $params[1]
        }
        else {
            Test-Installer-Legacy
        }
    }
    'docs' {
        Documentation
    }
    'docs-using-podman' {
        DocumentationUsingPodman
    }
    'validatepr' {
        Validate
    }
    'lint' {
        Lint
    }
    default {
        Write-Host 'Usage: ' $MyInvocation.MyCommand.Name '<target> [options] [<-architecture|-arch>=<amd64|arm64>]'
        Write-Host
        Write-Host 'Example: Build podman-remote '
        Write-Host ' .\winmake podman-remote'
        Write-Host
        Write-Host 'Example: Run all unit tests '
        Write-Host ' .\winmake localunit'
        Write-Host
        Write-Host 'Example: Run all machine tests '
        Write-Host ' .\winmake localmachine'
        Write-Host
        Write-Host 'Example: Run specfic machine tests '
        Write-Host ' .\winmake localmachine 'basic_test.go""
        Write-Host
        Write-Host 'Example: Download win-gvproxy and win-sshproxy helpers'
        Write-Host ' .\winmake win-gvproxy'
        Write-Host
        Write-Host 'Example: Build the windows installer'
        Write-Host ' .\winmake installer'
        Write-Host
        Write-Host 'Example: Run windows installer tests'
        Write-Host ' .\winmake installertest hyperv'
        Write-Host
        Write-Host 'Example: Generate the documentation artifacts'
        Write-Host ' .\winmake docs'
        Write-Host
        Write-Host 'Example: Generate the documentation artifacts by running pandoc in a container'
        Write-Host ' .\winmake docs-using-podman'
        Write-Host
        Write-Host 'Example: Validate code changes before submitting a PR'
        Write-Host ' .\winmake validatepr'
        Write-Host
        Write-Host 'Example: Run linters'
        Write-Host ' .\winmake lint'
    }
}