def _configure_from_file(self): """Initialize from .ini file. Configuration file is assumed to be named 'brook.ini' and to be located on the same directory than this file, unless the environment variable BROOK_INI_PATH says otherwise. """ brook_ini_default_path = \ os.path.join(os.path.dirname(os.path.realpath(__file__)), 'brook.ini') brook_ini_path = os.environ.get('BROOK_INI_PATH', brook_ini_default_path) config = ConfigParser(defaults={'api_token': '', 'project_id': ''}) config.read(brook_ini_path) self.api_token = config.get('brook', 'api_token') self.project_id = config.get('brook', 'project_id') if not self.api_token: sys.exit( 'You must provide (at least) your Brook.io API token to generate the dynamic ' 'inventory.')
def load_config(self, config_path): # Validate the config file is an actual file if not isfile(config_path): raise ConfigFileException( 'The specified config file does not exist') if not access(config_path, R_OK): raise ConfigFileException( "The specified config file cannot be read") # Read in the file contents: with open(config_path, 'r') as f: config_string = f.read() # First try to yaml load the content (which will also load json) try: config_data = yaml.load(config_string, Loader=yaml.SafeLoader) # If this is an actual ini file, yaml will return the whole thing as a string instead of a dict if type(config_data) is not dict: raise AssertionError( "The yaml config file is not properly formatted as a dict." ) except (AttributeError, yaml.YAMLError, AssertionError): # TowerCLI used to support a config file with a missing [general] section by prepending it if missing if '[general]' not in config_string: config_string = '[general]{0}'.format(config_string) config = ConfigParser() try: placeholder_file = StringIO(config_string) # py2 ConfigParser has readfp, that has been deprecated in favor of read_file in py3 # This "if" removes the deprecation warning if hasattr(config, 'read_file'): config.read_file(placeholder_file) else: config.readfp(placeholder_file) # If we made it here then we have values from reading the ini file, so let's pull them out into a dict config_data = {} for honorred_setting in self.honorred_settings: try: config_data[honorred_setting] = config.get( 'general', honorred_setting) except (NoOptionError): pass except Exception as e: raise ConfigFileException( "An unknown exception occured trying to ini load config file: {0}" .format(e)) except Exception as e: raise ConfigFileException( "An unknown exception occured trying to load config file: {0}". format(e)) # If we made it here, we have a dict which has values in it from our config, any final settings logic can be performed here for honorred_setting in self.honorred_settings: if honorred_setting in config_data: # Veriffy SSL must be a boolean if honorred_setting == 'verify_ssl': if type(config_data[honorred_setting]) is str: setattr(self, honorred_setting, strtobool(config_data[honorred_setting])) else: setattr(self, honorred_setting, bool(config_data[honorred_setting])) else: setattr(self, honorred_setting, config_data[honorred_setting])
def get_credentials(module): """ Get user_id and key from module configuration, environment, or dotfile. Order of priority is module, environment, dotfile. To set in environment: export MCP_USER='******' export MCP_PASSWORD='******' To set in dot file place a file at ~/.dimensiondata with the following contents: [dimensiondatacloud] MCP_USER: myusername MCP_PASSWORD: mypassword """ if not HAS_LIBCLOUD: module.fail_json(msg='libcloud is required for this module.') return None user_id = None key = None # First, try the module configuration if 'mcp_user' in module.params: if 'mcp_password' not in module.params: module.fail_json( '"mcp_user" parameter was specified, but not "mcp_password" ' + '(either both must be specified, or neither).') return None user_id = module.params['mcp_user'] key = module.params['mcp_password'] # Fall back to environment if not user_id or not key: user_id = os.environ.get('MCP_USER', None) key = os.environ.get('MCP_PASSWORD', None) # Finally, try dotfile (~/.dimensiondata) if not user_id or not key: home = expanduser('~') config = ConfigParser.RawConfigParser() config.read("%s/.dimensiondata" % home) try: user_id = config.get("dimensiondatacloud", "MCP_USER") key = config.get("dimensiondatacloud", "MCP_PASSWORD") except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): pass # One or more credentials not found. Function can't recover from this # so it has to raise an error instead of fail silently. if not user_id: raise MissingCredentialsError("Dimension Data user id not found") elif not key: raise MissingCredentialsError("Dimension Data key not found") # Both found, return data return dict(user_id=user_id, key=key)