~allenap/maas/region-daemon

« back to all changes in this revision

Viewing changes to src/maasserver/daemon.py

  • Committer: Gavin Panella
  • Date: 2012-10-13 07:22:42 UTC
  • Revision ID: gavin.panella@canonical.com-20121013072242-h8i2ybny3q9isntf
Beginnings of a maas-region Twisted plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Twisted Application Plugin code for the MAAS region server."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
from os import environ
 
16
 
 
17
from django.core.handlers.wsgi import WSGIHandler
 
18
from twisted.application.internet import TCPServer
 
19
from twisted.application.service import (
 
20
    IServiceMaker,
 
21
    MultiService,
 
22
    )
 
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
 
30
 
 
31
 
 
32
class DjangoService(TCPServer):
 
33
    """A Django server.
 
34
 
 
35
    Ensure that the ``DJANGO_SETTINGS_MODULE`` environment variable has been
 
36
    set before creating new instances of this service.
 
37
    """
 
38
 
 
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)
 
47
 
 
48
    def startService(self):
 
49
        self.threadpool.start()
 
50
        TCPServer.startService(self)
 
51
 
 
52
    def stopService(self):
 
53
        TCPServer.stopService(self)
 
54
        self.threadpool.stop()
 
55
 
 
56
 
 
57
class Options(usage.Options):
 
58
    """Command line options for the provisioning server."""
 
59
 
 
60
    optParameters = (
 
61
        ("django-settings", None, "maas", "Django settings module."),
 
62
        ("django-port", None, 5240, "Django listen port.", int),
 
63
        )
 
64
 
 
65
 
 
66
class RegionServerMaker(object):
 
67
    """Create a region service for the Twisted plugin."""
 
68
 
 
69
    implements(IServiceMaker, IPlugin)
 
70
 
 
71
    options = Options
 
72
 
 
73
    def __init__(self, name, description):
 
74
        self.tapname = name
 
75
        self.description = description
 
76
 
 
77
    def makeService(self, options):
 
78
        """Construct a service."""
 
79
        services = MultiService()
 
80
 
 
81
        environ["DJANGO_SETTINGS_MODULE"] = options["django-settings"]
 
82
 
 
83
        django_service = DjangoService(options["django-port"])
 
84
        django_service.setName("django")
 
85
        django_service.setServiceParent(services)
 
86
 
 
87
        print(options)
 
88
        return None
 
89
 
 
90
        return services