summaryrefslogtreecommitdiffstats
path: root/ipsilon/admin/common.py
blob: fd200778bb6940a27fd4efd0b24f494047389bb6 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# 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 import config as pconfig


ADMIN_STATUS_OK = "success"
ADMIN_STATUS_ERROR = "danger"
ADMIN_STATUS_WARN = "warning"


class AdminError(Exception):
    def __init__(self, message):
        super(AdminError, self).__init__(message)
        self.message = message

    def __str__(self):
        return str(self.message)


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

    def root_with_msg(self, message=None, message_type=None):
        return self._template('admin/plugin_config.html', title=self.title,
                              menu=self.menu, action=self.url, back=self.back,
                              message=message, message_type=message_type,
                              name='admin_%s_%s_form' % (self.facility,
                                                         self._po.name),
                              config=self._po.get_config_obj())

    def get_complex_list_value(self, name, old_value, **kwargs):
        delete = list()
        change = dict()
        for key, val in kwargs.iteritems():
            if not key.startswith(name):
                continue
            n = key[len(name):]
            if len(n) == 0 or n[0] != ' ':
                continue
            try:
                index, field = n[1:].split('-')
            except ValueError:
                continue
            if field == 'delete':
                delete.append(int(index))
            elif field == 'name':
                change[int(index)] = val

        if len(delete) == 0 and len(change) == 0:
            return None

        value = old_value

        # remove unwanted changes
        for i in delete:
            if i in change:
                del change[i]

        # perform requested changes
        for index, val in change.iteritems():
            val_list = val.split('/')
            stripped = list()
            for v in val_list:
                stripped.append(v.strip())
            if len(stripped) == 1:
                stripped = stripped[0]
            if len(value) <= index:
                value.extend([None]*(index + 1 - len(value)))
            value[index] = stripped

            if len(value[index]) == 0:
                value[index] = None

        # the previous loop may add 'None' entries
        # if any still exists mark them to be deleted
        for i in xrange(0, len(value)):
            if value[i] is None:
                delete.append(i)

        # remove duplicates and set in reverse order
        delete = list(set(delete))
        delete.sort(reverse=True)

        for i in delete:
            if len(value) > i:
                del value[i]

        if len(value) == 0:
            value = None
        return value

    def get_mapping_list_value(self, name, old_value, **kwargs):
        delete = list()
        change = dict()
        for key, val in kwargs.iteritems():
            if not key.startswith(name):
                continue
            n = key[len(name):]
            if len(n) == 0 or n[0] != ' ':
                continue
            try:
                index, field = n[1:].split('-')
            except ValueError:
                continue
            if field == 'delete':
                delete.append(int(index))
            else:
                i = int(index)
                if i not in change:
                    change[i] = dict()
                change[i][field] = val

        if len(delete) == 0 and len(change) == 0:
            return None

        value = old_value

        # remove unwanted changes
        for i in delete:
            if i in change:
                del change[i]

        # perform requested changes
        for index, fields in change.iteritems():
            for k in 'from', 'to':
                if k in fields:
                    val = fields[k]
                    val_list = val.split('/')
                    stripped = list()
                    for v in val_list:
                        stripped.append(v.strip())
                    if len(stripped) == 1:
                        stripped = stripped[0]
                    if len(value) <= index:
                        value.extend([None]*(index + 1 - len(value)))
                    if value[index] is None:
                        value[index] = [None, None]
                    if k == 'from':
                        i = 0
                    else:
                        i = 1
                    value[index][i] = stripped

            # eliminate incomplete/incorrect entries
            if value[index] is not None:
                if ((len(value[index]) != 2 or
                     value[index][0] is None or
                     len(value[index][0]) == 0 or
                     value[index][1] is None or
                     len(value[index][1]) == 0)):
                    value[index] = None

        # the previous loop may add 'None' entries
        # if any still exists mark them to be deleted
        for i in xrange(0, len(value)):
            if value[i] is None:
                delete.append(i)

        # remove duplicates and set in reverse order
        delete = list(set(delete))
        delete.sort(reverse=True)

        for i in delete:
            if len(value) > i:
                del value[i]

        if len(value) == 0:
            value = None
        return value

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

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

        if self._po.is_readonly:
            return self.root_with_msg(
                message="Configuration is marked Read-Only",
                message_type=ADMIN_STATUS_WARN)

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

        conf = self._po.get_config_obj()

        for name, option in conf.iteritems():
            if name in kwargs:
                value = kwargs[name]
                if isinstance(option, pconfig.List):
                    value = [x.strip() for x in value.split('\n')]
                elif isinstance(option, pconfig.Condition):
                    value = True
            else:
                if isinstance(option, pconfig.Condition):
                    value = False
                elif isinstance(option, pconfig.Choice):
                    value = list()
                    for a in option.get_allowed():
                        aname = '%s_%s' % (name, a)
                        if aname in kwargs:
                            value.append(a)
                elif type(option) is pconfig.ComplexList:
                    value = self.get_complex_list_value(name,
                                                        option.get_value(),
                                                        **kwargs)
                    if value is None:
                        continue
                elif type(option) is pconfig.MappingList:
                    value = self.get_mapping_list_value(name,
                                                        option.get_value(),
                                                        **kwargs)
                    if value is None:
                        continue
                else:
                    continue

            if value != option.get_value():
                cherrypy.log.error("Storing [%s]: %s = %s" %
                                   (self._po.name, name, value))
            option.set_value(value)
            new_db_values[name] = option.export_value()

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

            # Then refresh the actual objects
            self._po.refresh_plugin_config()

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


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):

        if self._site[self.facility].is_readonly:
            return self.parent.root_with_msg(
                message="Configuration is marked Read-Only",
                message_type=ADMIN_STATUS_WARN)

        message = "Nothing was modified."
        message_type = "info"
        changed = None
        cur_enabled = self._site[self.facility].enabled

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

                    self.parent.save_enabled_plugins(new_order)

                    # When all is saved update also live config. The
                    # live config is the ordered list of plugin names.
                    self._site[self.facility].refresh_enabled()

                    message = "New configuration saved."
                    message_type = ADMIN_STATUS_OK

                    changed = dict()
                    self.debug('%s -> %s' % (cur_enabled, new_order))
                    for i in range(0, len(cur_enabled)):
                        if cur_enabled[i] != new_order[i]:
                            changed[cur_enabled[i]] = 'reordered'

                except ValueError, e:
                    message = str(e)
                    message_type = ADMIN_STATUS_ERROR

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

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


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):
        self._site[self.facility].save_enabled(names)

    def root_with_msg(self, message=None, message_type=None, changed=None):
        plugins = self._site[self.facility]

        if changed is None:
            changed = dict()

        targs = {'title': self.title,
                 'menu': self._master.menu,
                 'message': message,
                 'message_type': message_type,
                 'available': plugins.available,
                 'enabled': plugins.enabled,
                 'changed': changed,
                 'baseurl': self.url,
                 'newurl': 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()

    def _get_plugin_obj(self, plugin):
        plugins = self._site[self.facility]
        if plugins.is_readonly:
            msg = "Configuration is marked Read-Only"
            raise AdminError(msg)
        if plugin not in plugins.available:
            msg = "Unknown plugin %s" % plugin
            raise AdminError(msg)
        obj = plugins.available[plugin]
        if obj.is_readonly:
            msg = "Plugin Configuration is marked Read-Only"
            raise AdminError(msg)
        return obj

    @admin_protect
    def enable(self, plugin):
        msg = None
        try:
            obj = self._get_plugin_obj(plugin)
        except AdminError, e:
            return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
        if not obj.is_enabled:
            obj.enable()
            obj.save_enabled_state()
            msg = "Plugin %s enabled" % obj.name
        return self.root_with_msg(msg, ADMIN_STATUS_OK,
                                  changed={obj.name: 'enabled'})
    enable.public_function = True

    @admin_protect
    def disable(self, plugin):
        msg = None
        try:
            obj = self._get_plugin_obj(plugin)
        except AdminError, e:
            return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
        if obj.is_enabled:
            obj.disable()
            obj.save_enabled_state()
            msg = "Plugin %s disabled" % obj.name
        return self.root_with_msg(msg, ADMIN_STATUS_OK,
                                  changed={obj.name: 'disabled'})
    disable.public_function = True


class Admin(AdminPage):

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

    def root(self, *args, **kwargs):
        return self._template('admin/index.html',
                              title='Configuration',
                              baseurl=self.url,
                              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]

    def get_menu_urls(self):
        urls = dict()
        for item in self.menu:
            name = getattr(item, 'name', None)
            if name:
                urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
                                                                 name))
        return urls

    @admin_protect
    def scheme(self):
        cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
        urls = self.get_menu_urls()
        # pylint: disable=star-args
        return str(self._template('admin/ipsilon-scheme.svg', **urls))
    scheme.public_function = True