## Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
## SPDX-License-Identifier: MIT-0
# Define Coordinator IP, Cloudformation replaces this when task is been created
$coordinator_ip = "COORDINATOR_IP"
# Template of the Swarm Agent Developper Options file
$developeroptions = '
true
LOCALCORES
BelowNormal
REMOTECORES
Idle
false
15
'
# Calculate number of cores
$cores = (Get-WmiObject -Class Win32_Processor | Select-Object -Property NumberOfLogicalProcessors).NumberOfLogicalProcessors
# Set the core values for the Swarm Agent
$developeroptions = $developeroptions.replace("REMOTECORES", $cores)
$developeroptions = $developeroptions.replace("LOCALCORES", $cores-1)
# Save the configureation file
$developeroptions | Out-File -FilePath "C:\ue4-swarm\SwarmAgent.DeveloperOptions.xml"
# Template of the Swarm Options file
$agentoptions = '
*
ue4-swarm-aws
ue4-swarm-aws
COORDINATORHOST
C:\ue4-swarm/SwarmCache
5
false
true
15
0
0
768
768
2
4
'
# Replace the Coordinator IP in the template
$agentoptions = $agentoptions.replace("COORDINATORHOST", $coordinator_ip)
# Save the configuration file
$agentoptions | Out-File -FilePath "C:\ue4-swarm\SwarmAgent.Options.xml"
# Define the Swarm agent as Scheduled Task that starts at instance boot
$action = New-ScheduledTaskAction -Execute "C:\ue4-swarm\SwarmAgent.exe"
$trigger = New-ScheduledTaskTrigger -AtStartup
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName "SwarmAgent" -Description "UE4 Swarm Agent"
# Restart the instance to trigger the Swarm Agent Scheduled Task
Restart-Computer