summaryrefslogtreecommitdiffstats
path: root/ipsilon/admin/common.py
blob: 827038e3f9f2087500611fb1ce142c283830713c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python
#
# Copyright (C) 2014  Simo Sorce <simo@redhat.com>
#
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import cherrypy
from ipsilon.util.page import Page
from ipsilon.util.page import admin_protect
from ipsilon.util.plugin import PluginObject


class AdminPage(Page):

    def __init__(self, *args, **kwargs):
        super(AdminPage, self).__init__(*args, **kwargs)
        self.default_headers.update({
            'Cache-Control': 'no-cache, must-revalidate',
            'Pragma': 'no-cache',
            'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
        })
        self.auth_protect = True


class AdminPluginConfig(AdminPage):

    def __init__(self, po, site, parent):
        super(AdminPluginConfig, self).__init__(site, form=True)
        self._po = po
        self.title = '%s plugin' % po.name
        self.url = '%s/%s' % (parent.url, po.name)
        self.facility = parent.facility
        self.menu = [parent]
        self.back = parent.url

        # Get the defaults
        options = po.get_config_desc()
        if options is None:
            options = dict()

        self.options_order = []
        if hasattr(po, 'conf_opt_order'):
            self.options_order = po.conf_opt_order

        # append any undefined options
        add = []
        for k in options.keys():
            if k not in self.options_order:
                add.append(k)
        if len(add):
            add.sort()
            for k in add:
                self.options_order.append(k)

    @admin_protect
    def GET(self, *args, **kwargs):
        return self._template('admin/plugin_config.html', title=self.title,
                              name='admin_%s_%s_form' % (self.facility,
                                                         self._po.name),
                              menu=self.menu, action=self.url, back=self.back,
                              options_order=self.options_order,
                              plugin=self._po)

    @admin_protect
    def POST(self, *args, **kwargs):

        message = "Nothing was modified."
        message_type = "info"
        new_values = dict()

        # Get the defaults
        options = self._po.get_config_desc()
        if options is None:
            options = dict()

        for key, value in kwargs.iteritems():
            if key in options:
                if value != self._po.get_config_value(key):
                    cherrypy.log.error("Storing [%s]: %s = %s" %
                                       (self._po.name, key, value))
                    new_values[key] = value

        if len(new_values) != 0:
            # First we try to save in the database
            try:
                self._po.save_plugin_config(self.facility, new_values)
                message = "New configuration saved."
                message_type = "success"
            except Exception:  # pylint: disable=broad-except
                message = "Failed to save data!"
                message_type = "error"

            # And only if it succeeds we change the live object
            self._po.refresh_plugin_config(self.facility)

        return self._template('admin/plugin_config.html', title=self.title,
                              message=message,
                              message_type=message_type,
                              name='admin_%s_%s_form' % (self.facility,
                                                         self._po.name),
                              menu=self.menu, action=self.url,
                              plugin=self._po)


class AdminPluginsOrder(AdminPage):

    def __init__(self, site, parent, facility):
        super(AdminPluginsOrder, self).__init__(site, form=True)
        self.parent = parent
        self.facility = facility
        self.url = '%s/order' % parent.url
        self.menu = [parent]

    @admin_protect
    def GET(self, *args, **kwargs):
        return self.parent.root_with_msg()

    @admin_protect
    def POST(self, *args, **kwargs):
        message = "Nothing was modified."
        message_type = "info"
        by_name = {p.name: p for p in self._site[self.facility]['enabled']}

        if 'order' in kwargs:
            order = kwargs['order'].split(',')
            if len(order) != 0:
                new_names = []
                new_plugins = []
                try:
                    for v in order:
                        val = v.strip()
                        if val not in by_name:
                            error = "Invalid plugin name: %s" % val
                            raise ValueError(error)
                        new_names.append(val)
                        new_plugins.append(by_name[val])
                    if len(new_names) < len(by_name):
                        for val in by_name:
                            if val not in new_names:
                                new_names.append(val)
                                new_plugins.append(by_name[val])

                    self.parent.save_enabled_plugins(new_names)
                    self.parent.reorder_plugins(new_names)

                    # When all is saved update also live config. The
                    # live config is a list of the actual plugin
                    # objects.
                    self._site[self.facility]['enabled'] = new_plugins

                    message = "New configuration saved."
                    message_type = "success"

                except ValueError, e:
                    message = str(e)
                    message_type = "error"

                except Exception, e:  # pylint: disable=broad-except
                    message = "Failed to save data!"
                    message_type = "error"

        return self.parent.root_with_msg(message=message,
                                         message_type=message_type)


