]> git.proxmox.com Git - ceph.git/blob - ceph/qa/workunits/windows/libvirt_vm/utils.ps1
update ceph source to reef 18.1.2
[ceph.git] / ceph / qa / workunits / windows / libvirt_vm / utils.ps1
1 function Invoke-CommandLine {
2 Param(
3 [Parameter(Mandatory=$true)]
4 [String]$Command,
5 [String]$Arguments,
6 [Int[]]$AllowedExitCodes=@(0)
7 )
8 & $Command $Arguments.Split(" ")
9 if($LASTEXITCODE -notin $AllowedExitCodes) {
10 Throw "$Command $Arguments returned a non zero exit code ${LASTEXITCODE}."
11 }
12 }
13
14 function Start-ExecuteWithRetry {
15 Param(
16 [Parameter(Mandatory=$true)]
17 [ScriptBlock]$ScriptBlock,
18 [Int]$MaxRetryCount=10,
19 [Int]$RetryInterval=3,
20 [String]$RetryMessage,
21 [Array]$ArgumentList=@()
22 )
23 $currentErrorActionPreference = $ErrorActionPreference
24 $ErrorActionPreference = "Continue"
25 $retryCount = 0
26 while ($true) {
27 try {
28 $res = Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList
29 $ErrorActionPreference = $currentErrorActionPreference
30 return $res
31 } catch [System.Exception] {
32 $retryCount++
33 if ($retryCount -gt $MaxRetryCount) {
34 $ErrorActionPreference = $currentErrorActionPreference
35 Throw $_
36 } else {
37 $prefixMsg = "Retry(${retryCount}/${MaxRetryCount})"
38 if($RetryMessage) {
39 Write-Host "${prefixMsg} - $RetryMessage"
40 } elseif($_) {
41 Write-Host "${prefixMsg} - $($_.ToString())"
42 }
43 Start-Sleep $RetryInterval
44 }
45 }
46 }
47 }
48
49 function Start-FileDownload {
50 Param(
51 [Parameter(Mandatory=$true)]
52 [String]$URL,
53 [Parameter(Mandatory=$true)]
54 [String]$Destination,
55 [Int]$RetryCount=10
56 )
57 Write-Output "Downloading $URL to $Destination"
58 Start-ExecuteWithRetry `
59 -ScriptBlock { Invoke-CommandLine -Command "curl.exe" -Arguments "-L -s -o $Destination $URL" } `
60 -MaxRetryCount $RetryCount `
61 -RetryMessage "Failed to download '${URL}'. Retrying"
62 Write-Output "Successfully downloaded."
63 }
64
65 function Add-ToPathEnvVar {
66 Param(
67 [Parameter(Mandatory=$true)]
68 [String[]]$Path,
69 [Parameter(Mandatory=$false)]
70 [ValidateSet([System.EnvironmentVariableTarget]::User, [System.EnvironmentVariableTarget]::Machine)]
71 [System.EnvironmentVariableTarget]$Target=[System.EnvironmentVariableTarget]::Machine
72 )
73 $pathEnvVar = [Environment]::GetEnvironmentVariable("PATH", $Target).Split(';')
74 $currentSessionPath = $env:PATH.Split(';')
75 foreach($p in $Path) {
76 if($p -notin $pathEnvVar) {
77 $pathEnvVar += $p
78 }
79 if($p -notin $currentSessionPath) {
80 $currentSessionPath += $p
81 }
82 }
83 $env:PATH = $currentSessionPath -join ';'
84 $newPathEnvVar = $pathEnvVar -join ';'
85 [Environment]::SetEnvironmentVariable("PATH", $newPathEnvVar, $Target)
86 }
87
88 function Install-Tool {
89 [CmdletBinding(DefaultParameterSetName = "URL")]
90 Param(
91 [Parameter(Mandatory=$true, ParameterSetName = "URL")]
92 [String]$URL,
93 [Parameter(Mandatory=$true, ParameterSetName = "LocalPath")]
94 [String]$LocalPath,
95 [Parameter(ParameterSetName = "URL")]
96 [Parameter(ParameterSetName = "LocalPath")]
97 [String[]]$Params=@(),
98 [Parameter(ParameterSetName = "URL")]
99 [Parameter(ParameterSetName = "LocalPath")]
100 [Int[]]$AllowedExitCodes=@(0)
101 )
102 PROCESS {
103 $installerPath = $LocalPath
104 if($PSCmdlet.ParameterSetName -eq "URL") {
105 $installerPath = Join-Path $env:TEMP $URL.Split('/')[-1]
106 Start-FileDownload -URL $URL -Destination $installerPath
107 }
108 Write-Output "Installing ${installerPath}"
109 $kwargs = @{
110 "FilePath" = $installerPath
111 "ArgumentList" = $Params
112 "NoNewWindow" = $true
113 "PassThru" = $true
114 "Wait" = $true
115 }
116 if((Get-ChildItem $installerPath).Extension -eq '.msi') {
117 $kwargs["FilePath"] = "msiexec.exe"
118 $kwargs["ArgumentList"] = @("/i", $installerPath) + $Params
119 }
120 $p = Start-Process @kwargs
121 if($p.ExitCode -notin $AllowedExitCodes) {
122 Throw "Installation failed. Exit code: $($p.ExitCode)"
123 }
124 if($PSCmdlet.ParameterSetName -eq "URL") {
125 Start-ExecuteWithRetry `
126 -ScriptBlock { Remove-Item -Force -Path $installerPath -ErrorAction Stop } `
127 -RetryMessage "Failed to remove ${installerPath}. Retrying"
128 }
129 }
130 }