def assume_role(self, region, profile):
# assume role
global connect_args
if six.PY3:
aws_creds = configparser.ConfigParser()
aws_config = configparser.ConfigParser()
else:
aws_creds = configparser.SafeConfigParser()
aws_config = configparser.SafeConfigParser()
aws_creds.read(os.path.expanduser("~/.aws/credentials"))
aws_config.read(os.path.expanduser("~/.aws/config"))
source_profile = self.get_option(aws_config, profile, 'source_profile')
arn = self.get_option(aws_config, profile, 'role_arn')
aws_access_key = self.get_option(aws_creds, source_profile, 'aws_access_key_id')
aws_secret_key = self.get_option(aws_creds, source_profile, 'aws_secret_access_key')
session_name = "role_session_name_" + self.boto_profile
sts_conn = sts.STSConnection(aws_access_key, aws_secret_key)
assume_role = sts_conn.assume_role(role_arn=arn, role_session_name=session_name)
connect_args['aws_access_key_id'] = assume_role.credentials.access_key
connect_args['aws_secret_access_key'] = assume_role.credentials.secret_key
connect_args['security_token'] = assume_role.credentials.session_token
python类ConfigParser()的实例源码
ec2_mod.py 文件源码
项目:ansible-tower-automated-deployment
作者: OliverCable
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def get_config(config_file_path="~/.glogcli.cfg"):
config = configparser.ConfigParser()
try:
open(os.path.expanduser(config_file_path))
except Exception:
click.echo("[WARNING] - Could not find %s file. Please create the configuration file like:" % config_file_path)
click.echo("\n[environment:default]\n"
"host=mygraylogserver.com\n"
"port=443\n"
"username=john.doe\n"
"default_stream=*\n"
"\n"
"[environment:dev]\n"
"host=mygraylogserver.dev.com\n"
"port=443\n"
"proxy=mycompanyproxy.com\n"
"username=john.doe\n"
"default_stream=57e14cde6fb78216a60d35e8\n"
"\n"
"[format:default]\n"
"format={host} {level} {facility} {timestamp} {message}\n"
"\n"
"[format:short]\n"
"format=[{timestamp}] {level} {message}\n"
"\n"
"[format:long]\n"
"format=time: [{timestamp}] level: {level} msg: {message} tags: {tags}\n"
"color=false\n"
"\n")
config.read(os.path.expanduser(config_file_path))
return config
def add(self, username, password, port, address, domain_name, libvirt_uri,
libvirt_sasl_username, libvirt_sasl_password):
# check libvirt's connection and if domain exist prior to adding it
utils.check_libvirt_connection_and_domain(
libvirt_uri, domain_name,
sasl_username=libvirt_sasl_username,
sasl_password=libvirt_sasl_password)
domain_path = os.path.join(self.config_dir, domain_name)
try:
os.makedirs(domain_path)
except OSError as e:
if e.errno == errno.EEXIST:
raise exception.DomainAlreadyExists(domain=domain_name)
raise exception.VirtualBMCError(
'Failed to create domain %(domain)s. Error: %(error)s' %
{'domain': domain_name, 'error': e})
config_path = os.path.join(domain_path, 'config')
with open(config_path, 'w') as f:
config = configparser.ConfigParser()
config.add_section(DEFAULT_SECTION)
config.set(DEFAULT_SECTION, 'username', username)
config.set(DEFAULT_SECTION, 'password', password)
config.set(DEFAULT_SECTION, 'port', six.text_type(port))
config.set(DEFAULT_SECTION, 'address', address)
config.set(DEFAULT_SECTION, 'domain_name', domain_name)
config.set(DEFAULT_SECTION, 'libvirt_uri', libvirt_uri)
if libvirt_sasl_username and libvirt_sasl_password:
config.set(DEFAULT_SECTION, 'libvirt_sasl_username',
libvirt_sasl_username)
config.set(DEFAULT_SECTION, 'libvirt_sasl_password',
libvirt_sasl_password)
config.write(f)
def initialize(self):
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
self._conf_dict = self._as_dict(config)
self._validate()
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def config_from_file():
if os.path.exists(".osfcli.config"):
config_ = configparser.ConfigParser()
config_.read(".osfcli.config")
# for python2 compatibility
config = dict(config_.items('osf'))
else:
config = {}
return config
def init(args):
"""Initialize or edit an existing .osfcli.config file."""
# reading existing config file, convert to configparser object
config = config_from_file()
config_ = configparser.ConfigParser()
config_.add_section('osf')
if 'username' not in config.keys():
config_.set('osf', 'username', '')
else:
config_.set('osf', 'username', config['username'])
if 'project' not in config.keys():
config_.set('osf', 'project', '')
else:
config_.set('osf', 'project', config['project'])
# now we can start asking for new values
print('Provide a username for the config file [current username: {}]:'.format(
config_.get('osf', 'username')))
username = input()
if username:
config_.set('osf', 'username', username)
print('Provide a project for the config file [current project: {}]:'.format(
config_.get('osf', 'project')))
project = input()
if project:
config_.set('osf', 'project', project)
cfgfile = open(".osfcli.config", "w")
config_.write(cfgfile)
cfgfile.close()
def config_from_ini(self, ini):
config = {}
if sys.version_info >= (3, 2):
parser = configparser.ConfigParser()
else:
parser = configparser.SafeConfigParser()
ini = textwrap.dedent(six.u(ini))
parser.readfp(io.StringIO(ini))
for section in parser.sections():
config[section] = dict(parser.items(section))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def update_credentials(profile, credentials):
credentials_file = os.path.expanduser('~/.aws/credentials')
config = configparser.ConfigParser()
config.read(credentials_file)
# Create profile section in credentials file
if not config.has_section(profile):
config.add_section(profile)
# Set access credentials
# `aws_security_token` is used by boto
# `aws_session_token` is used by aws cli
config.set(
profile, 'aws_access_key_id', credentials['AccessKeyId'])
config.set(
profile, 'aws_secret_access_key', credentials['SecretAccessKey'])
config.set(
profile, 'aws_session_token', credentials['SessionToken'])
config.set(
profile, 'aws_security_token', credentials['SessionToken'])
# Update credentials file
with open(credentials_file, 'w') as credentials_file:
config.write(credentials_file)
print(
"# Aws credentials file got updated with temporary access for profile %s"
% profile
)
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def _get_config(self, unit, filename):
"""Get a ConfigParser object for parsing a unit's config file."""
file_contents = unit.file_contents(filename)
# NOTE(beisner): by default, ConfigParser does not handle options
# with no value, such as the flags used in the mysql my.cnf file.
# https://bugs.python.org/issue7005
config = configparser.ConfigParser(allow_no_value=True)
config.readfp(io.StringIO(file_contents))
return config
def __init__(self, *args, **kwargs):
ConfigParser.__init__(self, *args, **kwargs)
self.read_string(parameterized_config(DEFAULT_CONFIG))
self.is_validated = False