def run(self): super(ConfigCLI, self).run() if context.CLIARGS['config_file']: self.config_file = unfrackpath(context.CLIARGS['config_file'], follow=False) self.config = ConfigManager(self.config_file) else: self.config = ConfigManager() self.config_file = find_ini_config_file() if self.config_file: try: if not os.path.exists(self.config_file): raise AnsibleOptionsError("%s does not exist or is not accessible" % (self.config_file)) elif not os.path.isfile(self.config_file): raise AnsibleOptionsError("%s is not a valid file" % (self.config_file)) os.environ['ANSIBLE_CONFIG'] = to_native(self.config_file) except Exception: if self.action in ['view']: raise elif self.action in ['edit', 'update']: display.warning("File does not exist, used empty file: %s" % self.config_file) elif self.action == 'view': raise AnsibleError('Invalid or no config file was supplied') self.execute()
def run(self): super(ConfigCLI, self).run() if self.options.config_file: self.config_file = to_bytes( unfrackpath(self.options.config_file, follow=False)) self.config = ConfigManager(self.config_file) else: self.config = ConfigManager() self.config_file = to_bytes(find_ini_config_file()) try: if not os.path.exists(self.config_file): raise AnsibleOptionsError( "%s does not exist or is not accessible" % (self.config_file)) elif not os.path.isfile(self.config_file): raise AnsibleOptionsError("%s is not a valid file" % (self.config_file)) os.environ['ANSIBLE_CONFIG'] = to_native(self.config_file) except: if self.action in ['view']: raise elif self.action in ['edit', 'update']: display.warning("File does not exist, used empty file: %s" % self.config_file) self.execute()
def ara_config(key, env_var, default, section='ara', value_type=None): """ Wrapper around Ansible's get_config backward/forward compatibility """ # Bootstrap Ansible configuration # Ansible >=2.4 takes care of loading the configuration file itself if LooseVersion(ansible_version) < LooseVersion('2.4.0'): config, path = load_config_file() else: path = find_ini_config_file() config = configparser.ConfigParser() if path is not None: config.read(path) # >= 2.3.0.0 (NOTE: Ansible trunk versioning scheme has 3 digits, not 4) if LooseVersion(ansible_version) >= LooseVersion('2.3.0'): return get_config(config, section, key, env_var, default, value_type=value_type) # < 2.3.0.0 compatibility if value_type is None: return get_config(config, section, key, env_var, default) args = { 'boolean': dict(boolean=True), 'integer': dict(integer=True), 'list': dict(islist=True), 'tmppath': dict(istmppath=True) } return get_config(config, section, key, env_var, default, **args[value_type])
def run(self): super(ConfigCLI, self).run() if self.options.config_file: self.config_file = unfrackpath(self.options.config_file, follow=False) self.config = ConfigManager(self.config_file) else: self.config = ConfigManager() self.config_file = find_ini_config_file() if self.config_file: try: if not os.path.exists(self.config_file): raise AnsibleOptionsError("%s does not exist or is not accessible" % (self.config_file)) elif not os.path.isfile(self.config_file): raise AnsibleOptionsError("%s is not a valid file" % (self.config_file)) os.environ['ANSIBLE_CONFIG'] = to_native(self.config_file) except: if self.action in ['view']: raise elif self.action in ['edit', 'update']: display.warning("File does not exist, used empty file: %s" % self.config_file) elif self.action == 'view': raise AnsibleError('Invalid or no config file was supplied') self.execute()
def run(self): super(ConfigCLI, self).run() if context.CLIARGS['config_file']: self.config_file = unfrackpath(context.CLIARGS['config_file'], follow=False) b_config = to_bytes(self.config_file) if os.path.exists(b_config) and os.access(b_config, os.R_OK): self.config = ConfigManager(self.config_file) else: raise AnsibleOptionsError('The provided configuration file is missing or not accessible: %s' % to_native(self.config_file)) else: self.config = ConfigManager() self.config_file = find_ini_config_file() if self.config_file: try: if not os.path.exists(self.config_file): raise AnsibleOptionsError("%s does not exist or is not accessible" % (self.config_file)) elif not os.path.isfile(self.config_file): raise AnsibleOptionsError("%s is not a valid file" % (self.config_file)) os.environ['ANSIBLE_CONFIG'] = to_native(self.config_file) except Exception: if context.CLIARGS['action'] in ['view']: raise elif context.CLIARGS['action'] in ['edit', 'update']: display.warning("File does not exist, used empty file: %s" % self.config_file) elif context.CLIARGS['action'] == 'view': raise AnsibleError('Invalid or no config file was supplied') context.CLIARGS['func']()
def test_env_has_no_cfg_file(self, setup_env, setup_existing_files): """ANSIBLE_CONFIG is specified but the file does not exist""" warnings = set() # since the cfg file specified by ANSIBLE_CONFIG doesn't exist, the one at cwd that does # exist should be returned assert find_ini_config_file(warnings) == cfg_in_cwd assert warnings == set()
def test_cwd_does_not_exist(self, setup_env, setup_existing_files, monkeypatch): """Smoketest current working directory doesn't exist""" def _os_stat(path): raise OSError('%s does not exist' % path) monkeypatch.setattr('os.stat', _os_stat) warnings = set() assert find_ini_config_file(warnings) == cfg_in_homedir assert warnings == set()
def ara_config(key, env_var, default, section='ara', value_type=None): """ Wrapper around Ansible's get_config backward/forward compatibility """ # Bootstrap Ansible configuration # Ansible >=2.4 takes care of loading the configuration file itself path = find_ini_config_file() config = configparser.ConfigParser() if path is not None: config.read(path) return get_config( config, section, key, env_var, default, value_type=value_type )
def test_cwd_warning_on_writable_no_warning_set(self, setup_env, setup_existing_files, monkeypatch): """Smoketest that the function succeeds even though no warning set was passed in""" real_stat = os.stat def _os_stat(path): if path == working_dir: from posix import stat_result stat_info = list(real_stat(path)) stat_info[stat.ST_MODE] |= stat.S_IWOTH return stat_result(stat_info) else: return real_stat(path) monkeypatch.setattr('os.stat', _os_stat) assert find_ini_config_file() == cfg_in_homedir
def ara_config(key, env_var, default, section='ara', value_type=None): """ Wrapper around Ansible's get_config backward/forward compatibility """ # Bootstrap Ansible configuration # Ansible >=2.4 takes care of loading the configuration file itself path = find_ini_config_file() config = configparser.ConfigParser() if path is not None: config.read(path) return get_config(config, section, key, env_var, default, value_type=value_type)
def test_no_cwd_cfg_no_warning_on_writable(self, setup_env, setup_existing_files, monkeypatch): """If the cwd is writable but there is no config file there, move on with no warning""" real_stat = os.stat def _os_stat(path): if path == working_dir: from posix import stat_result stat_info = list(real_stat(path)) stat_info[stat.ST_MODE] |= stat.S_IWOTH return stat_result(stat_info) else: return real_stat(path) monkeypatch.setattr('os.stat', _os_stat) warnings = set() assert find_ini_config_file(warnings) == cfg_in_homedir assert len(warnings) == 0
def test_no_warning_on_writable_if_env_used(self, setup_env, setup_existing_files, monkeypatch, expected): """If the cwd is writable but ANSIBLE_CONFIG was used, no warning should be issued""" real_stat = os.stat def _os_stat(path): if path == working_dir: from posix import stat_result stat_info = list(real_stat(path)) stat_info[stat.ST_MODE] |= stat.S_IWOTH return stat_result(stat_info) else: return real_stat(path) monkeypatch.setattr('os.stat', _os_stat) warnings = set() assert find_ini_config_file(warnings) == expected assert warnings == set()
def test_cwd_warning_on_writable(self, setup_env, setup_existing_files, monkeypatch): """If the cwd is writable, warn and skip it """ real_stat = os.stat def _os_stat(path): if path == working_dir: from posix import stat_result stat_info = list(real_stat(path)) stat_info[stat.ST_MODE] |= stat.S_IWOTH return stat_result(stat_info) else: return real_stat(path) monkeypatch.setattr('os.stat', _os_stat) warnings = set() assert find_ini_config_file(warnings) == cfg_in_homedir assert len(warnings) == 1 warning = warnings.pop() assert u'Ansible is being run in a world writable directory' in warning assert u'ignoring it as an ansible.cfg source' in warning
def test_ini_in_cwd(self, setup_env, setup_existing_files): """ANSIBLE_CONFIG not specified. Use the cwd cfg""" warnings = set() assert find_ini_config_file(warnings) == cfg_in_cwd assert warnings == set()
def test_env_has_cfg_file(self, setup_env, setup_existing_files, expected): """ANSIBLE_CONFIG is specified, use it""" warnings = set() assert find_ini_config_file(warnings) == expected assert warnings == set()
def test_find_ini_file(self): cur_config = os.environ['ANSIBLE_CONFIG'] os.environ['ANSIBLE_CONFIG'] = cfg_file self.assertEquals(cfg_file, find_ini_config_file()) os.environ['ANSIBLE_CONFIG'] = cur_config
'ARA_LOG_LEVEL': 'INFO', 'ARA_PATH_MAX': 40, 'ARA_PLAYBOOK_OVERRIDE': None, 'ARA_PLAYBOOK_PER_PAGE': 10, 'ARA_PORT': '9191', 'ARA_RESULT_PER_PAGE': 25, 'ARA_SQL_DEBUG': False, 'ARA_TMP_DIR': os.path.expanduser('~/.ansible/tmp') } # Bootstrap Ansible configuration # Ansible >=2.4 takes care of loading the configuration file itself if LooseVersion(ansible_version) < LooseVersion('2.4.0'): config, path = load_config_file() else: path = find_ini_config_file() config = configparser.ConfigParser() if path is not None: config.read(path) # Some defaults need to be based on top of a "processed" ARA_DIR ARA_DIR = _ara_config(config, 'dir', 'ARA_DIR') database_path = os.path.join(ARA_DIR, 'ansible.sqlite') DEFAULTS.update({ 'ARA_LOG_FILE': os.path.join(ARA_DIR, 'ara.log'), 'ARA_DATABASE': 'sqlite:///{}'.format(database_path) }) ARA_AUTOCREATE_DATABASE = _ara_config(config, 'autocreate_database', 'ARA_AUTOCREATE_DATABASE',
def test_ini_in_systemdir(self, setup_env, setup_existing_files): """First config found is the system config""" warnings = set() assert find_ini_config_file(warnings) == '/etc/ansible/ansible.cfg' assert warnings == set()
def test_ini_in_homedir(self, setup_env, setup_existing_files): """First config found is in the homedir""" warnings = set() assert find_ini_config_file(warnings) == cfg_in_homedir assert warnings == set()
def test_no_config(self, setup_env, setup_existing_files): """No config present, no config found""" warnings = set() assert find_ini_config_file(warnings) is None assert warnings == set()