Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simple-proliant.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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:
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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:
Expand Down
21 changes: 5 additions & 16 deletions redfish-client/redfish-client
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ 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
--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 <manager_name>
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
31 changes: 24 additions & 7 deletions redfish/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,36 @@
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
REDFISH_LOGFILE = "/var/log/python-redfish/python-redfish.log"
HOME = os.getenv('HOME')
if HOME is None:
print("$HOME environment variable not set, please check your system")
sys.exit(1)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so we should remove that check in the redfish-client to be DRY.

if HOME == '':
print("$HOME environment is set, but empty, please check your system")
sys.exit(1)

REDFISH_HOME = os.path.join(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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should be in the init method, or a function called here and at init step.
The code placed here will be executed at the import of the module, so it is fine for the default location but if the user is setting another location for the logs using the redfish-client options, no check will be done and it will fail.

REDFISH_LOGFILE = os.path.join(REDFISH_HOME, "python-redfish.log")
CONSOLE_LOGGER_LEVEL = logging.DEBUG
FILE_LOGGER_LEVEL = logging.DEBUG

Expand All @@ -43,14 +61,13 @@ def initialize_logger(REDFISH_LOGFILE,
formatter = logging.Formatter(
'%(asctime)s :: %(levelname)s :: %(message)s'
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my mind it is not needed. I guess you want to create the file, but that will be handled by RotatingFileHandler.
BTW, it is good practive to use the context manager "with" to open files. (I could explain you the benefits)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, in case the file doesn't exist yet, it's not created by RotatingFileHandler. I experienced that thus added these 2 lines. I'm not sure I undertand why we need a with here, but let's discuss this afternoon.

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)))
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
Expand Down