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())
70 def GET(self, *args, **kwargs):
71 return self.root_with_msg()
74 def POST(self, *args, **kwargs):
76 if self._po.is_readonly:
77 return self.root_with_msg(
78 message="Configuration is marked Read-Only",
79 message_type=ADMIN_STATUS_WARN)
81 message = "Nothing was modified."
83 new_db_values = dict()
85 conf = self._po.get_config_obj()
87 for name, option in conf.iteritems():
90 if isinstance(option, pconfig.List):
91 value = [x.strip() for x in value.split('\n')]
92 elif isinstance(option, pconfig.Condition):
95 if isinstance(option, pconfig.Condition):
97 elif isinstance(option, pconfig.Choice):
99 for a in option.get_allowed():
100 aname = '%s_%s' % (name, a)
106 if value != option.get_value():
107 cherrypy.log.error("Storing [%s]: %s = %s" %
108 (self._po.name, name, value))
109 option.set_value(value)
110 new_db_values[name] = option.export_value()
112 if len(new_db_values) != 0:
113 # First we try to save in the database
115 self._po.save_plugin_config(new_db_values)
116 message = "New configuration saved."
117 message_type = ADMIN_STATUS_OK
118 except Exception: # pylint: disable=broad-except
119 message = "Failed to save data!"
120 message_type = ADMIN_STATUS_ERROR
122 # Then refresh the actual objects
123 self._po.refresh_plugin_config()
125 return self.root_with_msg(message=message,
126 message_type=message_type)
129 class AdminPluginsOrder(AdminPage):
131 def __init__(self, site, parent, facility):
132 super(AdminPluginsOrder, self).__init__(site, form=True)
134 self.facility = facility
135 self.url = '%s/order' % parent.url
139 def GET(self, *args, **kwargs):
140 return self.parent.root_with_msg()
143 def POST(self, *args, **kwargs):
145 if self._site[self.facility].is_readonly:
146 return self.parent.root_with_msg(
147 message="Configuration is marked Read-Only",
148 message_type=ADMIN_STATUS_WARN)
150 message = "Nothing was modified."
151 message_type = "info"
153 cur_enabled = self._site[self.facility].enabled
155 if 'order' in kwargs:
156 order = kwargs['order'].split(',')
162 if val not in cur_enabled:
163 error = "Invalid plugin name: %s" % val
164 raise ValueError(error)
165 new_order.append(val)
166 if len(new_order) < len(cur_enabled):
167 for val in cur_enabled:
168 if val not in new_order:
169 new_order.append(val)
171 self.parent.save_enabled_plugins(new_order)
173 # When all is saved update also live config. The
174 # live config is the ordered list of plugin names.
175 self._site[self.facility].refresh_enabled()
177 message = "New configuration saved."
178 message_type = ADMIN_STATUS_OK
181 self.debug('%s -> %s' % (cur_enabled, new_order))
182 for i in range(0, len(cur_enabled)):
183 if cur_enabled[i] != new_order[i]:
184 changed[cur_enabled[i]] = 'reordered'
186 except ValueError, e:
188 message_type = ADMIN_STATUS_ERROR
190 except Exception, e: # pylint: disable=broad-except
191 message = "Failed to save data!"
192 message_type = ADMIN_STATUS_ERROR
194 return self.parent.root_with_msg(message=message,
195 message_type=message_type,
199 class AdminPlugins(AdminPage):
200 def __init__(self, name, site, parent, facility, ordered=True):
201 super(AdminPlugins, self).__init__(site)
202 self._master = parent
204 self.title = '%s plugins' % name
205 self.url = '%s/%s' % (parent.url, name)
206 self.facility = facility
207 self.template = 'admin/plugins.html'
209 parent.add_subtree(name, self)
211 for plugin in self._site[facility].available:
212 cherrypy.log.error('Admin info plugin: %s' % plugin)
213 obj = self._site[facility].available[plugin]
214 page = AdminPluginConfig(obj, self._site, self)
215 if hasattr(obj, 'admin'):
216 obj.admin.mount(page)
217 self.add_subtree(plugin, page)
220 self.order = AdminPluginsOrder(self._site, self, facility)
222 def save_enabled_plugins(self, names):
223 self._site[self.facility].save_enabled(names)
225 def root_with_msg(self, message=None, message_type=None, changed=None):
226 plugins = self._site[self.facility]
231 targs = {'title': self.title,
232 'menu': self._master.menu,
234 'message_type': message_type,
235 'available': plugins.available,
236 'enabled': plugins.enabled,
241 targs['order_name'] = '%s_order_form' % self.name
242 targs['order_action'] = self.order.url
244 # pylint: disable=star-args
245 return self._template(self.template, **targs)
247 def root(self, *args, **kwargs):
248 return self.root_with_msg()
250 def _get_plugin_obj(self, plugin):
251 plugins = self._site[self.facility]
252 if plugins.is_readonly:
253 msg = "Configuration is marked Read-Only"
254 raise AdminError(msg)
255 if plugin not in plugins.available:
256 msg = "Unknown plugin %s" % plugin
257 raise AdminError(msg)
258 obj = plugins.available[plugin]
260 msg = "Plugin Configuration is marked Read-Only"
261 raise AdminError(msg)
265 def enable(self, plugin):
268 obj = self._get_plugin_obj(plugin)
269 except AdminError, e:
270 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
271 if not obj.is_enabled:
273 obj.save_enabled_state()
274 msg = "Plugin %s enabled" % obj.name
275 return self.root_with_msg(msg, ADMIN_STATUS_OK,
276 changed={obj.name: 'enabled'})
277 enable.public_function = True
280 def disable(self, plugin):
283 obj = self._get_plugin_obj(plugin)
284 except AdminError, e:
285 return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
288 obj.save_enabled_state()
289 msg = "Plugin %s disabled" % obj.name
290 return self.root_with_msg(msg, ADMIN_STATUS_OK,
291 changed={obj.name: 'disabled'})
292 disable.public_function = True
295 class Admin(AdminPage):
297 def __init__(self, site, mount):
298 super(Admin, self).__init__(site)
301 self.url = '%s/%s' % (self.basepath, mount)
304 def root(self, *args, **kwargs):
305 return self._template('admin/index.html',
306 title='Configuration',
310 def add_subtree(self, name, page):
311 self.__dict__[name] = page
312 self.menu.append(page)
314 def del_subtree(self, name):
315 self.menu.remove(self.__dict__[name])
316 del self.__dict__[name]
318 def get_menu_urls(self):
320 for item in self.menu:
321 name = getattr(item, 'name', None)
323 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
329 cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
330 urls = self.get_menu_urls()
331 # pylint: disable=star-args
332 return str(self._template('admin/ipsilon-scheme.svg', **urls))
333 scheme.public_function = True