1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 Python Administration Daemon.
6 Python Administration Daemon is an modular, extensible administration tool
7 to administrate a set of services remotely (or localy) throw a simple
14 from pymin.dispatcher import handler
15 from pymin import dispatcher
16 from pymin import eventloop
17 from pymin import serializer
18 from pymin import procman
20 class PyminDaemon(eventloop.EventLoop):
21 r"""PyminDaemon(root, bind_addr) -> PyminDaemon instance
23 This class is well suited to run as a single process. It handles
24 signals for controlled termination (SIGINT and SIGTERM), as well as
25 a user signal to reload the configuration files (SIGUSR1).
27 root - the root handler. This is passed directly to the Dispatcher.
29 bind_addr - is a tuple of (ip, port) where to bind the UDP socket to.
31 Here is a simple usage example:
33 >>> from pymin import dispatcher
34 >>> class Root(dispatcher.Handler):
35 @handler('Test command.')
36 def test(self, *args):
38 >>> PyminDaemon(Root(), ('', 9999)).run()
41 def __init__(self, root, bind_addr=('', 9999), timer=1):
42 r"""Initialize the PyminDaemon object.
44 See PyminDaemon class documentation for more info.
48 # Create and bind socket
49 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
50 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
53 def quit(loop, signum):
54 print "Shuting down ..."
55 loop.stop() # tell main event loop to stop
56 def reload_config(loop, signum):
57 print "Reloading configuration..."
58 # TODO iterate handlers list propagating reload action
59 def timer(loop, signum):
61 signal.alarm(loop.timer)
62 def child(loop, signum):
63 procman.sigchild_handler(signum)
65 eventloop.EventLoop.__init__(self, sock, signals={
68 signal.SIGUSR1: reload_config,
69 signal.SIGALRM: timer,
70 signal.SIGCHLD: child,
73 #TODO root.pymin = PyminHandler()
74 self.dispatcher = dispatcher.Dispatcher(root)
77 r"handle() -> None :: Handle incoming events using the dispatcher."
78 (msg, addr) = self.file.recvfrom(65535)
80 result = self.dispatcher.dispatch(unicode(msg, 'utf-8'))
81 if result is not None:
82 result = serializer.serialize(result)
84 except dispatcher.Error, e:
85 result = unicode(e) + u'\n'
89 result = u'Internal server error\n'
90 traceback.print_exc() # TODO logging!
95 response += u'%d\n%s' % (len(result), result)
96 self.file.sendto(response.encode('utf-8'), addr)
98 def handle_timer(self):
99 r"handle_timer() -> None :: Call handle_timer() on handlers."
100 self.dispatcher.root.handle_timer()
103 r"run() -> None :: Run the event loop (shortcut to loop())"
106 signal.alarm(self.timer)
110 except eventloop.LoopInterruptedError, e:
113 if __name__ == '__main__':
115 class Root(dispatcher.Handler):
116 @handler(u"Print all the arguments, return nothing.")
117 def test(self, *args):
119 @handler(u"Echo the message passed as argument.")
120 def echo(self, message):
121 print 'echo:', message
124 PyminDaemon(Root()).run()