## Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
## SPDX-License-Identifier: MIT-0
# Get Administrator password from AWS Secrets Manager
$admin_password_plaintext = Get-SECSecretValue ADMIN_PASSWORD_SECRET_ARN | % { Echo $_.SecretString}
$admin_password_secure_string = $admin_password_plaintext | ConvertTo-SecureString -AsPlainText -Force
# Set Administrator password
Get-LocalUser -Name "Administrator" | Set-LocalUser -Password $admin_password_secure_string
# 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
Register-ScheduledTask -Action $action -Trigger $trigger -User "Administrator" -Password $admin_password_plaintext -TaskName "SwarmAgent" -Description "UE4 Swarm Agent" -RunLevel Highest
# Restart the instance to trigger the Swarm Agent Scheduled Task
Restart-Computer