From 65c8ac92e10f29025ff506e1c70998384691d0dc Mon Sep 17 00:00:00 2001 From: Bruno Cornec Date: Sat, 2 Apr 2016 03:07:41 +0200 Subject: [PATCH 1/3] Fix #39 with new log files management - Default location for log files is now under the HOME directory of the user instead of /var/log - Create that default .redfish dir if not already existing - Adapt error messages --- redfish-client/redfish-client | 4 ++-- redfish/config.py | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/redfish-client/redfish-client b/redfish-client/redfish-client index c6d1ddb..fffd91e 100755 --- a/redfish-client/redfish-client +++ b/redfish-client/redfish-client @@ -24,8 +24,8 @@ redfish-client :: --insecure Ignore SSL certificates --debug LEVEL Run in debug mode, LEVEL from 1 to 3 increase verbosity Security warning LEVEL > 1 could reveal password into the logs - --debugfile FILE Specify the client debugfile [default: /var/log/python-redfish/redfish-client.log] - --libdebugfile FILE Specify python-redfish library log file [default: /var/log/python-redfish/python-redfish.log] + --debugfile FILE Specify the client debugfile [default: $HOME/.redfish/redfish-client.log] + --libdebugfile FILE Specify python-redfish library log file [default: $HOME/.redfish/python-redfish.log] config commands : manage the configuration file. manager commands : manage the manager (Light out management). If diff --git a/redfish/config.py b/redfish/config.py index 9f2b7f1..6fd9b8c 100644 --- a/redfish/config.py +++ b/redfish/config.py @@ -16,7 +16,22 @@ from logging.handlers import RotatingFileHandler logger = None TORTILLADEBUG = True -REDFISH_LOGFILE = "/var/log/python-redfish/python-redfish.log" +HOME = os.getenv('HOME') +if HOME == '': + print("$HOME environment variable not set, please check your system") + sys.exit(1) + +REDFISH_HOME = HOME + "/.redfish" +if not os.path.exists(REDFISH_HOME): + try: + os.mkdir(REDFISH_HOME) + except IOError: + print('ERROR: can\'t create {}.\n'.format(REDFISH_HOME)) + print(' Try to create directory {}'.format(os.path.dirname(REDFISH_LOGFILE))) + print(' using: mkdir -p {}'.format(os.path.dirname(REDFISH_LOGFILE))) + sys.exit(1) + +REDFISH_LOGFILE = REDFISH_HOME + "/python-redfish.log" CONSOLE_LOGGER_LEVEL = logging.DEBUG FILE_LOGGER_LEVEL = logging.DEBUG @@ -43,14 +58,13 @@ def initialize_logger(REDFISH_LOGFILE, formatter = logging.Formatter( '%(asctime)s :: %(levelname)s :: %(message)s' ) + f = open(os.path.expandvars(REDFISH_LOGFILE), 'w') + f.close() + try: - file_handler = RotatingFileHandler(REDFISH_LOGFILE, 'a', 1000000, 1) + file_handler = RotatingFileHandler(os.path.expandvars(REDFISH_LOGFILE), 'a', 1000000, 1) except IOError: print('ERROR: {} does not exist or is not writeable.\n'.format(REDFISH_LOGFILE)) - print('1- Try to create directory {}'.format(os.path.dirname(REDFISH_LOGFILE))) - print(' using: sudo mkdir -p {}'.format(os.path.dirname(REDFISH_LOGFILE))) - print('2- Try to get the {} ownership'.format(os.path.dirname(REDFISH_LOGFILE))) - print(' using: sudo chown {} {}'.format(getpass.getuser(), os.path.dirname(REDFISH_LOGFILE))) sys.exit(1) # First logger to file From 0ae2d44bbc2a81aab153b8053ef2602c9ded4722 Mon Sep 17 00:00:00 2001 From: Bruno Cornec Date: Sat, 2 Apr 2016 03:29:15 +0200 Subject: [PATCH 2/3] Fix #40 by using the .redfish dir for inventory change references to .redfish.conf into .redfish.inventory --- examples/simple-proliant.py | 2 +- examples/simple-simulator.py | 2 +- redfish-client/redfish-client | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/simple-proliant.py b/examples/simple-proliant.py index a2f2a54..806b4d9 100644 --- a/examples/simple-proliant.py +++ b/examples/simple-proliant.py @@ -23,7 +23,7 @@ if HOME == '': sys.exit(1) try: - with open(HOME + "/.redfish.conf") as json_data: + with open(HOME + "/.redfish/inventory") as json_data: config = json.load(json_data) json_data.close() except IOError as e: diff --git a/examples/simple-simulator.py b/examples/simple-simulator.py index 3e73387..b431d67 100644 --- a/examples/simple-simulator.py +++ b/examples/simple-simulator.py @@ -21,7 +21,7 @@ if HOME == '': sys.exit(1) try: - with open(HOME + "/.redfish.conf") as json_data: + with open(HOME + "/.redfish/inventory") as json_data: config = json.load(json_data) json_data.close() except IOError as e: diff --git a/redfish-client/redfish-client b/redfish-client/redfish-client index fffd91e..ac4d327 100755 --- a/redfish-client/redfish-client +++ b/redfish-client/redfish-client @@ -20,7 +20,7 @@ redfish-client :: -h --help Show this screen. --version Show version. -c --config FILE Configuration file - -i --inventory FILE Configuration file [default: $HOME/.redfish.conf] + -i --inventory FILE Configuration file [default: $HOME/.redfish/inventory] --insecure Ignore SSL certificates --debug LEVEL Run in debug mode, LEVEL from 1 to 3 increase verbosity Security warning LEVEL > 1 could reveal password into the logs @@ -61,7 +61,7 @@ class InventoryFile(object): If the file does not exist create an empty one ready to receive data :param inventory_file: File name of the configuration file - default: ~/.redfish.conf + default: ~/.redfish/inventory :type config-file: str :returns: Nothing From 8aa83c50ee3be7d42002e86c8674e0ad832ef09b Mon Sep 17 00:00:00 2001 From: Bruno Cornec Date: Mon, 4 Apr 2016 21:24:06 +0200 Subject: [PATCH 3/3] Improve #39 - manage variable expanion for logfiles - improve inventory handling - remove now useless checks for HOME --- redfish-client/redfish-client | 13 +------------ redfish/config.py | 15 +++++++++------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/redfish-client/redfish-client b/redfish-client/redfish-client index ac4d327..bcc34eb 100755 --- a/redfish-client/redfish-client +++ b/redfish-client/redfish-client @@ -337,16 +337,6 @@ if __name__ == '__main__': logger.info("Arguments parsed") logger.debug(arguments) - # Get $HOME environment variables. - HOME = os.getenv('HOME') - - if(not HOME): - print('ERROR: $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) - # Load config config = configparser.ConfigParser(allow_no_value=True) logger.debug("Read configuration file") @@ -365,8 +355,7 @@ if __name__ == '__main__': logger.error('Configuration file not found at %s.' % configfile) sys.exit(1) - arguments['--inventory'] = arguments['--inventory'].replace('~', HOME) - arguments['--inventory'] = arguments['--inventory'].replace('$HOME', HOME) + arguments['--inventory'] = os.path.expandvars(arguments['--inventory']) inventory = InventoryFile(arguments['--inventory']) # Initialize Template system (jinja2) diff --git a/redfish/config.py b/redfish/config.py index 6fd9b8c..33391e4 100644 --- a/redfish/config.py +++ b/redfish/config.py @@ -5,23 +5,26 @@ from __future__ import print_function from __future__ import division from __future__ import absolute_import from future import standard_library -standard_library.install_aliases() import logging import sys import os import getpass from logging.handlers import RotatingFileHandler +standard_library.install_aliases() # Global variable definition logger = None TORTILLADEBUG = True HOME = os.getenv('HOME') -if HOME == '': +if HOME is None: print("$HOME environment variable not set, please check your system") sys.exit(1) +if HOME == '': + print("$HOME environment is set, but empty, please check your system") + sys.exit(1) -REDFISH_HOME = HOME + "/.redfish" +REDFISH_HOME = os.path.join(HOME, ".redfish") if not os.path.exists(REDFISH_HOME): try: os.mkdir(REDFISH_HOME) @@ -31,7 +34,7 @@ if not os.path.exists(REDFISH_HOME): print(' using: mkdir -p {}'.format(os.path.dirname(REDFISH_LOGFILE))) sys.exit(1) -REDFISH_LOGFILE = REDFISH_HOME + "/python-redfish.log" +REDFISH_LOGFILE = os.path.join(REDFISH_HOME, "python-redfish.log") CONSOLE_LOGGER_LEVEL = logging.DEBUG FILE_LOGGER_LEVEL = logging.DEBUG @@ -58,13 +61,13 @@ def initialize_logger(REDFISH_LOGFILE, formatter = logging.Formatter( '%(asctime)s :: %(levelname)s :: %(message)s' ) - f = open(os.path.expandvars(REDFISH_LOGFILE), 'w') - f.close() try: file_handler = RotatingFileHandler(os.path.expandvars(REDFISH_LOGFILE), 'a', 1000000, 1) except IOError: print('ERROR: {} does not exist or is not writeable.\n'.format(REDFISH_LOGFILE)) + print(' Try to create directory {}'.format(os.path.dirname(REDFISH_LOGFILE))) + print(' using: mkdir -p {}'.format(os.path.dirname(REDFISH_LOGFILE))) sys.exit(1) # First logger to file