forked from ZEISS/install-hashicorp-binaries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-hashicorp.ps1
359 lines (334 loc) · 14.7 KB
/
install-hashicorp.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
function Import-ScriptsWebDownload {
if (-not ([System.Management.Automation.PSTypeName]'Scripts.Web').Type){
@"
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
namespace Scripts
{
public class Web
{
private static readonly object LockObject = new object();
public static int DownloadFiles(params string[] args)
{
// WriteLine("Network interfaces:");
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var networkInterface in interfaces)
{
if (networkInterface.OperationalStatus != OperationalStatus.Up)
{
continue;
}
// WriteLine(" {1} \"{2}\" ({0}):", networkInterface.NetworkInterfaceType, networkInterface.Name, networkInterface.Description);
IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
foreach (IPAddress dnsAddress in dnsAddresses)
{
// WriteLine(" {0}", dnsAddress);
}
}
List<Task> tasks = new List<Task>();
for (int i = 0; i < args.Length / 2; i++)
{
tasks.Add(DownloadFile(args[i * 2], args[i * 2 + 1], 300, TimeSpan.FromSeconds(10)));
}
Task.WhenAll(tasks).Wait();
return 0;
}
private static async Task DownloadFile(string sourceUrl, string destinationFile, int attempts, TimeSpan delay)
{
string name = Path.GetFileName(destinationFile);
bool success = false;
for (int i = 0; i < attempts; i++)
{
try
{
if (i > 0)
{
WriteLine(" {0} attempt #{1} after {2}s", name, i + 1, delay.TotalSeconds);
await Task.Delay(delay);
}
else
{
WriteLine(" {0} {1}", name, sourceUrl);
}
success = await DownloadFile(name, sourceUrl, destinationFile);
if (success)
{
break;
}
WriteErrorLine(" {0} abort", name);
}
catch (Exception ex)
{
WriteErrorLine(" {0} error: \"{1}\"", name, ex.Message);
}
}
if (!success)
{
throw new Exception("Downloading " + sourceUrl + " failed!");
}
}
private static async Task<bool> DownloadFile(string name, string sourceUrl, string destinationFile)
{
Uri source = new Uri(sourceUrl);
string host = source.DnsSafeHost;
if (string.IsNullOrWhiteSpace(host))
{
throw new InvalidOperationException("Resolving " + sourceUrl + " failed!");
}
// WriteLine(" {0} resolved to domain {1}", name, host);
IPHostEntry hostInfo = await Dns.GetHostEntryAsync(host);
foreach (var address in hostInfo.AddressList)
{
// WriteLine(" {0} resolved to address {1}", name, address);
}
using (WebClient client = new WebClient())
{
long lastPercent = -1;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
client.DownloadProgressChanged += (sender, args) =>
{
long percent = 100 * args.BytesReceived / args.TotalBytesToReceive;
if (percent % 20 == 0 && percent > lastPercent)
{
double speed = args.TotalBytesToReceive / 1024.0 / 1024.0 / stopwatch.Elapsed.TotalSeconds;
if (lastPercent == -1)
{
speed = 0;
}
lastPercent = percent;
WriteLine(" {0}%\t{1} ({2:0.0} MB/s)", percent, name, speed);
}
};
bool completed = false;
client.DownloadFileCompleted += (sender, args) =>
{
completed = true;
};
await client.DownloadFileTaskAsync(source, destinationFile);
return completed;
}
}
private static void WriteLine(string message, params object[] args)
{
lock (LockObject)
{
Console.WriteLine(message, args);
}
}
private static void WriteErrorLine(string message, params object[] args)
{
lock (LockObject)
{
Console.Error.WriteLine(message, args);
}
}
}
}
"@ | Set-Content -Path "${env:Temp}\Web.cs"
[string]$scriptsWeb = Get-Content -Path "${env:Temp}\Web.cs" -Raw
Add-Type -TypeDefinition "$scriptsWeb" -Language CSharp
Remove-Item -Force "${env:Temp}\Web.cs"
}
}
function Update-SessionEnvironment {
$userName = $env:USERNAME
$architecture = $env:PROCESSOR_ARCHITECTURE
$psModulePath = $env:PSModulePath
#Path gets special treatment b/c it munges the two together
$paths = 'Machine', 'User' |
% {[System.Environment]::GetEnvironmentVariable("PATH","$_") -split ';'} |
Select -Unique
$env:PATH = $paths -join ';'
# PSModulePath is almost always updated by process, so we want to preserve it.
$env:PSModulePath = $psModulePath
# reset user and architecture
if ($userName) { $env:USERNAME = $userName; }
if ($architecture) { $env:PROCESSOR_ARCHITECTURE = $architecture; }
}
function Invoke-QuietGPG {
[string]$cacheErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
gpg --quiet @args 2> "${env:Temp}\gpg.error.log"
if ($LASTEXITCODE -ne 0){
[string]$gpgErrorLog = Get-Content -Path "${env:Temp}\gpg.error.log" -Raw
Remove-Item -Force "${env:Temp}\gpg.error.log"
Write-Error -Message "$gpgErrorLog" -ErrorAction $cacheErrorActionPreference
}
$ErrorActionPreference = $cacheErrorActionPreference
Remove-Item -Force "${env:Temp}\gpg.error.log"
}
#################################################
# Install multiple HashiCorp binaries
# ARGUMENTS:
# <name>[:<version>] [...]
# EXAMPLE:
# Install-HashiCorpBinaries packer terraform:0.14.0-rc1
# RETURN:
# * 0 if installation succeeded or skipped
# * non-zero on error
#################################################
function Install-HashiCorpBinaries {
[CmdletBinding()]
param (
[Parameter( Mandatory=$false,
ValueFromRemainingArguments=$true )]
[string[]]$archives
)
[string]$downloadUrl = 'https://releases.hashicorp.com'
# https://www.hashicorp.com/security
# HashiCorp PGP key
[string]$pgpKeystore = 'https://keybase.io/hashicorp/pgp_keys.asc'
[string]$pgpThumbprint = 'C874011F0AB405110D02105534365D9472D7468F'
# HashiCorp Code Signature
[string]$codeSignThumbprint = '35AB9FC834D217E9E7B1778FB1B97AF7C73792F2'
[string]$os = 'windows'
[string]$arch = 'undefined'
# Look up the architecture
if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -match '64'){
$arch = 'amd64'
if ((Get-WmiObject Win32_ComputerSystem).SystemType -match 'ARM'){
# Because of current lack of x64 emulation on ARM64 support
$arch = '386'
}
}
elseif ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -match '32'){
$arch = '386'
}
# Verify the system requirements
[string[]]$cmds = 'gpg'
[string]$cmds_error = ''
[string]$gpg = 0
foreach ($cmd in $cmds){
try {
Get-Command $cmd | Out-Null
}
catch {
switch ($cmd){
'gpg' {$gpg = 1}
Default {$cmds_error += "`r`n Command `"${cmd}`" not found"}
}
}
}
if (-not ([string]::IsNullOrEmpty($cmds_error))){
WriteError "FATAL: Ensure system requirements are installed and added to system's PATH!${cmd_errors}"
}
if ($gpg -eq 0){
# Verfiy the integrity of the PGP key and import the PGP key
Invoke-WebRequest -UseBasicParsing -Method Get `
-Uri "${pgpKeystore}" -OutFile "${env:Temp}\hashicorp.asc" | `
Out-Null
if ("${pgpThumbprint}" -ne (Invoke-QuietGPG --dry-run --import --import-options import-show "${env:Temp}\hashicorp.asc" | `
Select-String -Pattern '^[ \t]+([ A-Z0-9]{40,})$' -AllMatches | `
% {$_.Matches.Groups[1]} | % {$_.Value})){
Write-Error "FATAL: Integrity of the PGP key `"${pgpKeystore}`" is compromised"
}
Invoke-QuietGPG --import "${env:Temp}\hashicorp.asc"
Remove-Item -Force "${env:Temp}\hashicorp.asc"
}
[string[]]$verifiedArchives = @()
[string]$downloadFiles = ''
foreach ($archive in $archives){
[string]$name, [string]$version = "$archive" -split ":"
# Look up the latest stable version
if ([string]::IsNullOrEmpty($version) -or "$version" -eq "latest" ){
[string]$regex = "^([\.0-9]+)$"
try {
$version = (ConvertFrom-Json (Invoke-WebRequest -UseBasicParsing -Method Get `
-Uri "${downloadUrl}/${name}/index.json")).versions | `
% {$_.PSObject.Properties} | % {"$($_.Name)"} | `
Select-String -Pattern $regex -AllMatches | `
% {$_.Matches} | % {$_.Groups[1]} | % {$_.Value} | `
% {[System.Version]$_} | Sort-Object Major,Minor,Build -Descending | `
Select-Object -first 1 | % {"$($_.Major).$($_.Minor).$($_.Build)"}
}
catch {
$version = "undefined"
}
}
# Look up the archive
try {
Invoke-WebRequest -UseBasicParsing -Method Head `
-Uri "${downloadUrl}/${name}/${version}/${name}_${version}_${os}_${arch}.zip" | `
Out-Null
}
catch {
Write-Host "Installing ${name} (${version})"
Write-Host "ERROR: No appropriate archive for your product, version, operating"
Write-Host " system or architecture on ${downloadUrl}"
Write-Host " product: ${name}"
Write-Host " version: ${version}"
Write-Host " operating system: ${os}"
Write-Host " architecture: ${arch}"
continue
}
$verifiedArchives += "${name}:${version}"
$downloadFiles += "'${downloadUrl}/${name}/${version}/${name}_${version}_${os}_${arch}.zip', '${env:Temp}\${name}_${version}_${os}_${arch}.zip', "
$downloadFiles += "'${downloadUrl}/${name}/${version}/${name}_${version}_SHA256SUMS', '${env:Temp}\${name}_${version}_SHA256SUMS', "
if ($gpg -eq 0){
$downloadFiles += "'${downloadUrl}/${name}/${version}/${name}_${version}_SHA256SUMS.sig', '${env:Temp}\${name}_${version}_SHA256SUMS.sig', "
}
}
# Download the archive, checksums and signature files
Write-Host "Fetching ${downloadUrl}/"
$downloadFiles = $downloadFiles.SubString(0, [math]::Max(0, $downloadFiles.length - 2))
if (-not ([string]::IsNullOrEmpty($downloadFiles))){
Import-ScriptsWebDownload
[string]$download = "[Scripts.Web]::DownloadFiles($downloadFiles)"
Invoke-Expression $download | Out-Null
}
foreach ($archive in $verifiedArchives){
[string]$name, [string]$version = "$archive" -split ":"
Write-Host "Installing ${name} (${version})"
if ($gpg -eq 0){
# Verify the integrity of the checksums file
Invoke-QuietGPG --verify "${env:Temp}\${name}_${version}_SHA256SUMS.sig" "${env:Temp}\${name}_${version}_SHA256SUMS"
# Clean up the signature file
Remove-Item -Force "${env:Temp}\${name}_${version}_SHA256SUMS.sig"
}
# Verify the integrity of the archive
[string]$checksum = (Get-FileHash "${env:Temp}\${name}_${version}_${os}_${arch}.zip" -Algorithm SHA256).Hash
[string]$regex = "^([A-Fa-f0-9]{64}).*${name}_${version}_${os}_${arch}\.zip$"
if ($checksum -ne (Get-Content -Path "${env:Temp}\${name}_${version}_SHA256SUMS" | `
Select-String -Pattern $regex -AllMatches | % {$_.Matches.Groups[1]} | % {$_.Value})){
Write-Error "FATAL: Integrity of the archive `"${name}_${version}_${os}_${arch}.zip`" is compromised"
}
# Clean up the checksums file
Remove-Item -Force "${env:Temp}\${name}_${version}_SHA256SUMS"
# Extract the archive
Expand-Archive -Force -Path "${env:Temp}\${name}_${version}_${os}_${arch}.zip" -DestinationPath "${env:Temp}"
# Clean up the archive
Remove-Item -Force "${env:Temp}\${name}_${version}_${os}_${arch}.zip"
# Verify the integrity of the executable
if ($codeSignThumbprint -ne ((Get-AuthenticodeSignature -FilePath "${env:Temp}\${name}.exe").SignerCertificate).thumbprint){
Write-Error "FATAL: Integrity of the executable `"${name}.exe`" is compromised"
}
# Add the executable to system's PATH
if (-not (Test-Path "${env:ProgramW6432}\HashiCorp\bin")){
New-Item -ItemType Directory -Force -Path "${env:ProgramW6432}\HashiCorp\bin" | Out-Null
}
Move-Item -Force -Path "${env:Temp}\${name}.exe" "${env:ProgramW6432}\HashiCorp\bin\${name}.exe"
Update-SessionEnvironment
if (-not ("$env:PATH" -match [Regex]::Escape("${env:ProgramW6432}\HashiCorp\bin"))){
SETX /M PATH ('{0};{1};' -f "${env:PATH}", "${env:ProgramW6432}\HashiCorp\bin") | Out-Null
}
# Verify the CLI installation
Update-SessionEnvironment
$verify = Invoke-Expression "${name} version"
$verify = $verify | Select-String -Pattern "^.*([0-9]+\.[0-9]+\.[0-9]+[0-9a-zA-Z\.+-]*).*$" -AllMatches | `
% {$_.Matches.Groups[1]} | % {$_.Value} | Select-Object -First 1
if ("${verify}" -ne "${version}"){
Write-Host "WARNING: Another executable file is prioritized when the command `"${name}`" is executed"
Write-Host " Check your system's PATH!"
}
}
}
Install-HashiCorpBinaries @args