~oubiwann/txloadbalancer/0.9.0

« back to all changes in this revision

Viewing changes to pydirector/web/admin.py

  • Committer: Duncan McGreggor
  • Date: 2008-06-21 06:26:46 UTC
  • Revision ID: oubiwann@lorien-20080621062646-fu029s1r7h9gcdyw
Added we resources to the .tac file for rudimentary twisted admin web ui.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
 
1
import time
 
2
import urllib
 
3
import socket
 
4
 
 
5
from twisted.web import resource
 
6
 
 
7
from pydirector import Version
 
8
from pydirector.web import template
 
9
 
 
10
class BasePage(resource.Resource):
 
11
    """
 
12
 
 
13
    """
 
14
    def __init__(self, parent):
 
15
        resource.Resource.__init__(self)
 
16
        self.parent = parent
 
17
 
 
18
    def getHeader(self, refreshURL=''):
 
19
        """
 
20
 
 
21
        """
 
22
        refresh = ''
 
23
        if refreshURL:
 
24
            # XXX add an admin configuration option for setting the refresh
 
25
            # rate
 
26
            refreshRate = 30
 
27
            refresh = template.refresh % (refreshRate, refreshURL)
 
28
        return template.header % (
 
29
            refresh, self.parent.serverVersion, socket.gethostname())
 
30
 
 
31
    def getBody(self):
 
32
        """
 
33
        Subclasses must override this.
 
34
        """
 
35
        raise NotImplemented
 
36
 
 
37
    def getFooter(self, message=''):
 
38
        """
 
39
 
 
40
        """
 
41
        # XXX put the project URL in the admin config
 
42
        projectURL ='http://pythondirector.sf.net'
 
43
        if message:
 
44
            message = template.message % urllib.unquote(message)
 
45
        return template.footer % (projectURL, message)
 
46
 
 
47
 
 
48
    def getPage(self):
 
49
        """
 
50
 
 
51
        """
 
52
        return self.getHeader() + self.getBody() + self.getFooter()
 
53
 
 
54
    def render_GET(self, request):
 
55
        """
 
56
 
 
57
        """
 
58
        return self.getPage()
 
59
 
 
60
class RunningPage(BasePage):
 
61
    """
 
62
 
 
63
    """
 
64
    def getPage(self):
 
65
        """
 
66
 
 
67
        """
 
68
        refresh = False
 
69
        verbose = False
 
70
        resultMessage = ''
 
71
        content = self.getHeader(refreshURL='/running?refresh=1&ignore=%s' % time.time())
 
72
        if refresh:
 
73
            stopStart = template.stopRefresh % time.time()
 
74
        else:
 
75
            stopStart = template.startRefresh % time.time()
 
76
        content += template.refreshButtons % (
 
77
            time.ctime(time.time()), time.time(), stopStart)
 
78
        for service in self.parent.director.conf.getServices():
 
79
            content += template.serviceName % service.name
 
80
            for l in service.listen:
 
81
                content += template.listeningService % l
 
82
            eg = service.getEnabledGroup()
 
83
            groups = service.getGroups()
 
84
            for group in groups:
 
85
                sch = self.parent.director.getScheduler(service.name, group.name)
 
86
                stats = sch.getStats(verbose=verbose)
 
87
                hdict = sch.getHostNames()
 
88
                if group is eg:
 
89
                    klass = 'enabled'
 
90
                    desc = template.groupDescEnabled
 
91
                else:
 
92
                    klass = 'inactive'
 
93
                    desc = template.groupDescDisabled % (
 
94
                        service.name, group.name)
 
95
                content += template.groupName % (klass, group.name)
 
96
                content += desc
 
97
                content += template.groupHeaderForm % (
 
98
                    service.name, group.name, klass)
 
99
                counts = stats['open']
 
100
                totals = stats['totals']
 
101
                k = counts.keys()
 
102
                k.sort()
 
103
                for h in k:
 
104
                    if counts.has_key(h):
 
105
                        oc = counts[h]
 
106
                    else:
 
107
                        oc = '--'
 
108
                    if totals.has_key(h):
 
109
                        tc = totals[h]
 
110
                    else:
 
111
                        tc = '--'
 
112
                    content += template.hostInfo % (
 
113
                        klass, hdict[h], h, oc, tc, urllib.quote(service.name),
 
114
                        urllib.quote(group.name), urllib.quote(h))
 
115
                bad = stats['bad']
 
116
                if bad:
 
117
                    content += template.badHostGroup % klass
 
118
                for k in bad.keys():
 
119
                    host = '%s:%s' % k
 
120
                    when, what = bad[k]
 
121
                    content += template.badHostInfo % (
 
122
                        klass, hdict[host], host, what)
 
123
            content += template.serviceClose
 
124
        content += self.getFooter(resultMessage)
 
125
        return str(content)
 
126
 
 
127
class AdminServer(resource.Resource):
 
128
    """
 
129
 
 
130
    """
 
131
    def __init__(self, director):
 
132
        resource.Resource.__init__(self)
 
133
        self.director = director
 
134
        self.config = director.conf.admin
 
135
        self.starttime = time.time()
 
136
        self.serverVersion = "pythondirector/%s"%Version
 
137
 
 
138
    def getChild(self, name, request):
 
139
        """
 
140
 
 
141
        """
 
142
        if name == 'running':
 
143
            return RunningPage(self)
 
144
        return resource.Resource.getChild(self, name, request)
 
145
 
 
146
    def render_GET(self, request):
 
147
        """
 
148
 
 
149
        """
 
150
        return "base"