diff options
-rwxr-xr-x | foreman_ansible_inventory.py | 42 | ||||
-rw-r--r-- | tests/test_read_settings.py | 38 |
2 files changed, 65 insertions, 15 deletions
diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py index 7169bd6..a77ca5b 100755 --- a/foreman_ansible_inventory.py +++ b/foreman_ansible_inventory.py @@ -37,6 +37,11 @@ except ImportError: class ForemanInventory(object): + config_paths = [ + "/etc/ansible/foreman.ini", + os.path.dirname(os.path.realpath(__file__)) + '/foreman.ini', + ] + def __init__(self): self.inventory = dict() # A list of groups and the hosts in that group self.cache = dict() # Details about hosts in the inventory @@ -45,14 +50,18 @@ class ForemanInventory(object): self.hostgroups = dict() # host groups def run(self): - self._read_settings() + if not self._read_settings(): + return False self._get_inventory() self._print_data() + return True def _read_settings(self): # Read settings and parse CLI arguments - self.read_settings() + if not self.read_settings(): + return False self.parse_cli_args() + return True def _get_inventory(self): if self.args.refresh_cache: @@ -99,22 +108,21 @@ class ForemanInventory(object): """Reads the settings from the foreman.ini file""" config = ConfigParser.SafeConfigParser() - config_paths = [ - "/etc/ansible/foreman.ini", - os.path.dirname(os.path.realpath(__file__)) + '/foreman.ini', - ] - env_value = os.environ.get('FOREMAN_INI_PATH') if env_value is not None: - config_paths.append(os.path.expanduser(os.path.expandvars(env_value))) + self.config_paths.append(os.path.expanduser(os.path.expandvars(env_value))) - config.read(config_paths) + config.read(self.config_paths) # Foreman API related - self.foreman_url = config.get('foreman', 'url') - self.foreman_user = config.get('foreman', 'user') - self.foreman_pw = config.get('foreman', 'password') - self.foreman_ssl_verify = config.getboolean('foreman', 'ssl_verify') + try: + self.foreman_url = config.get('foreman', 'url') + self.foreman_user = config.get('foreman', 'user') + self.foreman_pw = config.get('foreman', 'password') + self.foreman_ssl_verify = config.getboolean('foreman', 'ssl_verify') + except (ConfigParser.NoOptionError, ConfigParser.NoSectionError) as e: + print("Error parsing configuration: %s" % e, file=sys.stderr) + return False # Ansible related try: @@ -144,7 +152,11 @@ class ForemanInventory(object): self.cache_path_inventory = cache_path + "/%s.index" % script self.cache_path_params = cache_path + "/%s.params" % script self.cache_path_facts = cache_path + "/%s.facts" % script - self.cache_max_age = config.getint('cache', 'max_age') + try: + self.cache_max_age = config.getint('cache', 'max_age') + except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): + self.cache_max_age = 60 + return True def parse_cli_args(self): """Command line argument processing""" @@ -364,4 +376,4 @@ class ForemanInventory(object): if __name__ == '__main__': inv = ForemanInventory() - inv.run() + sys.exit(not inv.run()) diff --git a/tests/test_read_settings.py b/tests/test_read_settings.py new file mode 100644 index 0000000..65cb0ae --- /dev/null +++ b/tests/test_read_settings.py @@ -0,0 +1,38 @@ +# vim: set fileencoding=utf-8 : + +from __future__ import print_function + + +import os +import unittest +import tempfile + +from foreman_ansible_inventory import ForemanInventory + + +class TestReadSettings(unittest.TestCase): + def setUp(self): + self.inv = ForemanInventory() + + def test_parse_nonexistent(self): + os.environ['FOREMAN_INI_PATH'] = '/doesnot/exist' + self.inv.config_paths = [] + self.assertFalse(self.inv.read_settings()) + + def test_parse_params(self): + with tempfile.NamedTemporaryFile() as t: + print(""" +[foreman] +url=http://127.0.0.1 +user=admin +password=secret +ssl_verify=True + """, file=t) + t.flush() + os.environ['FOREMAN_INI_PATH'] = t.name + self.inv.config_paths = [] + self.assertTrue(self.inv.read_settings()) + self.assertEqual(self.inv.foreman_url, 'http://127.0.0.1') + self.assertEqual(self.inv.foreman_user, 'admin') + self.assertEqual(self.inv.foreman_pw, 'secret') + self.assertTrue(self.inv.foreman_ssl_verify) |