__init__ needs to be in the main package
[cascardo/ipsilon.git] / ipsilon / admin / common.py
1 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
2 #
3 # see file 'COPYING' for use and warranty information
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 import cherrypy
19 from ipsilon.util.page import Page
20 from ipsilon.util.page import admin_protect
21 from ipsilon.util import config as pconfig
22
23
24 ADMIN_STATUS_OK = "success"
25 ADMIN_STATUS_ERROR = "danger"
26 ADMIN_STATUS_WARN = "warning"
27
28
29 class AdminError(Exception):
30     def __init__(self, message):
31         super(AdminError, self).__init__(message)
32         self.message = message
33
34     def __str__(self):
35         return str(self.message)
36
37
38 class AdminPage(Page):
39
40     def __init__(self, *args, **kwargs):
41         super(AdminPage, self).__init__(*args, **kwargs)
42         self.default_headers.update({
43             'Cache-Control': 'no-cache, must-revalidate',
44             'Pragma': 'no-cache',
45             'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
46         })
47         self.auth_protect = True
48
49
50 class AdminPluginConfig(AdminPage):
51
52     def __init__(self, po, site, parent):
53         super(AdminPluginConfig, self).__init__(site, form=True)
54         self._po = po
55         self.title = '%s plugin' % po.name
56         self.url = '%s/%s' % (parent.url, po.name)
57         self.facility = parent.facility
58         self.menu = [parent]
59         self.back = parent.url
60
61     def root_with_msg(self, message=None, message_type=None):
62         return self._template('admin/plugin_config.html', title=self.title,
63                               menu=self.menu, action=self.url, back=self.back,
64                               message=message, message_type=message_type,
65                               name='admin_%s_%s_form' % (self.facility,
66                                                          self._po.name),
67                               config=self._po.get_config_obj())
68
69     def get_complex_list_value(self, name, old_value, **kwargs):
70         delete = list()
71         change = dict()
72         for key, val in kwargs.iteritems():
73             if not key.startswith(name):
74                 continue
75             n = key[len(name):]
76             if len(n) == 0 or n[0] != ' ':
77                 continue
78             try:
79                 index, field = n[1:].split('-')
80             except ValueError:
81                 continue
82             if field == 'delete':
83                 delete.append(int(index))
84             elif field == 'name':
85                 change[int(index)] = val
86
87         if len(delete) == 0 and len(change) == 0:
88             return None
89
90         value = old_value
91
92         # remove unwanted changes
93         for i in delete:
94             if i in change:
95                 del change[i]
96
97         # perform requested changes
98         for index, val in change.iteritems():
99             val_list = val.split('/')
100             stripped = list()
101             for v in val_list:
102                 stripped.append(v.strip())
103             if len(stripped) == 1:
104                 stripped = stripped[0]
105             if len(value) <= index:
106                 value.extend([None]*(index + 1 - len(value)))
107             value[index] = stripped
108
109             if len(value[index]) == 0:
110                 value[index] = None
111
112         # the previous loop may add 'None' entries
113         # if any still exists mark them to be deleted
114         for i in xrange(0, len(value)):
115             if value[i] is None:
116                 delete.append(i)
117
118         # remove duplicates and set in reverse order
119         delete = list(set(delete))
120         delete.sort(reverse=True)
121
122         for i in delete:
123             if len(value) > i:
124                 del value[i]
125
126         if len(value) == 0:
127             value = None
128         return value
129
130     def get_mapping_list_value(self, name, old_value, **kwargs):
131         delete = list()
132         change = dict()
133         for key, val in kwargs.iteritems():
134             if not key.startswith(name):
135                 continue
136             n = key[len(name):]
137             if len(n) == 0 or n[0] != ' ':
138                 continue
139             try:
140                 index, field = n[1:].split('-')
141             except ValueError:
142                 continue
143             if field == 'delete':
144                 delete.append(int(index))
145             else:
146                 i = int(index)
147                 if i not in change:
148                     change[i] = dict()
149                 change[i][field] = val
150
151         if len(delete) == 0 and len(change) == 0:
152             return None
153
154         value = old_value
155
156         # remove unwanted changes
157         for i in delete:
158             if i in change:
159                 del change[i]
160
161         # perform requested changes
162         for index, fields in change.iteritems():
163             for k in 'from', 'to':
164                 if k in fields:
165                     val = fields[k]
166                     val_list = val.split('/')
167                     stripped = list()
168                     for v in val_list:
169                         stripped.append(v.strip())
170                     if len(stripped) == 1:
171                         stripped = stripped[0]
172                     if len(value) <= index:
173                         value.extend([None]*(index + 1 - len(value)))
174                     if value[index] is None:
175                         value[index] = [None, None]
176                     if k == 'from':
177                         i = 0
178                     else:
179                         i = 1
180                     value[index][i] = stripped
181
182             # eliminate incomplete/incorrect entries
183             if value[index] is not None:
184                 if ((len(value[index]) != 2 or
185                      value[index][0] is None or
186                      len(value[index][0]) == 0 or
187                      value[index][1] is None or
188                      len(value[index][1]) == 0)):
189                     value[index] = None
190
191         # the previous loop may add 'None' entries
192         # if any still exists mark them to be deleted
193         for i in xrange(0, len(value)):
194             if value[i] is None:
195                 delete.append(i)
196
197         # remove duplicates and set in reverse order
198         delete = list(set(delete))
199         delete.sort(reverse=True)
200
201         for i in delete:
202             if len(value) > i:
203                 del value[i]
204
205         if len(value) == 0:
206             value = None
207         return value
208
209     @admin_protect
210     def GET(self, *args, **kwargs):
211         return self.root_with_msg()
212
213     @admin_protect
214     def POST(self, *args, **kwargs):
215
216         if self._po.is_readonly:
217             return self.root_with_msg(
218                 message="Configuration is marked Read-Only",
219                 message_type=ADMIN_STATUS_WARN)
220
221         message = "Nothing was modified."
222         message_type = "info"
223         new_db_values = dict()
224
225         conf = self._po.get_config_obj()
226
227         for name, option in conf.iteritems():
228             if name in kwargs:
229                 value = kwargs[name]
230                 if isinstance(option, pconfig.List):
231                     value = [x.strip() for x in value.split('\n')]
232                 elif isinstance(option, pconfig.Condition):
233                     value = True
234             else:
235                 if isinstance(option, pconfig.Condition):
236                     value = False
237                 elif isinstance(option, pconfig.Choice):
238                     value = list()
239                     for a in option.get_allowed():
240                         aname = '%s_%s' % (name, a)
241                         if aname in kwargs:
242                             value.append(a)
243                 elif type(option) is pconfig.ComplexList:
244                     value = self.get_complex_list_value(name,
245                                                         option.get_value(),
246                                                         **kwargs)
247                     if value is None:
248                         continue
249                 elif type(option) is pconfig.MappingList:
250                     value = self.get_mapping_list_value(name,
251                                                         option.get_value(),
252                                                         **kwargs)
253                     if value is None:
254                         continue
255                 else:
256                     continue
257
258             if value != option.get_value():
259                 cherrypy.log.error("Storing [%s]: %s = %s" %
260                                    (self._po.name, name, value))
261             option.set_value(value)
262             new_db_values[name] = option.export_value()
263
264         if len(new_db_values) != 0:
265             # First we try to save in the database
266             try:
267                 self._po.save_plugin_config(new_db_values)
268                 message = "New configuration saved."
269                 message_type = ADMIN_STATUS_OK
270             except Exception:  # pylint: disable=broad-except
271                 message = "Failed to save data!"
272                 message_type = ADMIN_STATUS_ERROR
273
274             # Then refresh the actual objects
275             self._po.refresh_plugin_config()
276
277         return self.root_with_msg(message=message,
278                                   message_type=message_type)
279
280
281 class AdminPluginsOrder(AdminPage):
282
283     def __init__(self, site, parent, facility):
284         super(AdminPluginsOrder, self).__init__(site, form=True)
285         self.parent = parent
286         self.facility = facility
287         self.url = '%s/order' % parent.url
288         self.menu = [parent]
289
290     @admin_protect
291     def GET(self, *args, **kwargs):
292         return self.parent.root_with_msg()
293
294     @admin_protect
295     def POST(self, *args, **kwargs):
296
297         if self._site[self.facility].is_readonly:
298             return self.parent.root_with_msg(
299                 message="Configuration is marked Read-Only",
300                 message_type=ADMIN_STATUS_WARN)
301
302         message = "Nothing was modified."
303         message_type = "info"
304         changed = None
305         cur_enabled = self._site[self.facility].enabled
306
307         if 'order' in kwargs:
308             order = kwargs['order'].split(',')
309             if len(order) != 0:
310                 new_order = []
311                 try:
312                     for v in order:
313                         val = v.strip()
314                         if val not in cur_enabled:
315                             error = "Invalid plugin name: %s" % val
316                             raise ValueError(error)
317                         new_order.append(val)
318                     if len(new_order) < len(cur_enabled):
319                         for val in cur_enabled:
320                             if val not in new_order:
321                                 new_order.append(val)
322
323                     self.parent.save_enabled_plugins(new_order)
324
325                     # When all is saved update also live config. The
326                     # live config is the ordered list of plugin names.
327                     self._site[self.facility].refresh_enabled()
328
329                     message = "New configuration saved."
330                     message_type = ADMIN_STATUS_OK
331
332                     changed = dict()
333                     self.debug('%s -> %s' % (cur_enabled, new_order))
334                     for i in range(0, len(cur_enabled)):
335                         if cur_enabled[i] != new_order[i]:
336                             changed[cur_enabled[i]] = 'reordered'
337
338                 except ValueError, e:
339                     message = str(e)
340                     message_type = ADMIN_STATUS_ERROR
341
342                 except Exception, e:  # pylint: disable=broad-except
343                     message = "Failed to save data!"
344                     message_type = ADMIN_STATUS_ERROR
345
346         return self.parent.root_with_msg(message=message,
347                                          message_type=message_type,
348                                          changed=changed)
349
350
351 class AdminPlugins(AdminPage):
352     def __init__(self, name, site, parent, facility, ordered=True):
353         super(AdminPlugins, self).__init__(site)
354         self._master = parent
355         self.name = name
356         self.title = '%s plugins' % name
357         self.url = '%s/%s' % (parent.url, name)
358         self.facility = facility
359         self.template = 'admin/plugins.html'
360         self.order = None
361         parent.add_subtree(name, self)
362
363         for plugin in self._site[facility].available:
364             cherrypy.log.error('Admin info plugin: %s' % plugin)
365             obj = self._site[facility].available[plugin]
366             page = AdminPluginConfig(obj, self._site, self)
367             if hasattr(obj, 'admin'):
368                 obj.admin.mount(page)
369             self.add_subtree(plugin, page)
370
371         if ordered:
372             self.order = AdminPluginsOrder(self._site, self, facility)
373
374     def save_enabled_plugins(self, names):
375         self._site[self.facility].save_enabled(names)
376
377     def root_with_msg(self, message=None, message_type=None, changed=None):
378         plugins = self._site[self.facility]
379
380         if changed is None:
381             changed = dict()
382
383         targs = {'title': self.title,
384                  'menu': self._master.menu,
385                  'message': message,
386                  'message_type': message_type,
387                  'available': plugins.available,
388                  'enabled': plugins.enabled,
389                  'changed': changed,
390                  'baseurl': self.url,
391                  'newurl': self.url}
392         if self.order:
393             targs['order_name'] = '%s_order_form' % self.name
394             targs['order_action'] = self.order.url
395
396         # pylint: disable=star-args
397         return self._template(self.template, **targs)
398
399     def root(self, *args, **kwargs):
400         return self.root_with_msg()
401
402     def _get_plugin_obj(self, plugin):
403         plugins = self._site[self.facility]
404         if plugins.is_readonly:
405             msg = "Configuration is marked Read-Only"
406             raise AdminError(msg)
407         if plugin not in plugins.available:
408             msg = "Unknown plugin %s" % plugin
409             raise AdminError(msg)
410         obj = plugins.available[plugin]
411         if obj.is_readonly:
412             msg = "Plugin Configuration is marked Read-Only"
413             raise AdminError(msg)
414         return obj
415
416     @admin_protect
417     def enable(self, plugin):
418         msg = None
419         try:
420             obj = self._get_plugin_obj(plugin)
421         except AdminError, e:
422             return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
423         if not obj.is_enabled:
424             obj.enable()
425             obj.save_enabled_state()
426             msg = "Plugin %s enabled" % obj.name
427         return self.root_with_msg(msg, ADMIN_STATUS_OK,
428                                   changed={obj.name: 'enabled'})
429     enable.public_function = True
430
431     @admin_protect
432     def disable(self, plugin):
433         msg = None
434         try:
435             obj = self._get_plugin_obj(plugin)
436         except AdminError, e:
437             return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
438         if obj.is_enabled:
439             obj.disable()
440             obj.save_enabled_state()
441             msg = "Plugin %s disabled" % obj.name
442         return self.root_with_msg(msg, ADMIN_STATUS_OK,
443                                   changed={obj.name: 'disabled'})
444     disable.public_function = True
445
446
447 class Admin(AdminPage):
448
449     def __init__(self, site, mount):
450         super(Admin, self).__init__(site)
451         self.title = 'Home'
452         self.mount = mount
453         self.url = '%s/%s' % (self.basepath, mount)
454         self.menu = [self]
455
456     def root(self, *args, **kwargs):
457         return self._template('admin/index.html',
458                               title='Configuration',
459                               baseurl=self.url,
460                               menu=self.menu)
461
462     def add_subtree(self, name, page):
463         self.__dict__[name] = page
464         self.menu.append(page)
465
466     def del_subtree(self, name):
467         self.menu.remove(self.__dict__[name])
468         del self.__dict__[name]
469
470     def get_menu_urls(self):
471         urls = dict()
472         for item in self.menu:
473             name = getattr(item, 'name', None)
474             if name:
475                 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
476                                                                  name))
477         return urls
478
479     @admin_protect
480     def scheme(self):
481         cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
482         urls = self.get_menu_urls()
483         # pylint: disable=star-args
484         return str(self._template('admin/ipsilon-scheme.svg', **urls))
485     scheme.public_function = True