github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/e2e/terraform/packer/windows-2016-amd64/provision.ps1 (about) 1 param( 2 [string]$nomad_sha, 3 [string]$nomad_version, 4 [string]$nomad_binary, 5 [switch]$enterprise = $false, 6 [switch]$nomad_acls = $false, 7 [string]$config_profile, 8 [string]$role, 9 [string]$index, 10 [string]$autojoin, 11 [switch]$nostart = $false 12 ) 13 14 Set-StrictMode -Version latest 15 $ErrorActionPreference = "Stop" 16 17 $usage = @" 18 Usage: provision.ps1 [options...] 19 Options (use one of the following): 20 -nomad_sha SHA full git sha to install from S3 21 -nomad_version VERSION release version number (ex. 0.12.4+ent) 22 -nomad_binary FILEPATH path to file on host 23 24 Options for configuration: 25 -config_profile FILEPATH path to config profile directory 26 -role ROLE role within config profile directory 27 -index INDEX count of instance, for profiles with per-instance config 28 -nostart do not start or restart Nomad 29 -enterprise if nomad_sha is passed, use the ENT version 30 --autojoin the AWS ConsulAutoJoin tag value 31 32 "@ 33 34 $RunningAsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") 35 if (!$RunningAsAdmin) { 36 Write-Error "Must be executed in Administrator level shell." 37 exit 1 38 } 39 40 # Force TLS1.2 41 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 42 43 44 $install_path = "C:\opt\nomad.exe" 45 $platform = "windows_amd64" 46 47 Set-Location C:\opt 48 49 function Usage { 50 Write-Output "${usage}" 51 } 52 53 function InstallFromS3 { 54 55 Try { 56 # check that we don't already have this version 57 if (C:\opt\nomad.exe -version ` 58 | Select-String -Pattern $nomad_sha -SimpleMatch -Quiet) { 59 Write-Output "${nomad_sha} already installed" 60 return 61 } 62 } Catch { 63 Write-Output "${nomad_sha} not previously installed" 64 } 65 66 Stop-Service -Name nomad -ErrorAction Ignore 67 68 $build_folder = "builds-oss" 69 if ($enterprise) { 70 $build_folder = "builds-ent" 71 } 72 $key = "${build_folder}/nomad_${platform}_${nomad_sha}.zip" 73 74 Write-Output "Downloading Nomad from s3: $key" 75 Try { 76 Remove-Item -Path ./nomad.zip -Force -ErrorAction Ignore 77 Read-S3Object -BucketName nomad-team-dev-test-binaries ` 78 -Key $key -File ./nomad.zip -ErrorAction Stop 79 80 Remove-Item -Path $install_path -Force -ErrorAction Stop 81 Expand-Archive ./nomad.zip ./ -Force -ErrorAction Stop 82 Move-Item ` 83 -Path .\pkg\windows_amd64\nomad.exe ` 84 -Destination $install_path -Force -ErrorAction Stop 85 Remove-Item -Path nomad.zip -Force -ErrorAction Ignore 86 87 New-Item -ItemType Directory -Force -Path C:\opt\nomad.d -ErrorAction Stop 88 New-Item -ItemType Directory -Force -Path C:\opt\nomad -ErrorAction Stop 89 } Catch { 90 Write-Output "Failed to install Nomad." 91 Write-Output $_ 92 Write-Host $_.ScriptStackTrace 93 $host.SetShouldExit(-1) 94 throw 95 } 96 97 Write-Output "Installed Nomad." 98 } 99 100 function InstallFromUploadedBinary { 101 102 Stop-Service -Name nomad -ErrorAction Ignore 103 104 Try { 105 Remove-Item -Path $install_path -Force -ErrorAction Ignore 106 Move-Item -Path $nomad_binary -Destination $install_path -Force -ErrorAction Stop 107 108 New-Item -ItemType Directory -Force -Path C:\opt\nomad.d -ErrorAction Stop 109 New-Item -ItemType Directory -Force -Path C:\opt\nomad -ErrorAction Stop 110 } Catch { 111 Write-Output "Failed to install Nomad." 112 Write-Output $_ 113 $host.SetShouldExit(-1) 114 throw 115 } 116 117 Write-Output "Installed Nomad." 118 } 119 120 function InstallFromRelease { 121 Try { 122 # check that we don't already have this version 123 if (C:\opt\nomad.exe -version ` 124 | Select-String -Pattern $nomad_version -SimpleMatch -Quiet) { 125 if (C:\opt\nomad.exe -version ` 126 | Select-String -Pattern dev -SimpleMatch -Quiet -NotMatch) { 127 Write-Output "${nomad_version} already installed" 128 return 129 } 130 } 131 } Catch { 132 Write-Output "${nomad_version} not previously installed" 133 } 134 135 Stop-Service -Name nomad -ErrorAction Ignore 136 137 $releases = "https://releases.hashicorp.com" 138 $url = "${releases}/nomad/${nomad_version}/nomad_${nomad_version}_${platform}.zip" 139 140 Write-Output "Downloading Nomad from: $url" 141 Try { 142 Remove-Item -Path ./nomad.zip -Force -ErrorAction Ignore 143 Invoke-WebRequest -Uri $url -Outfile nomad.zip -ErrorAction Stop 144 145 Remove-Item -Path $install_path -Force -ErrorAction Ignore 146 Expand-Archive .\nomad.zip .\ -ErrorAction Stop 147 Remove-Item -Path nomad.zip -Force -ErrorAction Ignore 148 149 New-Item -ItemType Directory -Force -Path C:\opt\nomad.d -ErrorAction Stop 150 New-Item -ItemType Directory -Force -Path C:\opt\nomad -ErrorAction Stop 151 } Catch { 152 Write-Output "Failed to install Nomad." 153 Write-Output $_ 154 $host.SetShouldExit(-1) 155 throw 156 } 157 158 Write-Output "Installed Nomad." 159 } 160 161 162 function ConfigFiles($src, $dest) { 163 Get-ChildItem -Path "$src" -Name -Attributes !Directory -ErrorAction Ignore` 164 | ForEach-Object { ` 165 New-Item -ItemType SymbolicLink -Path "${dest}\$_" -Target "${src}\$_" } 166 } 167 168 function InstallConfigProfile { 169 170 if ( Test-Path -Path 'C:\tmp\custom' -PathType Container ) { 171 Remote-Item 'C:\opt\config\custom' -Force -ErrorAction Ignore 172 Move-Item -Path 'C:\tmp\custom' -Destination 'C:\opt\config\custom' -Force 173 } 174 175 $cfg = "C:\opt\config\${config_profile}" 176 177 Remove-Item "C:\opt\nomad.d\*" -Force -ErrorAction Ignore 178 Remove-Item "C:\opt\consul.d\*" -Force -ErrorAction Ignore 179 180 ConfigFiles "${cfg}\nomad" "C:\opt\nomad.d" 181 ConfigFiles "${cfg}\consul" "C:\opt\consul.d" 182 183 if ( "" -ne $role ) { 184 ConfigFiles "${cfg}\nomad\${role}" "C:\opt\nomad.d" 185 ConfigFiles "${cfg}\consul\${role}" "C:\opt\consul.d" 186 } 187 188 if ( "" -ne $index ) { 189 ConfigFiles "${cfg}\nomad\${role}\indexed\*${index}*" "C:\opt\nomad.d" 190 ConfigFiles "${cfg}\consul\${role}\indexed\*${index}*" "C:\opt\consul.d" 191 } 192 } 193 194 function UpdateConsulAutojoin { 195 (Get-Content C:\opt\consul.d\aws.json).replace("tag_key=ConsulAutoJoin tag_value=auto-join", "tag_key=ConsulAutoJoin tag_value=${autojoin}") | ` 196 Set-Content C:\opt\consul.d\aws.json 197 } 198 199 function CreateConsulService { 200 New-Service ` 201 -Name "Consul" ` 202 -BinaryPathName "C:\opt\consul.exe agent -config-dir C:\opt\consul.d" ` 203 -StartupType "Automatic" ` 204 -ErrorAction Ignore 205 } 206 207 function CreateNomadService { 208 New-NetFirewallRule ` 209 -DisplayName 'Nomad HTTP Inbound' ` 210 -Profile @('Public', 'Domain', 'Private') ` 211 -Direction Inbound ` 212 -Action Allow ` 213 -Protocol TCP ` 214 -LocalPort @('4646') 215 216 # idempotently enable as a service 217 New-Service ` 218 -Name "Nomad" ` 219 -BinaryPathName "C:\opt\nomad.exe agent -config C:\opt\nomad.d" ` 220 -StartupType "Automatic" ` 221 -ErrorAction Ignore 222 } 223 224 if ( "" -ne $nomad_sha ) { 225 InstallFromS3 226 CreateNomadService 227 } 228 if ( "" -ne $nomad_version ) { 229 InstallFromRelease 230 CreateNomadService 231 } 232 if ( "" -ne $nomad_binary ) { 233 InstallFromUploadedBinary 234 CreateNomadService 235 } 236 if ( "" -ne $config_profile) { 237 InstallConfigProfile 238 } 239 if ( "" -ne $autojoin) { 240 UpdateConsulAutojoin 241 } 242 243 if (!($nostart)) { 244 CreateConsulService 245 CreateNomadService 246 Restart-Service "Consul" 247 Restart-Service "Nomad" 248 }