Move base environment directory to an argument
In preparation for moving the current code into a new file but retaining the existing dynamic_inventory.py entry-point, this change modifies the main function so that can generate inventory without having to be in the same directory as the env.d dir. An initial attempt to move the `main` function of dynamic_inventory into a structure like this: playbooks/ inventory/ env.d/ <env files> dynamic_inventory.py lib/ generate.py (contents of dynamic_inventory moved here) resulted in errors when trying to access env.d due to using os.path.join(__file__) inside the function. Also, incorporating this change with the move would have resulted in a harder-to-review patch. Instead, the `args` function will be left inside of dynamic_inventory.py when `main` is moved out, so that the CLI-facing code is grouped together. Since the base environment was moved to a command line argument, documentation was added as to its function, as well as the differences from the --config argument. As a side effect, this opens the door to running tests with multiple environment layouts. However, none is added with this review. Change-Id: Ie690f31b64c7381257634a9dbb2ee5b4197fd9bf
This commit is contained in:
parent
ca7df2fa3f
commit
57fef57868
@ -35,12 +35,17 @@ This invocation is useful when testing changes to the dynamic inventory script.
|
|||||||
Inputs
|
Inputs
|
||||||
^^^^^^
|
^^^^^^
|
||||||
|
|
||||||
The ``dynamic_inventory.py`` script takes a single argument, ``--config``. If
|
The ``dynamic_inventory.py`` takes the ``--config`` argument for the directory
|
||||||
not specified, the default is ``/etc/openstack_deploy/``.
|
holding configuration from which to create the inventory. If not specified,
|
||||||
|
the default is ``/etc/openstack_deploy/``.
|
||||||
|
|
||||||
In addition to this argument, the base environment skeleton is provided in the
|
In addition to this argument, the base environment skeleton is provided in the
|
||||||
``playbooks/inventory/env.d`` directory of the OpenStack-Ansible codebase.
|
``playbooks/inventory/env.d`` directory of the OpenStack-Ansible codebase.
|
||||||
|
|
||||||
|
Should an ``env.d`` directory be found in the directory specified by
|
||||||
|
``--config``, it's contents will be added to the base environment, overriding
|
||||||
|
any previous contents in the event of conflicts.
|
||||||
|
|
||||||
.. note:: In all versions prior to |previous_release_formal_name|, this argument was ``--file``.
|
.. note:: In all versions prior to |previous_release_formal_name|, this argument was ``--file``.
|
||||||
|
|
||||||
The following file must be present in the configuration directory:
|
The following file must be present in the configuration directory:
|
||||||
@ -101,6 +106,17 @@ The same JSON structure is printed to stdout, which is consumed by Ansible as
|
|||||||
the inventory for the playbooks.
|
the inventory for the playbooks.
|
||||||
|
|
||||||
|
|
||||||
|
Changing the Base Environment Directory
|
||||||
|
---------------------------------------
|
||||||
|
|
||||||
|
The ``--environment/-e`` argument will take the path to a directory containing
|
||||||
|
an ``env.d`` directory. This defaults to ``playbooks/inventory/`` in the
|
||||||
|
OpenStack-Ansible codebase.
|
||||||
|
|
||||||
|
Contents of this directory are populated into the environment *before* the
|
||||||
|
``env.d`` found in the directory specified by ``--config``.
|
||||||
|
|
||||||
|
|
||||||
Checking Inventory Configuration for Errors
|
Checking Inventory Configuration for Errors
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
|
|
||||||
|
@ -158,6 +158,15 @@ def args(arg_list):
|
|||||||
default=False,
|
default=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-e',
|
||||||
|
'--environment',
|
||||||
|
help=('Directory that contains the base env.d directory.\n'
|
||||||
|
'Defaults to <OSA_ROOT>/playbooks/inventory/.'),
|
||||||
|
required=False,
|
||||||
|
default=os.path.dirname(__file__),
|
||||||
|
)
|
||||||
|
|
||||||
return vars(parser.parse_args(arg_list))
|
return vars(parser.parse_args(arg_list))
|
||||||
|
|
||||||
|
|
||||||
@ -1166,7 +1175,7 @@ def get_inventory(config_path, inventory_file_path):
|
|||||||
return dynamic_inventory
|
return dynamic_inventory
|
||||||
|
|
||||||
|
|
||||||
def main(config=None, check=False, debug=False, **kwargs):
|
def main(config=None, check=False, debug=False, environment=None, **kwargs):
|
||||||
"""Run the main application.
|
"""Run the main application.
|
||||||
|
|
||||||
:param config: ``str`` Directory from which to pull configs and overrides
|
:param config: ``str`` Directory from which to pull configs and overrides
|
||||||
@ -1175,6 +1184,7 @@ def main(config=None, check=False, debug=False, **kwargs):
|
|||||||
:param kwargs: ``dict`` Dictionary of arbitrary arguments; mostly for
|
:param kwargs: ``dict`` Dictionary of arbitrary arguments; mostly for
|
||||||
catching Ansible's required `--list` parameter without name shadowing
|
catching Ansible's required `--list` parameter without name shadowing
|
||||||
the `list` built-in.
|
the `list` built-in.
|
||||||
|
:param environment: ``str`` Directory containing the base env.d
|
||||||
"""
|
"""
|
||||||
if debug:
|
if debug:
|
||||||
log_fmt = "%(lineno)d - %(funcName)s: %(message)s"
|
log_fmt = "%(lineno)d - %(funcName)s: %(message)s"
|
||||||
@ -1188,8 +1198,8 @@ def main(config=None, check=False, debug=False, **kwargs):
|
|||||||
)
|
)
|
||||||
|
|
||||||
user_defined_config = load_user_configuration(config_path)
|
user_defined_config = load_user_configuration(config_path)
|
||||||
|
base_env_dir = environment
|
||||||
base_env = load_environment(os.path.dirname(__file__), {})
|
base_env = load_environment(base_env_dir, {})
|
||||||
environment = load_environment(config_path, base_env)
|
environment = load_environment(config_path, base_env)
|
||||||
|
|
||||||
# Load existing inventory file if found
|
# Load existing inventory file if found
|
||||||
|
@ -91,7 +91,8 @@ def get_inventory(clean=True, extra_args=None):
|
|||||||
"Return the inventory mapping in a dict."
|
"Return the inventory mapping in a dict."
|
||||||
# Use the list argument to more closely mirror
|
# Use the list argument to more closely mirror
|
||||||
# Ansible's use of the callable.
|
# Ansible's use of the callable.
|
||||||
args = {'config': TARGET_DIR, 'list': True}
|
args = {'config': TARGET_DIR, 'list': True,
|
||||||
|
'environment': BASE_ENV_DIR}
|
||||||
if extra_args:
|
if extra_args:
|
||||||
args.update(extra_args)
|
args.update(extra_args)
|
||||||
try:
|
try:
|
||||||
@ -1108,14 +1109,15 @@ class TestConfigCheckFunctional(TestConfigCheckBase):
|
|||||||
self.user_defined_config['log_hosts']['bogus'] = ip
|
self.user_defined_config['log_hosts']['bogus'] = ip
|
||||||
|
|
||||||
def test_checking_good_config(self):
|
def test_checking_good_config(self):
|
||||||
output = di.main(config=TARGET_DIR, check=True)
|
output = di.main(config=TARGET_DIR, check=True,
|
||||||
|
environment=BASE_ENV_DIR)
|
||||||
self.assertEqual(output, 'Configuration ok!')
|
self.assertEqual(output, 'Configuration ok!')
|
||||||
|
|
||||||
def test_duplicated_ip(self):
|
def test_duplicated_ip(self):
|
||||||
self.duplicate_ip()
|
self.duplicate_ip()
|
||||||
self.write_config()
|
self.write_config()
|
||||||
with self.assertRaises(di.MultipleHostsWithOneIPError) as context:
|
with self.assertRaises(di.MultipleHostsWithOneIPError) as context:
|
||||||
di.main(config=TARGET_DIR, check=True)
|
di.main(config=TARGET_DIR, check=True, environment=BASE_ENV_DIR)
|
||||||
self.assertEqual(context.exception.ip, '172.29.236.100')
|
self.assertEqual(context.exception.ip, '172.29.236.100')
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user