Skip to content

Instantly share code, notes, and snippets.

@zloeber
Created January 28, 2018 01:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zloeber/44f8b444fd3fa7003248af67bc7e4c7d to your computer and use it in GitHub Desktop.
Save zloeber/44f8b444fd3fa7003248af67bc7e4c7d to your computer and use it in GitHub Desktop.
PowerShell Hashicorp App Downloader
<#
Hashicorp recent app downloader
- Find and download the most recent versions of Hashicorp applications as defined in
$HashicorpPackages. Ignores all plugins and providers.
#>
$HashicorpReleaseManifestURL = 'https://releases.hashicorp.com/index.json'
# Update this to change the packages you want to download
$HashicorpPackages = @('vagrant','terraform','vault')
# Change for whatever platform you want to download for
$HashicorpOS = 'windows'
# Architecture to download for (default is 64 bit)
$HashicorpArch = "amd64|x86_64"
# Need this to download via Invoke-WebRequest
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$req = (Invoke-WebRequest -Uri $HashicorpReleaseManifestURL).Content | ConvertFrom-JSON
<#
function to find the most recent version in the json manifest
Note: We ignore anything not in strict x.x.x version format (betas and such)
#>
function Get-HashiCorpLatestVersion {
param(
$manifest,
$software
)
(($manifest.$software.versions | get-member -MemberType 'NoteProperty').Name | Where {$_ -match "^\d+\.\d+\.\d+$"} | Foreach {[version]$_} | Sort-Object -Descending | Select -First 1).ToString()
}
function Get-AllHashiCorpPackageLatestVersion {
<#
.SYNOPSIS
Retreives the most recent version of Hashicorp software packages from a json manifest
.DESCRIPTION
Retreives the most recent version of Hashicorp software packages from a json manifest
.PARAMETER manifest
JSON manifest data to search.
.EXAMPLE
Get-AllHashiCorpPackageLatestVersion
.NOTES
Author: Zachary Loeber
#>
param(
$manifest
)
$OutHash = @{}
$manifest | Get-Member -MemberType 'NoteProperty' | Foreach {
$OutHash.($_.Name) = Get-HashiCorpLatestVersion $manifest $_.Name
}
$OutHash
}
$req | Get-Member -MemberType 'NoteProperty' | Where {$_.Name -notmatch 'provider|plugin'} | Foreach {
$software = $_.Name
$recentversion = Get-HashiCorpLatestVersion $req $software
if ($HashicorpPackages -contains $software) {
Write-Output "Found $software - $recentversion"
$req.$software.Versions.$recentversion.builds | Where {($_.os -eq $HashicorpOS) -and ($_.arch -match $HashicorpArch)} | Foreach {
Invoke-WebRequest -Uri $_.url -OutFile $_.filename
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment