Fix file permissions and remove shebang's
[cascardo/ipsilon.git] / ipsilon / admin / common.py
1 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
2 #
3 # see file 'COPYING' for use and warranty information
4 #
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.
9 #
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.
14 #
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/>.
17
18 import cherrypy
19 from ipsilon.util.page import Page
20 from ipsilon.util.page import admin_protect
21 from ipsilon.util import config as pconfig
22
23
24 ADMIN_STATUS_OK = "success"
25 ADMIN_STATUS_ERROR = "danger"
26 ADMIN_STATUS_WARN = "warning"
27
28
29 class AdminError(Exception):
30     def __init__(self, message):
31         super(AdminError, self).__init__(message)
32         self.message = message
33
34     def __str__(self):
35         return str(self.message)
36
37
38 class AdminPage(Page):
39
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',
44             'Pragma': 'no-cache',
45             'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
46         })
47         self.auth_protect = True
48
49
50 class AdminPluginConfig(AdminPage):
51
52     def __init__(self, po, site, parent):
53         super(AdminPluginConfig, self).__init__(site, form=True)
54         self._po = po
55         self.title = '%s plugin' % po.name
56         self.url = '%s/%s' % (parent.url, po.name)
57         self.facility = parent.facility
58         self.menu = [parent]
59         self.back = parent.url
60
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,
66                                                          self._po.name),
67                               config=self._po.get_config_obj())
68
69     @admin_protect
70     def GET(self, *args, **kwargs):
71         return self.root_with_msg()
72
73     @admin_protect
74     def POST(self, *args, **kwargs):
75
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)
80
81         message = "Nothing was modified."
82         message_type = "info"
83         new_db_values = dict()
84
85         conf = self._po.get_config_obj()
86
87         for name, option in conf.iteritems():
88             if name in kwargs:
89                 value = kwargs[name]
90                 if isinstance(option, pconfig.List):
91                     value = [x.strip() for x in value.split('\n')]
92                 elif isinstance(option, pconfig.Condition):
93                     value = True
94             else:
95                 if isinstance(option, pconfig.Condition):
96                     value = False
97                 elif isinstance(option, pconfig.Choice):
98                     value = list()
99                     for a in option.get_allowed():
100                         aname = '%s_%s' % (name, a)
101                         if aname in kwargs:
102                             value.append(a)
103                 else:
104                     continue
105
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()
111
112         if len(new_db_values) != 0:
113             # First we try to save in the database
114             try:
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
121
122             # Then refresh the actual objects
123             self._po.refresh_plugin_config()
124
125         return self.root_with_msg(message=message,
126                                   message_type=message_type)
127
128
129 class AdminPluginsOrder(AdminPage):
130
131     def __init__(self, site, parent, facility):
132         super(AdminPluginsOrder, self).__init__(site, form=True)
133         self.parent = parent
134         self.facility = facility
135         self.url = '%s/order' % parent.url
136         self.menu = [parent]
137
138     @admin_protect
139     def GET(self, *args, **kwargs):
140         return self.parent.root_with_msg()
141
142     @admin_protect
143     def POST(self, *args, **kwargs):
144
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)
149
150         message = "Nothing was modified."
151         message_type = "info"
152         changed = None
153         cur_enabled = self._site[self.facility].enabled
154
155         if 'order' in kwargs:
156             order = kwargs['order'].split(',')
157             if len(order) != 0:
158                 new_order = []
159                 try:
160                     for v in order:
161                         val = v.strip()
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)
170
171                     self.parent.save_enabled_plugins(new_order)
172
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()
176
177                     message = "New configuration saved."
178                     message_type = ADMIN_STATUS_OK
179
180                     changed = dict()
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'
185
186                 except ValueError, e:
187                     message = str(e)
188                     message_type = ADMIN_STATUS_ERROR
189
190                 except Exception, e:  # pylint: disable=broad-except
191                     message = "Failed to save data!"
192                     message_type = ADMIN_STATUS_ERROR
193
194         return self.parent.root_with_msg(message=message,
195                                          message_type=message_type,
196                                          changed=changed)
197
198
199 class AdminPlugins(AdminPage):
200     def __init__(self, name, site, parent, facility, ordered=True):
201         super(AdminPlugins, self).__init__(site)
202         self._master = parent
203         self.name = name
204         self.title = '%s plugins' % name
205         self.url = '%s/%s' % (parent.url, name)
206         self.facility = facility
207         self.template = 'admin/plugins.html'
208         self.order = None
209         parent.add_subtree(name, self)
210
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)
218
219         if ordered:
220             self.order = AdminPluginsOrder(self._site, self, facility)
221
222     def save_enabled_plugins(self, names):
223         self._site[self.facility].save_enabled(names)
224
225     def root_with_msg(self, message=None, message_type=None, changed=None):
226         plugins = self._site[self.facility]
227
228         if changed is None:
229             changed = dict()
230
231         targs = {'title': self.title,
232                  'menu': self._master.menu,
233                  'message': message,
234                  'message_type': message_type,
235                  'available': plugins.available,
236                  'enabled': plugins.enabled,
237                  'changed': changed,
238                  'baseurl': self.url,
239                  'newurl': self.url}
240         if self.order:
241             targs['order_name'] = '%s_order_form' % self.name
242             targs['order_action'] = self.order.url
243
244         # pylint: disable=star-args
245         return self._template(self.template, **targs)
246
247     def root(self, *args, **kwargs):
248         return self.root_with_msg()
249
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]
259         if obj.is_readonly:
260             msg = "Plugin Configuration is marked Read-Only"
261             raise AdminError(msg)
262         return obj
263
264     @admin_protect
265     def enable(self, plugin):
266         msg = None
267         try:
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:
272             obj.enable()
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
278
279     @admin_protect
280     def disable(self, plugin):
281         msg = None
282         try:
283             obj = self._get_plugin_obj(plugin)
284         except AdminError, e:
285             return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
286         if obj.is_enabled:
287             obj.disable()
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
293
294
295 class Admin(AdminPage):
296
297     def __init__(self, site, mount):
298         super(Admin, self).__init__(site)
299         self.title = 'Home'
300         self.mount = mount
301         self.url = '%s/%s' % (self.basepath, mount)
302         self.menu = [self]
303
304     def root(self, *args, **kwargs):
305         return self._template('admin/index.html',
306                               title='Configuration',
307                               baseurl=self.url,
308                               menu=self.menu)
309
310     def add_subtree(self, name, page):
311         self.__dict__[name] = page
312         self.menu.append(page)
313
314     def del_subtree(self, name):
315         self.menu.remove(self.__dict__[name])
316         del self.__dict__[name]
317
318     def get_menu_urls(self):
319         urls = dict()
320         for item in self.menu:
321             name = getattr(item, 'name', None)
322             if name:
323                 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
324                                                                  name))
325         return urls
326
327     @admin_protect
328     def scheme(self):
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