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:
parent
a3bcfd1266
commit
175a4c00c2
2
redfish-client/etc/redfish-client.conf
Normal file
2
redfish-client/etc/redfish-client.conf
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[redfish-client]
|
||||||
|
templates_path = /usr/share/redfish-client/templates
|
@ -37,10 +37,10 @@ import json
|
|||||||
import pprint
|
import pprint
|
||||||
import docopt
|
import docopt
|
||||||
import logging
|
import logging
|
||||||
import redfish
|
import ConfigParser
|
||||||
import requests.packages.urllib3
|
|
||||||
import jinja2
|
import jinja2
|
||||||
|
import requests.packages.urllib3
|
||||||
|
import redfish
|
||||||
|
|
||||||
class ConfigFile(object):
|
class ConfigFile(object):
|
||||||
'''redfisht-client configuration file management'''
|
'''redfisht-client configuration file management'''
|
||||||
@ -253,7 +253,13 @@ if __name__ == '__main__':
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Display manager information using jinja2 template
|
# Display manager information using jinja2 template
|
||||||
|
try:
|
||||||
template = jinja2_env.get_template("manager_info.template")
|
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)
|
print template.render(r=remote_mgmt)
|
||||||
|
|
||||||
|
|
||||||
@ -314,8 +320,9 @@ if __name__ == '__main__':
|
|||||||
logger.info("Arguments parsed")
|
logger.info("Arguments parsed")
|
||||||
logger.debug(arguments)
|
logger.debug(arguments)
|
||||||
|
|
||||||
# Get $HOME environment.
|
# Get $HOME and $VIRTUAL_ENV environment variables.
|
||||||
HOME = os.getenv('HOME')
|
HOME = os.getenv('HOME')
|
||||||
|
VIRTUAL_ENV = os.getenv('VIRTUAL_ENV')
|
||||||
|
|
||||||
if not HOME:
|
if not HOME:
|
||||||
print('$HOME environment variable not set, please check your system')
|
print('$HOME environment variable not set, please check your system')
|
||||||
@ -323,14 +330,38 @@ if __name__ == '__main__':
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
logger.debug("Home directory : %s" % HOME)
|
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)
|
arguments['--conf_file'] = arguments['--conf_file'].replace('~', HOME)
|
||||||
conf_file = ConfigFile(arguments['--conf_file'])
|
conf_file = ConfigFile(arguments['--conf_file'])
|
||||||
|
|
||||||
# Initialize Template system (jinja2)
|
# Initialize Template system (jinja2)
|
||||||
# TODO : set the template file location into cmd line default to /usr/share/python-redfish/templates ?
|
# 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")
|
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:
|
if arguments['config'] is True:
|
||||||
logger.debug("Config commands")
|
logger.debug("Config commands")
|
||||||
if arguments['show'] is True:
|
if arguments['show'] is True:
|
||||||
|
Loading…
Reference in New Issue
Block a user