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/>.
20 from ipsilon.util.page import Page
21 from ipsilon.util.page import admin_protect
22 from ipsilon.util.endpoint import allow_iframe
23 from ipsilon.util import config as pconfig
26 ADMIN_STATUS_OK = "success"
27 ADMIN_STATUS_ERROR = "danger"
28 ADMIN_STATUS_WARN = "warning"
31 class AdminError(Exception):
32 def __init__(self, message):
33 super(AdminError, self).__init__(message)
34 self.message = message
37 return str(self.message)
40 class AdminPage(Page):
42 def __init__(self, *args, **kwargs):
43 super(AdminPage, self).__init__(*args, **kwargs)
44 self.auth_protect = True
47 class AdminPluginConfig(AdminPage):
49 def __init__(self, po, site, parent):
50 super(AdminPluginConfig, self).__init__(site, form=True)
52 self.title = '%s plugin' % po.name
53 self.url = '%s/%s' % (parent.url, po.name)
54 self.facility = parent.facility
56 self.back = parent.url
58 def root_with_msg(self, message=None, message_type=None):
59 return self._template('admin/option_config.html', title=self.title,
60 menu=self.menu, action=self.url, back=self.back,
61 message=message, message_type=message_type,
62 name='admin_%s_%s_form' % (self.facility,
64 config=self._po.get_config_obj())
67 def GET(self, *args, **kwargs):
68 return self.root_with_msg()
71 def POST(self, *args, **kwargs):
73 if self._po.is_readonly:
74 return self.root_with_msg(
75 message="Configuration is marked Read-Only",
76 message_type=ADMIN_STATUS_WARN)
78 message = "Nothing was modified."
80 new_db_values = dict()
82 conf = self._po.get_config_obj()
84 for name, option in conf.iteritems():
87 if isinstance(option, pconfig.List):
88 value = [x.strip() for x in value.split('\n')]
89 elif isinstance(option, pconfig.Condition):
92 if isinstance(option, pconfig.Condition):
94 elif isinstance(option, pconfig.Choice):
96 for a in option.get_allowed():
97 aname = '%s_%s' % (name, a)
100 elif type(option) is pconfig.ComplexList:
101 value = get_complex_list_value(name,
106 elif type(option) is pconfig.MappingList:
107 value = get_mapping_list_value(name,
115 if value != option.get_value():
116 cherrypy.log.error("Storing [%s]: %s = %s" %
117 (self._po.name, name, value),
118 severity=logging.DEBUG)
119 option.set_value(value)
120 new_db_values[name] = option.export_value()
122 if len(new_db_values) != 0:
123 # First we try to save in the database
125 self._po.save_plugin_config(new_db_values)
126 message = "New configuration saved."
127 message_type = ADMIN_STATUS_OK
128 except Exception as e: # pylint: disable=broad-except
129 self.error('Failed to save data: %s' % e)
130 message = "Failed to save data!"
131 message_type = ADMIN_STATUS_ERROR
133 # Then refresh the actual objects
134 self._po.refresh_plugin_config()
136 return self.root_with_msg(message=message,
137 message_type=message_type)
140 class AdminPluginsOrder(AdminPage):
142 def __init__(self, site, parent, facility):
143 super(AdminPluginsOrder, self).__init__(site, form=True)
145 self.facility = facility
146 self.url = '%s/order' % parent.url
150 def GET(self, *args, **kwargs):
151 return self.parent.root_with_msg()
154 def POST(self, *args, **kwargs):
156 if self._site[self.facility].is_readonly:
157 return self.parent.root_with_msg(
158 message="Configuration is marked Read-Only",
159 message_type=ADMIN_STATUS_WARN)
161 message = "Nothing was modified."
162 message_type = "info"
164 cur_enabled = self._site[self.facility].enabled
166 if 'order' in kwargs:
167 order = kwargs['order'].split(',')
173 if val not in cur_enabled:
174 error = "Invalid plugin name: %s" % val
175 raise ValueError(error)
176 new_order.append(val)
177 if len(new_order) < len(cur_enabled):
178 for val in cur_enabled:
179 if val not in new_order:
180 new_order.append(val)
182 self.parent.save_enabled_plugins(new_order)
184 # When all is saved update also live config. The
185 # live config is the ordered list of plugin names.
186 self._site[self.facility].refresh_enabled()
188 message = "New configuration saved."
189 message_type = ADMIN_STATUS_OK
192 self.debug('%s -> %s' % (cur_enabled, new_order))
193 for i in range(0, len(cur_enabled)):
194 if cur_enabled[i] != new_order[i]:
195 changed[cur_enabled[i]] = 'reordered'
197 except ValueError, e:
199 message_type = ADMIN_STATUS_ERROR
201 except Exception as e: # pylint: disable=broad-except
202 self.error('Failed to save data: %s' % e)
203 message = "Failed to save data!"
204 message_type = ADMIN_STATUS_ERROR
206 return self.parent.root_with_msg(message=message,
207 message_type=message_type,
211 class AdminPlugins(AdminPage):
212 def __init__(self, name, site, parent, facility, ordered=True):
213 super(AdminPlugins, self).__init__(site)
214 self._master = parent
216 self.title = '%s plugins' % name
217 self.url = '%s/%s' % (parent.url, name)
218 self.facility = facility
219 self.template = 'admin/plugins.html'
221 parent.add_subtree(name, self)
223 if self._site[facility] is None:
226 for plugin in self._site[facility].available:
227 cherrypy.log.error('Admin info plugin: %s' % plugin,
228 severity=logging.DEBUG)
229 obj = self._site[facility].available[plugin]
230 page = AdminPluginConfig(obj, self._site, self)
231 if hasattr(obj, 'admin'):
232 obj.admin.mount(page)
233 self.add_subtree(plugin, page)
236 self.order = AdminPluginsOrder(self._site, self, facility)
238 def save_enabled_plugins(self, names):
239 self._site[self.facility].save_enabled(names)
241 def root_with_msg(self, message=None, message_type=None, changed=None):
242 plugins = self._site[self.facility]
247 targs = {'title': self.title,
248 'menu': self._master.menu,
250 'message_type': message_type,
251 'available': plugins.available,
252 'enabled': plugins.enabled,
257 targs['order_name'] = '%s_order_form' % self.name
258 targs['order_action'] = self.order.url
260 # pylint: disable=star-args
261 return self._template(self.template, **targs)
263 def root(self, *args, **kwargs):
264 return self.root_with_msg()
266 def _get_plugin_obj(self, plugin):
267 plugins = self._site[self.facility]
268 if plugins.is_readonly:
269 msg = "Configuration is marked Read-Only"
270 raise AdminError(msg)
271 if plugin not in plugins.available:
272 msg = "Unknown plugin %s" % plugin
273 raise AdminError(msg)
274 obj = plugins.available[plugin]
276 msg = "Plugin Configuration is marked Read-Only"
277 raise AdminError(msg)
281 def enable(self, plugin):
284 obj = self._get_plugin_obj(plugin)
285 except AdminError, e:
286 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
287 if not obj.is_enabled:
290 except Exception as e: # pylint: disable=broad-except
291 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
292 obj.save_enabled_state()
293 msg = "Plugin %s enabled" % obj.name
294 return self.root_with_msg(msg, ADMIN_STATUS_OK,
295 changed={obj.name: 'enabled'})
296 enable.public_function = True
299 def disable(self, plugin):
302 obj = self._get_plugin_obj(plugin)
303 except AdminError, e:
304 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
308 except Exception as e: # pylint: disable=broad-except
309 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
310 obj.save_enabled_state()
311 msg = "Plugin %s disabled" % obj.name
312 return self.root_with_msg(msg, ADMIN_STATUS_OK,
313 changed={obj.name: 'disabled'})
314 disable.public_function = True
317 class Admin(AdminPage):
319 def __init__(self, site, mount):
320 super(Admin, self).__init__(site)
323 self.url = '%s/%s' % (self.basepath, mount)
326 def root(self, *args, **kwargs):
327 return self._template('admin/index.html',
328 title='Configuration',
332 def add_subtree(self, name, page):
333 self.__dict__[name] = page
334 self.menu.append(page)
336 def del_subtree(self, name):
337 self.menu.remove(self.__dict__[name])
338 del self.__dict__[name]
340 def get_menu_urls(self):
342 for item in self.menu:
343 name = getattr(item, 'name', None)
345 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
352 cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
353 urls = self.get_menu_urls()
354 # pylint: disable=star-args
355 return str(self._template('admin/ipsilon-scheme.svg', **urls))
356 scheme.public_function = True
359 def get_complex_list_value(name, old_value, **kwargs):
362 for key, val in kwargs.iteritems():
363 if not key.startswith(name):
366 if len(n) == 0 or n[0] != ' ':
369 index, field = n[1:].split('-')
372 if field == 'delete':
373 delete.append(int(index))
374 elif field == 'name':
375 change[int(index)] = val
377 if len(delete) == 0 and len(change) == 0:
382 # remove unwanted changes
387 # perform requested changes
388 for index, val in change.iteritems():
389 val_list = val.split('/')
392 stripped.append(v.strip())
393 if len(stripped) == 1:
394 stripped = stripped[0]
395 if len(value) <= index:
396 value.extend([None]*(index + 1 - len(value)))
397 value[index] = stripped
399 if len(value[index]) == 0:
402 # the previous loop may add 'None' entries
403 # if any still exists mark them to be deleted
404 for i in xrange(0, len(value)):
408 # remove duplicates and set in reverse order
409 delete = list(set(delete))
410 delete.sort(reverse=True)
421 def get_mapping_list_value(name, old_value, **kwargs):
424 for key, val in kwargs.iteritems():
425 if not key.startswith(name):
428 if len(n) == 0 or n[0] != ' ':
431 index, field = n[1:].split('-')
434 if field == 'delete':
435 delete.append(int(index))
440 change[i][field] = val
442 if len(delete) == 0 and len(change) == 0:
447 # remove unwanted changes
452 # perform requested changes
453 for index, fields in change.iteritems():
454 for k in 'from', 'to':
457 val_list = val.split('/')
460 stripped.append(v.strip())
461 if len(stripped) == 1:
462 stripped = stripped[0]
463 if len(value) <= index:
464 value.extend([None]*(index + 1 - len(value)))
465 if value[index] is None:
466 value[index] = [None, None]
471 value[index][i] = stripped
473 # eliminate incomplete/incorrect entries
474 if value[index] is not None:
475 if ((len(value[index]) != 2 or
476 value[index][0] is None or
477 len(value[index][0]) == 0 or
478 value[index][1] is None or
479 len(value[index][1]) == 0)):
482 # the previous loop may add 'None' entries
483 # if any still exists mark them to be deleted
484 for i in xrange(0, len(value)):
488 # remove duplicates and set in reverse order
489 delete = list(set(delete))
490 delete.sort(reverse=True)