- manage variable expanion for logfiles
- improve inventory handling
- remove now useless checks for HOME
This commit is contained in:
Bruno Cornec
2016-04-04 21:24:06 +02:00
parent 0ae2d44bbc
commit 8aa83c50ee
2 changed files with 10 additions and 18 deletions

View File

@@ -337,16 +337,6 @@ if __name__ == '__main__':
logger.info("Arguments parsed") logger.info("Arguments parsed")
logger.debug(arguments) 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 # Load config
config = configparser.ConfigParser(allow_no_value=True) config = configparser.ConfigParser(allow_no_value=True)
logger.debug("Read configuration file") logger.debug("Read configuration file")
@@ -365,8 +355,7 @@ if __name__ == '__main__':
logger.error('Configuration file not found at %s.' % configfile) logger.error('Configuration file not found at %s.' % configfile)
sys.exit(1) sys.exit(1)
arguments['--inventory'] = arguments['--inventory'].replace('~', HOME) arguments['--inventory'] = os.path.expandvars(arguments['--inventory'])
arguments['--inventory'] = arguments['--inventory'].replace('$HOME', HOME)
inventory = InventoryFile(arguments['--inventory']) inventory = InventoryFile(arguments['--inventory'])
# Initialize Template system (jinja2) # Initialize Template system (jinja2)

View File

@@ -5,23 +5,26 @@ from __future__ import print_function
from __future__ import division from __future__ import division
from __future__ import absolute_import from __future__ import absolute_import
from future import standard_library from future import standard_library
standard_library.install_aliases()
import logging import logging
import sys import sys
import os import os
import getpass import getpass
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
standard_library.install_aliases()
# Global variable definition # Global variable definition
logger = None logger = None
TORTILLADEBUG = True TORTILLADEBUG = True
HOME = os.getenv('HOME') HOME = os.getenv('HOME')
if HOME == '': if HOME is None:
print("$HOME environment variable not set, please check your system") print("$HOME environment variable not set, please check your system")
sys.exit(1) 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): if not os.path.exists(REDFISH_HOME):
try: try:
os.mkdir(REDFISH_HOME) 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))) print(' using: mkdir -p {}'.format(os.path.dirname(REDFISH_LOGFILE)))
sys.exit(1) 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 CONSOLE_LOGGER_LEVEL = logging.DEBUG
FILE_LOGGER_LEVEL = logging.DEBUG FILE_LOGGER_LEVEL = logging.DEBUG
@@ -58,13 +61,13 @@ def initialize_logger(REDFISH_LOGFILE,
formatter = logging.Formatter( formatter = logging.Formatter(
'%(asctime)s :: %(levelname)s :: %(message)s' '%(asctime)s :: %(levelname)s :: %(message)s'
) )
f = open(os.path.expandvars(REDFISH_LOGFILE), 'w')
f.close()
try: try:
file_handler = RotatingFileHandler(os.path.expandvars(REDFISH_LOGFILE), 'a', 1000000, 1) file_handler = RotatingFileHandler(os.path.expandvars(REDFISH_LOGFILE), 'a', 1000000, 1)
except IOError: except IOError:
print('ERROR: {} does not exist or is not writeable.\n'.format(REDFISH_LOGFILE)) 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) sys.exit(1)
# First logger to file # First logger to file