Refactor plugin initialization and enablement
[cascardo/ipsilon.git] / ipsilon / admin / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import cherrypy
21 from ipsilon.util.page import Page
22 from ipsilon.util.page import admin_protect
23 from ipsilon.util import config as pconfig
24
25
26 class AdminPage(Page):
27
28     def __init__(self, *args, **kwargs):
29         super(AdminPage, self).__init__(*args, **kwargs)
30         self.default_headers.update({
31             'Cache-Control': 'no-cache, must-revalidate',
32             'Pragma': 'no-cache',
33             'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
34         })
35         self.auth_protect = True
36
37
38 class AdminPluginConfig(AdminPage):
39
40     def __init__(self, po, site, parent):
41         super(AdminPluginConfig, self).__init__(site, form=True)
42         self._po = po
43         self.title = '%s plugin' % po.name
44         self.url = '%s/%s' % (parent.url, po.name)
45         self.facility = parent.facility
46         self.menu = [parent]
47         self.back = parent.url
48
49     def root_with_msg(self, message=None, message_type=None):
50         return self._template('admin/plugin_config.html', title=self.title,
51                               menu=self.menu, action=self.url, back=self.back,
52                               message=message, message_type=message_type,
53                               name='admin_%s_%s_form' % (self.facility,
54                                                          self._po.name),
55                               config=self._po.get_config_obj())
56
57     @admin_protect
58     def GET(self, *args, **kwargs):
59         return self.root_with_msg()
60
61     @admin_protect
62     def POST(self, *args, **kwargs):
63
64         message = "Nothing was modified."
65         message_type = "info"
66         new_db_values = dict()
67
68         conf = self._po.get_config_obj()
69
70         for name, option in conf.iteritems():
71             if name in kwargs:
72                 value = kwargs[name]
73                 if isinstance(option, pconfig.List):
74                     value = [x.strip() for x in value.split('\n')]
75                 elif isinstance(option, pconfig.Condition):
76                     value = True
77             else:
78                 if isinstance(option, pconfig.Condition):
79                     value = False
80                 elif isinstance(option, pconfig.Choice):
81                     value = list()
82                     for a in option.get_allowed():
83                         aname = '%s_%s' % (name, a)
84                         if aname in kwargs:
85                             value.append(a)
86                 else:
87                     continue
88
89             if value != option.get_value():
90                 cherrypy.log.error("Storing [%s]: %s = %s" %
91                                    (self._po.name, name, value))
92             option.set_value(value)
93             new_db_values[name] = option.export_value()
94
95         if len(new_db_values) != 0:
96             # First we try to save in the database
97             try:
98                 self._po.save_plugin_config(new_db_values)
99                 message = "New configuration saved."
100                 message_type = "success"
101             except Exception:  # pylint: disable=broad-except
102                 message = "Failed to save data!"
103                 message_type = "error"
104
105             # Then refresh the actual objects
106             self._po.refresh_plugin_config()
107
108         return self.root_with_msg(message=message,
109                                   message_type=message_type)
110
111
112 class AdminPluginsOrder(AdminPage):
113
114     def __init__(self, site, parent, facility):
115         super(AdminPluginsOrder, self).__init__(site, form=True)
116         self.parent = parent
117         self.facility = facility
118         self.url = '%s/order' % parent.url
119         self.menu = [parent]
120
121     @admin_protect
122     def GET(self, *args, **kwargs):
123         return self.parent.root_with_msg()
124
125     def _get_enabled_list(self):
126         cur = list()
127         for p in self._site[self.facility].available.values():
128             if p.is_enabled:
129                 cur.append(p.name)
130         return cur
131
132     @admin_protect
133     def POST(self, *args, **kwargs):
134         message = "Nothing was modified."
135         message_type = "info"
136         cur_enabled = self._get_enabled_list()
137
138         if 'order' in kwargs:
139             order = kwargs['order'].split(',')
140             if len(order) != 0:
141                 new_order = []
142                 try:
143                     for v in order:
144                         val = v.strip()
145                         if val not in cur_enabled:
146                             error = "Invalid plugin name: %s" % val
147                             raise ValueError(error)
148                         new_order.append(val)
149                     if len(new_order) < len(cur_enabled):
150                         for val in cur_enabled:
151                             if val not in new_order:
152                                 new_order.append(val)
153
154                     self.parent.save_enabled_plugins(new_order)
155
156                     # When all is saved update also live config. The
157                     # live config is the ordered list of plugin names.
158                     self._site[self.facility].refresh_enabled()
159
160                     message = "New configuration saved."
161                     message_type = "success"
162
163                 except ValueError, e:
164                     message = str(e)
165                     message_type = "error"
166
167                 except Exception, e:  # pylint: disable=broad-except
168                     message = "Failed to save data!"
169                     message_type = "error"
170
171         return self.parent.root_with_msg(message=message,
172                                          message_type=message_type)
173
174
175 class AdminPlugins(AdminPage):
176     def __init__(self, name, site, parent, facility, ordered=True):
177         super(AdminPlugins, self).__init__(site)
178         self._master = parent
179         self.name = name
180         self.title = '%s plugins' % name
181         self.url = '%s/%s' % (parent.url, name)
182         self.facility = facility
183         self.template = 'admin/plugins.html'
184         self.order = None
185         parent.add_subtree(name, self)
186
187         for plugin in self._site[facility].available:
188             cherrypy.log.error('Admin info plugin: %s' % plugin)
189             obj = self._site[facility].available[plugin]
190             page = AdminPluginConfig(obj, self._site, self)
191             if hasattr(obj, 'admin'):
192                 obj.admin.mount(page)
193             self.add_subtree(plugin, page)
194
195         if ordered:
196             self.order = AdminPluginsOrder(self._site, self, facility)
197
198     def save_enabled_plugins(self, names):
199         self._site[self.facility].save_enabled(names)
200
201     def root_with_msg(self, message=None, message_type=None):
202         plugins = self._site[self.facility]
203
204         targs = {'title': self.title,
205                  'menu': self._master.menu,
206                  'message': message,
207                  'message_type': message_type,
208                  'available': plugins.available,
209                  'enabled': plugins.enabled,
210                  'baseurl': self.url,
211                  'newurl': self.url}
212         if self.order:
213             targs['order_name'] = '%s_order_form' % self.name
214             targs['order_action'] = self.order.url
215
216         # pylint: disable=star-args
217         return self._template(self.template, **targs)
218
219     def root(self, *args, **kwargs):
220         return self.root_with_msg()
221
222     @admin_protect
223     def enable(self, plugin):
224         msg = None
225         plugins = self._site[self.facility]
226         if plugin not in plugins.available:
227             msg = "Unknown plugin %s" % plugin
228             return self.root_with_msg(msg, "error")
229         obj = plugins.available[plugin]
230         if not obj.is_enabled:
231             obj.enable()
232             obj.save_enabled_state()
233             msg = "Plugin %s enabled" % obj.name
234         return self.root_with_msg(msg, "success")
235     enable.public_function = True
236
237     @admin_protect
238     def disable(self, plugin):
239         msg = None
240         plugins = self._site[self.facility]
241         if plugin not in plugins.available:
242             msg = "Unknown plugin %s" % plugin
243             return self.root_with_msg(msg, "error")
244         obj = plugins.available[plugin]
245         if obj.is_enabled:
246             obj.disable()
247             obj.save_enabled_state()
248             msg = "Plugin %s disabled" % obj.name
249         return self.root_with_msg(msg, "success")
250     disable.public_function = True
251
252
253 class Admin(AdminPage):
254
255     def __init__(self, site, mount):
256         super(Admin, self).__init__(site)
257         self.title = 'Home'
258         self.mount = mount
259         self.url = '%s/%s' % (self.basepath, mount)
260         self.menu = [self]
261
262     def root(self, *args, **kwargs):
263         return self._template('admin/index.html',
264                               title='Configuration',
265                               baseurl=self.url,
266                               menu=self.menu)
267
268     def add_subtree(self, name, page):
269         self.__dict__[name] = page
270         self.menu.append(page)
271
272     def del_subtree(self, name):
273         self.menu.remove(self.__dict__[name])
274         del self.__dict__[name]
275
276     def get_menu_urls(self):
277         urls = dict()
278         for item in self.menu:
279             name = getattr(item, 'name', None)
280             if name:
281                 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
282                                                                  name))
283         return urls
284
285     @admin_protect
286     def scheme(self):
287         cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
288         urls = self.get_menu_urls()
289         # pylint: disable=star-args
290         return self._template('admin/ipsilon-scheme.svg', **urls)
291     scheme.public_function = True