podman

Форк
0
/
win-lib.ps1 
110 строк · 4.4 Кб
1
#!/usr/bin/env powershell
2

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.
5

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'
9

10
# Any golang compilation needs to know what it's building for.
11
$Env:GOOS = "windows"
12
$Env:GOARCH = "amd64"
13

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"
18

19
# Unnecessary and potentially disruptive.  Powershell will
20
# never ever be updated during automation execution.  Stop this.
21
$Env:POWERSHELL_UPDATECHECK = "off"
22

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 :(
26
$Env:NO_COLOR = "true"
27

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
35

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
41
}
42

43
function Invoke-Logformatter {
44
    param (
45
        [Collections.ArrayList] $unformattedLog
46
    )
47

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"
53
    } else {
54
        $logformatterArg = "podman-windows-rootless-host-sqlite"
55
    }
56
    $null =  $logFormatterInput | perl $logformatterPath $logformatterArg
57
    $logformatterGeneratedFile = "$logformatterArg.log.html"
58
    if (Test-Path $logformatterGeneratedFile) {
59
        Move-Item $logformatterGeneratedFile .. -Force
60
    } else {
61
        Write-Host "Logformatter did not generate the expected file: $logformatterGeneratedFile"
62
    }
63
}
64

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.
70
function Check-Exit {
71
    param (
72
        [int] $stackPos = 1,
73
        [string] $command = 'command',
74
        [string] $exitCode = $LASTEXITCODE # WARNING: might not be a number!
75
    )
76

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)"
81
    }
82
}
83

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!
89
function Run-Command {
90
    param (
91
        [string] $command
92
    )
93

94
    Write-Host $command
95

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
102

103
    $exitCode = $LASTEXITCODE
104

105
    if ($Env:CIRRUS_CI -eq "true") {
106
        Invoke-Logformatter $unformattedLog
107
    }
108

109
    Check-Exit 2 "'$command'" "$exitCode"
110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.