1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Twisted Application Plugin code for the MAAS region server."""
6
from __future__ import (
15
from os import environ
17
from django.core.handlers.wsgi import WSGIHandler
18
from twisted.application.internet import TCPServer
19
from twisted.application.service import (
23
from twisted.internet import reactor
24
from twisted.plugin import IPlugin
25
from twisted.python import usage
26
from twisted.python.threadpool import ThreadPool
27
from twisted.web.server import Site
28
from twisted.web.wsgi import WSGIResource
29
from zope.interface import implements
32
class DjangoService(TCPServer):
35
Ensure that the ``DJANGO_SETTINGS_MODULE`` environment variable has been
36
set before creating new instances of this service.
39
def __init__(self, port, **kwargs):
40
self.threadpool = ThreadPool()
41
self.handler = WSGIHandler()
42
self.reactor = kwargs.get("reactor", reactor)
43
self.resource = WSGIResource(
44
self.reactor, self.threadpool, self.handler)
45
self.site = Site(self.resource)
46
TCPServer.__init__(self, port, self.site, **kwargs)
48
def startService(self):
49
self.threadpool.start()
50
TCPServer.startService(self)
52
def stopService(self):
53
TCPServer.stopService(self)
54
self.threadpool.stop()
57
class Options(usage.Options):
58
"""Command line options for the provisioning server."""
61
("django-settings", None, "maas", "Django settings module."),
62
("django-port", None, 5240, "Django listen port.", int),
66
class RegionServerMaker(object):
67
"""Create a region service for the Twisted plugin."""
69
implements(IServiceMaker, IPlugin)
73
def __init__(self, name, description):
75
self.description = description
77
def makeService(self, options):
78
"""Construct a service."""
79
services = MultiService()
81
environ["DJANGO_SETTINGS_MODULE"] = options["django-settings"]
83
django_service = DjangoService(options["django-port"])
84
django_service.setName("django")
85
django_service.setServiceParent(services)