Atria.Remote.Setup.ps1

<#PSScriptInfo
 
.VERSION 1.0.14
 
.GUID 438791b3-087c-446d-a8bd-924e6d97bc82
 
.AUTHOR Automate101
 
.COMPANYNAME Automate101
 
.COPYRIGHT Automate101 Ltd
 
.TAGS
 
.LICENSEURI https://getatria.com/
 
.PROJECTURI https://getatria.com/
 
.ICONURI https://getatria.com/wp-content/uploads/2020/06/Atria_ApprovedIcon_WithWordmark_Colour_PNG.png
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
#>

#requires -RunAsAdministrator

<#
 
.DESCRIPTION
 Installs the necessary components to manage a remote active directory with Atria
 
#>
 

param(
    [parameter(Mandatory=$true)]
    [string]$InstallToken,
    [parameter(Mandatory=$true)]
    [string]$ExternalApiUrl
)

$ErrorActionPreference = "stop"

function Test-Connection {
  Param (
    [Parameter(Mandatory)]
    [String]$ComputerName,
    [Parameter(Mandatory)]
    [String]$Port
  )

  $TestResult = Test-NetConnection -ComputerName $ComputerName -Port $Port
  if ($TestResult.NameResolutionSucceeded -eq $false) {
    Write-Error ('Cannot resolve the connection to the hostname {0}. Please ensure this name is resolveable' -f $ComputerName)
  }
  if ($TestResult.TcpTestSucceeded -eq $false) {
    Write-Error ('Cannot connect to {0} on port {1}. Please ensure outbound connections are allowed to this port' -f $ComputerName, $Port)
  }
}

#Test the connection to the API before attempting it and getting an ugly error if it fails
Test-Connection -ComputerName (([uri]$ExternalApiUrl).host) -Port (([uri]$ExternalApiUrl).port)

$url = '{0}/api/environments/install/installs/setup-data?token={1}' -f $ExternalApiUrl, $InstallToken


$response = Invoke-WebRequest $url -UseBasicParsing

if ($response.StatusCode -ne 200) {
  Write-Verbose $response.Content
  Write-Error "Failed to get data from external api"
  exit
}

$installJson = ConvertFrom-Json $response.Content
$installConfig = $InstallJson.Data

#Test the connection to the RabbitMQ Queues before installing any component
$SchemePort = @{
  amqps=5671
  amqp=5672
}
Test-Connection -ComputerName (([uri]$InstallConfig.MessagingUrl).host) -Port ($SchemePort[([uri]$InstallConfig.MessagingUrl).scheme])
Test-Connection -ComputerName (([uri]$InstallConfig.feedUrl).host) -Port (([uri]$InstallConfig.feedUrl).port)

$RepositoryName = 'Atria'
$V2RepositoryLocation = $installConfig.FeedUrl.Replace('/v3/index.json', '/v2')

$passwordSecureString = ConvertTo-SecureString -String $InstallConfig.feedUser -AsPlainText -force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($InstallConfig.feedToken, $passwordSecureString)

if (([Net.ServicePointManager]::SecurityProtocol).ToString().split(',').Trim() -notContains 'Tls12') {
  [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
}

if (!(Get-PackageProvider | Where-Object {$_.Name -eq 'NuGet'})) {
  $null = Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -Force -Confirm:$false
}

Write-Host 'Setup PSRepository'
$repo = Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue
if (!$repo) {
  Register-PSRepository -Name $RepositoryName -SourceLocation $V2RepositoryLocation -Credential $credential -InstallationPolicy Trusted
} else {
  Set-PSRepository -Name $RepositoryName -SourceLocation $V2RepositoryLocation -Credential $credential -InstallationPolicy Trusted
}

#Install-Module -Name Atria.Install -Repository Atria -Credential $credential
$InstallParams = @{
  Repository = $RepositoryName
  Credential = $credential 
  Force = $true
  AllowClobber = $true
}

if ($InstallConfig.PreferredVersionTools) {
  $InstallParams.Add('MaximumVersion', $InstallConfig.PreferredVersionTools)
}

Install-Module -Name Atria.Tools @InstallParams
Install-Module -Name Atria.Platform @InstallParams

Get-Module Atria.Tools | Remove-Module -Confirm:$false -Force
Import-Module -Name Atria.Tools -force

Connect-AtriaFeed -UserName $InstallConfig.feedUser -PersonalAccessToken $InstallConfig.feedToken -FeedUrl $InstallConfig.feedUrl

Unregister-PSRepository -Name Atria

#Make sure we have the ConfigService version closest to the version of Atria.Tools
$ConfigServiceVersion = (Get-AtriaPackageVersions -Name ConfigService | Where-Object {$_.Version -le ((Get-Module -Name Atria.Tools).Version) } | Sort-Object Version -Descending | Select-Object -First 1).Version

Write-Host "Please enter domain admin credentials"

If ($installConfig.EnvironmentType -eq 'Dedicated' -or $installConfig.EnvironmentType -eq 'Private') {
    Install-AtriaConfigService `
     -Credential (Get-Credential) `
     -StoreSecretsInPlatformEnvironment `
     -MessagingUrl $installConfig.messagingUrl `
     -MessagingUsername $installConfig.messagingUser `
     -MessagingPassword $installConfig.messagingPassword `
     -CustomerId $installConfig.CustomerId `
     -EnvironmentId $installConfig.EnvironmentId `
     -Version $ConfigServiceVersion
} else {
    Install-AtriaConfigService `
     -Credential (Get-Credential) `
     -StoreSecretsInPlatformEnvironment `
     -MessagingUrl $installConfig.messagingUrl `
     -MessagingUsername $installConfig.messagingUser `
     -MessagingPassword $installConfig.messagingPassword `
     -LocationId $installConfig.LocationId `
     -EnvironmentId $installConfig.EnvironmentId `
     -Version $ConfigServiceVersion
}     

Install-AtriaAgent
    
Install-AtriaProvisioning
    
Install-AtriaDirectory

$postInstallationUrl = '{0}/api/environments/install/installs/post-installation?token={1}' -f $ExternalApiUrl, $InstallToken
Invoke-WebRequest $postInstallationUrl -Method Post -ContentType 'application/json' -UseBasicParsing