class AdminPlugins(AdminPage):
    def __init__(self, name, site, parent, facility, ordered=True):
        super(AdminPlugins, self).__init__(site)
        self._master = parent
        self.name = name
        self.title = '%s plugins' % name
        self.url = '%s/%s' % (parent.url, name)
        self.facility = facility
        self.template = 'admin/plugins.html'
        self.order = None
        parent.add_subtree(name, self)

        for plugin in self._site[facility]['available']:
            cherrypy.log.error('Admin info plugin: %s' % plugin)
            obj = self._site[facility]['available'][plugin]
            page = AdminPluginConfig(obj, self._site, self)
            if hasattr(obj, 'admin'):
                obj.admin.mount(page)
            self.add_subtree(plugin, page)

        if ordered:
            self.order = AdminPluginsOrder(self._site, self, facility)

    def save_enabled_plugins(self, names):
        po = PluginObject()
        po.name = "global"
        globalconf = dict()
        globalconf['order'] = ','.join(names)
        po.set_config(globalconf)
        po.save_plugin_config(self.facility)

    def reorder_plugins(self, names):
        return

    def root_with_msg(self, message=None, message_type=None):
        plugins = self._site[self.facility]
        enabled = []
        for p in plugins['enabled']:
            enabled.append(p.name)
        targs = {'title': self.title,
                 'menu': self._master.menu,
                 'message': message,
                 'message_type': message_type,
                 'available': plugins['available'],
                 'enabled': enabled,
                 'baseurl': self.url}
        if self.order:
            targs['order_name'] = '%s_order_form' % self.name
            targs['order_action'] = self.order.url

        # pylint: disable=star-args
        return self._template(self.template, **targs)

    def root(self, *args, **kwargs):
        return self.root_with_msg()

    @admin_protect
    def enable(self, plugin):
        msg = None
        plugins = self._site[self.facility]
        if plugin not in plugins['available']:
            msg = "Unknown plugin %s" % plugin
            return self.root_with_msg(msg, "error")
        obj = plugins['available'][plugin]
        if obj not in plugins['enabled']:
            obj.enable(self._site)
            if self.order:
                enabled = list(x.name for x in plugins['enabled'])
                self.save_enabled_plugins(enabled)
            msg = "Plugin %s enabled" % obj.name
        return self.root_with_msg(msg, "success")
    enable.public_function = True

    @admin_protect
    def disable(self, plugin):
        msg = None
        plugins = self._site[self.facility]
        if plugin not in plugins['available']:
            msg = "Unknown plugin %s" % plugin
            return self.root_with_msg(msg, "error")
        obj = plugins['available'][plugin]
        if obj in plugins['enabled']:
            obj.disable(self._site)
            if self.order:
                enabled = list(x.name for x in plugins['enabled'])
                self.save_enabled_plugins(enabled)
            msg = "Plugin %s disabled" % obj.name
        return self.root_with_msg(msg, "success")
    disable.public_function = True


class Admin(AdminPage):

    def __init__(self, site, mount):
        super(Admin, self).__init__(site)
        self.url = '%s/%s' % (self.basepath, mount)
        self.menu = []

    def root(self, *args, **kwargs):
        return self._template('admin/index.html',
                              title='Configuration',
                              menu=self.menu)

    def add_subtree(self, name, page):
        self.__dict__[name] = page
        self.menu.append(page)

    def del_subtree(self, name):
        self.menu.remove(self.__dict__[name])
        del self.__dict__[name]