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
def change_data_dir(self, password=None): data_dir = boto.config.get('MySQL', 'data_dir', '/mnt') fresh_install = False is_mysql_running_command = ShellCommand( 'mysqladmin ping') # exit status 0 if mysql is running is_mysql_running_command.run() if is_mysql_running_command.getStatus() == 0: # mysql is running. This is the state apt-get will leave it in. If it isn't running, # that means mysql was already installed on the AMI and there's no need to stop it, # saving 40 seconds on instance startup. time.sleep( 10 ) #trying to stop mysql immediately after installing it fails # We need to wait until mysql creates the root account before we kill it # or bad things will happen i = 0 while self.run("echo 'quit' | mysql -u root") != 0 and i < 5: time.sleep(5) i = i + 1 self.run('/etc/init.d/mysql stop') self.run("pkill -9 mysql") mysql_path = os.path.join(data_dir, 'mysql') if not os.path.exists(mysql_path): self.run('mkdir %s' % mysql_path) fresh_install = True self.run('chown -R mysql:mysql %s' % mysql_path) fp = open('/etc/mysql/conf.d/use_mnt.cnf', 'w') fp.write('# created by pyami\n') fp.write('# use the %s volume for data\n' % data_dir) fp.write('[mysqld]\n') fp.write('datadir = %s\n' % mysql_path) fp.write('log_bin = %s\n' % os.path.join(mysql_path, 'mysql-bin.log')) fp.close() if fresh_install: self.run('cp -pr /var/lib/mysql/* %s/' % mysql_path) self.start('mysql') else: #get the password ubuntu expects to use: config_parser = ConfigParser() config_parser.read('/etc/mysql/debian.cnf') password = config_parser.get('client', 'password') # start the mysql deamon, then mysql with the required grant statement piped into it: self.start('mysql') time.sleep(10) #time for mysql to start grant_command = "echo \"GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '%s' WITH GRANT OPTION;\" | mysql" % password while self.run(grant_command) != 0: time.sleep(5)
def change_data_dir(self, password=None): data_dir = boto.config.get('MySQL', 'data_dir', '/mnt') fresh_install = False is_mysql_running_command = ShellCommand('mysqladmin ping') # exit status 0 if mysql is running is_mysql_running_command.run() if is_mysql_running_command.getStatus() == 0: # mysql is running. This is the state apt-get will leave it in. If it isn't running, # that means mysql was already installed on the AMI and there's no need to stop it, # saving 40 seconds on instance startup. time.sleep(10) #trying to stop mysql immediately after installing it fails # We need to wait until mysql creates the root account before we kill it # or bad things will happen i = 0 while self.run("echo 'quit' | mysql -u root") != 0 and i < 5: time.sleep(5) i = i + 1 self.run('/etc/init.d/mysql stop') self.run("pkill -9 mysql") mysql_path = os.path.join(data_dir, 'mysql') if not os.path.exists(mysql_path): self.run('mkdir %s' % mysql_path) fresh_install = True self.run('chown -R mysql:mysql %s' % mysql_path) fp = open('/etc/mysql/conf.d/use_mnt.cnf', 'w') fp.write('# created by pyami\n') fp.write('# use the %s volume for data\n' % data_dir) fp.write('[mysqld]\n') fp.write('datadir = %s\n' % mysql_path) fp.write('log_bin = %s\n' % os.path.join(mysql_path, 'mysql-bin.log')) fp.close() if fresh_install: self.run('cp -pr /var/lib/mysql/* %s/' % mysql_path) self.start('mysql') else: #get the password ubuntu expects to use: config_parser = ConfigParser() config_parser.read('/etc/mysql/debian.cnf') password = config_parser.get('client', 'password') # start the mysql deamon, then mysql with the required grant statement piped into it: self.start('mysql') time.sleep(10) #time for mysql to start grant_command = "echo \"GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '%s' WITH GRANT OPTION;\" | mysql" % password while self.run(grant_command) != 0: time.sleep(5)
def get(self, section, name, default=None): try: val = ConfigParser.get(self, section, name) except: val = default return val
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)