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)
131 def GET(self, *args, **kwargs):
132 return self.root_with_msg()
135 def POST(self, *args, **kwargs):
137 if self._po.is_readonly:
138 return self.root_with_msg(
139 message="Configuration is marked Read-Only",
140 message_type=ADMIN_STATUS_WARN)
142 message = "Nothing was modified."
143 message_type = "info"
144 new_db_values = dict()
146 conf = self._po.get_config_obj()
148 for name, option in conf.iteritems():
151 if isinstance(option, pconfig.List):
152 value = [x.strip() for x in value.split('\n')]
153 elif isinstance(option, pconfig.Condition):
156 if isinstance(option, pconfig.Condition):
158 elif isinstance(option, pconfig.Choice):
160 for a in option.get_allowed():
161 aname = '%s_%s' % (name, a)
164 elif type(option) is pconfig.ComplexList:
165 value = self.get_complex_list_value(name,
173 if value != option.get_value():
174 cherrypy.log.error("Storing [%s]: %s = %s" %
175 (self._po.name, name, value))
176 option.set_value(value)
177 new_db_values[name] = option.export_value()
179 if len(new_db_values) != 0:
180 # First we try to save in the database
182 self._po.save_plugin_config(new_db_values)
183 message = "New configuration saved."
184 message_type = ADMIN_STATUS_OK
185 except Exception: # pylint: disable=broad-except
186 message = "Failed to save data!"
187 message_type = ADMIN_STATUS_ERROR
189 # Then refresh the actual objects
190 self._po.refresh_plugin_config()
192 return self.root_with_msg(message=message,
193 message_type=message_type)
196 class AdminPluginsOrder(AdminPage):
198 def __init__(self, site, parent, facility):
199 super(AdminPluginsOrder, self).__init__(site, form=True)
201 self.facility = facility
202 self.url = '%s/order' % parent.url
206 def GET(self, *args, **kwargs):
207 return self.parent.root_with_msg()
210 def POST(self, *args, **kwargs):
212 if self._site[self.facility].is_readonly:
213 return self.parent.root_with_msg(
214 message="Configuration is marked Read-Only",
215 message_type=ADMIN_STATUS_WARN)
217 message = "Nothing was modified."
218 message_type = "info"
220 cur_enabled = self._site[self.facility].enabled
222 if 'order' in kwargs:
223 order = kwargs['order'].split(',')
229 if val not in cur_enabled:
230 error = "Invalid plugin name: %s" % val
231 raise ValueError(error)
232 new_order.append(val)
233 if len(new_order) < len(cur_enabled):
234 for val in cur_enabled:
235 if val not in new_order:
236 new_order.append(val)
238 self.parent.save_enabled_plugins(new_order)
240 # When all is saved update also live config. The
241 # live config is the ordered list of plugin names.
242 self._site[self.facility].refresh_enabled()
244 message = "New configuration saved."
245 message_type = ADMIN_STATUS_OK
248 self.debug('%s -> %s' % (cur_enabled, new_order))
249 for i in range(0, len(cur_enabled)):
250 if cur_enabled[i] != new_order[i]:
251 changed[cur_enabled[i]] = 'reordered'
253 except ValueError, e:
255 message_type = ADMIN_STATUS_ERROR
257 except Exception, e: # pylint: disable=broad-except
258 message = "Failed to save data!"
259 message_type = ADMIN_STATUS_ERROR
261 return self.parent.root_with_msg(message=message,
262 message_type=message_type,
266 class AdminPlugins(AdminPage):
267 def __init__(self, name, site, parent, facility, ordered=True):
268 super(AdminPlugins, self).__init__(site)
269 self._master = parent
271 self.title = '%s plugins' % name
272 self.url = '%s/%s' % (parent.url, name)
273 self.facility = facility
274 self.template = 'admin/plugins.html'
276 parent.add_subtree(name, self)
278 for plugin in self._site[facility].available:
279 cherrypy.log.error('Admin info plugin: %s' % plugin)
280 obj = self._site[facility].available[plugin]
281 page = AdminPluginConfig(obj, self._site, self)
282 if hasattr(obj, 'admin'):
283 obj.admin.mount(page)
284 self.add_subtree(plugin, page)
287 self.order = AdminPluginsOrder(self._site, self, facility)
289 def save_enabled_plugins(self, names):
290 self._site[self.facility].save_enabled(names)
292 def root_with_msg(self, message=None, message_type=None, changed=None):
293 plugins = self._site[self.facility]
298 targs = {'title': self.title,
299 'menu': self._master.menu,
301 'message_type': message_type,
302 'available': plugins.available,
303 'enabled': plugins.enabled,
308 targs['order_name'] = '%s_order_form' % self.name
309 targs['order_action'] = self.order.url
311 # pylint: disable=star-args
312 return self._template(self.template, **targs)
314 def root(self, *args, **kwargs):
315 return self.root_with_msg()
317 def _get_plugin_obj(self, plugin):
318 plugins = self._site[self.facility]
319 if plugins.is_readonly:
320 msg = "Configuration is marked Read-Only"
321 raise AdminError(msg)
322 if plugin not in plugins.available:
323 msg = "Unknown plugin %s" % plugin
324 raise AdminError(msg)
325 obj = plugins.available[plugin]
327 msg = "Plugin Configuration is marked Read-Only"
328 raise AdminError(msg)
332 def enable(self, plugin):
335 obj = self._get_plugin_obj(plugin)
336 except AdminError, e:
337 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
338 if not obj.is_enabled:
340 obj.save_enabled_state()
341 msg = "Plugin %s enabled" % obj.name
342 return self.root_with_msg(msg, ADMIN_STATUS_OK,
343 changed={obj.name: 'enabled'})
344 enable.public_function = True
347 def disable(self, plugin):
350 obj = self._get_plugin_obj(plugin)
351 except AdminError, e:
352 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
355 obj.save_enabled_state()
356 msg = "Plugin %s disabled" % obj.name
357 return self.root_with_msg(msg, ADMIN_STATUS_OK,
358 changed={obj.name: 'disabled'})
359 disable.public_function = True
362 class Admin(AdminPage):
364 def __init__(self, site, mount):
365 super(Admin, self).__init__(site)
368 self.url = '%s/%s' % (self.basepath, mount)
371 def root(self, *args, **kwargs):
372 return self._template('admin/index.html',
373 title='Configuration',
377 def add_subtree(self, name, page):
378 self.__dict__[name] = page
379 self.menu.append(page)
381 def del_subtree(self, name):
382 self.menu.remove(self.__dict__[name])
383 del self.__dict__[name]
385 def get_menu_urls(self):
387 for item in self.menu:
388 name = getattr(item, 'name', None)
390 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
396 cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
397 urls = self.get_menu_urls()
398 # pylint: disable=star-args
399 return str(self._template('admin/ipsilon-scheme.svg', **urls))
400 scheme.public_function = True