From 3358b01dd4fd9847137af48454bbd053ad429642 Mon Sep 17 00:00:00 2001 From: Steve Lewis Date: Mon, 31 Oct 2016 12:46:52 -0700 Subject: [PATCH] Refactor inventory management filesystem Segmenting the storage-related operations from the rest of the inventory management tooling, in support of better maintainability testability and future support for alternate storage options. Some changes were made in the course of the refactor to facilitate future changes. The include_cwd parameter was removed in the course of the refactor as it was determined to be unused and unneededduring patch set review. Change-Id: Iaa9761a73a626c4f02c7662b119881a79397d690 --- lib/filesystem.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++ lib/manage.py | 49 ++++----------------------- 2 files changed, 91 insertions(+), 43 deletions(-) create mode 100644 lib/filesystem.py diff --git a/lib/filesystem.py b/lib/filesystem.py new file mode 100644 index 0000000000..a6af89ade5 --- /dev/null +++ b/lib/filesystem.py @@ -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 +# (c) 2015, Major Hayden +# + +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 diff --git a/lib/manage.py b/lib/manage.py index 6859c64a27..99261d30b3 100644 --- a/lib/manage.py +++ b/lib/manage.py @@ -20,43 +20,11 @@ """Returns data about containers and groups in tabular formats.""" import argparse import json -import os import prettytable from dictutils import recursive_dict_removal - - -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 +from filesystem import load_from_json +from filesystem import save_to_json def args(): @@ -338,10 +306,8 @@ def main(): # Parse user args user_args = args() - # Get the contents of the system environment json - environment_file = file_find(filename=user_args['file']) - with open(environment_file, 'rb') as f_handle: - inventory = json.loads(f_handle.read()) + # Get the contents of the system inventory + inventory = load_from_json(user_args['file']) # Make a table with hosts in the left column and details about each in the # columns to the right @@ -359,15 +325,12 @@ def main(): print(json.dumps(export_host_info(inventory), indent=2)) elif user_args['clear_ips'] is True: remove_ip_addresses(inventory) - with open(environment_file, 'wb') as f_handle: - f_handle.write(json.dumps(inventory, indent=2)) + save_to_json(user_args['file'], inventory) print('Success. . .') else: recursive_dict_removal(inventory, user_args['remove_item']) - with open(environment_file, 'wb') as f_handle: - f_handle.write(json.dumps(inventory, indent=2)) + save_to_json(user_args['file'], inventory) print('Success. . .') - if __name__ == "__main__": main()