Last active
June 17, 2019 22:44
-
-
Save benperove/e200542238ebe30381d79cb85adb70da to your computer and use it in GitHub Desktop.
A python script to automatically update/install Terraform, Consul, Vault, Nomad, Vagrant, or Packer binaries on linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#benperove@gmail.com | |
import os, re, requests, argparse | |
def upgrade(args): | |
p = os.uname()[0].lower() | |
a = os.uname()[4] | |
h = { | |
'x86' : '386', | |
'x86_64': 'amd64', | |
'armv7l': 'arm', | |
} | |
i = args.dir or '/usr/local/sbin' | |
u = 'https://releases.hashicorp.com/' + args.app | |
s = requests.Session() | |
r = s.get(u) | |
w = re.search(args.app + '.(?P<ver>[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}).', r.text) | |
v = args.version or w.group('ver') | |
z = args.app + '_' + v + '_' + p + '_' + h[a] + '.zip' | |
f = requests.get('%s/%s/%s' % (u, v, z), allow_redirects=True) | |
open(z, 'wb').write(f.content) | |
print("Installing " + args.app + ' ' + v + "\n") | |
os.system('sudo unzip -o %s -d %s && rm -f %s && %s version' % (z, i, z, args.app)) | |
def main(): | |
try: | |
print('\033[38;5;39m' + "\nHashi Updater 0.3\n" + '\033[0m') | |
parser = argparse.ArgumentParser(description='Install the latest release of Hashi apps') | |
parser.add_argument('-a', '--app', metavar='app', required=True, choices=['terraform', 'vault', 'consul', 'nomad', 'vagrant', 'packer'], help='Application [terraform|vault|consul|nomad|vagrant|packer]') | |
parser.add_argument('-d', '--dir', metavar='dir', required=False, help='Install dir if different from default [/usr/local/sbin]') | |
parser.add_argument('-v', '--version', metavar='ver', required=False, help='Version if not the latest') | |
args = parser.parse_args() | |
upgrade(args) | |
except ImportError as e: | |
print("There must have been a glitch in the system\n") | |
print(e.args[0]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment