-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathnetperf-lib.psm1
More file actions
211 lines (196 loc) · 8.2 KB
/
netperf-lib.psm1
File metadata and controls
211 lines (196 loc) · 8.2 KB
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
function ValidateInheritedParams {
Write-Host "Inherited run id: $env:netperf_run_id"
Write-Host "Inherited role: $env:netperf_role"
Write-Host "Inherited remote_powershell_supported: $env:netperf_remote_powershell_supported"
Write-Host "Inherited api url: $env:netperf_api_url"
$headers = @{
'secret' = $env:netperf_syncer_secret
}
Invoke-WebRequest -Uri "$env:netperf_api_url/hello" -Headers $headers
}
function NetperfSendCommand {
param (
[Parameter(Mandatory = $true)]
[string]$Command,
[Parameter(Mandatory = $false)]
$Session
)
if ($Session -and $Session -ne "NOT_SUPPORTED") {
$CallBackName = $env:CallBackName
$RemoteDir = $env:RemoteDir
if ($isWindows) {
Write-Host "Sending command (via remote powershell): $RemoteDir/scripts/$CallBackName -Command $Command -WorkingDir $RemoteDir"
Invoke-Command -Session $Session -ScriptBlock {
& pwsh -NoProfile -NonInteractive -WorkingDirectory $Using:RemoteDir -File `
"$Using:RemoteDir/scripts/$Using:CallBackName" `
-Command $Using:Command `
-WorkingDir $Using:RemoteDir
}
} else {
Write-Host "Sending command (via remote powershell): sudo -n pwsh -NoProfile -NonInteractive -File $RemoteDir/scripts/$CallBackName -Command $Command -WorkingDir $RemoteDir"
Invoke-Command -Session $Session -ScriptBlock {
& sudo -n pwsh -NoProfile -NonInteractive -WorkingDirectory $Using:RemoteDir -File `
"$Using:RemoteDir/scripts/$Using:CallBackName" `
-Command $Using:Command `
-WorkingDir $Using:RemoteDir
}
}
return
}
Write-Host "Sending command (via remote cache): $Command"
# Should send a command to the shared cache and wait for the server process to execute said command before exiting.
$headers = @{
"secret" = "$env:netperf_syncer_secret"
}
$url = "$env:netperf_api_url"
$RunId = "$env:netperf_run_id"
try {
$Response = Invoke-WebRequest -Uri "$url/getkeyvalue?key=$RunId" -Headers $headers -UseBasicParsing
} catch {
Write-Host "Unable to fetch state. Creating a new one now."
$state = [pscustomobject]@{
value=[pscustomobject]@{
"SeqNum" = 0
"Commands" = @($Command)
}}
$StateJson = $state | ConvertTo-Json
$Response = Invoke-WebRequest -Uri "$url/setkeyvalue?key=$RunId" -Headers $headers -Method Post -Body $StateJson -ContentType "application/json" -UseBasicParsing
if ($Response.StatusCode -ne 200) {
throw "Failed to set the key value!"
}
return
}
$CurrState = $Response.Content | ConvertFrom-Json
$CurrState.Commands += $Command
$CurrState = [pscustomobject]@{
value=$CurrState
}
$StateJson = $CurrState | ConvertTo-Json
$Response = Invoke-WebRequest -Uri "$url/setkeyvalue?key=$RunId" -Headers $headers -Method Post -Body $StateJson -ContentType "application/json" -UseBasicParsing
if ($Response.StatusCode -ne 200) {
throw "Failed to set the key value!"
}
}
function NetperfWaitServerFinishExecution {
param (
[Parameter(Mandatory = $false)]
[int]$MaxAttempts = 30,
[Parameter(Mandatory = $false)]
[int]$WaitPerAttempt = 8,
[Parameter(Mandatory = $false)]
[scriptblock]$UnblockRoutine = {},
[Parameter(Mandatory = $false)]
$Session
)
if ($Session -and $Session -ne "NOT_SUPPORTED") {
Write-Host "No need to wait if we are doing remote powershell..."
return
}
for ($i = 0; $i -lt $maxattempts; $i++) {
$UnblockRoutine.Invoke()
Write-Host "Waiting for server to finish execution... Attempt $i"
Start-Sleep -Seconds $WaitPerAttempt
$headers = @{
"secret" = "$env:netperf_syncer_secret"
}
$url = "$env:netperf_api_url"
$RunId = "$env:netperf_run_id"
try {
$Response = Invoke-WebRequest -Uri "$url/getkeyvalue?key=$RunId" -Headers $headers -UseBasicParsing
if (!($Response.StatusCode -eq 200)) {
throw "Remote Cache State Not Set!"
}
} catch {
Write-Host "Failed to fetch state. Retrying... Error: $_"
continue
}
$CurrState = $Response.Content | ConvertFrom-Json
if ($CurrState.SeqNum -eq $CurrState.Commands.Count) {
return
} else {
Write-Host "Server not done yet. Seq num: $($CurrState.SeqNum), Commands count: $($CurrState.Commands.Count)"
}
}
throw "Server did not finish execution in time! Tried $maxattempts times with $WaitPerAttempt seconds interval."
}
function InitNetperfLib {
param (
$CallBackName,
$RemoteDir,
$RemoteName,
$UserNameOnLinux
)
$env:CallBackName = $CallBackName
$env:RemoteDir = $RemoteDir
$env:RemoteName = $RemoteName
$env:UserNameOnLinux = $UserNameOnLinux
$RemotePowershellSupported = $env:netperf_remote_powershell_supported
if ($RemotePowershellSupported -eq $true) {
# Set up the connection to the peer over remote powershell.
Write-Host "Connecting to $RemoteName"
Write-Host "Using Callback Script: $RemoteDir/scripts/$CallBackName"
$Attempts = 0
while ($Attempts -lt 5) {
try {
if ($isWindows) {
$username = (Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon').DefaultUserName
$password = (Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon').DefaultPassword | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($username, $password)
$Session = New-PSSession -ComputerName $RemoteName -Credential $cred -ConfigurationName PowerShell.7
} else {
$Session = New-PSSession -HostName $RemoteName -UserName $UserNameOnLinux -SSHTransport
}
break
} catch {
Write-Host "Error $_"
$Attempts += 1
Start-Sleep -Seconds 10
}
}
if ($null -eq $Session) {
Write-GHError "Failed to create remote session"
exit 1
}
} else {
$Session = "NOT_SUPPORTED"
Write-Host "Remote PowerShell is not supported in this environment"
}
return $Session
}
function Copy-RepoToPeer {
param($Session)
$RemoteDir = $env:RemoteDir
$UserNameOnLinux = $env:UserNameOnLinux
if (!($Session -eq "NOT_SUPPORTED")) {
# Copy the artifacts to the peer.
Write-Host "Copying files to peer"
if ($isWindows) {
Invoke-Command -Session $Session -ScriptBlock {
if (Test-Path $Using:RemoteDir) {
Remove-Item -Force -Recurse $Using:RemoteDir | Out-Null
}
New-Item -ItemType Directory -Path $Using:RemoteDir -Force | Out-Null
}
} else {
Invoke-Command -Session $Session -ScriptBlock {
# Create tmp script
$Script = @"
if (Test-Path $Using:RemoteDir) {
Remove-Item -Force -Recurse $Using:RemoteDir | Out-Null
}
New-Item -ItemType Directory -Path $Using:RemoteDir -Force | Out-Null
chown $Using:UserNameOnLinux:$Using:UserNameOnLinux $Using:RemoteDir
chmod 755 $Using:RemoteDir
"@
# Create file
New-Item -ItemType File -Path "$Using:RemoteDir/../tmp_script.ps1" -Force | Out-Null
Set-Content -Path "$Using:RemoteDir/../tmp_script.ps1" -Value $Script -Force
& sudo -n pwsh -NoProfile -NonInteractive -File `
"$Using:RemoteDir/../tmp_script.ps1"
}
}
Copy-Item -ToSession $Session -Path ./*, ./.* -Destination "$RemoteDir" -Recurse -Force
} else {
Write-Host "Not using remote powershell, assuming peer has checked out the repo."
}
}