From 175a4c00c2484493fb70c3574ea0a300bbfccfc3 Mon Sep 17 00:00:00 2001 From: Uggla Date: Sat, 13 Feb 2016 13:54:58 +0100 Subject: [PATCH] Manage templates - Add a master conf file to specify templates location. - Add conf files into package. - Improve client code to support installation with or without virtual env. --- redfish-client/etc/redfish-client.conf | 2 ++ redfish-client/redfish-client.py | 45 ++++++++++++++++++++++---- setup.cfg | 1 + 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 redfish-client/etc/redfish-client.conf diff --git a/redfish-client/etc/redfish-client.conf b/redfish-client/etc/redfish-client.conf new file mode 100644 index 0000000..89cd47b --- /dev/null +++ b/redfish-client/etc/redfish-client.conf @@ -0,0 +1,2 @@ +[redfish-client] +templates_path = /usr/share/redfish-client/templates diff --git a/redfish-client/redfish-client.py b/redfish-client/redfish-client.py index 25ed9fc..d7a509e 100755 --- a/redfish-client/redfish-client.py +++ b/redfish-client/redfish-client.py @@ -37,10 +37,10 @@ import json import pprint import docopt import logging -import redfish -import requests.packages.urllib3 +import ConfigParser import jinja2 - +import requests.packages.urllib3 +import redfish class ConfigFile(object): '''redfisht-client configuration file management''' @@ -253,7 +253,13 @@ if __name__ == '__main__': sys.exit(1) # Display manager information using jinja2 template - template = jinja2_env.get_template("manager_info.template") + try: + template = jinja2_env.get_template("manager_info.template") + except jinja2.exceptions.TemplateNotFound as e: + print('Template "{}" not found in {}.'.format(e.message, jinja2_env.loader.searchpath[0])) + logger.debug('Template "%s" not found in %s.' % (e.message, jinja2_env.loader.searchpath[0])) + sys.exit(1) + print template.render(r=remote_mgmt) @@ -314,23 +320,48 @@ if __name__ == '__main__': logger.info("Arguments parsed") logger.debug(arguments) - # Get $HOME environment. + # Get $HOME and $VIRTUAL_ENV environment variables. HOME = os.getenv('HOME') + VIRTUAL_ENV = os.getenv('VIRTUAL_ENV') if not HOME: print('$HOME environment variable not set, please check your system') logger.error('$HOME environment variable not set') sys.exit(1) logger.debug("Home directory : %s" % HOME) + + if VIRTUAL_ENV: + logger.debug("Virtual env : %s" % VIRTUAL_ENV) + + # Load master conf file + config = ConfigParser.ConfigParser(allow_no_value=True) + logger.debug("Read master configuration file") + master_conf_file_path = "/etc/redfish-client.conf" + + if VIRTUAL_ENV: + logger.debug("Read master configuration file from virtual environment") + master_conf_file_path = VIRTUAL_ENV + master_conf_file_path + + if not os.path.isfile(master_conf_file_path): + print('Master configuration file not found at {}.'.format(master_conf_file_path)) + logger.error('Master configuration file not found at %s.' % master_conf_file_path) + sys.exit(1) + + config.read(master_conf_file_path) arguments['--conf_file'] = arguments['--conf_file'].replace('~', HOME) conf_file = ConfigFile(arguments['--conf_file']) # Initialize Template system (jinja2) # TODO : set the template file location into cmd line default to /usr/share/python-redfish/templates ? + templates_path = config.get("redfish-client", "templates_path") logger.debug("Initialize template system") - jinja2_env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates")) - + if VIRTUAL_ENV: + logger.debug("Read templates file from virtual environment") + templates_path = VIRTUAL_ENV + templates_path + jinja2_env = jinja2.Environment(loader=jinja2.FileSystemLoader(templates_path)) + + # Check cmd line parameters if arguments['config'] is True: logger.debug("Config commands") if arguments['show'] is True: diff --git a/setup.cfg b/setup.cfg index 34337ba..a04f1e1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,7 @@ scripts = data_files = usr/share/redfish-client/templates = redfish-client/templates/* + etc/ = redfish-client/etc/* [build_sphinx]