From 064afab85ea8ce7df11ad0e4b92eab8b431c90e3 Mon Sep 17 00:00:00 2001 From: Adrian Likins Date: Thu, 27 Sep 2007 13:47:50 -0400 Subject: move logger and config_data to func/func update modules that need new location modules/func_module.py: update to use new logger/config locations, also go ahead and register as a real module, to shut up the start up. It shouldn't hurt anything Some minor import reordering at a couple places (I try to keep at least system import alphabetical) --- func/config_data.py | 52 +++++++++++++++++++++++++++++++ func/logger.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ minion/server.py | 8 +++-- minion/utils.py | 9 +++--- modules/func_module.py | 10 ++++-- overlord/sslclient.py | 15 ++++++--- 6 files changed, 165 insertions(+), 12 deletions(-) create mode 100755 func/config_data.py create mode 100755 func/logger.py diff --git a/func/config_data.py b/func/config_data.py new file mode 100755 index 0000000..daa366a --- /dev/null +++ b/func/config_data.py @@ -0,0 +1,52 @@ +#!/usr/bin/python + +# func +# +# Copyright 2006, Red Hat, Inc +# see AUTHORS +# +# This software may be freely redistributed under the terms of the GNU +# general public license. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +import codes + +import os +import ConfigParser + +CONFIG_FILE = "/etc/func/minion.conf" + +class Config: + + # this class is a Borg + __shared_state = {} + has_read = False + ds = {} + + def __init__(self): + self.__dict__ = self.__shared_state + if not self.has_read: + self.read() + Config.has_read = True + + def read(self): + + if not os.path.exists(CONFIG_FILE): + raise codes.FuncException("Missing %s" % CONFIG_FILE) + + cp = ConfigParser.ConfigParser() + + cp.read([CONFIG_FILE]) + + self.ds["log_level"] = cp.get("general","log_level") + self.ds["overlord_server"] = cp.get("general","overlord_server") + self.ds["certmaster"] = cp.get("general", "certmaster") + self.ds["cert_dir"] = cp.get("general", "cert_dir") + + def get(self): + return self.ds + + diff --git a/func/logger.py b/func/logger.py new file mode 100755 index 0000000..f6f9c0f --- /dev/null +++ b/func/logger.py @@ -0,0 +1,83 @@ +#!/usr/bin/python + +## func +## +## Copyright 2007, Red Hat, Inc +## See AUTHORS +## +## This software may be freely redistributed under the terms of the GNU +## general public license. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +## +## + + +import logging +import config_data + + +# from the comments in http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 +class Singleton(object): + def __new__(type, *args, **kwargs): + if not '_the_instance' in type.__dict__: + type._the_instance = object.__new__(type, *args, **kwargs) + return type._the_instance + +# logging is weird, we don't want to setup multiple handlers +# so make sure we do that mess only once + +class Logger(Singleton): + _no_handlers = True + + def __init__(self, logfilepath ="/var/log/func/func.log"): + + self.config = config_data.Config().get() + if self.config.has_key("log_level"): + self.loglevel = logging._levelNames[self.config["log_level"]] + else: + self.loglevel = logging.INFO + self._setup_logging() + if self._no_handlers: + self._setup_handlers(logfilepath=logfilepath) + + def _setup_logging(self): + self.logger = logging.getLogger("svc") + + def _setup_handlers(self, logfilepath="/var/log/func/func.log"): + handler = logging.FileHandler(logfilepath, "a") + self.logger.setLevel(self.loglevel) + formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") + handler.setFormatter(formatter) + self.logger.addHandler(handler) + self._no_handlers = False + + +class AuditLogger(Singleton): + _no_handlers = True + def __init__(self, logfilepath = "/var/log/func/audit.log"): + self.loglevel = logging.INFO + self._setup_logging() + if self._no_handlers: + self._setup_handlers(logfilepath=logfilepath) + + def log_call(self, CN, cert_hash, method, params): + # square away a good parseable format at some point -akl + self.logger.info("%s %s %s called with %s" % (CN, cert_hash, method, params)) + + + def _setup_logging(self): + self.logger = logging.getLogger("audit") + + def _setup_handlers(self, logfilepath="/var/log/func/audit.log"): + handler = logging.FileHandler(logfilepath, "a") + self.logger.setLevel(self.loglevel) + formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") + handler.setFormatter(formatter) + self.logger.addHandler(handler) + self._no_handlers = False + + + diff --git a/minion/server.py b/minion/server.py index 03c4fa5..ce9df41 100755 --- a/minion/server.py +++ b/minion/server.py @@ -24,11 +24,15 @@ import socket from rhpl.translate import textdomain I18N_DOMAIN = "func" + +from func import config_data +from func import logger + # our modules import AuthedXMLRPCServer import codes -import config_data -import logger +#import config_data +#import logger import module_loader import utils diff --git a/minion/utils.py b/minion/utils.py index 307141f..177d4ee 100755 --- a/minion/utils.py +++ b/minion/utils.py @@ -13,16 +13,17 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ import os +import socket import string import sys +import time import traceback import xmlrpclib -from func import certs + import codes -import socket -import time -import config_data +from func import certs +from func import config_data diff --git a/modules/func_module.py b/modules/func_module.py index 83eb6fc..ae8bb11 100755 --- a/modules/func_module.py +++ b/modules/func_module.py @@ -13,8 +13,8 @@ ## -from func.minion import config_data -from func.minion import logger +from func import config_data +from func import logger class FuncModule(object): @@ -49,6 +49,9 @@ class FuncModule(object): for meth in self.methods: handlers["%s.%s" % (module_name,meth)] = self.methods[meth] +# def list_methods(self): +# return self.methods.keys() + self.__base_methods.keys() + def __module_version(self): return self.version @@ -58,3 +61,6 @@ class FuncModule(object): def __module_description(self): return self.description + +methods = FuncModule() +register_rpc = methods.register_rpc diff --git a/overlord/sslclient.py b/overlord/sslclient.py index 463b8c6..2d2f38e 100644 --- a/overlord/sslclient.py +++ b/overlord/sslclient.py @@ -1,10 +1,12 @@ #!/usr/bin/python +import socket import sys import xmlrpclib import urllib from func import SSLCommon +from func import config_data class SSL_Transport(xmlrpclib.Transport): @@ -35,10 +37,15 @@ class SSLXMLRPCServerProxy(xmlrpclib.ServerProxy): class FuncServer(SSLXMLRPCServerProxy): - def __init__(self, uri): - self.pem = "/etc/pki/func/slave.pem" - self.crt = "/etc/pki/func/slave.cert" - self.ca = "/etc/pki/func/ca/funcmaster.crt" + def __init__(self, uri, pem=None, crt=None, ca=None): + + config_obj = config_data.Config() + self.config = config_obj.get() + + hn = socket.getfqdn() + self.key = "%s/%s.pem" % (self.config['cert_dir'], hn) + self.cert = "%s/%s.cert" % (self.config['cert_dir'], hn) + self.ca = "%s/ca.cert" % self.config['cert_dir'] SSLXMLRPCServerProxy.__init__(self, uri, self.pem, -- cgit