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.
This commit is contained in:
Uggla 2016-02-13 13:54:58 +01:00
parent a3bcfd1266
commit 175a4c00c2
3 changed files with 41 additions and 7 deletions

View File

@ -0,0 +1,2 @@
[redfish-client]
templates_path = /usr/share/redfish-client/templates

View File

@ -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
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,8 +320,9 @@ 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')
@ -323,14 +330,38 @@ if __name__ == '__main__':
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:

View File

@ -30,6 +30,7 @@ scripts =
data_files =
usr/share/redfish-client/templates = redfish-client/templates/*
etc/ = redfish-client/etc/*
[build_sphinx]