The Evolution of Finger: configuration and packaging of the finger service

  1. Introduction
  2. Plugins
  3. OS Integration

Introduction

This is the eleventh part of the Twisted tutorial Twisted from Scratch, or The Evolution of Finger.

In this part, we make it easier for non-programmers to configure a finger server and show how to package it in the .deb and RPM package formats. Plugins are discussed further in the Twisted Plugin System howto. Writing twistd plugins is covered in Writing a twistd Plugin, and .tac applications are covered in Using the Twisted Application Framework.

Plugins

So far, the user had to be somewhat of a programmer to be able to configure stuff. Maybe we can eliminate even that? Move old code to finger/__init__.py and...

Full source code for finger module here:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

# finger.py module from zope.interface import Interface, implements from twisted.application import internet, service from twisted.internet import protocol, reactor, defer from twisted.words.protocols import irc from twisted.protocols import basic from twisted.python import components, log from twisted.web import resource, server, xmlrpc from twisted.spread import pb from OpenSSL import SSL class IFingerService(Interface): def getUser(user): """ Return a deferred returning a string. """ def getUsers(): """ Return a deferred returning a list of strings. """ class IFingerSetterService(Interface): def setUser(user, status): """ Set the user's status to something. """ def catchError(err): return "Internal error in server" class FingerProtocol(basic.LineReceiver): def lineReceived(self, user): d = self.factory.getUser(user) d.addErrback(catchError) def writeValue(value): self.transport.write(value+'\n') self.transport.loseConnection() d.addCallback(writeValue) class IFingerFactory(Interface): def getUser(user): """ Return a deferred returning a string. """ def buildProtocol(addr): """ Return a protocol returning a string. """ class FingerFactoryFromService(protocol.ServerFactory): implements(IFingerFactory) protocol = FingerProtocol def __init__(self, service): self.service = service def getUser(self, user): return self.service.getUser(user) components.registerAdapter(FingerFactoryFromService, IFingerService, IFingerFactory) class FingerSetterProtocol(basic.LineReceiver): def connectionMade(self): self.lines = [] def lineReceived(self, line): self.lines.append(line) def connectionLost(self, reason): if len(self.lines) == 2: self.factory.setUser(*self.lines) class IFingerSetterFactory(Interface): def setUser(user, status): """ Return a deferred returning a string. """ def buildProtocol(addr): """ Return a protocol returning a string. """ class FingerSetterFactoryFromService(protocol.ServerFactory): implements(IFingerSetterFactory) protocol = FingerSetterProtocol def __init__(self, service): self.service = service def setUser(self, user, status): self.service.setUser(user, status) components.registerAdapter(FingerSetterFactoryFromService, IFingerSetterService, IFingerSetterFactory) class IRCReplyBot(irc.IRCClient): def connectionMade(self): self.nickname = self.factory.nickname irc.IRCClient.connectionMade(self) def privmsg(self, user, channel, msg): user = user.split('!')[0] if self.nickname.lower() == channel.lower(): d = self.factory.getUser(msg) d.addErrback(catchError) d.addCallback(lambda m: "Status of %s: %s" % (msg, m)) d.addCallback(lambda m: self.msg(user, m)) class IIRCClientFactory(Interface): """ @ivar nickname """ def getUser(user): """ Return a deferred returning a string. """ def buildProtocol(addr): """ Return a protocol. """ class IRCClientFactoryFromService(protocol.ClientFactory): implements(IIRCClientFactory) protocol = IRCReplyBot nickname = None def __init__(self, service): self.service = service def getUser(self, user): return self.service.getUser(user) components.registerAdapter(IRCClientFactoryFromService, IFingerService, IIRCClientFactory) class UserStatusTree(resource.Resource): template = """<html><head><title>Users</title></head><body> <h1>Users</h1> <ul> %(users)s </ul> </body> </html>""" def __init__(self, service): resource.Resource.__init__(self) self.service = service def getChild(self, path, request): if path == '': return self elif path == 'RPC2': return UserStatusXR(self.service) else: return UserStatus(path, self.service) def render_GET(self, request): users = self.service.getUsers() def cbUsers(users): request.write(self.template % {'users': ''.join([ # Name should be quoted properly these uses. '<li><a href="%s">%s</a></li>' % (name, name) for name in users])}) request.finish() users.addCallback(cbUsers) def ebUsers(err): log.err(err, "UserStatusTree failed") request.finish() users.addErrback(ebUsers) return server.NOT_DONE_YET components.registerAdapter(UserStatusTree, IFingerService, resource.IResource) class UserStatus(resource.Resource): template='''<html><head><title>%(title)s</title></head> <body><h1>%(name)s</h1><p>%(status)s</p></body></html>''' def __init__(self, user, service): resource.Resource.__init__(self) self.user = user self.service = service def render_GET(self, request): status = self.service.getUser(self.user) def cbStatus(status): request.write(self.template % { 'title': self.user, 'name': self.user, 'status': status}) request.finish() status.addCallback(cbStatus) def ebStatus(err): log.err(err, "UserStatus failed") request.finish() status.addErrback(ebStatus) return server.NOT_DONE_YET class UserStatusXR(xmlrpc.XMLRPC): def __init__(self, service): xmlrpc.XMLRPC.__init__(self) self.service = service def xmlrpc_getUser(self, user): return self.service.getUser(user) def xmlrpc_getUsers(self): return self.service.getUsers() class IPerspectiveFinger(Interface): def remote_getUser(username): """ Return a user's status. """ def remote_getUsers(): """ Return a user's status. """ class PerspectiveFingerFromService(pb.Root): implements(IPerspectiveFinger) def __init__(self, service): self.service = service def remote_getUser(self, username): return self.service.getUser(username) def remote_getUsers(self): return self.service.getUsers() components.registerAdapter(PerspectiveFingerFromService, IFingerService, IPerspectiveFinger) class FingerService(service.Service): implements(IFingerService) def __init__(self, filename): self.filename = filename def _read(self): self.users = {} for line in file(self.filename): user, status = line.split(':', 1) user = user.strip() status = status.strip() self.users[user] = status self.call = reactor.callLater(30, self._read) def getUser(self, user): return defer.succeed(self.users.get(user, "No such user")) def getUsers(self): return defer.succeed(self.users.keys()) def startService(self): self._read() service.Service.startService(self) def stopService(self): service.Service.stopService(self) self.call.cancel() class ServerContextFactory: def getContext(self): """ Create an SSL context. This is a sample implementation that loads a certificate from a file called 'server.pem'. """ ctx = SSL.Context(SSL.SSLv23_METHOD) ctx.use_certificate_file('server.pem') ctx.use_privatekey_file('server.pem') return ctx # Easy configuration def makeService(config): # finger on port 79 s = service.MultiService() f = FingerService(config['file']) h = internet.TCPServer(1079, IFingerFactory(f)) h.setServiceParent(s) # website on port 8000 r = resource.IResource(f) r.templateDirectory = config['templates'] site = server.Site(r) j = internet.TCPServer(8000, site) j.setServiceParent(s) # ssl on port 443 # if config.get('ssl'): # k = internet.SSLServer(443, site, ServerContextFactory()) # k.setServiceParent(s) # irc fingerbot if 'ircnick' in config: i = IIRCClientFactory(f) i.nickname = config['ircnick'] ircserver = config['ircserver'] b = internet.TCPClient(ircserver, 6667, i) b.setServiceParent(s) # Pespective Broker on port 8889 if 'pbport' in config: m = internet.TCPServer( int(config['pbport']), pb.PBServerFactory(IPerspectiveFinger(f))) m.setServiceParent(s) return s

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

