Is there a way of adding a new node to my Octopus HA cluster without downloading, installing, and configuring Octopus manually?
Adding new nodes to an Octopus HA cluster should be a painless and easy task. Using the Octopus API and Octopus Server command line we can automate this.
For the script to run, we need some information for our script;
- Octopus URL
- Octopus API Key
- Octopus Database Host
- Octopus Database Name
- Octopus Database SQL Username
- Octopus Database SQL Password
- Octopus Master Key
To automate the installation of the node, we need to complete the following steps in our script;
-
Get the Octopus Server version of our Octopus HA cluster
-
Download Octopus Server MSI
-
Install the Octopus Server MSI
-
Configure the Octopus Node
To get the Octopus Server version, there are two ways;
-
Use the Octopus API to call the API endpoint and store the version field.
-
Run a SQL query to query dbo.OctopusServerInstallationHistory table in the Octopus database to get the current Octopus version.
In my example below, I use the Octopus API to get the current version. It would be easy to query the Octopus database because the script requires a username and password for the Octopus Database anyway, but if you are using integrated SQL security and don’t have the username or password, I wanted to show how the version can be retrieved from the API.
- Set the variables for the script
$octopusURL = “OCTOPUS_URL”
$octopusAPIKey = “OCTOPUS_API_KEY”
$octopusSqlDatabaseHost = "DATABASE_HOST
$octopusSqlDatabaseName = “DATABASE_NAME”
$octopusSqlUsername = “DATABASE_USERNAME”
$octopusSqlPassword = “DATABASE_PASSWORD”
$octopusMasterKey = “OCTOPUS_MASTER_KEY”
$header = @{ “X-Octopus-ApiKey” = $octopusAPIKey }
- Call the Octopus API to get the current Octopus version
$version = “Octopus.{0}-x64.msi” -f (Invoke-RestMethod -Method Get -Uri “$octopusURL/api” -Headers $header).Version
- Download the Octopus Server MSI using the version number from the last step
Invoke-WebRequest -Uri “https://download.octopusdeploy.com/octopus/$version” -OutFile “$version”
- Set the Octopus Programs installation directory
$InstallTarget = "C:\Program Files\Octopus Deploy\Octopus"
- Using msiexec, install the downloaded Octopus Server MSI (Waits until installation is complete)
$msiExitCode = (Start-Process -FilePath “msiexec.exe” -ArgumentList “INSTALLLOCATION=
"$InstallTarget
” /i $version /quiet" -Wait -Passthru).ExitCode
- Use the Octopus command line to configure the Octopus instance.
& “$InstallTarget\Octopus.Server.exe” create-instance --instance “default” --config “C:\Octopus\OctopusServer.config”
& “$InstallTarget\Octopus.Server.exe” database --instance “default” --masterKey $octopusMasterKey --connectionString “Data Source=$octopusSqlDatabaseHost;Initial Catalog=$octopusSqlDatabaseName;Integrated Security=False;User ID=$octopusSqlUsername;Password=$octopusSqlPassword”
& “$InstallTarget\Octopus.Server.exe” configure --instance “default” --webForceSSL “False” --webListenPrefixes “http://localhost:80/” --commsListenPort “10943”
& “$InstallTarget\Octopus.Server.exe” service --instance “default” --stop
& “$InstallTarget\Octopus.Server.exe” service --instance “default” --install --reconfigure --start
Because we used the master key in the database configuration, Octopus didn’t create a new node and added the instance as an additional node to the Octopus HA cluster.
If you are trying to create Octopus UI/API nodes only then you can use the Octopus server command line to drain the node and set the task cap to 0.
& “$InstallTarget\Octopus.Server.exe” octopus.server node --taskCap=“15” --instance=“default”