Created
October 24, 2019 09:58
-
-
Save nohtyp/496ce24f346c543e4aff205b5ffa5373 to your computer and use it in GitHub Desktop.
This script will allow use of Iam roles with vagrant.
This file contains hidden or 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 python3 | |
import os | |
import subprocess | |
import json | |
import sys | |
iam_output = subprocess.check_output(["curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/<role name>], shell=True) | |
#print(iam_output) | |
dict = json.loads(iam_output.decode("utf-8")) | |
os.environ["AWS_DEFAULT_REGION"] = 'us-east-1' | |
os.environ['AWS_ACCESS_KEY_ID'] = dict['AccessKeyId'] | |
os.environ['AWS_SECRET_ACCESS_KEY'] = dict["SecretAccessKey"] | |
os.environ["AWS_ACCESS_SESSION_TOKEN"] = dict["Token"] | |
os.environ["aws_access_session_token"] = dict["Token"] | |
os.environ['access_key_id'] = dict['AccessKeyId'].strip() | |
os.environ['secret_access_key'] = dict['SecretAccessKey'].strip() | |
try: | |
if sys.argv[1] == 'status': | |
os.system("vagrant status") | |
elif sys.argv[1] == 'up': | |
if sys.argv[2] == 'all': | |
os.system("vagrant up") | |
else: | |
os.system("vagrant up " + str(sys.argv[2])) | |
elif sys.argv[1] == 'provision': | |
if sys.argv[2] == 'all': | |
os.system("vagrant provision") | |
else: | |
os.system("vagrant provision " + str(sys.argv[2])) | |
elif sys.argv[1] == 'destroy': | |
if sys.argv[2] == 'all': | |
os.system("vagrant destroy") | |
else: | |
os.system("vagrant destroy " + str(sys.argv[2])) | |
elif sys.argv[1] == 'ssh': | |
os.system("vagrant ssh " + str(sys.argv[2])) | |
else: | |
print("option does not exist in current script") | |
except: | |
print("You have not provided any options and this is the menu you receive") | |
print('\x1b[6;30;42m' + 'You can use either option: status, up, provision, destroy or ssh' + '\x1b[0m') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment