-
Notifications
You must be signed in to change notification settings - Fork 13
/
1es-machine-manager.ps1
205 lines (193 loc) · 7.48 KB
/
1es-machine-manager.ps1
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
param (
[string]$Action,
[string]$GithubContextInput1 = "",
[string]$GithubContextInput2 = "",
[string]$GithubContextInput3 = "",
[string]$GithubContextInput4 = ""
)
Set-StrictMode -Version "Latest"
$PSDefaultParameterValues["*:ErrorAction"] = "Stop"
$psVersion = $PSVersionTable.PSVersion
if ($psVersion.Major -lt 7) {
$notWindows = $false
} else {
$notWindows = !$isWindows
}
Write-Host "Executing action: $Action"
if ($Action -eq "Deserialize_matrix") {
$matrix = ConvertFrom-Json $GithubContextInput1
$remote_powershell_supported = $matrix.remote_powershell_supported
$role = $matrix.role
$env_str = $matrix.env_str
echo "remote_powershell_supported=$remote_powershell_supported" >> $env:GITHUB_ENV
echo "role=$role" >> $env:GITHUB_ENV
echo "env_str=$env_str" >> $env:GITHUB_ENV
}
if ($Action -eq "Disable_Windows_Defender") {
# Disable Windows defender / firewall.
Write-Host "Disabling Windows Defender / Firewall."
netsh.exe advfirewall set allprofiles state off
Set-MpPreference -EnableNetworkProtection Disabled
Set-MpPreference -DisableDatagramProcessing $True
}
if ($Action -eq "Broadcast_IP") {
if (!$notWindows -eq $false) {
$ipAddress = ip addr | grep 'inet ' | grep '10' | awk '{print $2}' | cut -d'/' -f1
} else {
$ipAddress = (Get-NetIpAddress -AddressFamily IPv4).IpAddress
}
Write-Output "Broadcasting ip address: $ipAddress"
$headers = @{
"secret" = $GithubContextInput1
}
Invoke-WebRequest -Uri "https://netperfapi.azurewebsites.net/setkeyvalue?key=$GithubContextInput2-$GithubContextInput3-ipaddress&value=$ipAddress" -Headers $headers -Method Post -UseBasicParsing
}
if ($Action -eq "Poll_IP") {
$found = $false
$headers = @{
"secret" = $GithubContextInput1
}
$uri = "https://netperfapi.azurewebsites.net/getkeyvalue?key=$GithubContextInput2-$GithubContextInput3-ipaddress"
$iterations = 0
do {
Write-Output "Checking for ip address..."
$iterations++
try {
$Response = Invoke-WebRequest -Uri $uri -Headers $headers -UseBasicParsing
if (!($Response.StatusCode -eq 200)) {
throw "Failed to get ip address. Status code: $($Response.StatusCode)"
}
$ipAddress = $Response.Content
Write-Output "Ip Address found: $ipAddress"
if (!$notWindows -eq $false) {
$serverIp = $ipAddress
} else {
$serverIp = $ipAddress.Split(" ") | Where-Object { $_.StartsWith("10") } | Select-Object -First 1
}
Write-Output "Server IP: $serverIp"
$found = $true
}
catch {
Write-Output "Ip Address not found: $_"
if ($iterations -gt 90) {
throw "Failed to get ip address after 15 minutes."
}
Start-Sleep -Seconds 10
}
} while (-not $found)
Write-Host "Setting netperf-peer"
if (!$notWindows -eq $false) {
echo "$serverIp netperf-peer" | sudo tee -a /etc/hosts
} else {
"$serverIp netperf-peer" | Out-File -Encoding ASCII -Append "$env:SystemRoot\System32\drivers\etc\hosts"
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'netperf-peer' -Force
}
}
if ($Action -eq "Deprecated_remote_pwsh_poll_instructions") {
$found = $false
do {
$donepath = "C:\done.txt"
Write-Output "Checking for done.txt..."
if (Test-Path $donepath) {
Write-Output "done.txt found"
$found = $true
break
} else {
Write-Output "done.txt not found"
}
$StatePath = "C:\_state"
if (Test-Path $StatePath) {
ls $StatePath
# Fetch all files in the _state directory
$files = Get-ChildItem -Path $StatePath -File
# Find the highest lexicographically sorted file name
$max = 0
foreach ($file in $files) {
$filename = $file.Name.split(".")[0]
$num = [int]($filename -replace "[^0-9]", "")
if ($num -gt $max) {
$max = $num
}
}
# Check if there is a corresponding "completed" file
$ExecuteFileExist = Test-Path "$StatePath\execute_$($max).ps1"
$CompletedFileExist = Test-Path "$StatePath\completed_$($max).txt"
if ($ExecuteFileExist -and !($CompletedFileExist)) {
Write-Host "Executing $StatePath\execute_$($max).ps1"
Invoke-Expression "$StatePath\execute_$($max).ps1"
Write-Host "Creating $StatePath\completed_$($max).txt"
New-Item -ItemType File -Name "completed_$($max).txt" -Path $StatePath
} else {
Write-Host "No outstanding script to execute... Highest order script found so far: $max"
}
} else {
Write-Host "State directory not found"
}
Start-Sleep -Seconds 10
} while (-not $found)
}
if ($Action -eq "Poll_client_instructions") {
$found = $false
$headers = @{
"secret" = $GithubContextInput1
}
$url = "https://netperfapi.azurewebsites.net"
$iterations = 0
do {
try {
$Response = Invoke-WebRequest -Uri "$url/getkeyvalue?key=$GithubContextInput2-$GithubContextInput3-state" -Headers $headers -UseBasicParsing
$data = $Response.Content
if ($data -eq "done") {
$found = $true
break
}
$dataJson = ConvertFrom-Json $data
if ($dataJson.SeqNum -lt $dataJson.Commands.Count) {
$iterations = 0
$command = $dataJson.Commands[$dataJson.SeqNum]
$dataJson.SeqNum++
$dataJson = @{
value=$dataJson
}
$body = $dataJson | ConvertTo-Json
try {
Invoke-Expression "$GithubContextInput4 -Command '$command'"
} catch {
throw "CALLBACK_ERROR: $_"
}
Invoke-WebRequest -Uri "$url/setkeyvalue?key=$GithubContextInput2-$GithubContextInput3-state" -Headers $headers -Method POST -Body $body -ContentType "application/json" -UseBasicParsing
Write-Host "Finished executing command: $command, SeqNum: $($dataJson.value.SeqNum), Commands count: $($dataJson.value.Commands.Count)"
} else {
$iterations++
if ($iterations -gt 60) {
throw "No new instructions after 10 minutes."
}
Write-Host "Nothing to execute. SeqNum: $($dataJson.SeqNum), Commands count: $($dataJson.Commands.Count)"
Start-Sleep -Seconds 10
}
}
catch {
$iterations++
if ($_.ToString().Contains("CALLBACK_ERROR")) {
throw $_
}
if ($iterations -gt 30) {
throw "Failed to get assigned to a client after 15 minutes."
}
Write-Output "Client not done yet. Silently ignoring non-callback error: $_"
Start-Sleep -Seconds 30
}
} while (-not $found)
}
if ($Action -eq "Stop-1es-machine") {
$headers = @{
"secret" = "$GithubContextInput1"
}
Invoke-WebRequest -Uri "https://netperfapi.azurewebsites.net/setkeyvalue?key=$GithubContextInput2-$GithubContextInput3-state&value=done" -Headers $headers -Method Post -UseBasicParsing
}
if ($Action -eq "deprecated_stop-1es-machine-remote-pwsh") {
$Session = New-PSSession -ComputerName netperf-peer
Invoke-Command -Session $Session -ScriptBlock {
New-Item -ItemType File -Name "done.txt" -Path "C:\"
}
}