From c250cf2df8cccfba50ae783b5597e194d9a8d5b3 Mon Sep 17 00:00:00 2001 From: David Whiteside Date: Mon, 28 Nov 2016 15:55:12 -0700 Subject: [PATCH] added ability to change configuration with yaml config file --- .travis.yml | 2 +- README.md | 9 ++++++++- lib/tracknodes/cli.py | 16 +++++++++++++++- lib/tracknodes/tracknodes.py | 3 +++ setup.py | 2 +- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4d79ad5..9fa0541 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ python: # Install Dependencies install: - - pip install nose coveralls + - pip install nose coveralls PyYAML # Run Tests script: diff --git a/README.md b/README.md index 0ff5739..51832e3 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,14 @@ n021 | 2016-11-26 19:00:01 | offline,down | 'DIMM Configuration Error' -- -- ``` -Use --help to show all options. +You can setup the configuration file for tracknodes to change the database location or the command to get node status. Use the below as an example. + +```shell +$ cat /etc/tracknodes.conf +--- +dbfile: "/opt/tracknodes.db" +pbsnodes_cmd: "/opt/pbsnodes" +``` ```shell $ tracknodes --help diff --git a/lib/tracknodes/cli.py b/lib/tracknodes/cli.py index 104d587..3d57a68 100644 --- a/lib/tracknodes/cli.py +++ b/lib/tracknodes/cli.py @@ -1,5 +1,7 @@ """ Command Line Interface Module """ import optparse +import os +import yaml from tracknodes import TrackNodes @@ -21,7 +23,7 @@ def __init__(self): parser.add_option("-c", "--pbsnodes_cmd", dest="pbsnodes_cmd", help="pbsnodes binary location, example: /opt/pbsnodes", metavar="PBSNODESCMD", - default="pbsnodes") + default=None) parser.add_option("-v", "--verbose", dest="verbose", help="Verbose Output", metavar="VERBOSE", @@ -33,6 +35,18 @@ def __init__(self): self.dbfile = options.dbfile self.verbose = options.verbose + # Load Configurations if not set on CLI + if os.path.isfile('/etc/tracknodes.conf'): + with open('/etc/tracknodes.conf', 'r') as f: + tracknodes_conf = yaml.load(f) + if tracknodes_conf is not None: + if "dbfile" in tracknodes_conf: + if self.dbfile is None: + self.dbfile = str(tracknodes_conf["dbfile"]) + if "pbsnodes_cmd" in tracknodes_conf: + if self.pbsnodes_cmd is None: + self.pbsnodes_cmd = str(tracknodes_conf["pbsnodes_cmd"]) + def run(self): """ EntryPoint Of Application """ pbsnodes = TrackNodes(update=self.update, dbfile=self.dbfile, pbsnodes_cmd=self.pbsnodes_cmd, verbose=self.verbose) diff --git a/lib/tracknodes/tracknodes.py b/lib/tracknodes/tracknodes.py index 2be3238..31b93ad 100644 --- a/lib/tracknodes/tracknodes.py +++ b/lib/tracknodes/tracknodes.py @@ -118,6 +118,9 @@ def is_exe(fpath): """ File must have execute bit set """ return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + if program is None: + return None + fpath, fname = os.path.split(program) if fpath: if is_exe(program): diff --git a/setup.py b/setup.py index 559aa44..1145878 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ author_email='david.whiteside@nrel.gov', url='https://github.com/NREL/tracknodes', license='GPL', - install_requires=[], + install_requires=["PyYAML"], package_dir={ '': 'lib'}, packages=find_packages('lib'),