def main(argv): arguments = docopt( __doc__, version=str(os.path.basename(__file__)) + " " + VERSION, options_first=False) config_file = os.path.expanduser('~/.boto') default_region = 'us-west-2' if arguments['--profile'] is not None: profile = arguments['--profile'] if arguments['--variable'] is not None: variable = arguments['--variable'] profile_found = False with open(config_file) as fp: config = ConfigParser() config.readfp(fp) profiles = config.sections() profile_search_string = "profile " + profile ''' aws-env-vars () { export AWS_ACCESS_KEY_ID=`/Users/jpancoast/Stuff/code/git/github/aws-py-tools/set_aws_env_vars_from_boto.py -p $1 -v access_key` export AWS_SECRET_ACCESS_KEY=`/Users/jpancoast/Stuff/code/git/github/aws-py-tools/set_aws_env_vars_from_boto.py -p $1 -v secret_key` export AWS_DEFAULT_REGION=`/Users/jpancoast/Stuff/code/git/github/aws-py-tools/set_aws_env_vars_from_boto.py -p $1 -v region` } ''' for config_profile in profiles: if config_profile == profile_search_string: profile_found = True aws_region = None if config.has_option(config_profile, 'region'): aws_region = config.get(config_profile, 'region') aws_access_key_id = config.get(config_profile, 'aws_access_key_id' ) aws_secret_access_key = config.get(config_profile, 'aws_secret_access_key' ) if variable == 'access_key': print aws_access_key_id if variable == 'secret_key': print aws_secret_access_key if variable == 'region': if aws_region is not None: print aws_region else: print default_region if not profile_found: print "Error: couldn't find the profile you requested: " + profile
class Config(object): def __init__(self, path=None, fp=None, do_load=True): self._parser = ConfigParser({ 'working_dir': '/mnt/pyami', 'debug': '0' }) if do_load: if path: self.load_from_path(path) elif fp: self.readfp(fp) else: self.read(BotoConfigLocations) if "AWS_CREDENTIAL_FILE" in os.environ: full_path = expanduser(os.environ['AWS_CREDENTIAL_FILE']) try: self.load_credential_file(full_path) except IOError: warnings.warn('Unable to load AWS_CREDENTIAL_FILE (%s)' % full_path) def __setstate__(self, state): # There's test that verify that (transitively) a Config # object can be pickled. Now that we're storing a _parser # attribute and relying on __getattr__ to proxy requests, # we need to implement setstate to ensure we don't get # into recursive loops when looking up _parser when # this object is unpickled. self._parser = state['_parser'] def __getattr__(self, name): return getattr(self._parser, name) def has_option(self, *args, **kwargs): return self._parser.has_option(*args, **kwargs) def load_credential_file(self, path): """Load a credential file as is setup like the Java utilities""" c_data = StringIO() c_data.write("[Credentials]\n") for line in open(path, "r").readlines(): c_data.write( line.replace("AWSAccessKeyId", "aws_access_key_id").replace( "AWSSecretKey", "aws_secret_access_key")) c_data.seek(0) self.readfp(c_data) def load_from_path(self, path): file = open(path) for line in file.readlines(): match = re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line) if match: extended_file = match.group(1) (dir, file) = os.path.split(path) self.load_from_path(os.path.join(dir, extended_file)) self.read(path) def save_option(self, path, section, option, value): """ Write the specified Section.Option to the config file specified by path. Replace any previous value. If the path doesn't exist, create it. Also add the option the the in-memory config. """ config = ConfigParser() config.read(path) if not config.has_section(section): config.add_section(section) config.set(section, option, value) fp = open(path, 'w') config.write(fp) fp.close() if not self.has_section(section): self.add_section(section) self.set(section, option, value) def save_user_option(self, section, option, value): self.save_option(UserConfigPath, section, option, value) def save_system_option(self, section, option, value): self.save_option(BotoConfigPath, section, option, value) def get_instance(self, name, default=None): try: val = self.get('Instance', name) except (NoOptionError, NoSectionError): val = default return val def get_user(self, name, default=None): try: val = self.get('User', name) except (NoOptionError, NoSectionError): val = default return val def getint_user(self, name, default=0): try: val = self.getint('User', name) except (NoOptionError, NoSectionError): val = default return val def get_value(self, section, name, default=None): return self.get(section, name, default) def get(self, section, name, default=None): try: return self._parser.get(section, name) except (NoOptionError, NoSectionError): return default def getint(self, section, name, default=0): try: return self._parser.getint(section, name) except (NoOptionError, NoSectionError): return int(default) def getfloat(self, section, name, default=0.0): try: return self._parser.getfloat(section, name) except (NoOptionError, NoSectionError): return float(default) def getbool(self, section, name, default=False): if self.has_option(section, name): val = self.get(section, name) if val.lower() == 'true': val = True else: val = False else: val = default return val def setbool(self, section, name, value): if value: self.set(section, name, 'true') else: self.set(section, name, 'false') def dump(self): s = StringIO() self.write(s) print(s.getvalue()) def dump_safe(self, fp=None): if not fp: fp = StringIO() for section in self.sections(): fp.write('[%s]\n' % section) for option in self.options(section): if option == 'aws_secret_access_key': fp.write('%s = xxxxxxxxxxxxxxxxxx\n' % option) else: fp.write('%s = %s\n' % (option, self.get(section, option))) def dump_to_sdb(self, domain_name, item_name): from boto.compat import json sdb = boto.connect_sdb() domain = sdb.lookup(domain_name) if not domain: domain = sdb.create_domain(domain_name) item = domain.new_item(item_name) item.active = False for section in self.sections(): d = {} for option in self.options(section): d[option] = self.get(section, option) item[section] = json.dumps(d) item.save() def load_from_sdb(self, domain_name, item_name): from boto.compat import json sdb = boto.connect_sdb() domain = sdb.lookup(domain_name) item = domain.get_item(item_name) for section in item.keys(): if not self.has_section(section): self.add_section(section) d = json.loads(item[section]) for attr_name in d.keys(): attr_value = d[attr_name] if attr_value is None: attr_value = 'None' if isinstance(attr_value, bool): self.setbool(section, attr_name, attr_value) else: self.set(section, attr_name, attr_value)
class Config(object): def __init__(self, path=None, fp=None, do_load=True): self._parser = ConfigParser({'working_dir': '/mnt/pyami', 'debug': '0'}) if do_load: if path: self.load_from_path(path) elif fp: self.readfp(fp) else: self.read(BotoConfigLocations) if "AWS_CREDENTIAL_FILE" in os.environ: full_path = expanduser(os.environ['AWS_CREDENTIAL_FILE']) try: self.load_credential_file(full_path) except IOError: warnings.warn('Unable to load AWS_CREDENTIAL_FILE (%s)' % full_path) def __setstate__(self, state): # There's test that verify that (transitively) a Config # object can be pickled. Now that we're storing a _parser # attribute and relying on __getattr__ to proxy requests, # we need to implement setstate to ensure we don't get # into recursive loops when looking up _parser when # this object is unpickled. self._parser = state['_parser'] def __getattr__(self, name): return getattr(self._parser, name) def has_option(self, *args, **kwargs): return self._parser.has_option(*args, **kwargs) def load_credential_file(self, path): """Load a credential file as is setup like the Java utilities""" c_data = StringIO() c_data.write("[Credentials]\n") for line in open(path, "r").readlines(): c_data.write(line.replace("AWSAccessKeyId", "aws_access_key_id").replace("AWSSecretKey", "aws_secret_access_key")) c_data.seek(0) self.readfp(c_data) def load_from_path(self, path): file = open(path) for line in file.readlines(): match = re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line) if match: extended_file = match.group(1) (dir, file) = os.path.split(path) self.load_from_path(os.path.join(dir, extended_file)) self.read(path) def save_option(self, path, section, option, value): """ Write the specified Section.Option to the config file specified by path. Replace any previous value. If the path doesn't exist, create it. Also add the option the the in-memory config. """ config = ConfigParser() config.read(path) if not config.has_section(section): config.add_section(section) config.set(section, option, value) fp = open(path, 'w') config.write(fp) fp.close() if not self.has_section(section): self.add_section(section) self.set(section, option, value) def save_user_option(self, section, option, value): self.save_option(UserConfigPath, section, option, value) def save_system_option(self, section, option, value): self.save_option(BotoConfigPath, section, option, value) def get_instance(self, name, default=None): try: val = self.get('Instance', name) except (NoOptionError, NoSectionError): val = default return val def get_user(self, name, default=None): try: val = self.get('User', name) except (NoOptionError, NoSectionError): val = default return val def getint_user(self, name, default=0): try: val = self.getint('User', name) except (NoOptionError, NoSectionError): val = default return val def get_value(self, section, name, default=None): return self.get(section, name, default) def get(self, section, name, default=None): try: return self._parser.get(section, name) except (NoOptionError, NoSectionError): return default def getint(self, section, name, default=0): try: return self._parser.getint(section, name) except (NoOptionError, NoSectionError): return int(default) def getfloat(self, section, name, default=0.0): try: return self._parser.getfloat(section, name) except (NoOptionError, NoSectionError): return float(default) def getbool(self, section, name, default=False): if self.has_option(section, name): val = self.get(section, name) if val.lower() == 'true': val = True else: val = False else: val = default return val def setbool(self, section, name, value): if value: self.set(section, name, 'true') else: self.set(section, name, 'false') def dump(self): s = StringIO() self.write(s) print(s.getvalue()) def dump_safe(self, fp=None): if not fp: fp = StringIO() for section in self.sections(): fp.write('[%s]\n' % section) for option in self.options(section): if option == 'aws_secret_access_key': fp.write('%s = xxxxxxxxxxxxxxxxxx\n' % option) else: fp.write('%s = %s\n' % (option, self.get(section, option))) def dump_to_sdb(self, domain_name, item_name): from boto.compat import json sdb = boto.connect_sdb() domain = sdb.lookup(domain_name) if not domain: domain = sdb.create_domain(domain_name) item = domain.new_item(item_name) item.active = False for section in self.sections(): d = {} for option in self.options(section): d[option] = self.get(section, option) item[section] = json.dumps(d) item.save() def load_from_sdb(self, domain_name, item_name): from boto.compat import json sdb = boto.connect_sdb() domain = sdb.lookup(domain_name) item = domain.get_item(item_name) for section in item.keys(): if not self.has_section(section): self.add_section(section) d = json.loads(item[section]) for attr_name in d.keys(): attr_value = d[attr_name] if attr_value is None: attr_value = 'None' if isinstance(attr_value, bool): self.setbool(section, attr_name, attr_value) else: self.set(section, attr_name, attr_value)