1
#!/usr/bin/env powershell
3
# This powershell script is intended to be "dot-sourced" by other scripts.
4
# It's purpose is identical to that of the `lib.sh` script for Linux environments.
6
# Behave similar to `set -e` in bash, but ONLY for powershell commandlets!
7
# For all legacy, program, and script calls use Run-Command() or Check-Exit()
8
$ErrorActionPreference = 'Stop'
10
# Any golang compilation needs to know what it's building for.
14
# Unnecessary and intrusive. They claim parameter/variable
15
# values aren't collected, but there could be a bug leading
16
# to a concern over leaking of some sensitive-value. Stop this.
17
$Env:POWERSHELL_TELEMETRY_OPTOUT = "true"
19
# Unnecessary and potentially disruptive. Powershell will
20
# never ever be updated during automation execution. Stop this.
21
$Env:POWERSHELL_UPDATECHECK = "off"
23
# Color in output may confuse tooling and makes logs hard to read.
24
# TODO: There are probably other places where color needs to be disabled
25
# in a slightly different way :(
28
# Items only relevant in a CI environment.
29
if ($Env:CI -eq "true") {
30
# Defined by .cirrus.yml for use by all the linux tasks.
31
# Drop all global envs which have unix paths, defaults are fine.
32
Remove-Item Env:\GOPATH -ErrorAction:Ignore
33
Remove-Item Env:\GOSRC -ErrorAction:Ignore
34
Remove-Item Env:\GOCACHE -ErrorAction:Ignore
36
# Defined by Cirrus-CI
37
# Drop large known env variables (an env > 32k will break MSI/ICE validation)
38
Remove-Item Env:\CIRRUS_COMMIT_MESSAGE -ErrorAction:Ignore
39
Remove-Item Env:\CIRRUS_CHANGE_MESSAGE -ErrorAction:Ignore
40
Remove-Item Env:\CIRRUS_PR_BODY -ErrorAction:Ignore
43
function Invoke-Logformatter {
45
[Collections.ArrayList] $unformattedLog
48
Write-Host "Invoking Logformatter"
49
$logFormatterInput = @('/define.gitCommit=' + $(git rev-parse HEAD)) + $unformattedLog
50
$logformatterPath = "$PSScriptRoot\logformatter"
51
if ($Env:TEST_FLAVOR) {
52
$logformatterArg = "$Env:TEST_FLAVOR-podman-windows-rootless-host-sqlite"
54
$logformatterArg = "podman-windows-rootless-host-sqlite"
56
$null = $logFormatterInput | perl $logformatterPath $logformatterArg
57
$logformatterGeneratedFile = "$logformatterArg.log.html"
58
if (Test-Path $logformatterGeneratedFile) {
59
Move-Item $logformatterGeneratedFile .. -Force
61
Write-Host "Logformatter did not generate the expected file: $logformatterGeneratedFile"
65
# Non-powershell commands do not halt execution on error! This helper
66
# should be called after every critical operation to check and halt on a
67
# non-zero exit code. Be careful not to use this for powershell commandlets
68
# (builtins)! They set '$?' to "True" (failed) or "False" success so calling
69
# this would mask failures. Rely on $ErrorActionPreference = 'Stop' instead.
73
[string] $command = 'command',
74
[string] $exitCode = $LASTEXITCODE # WARNING: might not be a number!
77
if ( ($exitCode -ne $null) -and ($exitCode -ne 0) ) {
78
# https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.callstackframe
79
$caller = (Get-PSCallStack)[$stackPos]
80
throw "Exit code = '$exitCode' running $command at $($caller.ScriptName):$($caller.ScriptLineNumber)"
84
# Small helper to avoid needing to write 'Check-Exit' after every
85
# non-powershell instruction. It simply prints then executes the _QUOTED_
86
# argument followed by Check-Exit.
87
# N/B: Escape any nested quotes with back-tick ("`") characters.
88
# WARNING: DO NOT use this with powershell builtins! It will not do what you expect!
96
# The command output is saved into the variable $unformattedLog to be
97
# processed by `logformatter` later. The alternative is to redirect the
98
# command output to logformatter using a pipeline (`|`). But this approach
99
# doesn't work as the command exit code would be overridden by logformatter.
100
# It isn't possible to get a behavior of bash `pipefail` on Windows.
101
Invoke-Expression $command -OutVariable unformattedLog | Write-Output
103
$exitCode = $LASTEXITCODE
105
if ($Env:CIRRUS_CI -eq "true") {
106
Invoke-Logformatter $unformattedLog
109
Check-Exit 2 "'$command'" "$exitCode"