forked from antrea-io/antrea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-vm.ps1
277 lines (239 loc) · 9.09 KB
/
install-vm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<#
.SYNOPSIS
Installs Antrea-Agent service.
.PARAMETER Namespace
ExternalNode Namespace to be used.
.PARAMETER BinaryPath
Specifies the path of the antrea-agent binary to be used.
.PARAMETER ConfigPath
Specifies the path of the antrea-agent configuration file to be used.
.PARAMETER KubeConfigPath
Specifies the path of the kubeconfig to access K8s API Server.
.PARAMETER AntreaKubeConfigPath
Specifies the path of the kubeconfig to access Antrea API Server.
.PARAMETER NodeName
Specifies the ExternalNode name to be used by the antrea-agent.
.PARAMETER OVSBridge
Specifies the OVS bridge name.
.PARAMETER InstallDir
The target installation directory. The default path is "C:\antrea-agent".
#>
Param(
[parameter(Mandatory = $true)] [string] $Namespace,
[parameter(Mandatory = $true)] [string] $BinaryPath,
[parameter(Mandatory = $true)] [string] $ConfigPath,
[parameter(Mandatory = $true)] [string] $KubeConfigPath,
[parameter(Mandatory = $true)] [string] $AntreaKubeConfigPath,
[parameter(Mandatory = $false)] [string] $NodeName = $(hostname),
[parameter(Mandatory = $false)] [string] $OVSBridge = "br-int",
[parameter(Mandatory = $false)] [string] $InstallDir = "C:\antrea-agent"
)
$ErrorActionPreference = "Stop"
$Powershell = (Get-Command powershell).Source
$PowershellArgs = "-ExecutionPolicy Bypass -NoProfile -File"
# Antrea paths
$AntreaAgentConfDir = [io.path]::combine($InstallDir, "conf")
$AntreaAgentLogDir = [io.path]::combine($InstallDir, "logs")
$AntreaAgentConfPath = [io.path]::combine($AntreaAgentConfDir, "antrea-agent.conf")
$LogFile = [io.path]::combine($AntreaAgentLogDir, "antrea-agent-service.log")
$StartAntreaAgentScript = ""
# Constants
$AntreaAgent = "antrea-agent"
$OVSServices = "ovsdb-server", "ovs-vswitchd"
$OVSVswitchd = "ovs-vswitchd"
$K8sKubeconfig = "antrea-agent.kubeconfig"
$AntreaKubeconfig = "antrea-agent.antrea.kubeconfig"
$Bridge = "ovsBridge"
$ExternalNodeNamespace = "externalNodeNamespace"
$Kubeconfig = "kubeconfig"
# List of supported OS versions, verified by antrea
# Versions are named like Major.Minor.Build
$SupportedVersions = @("10.0.17763")
function Log($Info) {
$time = $(get-date -Format g)
"$time $Info " | Tee-Object $LogFile -Append | Write-Host
}
function ServiceExists($ServiceName) {
If (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
return $true
}
return $false
}
function CheckSupportedVersions() {
Log "Checking supported Windows OS versions"
$OSVersion = [System.Environment]::OSVersion.Version
$Version = $OSVersion.Major.ToString() + "." + $OSVersion.Minor.ToString() + "." + $OSVersion.Build.ToString()
foreach ($v in $SupportedVersions) {
if ($v -eq $Version) {
return
}
}
Log "Error only Windows $SupportedVersions is supported"
exit 1
}
function PrintPrerequisites() {
Write-Host "Please execute these commands to enable Hyper-V"
Write-Host "Install-WindowsFeature Hyper-V-Powershell"
Write-Host "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart"
exit 1
}
function CheckPrerequisites() {
CheckSupportedVersions
$valid = $true
Log "Check Hyper-v feature is enabled"
if ((Get-WindowsOptionalFeature -FeatureName "Microsoft-Hyper-V" -Online).State -eq "Disabled") {
Log "Windows optional feature Microsoft-Hyper-V is disabled"
$valid = $false
}
if ((Get-WindowsFeature -Name "Hyper-V-Powershell").InstallState -ne "Installed") {
Log "Windows feature Hyper-V-Powershell is not installed"
$valid = $false
}
if ($valid -eq $false) {
PrintPrerequisites
}
Log "Check OVS services are installed"
foreach ($daemon in $OVSServices) {
If (-Not (ServiceExists($daemon))) {
Log "Service $daemon does not exist."
exit 1
}
}
}
function SetupInstallDir() {
if (-Not (Test-Path $AntreaAgentConfDir)) {
New-Item $AntreaAgentConfDir -type directory -Force | Out-Null
}
if (-Not (Test-Path $AntreaAgentLogDir)) {
New-Item $AntreaAgentLogDir -type directory -Force | Out-Null
}
}
function CopyAntreaAgentFiles() {
if ( -Not (Test-Path $BinaryPath)) {
Log "$BinaryPath file not found"
exit 1
}
Log "Copying $BinaryPath to $InstallDir"
Copy-Item -Path "$BinaryPath" -Destination $InstallDir -Force | Out-Null
if ( -Not (Test-Path $KubeConfigPath)) {
Log "$KubeConfigPath file not found"
exit 1
}
Log "Copying $KubeConfigPath to $AntreaAgentConfDir"
Copy-Item -Path "$KubeConfigPath" -Destination "$AntreaAgentConfDir\$K8sKubeconfig" -Force
if ( -Not (Test-Path $AntreaKubeConfigPath)) {
Log "$AntreaKubeConfigPath file not found"
exit 1
}
Log "Copying $AntreaKubeConfigPath to $AntreaAgentConfDir"
Copy-Item -Path "$AntreaKubeConfigPath" -Destination "$AntreaAgentConfDir\$AntreaKubeconfig" -Force
}
function UpdateAgentConf() {
if ( -Not (Test-Path $ConfigPath)) {
Log "$ConfigPath file not found"
exit 1
}
New-Item $AntreaAgentConfPath -type file -Force | Out-Null
$file = Get-Content $ConfigPath
foreach ($line in $file) {
if ($line -like "*$Kubeconfig") {
$key, $val = $line.split(":", 2).trim()
$newval = "${AntreaAgentConfDir}\${val}"
Log "Updating $AntreaAgentConfPath with ${key}: ${newval}"
[System.IO.File]::AppendAllText($AntreaAgentConfPath, " ${key}: ${newval}" +
([Environment]::NewLine))
} elseif ($line -like "*$ExternalNodeNamespace*") {
Log "Updating $AntreaAgentConfPath with ${ExternalNodeNamespace}: ${Namespace}"
[System.IO.File]::AppendAllText($AntreaAgentConfPath, " ${ExternalNodeNamespace}: ${Namespace}" +
([Environment]::NewLine))
} elseif ($line -like "*$Bridge*") {
Log "Updating $AntreaAgentConfPath with ${Bridge}: ${OVSBridge}"
[System.IO.File]::AppendAllText($AntreaAgentConfPath, "${Bridge}: ${OVSBridge}" +
([Environment]::NewLine))
} else {
[System.IO.File]::AppendAllText($AntreaAgentConfPath, $line +
([Environment]::NewLine))
}
}
}
function CreateAntreaAgentStartupScript() {
$Script:StartAntreaAgentScript = "$AntreaAgentConfDir\Start-AntreaAgent.ps1"
$StartAntreaAgentScriptContent = '
Param(
[parameter(Mandatory = $true)] [string] $OVSBridge,
[parameter(Mandatory = $true)] [string] $InstallDir
)
$AntreaSwitch = "antrea-switch"
$AntreaAgentConfDir = [io.path]::combine($InstallDir, "conf")
$AntreaAgentLogDir = [io.path]::combine($InstallDir, "logs")
$AntreaAgentConfPath = [io.path]::combine($AntreaAgentConfDir, "antrea-agent.conf")
$AntreaAgentLogFile = [io.path]::combine($AntreaAgentLogDir, "antrea-agent.log")
$AntreaAgentPath = [io.path]::combine($InstallDir, "antrea-agent.exe")
$LogFile = [io.path]::combine($AntreaAgentLogDir, "antrea-agent-service.log")
function Log($Info) {
$time = $(get-date -Format g)
"$time $Info " | Tee-Object $LogFile -Append | Write-Host
}
function ClearOVSConfig() {
Log "Deleting OVS bridge $OVSBridge"
try {
$adapterName = (Get-VMNetworkAdapter -ComputerName $(hostname.exe) -SwitchName $AntreaSwitch -ManagementOS).Name
ovs-vsctl.exe del-br $OVSBridge
} catch {
Log "Failed to get VMSwitch $AntreaSwitch, rc $_"
exit 1
}
try {
Remove-VMSwitch -ComputerName $(hostname.exe) $AntreaSwitch -Force
} catch {
Log "Ignore error while removing VMSwitch, rc $_"
}
try {
Rename-NetAdapter -Name "$adapterName~" -NewName "$adapterName"
} catch {
Log "Failed to rename network adapter $adapterName~ to $adapterName, rc $_"
exit 1
}
}
function CheckOVSConfigAndCleanup() {
$bridges = ovs-vsctl list-br
foreach ($br in $bridges) {
if ($br -ne $OVSBridge) {
continue
}
$ports = ovs-vsctl list-ports $OVSBridge
foreach ($port in $ports) {
$output = ovs-vsctl --no-headings --columns=error list interface "$port"
if ($output -ne "[]") {
ClearOVSConfig
break
}
}
}
}
function StartAntreaAgent() {
$antreaAgentArgs = "--config $AntreaAgentConfPath --log_file $AntreaAgentLogFile --logtostderr=false"
$cmd = "$AntreaAgentPath $antreaAgentArgs"
Invoke-Expression $cmd
}
CheckOVSConfigAndCleanup
StartAntreaAgent
'
Set-Content -Path $StartAntreaAgentScript -Value $StartAntreaAgentScriptContent
}
function ConfigureAntreaAgentService() {
$AntreaAgentArgs = "$StartAntreaAgentScript -InstallDir $InstallDir -OVSBridge $OVSBridge"
nssm install $AntreaAgent $Powershell $PowershellArgs $AntreaAgentArgs
# Add OVS as a dependent service
nssm set $AntreaAgent DependOnService $OVSVswitchd
}
function StartAntreaAgentService() {
nssm start $AntreaAgent
}
SetupInstallDir
CheckPrerequisites
CopyAntreaAgentFiles
UpdateAgentConf
CreateAntreaAgentStartupScript
ConfigureAntreaAgentService
StartAntreaAgentService