2020-08-20 20:10:00 +00:00
|
|
|
param(
|
|
|
|
[string]$nomad_sha,
|
|
|
|
[string]$nomad_version,
|
|
|
|
[string]$nomad_binary,
|
2020-09-23 14:39:04 +00:00
|
|
|
[switch]$enterprise = $false,
|
2020-09-28 13:22:36 +00:00
|
|
|
[switch]$nomad_acls = $false,
|
2020-09-18 15:27:24 +00:00
|
|
|
[string]$config_profile,
|
|
|
|
[string]$role,
|
|
|
|
[string]$index,
|
2021-02-01 02:32:18 +00:00
|
|
|
[string]$autojoin,
|
2020-08-20 20:10:00 +00:00
|
|
|
[switch]$nostart = $false
|
|
|
|
)
|
|
|
|
|
2019-11-19 16:06:10 +00:00
|
|
|
Set-StrictMode -Version latest
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
$usage = @"
|
2020-09-04 12:51:22 +00:00
|
|
|
Usage: provision.ps1 [options...]
|
2020-08-20 20:10:00 +00:00
|
|
|
Options (use one of the following):
|
2020-09-18 15:27:24 +00:00
|
|
|
-nomad_sha SHA full git sha to install from S3
|
|
|
|
-nomad_version VERSION release version number (ex. 0.12.4+ent)
|
|
|
|
-nomad_binary FILEPATH path to file on host
|
|
|
|
|
|
|
|
Options for configuration:
|
|
|
|
-config_profile FILEPATH path to config profile directory
|
|
|
|
-role ROLE role within config profile directory
|
|
|
|
-index INDEX count of instance, for profiles with per-instance config
|
|
|
|
-nostart do not start or restart Nomad
|
2020-09-23 14:39:04 +00:00
|
|
|
-enterprise if nomad_sha is passed, use the ENT version
|
2021-02-01 02:32:18 +00:00
|
|
|
--autojoin the AWS ConsulAutoJoin tag value
|
2020-09-23 14:39:04 +00:00
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
"@
|
|
|
|
|
|
|
|
$RunningAsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
|
|
|
|
if (!$RunningAsAdmin) {
|
|
|
|
Write-Error "Must be executed in Administrator level shell."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:06:10 +00:00
|
|
|
# Force TLS1.2
|
|
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
|
|
|
|
$install_path = "C:\opt\nomad.exe"
|
2020-08-21 13:20:09 +00:00
|
|
|
$platform = "windows_amd64"
|
2020-08-20 20:10:00 +00:00
|
|
|
|
2019-11-19 16:06:10 +00:00
|
|
|
Set-Location C:\opt
|
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
function Usage {
|
|
|
|
Write-Output "${usage}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function InstallFromS3 {
|
2020-11-09 18:29:40 +00:00
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
Try {
|
|
|
|
# check that we don't already have this version
|
|
|
|
if (C:\opt\nomad.exe -version `
|
|
|
|
| Select-String -Pattern $nomad_sha -SimpleMatch -Quiet) {
|
|
|
|
Write-Output "${nomad_sha} already installed"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} Catch {
|
|
|
|
Write-Output "${nomad_sha} not previously installed"
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:29:40 +00:00
|
|
|
Stop-Service -Name nomad -ErrorAction Ignore
|
|
|
|
|
|
|
|
$build_folder = "builds-oss"
|
|
|
|
if ($enterprise) {
|
|
|
|
$build_folder = "builds-ent"
|
|
|
|
}
|
|
|
|
$key = "${build_folder}/nomad_${platform}_${nomad_sha}.zip"
|
|
|
|
|
|
|
|
Write-Output "Downloading Nomad from s3: $key"
|
2020-08-20 20:10:00 +00:00
|
|
|
Try {
|
2020-11-09 18:29:40 +00:00
|
|
|
Remove-Item -Path ./nomad.zip -Force -ErrorAction Ignore
|
|
|
|
Read-S3Object -BucketName nomad-team-dev-test-binaries `
|
|
|
|
-Key $key -File ./nomad.zip -ErrorAction Stop
|
|
|
|
|
|
|
|
Remove-Item -Path $install_path -Force -ErrorAction Stop
|
|
|
|
Expand-Archive ./nomad.zip ./ -Force -ErrorAction Stop
|
|
|
|
Move-Item `
|
|
|
|
-Path .\pkg\windows_amd64\nomad.exe `
|
|
|
|
-Destination $install_path -Force -ErrorAction Stop
|
|
|
|
Remove-Item -Path nomad.zip -Force -ErrorAction Ignore
|
|
|
|
|
|
|
|
New-Item -ItemType Directory -Force -Path C:\opt\nomad.d -ErrorAction Stop
|
|
|
|
New-Item -ItemType Directory -Force -Path C:\opt\nomad -ErrorAction Stop
|
2020-08-20 20:10:00 +00:00
|
|
|
} Catch {
|
2020-11-09 18:29:40 +00:00
|
|
|
Write-Output "Failed to install Nomad."
|
|
|
|
Write-Output $_
|
|
|
|
Write-Host $_.ScriptStackTrace
|
2020-08-20 20:10:00 +00:00
|
|
|
$host.SetShouldExit(-1)
|
|
|
|
throw
|
|
|
|
}
|
2020-11-09 18:29:40 +00:00
|
|
|
|
|
|
|
Write-Output "Installed Nomad."
|
2020-08-20 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InstallFromUploadedBinary {
|
2020-11-09 18:29:40 +00:00
|
|
|
|
|
|
|
Stop-Service -Name nomad -ErrorAction Ignore
|
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
Try {
|
2020-11-09 18:29:40 +00:00
|
|
|
Remove-Item -Path $install_path -Force -ErrorAction Ignore
|
|
|
|
Move-Item -Path $nomad_binary -Destination $install_path -Force -ErrorAction Stop
|
2020-08-20 20:10:00 +00:00
|
|
|
|
2020-11-09 18:29:40 +00:00
|
|
|
New-Item -ItemType Directory -Force -Path C:\opt\nomad.d -ErrorAction Stop
|
|
|
|
New-Item -ItemType Directory -Force -Path C:\opt\nomad -ErrorAction Stop
|
2020-08-20 20:10:00 +00:00
|
|
|
} Catch {
|
2020-11-09 18:29:40 +00:00
|
|
|
Write-Output "Failed to install Nomad."
|
|
|
|
Write-Output $_
|
2020-08-20 20:10:00 +00:00
|
|
|
$host.SetShouldExit(-1)
|
|
|
|
throw
|
|
|
|
}
|
2020-11-09 18:29:40 +00:00
|
|
|
|
|
|
|
Write-Output "Installed Nomad."
|
2020-08-20 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InstallFromRelease {
|
|
|
|
Try {
|
|
|
|
# check that we don't already have this version
|
|
|
|
if (C:\opt\nomad.exe -version `
|
|
|
|
| Select-String -Pattern $nomad_version -SimpleMatch -Quiet) {
|
|
|
|
if (C:\opt\nomad.exe -version `
|
|
|
|
| Select-String -Pattern dev -SimpleMatch -Quiet -NotMatch) {
|
|
|
|
Write-Output "${nomad_version} already installed"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} Catch {
|
|
|
|
Write-Output "${nomad_version} not previously installed"
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:29:40 +00:00
|
|
|
Stop-Service -Name nomad -ErrorAction Ignore
|
|
|
|
|
|
|
|
$releases = "https://releases.hashicorp.com"
|
|
|
|
$url = "${releases}/nomad/${nomad_version}/nomad_${nomad_version}_${platform}.zip"
|
|
|
|
|
|
|
|
Write-Output "Downloading Nomad from: $url"
|
2020-08-20 20:10:00 +00:00
|
|
|
Try {
|
2020-11-09 18:29:40 +00:00
|
|
|
Remove-Item -Path ./nomad.zip -Force -ErrorAction Ignore
|
|
|
|
Invoke-WebRequest -Uri $url -Outfile nomad.zip -ErrorAction Stop
|
|
|
|
|
|
|
|
Remove-Item -Path $install_path -Force -ErrorAction Ignore
|
|
|
|
Expand-Archive .\nomad.zip .\ -ErrorAction Stop
|
|
|
|
Remove-Item -Path nomad.zip -Force -ErrorAction Ignore
|
|
|
|
|
|
|
|
New-Item -ItemType Directory -Force -Path C:\opt\nomad.d -ErrorAction Stop
|
|
|
|
New-Item -ItemType Directory -Force -Path C:\opt\nomad -ErrorAction Stop
|
2020-08-20 20:10:00 +00:00
|
|
|
} Catch {
|
2020-11-09 18:29:40 +00:00
|
|
|
Write-Output "Failed to install Nomad."
|
|
|
|
Write-Output $_
|
2020-08-20 20:10:00 +00:00
|
|
|
$host.SetShouldExit(-1)
|
|
|
|
throw
|
|
|
|
}
|
2020-11-09 18:29:40 +00:00
|
|
|
|
|
|
|
Write-Output "Installed Nomad."
|
2020-08-20 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 15:27:24 +00:00
|
|
|
|
|
|
|
function ConfigFiles($src, $dest) {
|
|
|
|
Get-ChildItem -Path "$src" -Name -Attributes !Directory -ErrorAction Ignore`
|
|
|
|
| ForEach-Object { `
|
|
|
|
New-Item -ItemType SymbolicLink -Path "${dest}\$_" -Target "${src}\$_" }
|
|
|
|
}
|
|
|
|
|
|
|
|
function InstallConfigProfile {
|
|
|
|
|
|
|
|
if ( Test-Path -Path 'C:\tmp\custom' -PathType Container ) {
|
|
|
|
Remote-Item 'C:\opt\config\custom' -Force -ErrorAction Ignore
|
2020-11-09 18:29:40 +00:00
|
|
|
Move-Item -Path 'C:\tmp\custom' -Destination 'C:\opt\config\custom' -Force
|
2020-09-18 15:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$cfg = "C:\opt\config\${config_profile}"
|
|
|
|
|
|
|
|
Remove-Item "C:\opt\nomad.d\*" -Force -ErrorAction Ignore
|
|
|
|
Remove-Item "C:\opt\consul.d\*" -Force -ErrorAction Ignore
|
|
|
|
|
|
|
|
ConfigFiles "${cfg}\nomad" "C:\opt\nomad.d"
|
|
|
|
ConfigFiles "${cfg}\consul" "C:\opt\consul.d"
|
|
|
|
|
|
|
|
if ( "" -ne $role ) {
|
|
|
|
ConfigFiles "${cfg}\nomad\${role}" "C:\opt\nomad.d"
|
|
|
|
ConfigFiles "${cfg}\consul\${role}" "C:\opt\consul.d"
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( "" -ne $index ) {
|
|
|
|
ConfigFiles "${cfg}\nomad\${role}\indexed\*${index}*" "C:\opt\nomad.d"
|
|
|
|
ConfigFiles "${cfg}\consul\${role}\indexed\*${index}*" "C:\opt\consul.d"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 02:32:18 +00:00
|
|
|
function UpdateConsulAutojoin {
|
|
|
|
(Get-Content C:\opt\consul.d\aws.json).replace("tag_key=ConsulAutoJoin tag_value=auto-join", "tag_key=ConsulAutoJoin tag_value=${autojoin}") | `
|
|
|
|
Set-Content C:\opt\consul.d\aws.json
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:27:24 +00:00
|
|
|
function CreateConsulService {
|
|
|
|
New-Service `
|
|
|
|
-Name "Consul" `
|
|
|
|
-BinaryPathName "C:\opt\consul.exe agent -config-dir C:\opt\consul.d" `
|
|
|
|
-StartupType "Automatic" `
|
|
|
|
-ErrorAction Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
function CreateNomadService {
|
2020-08-20 20:10:00 +00:00
|
|
|
New-NetFirewallRule `
|
|
|
|
-DisplayName 'Nomad HTTP Inbound' `
|
|
|
|
-Profile @('Public', 'Domain', 'Private') `
|
|
|
|
-Direction Inbound `
|
|
|
|
-Action Allow `
|
|
|
|
-Protocol TCP `
|
|
|
|
-LocalPort @('4646')
|
2019-11-19 16:06:10 +00:00
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
# idempotently enable as a service
|
|
|
|
New-Service `
|
|
|
|
-Name "Nomad" `
|
|
|
|
-BinaryPathName "C:\opt\nomad.exe agent -config C:\opt\nomad.d" `
|
|
|
|
-StartupType "Automatic" `
|
|
|
|
-ErrorAction Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( "" -ne $nomad_sha ) {
|
|
|
|
InstallFromS3
|
2020-09-18 15:27:24 +00:00
|
|
|
CreateNomadService
|
2020-08-20 20:10:00 +00:00
|
|
|
}
|
|
|
|
if ( "" -ne $nomad_version ) {
|
|
|
|
InstallFromRelease
|
2020-09-18 15:27:24 +00:00
|
|
|
CreateNomadService
|
2020-08-20 20:10:00 +00:00
|
|
|
}
|
|
|
|
if ( "" -ne $nomad_binary ) {
|
|
|
|
InstallFromUploadedBinary
|
2020-09-18 15:27:24 +00:00
|
|
|
CreateNomadService
|
|
|
|
}
|
|
|
|
if ( "" -ne $config_profile) {
|
|
|
|
InstallConfigProfile
|
2019-11-19 16:06:10 +00:00
|
|
|
}
|
2021-02-01 02:32:18 +00:00
|
|
|
if ( "" -ne $autojoin) {
|
|
|
|
UpdateConsulAutojoin
|
|
|
|
}
|
2019-11-19 16:06:10 +00:00
|
|
|
|
2020-09-18 15:27:24 +00:00
|
|
|
if (!($nostart)) {
|
|
|
|
CreateConsulService
|
|
|
|
CreateNomadService
|
|
|
|
Restart-Service "Consul"
|
|
|
|
Restart-Service "Nomad"
|
|
|
|
}
|