1 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
3 # see file 'COPYING' for use and warranty information
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.
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.
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/>.
19 from ipsilon.util.page import Page
20 from ipsilon.util.page import admin_protect
21 from ipsilon.util import config as pconfig
24 ADMIN_STATUS_OK = "success"
25 ADMIN_STATUS_ERROR = "danger"
26 ADMIN_STATUS_WARN = "warning"
29 class AdminError(Exception):
30 def __init__(self, message):
31 super(AdminError, self).__init__(message)
32 self.message = message
35 return str(self.message)
38 class AdminPage(Page):
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',
45 'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
47 self.auth_protect = True
50 class AdminPluginConfig(AdminPage):
52 def __init__(self, po, site, parent):
53 super(AdminPluginConfig, self).__init__(site, form=True)
55 self.title = '%s plugin' % po.name
56 self.url = '%s/%s' % (parent.url, po.name)
57 self.facility = parent.facility
59 self.back = parent.url
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,
67 config=self._po.get_config_obj())
69 def get_complex_list_value(self, name, old_value, **kwargs):
72 for key, val in kwargs.iteritems():
73 if not key.startswith(name):
76 if len(n) == 0 or n[0] != ' ':
79 index, field = n[1:].split('-')
83 delete.append(int(index))
85 change[int(index)] = val
87 if len(delete) == 0 and len(change) == 0:
92 # remove unwanted changes
97 # perform requested changes
98 for index, val in change.iteritems():
99 val_list = val.split('/')
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
109 if len(value[index]) == 0:
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)):
118 # remove duplicates and set in reverse order
119 delete = list(set(delete))
120 delete.sort(reverse=True)
130 def get_mapping_list_value(self, name, old_value, **kwargs):
133 for key, val in kwargs.iteritems():
134 if not key.startswith(name):
137 if len(n) == 0 or n[0] != ' ':
140 index, field = n[1:].split('-')
143 if field == 'delete':
144 delete.append(int(index))
149 change[i][field] = val
151 if len(delete) == 0 and len(change) == 0:
156 # remove unwanted changes
161 # perform requested changes
162 for index, fields in change.iteritems():
163 for k in 'from', 'to':
166 val_list = val.split('/')
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]
180 value[index][i] = stripped
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)):
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)):
197 # remove duplicates and set in reverse order
198 delete = list(set(delete))
199 delete.sort(reverse=True)
210 def GET(self, *args, **kwargs):
211 return self.root_with_msg()
214 def POST(self, *args, **kwargs):
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)
221 message = "Nothing was modified."
222 message_type = "info"
223 new_db_values = dict()
225 conf = self._po.get_config_obj()
227 for name, option in conf.iteritems():
230 if isinstance(option, pconfig.List):
231 value = [x.strip() for x in value.split('\n')]
232 elif isinstance(option, pconfig.Condition):
235 if isinstance(option, pconfig.Condition):
237 elif isinstance(option, pconfig.Choice):
239 for a in option.get_allowed():
240 aname = '%s_%s' % (name, a)
243 elif type(option) is pconfig.ComplexList:
244 value = self.get_complex_list_value(name,
249 elif type(option) is pconfig.MappingList:
250 value = self.get_mapping_list_value(name,
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()
264 if len(new_db_values) != 0:
265 # First we try to save in the database
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
274 # Then refresh the actual objects
275 self._po.refresh_plugin_config()
277 return self.root_with_msg(message=message,
278 message_type=message_type)
281 class AdminPluginsOrder(AdminPage):
283 def __init__(self, site, parent, facility):
284 super(AdminPluginsOrder, self).__init__(site, form=True)
286 self.facility = facility
287 self.url = '%s/order' % parent.url
291 def GET(self, *args, **kwargs):
292 return self.parent.root_with_msg()
295 def POST(self, *args, **kwargs):
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)
302 message = "Nothing was modified."
303 message_type = "info"
305 cur_enabled = self._site[self.facility].enabled
307 if 'order' in kwargs:
308 order = kwargs['order'].split(',')
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)
323 self.parent.save_enabled_plugins(new_order)
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()
329 message = "New configuration saved."
330 message_type = ADMIN_STATUS_OK
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'
338 except ValueError, e:
340 message_type = ADMIN_STATUS_ERROR
342 except Exception, e: # pylint: disable=broad-except
343 message = "Failed to save data!"
344 message_type = ADMIN_STATUS_ERROR
346 return self.parent.root_with_msg(message=message,
347 message_type=message_type,
351 class AdminPlugins(AdminPage):
352 def __init__(self, name, site, parent, facility, ordered=True):
353 super(AdminPlugins, self).__init__(site)
354 self._master = parent
356 self.title = '%s plugins' % name
357 self.url = '%s/%s' % (parent.url, name)
358 self.facility = facility
359 self.template = 'admin/plugins.html'
361 parent.add_subtree(name, self)
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)
372 self.order = AdminPluginsOrder(self._site, self, facility)
374 def save_enabled_plugins(self, names):
375 self._site[self.facility].save_enabled(names)
377 def root_with_msg(self, message=None, message_type=None, changed=None):
378 plugins = self._site[self.facility]
383 targs = {'title': self.title,
384 'menu': self._master.menu,
386 'message_type': message_type,
387 'available': plugins.available,
388 'enabled': plugins.enabled,
393 targs['order_name'] = '%s_order_form' % self.name
394 targs['order_action'] = self.order.url
396 # pylint: disable=star-args
397 return self._template(self.template, **targs)
399 def root(self, *args, **kwargs):
400 return self.root_with_msg()
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]
412 msg = "Plugin Configuration is marked Read-Only"
413 raise AdminError(msg)
417 def enable(self, plugin):
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:
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
432 def disable(self, plugin):
435 obj = self._get_plugin_obj(plugin)
436 except AdminError, e:
437 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
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
447 class Admin(AdminPage):
449 def __init__(self, site, mount):
450 super(Admin, self).__init__(site)
453 self.url = '%s/%s' % (self.basepath, mount)
456 def root(self, *args, **kwargs):
457 return self._template('admin/index.html',
458 title='Configuration',
462 def add_subtree(self, name, page):
463 self.__dict__[name] = page
464 self.menu.append(page)
466 def del_subtree(self, name):
467 self.menu.remove(self.__dict__[name])
468 del self.__dict__[name]
470 def get_menu_urls(self):
472 for item in self.menu:
473 name = getattr(item, 'name', None)
475 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
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