Merge "Clarify user config loading in dynamic inventory"

This commit is contained in:
Jenkins 2016-02-25 02:04:03 +00:00 committed by Gerrit Code Review
commit 32c92f208b

View File

@ -57,8 +57,8 @@ def args():
epilog='Inventory Generator Licensed "Apache 2.0"')
parser.add_argument(
'--file',
help='User defined configuration file',
'--config',
help='Path containing the user defined configuration files',
required=False,
default=None
)
@ -707,32 +707,28 @@ def container_skel_load(container_skel, inventory, config):
)
def file_find(pass_exception=False, user_file=None):
"""Return the path to a file.
def find_config_path(user_config_path=None):
"""Return the path to the user configuration files.
If no file is found the system will exit.
The file lookup will be done in the following directories:
If no directory is found the system will exit.
The lookup will be done in the following directories:
$(user_config_path)/
/etc/openstack_deploy/
$(pwd)/openstack_deploy/
:param pass_exception: ``bol``
:param user_file: ``str`` Additional location to look in FIRST for a file
:param user_config_path: ``str`` Location to look in FIRST for a file
"""
file_check = [
path_check = [
os.path.join('/etc', 'openstack_deploy'),
]
if user_file is not None:
file_check.insert(0, os.path.expanduser(user_file))
if user_config_path is not None:
path_check.insert(0, os.path.expanduser(user_config_path))
for f in file_check:
for f in path_check:
if os.path.isdir(f):
return f
else:
if pass_exception is False:
raise SystemExit('No file found at: %s' % file_check)
else:
return False
raise SystemExit('No config found at: %s' % path_check)
def _set_used_ips(user_defined_config, inventory):
@ -865,19 +861,19 @@ def main():
all_args = args()
user_defined_config = dict()
# Get the local path
local_path = file_find(
user_file=all_args.get('file')
# Get the path to the user configuration files
config_path = find_config_path(
user_config_path=all_args.get('config')
)
# Load the user defined configuration file
user_config_file = os.path.join(local_path, 'openstack_user_config.yml')
user_config_file = os.path.join(config_path, 'openstack_user_config.yml')
if os.path.isfile(user_config_file):
with open(user_config_file, 'rb') as f:
user_defined_config.update(yaml.safe_load(f.read()) or {})
# Load anything in a conf.d directory if found
base_dir = os.path.join(local_path, 'conf.d')
base_dir = os.path.join(config_path, 'conf.d')
if os.path.isdir(base_dir):
_extra_config(user_defined_config, base_dir)
@ -885,25 +881,25 @@ def main():
if not user_defined_config:
raise SystemExit(
'No user config loaded\n'
'No openstack_user_config files are available in either the base'
' location or the conf.d directory'
'No openstack_user_config files are available in either \n%s'
'\nor \n%s/conf.d directory' % (config_path, config_path)
)
# Get the contents of the system environment json
environment_file = os.path.join(local_path, 'openstack_environment.yml')
environment_file = os.path.join(config_path, 'openstack_environment.yml')
# Load existing openstack environment json
with open(environment_file, 'rb') as f:
environment = yaml.safe_load(f.read())
# Load anything in an env.d directory if found
env_plugins = os.path.join(local_path, 'env.d')
env_plugins = os.path.join(config_path, 'env.d')
if os.path.isdir(env_plugins):
_extra_config(user_defined_config=environment, base_dir=env_plugins)
# Load existing inventory file if found
dynamic_inventory_file = os.path.join(
local_path, 'openstack_inventory.json'
config_path, 'openstack_inventory.json'
)
if os.path.isfile(dynamic_inventory_file):
with open(dynamic_inventory_file, 'rb') as f:
@ -911,7 +907,7 @@ def main():
# Create a backup of all previous inventory files as a tar archive
inventory_backup_file = os.path.join(
local_path,
config_path,
'backup_openstack_inventory.tar'
)
with tarfile.open(inventory_backup_file, 'a') as tar:
@ -979,7 +975,8 @@ def main():
host_hash[_key] = _value
# Save a list of all hosts and their given IP addresses
hostnames_ip_file = os.path.join(local_path, 'openstack_hostnames_ips.yml')
hostnames_ip_file = os.path.join(
config_path, 'openstack_hostnames_ips.yml')
with open(hostnames_ip_file, 'wb') as f:
f.write(
json.dumps(