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.auth_protect = True
45 class AdminPluginConfig(AdminPage):
47 def __init__(self, po, site, parent):
48 super(AdminPluginConfig, self).__init__(site, form=True)
50 self.title = '%s plugin' % po.name
51 self.url = '%s/%s' % (parent.url, po.name)
52 self.facility = parent.facility
54 self.back = parent.url
56 def root_with_msg(self, message=None, message_type=None):
57 return self._template('admin/option_config.html', title=self.title,
58 menu=self.menu, action=self.url, back=self.back,
59 message=message, message_type=message_type,
60 name='admin_%s_%s_form' % (self.facility,
62 config=self._po.get_config_obj())
65 def GET(self, *args, **kwargs):
66 return self.root_with_msg()
69 def POST(self, *args, **kwargs):
71 if self._po.is_readonly:
72 return self.root_with_msg(
73 message="Configuration is marked Read-Only",
74 message_type=ADMIN_STATUS_WARN)
76 message = "Nothing was modified."
78 new_db_values = dict()
80 conf = self._po.get_config_obj()
82 for name, option in conf.iteritems():
85 if isinstance(option, pconfig.List):
86 value = [x.strip() for x in value.split('\n')]
87 elif isinstance(option, pconfig.Condition):
90 if isinstance(option, pconfig.Condition):
92 elif isinstance(option, pconfig.Choice):
94 for a in option.get_allowed():
95 aname = '%s_%s' % (name, a)
98 elif type(option) is pconfig.ComplexList:
99 value = get_complex_list_value(name,
104 elif type(option) is pconfig.MappingList:
105 value = get_mapping_list_value(name,
113 if value != option.get_value():
114 cherrypy.log.error("Storing [%s]: %s = %s" %
115 (self._po.name, name, value))
116 option.set_value(value)
117 new_db_values[name] = option.export_value()
119 if len(new_db_values) != 0:
120 # First we try to save in the database
122 self._po.save_plugin_config(new_db_values)
123 message = "New configuration saved."
124 message_type = ADMIN_STATUS_OK
125 except Exception as e: # pylint: disable=broad-except
126 self.error('Failed to save data: %s' % e)
127 message = "Failed to save data!"
128 message_type = ADMIN_STATUS_ERROR
130 # Then refresh the actual objects
131 self._po.refresh_plugin_config()
133 return self.root_with_msg(message=message,
134 message_type=message_type)
137 class AdminPluginsOrder(AdminPage):
139 def __init__(self, site, parent, facility):
140 super(AdminPluginsOrder, self).__init__(site, form=True)
142 self.facility = facility
143 self.url = '%s/order' % parent.url
147 def GET(self, *args, **kwargs):
148 return self.parent.root_with_msg()
151 def POST(self, *args, **kwargs):
153 if self._site[self.facility].is_readonly:
154 return self.parent.root_with_msg(
155 message="Configuration is marked Read-Only",
156 message_type=ADMIN_STATUS_WARN)
158 message = "Nothing was modified."
159 message_type = "info"
161 cur_enabled = self._site[self.facility].enabled
163 if 'order' in kwargs:
164 order = kwargs['order'].split(',')
170 if val not in cur_enabled:
171 error = "Invalid plugin name: %s" % val
172 raise ValueError(error)
173 new_order.append(val)
174 if len(new_order) < len(cur_enabled):
175 for val in cur_enabled:
176 if val not in new_order:
177 new_order.append(val)
179 self.parent.save_enabled_plugins(new_order)
181 # When all is saved update also live config. The
182 # live config is the ordered list of plugin names.
183 self._site[self.facility].refresh_enabled()
185 message = "New configuration saved."
186 message_type = ADMIN_STATUS_OK
189 self.debug('%s -> %s' % (cur_enabled, new_order))
190 for i in range(0, len(cur_enabled)):
191 if cur_enabled[i] != new_order[i]:
192 changed[cur_enabled[i]] = 'reordered'
194 except ValueError, e:
196 message_type = ADMIN_STATUS_ERROR
198 except Exception as e: # pylint: disable=broad-except
199 self.error('Failed to save data: %s' % e)
200 message = "Failed to save data!"
201 message_type = ADMIN_STATUS_ERROR
203 return self.parent.root_with_msg(message=message,
204 message_type=message_type,
208 class AdminPlugins(AdminPage):
209 def __init__(self, name, site, parent, facility, ordered=True):
210 super(AdminPlugins, self).__init__(site)
211 self._master = parent
213 self.title = '%s plugins' % name
214 self.url = '%s/%s' % (parent.url, name)
215 self.facility = facility
216 self.template = 'admin/plugins.html'
218 parent.add_subtree(name, self)
220 for plugin in self._site[facility].available:
221 cherrypy.log.error('Admin info plugin: %s' % plugin)
222 obj = self._site[facility].available[plugin]
223 page = AdminPluginConfig(obj, self._site, self)
224 if hasattr(obj, 'admin'):
225 obj.admin.mount(page)
226 self.add_subtree(plugin, page)
229 self.order = AdminPluginsOrder(self._site, self, facility)
231 def save_enabled_plugins(self, names):
232 self._site[self.facility].save_enabled(names)
234 def root_with_msg(self, message=None, message_type=None, changed=None):
235 plugins = self._site[self.facility]
240 targs = {'title': self.title,
241 'menu': self._master.menu,
243 'message_type': message_type,
244 'available': plugins.available,
245 'enabled': plugins.enabled,
250 targs['order_name'] = '%s_order_form' % self.name
251 targs['order_action'] = self.order.url
253 # pylint: disable=star-args
254 return self._template(self.template, **targs)
256 def root(self, *args, **kwargs):
257 return self.root_with_msg()
259 def _get_plugin_obj(self, plugin):
260 plugins = self._site[self.facility]
261 if plugins.is_readonly:
262 msg = "Configuration is marked Read-Only"
263 raise AdminError(msg)
264 if plugin not in plugins.available:
265 msg = "Unknown plugin %s" % plugin
266 raise AdminError(msg)
267 obj = plugins.available[plugin]
269 msg = "Plugin Configuration is marked Read-Only"
270 raise AdminError(msg)
274 def enable(self, plugin):
277 obj = self._get_plugin_obj(plugin)
278 except AdminError, e:
279 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
280 if not obj.is_enabled:
282 obj.save_enabled_state()
283 msg = "Plugin %s enabled" % obj.name
284 return self.root_with_msg(msg, ADMIN_STATUS_OK,
285 changed={obj.name: 'enabled'})
286 enable.public_function = True
289 def disable(self, plugin):
292 obj = self._get_plugin_obj(plugin)
293 except AdminError, e:
294 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
297 obj.save_enabled_state()
298 msg = "Plugin %s disabled" % obj.name
299 return self.root_with_msg(msg, ADMIN_STATUS_OK,
300 changed={obj.name: 'disabled'})
301 disable.public_function = True
304 class Admin(AdminPage):
306 def __init__(self, site, mount):
307 super(Admin, self).__init__(site)
310 self.url = '%s/%s' % (self.basepath, mount)
313 def root(self, *args, **kwargs):
314 return self._template('admin/index.html',
315 title='Configuration',
319 def add_subtree(self, name, page):
320 self.__dict__[name] = page
321 self.menu.append(page)
323 def del_subtree(self, name):
324 self.menu.remove(self.__dict__[name])
325 del self.__dict__[name]
327 def get_menu_urls(self):
329 for item in self.menu:
330 name = getattr(item, 'name', None)
332 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
338 cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
339 urls = self.get_menu_urls()
340 # pylint: disable=star-args
341 return str(self._template('admin/ipsilon-scheme.svg', **urls))
342 scheme.public_function = True
345 def get_complex_list_value(name, old_value, **kwargs):
348 for key, val in kwargs.iteritems():
349 if not key.startswith(name):
352 if len(n) == 0 or n[0] != ' ':
355 index, field = n[1:].split('-')
358 if field == 'delete':
359 delete.append(int(index))
360 elif field == 'name':
361 change[int(index)] = val
363 if len(delete) == 0 and len(change) == 0:
368 # remove unwanted changes
373 # perform requested changes
374 for index, val in change.iteritems():
375 val_list = val.split('/')
378 stripped.append(v.strip())
379 if len(stripped) == 1:
380 stripped = stripped[0]
381 if len(value) <= index:
382 value.extend([None]*(index + 1 - len(value)))
383 value[index] = stripped
385 if len(value[index]) == 0:
388 # the previous loop may add 'None' entries
389 # if any still exists mark them to be deleted
390 for i in xrange(0, len(value)):
394 # remove duplicates and set in reverse order
395 delete = list(set(delete))
396 delete.sort(reverse=True)
407 def get_mapping_list_value(name, old_value, **kwargs):
410 for key, val in kwargs.iteritems():
411 if not key.startswith(name):
414 if len(n) == 0 or n[0] != ' ':
417 index, field = n[1:].split('-')
420 if field == 'delete':
421 delete.append(int(index))
426 change[i][field] = val
428 if len(delete) == 0 and len(change) == 0:
433 # remove unwanted changes
438 # perform requested changes
439 for index, fields in change.iteritems():
440 for k in 'from', 'to':
443 val_list = val.split('/')
446 stripped.append(v.strip())
447 if len(stripped) == 1:
448 stripped = stripped[0]
449 if len(value) <= index:
450 value.extend([None]*(index + 1 - len(value)))
451 if value[index] is None:
452 value[index] = [None, None]
457 value[index][i] = stripped
459 # eliminate incomplete/incorrect entries
460 if value[index] is not None:
461 if ((len(value[index]) != 2 or
462 value[index][0] is None or
463 len(value[index][0]) == 0 or
464 value[index][1] is None or
465 len(value[index][1]) == 0)):
468 # the previous loop may add 'None' entries
469 # if any still exists mark them to be deleted
470 for i in xrange(0, len(value)):
474 # remove duplicates and set in reverse order
475 delete = list(set(delete))
476 delete.sort(reverse=True)