Add support for restarting tempmon client

This commit is contained in:
Lance Edgar 2016-11-22 00:55:30 -06:00
parent a39c347ad3
commit 0296c29dd7
2 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,22 @@
## -*- coding: utf-8 -*-
<%inherit file="/master/view.mako" />
<%def name="head_tags()">
${parent.head_tags()}
<script type="text/javascript">
$(function() {
$('#restart-client').click(function() {
$(this).button('disable').button('option', 'label', "Restarting, please wait...");
location.href = '${url('tempmon.clients.restart', uuid=instance.uuid)}';
});
});
</script>
</%def>
${parent.body()}
% if instance.enabled and request.has_perm('tempmon.clients.restart'):
<div class="buttons">
<button type="button" id="restart-client">Restart this Client</button>
</div>
% endif

View file

@ -26,6 +26,8 @@ Views for tempmon clients
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import subprocess
from rattail.db import model from rattail.db import model
import formalchemy as fa import formalchemy as fa
@ -102,6 +104,35 @@ class TempmonClientView(MasterView):
del fs.probes del fs.probes
del fs.online del fs.online
def restart(self):
client = self.get_instance()
try:
subprocess.check_output(['ssh', client.hostname, 'sudo service tempmon restart'],
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)))
return self.redirect(self.get_action_url('view', client))
@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))
def includeme(config): def includeme(config):
TempmonClientView.defaults(config) TempmonClientView.defaults(config)