check for deployment/availability of services

This commit is contained in:
Frédéric Péters 2014-03-25 16:04:36 +01:00
parent 25c290b8ae
commit dd14cccf48
6 changed files with 89 additions and 2 deletions

View File

@ -6,7 +6,8 @@ from .models import IdentityProvider, WebForms
class IdentityProviderForm(forms.ModelForm):
class Meta:
model = IdentityProvider
exclude = ('slug',)
exclude = ('slug', 'last_operational_check_timestamp',
'last_operational_success_timestamp')
def save(self, commit=True):
if not self.instance.slug:
@ -17,7 +18,8 @@ class IdentityProviderForm(forms.ModelForm):
class WebFormsForm(forms.ModelForm):
class Meta:
model = WebForms
exclude = ('slug',)
exclude = ('slug', 'last_operational_check_timestamp',
'last_operational_success_timestamp')
def save(self, commit=True):
if not self.instance.slug:

View File

@ -1,4 +1,8 @@
import datetime
import urllib2
from django.db import models
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
@ -10,6 +14,30 @@ class ServiceBase(models.Model):
slug = models.SlugField()
base_url = models.CharField(_('Base URL'), max_length=200)
last_operational_check_timestamp = models.DateTimeField(null=True)
last_operational_success_timestamp = models.DateTimeField(null=True)
def is_operational(self):
return (self.last_operational_success_timestamp is not None and
self.last_operational_success_timestamp == self.last_operational_check_timestamp)
def check_operational(self):
once_now = now()
self.last_operational_check_timestamp = once_now
try:
fd = urllib2.urlopen(self.base_url, timeout=10)
fd.close()
self.last_operational_success_timestamp = once_now
except (urllib2.URLError, urllib2.HTTPError):
pass
self.save()
def wants_frequent_checks(self):
if self.last_operational_check_timestamp is None:
return True
two_minutes = datetime.timedelta(minutes=2)
return (now() - self.last_operational_check_timestamp < two_minutes)
class IdentityProvider(ServiceBase):
class Meta:

View File

@ -28,11 +28,27 @@
</p>
{% for service in installed_services %}
<div data-service-id="{{ service.Extra.service_id }}"
data-slug="{{ service.slug }}"
{% if service.wants_frequent_checks %}data-wants-check="true"{% endif %}>
<h3>{{ service.title }}</h3>
{% if not service.is_operational %}
{% if service.wants_frequent_checks %}
<p class="info being-deployed">
{% trans 'This service is still being deployed.' %}
</p>
{% else %}
<p class="warning">
{% trans 'This service is not operational.' %}
</p>
{% endif %}
{% endif %}
<p>
<form class="small">
{{ service|as_update_form }}
<button disabled="disabled">{% trans 'Save' %}</button>
</form>
</div>
{% endfor %}
{% endblock %}
@ -40,6 +56,24 @@
{% block page-end %}
<script>
jQuery.fn.extend({
operational_check: function() {
return this.each(function() {
var div = $(this)
var p_info = $(div).find('p.info');
var url = 'check_operational/' + $(div).data('service-id') + '/' + $(div).data('slug');
$.getJSON(url, function(data) {
console.log('div:', $(div));
if (data.operational == true) {
$(p_info).hide('size');
} else {
setTimeout(function() { $(div).operational_check(); }, 10000);
}
});
});
}
});
$(function() {
$('#new-service').change(function() {
var service_id = $(this).val();
@ -50,6 +84,9 @@ $(function() {
$(form).dialog({modal: true, title: title, width: 'auto'});
});
});
$("div[data-wants-check='true']").each(function(index, element) {
$(element).operational_check();
});
});
</script>
{% endblock %}

View File

@ -4,6 +4,8 @@ from .views import *
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name='environment-home'),
url(r'^check_operational/(?P<service>\w+)/(?P<slug>[\w-]+)$',
operational_check_view, name='operational-check'),
url(r'^new-idp$', IdentityProviderCreateView.as_view()),
url(r'^new-wcs$', WebFormsCreateView.as_view()),
)

View File

@ -1,7 +1,9 @@
import json
import string
from django.conf import settings
from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponse
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView
@ -56,3 +58,14 @@ class IdentityProviderCreateView(ServiceCreateView):
class WebFormsCreateView(ServiceCreateView):
form_class = WebFormsForm
model = WebForms
def operational_check_view(request, service, slug, **kwargs):
if service == 'wcs':
object = WebForms.objects.get(slug=slug)
elif service == 'idp':
object = IdentityProvider.objects.get(slug=slug)
object.check_operational()
response = HttpResponse(content_type='application/json')
json.dump({'operational': object.is_operational()}, response)
return response

View File

@ -1,2 +1,7 @@
li#settings a { background-image: url(../img/icon-settings.png); }
li#settings a:hover { background-image: url(../img/icon-settings-hover.png); }
p.being-deployed {
background: url(indicator.gif) no-repeat;
padding-left: 2em;
}