-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathsetupMatchMakerVM.ps1
217 lines (178 loc) · 7.44 KB
/
setupMatchMakerVM.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
Param (
[Parameter(Mandatory = $True, HelpMessage = "subscription id from terraform")]
[String]$subscription_id = "",
[Parameter(Mandatory = $True, HelpMessage = "resource group name")]
[String]$resource_group_name = "",
[Parameter(Mandatory = $True, HelpMessage = "vmss name")]
[String]$vmss_name = "",
[Parameter(Mandatory = $True, HelpMessage = "application insights key")]
[String]$application_insights_key = "",
[Parameter(Mandatory = $True, HelpMessage = "git path")]
[String]$gitpath = "",
[Parameter(Mandatory = $False, HelpMessage = "github access token")]
[String]$pat = ""
)
$StartTime = Get-Date
#####################################################################################################
#base variables
#####################################################################################################
$logsfolder = "c:\gaming\logs"
$logoutput = $logsfolder + '\ue4-setupMM-output-' + (get-date).ToString('MMddyyhhmmss') + '.txt'
$folder = "c:\Unreal\"
$folderNoTrail = $folder
if ($folderNoTrail.EndsWith("\")) {
$l = $folderNoTrail.Length - 1
$folderNoTrail = $folderNoTrail.Substring(0, $l)
}
$mmServiceFolder = "$folderNoTrail\Engine\Source\Programs\PixelStreaming\WebServers\Matchmaker"
$mmCertFolder = $mmServiceFolder + "\Certificates"
$executionfilepath = "$folderNoTrail\scripts\startMMS.ps1"
$deploymentLocation = $resource_group_name.Split('-')[1]
$base_name = $resource_group_name.Substring(0, $resource_group_name.IndexOf("-"))
$akv = "akv-" + $base_name + "-eastus"
#handle if a Personal Access Token is being passed
#$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$pat"))
if ($pat.Length -gt 0) {
#handle if a PAT was passed and use that in the url
$newprefix = "https://$pat@"
$gitpath = $gitpath -replace "https://", $newprefix
}
#####################################################################################################
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Set-ExecutionPolicy Bypass -Scope Process -Force
New-Item -Path $logsfolder -ItemType directory -Force
function logmessage() {
$logmessage = $args[0]
$MessageTime = Get-Date -Format "[MM/dd/yyyy HH:mm:ss]"
$output = "$MessageTime - $logmessage"
Add-Content -Path $logoutput -Value $output
}
logmessage "Starting BE Setup at:$StartTime"
logmessage "Disabling Windows Firewalls started"
New-NetFirewallRule -DisplayName 'Matchmaker-IB-80' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 80
New-NetFirewallRule -DisplayName 'Matchmaker-IB-90' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 90
New-NetFirewallRule -DisplayName 'Matchmaker-IB-9999' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 9999
New-NetFirewallRule -DisplayName 'Matchmaker-IB-443' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 443
New-NetFirewallRule -DisplayName 'Matchmaker-OB-80' -Direction Outbound -Action Allow -Protocol TCP -LocalPort 80
New-NetFirewallRule -DisplayName 'Matchmaker-OB-19302' -Direction Outbound -Action Allow -Protocol TCP -LocalPort 19302
New-NetFirewallRule -DisplayName 'Matchmaker-OB-19303' -Direction Outbound -Action Allow -Protocol TCP -LocalPort 19303
New-NetFirewallRule -DisplayName 'Matchmaker-OB-443' -Direction Outbound -Action Allow -Protocol TCP -LocalPort 443
logmessage "Disabling Windows Firewalls complete"
#git and git-lfs is on the image already
[reflection.assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement")
$whoami = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
logmessage "Current Context: $whoami"
logmessage "Creating: $folder"
New-Item -Path $folder -ItemType directory
logmessage "Cloning code process from Git Start"
if ( (Get-ChildItem $folderNoTrail | Measure-Object).Count -eq 0) {
try {
logmessage "Set Path to: $folder"
Set-Location -Path $folder
logmessage "Git LFS Install"
git lfs install
Start-Sleep 10
logmessage "Git Clone Start"
git clone --depth 1 $gitpath $folderNoTrail --q
logmessage "Git Clone Complete"
logmessage "Git cloning process Complete"
}
catch {
logmessage $_.Exception.Message
break
}
finally {
$error.clear()
}
}
else {
logmessage "Unreal Folder was not Empty. ABORTING."
break
}
#put a check here if the clone actually occurred, if not break
try {
Set-Location -Path $mmServiceFolder
}
catch {
logmessage $_.Exception.Message
break
}
finally {
$error.clear()
}
logmessage "Current folder: $mmServiceFolder"
logmessage $subscription_id
logmessage $resource_group_name
logmessage $vmss_name
logmessage $application_insights_key
$mmConfigJson = (Get-Content "config.json" -Raw) | ConvertFrom-Json
logmessage "Config json before update : $mmConfigJson"
$mmConfigJson.resourceGroup = $resource_group_name
$mmConfigJson.subscriptionId = $subscription_id
$mmConfigJson.virtualMachineScaleSet = $vmss_name
$mmConfigJson.appInsightsInstrumentationKey = $application_insights_key
$mmConfigJson.region = $deploymentLocation
$mmConfigJson | ConvertTo-Json | set-content "config.json"
# Reading again to confirm the update
$mmConfigJson = (Get-Content "config.json" -Raw) | ConvertFrom-Json
logmessage "Writing parameters from extension complete."
logmessage "Updated config : $mmConfigJson"
#create the certificates folder
$mmCertFolder = $mmServiceFolder + "\certificates"
if (-not (Test-Path -LiteralPath $mmCertFolder)) {
$fso = new-object -ComObject scripting.filesystemobject
$fso.CreateFolder($mmCertFolder)
}
else {
logmessage "Path already exists: $mmCertFolder"
}
#set the path to the certificates folder
Set-Location -Path $mmCertFolder
logmessage "Starting Certificate Process"
logmessage "Az Login"
az login --identity
logmessage "Az Set Subscription"
az account set --subscription $subscription_id
logmessage "AKV: $akv"
[reflection.assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement")
$whoami = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
logmessage "Current Context: $whoami"
#check to see if the key exists?
$akvcert = (az keyvault certificate list-versions --vault-name $akv -n "unrealpixelstreaming" --query "[].{Name:name}" -o table).Count
#download the cert to the folder (1 means there is no cert, 3 or more indicates the specific cert was found)
if ($akvcert -gt 1) {
try {
logmessage "Starting Downloading of certs"
### end process to download certs
az keyvault certificate download --vault-name $akv -n "unrealpixelstreaming" -f client-cert.pem
logmessage "Certificates Download Succeeded"
}
catch {
logmessage "Certificates Download Failed"
}
}
else {
logmessage "Certificate does not exist"
}
logmessage "Creating a job schedule "
$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:10
try {
$User = "NT AUTHORITY\SYSTEM"
$PS = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-executionpolicy bypass -noprofile -file $executionfilepath"
Register-ScheduledTask -Trigger $trigger -User $User -TaskName "StartMMS" -Action $PS -RunLevel Highest -Force
}
catch {
logmessage "Exception: " + $_.Exception
}
finally {
$error.clear()
}
logmessage "Creating a job schedule complete"
logmessage "Starting the MMS Process "
#invoke the script to start it this time
#Invoke-Expression -Command $executionfilepath
Start-ScheduledTask -TaskName "StartMMS" -AsJob
$EndTime = Get-Date
logmessage "Completed at:$EndTime"