# finger/tap.py from twisted.application import internet, service from twisted.internet import interfaces from twisted.python import usage import finger class Options(usage.Options): optParameters = [ ['file', 'f', '/etc/users'], ['templates', 't', '/usr/share/finger/templates'], ['ircnick', 'n', 'fingerbot'], ['ircserver', None, 'irc.freenode.net'], ['pbport', 'p', 8889], ] optFlags = [['ssl', 's']] def makeService(config): return finger.makeService(config)

And register it all:

1 2 3 4 5

from twisted.application.service import ServiceMaker finger = ServiceMaker( 'finger', 'finger.tap', 'Run a finger service', 'finger')
twisted/plugins/finger_tutorial.py - listings/finger/twisted/plugins/finger_tutorial.py

Note that the second argument to ServiceMaker, finger.tap, is a reference to a module (finger/tap.py), not to a filename.

And now, the following works

% sudo twistd -n finger --file=/etc/users --ircnick=fingerbot

For more details about this, see the twistd plugin documentation.

OS Integration

If we already have the finger package installed in PYTHONPATH (e.g. we added it to site-packages), we can achieve easy integration:

Debian

% tap2deb --unsigned -m "Foo <foo@example.com>" --type=python finger.tac
% sudo dpkg -i .build/*.deb

Red Hat / Mandrake

% tap2rpm --type=python finger.tac
% sudo rpm -i *.rpm

These packages will properly install and register init.d scripts, etc. for the given file.

If it doesn't work on your favorite OS: patches accepted!

Index

Version: 13.2.0