3 ### Administration connection with tripe server
5 ### (c) 2006 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of Trivial IP Encryption (TrIPE).
12 ### TrIPE is free software: you can redistribute it and/or modify it under
13 ### the terms of the GNU General Public License as published by the Free
14 ### Software Foundation; either version 3 of the License, or (at your
15 ### option) any later version.
17 ### TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 ### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 ### FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 ### You should have received a copy of the GNU General Public License
23 ### along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 This module provides classes and functions for connecting to a running tripe
27 server, sending it commands, receiving and processing replies, and
28 implementing services.
30 Rather than end up in lost in a storm of little event-driven classes, or a
31 morass of concurrent threads, the module uses coroutines to present a fairly
32 simple function call/return interface to potentially long-running commands
33 which must run without blocking the main process. It assumes a coroutine
34 module presenting a subset of the `greenlet' interface: if actual greenlets
35 are available, they are used; otherwise there's an implementation in terms of
36 threads (with lots of locking) which will do instead.
38 The simple rule governing the coroutines used here is this:
40 * The root coroutine never cares what values are passed to it when it
41 resumes: it just discards them.
43 * Other, non-root, coroutines are presumed to be waiting for some specific
46 Configuration variables:
54 Other useful variables:
79 TripeSynchronousCommand
80 TripeAsynchronousCommand
83 TripeCommandDispatcher
96 __pychecker__ = 'self=me no-constCond no-argsused'
100 ###--------------------------------------------------------------------------
101 ### External dependencies.
111 if OS.getenv('TRIPE_FORCE_RMCR') is not None:
113 from py.magic import greenlet as _Coroutine
115 from rmcr import Coroutine as _Coroutine
117 ###--------------------------------------------------------------------------
118 ### Coroutine hacking.
120 rootcr = _Coroutine.getcurrent()
122 class Coroutine (_Coroutine):
124 A coroutine class which can only be invoked by the root coroutine.
126 The root, by construction, cannot be an instance of this class.
128 def switch(me, *args, **kw):
129 assert _Coroutine.getcurrent() is rootcr
130 if _debug: print '* %s' % me
131 _Coroutine.switch(me, *args, **kw)
132 if _debug: print '* %s' % rootcr
134 ###--------------------------------------------------------------------------
135 ### Default places for things.
137 configdir = OS.environ.get('TRIPEDIR', "@configdir@")
138 socketdir = "@socketdir@"
139 PACKAGE = "@PACKAGE@"
140 VERSION = "@VERSION@"
142 tripesock = OS.environ.get('TRIPESOCK', OS.path.join(socketdir, 'tripesock'))
143 peerdb = OS.environ.get('TRIPEPEERDB', 'peers.cdb')
145 ###--------------------------------------------------------------------------
146 ### Connection to the server.
148 def readnonblockingly(sock, len):
150 Nonblocking read from SOCK.
152 Try to return LEN bytes. If couldn't read anything, return `None'. EOF is
153 returned as an empty string.
157 return sock.recv(len)
159 if exc[0] == E.EWOULDBLOCK:
163 class TripeConnectionError (StandardError):
164 """Something happened to the connection with the server."""
166 class TripeInternalError (StandardError):
167 """This program is very confused."""
170 class TripeConnection (object):
172 A logical connection to the tripe administration socket.
174 There may or may not be a physical connection. (This is needed for the
175 monitor, for example.)
177 This class isn't very useful on its own, but it has useful subclasses. At
178 this level, the class is agnostic about I/O multiplexing schemes; that gets
182 def __init__(me, socket):
184 Make a connection to the named SOCKET.
186 No physical connection is made initially.
191 me.iowatch = SelIOWatcher(me)
195 Ensure that there's a physical connection.
197 Do nothing if we're already connected. Invoke the `connected' method if
201 sock = S.socket(S.AF_UNIX, S.SOCK_STREAM)
202 sock.connect(me.socket)
204 me.lbuf = M.LineBuffer(me.line, me._eof)
209 def disconnect(me, reason):
211 Disconnect the physical connection.
213 Invoke the `disconnected' method, giving the provided REASON, which
214 should be either `None' or an exception.
216 if not me.sock: return
217 me.disconnected(reason)
226 Return true if there's a current, believed-good physical connection.
228 return me.sock is not None
230 __nonzero__ = connectedp
234 Send the LINE to the connection's socket.
236 All output is done through this method; it can be overridden to provide
237 proper nonblocking writing, though this seems generally unnecessary.
240 me.sock.setblocking(1)
241 me.sock.send(line + '\n')
242 except Exception, exc:
249 Receive whatever's ready from the connection's socket.
251 Call `line' on each complete line, and `eof' if the connection closed.
252 Subclasses which attach this class to an I/O-event system should call
253 this method when the socket (the `sock' attribute) is ready for reading.
255 while me.sock is not None:
257 buf = readnonblockingly(me.sock, 16384)
258 except Exception, exc:
270 """Internal end-of-file handler."""
271 me.disconnect(TripeConnectionError('connection lost'))
276 To be overridden by subclasses to react to a connection being
279 me.iowatch.connected(me.sock)
281 def disconnected(me, reason):
283 To be overridden by subclasses to react to a connection being severed.
285 me.iowatch.disconnected()
288 """To be overridden by subclasses to handle end-of-file."""
292 """To be overridden by subclasses to handle incoming lines."""
295 ###--------------------------------------------------------------------------
296 ### I/O loop integration.
298 class SelIOWatcher (object):
300 Integration with mLib's I/O event system.
302 You can replace this object with a different one for integration with,
303 e.g., glib's main loop, by setting `CONN.iowatcher' to a different object
304 while the CONN is disconnected.
307 def __init__(me, conn):
311 def connected(me, sock):
313 Called when a connection is made.
315 SOCK is the socket. The watcher must arrange to call `CONN.receive' when
318 me._selfile = M.SelFile(sock.fileno(), M.SEL_READ, me._conn.receive)
321 def disconnected(me):
323 Called when the connection is lost.
329 Wait for something interesting to happen, and issue events.
331 That is, basically, do one iteration of a main select loop, processing
332 all of the events, and then return. This is used in the method
333 `TripeCommandDispatcher.mainloop', but that's mostly for the benefit of
334 `runservices'; if your I/O watcher has a different main loop, you can
339 ###--------------------------------------------------------------------------
340 ### Inter-coroutine communication.
342 class Queue (object):
344 A queue of things arriving asynchronously.
346 This is a very simple single-reader multiple-writer queue. It's useful for
347 more complex coroutines which need to cope with a variety of possible
352 """Create a new empty queue."""
353 me.contents = M.Array()
358 Internal: wait for an item to arrive in the queue.
360 Complain if someone is already waiting, because this is just a
364 raise ValueError('queue already being waited on')
366 me.waiter = Coroutine.getcurrent()
367 while not me.contents:
368 me.waiter.parent.switch()
374 Remove and return the item at the head of the queue.
376 If the queue is empty, wait until an item arrives.
379 return me.contents.shift()
383 Return the item at the head of the queue without removing it.
385 If the queue is empty, wait until an item arrives.
388 return me.contents[0]
392 Write THING to the queue.
394 If someone is waiting on the queue, wake him up immediately; otherwise
395 just leave the item there for later.
397 me.contents.push(thing)
401 ###--------------------------------------------------------------------------
402 ### Dispatching coroutine.
404 ## Match a string if it can stand on its own as a bareword: i.e., it doesn't
405 ## contain backslashes, quotes or whitespace.
406 rx_ordinary = RX.compile(r'^[^\\\'\"\s]+$')
408 ## Match characters which need to be escaped, even in quoted text.
409 rx_weird = RX.compile(r'([\\\'])')
412 """Quote S according to the tripe-admin(5) rules."""
413 m = rx_ordinary.match(s)
414 if m and m.end() == len(s):
417 return "'" + rx_weird.sub(r'\\\1', s) + "'"
421 Return a wrapper for FUNC which reports exceptions thrown by it.
423 Useful in the case of callbacks invoked by C functions which ignore
428 return func(*a, **kw)
430 SYS.excepthook(*SYS.exc_info())
434 class TripeCommand (object):
436 This abstract class represents a command in progress.
438 The `words' attribute contains the list of tokens which make up the
441 Subclasses must implement a method to handle server responses:
443 * response(CODE, *ARGS): CODE is one of the strings `OK', `INFO' or
444 `FAIL'; ARGS are the remaining tokens from the server's response.
447 def __init__(me, words):
448 """Make a new command consisting of the given list of WORDS."""
451 raise TripeInternalError("command word contains newline")
454 class TripeSynchronousCommand (TripeCommand):
456 A simple command, processed apparently synchronously.
458 Must be invoked from a coroutine other than the root (or whichever one is
459 running the dispatcher); in reality, other coroutines carry on running
460 while we wait for a response from the server.
462 Each server response causes the calling coroutine to be resumed with the
463 pair (CODE, REST) -- where CODE is the server's response code (`OK', `INFO'
464 or `FAIL') and REST is a list of the server's other response tokens. The
465 calling coroutine must continue switching back to the dispatcher until a
466 terminating response (`OK' or `FAIL') is received or become very
469 Mostly it's better to use the `TripeCommandIterator' to do this
473 def __init__(me, words):
474 """Initialize the command, specifying the WORDS to send to the server."""
475 TripeCommand.__init__(me, words)
476 me.owner = Coroutine.getcurrent()
478 def response(me, code, *rest):
479 """Handle a server response by forwarding it to the calling coroutine."""
480 me.owner.switch((code, rest))
482 class TripeError (StandardError):
484 A tripe command failed with an error (a `FAIL' code). The args attribute
485 contains a list of the server's message tokens.
489 class TripeCommandIterator (object):
491 Iterator interface to a tripe command.
493 The values returned by the iterator are lists of tokens from the server's
494 `INFO' lines, as processed by the given filter function, if any. The
495 iterator completes normally (by raising `StopIteration') if the server
496 reported `OK', and raises an exception if the command failed for some reason.
498 A `TripeError' is raised if the server issues a `FAIL' code. If the
499 connection failed, some other exception is raised.
502 def __init__(me, dispatcher, words, bg = False, filter = None):
504 Create a new command iterator.
506 The command is submitted to the DISPATCHER; it consists of the given
507 WORDS. If BG is true, then an option is inserted to request that the
508 server run the command in the background. The FILTER is applied to the
509 token lists which the server responds, and the filter's output are the
510 items returned by the iterator.
512 me.dcr = Coroutine.getcurrent().parent
514 raise ValueError('must invoke from coroutine')
515 me.filter = filter or (lambda x: x)
517 words = [words[0], '-background', dispatcher.bgtag()] + list(words[1:])
518 dispatcher.rawcommand(TripeSynchronousCommand(words))
521 """Iterator protocol: I am my own iterator."""
526 Iterator protocol: return the next piece of information from the server.
528 `INFO' responses are filtered and returned as the values of the
529 iteration. `FAIL' and `CONNERR' responses are turned into exceptions and
530 raised. Finally, `OK' is turned into `StopIteration', which should cause
531 a normal end to the iteration process.
533 thing = me.dcr.switch()
536 return me.filter(rest)
538 raise StopIteration()
539 elif code == 'CONNERR':
541 raise TripeConnectionError('connection terminated by user')
545 raise TripeError(*rest)
547 raise TripeInternalError('unexpected tripe response %r' %
550 ### Simple utility functions for the TripeCommandIterator convenience
553 def _tokenjoin(words):
554 """Filter function: simply join the given tokens with spaces between."""
555 return ' '.join(words)
558 """Return a dictionary formed from the `KEY=VALUE' pairs returned by the
564 kv[w[:q]] = w[q + 1:]
568 """Raise an error if ITER contains any item."""
571 raise TripeInternalError('expected no response')
575 """If ITER contains a single item, return it; otherwise raise an error."""
578 raise TripeInternalError('expected only one line of response')
581 def _tracelike(iter):
582 """Handle a TRACE-like command. The result is a list of tuples (CHAR,
583 STATUS, DESC): CHAR is a selector character, STATUS is the status (empty if
584 disabled, `+' if enabled, maybe something else later), and DESC is the
585 human-readable description."""
590 desc = ' '.join(ww[1:])
591 stuff.append((ch, st, desc))
594 def _kwopts(kw, allowed):
595 """Parse keyword arguments into options. ALLOWED is a list of allowable
596 keywords; raise errors if other keywords are present. `KEY = VALUE'
597 becomes an option pair `-KEY VALUE' if VALUE is a string, just the option
598 `-KEY' if VALUE is a true non-string, or nothing if VALUE is false. Insert
599 a `--' at the end to stop the parser getting confused."""
602 for a in allowed: amap[a] = True
603 for k, v in kw.iteritems():
605 raise ValueError('option %s not allowed here' % k)
606 if isinstance(v, str):
615 def defer(func, *args, **kw):
616 """Call FUNC(*ARGS, **KW) later, in the root coroutine."""
617 _deferq.append((func, args, kw))
619 def funargstr(func, args, kw):
620 items = [repr(a) for a in args]
621 for k, v in kw.iteritems():
622 items.append('%s = %r' % (k, v))
623 return '%s(%s)' % (func.__name__, ', '.join(items))
625 def spawn(func, *args, **kw):
626 """Call FUNC, passing ARGS and KW, in a fresh coroutine."""
627 defer(lambda: (Coroutine(func, name = funargstr(func, args, kw))
628 .switch(*args, **kw)))
634 Read (FUNC, ARGS, KW) triples from queue and invoke FUNC(*ARGS, **KW).
637 func, args, kw = _asideq.get()
641 SYS.excepthook(*SYS.exc_info())
643 def aside(func, *args, **kw):
644 """Call FUNC(*ARGS, **KW) later, in a non-root coroutine."""
645 defer(_asideq.put, (func, args, kw))
647 class TripeCommandDispatcher (TripeConnection):
651 The command dispatcher is a connection which knows how to handle commands.
652 This is probably the most important class in this module to understand.
654 Lines from the server are parsed into tokens. The first token is a code
655 (`OK' or `NOTE' or something) explaining what kind of line this is. The
656 `handler' attribute is a dictionary mapping server line codes to handler
657 functions, which are applied to the words of the line as individual
658 arguments. *Exception*: the content of `TRACE' lines is not tokenized.
660 There are default handlers for server codes which respond to commands.
661 Commands arrive as `TripeCommand' instances through the `rawcommand'
662 interface. The dispatcher keeps track of which command objects represent
663 which jobs, and sends responses on to the appropriate command objects by
664 invoking their `response' methods. Command objects don't see the `BG...'
665 codes, because the dispatcher has already transformed them into regular
666 codes when it was looking up the job tag.
668 The dispatcher also has a special response code of its own: `CONNERR'
669 indicates that the connection failed and the command has therefore been
670 lost. This is sent to all outstanding commands when a connection error is
671 encountered: rather than a token list, it is accompanied by an exception
672 object which is the cause of the disconnection, which may be `None' if the
673 disconnection is expected (e.g., the direct result of a user request).
676 ## --- Infrastructure ---
678 ## We will get confused if we pipeline commands. Send them one at a time.
679 ## Only send a command when the previous one detaches or completes.
681 ## The following attributes are interesting:
683 ## tagseq Sequence number for next background job (for bgtag)
685 ## queue Commands awaiting submission.
687 ## cmd Mapping from job tags to commands: cmd[None] is the
688 ## foreground command.
690 ## handler Mapping from server codes to handler functions.
692 def __init__(me, socket):
694 Initialize the dispatcher.
696 The SOCKET is the filename of the administration socket to connect to,
697 for TripeConnection.__init__.
699 TripeConnection.__init__(me, socket)
702 me.handler['BGDETACH'] = me._detach
703 for i in 'BGOK', 'BGINFO', 'BGFAIL':
704 me.handler[i] = me._response
705 for i in 'OK', 'INFO', 'FAIL':
706 me.handler[i] = me._fgresponse
709 """Should we quit the main loop? Subclasses should override."""
712 def mainloop(me, quitp = None):
714 Iterate the I/O watcher until QUITP returns true.
716 Arranges for asides and deferred calls to be made at the right times.
720 assert _Coroutine.getcurrent() is rootcr
721 Coroutine(_runasides, name = '_runasides').switch()
728 for func, args, kw in q:
736 If a subclass overrides this method, it must call us; clears out the
737 command queue and job map.
741 TripeConnection.connected(me)
743 def disconnected(me, reason):
747 If a subclass hooks overrides this method, it must call us; sends a
748 special `CONNERR' code to all incomplete commands.
750 TripeConnection.disconnected(me, reason)
751 for cmd in me.cmd.itervalues():
752 cmd.response('CONNERR', reason)
754 cmd.response('CONNERR', reason)
758 """Handle an incoming line, sending it to the right place."""
759 if _debug: print '<', line
760 code, rest = M.word(line, quotep = True)
761 func = me.handler.get(code)
766 func(code, *M.split(rest, quotep = True)[0])
771 Pull the oldest command off the queue and try to send it to the server.
773 if not me.queue or None in me.cmd: return
774 cmd = me.queue.shift()
775 if _debug: print '>', ' '.join([quotify(w) for w in cmd.words])
776 me.send(' '.join([quotify(w) for w in cmd.words]))
781 Return an unused job tag.
783 May be of use when composing commands by hand.
785 tag = 'J%05d' % me.tagseq
789 ## --- Built-in handler functions for server responses ---
791 def _detach(me, _, tag):
793 Respond to a `BGDETACH' TAG message.
795 Move the current foreground command to the background.
797 assert tag not in me.cmd
798 me.cmd[tag] = me.cmd[None]
801 def _response(me, code, tag, *w):
803 Respond to an `OK', `INFO' or `FAIL' message.
805 If this is a message for a background job, find the tag; then dispatch
806 the result to the command object. This is also called by `_fgresponse'
807 (wth TAG set to `None') to handle responses for foreground commands, and
808 is therefore a useful method to extend or override in subclasses.
810 if code.startswith('BG'):
815 cmd.response(code, *w)
817 def _fgresponse(me, code, *w):
818 """Process responses to the foreground command."""
819 me._response(code, None, *w)
821 ## --- Interface methods ---
823 def rawcommand(me, cmd):
825 Submit the `TripeCommand' CMD to the server, and look after it until it
828 if not me.connectedp():
829 raise TripeConnectionError('connection closed')
833 def command(me, *cmd, **kw):
834 """Convenience wrapper for creating a TripeCommandIterator object."""
835 return TripeCommandIterator(me, cmd, **kw)
837 ## --- Convenience methods for server commands ---
839 def add(me, peer, *addr, **kw):
840 return _simple(me.command(bg = True,
842 _kwopts(kw, ['tunnel', 'keepalive',
843 'key', 'priv', 'cork',
849 return _oneline(me.command('ADDR', peer))
850 def algs(me, peer = None):
851 return _keyvals(me.command('ALGS',
852 *((peer is not None and [peer]) or [])))
853 def checkchal(me, chal):
854 return _simple(me.command('CHECKCHAL', chal))
856 return _simple(me.command('DAEMON'))
857 def eping(me, peer, **kw):
858 return _oneline(me.command(bg = True,
860 _kwopts(kw, ['timeout']) +
862 def forcekx(me, peer, **kw):
863 return _simple(me.command(*['FORCEKX'] +
864 _kwopts(kw, ["quiet"]) +
867 return _oneline(me.command('GETCHAL', filter = _tokenjoin))
868 def greet(me, peer, chal):
869 return _simple(me.command('GREET', peer, chal))
871 return list(me.command('HELP', filter = _tokenjoin))
872 def ifname(me, peer):
873 return _oneline(me.command('IFNAME', peer, filter = _tokenjoin))
874 def kill(me, peer, **kw):
875 return _simple(me.command(*['KILL'] +
876 _kwopts(kw, ["quiet"]) +
879 return list(me.command('LIST', filter = _tokenjoin))
880 def notify(me, *msg):
881 return _simple(me.command('NOTIFY', *msg))
882 def peerinfo(me, peer):
883 return _keyvals(me.command('PEERINFO', peer))
884 def ping(me, peer, **kw):
885 return _oneline(me.command(bg = True,
887 _kwopts(kw, ['timeout']) +
889 def port(me, af = None):
890 return _oneline(me.command('PORT',
891 *((af is not None) and [af] or []),
892 filter = _tokenjoin))
894 return _simple(me.command('QUIT'))
896 return _simple(me.command('RELOAD'))
898 return _keyvals(me.command('SERVINFO'))
899 def setifname(me, new):
900 return _simple(me.command('SETIFNAME', new))
901 def svcclaim(me, service, version):
902 return _simple(me.command('SVCCLAIM', service, version))
903 def svcensure(me, service, version = None):
904 return _simple(me.command('SVCENSURE', service,
905 *((version is not None and [version]) or [])))
906 def svcfail(me, job, *msg):
907 return _simple(me.command('SVCFAIL', job, *msg))
908 def svcinfo(me, job, *msg):
909 return _simple(me.command('SVCINFO', job, *msg))
911 return list(me.command('SVCLIST'))
913 return _simple(me.command('SVCOK', job))
914 def svcquery(me, service):
915 return _keyvals(me.command('SVCQUERY', service))
916 def svcrelease(me, service):
917 return _simple(me.command('SVCRELEASE', service))
918 def svcsubmit(me, service, *args, **kw):
919 return me.command(bg = True,
921 _kwopts(kw, ['version']) +
925 return _keyvals(me.command('STATS', peer))
926 def trace(me, *args):
927 return _tracelike(me.command('TRACE', *args))
929 return list(me.command('TUNNELS', filter = _tokenjoin))
931 return _oneline(me.command('VERSION', filter = _tokenjoin))
933 return _simple(me.command('WARN', *msg))
934 def watch(me, *args):
935 return _tracelike(me.command('WATCH', *args))
937 ###--------------------------------------------------------------------------
938 ### Asynchronous commands.
940 class TripeAsynchronousCommand (TripeCommand):
942 Asynchronous commands.
944 This is the complicated way of issuing commands. You must set up a queue,
945 and associate the command with the queue. Responses arriving for the
946 command will be put on the queue as an triple of the form (TAG, CODE, REST)
947 -- where TAG is an object of your choice, not interpreted by this class,
948 CODE is the server's response code (`OK', `INFO', `FAIL', or `CONNERR'),
949 and REST is the list of the rest of the server's tokens.
951 Using this, you can write coroutines which process many commands (and
952 possibly other events) simultaneously.
955 def __init__(me, queue, tag, words):
956 """Make an asynchronous command consisting of the given WORDS, which
957 sends responses to QUEUE, labelled with TAG."""
958 TripeCommand.__init__(me, words)
962 def response(me, code, *stuff):
963 """Handle a server response by writing it to the caller's queue."""
964 me.queue.put((me.tag, code, list(stuff)))
966 ###--------------------------------------------------------------------------
969 class TripeJobCancelled (Exception):
971 Exception sent to job handler if the client kills the job.
973 Not propagated further.
977 class TripeJobError (Exception):
979 Exception to cause failure report for running job.
981 Sends an SVCFAIL code back.
985 class TripeSyntaxError (Exception):
987 Exception to report a syntax error for a job.
989 Sends an SVCFAIL bad-svc-syntax message back.
993 class TripeServiceManager (TripeCommandDispatcher):
995 A command dispatcher with added handling for incoming service requests.
997 There is usually only one instance of this class, called svcmgr. Some of
998 the support functions in this module assume that this is the case.
1000 To use, run `mLib.select' in a loop until the quitp method returns true;
1001 then, in a non-root coroutine, register your services by calling `add', and
1002 then call `running' when you've finished setting up.
1004 The instance handles server service messages `SVCJOB', `SVCCANCEL' and
1005 `SVCCLAIM'. It maintains a table of running services. Incoming jobs cause
1006 the service's `job' method to be invoked; `SVCCANCEL' sends a
1007 `TripeJobCancelled' exception to the handler coroutine, and `SVCCLAIM'
1008 causes the relevant service to be deregistered.
1010 There is no base class for jobs, but a job must implement two methods:
1012 start() Begin processing; might be a no-op.
1014 cancel() Stop processing; the original client has killed the
1017 The life of a service manager is divided into two parts: setup and running;
1018 you tell the manager that you've finished setting up by calling the
1019 `running' method. If, at any point after setup is finished, there are no
1020 remaining services or jobs, `quitp' will return true, ending the process.
1023 ## --- Attributes ---
1025 ## svc Mapping name -> service object
1027 ## job Mapping jobid -> job handler coroutine
1029 ## runningp True when setup is finished
1031 ## _quitp True if explicit quit has been requested
1033 def __init__(me, socket):
1035 Initialize the service manager.
1037 SOCKET is the administration socket to connect to.
1039 TripeCommandDispatcher.__init__(me, socket)
1043 me.handler['SVCCANCEL'] = me._cancel
1044 me.handler['SVCJOB'] = me._job
1045 me.handler['SVCCLAIM'] = me._claim
1048 def addsvc(me, svc):
1049 """Register a new service; SVC is a `TripeService' instance."""
1050 assert svc.name not in me.svc
1051 me.svcclaim(svc.name, svc.version)
1052 me.svc[svc.name] = svc
1054 def _cancel(me, _, jid):
1056 Called when the server cancels a job; invokes the job's `cancel' method.
1062 def _claim(me, _, svc, __):
1063 """Called when another program claims our service at a higher version."""
1066 def _job(me, _, jid, svc, cmd, *args):
1068 Called when the server sends us a job to do.
1070 Calls the service to collect a job, and begins processing it.
1072 assert jid not in me.job
1073 svc = me.svc[svc.lower()]
1074 job = svc.job(jid, cmd, args)
1079 """Answer true if setup is finished."""
1082 def jobdone(me, jid):
1083 """Informs the service manager that the job with id JID has finished."""
1091 Return true if no services or jobs are active (and, therefore, if this
1092 process can quit without anyone caring).
1094 return me._quitp or (me.runningp and ((not me.svc and not me.job) or
1098 """Forces the quit flag (returned by quitp) on."""
1101 class TripeService (object):
1105 The NAME and VERSION are passed on to the server. The CMDTAB is a
1106 dictionary mapping command names (in lowercase) to command objects.
1108 If the CMDTAB doesn't have entries for commands `HELP' and `QUIT' then
1109 defaults are provided.
1111 TripeService itself is mostly agnostic about the nature of command objects,
1112 but the TripeServiceJob class (below) has some requirements. The built-in
1113 HELP command requires command objects to have `usage' attributes.
1116 def __init__(me, name, version, cmdtab):
1118 Create and register a new service with the given NAME and VERSION.
1120 CMDTAB maps command names (in lower-case) to command objects.
1123 me.version = version
1126 me.cmd.setdefault('help',
1127 TripeServiceCommand('help', 0, 0, '', me._help))
1128 me.cmd.setdefault('quit',
1129 TripeServiceCommand('quit', 0, 0, '', me._quit))
1131 def job(me, jid, cmd, args):
1133 Called by the service manager: a job arrived with id JID.
1135 It asks for comamnd CMD with argument list ARGS. Creates a new job,
1136 passing it the information needed.
1138 return TripeServiceJob(jid, me, cmd, me.cmd.get(cmd.lower()), args)
1140 ## Simple default command handlers, complying with the spec in
1141 ## tripe-service(7).
1144 """Send a help summary to the user."""
1145 cmds = me.cmd.items()
1147 for name, cmd in cmds:
1148 svcinfo(name, *cmd.usage)
1151 """Terminate the service manager."""
1152 svcmgr.notify('svc-quit', me.name, 'admin-request')
1155 class TripeServiceCommand (object):
1156 """A simple service command."""
1158 def __init__(me, name, min, max, usage, func):
1160 Creates a new command.
1162 NAME is the command's name (in lowercase).
1164 MIN and MAX are the minimum and maximum number of allowed arguments (used
1165 for checking); either may be None to indicate no minimum or maximum.
1167 USAGE is a usage string, used for generating help and error messages.
1169 FUNC is the function to invoke.
1174 me.usage = usage.split()
1179 Called when the command is invoked.
1181 Does minimal checking of the arguments and calls the supplied function.
1183 if (me.min is not None and len(args) < me.min) or \
1184 (me.max is not None and len(args) > me.max):
1185 raise TripeSyntaxError()
1188 class TripeServiceJob (Coroutine):
1190 Job handler coroutine.
1192 A standard `TripeService' invokes a `TripeServiceJob' for each incoming job
1193 request, passing it the jobid, command and arguments, and a command object.
1194 The command object needs the following attributes.
1196 usage A usage list (excluding the command name) showing
1197 arguments and options.
1199 run(*ARGS) Function to react to the command with ARGS split into
1200 separate arguments. Invoked in a coroutine. The
1201 `svcinfo function (not the `TripeCommandDispatcher'
1202 method) may be used to send `INFO' lines. The
1203 function may raise `TripeJobError' to send a `FAIL'
1204 response back, or `TripeSyntaxError' to send a
1205 generic usage error. `TripeJobCancelled' exceptions
1206 are trapped silently. Other exceptions are
1207 translated into a generic internal-error message.
1209 This class automatically takes care of sending some closing response to the
1210 job, and for informing the service manager that the job is completed.
1212 The `jid' attribute stores the job's id.
1215 def __init__(me, jid, svc, cmd, command, args):
1219 The job is created with id JID, for service SVC, processing command name
1220 CMD (which the service resolved into the command object COMMAND, or
1221 `None'), and with the arguments ARGS.
1223 Coroutine.__init__(me)
1227 me.command = command
1232 Main body of the coroutine.
1234 Does the tedious exception handling boilerplate and invokes the command's
1239 if me.command is None:
1240 svcmgr.svcfail(me.jid, 'unknown-svc-command', me.cmd)
1242 me.command.run(*me.args)
1243 svcmgr.svcok(me.jid)
1244 except TripeJobError, exc:
1245 svcmgr.svcfail(me.jid, *exc.args)
1246 except TripeSyntaxError:
1247 svcmgr.svcfail(me.jid, 'bad-svc-syntax',
1248 me.svc.name, me.command.name,
1250 except TripeJobCancelled:
1252 except Exception, exc:
1253 svcmgr.svcfail(me.jid, 'svc-internal-error',
1254 exc.__class__.__name__, str(exc))
1256 svcmgr.jobdone(me.jid)
1259 """Invoked by the service manager to start running the coroutine."""
1263 """Invoked by the service manager to cancel the job."""
1264 me.throw(TripeJobCancelled())
1268 If invoked from a TripeServiceJob coroutine, sends an `INFO' line to the
1269 job's sender, automatically using the correct job id.
1271 svcmgr.svcinfo(Coroutine.getcurrent().jid, *args)
1273 def _setupsvc(tab, func):
1275 Setup coroutine for setting up service programs.
1277 Register the given services.
1281 svcmgr.addsvc(service)
1287 svcmgr = TripeServiceManager(None)
1288 def runservices(socket, tab, init = None, setup = None, daemon = False):
1290 Function to start a service provider.
1292 SOCKET is the socket to connect to, usually tripesock.
1294 TAB is a list of entries. An entry may be either a tuple
1296 (NAME, VERSION, COMMANDS)
1298 or a service object (e.g., a `TripeService' instance).
1300 COMMANDS is a dictionary mapping command names to tuples
1302 (MIN, MAX, USAGE, FUNC)
1304 of arguments for a `TripeServiceCommand' object.
1306 If DAEMON is true, then the process is forked into the background before we
1307 start. If INIT is given, it is called in the main coroutine, immediately
1308 after forking. If SETUP is given, it is called in a coroutine, after
1309 calling INIT and setting up the services but before marking the service
1312 It is a really bad idea to do any initialization, particularly setting up
1313 coroutines, outside of the INIT or SETUP functions. In particular, if
1314 we're using rmcr for fake coroutines, the daemonizing fork will kill off
1315 the currently established coroutines in a most surprising way.
1317 The function runs a main select loop until the service manager decides to
1321 svcmgr.socket = socket
1325 if not isinstance(service, tuple):
1326 svcs.append(service)
1328 name, version, commands = service
1330 for cmd, stuff in commands.iteritems():
1331 cmdmap[cmd] = TripeServiceCommand(cmd, *stuff)
1332 svcs.append(TripeService(name, version, cmdmap))
1335 if init is not None:
1337 spawn(_setupsvc, svcs, setup)
1340 ###--------------------------------------------------------------------------
1341 ### Utilities for services.
1343 _timeunits = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}
1345 """Parse the timespec SPEC, returning a number of seconds."""
1347 if len(spec) > 1 and spec[-1] in _timeunits:
1348 mul = _timeunits[spec[-1]]
1353 raise TripeJobError('bad-time-spec', spec)
1355 raise TripeJobError('bad-time-spec', spec)
1356 return mul * int(spec)
1358 class OptParse (object):
1360 Parse options from a command list in the conventional fashion.
1362 ARGS is a list of arguments to a command. ALLOWED is a sequence of allowed
1363 options. The returned values are the option tags. During parsing, the
1364 `arg' method may be used to retrieve the argument for the most recent
1365 option. Afterwards, `rest' may be used to retrieve the remaining
1366 non-option arguments, and do a simple check on how many there are.
1368 The parser correctly handles `--' option terminators.
1371 def __init__(me, args, allowed):
1373 Create a new option parser.
1375 The parser will scan the ARGS for options given in the sequence ALLOWED
1376 (which are expected to include the `-' prefix).
1380 me.allowed[a] = True
1381 me.args = list(args)
1384 """Iterator protocol: I am my own iterator."""
1389 Iterator protocol: return the next option.
1391 If we've run out, raise `StopIteration'.
1393 if len(me.args) == 0 or \
1394 len(me.args[0]) < 2 or \
1395 not me.args[0].startswith('-'):
1396 raise StopIteration()
1397 opt = me.args.pop(0)
1399 raise StopIteration()
1400 if opt not in me.allowed:
1401 raise TripeSyntaxError()
1406 Return the argument for the most recent option.
1408 If none is available, raise `TripeSyntaxError'.
1410 if len(me.args) == 0:
1411 raise TripeSyntaxError()
1412 return me.args.pop(0)
1414 def rest(me, min = None, max = None):
1416 After option parsing is done, return the remaining arguments.
1418 Check that there are at least MIN and at most MAX arguments remaining --
1419 either may be None to suppress the check.
1421 if (min is not None and len(me.args) < min) or \
1422 (max is not None and len(me.args) > max):
1423 raise TripeSyntaxError()
1426 ###----- That's all, folks --------------------------------------------------