Break out common components from mgm

Add config file parser
This commit is contained in:
Andrew Hutchings 2012-09-14 14:24:57 +01:00
parent 119384c061
commit 7d03d16e16
3 changed files with 73 additions and 15 deletions

27
libra/common/logger.py Normal file
View File

@ -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)

38
libra/common/options.py Normal file
View File

@ -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)

View File

@ -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):