Skip to content

Instantly share code, notes, and snippets.

@aduston
Created November 29, 2016 23:26
Show Gist options
  • Save aduston/7f55be2fa5d5777c4f993cf7eb2d8ee1 to your computer and use it in GitHub Desktop.
Save aduston/7f55be2fa5d5777c4f993cf7eb2d8ee1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup
import tempfile
import shutil
import zipfile
import requests
import StringIO
import re
url = 'https://releases.hashicorp.com/terraform/'
r = requests.get(url)
soup = BeautifulSoup(r.text)
max_version = (0, 0, 0)
max_version_link = None
for a in soup.findAll('a'):
match = re.match("/terraform/(\d+\.\d+\.\d+)/", a['href'])
if match is not None:
version = tuple(int(part) for part in match.group(1).split('.'))
if version > max_version:
max_version = version
max_version_link = match.group(0)
print("Identified max version as {0}".format(max_version))
r = requests.get("https://releases.hashicorp.com{0}".format(max_version_link))
soup = BeautifulSoup(r.text)
link = next((a['href'] for a in soup.findAll('a') if a['href'].endswith('darwin_amd64.zip')), None)
url = "https://releases.hashicorp.com{0}".format(link)
print("Downloading {0}".format(url))
r = requests.get(url, stream=True)
z = zipfile.ZipFile(StringIO.StringIO(r.content))
z.extract('terraform', '/usr/local/bin')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment