service.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. # -*- test-case-name: twisted.application.test.test_service -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Service architecture for Twisted.
  6. Services are arranged in a hierarchy. At the leafs of the hierarchy,
  7. the services which actually interact with the outside world are started.
  8. Services can be named or anonymous -- usually, they will be named if
  9. there is need to access them through the hierarchy (from a parent or
  10. a sibling).
  11. Maintainer: Moshe Zadka
  12. """
  13. from __future__ import absolute_import, division
  14. from zope.interface import implementer, Interface, Attribute
  15. from twisted.persisted import sob
  16. from twisted.python.reflect import namedAny
  17. from twisted.python import components
  18. from twisted.python._oldstyle import _oldStyle
  19. from twisted.internet import defer
  20. from twisted.plugin import IPlugin
  21. class IServiceMaker(Interface):
  22. """
  23. An object which can be used to construct services in a flexible
  24. way.
  25. This interface should most often be implemented along with
  26. L{twisted.plugin.IPlugin}, and will most often be used by the
  27. 'twistd' command.
  28. """
  29. tapname = Attribute(
  30. "A short string naming this Twisted plugin, for example 'web' or "
  31. "'pencil'. This name will be used as the subcommand of 'twistd'.")
  32. description = Attribute(
  33. "A brief summary of the features provided by this "
  34. "Twisted application plugin.")
  35. options = Attribute(
  36. "A C{twisted.python.usage.Options} subclass defining the "
  37. "configuration options for this application.")
  38. def makeService(options):
  39. """
  40. Create and return an object providing
  41. L{twisted.application.service.IService}.
  42. @param options: A mapping (typically a C{dict} or
  43. L{twisted.python.usage.Options} instance) of configuration
  44. options to desired configuration values.
  45. """
  46. @implementer(IPlugin, IServiceMaker)
  47. class ServiceMaker(object):
  48. """
  49. Utility class to simplify the definition of L{IServiceMaker} plugins.
  50. """
  51. def __init__(self, name, module, description, tapname):
  52. self.name = name
  53. self.module = module
  54. self.description = description
  55. self.tapname = tapname
  56. def options():
  57. def get(self):
  58. return namedAny(self.module).Options
  59. return get,
  60. options = property(*options())
  61. def makeService():
  62. def get(self):
  63. return namedAny(self.module).makeService
  64. return get,
  65. makeService = property(*makeService())
  66. class IService(Interface):
  67. """
  68. A service.
  69. Run start-up and shut-down code at the appropriate times.
  70. @type name: C{string}
  71. @ivar name: The name of the service (or None)
  72. @type running: C{boolean}
  73. @ivar running: Whether the service is running.
  74. """
  75. def setName(name):
  76. """
  77. Set the name of the service.
  78. @type name: C{str}
  79. @raise RuntimeError: Raised if the service already has a parent.
  80. """
  81. def setServiceParent(parent):
  82. """
  83. Set the parent of the service. This method is responsible for setting
  84. the C{parent} attribute on this service (the child service).
  85. @type parent: L{IServiceCollection}
  86. @raise RuntimeError: Raised if the service already has a parent
  87. or if the service has a name and the parent already has a child
  88. by that name.
  89. """
  90. def disownServiceParent():
  91. """
  92. Use this API to remove an L{IService} from an L{IServiceCollection}.
  93. This method is used symmetrically with L{setServiceParent} in that it
  94. sets the C{parent} attribute on the child.
  95. @rtype: L{Deferred<defer.Deferred>}
  96. @return: a L{Deferred<defer.Deferred>} which is triggered when the
  97. service has finished shutting down. If shutting down is immediate,
  98. a value can be returned (usually, L{None}).
  99. """
  100. def startService():
  101. """
  102. Start the service.
  103. """
  104. def stopService():
  105. """
  106. Stop the service.
  107. @rtype: L{Deferred<defer.Deferred>}
  108. @return: a L{Deferred<defer.Deferred>} which is triggered when the
  109. service has finished shutting down. If shutting down is immediate,
  110. a value can be returned (usually, L{None}).
  111. """
  112. def privilegedStartService():
  113. """
  114. Do preparation work for starting the service.
  115. Here things which should be done before changing directory,
  116. root or shedding privileges are done.
  117. """
  118. @implementer(IService)
  119. class Service(object):
  120. """
  121. Base class for services.
  122. Most services should inherit from this class. It handles the
  123. book-keeping responsibilities of starting and stopping, as well
  124. as not serializing this book-keeping information.
  125. """
  126. running = 0
  127. name = None
  128. parent = None
  129. def __getstate__(self):
  130. dict = self.__dict__.copy()
  131. if "running" in dict:
  132. del dict['running']
  133. return dict
  134. def setName(self, name):
  135. if self.parent is not None:
  136. raise RuntimeError("cannot change name when parent exists")
  137. self.name = name
  138. def setServiceParent(self, parent):
  139. if self.parent is not None:
  140. self.disownServiceParent()
  141. parent = IServiceCollection(parent, parent)
  142. self.parent = parent
  143. self.parent.addService(self)
  144. def disownServiceParent(self):
  145. d = self.parent.removeService(self)
  146. self.parent = None
  147. return d
  148. def privilegedStartService(self):
  149. pass
  150. def startService(self):
  151. self.running = 1
  152. def stopService(self):
  153. self.running = 0
  154. class IServiceCollection(Interface):
  155. """
  156. Collection of services.
  157. Contain several services, and manage their start-up/shut-down.
  158. Services can be accessed by name if they have a name, and it
  159. is always possible to iterate over them.
  160. """
  161. def getServiceNamed(name):
  162. """
  163. Get the child service with a given name.
  164. @type name: C{str}
  165. @rtype: L{IService}
  166. @raise KeyError: Raised if the service has no child with the
  167. given name.
  168. """
  169. def __iter__():
  170. """
  171. Get an iterator over all child services.
  172. """
  173. def addService(service):
  174. """
  175. Add a child service.
  176. Only implementations of L{IService.setServiceParent} should use this
  177. method.
  178. @type service: L{IService}
  179. @raise RuntimeError: Raised if the service has a child with
  180. the given name.
  181. """
  182. def removeService(service):
  183. """
  184. Remove a child service.
  185. Only implementations of L{IService.disownServiceParent} should
  186. use this method.
  187. @type service: L{IService}
  188. @raise ValueError: Raised if the given service is not a child.
  189. @rtype: L{Deferred<defer.Deferred>}
  190. @return: a L{Deferred<defer.Deferred>} which is triggered when the
  191. service has finished shutting down. If shutting down is immediate,
  192. a value can be returned (usually, L{None}).
  193. """
  194. @implementer(IServiceCollection)
  195. class MultiService(Service):
  196. """
  197. Straightforward Service Container.
  198. Hold a collection of services, and manage them in a simplistic
  199. way. No service will wait for another, but this object itself
  200. will not finish shutting down until all of its child services
  201. will finish.
  202. """
  203. def __init__(self):
  204. self.services = []
  205. self.namedServices = {}
  206. self.parent = None
  207. def privilegedStartService(self):
  208. Service.privilegedStartService(self)
  209. for service in self:
  210. service.privilegedStartService()
  211. def startService(self):
  212. Service.startService(self)
  213. for service in self:
  214. service.startService()
  215. def stopService(self):
  216. Service.stopService(self)
  217. l = []
  218. services = list(self)
  219. services.reverse()
  220. for service in services:
  221. l.append(defer.maybeDeferred(service.stopService))
  222. return defer.DeferredList(l)
  223. def getServiceNamed(self, name):
  224. return self.namedServices[name]
  225. def __iter__(self):
  226. return iter(self.services)
  227. def addService(self, service):
  228. if service.name is not None:
  229. if service.name in self.namedServices:
  230. raise RuntimeError("cannot have two services with same name"
  231. " '%s'" % service.name)
  232. self.namedServices[service.name] = service
  233. self.services.append(service)
  234. if self.running:
  235. # It may be too late for that, but we will do our best
  236. service.privilegedStartService()
  237. service.startService()
  238. def removeService(self, service):
  239. if service.name:
  240. del self.namedServices[service.name]
  241. self.services.remove(service)
  242. if self.running:
  243. # Returning this so as not to lose information from the
  244. # MultiService.stopService deferred.
  245. return service.stopService()
  246. else:
  247. return None
  248. class IProcess(Interface):
  249. """
  250. Process running parameters.
  251. Represents parameters for how processes should be run.
  252. """
  253. processName = Attribute(
  254. """
  255. A C{str} giving the name the process should have in ps (or L{None}
  256. to leave the name alone).
  257. """)
  258. uid = Attribute(
  259. """
  260. An C{int} giving the user id as which the process should run (or
  261. L{None} to leave the UID alone).
  262. """)
  263. gid = Attribute(
  264. """
  265. An C{int} giving the group id as which the process should run (or
  266. L{None} to leave the GID alone).
  267. """)
  268. @implementer(IProcess)
  269. @_oldStyle
  270. class Process:
  271. """
  272. Process running parameters.
  273. Sets up uid/gid in the constructor, and has a default
  274. of L{None} as C{processName}.
  275. """
  276. processName = None
  277. def __init__(self, uid=None, gid=None):
  278. """
  279. Set uid and gid.
  280. @param uid: The user ID as whom to execute the process. If
  281. this is L{None}, no attempt will be made to change the UID.
  282. @param gid: The group ID as whom to execute the process. If
  283. this is L{None}, no attempt will be made to change the GID.
  284. """
  285. self.uid = uid
  286. self.gid = gid
  287. def Application(name, uid=None, gid=None):
  288. """
  289. Return a compound class.
  290. Return an object supporting the L{IService}, L{IServiceCollection},
  291. L{IProcess} and L{sob.IPersistable} interfaces, with the given
  292. parameters. Always access the return value by explicit casting to
  293. one of the interfaces.
  294. """
  295. ret = components.Componentized()
  296. availableComponents = [MultiService(), Process(uid, gid),
  297. sob.Persistent(ret, name)]
  298. for comp in availableComponents:
  299. ret.addComponent(comp, ignoreClass=1)
  300. IService(ret).setName(name)
  301. return ret
  302. def loadApplication(filename, kind, passphrase=None):
  303. """
  304. Load Application from a given file.
  305. The serialization format it was saved in should be given as
  306. C{kind}, and is one of C{pickle}, C{source}, C{xml} or C{python}. If
  307. C{passphrase} is given, the application was encrypted with the
  308. given passphrase.
  309. @type filename: C{str}
  310. @type kind: C{str}
  311. @type passphrase: C{str}
  312. """
  313. if kind == 'python':
  314. application = sob.loadValueFromFile(filename, 'application')
  315. else:
  316. application = sob.load(filename, kind)
  317. return application
  318. __all__ = ['IServiceMaker', 'IService', 'Service',
  319. 'IServiceCollection', 'MultiService',
  320. 'IProcess', 'Process', 'Application', 'loadApplication']