tailbone/tailbone/views/tempmon/clients.py

152 lines
5.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8; -*-
2016-11-21 23:53:20 -06:00
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar
2016-11-21 23:53:20 -06:00
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
2016-11-21 23:53:20 -06:00
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
2016-11-21 23:53:20 -06:00
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
2016-11-21 23:53:20 -06:00
#
################################################################################
"""
Views for tempmon clients
"""
from __future__ import unicode_literals, absolute_import
import subprocess
from rattail_tempmon.db import model as tempmon
2016-11-21 23:53:20 -06:00
import formalchemy as fa
from webhelpers2.html import HTML, tags
2016-11-21 23:53:20 -06:00
from tailbone.db import TempmonSession
from tailbone.views.tempmon import MasterView
2016-11-21 23:53:20 -06:00
class ProbesFieldRenderer(fa.FieldRenderer):
def render_readonly(self, **kwargs):
probes = self.raw_value
if not probes:
return ''
items = []
for probe in probes:
items.append(HTML.tag('li', c=tags.link_to(probe, self.request.route_url('tempmon.probes.view', uuid=probe.uuid))))
return HTML.tag('ul', c=items)
def unique_config_key(value, field):
client = field.parent.model
query = TempmonSession.query(tempmon.Client)\
.filter(tempmon.Client.config_key == value)
2016-11-21 23:53:20 -06:00
if client.uuid:
query = query.filter(tempmon.Client.uuid != client.uuid)
2016-11-21 23:53:20 -06:00
if query.count():
raise fa.ValidationError("Config key must be unique")
class TempmonClientView(MasterView):
"""
Master view for tempmon clients.
"""
model_class = tempmon.Client
model_title = "TempMon Client"
model_title_plural = "TempMon Clients"
2016-11-21 23:53:20 -06:00
route_prefix = 'tempmon.clients'
url_prefix = '/tempmon/clients'
def _preconfigure_grid(self, g):
g.filters['hostname'].default_active = True
g.filters['hostname'].default_verb = 'contains'
g.filters['location'].default_active = True
g.filters['location'].default_verb = 'contains'
g.default_sortkey = 'config_key'
g.config_key.set(label="Key")
def configure_grid(self, g):
g.configure(
include=[
g.config_key,
g.hostname,
g.location,
g.delay,
2016-11-21 23:53:20 -06:00
g.enabled,
g.online,
],
readonly=True)
def _preconfigure_fieldset(self, fs):
fs.config_key.set(validate=unique_config_key)
fs.probes.set(renderer=ProbesFieldRenderer)
def configure_fieldset(self, fs):
fs.configure(
include=[
fs.config_key,
fs.hostname,
fs.location,
fs.delay,
2016-11-21 23:53:20 -06:00
fs.probes,
fs.enabled,
fs.online,
])
if self.creating or self.editing:
del fs.probes
del fs.online
def restartable_client(self, client):
return True
def restart(self):
client = self.get_instance()
if self.restartable_client(client):
try:
2016-12-10 11:59:32 -06:00
subprocess.check_output(self.get_restart_cmd(client),
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as error:
self.request.session.flash("Failed to restart client: {}".format(error.output), 'error')
else:
self.request.session.flash("Client has been restarted: {}".format(
self.get_instance_title(client)))
else:
self.request.session.flash("Restart not supported for client: {}".format(client), 'error')
return self.redirect(self.get_action_url('view', client))
def get_restart_cmd(self, client):
return ['ssh', client.hostname, 'sudo service tempmon-client restart']
@classmethod
def defaults(cls, config):
route_prefix = cls.get_route_prefix()
url_prefix = cls.get_url_prefix()
permission_prefix = cls.get_permission_prefix()
model_key = cls.get_model_key()
model_title = cls.get_model_title()
cls._defaults(config)
# restart tempmon client
config.add_tailbone_permission(permission_prefix, '{}.restart'.format(permission_prefix),
"Restart a {}".format(model_title))
config.add_route('{}.restart'.format(route_prefix), '{}/{{{}}}/restart'.format(url_prefix, model_key))
config.add_view(cls, attr='restart', route_name='{}.restart'.format(route_prefix),
permission='{}.restart'.format(permission_prefix))
2016-11-21 23:53:20 -06:00
def includeme(config):
TempmonClientView.defaults(config)