Created
June 4, 2017 05:23
-
-
Save fernando-mc/0024cbff6aa7214e3dd0ff03536e1501 to your computer and use it in GitHub Desktop.
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 | |
# Python Script to swap AWS Profile configs | |
# Assumes that credentials file is at ~/.aws/credentials | |
import argparse | |
import ConfigParser | |
import os | |
Config = ConfigParser.ConfigParser() | |
Config.read('credentials') | |
def get_profile_to_swap_to(): | |
"""Parse the profile requested from user input""" | |
parser = argparse.ArgumentParser() | |
parser.add_argument("profile", help="profile to set as default") | |
args = parser.parse_args() | |
profile_to_swap_to = args.profile | |
sections = Config.sections() | |
if profile_to_swap_to in sections: | |
return profile_to_swap_to | |
else: | |
section_list = '' | |
for i in sections: | |
section_list += (i + '\n') | |
raise ValueError( | |
"The profile '{0}' is not in your credentials file. \n\n" \ | |
"Available profiles are:\n{1}".format( | |
profile_to_swap_to, | |
section_list | |
) | |
) | |
def swap_profile(profile_to_swap_to): | |
key = Config.get(profile_to_swap_to, 'aws_access_key_id') | |
secret = Config.get(profile_to_swap_to, 'aws_secret_access_key') | |
Config.set('default', 'aws_access_key_id', key) | |
Config.set('default', 'aws_secret_access_key', secret) | |
with open('credentials', 'wb') as configfile: | |
Config.write(configfile) | |
def main(): | |
profile = get_profile_to_swap_to() | |
swap_profile(profile) | |
print 'Swapped to profile: {0}'.format(profile) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment