Revision: 1471 http://trac.macosforge.org/projects/calendarserver/changeset/1471 Author: dreid@apple.com Date: 2007-04-11 10:48:12 -0700 (Wed, 11 Apr 2007) Log Message: ----------- twistedcaldav.pdmonster.PDClientAddressWrapper mutates all incoming requests to point to have the correct remoteAddr as determined by the Pydirector load balancer listening on a unix socket config.PythonDirector['ControlSocket']. This lets us get the proper remoteAddr for the log files and for digest auth. Modified Paths: -------------- CalendarServer/trunk/conf/caldavd-test.plist CalendarServer/trunk/twistedcaldav/cluster.py CalendarServer/trunk/twistedcaldav/config.py CalendarServer/trunk/twistedcaldav/tap.py Added Paths: ----------- CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdamp.patch CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdconf.patch CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdmain.patch CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdnetworktwisted.patch CalendarServer/trunk/twistedcaldav/pdmonster.py Modified: CalendarServer/trunk/conf/caldavd-test.plist =================================================================== --- CalendarServer/trunk/conf/caldavd-test.plist 2007-04-11 03:48:11 UTC (rev 1470) +++ CalendarServer/trunk/conf/caldavd-test.plist 2007-04-11 17:48:12 UTC (rev 1471) @@ -349,6 +349,9 @@ <key>ConfigFile</key> <string>conf/pydir.xml</string> + + <key>ControlSocket</key> + <string>logs/caldavd-pydir.sock</string> </dict> <key>ControlSocket</key> Added: CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdamp.patch =================================================================== --- CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdamp.patch (rev 0) +++ CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdamp.patch 2007-04-11 17:48:12 UTC (rev 1471) @@ -0,0 +1,29 @@ +--- pydirector/pdamp.py 1969-12-31 16:00:00.000000000 -0800 ++++ pydirector/pdamp.py 2007-04-10 17:25:22.000000000 -0700 +@@ -0,0 +1,26 @@ ++from twisted.internet import protocol ++from twisted.protocols import amp ++ ++class GetClientAddress(amp.Command): ++ arguments = [('host', amp.String()), ++ ('port', amp.Integer())] ++ ++ response = [('host', amp.String()), ++ ('port', amp.Integer())] ++ ++ ++class PDControlProtocol(amp.AMP): ++ def __init__(self, director): ++ self.director = director ++ ++ def getClientAddress(self, host, port): ++ host, port = self.director.getClientAddress(host, port) ++ return {'host': host, 'port': port} ++ GetClientAddress.responder(getClientAddress) ++ ++class PDControlFactory(protocol.ServerFactory): ++ def __init__(self, director): ++ self.director = director ++ ++ def buildProtocol(self, addr): ++ return PDControlProtocol(self.director) Added: CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdconf.patch =================================================================== --- CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdconf.patch (rev 0) +++ CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdconf.patch 2007-04-11 17:48:12 UTC (rev 1471) @@ -0,0 +1,30 @@ +--- pydirector/pdconf.py 2004-12-14 05:31:39.000000000 -0800 ++++ pydirector/pdconf.py 2007-04-10 17:25:31.000000000 -0700 +@@ -174,7 +174,7 @@ + + + class PDConfig(object): +- __slots__ = [ 'services', 'admin', 'dom' ] ++ __slots__ = [ 'services', 'admin', 'dom', 'socket' ] + + def __init__(self, filename=None, xml=None): + import pdlogging +@@ -186,7 +186,8 @@ + dom.nodeName) + for item in dom.childNodes: + if item.nodeName in ("#text", "#comment"): continue +- if item.nodeName not in ( u'service', u'admin', u'logging' ): ++ if item.nodeName not in ( u'service', u'admin', ++ u'logging', u'control' ): + raise ConfigError, \ + "expected 'service' or 'admin', got '%s'"%item.nodeName + if item.nodeName == u'service': +@@ -198,6 +199,8 @@ + raise ConfigError, "only one 'admin' block allowed" + elif item.nodeName == u'logging': + pdlogging.initlog(item.getAttribute('file')) ++ elif item.nodeName == u'control': ++ self.socket = item.getAttribute('socket') + + def _loadDOM(self, filename, xml): + from xml.dom.minidom import parseString Added: CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdmain.patch =================================================================== --- CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdmain.patch (rev 0) +++ CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdmain.patch 2007-04-11 17:48:12 UTC (rev 1471) @@ -0,0 +1,42 @@ +--- pydirector/pdmain.py 2004-12-14 05:31:39.000000000 -0800 ++++ pydirector/pdmain.py 2007-04-10 17:25:12.000000000 -0700 +@@ -9,6 +9,9 @@ + if sys.version_info < (2,2): + class object: pass + ++from twisted.internet import reactor ++from pydirector import pdamp ++ + class PythonDirector(object): + + def __init__(self, config): +@@ -17,8 +20,17 @@ + self.schedulers = {} + self.manager = None + self.conf = pdconf.PDConfig(config) ++ self._connections = {} + self.createManager() + self.createListeners() ++ reactor.listenUNIX(self.conf.socket, ++ pdamp.PDControlFactory(self)) ++ ++ def getClientAddress(self, host, port): ++ return self._connections[(host, port)] ++ ++ def setClientAddress(self, host, peer): ++ self._connections[host] = peer + + def start(self, profile=0): + import sys +@@ -69,8 +81,9 @@ + self.listeners[service.name] = [] + for lobj in service.listen: + l = pdnetwork.Listener(service.name, +- pdconf.splitHostPort(lobj), +- scheduler) ++ pdconf.splitHostPort(lobj), ++ scheduler, ++ self) + self.listeners[service.name].append(l) + + def enableGroup(self, serviceName, groupName): Added: CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdnetworktwisted.patch =================================================================== --- CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdnetworktwisted.patch (rev 0) +++ CalendarServer/trunk/lib-patches/PyDirector/pydirector.pdnetworktwisted.patch 2007-04-11 17:48:12 UTC (rev 1471) @@ -0,0 +1,46 @@ +--- pydirector/pdnetworktwisted.py 2004-12-14 05:31:39.000000000 -0800 ++++ pydirector/pdnetworktwisted.py 2007-04-10 17:25:44.000000000 -0700 +@@ -28,10 +28,13 @@ + attribute .listening_address: read - a tuple of (host,port) + """ + +- def __init__(self, name, (bindhost, bindport), scheduler): ++ def __init__(self, name, (bindhost, bindport), scheduler, director): + self.name = name + self.listening_address = (bindhost, bindport) +- self.rfactory = ReceiverFactory((bindhost,bindport), scheduler) ++ self.director = director ++ self.rfactory = ReceiverFactory((bindhost,bindport), ++ scheduler, ++ self.director) + self.setScheduler(scheduler) + reactor.listenTCP(bindport, self.rfactory, interface=bindhost) + +@@ -79,6 +82,14 @@ + it's ok to send any buffered data from the client. + """ + #print "client connection",self.factory ++ #XXX: OMG THIS IS HORRIBLE ++ inSrc = self.receiver.transport.getPeer() ++ outSrc = self.transport.getHost() ++ ++ self.receiver.factory.director.setClientAddress( ++ (outSrc.host, outSrc.port), ++ (inSrc.host, inSrc.port)) ++ + if self.receiver.receiverOk: + self.receiver.setSender(self) + else: +@@ -197,10 +208,11 @@ + protocol = Receiver + noisy = 0 + +- def __init__(self, (bindhost, bindport), scheduler): ++ def __init__(self, (bindhost, bindport), scheduler, director): + self.bindhost = bindhost + self.bindport = bindport + self.scheduler = scheduler ++ self.director = director + + def setScheduler(self, scheduler): + self.scheduler = scheduler Modified: CalendarServer/trunk/twistedcaldav/cluster.py =================================================================== --- CalendarServer/trunk/twistedcaldav/cluster.py 2007-04-11 03:48:11 UTC (rev 1470) +++ CalendarServer/trunk/twistedcaldav/cluster.py 2007-04-11 17:48:12 UTC (rev 1471) @@ -44,6 +44,7 @@ configTemplate = """ <pdconfig> %(services)s + <control socket="%(controlSocket)s" /> </pdconfig> """ @@ -235,6 +236,7 @@ pdconfig = configTemplate % { 'services': '\n'.join(services), + 'controlSocket': config.PythonDirector["ControlSocket"], } fd, fname = tempfile.mkstemp(prefix='pydir') Modified: CalendarServer/trunk/twistedcaldav/config.py =================================================================== --- CalendarServer/trunk/twistedcaldav/config.py 2007-04-11 03:48:11 UTC (rev 1470) +++ CalendarServer/trunk/twistedcaldav/config.py 2007-04-11 17:48:12 UTC (rev 1471) @@ -163,6 +163,7 @@ "PythonDirector": { "pydir": "/usr/share/caldavd/bin/pydir.py", "ConfigFile": "/etc/caldavd/pydir.xml", + "ControlSocket": "/var/run/caldavd-pydir.sock", }, # Umask Added: CalendarServer/trunk/twistedcaldav/pdmonster.py =================================================================== --- CalendarServer/trunk/twistedcaldav/pdmonster.py (rev 0) +++ CalendarServer/trunk/twistedcaldav/pdmonster.py 2007-04-11 17:48:12 UTC (rev 1471) @@ -0,0 +1,47 @@ +from twisted.internet import protocol +from twisted.internet import address +from twisted.protocols import amp + +from twisted.web2.resource import WrapperResource + +from twistedcaldav import logging + +class PDClientAddressWrapper(WrapperResource): + def __init__(self, resource, socket): + super(PDClientAddressWrapper, self).__init__(resource) + + self.socket = socket + self.client = None + self.protocol = None + + def hook(self, request): + from twisted.internet import reactor + + def _gotProtocol(proto): + self.protocol = proto + + return self.hook(request) + + def _gotAddress(result): + logging.debug('result = %r' % (result,)) + request.remoteAddr = address.IPv4Address( + 'TCP', + result['host'], + int(result['port'])) + + if self.protocol is not None: + host, port = request.remoteAddr.host, request.remoteAddr.port + logging.debug("GetClientAddress(host=%r, port=%r)" % (host, port)) + d = self.protocol.callRemoteString("GetClientAddress", + host=host, + port=str(port)) + d.addCallback(_gotAddress) + return d + + else: + self.client = protocol.ClientCreator(reactor, amp.AMP) + + d = self.client.connectUNIX(self.socket) + d.addCallback(_gotProtocol) + + return d Modified: CalendarServer/trunk/twistedcaldav/tap.py =================================================================== --- CalendarServer/trunk/twistedcaldav/tap.py 2007-04-11 03:48:11 UTC (rev 1470) +++ CalendarServer/trunk/twistedcaldav/tap.py 2007-04-11 17:48:12 UTC (rev 1471) @@ -63,6 +63,8 @@ from twistedcaldav.static import CalendarHomeProvisioningFile +from twistedcaldav import pdmonster + try: from twistedcaldav.authkerb import NegotiateCredentialFactory except ImportError: @@ -446,7 +448,7 @@ (auth.IPrincipal,) ) - site = Site(LogWrapperResource(authWrapper)) + logWrapper = LogWrapperResource(authWrapper) # # Configure the service @@ -454,13 +456,17 @@ log.msg("Setting up service") - channel = http.HTTPFactory(site) + if config.ProcessType == 'Slave': + realRoot = pdmonster.PDClientAddressWrapper( + logWrapper, + config.PythonDirector['ControlSocket']) - if config.ProcessType == 'Slave': logObserver = logging.AMPCommonAccessLoggingObserver( config.ControlSocket) elif config.ProcessType == 'Single': + realRoot = logWrapper + logObserver = logging.RotatingFileAccessLoggingObserver( config.AccessLogFile) @@ -469,6 +475,10 @@ service = CalDAVService(logObserver) + site = Site(realRoot) + + channel = http.HTTPFactory(site) + if not config.BindAddresses: config.BindAddresses = [""]
participants (1)
-
source_changes@macosforge.org