diff --git a/libra/common/logger.py b/libra/common/logger.py new file mode 100644 index 00000000..9ab3b612 --- /dev/null +++ b/libra/common/logger.py @@ -0,0 +1,27 @@ +# Copyright 2012 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations + +import logging + + +class Logger(object): + def __init__(self, logfile, level): + logging.basicConfig( + format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', + filename=logfile + ) + self.logger = logging.getLogger('libra') + if level == 'debug': + self.logger.setLevel(logging.DEBUG) + elif level == 'info': + self.logger.setLevel(logging.INFO) diff --git a/libra/common/options.py b/libra/common/options.py new file mode 100644 index 00000000..25ba00a0 --- /dev/null +++ b/libra/common/options.py @@ -0,0 +1,38 @@ +# Copyright 2012 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations + +import argparse +import ConfigParser + + +class Options(object): + def __init__(self, title): + self.title = title + self._parse_args() + self._load_config() + + def _parse_args(self): + parser = argparse.ArgumentParser( + description='Libra {title}'.format(title=self.title) + ) + parser.add_argument('config', help='configuration file', type=file) + parser.add_argument( + '-d', dest='nodaemon', action='store_true', + help='do not run in daemon mode' + ) + + self.args = parser.parse_args() + + def _load_config(self): + self.config = ConfigParser.ConfigParser() + config.readfp(self.args.config) diff --git a/libra/mgm/mgm.py b/libra/mgm/mgm.py index 7130429f..013604cf 100644 --- a/libra/mgm/mgm.py +++ b/libra/mgm/mgm.py @@ -20,6 +20,8 @@ import signal import sys from libra.mgm.listener import Listener +from libra.common.options import Options +from libra.common.logger import Logger class Server(object): @@ -59,28 +61,19 @@ class Server(object): def main(): - parser = argparse.ArgumentParser( - description='LBaaS Node Management Daemon' - ) - parser.add_argument('nodes', metavar='N', type=int, - help='number of nodes to maintain') - parser.add_argument('-d', dest='nodaemon', action='store_true', - help='do not run in daemon mode') - options = parser.parse_args() + options = Options('Node Management Daemon') - logging.basicConfig( - format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', - filename='/var/log/lbaas/lbaas_mgm.log' + logger = Logger( + options.config.get('mgm', 'logfile'), + options.config.get('mgm', 'loglevel') ) - logger = logging.getLogger('lbaas_mgm') - logger.setLevel(logging.INFO) pid_fn = '/var/run/lbaas_mgm/lbaas_mgm.pid' pid = daemon.pidlockfile.TimeoutPIDLockFile(pid_fn, 10) - server = Server(logger, options.nodes) + server = Server(logger.logger, options.config.get('mgm', 'nodes')) - if options.nodaemon: + if options.args.nodaemon: server.main() else: with daemon.DaemonContext(pidfile=pid):