Wednesday, February 08, 2006

The mdns patch for Zope

People asked for it. This is about the simplest version of the thing I could come up with but you will need the Avahi and DBUS Python bindings up and running. After that this patch should apply to the Zope 2.8/2.9 releases pretty easily.

Then you just restart Zope and you should see these services on your network.


diff -ur Zope-2.8.5-final/lib/python/ZServer/FTPServer.py Zope-2.8.5-mdns/lib/python/ZServer/FTPServer.py
--- Zope-2.8.5-final/lib/python/ZServer/FTPServer.py 2005-12-18 23:25:56.000000000 -0800
+++ Zope-2.8.5-mdns/lib/python/ZServer/FTPServer.py 2006-02-08 12:46:15.000000000 -0800
@@ -80,6 +80,7 @@
import marshal
import stat
import time
+from utils import mdns_announce


class zope_ftp_channel(ftp_channel):
@@ -640,6 +641,10 @@
self.hostname,
self.port
))
+ mdns_announce(None,
+ self.port,
+ service="_ftp._tcp")
+

def clean_shutdown_control(self,phase,time_in_this_phase):
if phase==2:
diff -ur Zope-2.8.5-final/lib/python/ZServer/HTTPServer.py Zope-2.8.5-mdns/lib/python/ZServer/HTTPServer.py
--- Zope-2.8.5-final/lib/python/ZServer/HTTPServer.py 2005-12-18 23:25:56.000000000 -0800
+++ Zope-2.8.5-mdns/lib/python/ZServer/HTTPServer.py 2006-02-08 12:47:46.000000000 -0800
@@ -58,6 +58,7 @@
from zLOG import LOG, register_subsystem, BLATHER, INFO, WARNING, ERROR
import DebugLogger
from medusa import logger
+from utils import mdns_announce

register_subsystem('ZServer HTTPServer')

@@ -384,6 +385,7 @@
server_protocol = 'HTTP'
channel_class = zhttp_channel
shutup=0
+ service = "_http._tcp"

def __init__ (self, ip, port, resolver=None, logger_object=None):
self.shutup=1
@@ -397,6 +399,9 @@
self.server_port
))

+ self.mdns = mdns_announce(None, port, service = self.service, info=self.SERVER_IDENT)
+
+
def clean_shutdown_control(self,phase,time_in_this_phase):
if phase==2:
self.log_info('closing %s to new connections' % self.server_protocol)
@@ -422,3 +427,4 @@

class zwebdav_server(zhttp_server):
server_protocol = 'WebDAV'
+ service = "_webdav._tcp"
diff -ur Zope-2.8.5-final/lib/python/ZServer/utils.py Zope-2.8.5-mdns/lib/python/ZServer/utils.py
--- Zope-2.8.5-final/lib/python/ZServer/utils.py 2005-12-18 23:25:56.000000000 -0800
+++ Zope-2.8.5-mdns/lib/python/ZServer/utils.py 2006-02-08 12:48:02.000000000 -0800
@@ -56,3 +56,39 @@
from medusa import logger
# override the service name in logger.syslog_logger
logger.syslog_logger.svc_name='ZServer'
+try:
+ import avahi, dbus, dbus.glib
+ hostname = None
+ def mdns_announce(name, port, service="_http._tcp",
+ **kwargs):
+ global hostname
+ bus = dbus.SystemBus()
+ server = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
+ avahi.DBUS_PATH_SERVER),
+ avahi.DBUS_INTERFACE_SERVER)
+
+ g = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
+ server.EntryGroupNew()),
+ avahi.DBUS_INTERFACE_ENTRY_GROUP)
+
+
+ if name is None:
+ if hostname is None:
+ hostname = "%s:%s" % (server.GetHostName(), port)
+ name = hostname
+
+ info = ["%s=%s" % (k,v) for k,v in kwargs.items()]
+ g.AddService(avahi.IF_UNSPEC,
+ avahi.PROTO_UNSPEC, 0,
+ name,
+ service,
+ "", "", # domain, host (let the system figure it out)
+ dbus.UInt16(port),
+ info,
+ )
+ g.Commit()
+ return g
+
+except ImportError:
+ def mdns_announce(server, name, port): pass
+

2 comments:

Sidnei da Silva said...

There is a place where you could put those calls to mdns_announce, instead of spreading code in three different places just add it to:

Zope2/Startup/__init__.py:setupServers()

Would give a even smaller patch, IMHO.

Benjamin Saller said...

Sidnei,


Cool, I hadn't been in there, didn't know about that. Its certainly an option. I think it would still require some mapping between the server type and the way it gets published though, _http._tcp, _ftp._tcp, etc and thats not obvious from the port. I also thing some servers like ICP wouldn't be advertised, though one could make a case for a squid server that discovered cluster nodes using zeroconf. Who knows...

-Ben

Followers