do we have octopus deploy provider for the terraform to install and configure octopus tentacle in azure vm
Hello @YuvarajDuraisamy , thanks for reaching out!
While the Octopus deploy provider does provide resources for configuring deployment targets, these resources are focused on how the targets are registered within the Octopus server instance, and don’t install the Tentacle software on a specified VM.
To control tentacle registration through Terraform, I’ve used this approach in the past:
- Create an automated script for installing and configuring the Octopus Tentacle
- We have existing documentation on automating Tentacle installation you can use as a starting point
- With a functional registration script, you can use the
azurerm_virtual_machine_extension
resource from the azurerm Terraform provider to set your script to run on creation of the virtual machine
We have a public facing sample that uses this pattern to register targets.
- Here’s the runbook that creates the Terraform resources (log in as a guest to see the content)
- It uses this public facing repo for the Terraform code, here are the key lines:
-
Lines 45 - 47 turn the
bootstrap.ps1
file into a template file for the Terraform execution, and pass relevant configuration variables into the script - Lines 182 - 194 create the VM extension resource and pass the script in as a runnable extension.
-
Lines 45 - 47 turn the
Hopefully that gives you a solid idea of how you could automate Azure VM tentacle installations using Terraform - if you have any other questions or need assistance, let me know! Happy to help however I can.
I dont have access to the public facing repo , could you please help to see the code
Sorry, my mistake on the repository access level.
The code on the linked lines is:
Creating a template file and passing configuration:
data "template_file" "extension_script_app" {
template = templatefile("${path.module}/bootstrap.ps1", { octopusURL = local.octopusURL, serverThumbprint = local.octopusThumbprint, octopusApiKey = local.octopusApiKey, octopusSpaceName = local.octopusSpaceName, targetEnvironment = local.targetEnvironment, targetRole = local.appServerTargetRole })
}
Using the file in a VM extension resource:
resource "azurerm_virtual_machine_extension" "pattern-iac-appserver-ext" {
name = "Pattern-IaC-AppServer-Ext"
virtual_machine_id = azurerm_windows_virtual_machine.pattern-iac-appserver.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
protected_settings = <<PROT
{
"commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.extension_script_app.rendered)}')) | Out-File -filepath bootstrap.ps1\" && powershell -ExecutionPolicy Unrestricted -File bootstrap.ps1"
}
PROT
}
Thank you , let me check and get back
could you please give bootstrap.ps1 file code
Sure thing - it’s a collection of the automation commands from the linked documentation, wrapped together to enable all of the actions to happen in a streamlined fashion:
Start-Transcript -path "C:\Bootstrap.txt" -append
# Define needed values
$tentacleDownloadPath = "https://octopus.com/downloads/latest/WindowsX64/OctopusTentacle"
$octopusServerUrl = "${octopusURL}"
$octopusApiKey = "${octopusApiKey}"
$octopusServerThumbprint = "${serverThumbprint}"
$registerInEnvironments = "${targetEnvironment}"
$registerInRoles = "${targetRole}"
$spaceName = "${octopusSpaceName}"
$tentacleListenPort = 10933
$tentacleHomeDirectory = "C:\Octopus"
$tentacleAppDirectory = "C:\Octopus\Applications"
$tentacleConfigFile = "C:\Octopus\Tentacle\Tentacle.config"
$tentaclePath = "C:\Tools\Octopus.Tentacle.msi"
function Get-MyPublicIPAddress {
# Get Ip Address of Machine
Write-Host "Getting public IP address"
$ipAddress = Invoke-RestMethod http://ipinfo.io/json | Select-Object -exp ip
return $ipAddress
}
function Get-FileFromServer
{
param (
[string]$url,
[string]$saveAs
)
Write-Host "Downloading $url to $saveAs"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$downloader = new-object System.Net.WebClient
$downloader.DownloadFile($url, $saveAs)
}
function Install-Tentacle
{
param (
[Parameter(Mandatory=$True)]
[string]$apiKey,
[Parameter(Mandatory=$True)]
[System.Uri]$octopusServerUrl,
[Parameter(Mandatory=$True)]
[string]$environment,
[Parameter(Mandatory=$True)]
[string]$role
)
Write-Output "Beginning Tentacle installation"
Write-Output "Downloading latest Octopus Tentacle MSI..."
$tentaclePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\Tentacle.msi")
if ((test-path $tentaclePath) -ne $true) {
Get-FileFromServer $tentacleDownloadPath $tentaclePath
}
Write-Output "Installing MSI"
$msiExitCode = (Start-Process -FilePath "msiexec.exe" -ArgumentList "/i Tentacle.msi /quiet" -Wait -Passthru).ExitCode
Write-Output "Tentacle MSI installer returned exit code $msiExitCode"
if ($msiExitCode -ne 0) {
throw "Installation aborted"
}
Write-Output "Open port $tentacleListenPort on Windows Firewall"
& netsh.exe firewall add portopening TCP $tentacleListenPort "Octopus Tentacle"
if ($lastExitCode -ne 0) {
throw "Installation failed when modifying firewall rules"
}
$ipAddress = Get-MyPublicIPAddress
$ipAddress = $ipAddress.Trim()
Write-Output "Public IP address: " + $ipAddress
Write-Output "Configuring and registering Tentacle"
Set-Location "C:\Program Files\Octopus Deploy\Tentacle"
& .\tentacle.exe create-instance --instance "Tentacle" --config $tentacleConfigFile --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on create-instance"
}
& .\tentacle.exe configure --instance "Tentacle" --home $tentacleHomeDirectory --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on configure"
}
& .\tentacle.exe configure --instance "Tentacle" --app $tentacleAppDirectory --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on configure"
}
& .\tentacle.exe configure --instance "Tentacle" --port $tentacleListenPort --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on configure"
}
& .\tentacle.exe new-certificate --instance "Tentacle" --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on creating new certificate"
}
& .\tentacle.exe configure --instance "Tentacle" --trust $octopusServerThumbprint --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on configure"
}
& .\tentacle.exe register-with --instance "Tentacle" --server $octopusServerUrl --environment $environment --role $role --space $spaceName --name $env:COMPUTERNAME --publicHostName $ipAddress --apiKey $apiKey --comms-style TentaclePassive --force --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on register-with"
}
& .\tentacle.exe service --instance "Tentacle" --install --start --console | Write-Host
if ($lastExitCode -ne 0) {
throw "Installation failed on service install"
}
Write-Output "Tentacle commands complete"
}
# Set Environment Variable for ASP.NET CORE
[Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "$registerInEnvironments", "Machine")
# Install tentacle now ...
Install-Tentacle -apikey $octopusApiKey -octopusServerUrl $octopusServerUrl -environment $registerInEnvironments -role $registerInRoles
</powershell>
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.