Commit 7f451e3e by Őry Máté

Merge branch 'master' into feature-store

Conflicts:
	circle/circle/settings/base.py
parents 68533e15 fe84d9e6
...@@ -59,10 +59,10 @@ def create_levels(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, ...@@ -59,10 +59,10 @@ def create_levels(app, created_models, verbosity, db=DEFAULT_DB_ALIAS,
] ]
Level.objects.using(db).bulk_create(levels) Level.objects.using(db).bulk_create(levels)
if verbosity >= 2: if verbosity >= 2:
print("Adding levels [%s]." % ", ".join(levels)) print("Adding levels [%s]." % ", ".join(unicode(l) for l in levels))
print("Searched: [%s]." % ", ".join( print("Searched: [%s]." % ", ".join(
[unicode(l) for l in searched_levels])) unicode(l) for l in searched_levels))
print("All: [%s]." % ", ".join([unicode(l) for l in all_levels])) print("All: [%s]." % ", ".join(unicode(l) for l in all_levels))
# set weights # set weights
for ctype, codename, weight in level_weights: for ctype, codename, weight in level_weights:
......
...@@ -22,6 +22,7 @@ from os.path import (abspath, basename, dirname, join, normpath, isfile, ...@@ -22,6 +22,7 @@ from os.path import (abspath, basename, dirname, join, normpath, isfile,
expanduser) expanduser)
from sys import path from sys import path
from subprocess import check_output from subprocess import check_output
from uuid import getnode
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
...@@ -453,3 +454,6 @@ STORE_CLIENT_PASSWORD = get_env_variable("STORE_CLIENT_PASSWORD", "") ...@@ -453,3 +454,6 @@ STORE_CLIENT_PASSWORD = get_env_variable("STORE_CLIENT_PASSWORD", "")
STORE_CLIENT_KEY = get_env_variable("STORE_CLIENT_KEY", "") STORE_CLIENT_KEY = get_env_variable("STORE_CLIENT_KEY", "")
STORE_CLIENT_CERT = get_env_variable("STORE_CLIENT_CERT", "") STORE_CLIENT_CERT = get_env_variable("STORE_CLIENT_CERT", "")
STORE_URL = get_env_variable("STORE_URL") STORE_URL = get_env_variable("STORE_URL")
SESSION_COOKIE_NAME = "csessid%x" % (((getnode() // 139) ^
(getnode() % 983)) & 0xffff)
...@@ -46,8 +46,9 @@ CACHES = { ...@@ -46,8 +46,9 @@ CACHES = {
LOGGING['loggers']['djangosaml2'] = {'handlers': ['console'], LOGGING['loggers']['djangosaml2'] = {'handlers': ['console'],
'level': 'CRITICAL'} 'level': 'CRITICAL'}
LOGGING['handlers']['console'] = {'level': 'WARNING', level = environ.get('LOGLEVEL', 'CRITICAL')
LOGGING['handlers']['console'] = {'level': level,
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
'formatter': 'simple'} 'formatter': 'simple'}
for i in LOCAL_APPS: for i in LOCAL_APPS:
LOGGING['loggers'][i] = {'handlers': ['console'], 'level': 'CRITICAL'} LOGGING['loggers'][i] = {'handlers': ['console'], 'level': level}
...@@ -43,8 +43,9 @@ urlpatterns = patterns( ...@@ -43,8 +43,9 @@ urlpatterns = patterns(
url(r'^network/', include('network.urls')), url(r'^network/', include('network.urls')),
url(r'^dashboard/', include('dashboard.urls')), url(r'^dashboard/', include('dashboard.urls')),
url((r'^accounts/reset/(?P<uidb36>[0-9A-Za-z]{1,13})-' # django/contrib/auth/urls.py (care when new version)
'(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$'), url((r'^accounts/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/'
r'(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$'),
'django.contrib.auth.views.password_reset_confirm', 'django.contrib.auth.views.password_reset_confirm',
{'set_password_form': CircleSetPasswordForm}, {'set_password_form': CircleSetPasswordForm},
name='accounts.password_reset_confirm' name='accounts.password_reset_confirm'
...@@ -64,3 +65,5 @@ if get_env_variable('DJANGO_SAML', 'FALSE') == 'TRUE': ...@@ -64,3 +65,5 @@ if get_env_variable('DJANGO_SAML', 'FALSE') == 'TRUE':
'', '',
(r'^saml2/', include('djangosaml2.urls')), (r'^saml2/', include('djangosaml2.urls')),
) )
handler500 = 'common.views.handler500'
...@@ -21,13 +21,19 @@ from hashlib import sha224 ...@@ -21,13 +21,19 @@ from hashlib import sha224
from itertools import chain, imap from itertools import chain, imap
from logging import getLogger from logging import getLogger
from time import time from time import time
from warnings import warn
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.cache import cache from django.core.cache import cache
from django.db.models import (CharField, DateTimeField, ForeignKey, from django.core.serializers.json import DjangoJSONEncoder
NullBooleanField, TextField) from django.db.models import (
CharField, DateTimeField, ForeignKey, NullBooleanField
)
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import force_text
from django.utils.functional import Promise
from django.utils.translation import ugettext_lazy as _, ugettext_noop
from jsonfield import JSONField
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
...@@ -45,7 +51,11 @@ def activitycontextimpl(act, on_abort=None, on_commit=None): ...@@ -45,7 +51,11 @@ def activitycontextimpl(act, on_abort=None, on_commit=None):
# BaseException is the common parent of Exception and # BaseException is the common parent of Exception and
# system-exiting exceptions, e.g. KeyboardInterrupt # system-exiting exceptions, e.g. KeyboardInterrupt
handler = None if on_abort is None else lambda a: on_abort(a, e) handler = None if on_abort is None else lambda a: on_abort(a, e)
act.finish(succeeded=False, result=str(e), event_handler=handler) result = create_readable(ugettext_noop("Failure."),
ugettext_noop("Unhandled exception: "
"%(error)s"),
error=unicode(e))
act.finish(succeeded=False, result=result, event_handler=handler)
raise e raise e
else: else:
act.finish(succeeded=True, event_handler=on_commit) act.finish(succeeded=True, event_handler=on_commit)
...@@ -103,8 +113,23 @@ def split_activity_code(activity_code): ...@@ -103,8 +113,23 @@ def split_activity_code(activity_code):
return activity_code.split(activity_code_separator) return activity_code.split(activity_code_separator)
class Encoder(DjangoJSONEncoder):
def default(self, obj):
if isinstance(obj, Promise):
obj = force_text(obj)
try:
return super(Encoder, self).default(obj)
except TypeError:
return unicode(obj)
class ActivityModel(TimeStampedModel): class ActivityModel(TimeStampedModel):
activity_code = CharField(max_length=100, verbose_name=_('activity code')) activity_code = CharField(max_length=100, verbose_name=_('activity code'))
readable_name_data = JSONField(blank=True, null=True,
dump_kwargs={"cls": Encoder},
verbose_name=_('human readable name'),
help_text=_('Human readable name of '
'activity.'))
parent = ForeignKey('self', blank=True, null=True, related_name='children') parent = ForeignKey('self', blank=True, null=True, related_name='children')
task_uuid = CharField(blank=True, max_length=50, null=True, unique=True, task_uuid = CharField(blank=True, max_length=50, null=True, unique=True,
help_text=_('Celery task unique identifier.'), help_text=_('Celery task unique identifier.'),
...@@ -120,8 +145,9 @@ class ActivityModel(TimeStampedModel): ...@@ -120,8 +145,9 @@ class ActivityModel(TimeStampedModel):
succeeded = NullBooleanField(blank=True, null=True, succeeded = NullBooleanField(blank=True, null=True,
help_text=_('True, if the activity has ' help_text=_('True, if the activity has '
'finished successfully.')) 'finished successfully.'))
result = TextField(verbose_name=_('result'), blank=True, null=True, result_data = JSONField(verbose_name=_('result'), blank=True, null=True,
help_text=_('Human readable result of activity.')) dump_kwargs={"cls": Encoder},
help_text=_('Human readable result of activity.'))
def __unicode__(self): def __unicode__(self):
if self.parent: if self.parent:
...@@ -150,6 +176,29 @@ class ActivityModel(TimeStampedModel): ...@@ -150,6 +176,29 @@ class ActivityModel(TimeStampedModel):
def has_failed(self): def has_failed(self):
return self.finished and not self.succeeded return self.finished and not self.succeeded
@property
def readable_name(self):
return HumanReadableObject.from_dict(self.readable_name_data)
@readable_name.setter
def readable_name(self, value):
self.readable_name_data = None if value is None else value.to_dict()
@property
def result(self):
return HumanReadableObject.from_dict(self.result_data)
@result.setter
def result(self, value):
if isinstance(value, basestring):
warn("Using string as result value is deprecated. Use "
"HumanReadableObject instead.",
DeprecationWarning, stacklevel=2)
value = create_readable(user_text_template="",
admin_text_template=value)
self.result_data = None if value is None else value.to_dict()
def method_cache(memcached_seconds=60, instance_seconds=5): # noqa def method_cache(memcached_seconds=60, instance_seconds=5): # noqa
"""Cache return value of decorated method to memcached and memory. """Cache return value of decorated method to memcached and memory.
...@@ -299,3 +348,69 @@ try: ...@@ -299,3 +348,69 @@ try:
], patterns=['common\.models\.']) ], patterns=['common\.models\.'])
except ImportError: except ImportError:
pass pass
class HumanReadableObject(object):
def __init__(self, user_text_template, admin_text_template, params):
self._set_values(user_text_template, admin_text_template, params)
def _set_values(self, user_text_template, admin_text_template, params):
self.user_text_template = user_text_template
self.admin_text_template = admin_text_template
self.params = params
@classmethod
def create(cls, user_text_template, admin_text_template=None, **params):
return cls(user_text_template,
admin_text_template or user_text_template, params)
def set(self, user_text_template, admin_text_template=None, **params):
self._set_values(user_text_template,
admin_text_template or user_text_template, params)
@classmethod
def from_dict(cls, d):
return None if d is None else cls(**d)
def get_admin_text(self):
if self.admin_text_template == "":
return ""
return _(self.admin_text_template) % self.params
def get_user_text(self):
if self.user_text_template == "":
return ""
return _(self.user_text_template) % self.params
def to_dict(self):
return {"user_text_template": self.user_text_template,
"admin_text_template": self.admin_text_template,
"params": self.params}
def __unicode__(self):
return self.get_user_text()
create_readable = HumanReadableObject.create
class HumanReadableException(HumanReadableObject, Exception):
"""HumanReadableObject that is an Exception so can used in except clause.
"""
pass
def humanize_exception(message, exception=None, **params):
"""Return new dynamic-class exception which is based on
HumanReadableException and the original class with the dict of exception.
>>> try: raise humanize_exception("Welcome!", TypeError("hello"))
... except HumanReadableException as e: print e.get_admin_text()
...
Welcome!
"""
Ex = type("HumanReadable" + type(exception).__name__,
(HumanReadableException, type(exception)),
exception.__dict__)
return Ex.create(message, **params)
...@@ -59,6 +59,8 @@ class Operation(object): ...@@ -59,6 +59,8 @@ class Operation(object):
skip_auth_check = auxargs.pop('system') skip_auth_check = auxargs.pop('system')
user = auxargs.pop('user') user = auxargs.pop('user')
parent_activity = auxargs.pop('parent_activity') parent_activity = auxargs.pop('parent_activity')
if parent_activity and user is None and not skip_auth_check:
user = parent_activity.user
# check for unexpected keyword arguments # check for unexpected keyword arguments
argspec = getargspec(self._operation) argspec = getargspec(self._operation)
...@@ -72,7 +74,8 @@ class Operation(object): ...@@ -72,7 +74,8 @@ class Operation(object):
self.check_auth(user) self.check_auth(user)
self.check_precond() self.check_precond()
activity = self.create_activity(parent=parent_activity, user=user) activity = self.create_activity(
parent=parent_activity, user=user, kwargs=kwargs)
return activity, allargs, auxargs return activity, allargs, auxargs
...@@ -148,7 +151,7 @@ class Operation(object): ...@@ -148,7 +151,7 @@ class Operation(object):
raise PermissionDenied("%s doesn't have the required permissions." raise PermissionDenied("%s doesn't have the required permissions."
% user) % user)
def create_activity(self, parent, user): def create_activity(self, parent, user, kwargs):
raise NotImplementedError raise NotImplementedError
def on_abort(self, activity, error): def on_abort(self, activity, error):
...@@ -157,6 +160,18 @@ class Operation(object): ...@@ -157,6 +160,18 @@ class Operation(object):
""" """
pass pass
def get_activity_name(self, kwargs):
try:
return self.activity_name
except AttributeError:
try:
return self.name._proxy____args[0] # ewww!
except AttributeError:
raise ImproperlyConfigured(
"Set Operation.activity_name to an ugettext_nooped "
"string or a create_readable call, or override "
"get_activity_name to create a name dynamically")
def on_commit(self, activity): def on_commit(self, activity):
"""This method is called when the operation executes successfully. """This method is called when the operation executes successfully.
""" """
......
from sys import exc_info
import logging
from django.template import RequestContext
from django.shortcuts import render_to_response
from .models import HumanReadableException
logger = logging.getLogger(__name__)
def handler500(request):
cls, exception, traceback = exc_info()
logger.exception("unhandled exception")
ctx = {}
if isinstance(exception, HumanReadableException):
try:
ctx['error'] = exception.get_user_text()
except:
pass
else:
try:
if request.user.is_superuser():
ctx['error'] = exception.get_admin_text()
except:
pass
try:
resp = render_to_response("500.html", ctx, RequestContext(request))
except:
resp = render_to_response("500.html", ctx)
resp.status_code = 500
return resp
...@@ -131,7 +131,7 @@ class VmCustomizeForm(forms.Form): ...@@ -131,7 +131,7 @@ class VmCustomizeForm(forms.Form):
"button", "button",
AnyTag( AnyTag(
"i", "i",
css_class="icon-play" css_class="fa fa-play"
), ),
HTML(" Start"), HTML(" Start"),
css_id="vm-create-customized-start", css_id="vm-create-customized-start",
...@@ -163,7 +163,7 @@ class VmCustomizeForm(forms.Form): ...@@ -163,7 +163,7 @@ class VmCustomizeForm(forms.Form):
Div( # cpu priority Div( # cpu priority
Div( Div(
HTML('<label for="vm-cpu-priority-slider">' HTML('<label for="vm-cpu-priority-slider">'
'<i class="icon-trophy"></i> CPU priority' '<i class="fa fa-trophy"></i> CPU priority'
'</label>'), '</label>'),
css_class="col-sm-3" css_class="col-sm-3"
), ),
...@@ -182,7 +182,7 @@ class VmCustomizeForm(forms.Form): ...@@ -182,7 +182,7 @@ class VmCustomizeForm(forms.Form):
Div( # cpu count Div( # cpu count
Div( Div(
HTML('<label for="cpu-count-slider">' HTML('<label for="cpu-count-slider">'
'<i class="icon-cogs"></i> CPU count' '<i class="fa fa-cogs"></i> CPU count'
'</label>'), '</label>'),
css_class="col-sm-3" css_class="col-sm-3"
), ),
...@@ -201,7 +201,7 @@ class VmCustomizeForm(forms.Form): ...@@ -201,7 +201,7 @@ class VmCustomizeForm(forms.Form):
Div( # ram size Div( # ram size
Div( Div(
HTML('<label for="ram-slider">' HTML('<label for="ram-slider">'
'<i class="icon-ticket"></i> RAM amount' '<i class="fa fa-ticket"></i> RAM amount'
'</label>'), '</label>'),
css_class="col-sm-3" css_class="col-sm-3"
), ),
...@@ -313,7 +313,7 @@ class VmCustomizeForm(forms.Form): ...@@ -313,7 +313,7 @@ class VmCustomizeForm(forms.Form):
"a", "a",
AnyTag( AnyTag(
"i", "i",
css_class="icon-plus-sign", css_class="fa fa-plus-circle",
), ),
css_id=("vm-create-network-add" css_id=("vm-create-network-add"
"-button"), "-button"),
...@@ -556,7 +556,7 @@ class NodeForm(forms.ModelForm): ...@@ -556,7 +556,7 @@ class NodeForm(forms.ModelForm):
"button", "button",
AnyTag( AnyTag(
"i", "i",
css_class="icon-play" css_class="fa fa-play"
), ),
HTML("Start"), HTML("Start"),
css_id="node-create-submit", css_id="node-create-submit",
...@@ -612,6 +612,9 @@ class TemplateForm(forms.ModelForm): ...@@ -612,6 +612,9 @@ class TemplateForm(forms.ModelForm):
self.instance.ram_size = 512 self.instance.ram_size = 512
self.instance.num_cores = 2 self.instance.num_cores = 2
self.fields["lease"].queryset = Lease.get_objects_with_level(
"operator", self.user)
def clean_owner(self): def clean_owner(self):
if self.instance.pk is not None: if self.instance.pk is not None:
return User.objects.get(pk=self.instance.owner.pk) return User.objects.get(pk=self.instance.owner.pk)
...@@ -888,6 +891,27 @@ class LeaseForm(forms.ModelForm): ...@@ -888,6 +891,27 @@ class LeaseForm(forms.ModelForm):
model = Lease model = Lease
class VmRenewForm(forms.Form):
def __init__(self, *args, **kwargs):
choices = kwargs.pop('choices')
default = kwargs.pop('default')
super(VmRenewForm, self).__init__(*args, **kwargs)
self.fields['lease'] = forms.ModelChoiceField(queryset=choices,
initial=default,
required=True,
label=_('Length'))
if len(choices) < 2:
self.fields['lease'].widget = HiddenInput()
@property
def helper(self):
helper = FormHelper(self)
helper.form_tag = False
return helper
class VmCreateDiskForm(forms.Form): class VmCreateDiskForm(forms.Form):
name = forms.CharField(max_length=100, label=_("Name")) name = forms.CharField(max_length=100, label=_("Name"))
size = forms.CharField( size = forms.CharField(
...@@ -934,7 +958,7 @@ class CircleAuthenticationForm(AuthenticationForm): ...@@ -934,7 +958,7 @@ class CircleAuthenticationForm(AuthenticationForm):
"span", "span",
AnyTag( AnyTag(
"i", "i",
css_class="icon-user", css_class="fa fa-user",
), ),
css_class="input-group-addon", css_class="input-group-addon",
), ),
...@@ -948,7 +972,7 @@ class CircleAuthenticationForm(AuthenticationForm): ...@@ -948,7 +972,7 @@ class CircleAuthenticationForm(AuthenticationForm):
"span", "span",
AnyTag( AnyTag(
"i", "i",
css_class="icon-lock", css_class="fa fa-lock",
), ),
css_class="input-group-addon", css_class="input-group-addon",
), ),
...@@ -976,7 +1000,7 @@ class CirclePasswordResetForm(PasswordResetForm): ...@@ -976,7 +1000,7 @@ class CirclePasswordResetForm(PasswordResetForm):
"span", "span",
AnyTag( AnyTag(
"i", "i",
css_class="icon-envelope", css_class="fa fa-envelope",
), ),
css_class="input-group-addon", css_class="input-group-addon",
), ),
......
...@@ -30,16 +30,17 @@ from django.db.models import ( ...@@ -30,16 +30,17 @@ from django.db.models import (
DateTimeField, permalink, BooleanField DateTimeField, permalink, BooleanField
) )
from django.db.models.signals import post_save, pre_delete from django.db.models.signals import post_save, pre_delete
from django.template.loader import render_to_string
from django.templatetags.static import static from django.templatetags.static import static
from django.utils.translation import ugettext_lazy as _, override, ugettext from django.utils.translation import ugettext_lazy as _
from django_sshkey.models import UserKey from django_sshkey.models import UserKey
from jsonfield import JSONField
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from model_utils.fields import StatusField from model_utils.fields import StatusField
from model_utils import Choices from model_utils import Choices
from acl.models import AclBase from acl.models import AclBase
from common.models import HumanReadableObject, create_readable, Encoder
from vm.tasks.agent_tasks import add_keys, del_keys from vm.tasks.agent_tasks import add_keys, del_keys
...@@ -62,26 +63,39 @@ class Notification(TimeStampedModel): ...@@ -62,26 +63,39 @@ class Notification(TimeStampedModel):
status = StatusField() status = StatusField()
to = ForeignKey(User) to = ForeignKey(User)
subject = CharField(max_length=128) subject_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
message = TextField() message_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
valid_until = DateTimeField(null=True, default=None) valid_until = DateTimeField(null=True, default=None)
class Meta: class Meta:
ordering = ['-created'] ordering = ['-created']
@classmethod @classmethod
def send(cls, user, subject, template, context={}, valid_until=None): def send(cls, user, subject, template, context,
try: valid_until=None, subject_context=None):
language = user.profile.preferred_language hro = create_readable(template, user=user, **context)
except: subject = create_readable(subject, subject_context or context)
language = None return cls.objects.create(to=user,
with override(language): subject_data=subject.to_dict(),
context['user'] = user message_data=hro.to_dict(),
rendered = render_to_string(template, context)
subject = ugettext(unicode(subject))
return cls.objects.create(to=user, subject=subject, message=rendered,
valid_until=valid_until) valid_until=valid_until)
@property
def subject(self):
return HumanReadableObject.from_dict(self.subject_data)
@subject.setter
def subject(self, value):
self.subject_data = None if value is None else value.to_dict()
@property
def message(self):
return HumanReadableObject.from_dict(self.message_data)
@message.setter
def message(self, value):
self.message_data = None if value is None else value.to_dict()
class Profile(Model): class Profile(Model):
user = OneToOneField(User) user = OneToOneField(User)
...@@ -112,8 +126,11 @@ class Profile(Model): ...@@ -112,8 +126,11 @@ class Profile(Model):
default=2048, default=2048,
help_text=_('Disk quota in mebibytes.')) help_text=_('Disk quota in mebibytes.'))
def notify(self, subject, template, context={}, valid_until=None): def notify(self, subject, template, context=None, valid_until=None,
return Notification.send(self.user, subject, template, context, **kwargs):
if context is not None:
kwargs.update(context)
return Notification.send(self.user, subject, template, kwargs,
valid_until) valid_until)
def get_absolute_url(self): def get_absolute_url(self):
......
...@@ -146,10 +146,10 @@ ...@@ -146,10 +146,10 @@
height: 26px; height: 26px;
margin-top: -4px!important; margin-top: -4px!important;
margin-left: -6px !important; margin-left: -6px !important;
border-radius: 0px; border-radius: 0px;
-moz-border-radius: 0px; -moz-border-radius: 0px;
-webkit-border-radius: 0px; -webkit-border-radius: 0px;
text-shadow: 0 1px 0 #fff; text-shadow: 0 1px 0 #fff;
background-image: -webkit-gradient(linear, left 0%, left 100%, from(#428bca), to(#3071a9)); background-image: -webkit-gradient(linear, left 0%, left 100%, from(#428bca), to(#3071a9));
...@@ -196,10 +196,10 @@ ...@@ -196,10 +196,10 @@
} }
.vm-slider { .vm-slider {
width: 300px; width: 300px;
} }
.output { .output {
padding-left: 10px; padding-left: 10px;
font-weight: bold; font-weight: bold;
} }
...@@ -145,13 +145,13 @@ html { ...@@ -145,13 +145,13 @@ html {
background-color: transparent; background-color: transparent;
} }
.sub-timeline { .sub-timeline {
} }
.sub-activity { .sub-activity {