Merge "Refactor inventory management filesystem"

This commit is contained in:
Jenkins 2016-11-16 11:53:14 +00:00 committed by Gerrit Code Review
commit bb78b911e7
2 changed files with 91 additions and 43 deletions

85
lib/filesystem.py Normal file
View File

@ -0,0 +1,85 @@
# Copyright 2014, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# (c) 2014, Kevin Carter <kevin.carter@rackspace.com>
# (c) 2015, Major Hayden <major@mhtx.net>
#
import json
import os
def _get_search_paths(preferred_path=None, suffix=None):
search_paths = [
os.path.join(
'/etc', 'openstack_deploy'
),
]
if preferred_path is not None:
search_paths.insert(0, os.path.expanduser(preferred_path))
if suffix:
search_paths = [os.path.join(p, suffix) for p in search_paths]
return search_paths
def file_find(filename, preferred_path=None, pass_exception=False):
"""Return the path to a file, or False if no file is found.
If no file is found and pass_exception is True, the system will exit.
The file lookup will be done in the following directories:
``preferred_path`` [Optional]
/etc/openstack_deploy/
$(pwd)/openstack_deploy/
:param filename: ``str`` Name of the file to find
:param preferred_path: ``str`` Additional directory to look in FIRST
:param pass_exception: ``bool`` Should a SystemExit be raised if the file
is not found
"""
search_paths = _get_search_paths(preferred_path, suffix=filename)
for file_candidate in search_paths:
if os.path.isfile(file_candidate):
return file_candidate
else:
if pass_exception is False:
raise SystemExit('No file found at: {}'.format(search_paths))
else:
return False
def save_to_json(filename, dictionary):
"""Write out the given dictionary
:param filename: ``str`` Name of the file to write to
:param dictionary: ``dict`` A dictionary to write
"""
target_file = file_find(filename)
with open(target_file, 'wb') as f_handle:
inventory_json = json.dumps(dictionary, indent=2)
f_handle.write(inventory_json)
def load_from_json(filename):
"""Return a dictionary found in a given file
:param filename: ``str`` Name of the file to read from
"""
target_file = file_find(filename)
with open(target_file, 'rb') as f_handle:
dictionary = json.loads(f_handle.read())
return dictionary

View File

@ -20,43 +20,11 @@
"""Returns data about containers and groups in tabular formats.""" """Returns data about containers and groups in tabular formats."""
import argparse import argparse
import json import json
import os
import prettytable import prettytable
from dictutils import recursive_dict_removal from dictutils import recursive_dict_removal
from filesystem import load_from_json
from filesystem import save_to_json
def file_find(filename, user_file=None, pass_exception=False):
"""Return the path to a file.
If no file is found the system will exit.
The file lookup will be done in the following directories:
/etc/openstack_deploy/
$(pwd)/openstack_deploy/
:param filename: ``str`` Name of the file to find
:param user_file: ``str`` Additional location to look in FIRST for a file
"""
file_check = [
os.path.join(
'/etc', 'openstack_deploy', filename
),
os.path.join(
os.getcwd(), filename
)
]
if user_file is not None:
file_check.insert(0, os.path.expanduser(user_file))
for filename in file_check:
if os.path.isfile(filename):
return filename
else:
if pass_exception is False:
raise SystemExit('No file found at: {}'.format(file_check))
else:
return False
def args(): def args():
@ -338,10 +306,8 @@ def main():
# Parse user args # Parse user args
user_args = args() user_args = args()
# Get the contents of the system environment json # Get the contents of the system inventory
environment_file = file_find(filename=user_args['file']) inventory = load_from_json(user_args['file'])
with open(environment_file, 'rb') as f_handle:
inventory = json.loads(f_handle.read())
# Make a table with hosts in the left column and details about each in the # Make a table with hosts in the left column and details about each in the
# columns to the right # columns to the right
@ -359,15 +325,12 @@ def main():
print(json.dumps(export_host_info(inventory), indent=2)) print(json.dumps(export_host_info(inventory), indent=2))
elif user_args['clear_ips'] is True: elif user_args['clear_ips'] is True:
remove_ip_addresses(inventory) remove_ip_addresses(inventory)
with open(environment_file, 'wb') as f_handle: save_to_json(user_args['file'], inventory)
f_handle.write(json.dumps(inventory, indent=2))
print('Success. . .') print('Success. . .')
else: else:
recursive_dict_removal(inventory, user_args['remove_item']) recursive_dict_removal(inventory, user_args['remove_item'])
with open(environment_file, 'wb') as f_handle: save_to_json(user_args['file'], inventory)
f_handle.write(json.dumps(inventory, indent=2))
print('Success. . .') print('Success. . .')
if __name__ == "__main__": if __name__ == "__main__":
main() main()