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,
]
Level.objects.using(db).bulk_create(levels)
if verbosity >= 2:
print("Adding levels [%s]." % ", ".join(levels))
print("Adding levels [%s]." % ", ".join(unicode(l) for l in levels))
print("Searched: [%s]." % ", ".join(
[unicode(l) for l in searched_levels]))
print("All: [%s]." % ", ".join([unicode(l) for l in all_levels]))
unicode(l) for l in searched_levels))
print("All: [%s]." % ", ".join(unicode(l) for l in all_levels))
# set weights
for ctype, codename, weight in level_weights:
......
......@@ -22,6 +22,7 @@ from os.path import (abspath, basename, dirname, join, normpath, isfile,
expanduser)
from sys import path
from subprocess import check_output
from uuid import getnode
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import ugettext_lazy as _
......@@ -453,3 +454,6 @@ STORE_CLIENT_PASSWORD = get_env_variable("STORE_CLIENT_PASSWORD", "")
STORE_CLIENT_KEY = get_env_variable("STORE_CLIENT_KEY", "")
STORE_CLIENT_CERT = get_env_variable("STORE_CLIENT_CERT", "")
STORE_URL = get_env_variable("STORE_URL")
SESSION_COOKIE_NAME = "csessid%x" % (((getnode() // 139) ^
(getnode() % 983)) & 0xffff)
......@@ -46,8 +46,9 @@ CACHES = {
LOGGING['loggers']['djangosaml2'] = {'handlers': ['console'],
'level': 'CRITICAL'}
LOGGING['handlers']['console'] = {'level': 'WARNING',
level = environ.get('LOGLEVEL', 'CRITICAL')
LOGGING['handlers']['console'] = {'level': level,
'class': 'logging.StreamHandler',
'formatter': 'simple'}
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(
url(r'^network/', include('network.urls')),
url(r'^dashboard/', include('dashboard.urls')),
url((r'^accounts/reset/(?P<uidb36>[0-9A-Za-z]{1,13})-'
'(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$'),
# django/contrib/auth/urls.py (care when new version)
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',
{'set_password_form': CircleSetPasswordForm},
name='accounts.password_reset_confirm'
......@@ -64,3 +65,5 @@ if get_env_variable('DJANGO_SAML', 'FALSE') == 'TRUE':
'',
(r'^saml2/', include('djangosaml2.urls')),
)
handler500 = 'common.views.handler500'
......@@ -21,13 +21,19 @@ from hashlib import sha224
from itertools import chain, imap
from logging import getLogger
from time import time
from warnings import warn
from django.contrib.auth.models import User
from django.core.cache import cache
from django.db.models import (CharField, DateTimeField, ForeignKey,
NullBooleanField, TextField)
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import (
CharField, DateTimeField, ForeignKey, NullBooleanField
)
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
......@@ -45,7 +51,11 @@ def activitycontextimpl(act, on_abort=None, on_commit=None):
# BaseException is the common parent of Exception and
# system-exiting exceptions, e.g. KeyboardInterrupt
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
else:
act.finish(succeeded=True, event_handler=on_commit)
......@@ -103,8 +113,23 @@ def split_activity_code(activity_code):
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):
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')
task_uuid = CharField(blank=True, max_length=50, null=True, unique=True,
help_text=_('Celery task unique identifier.'),
......@@ -120,8 +145,9 @@ class ActivityModel(TimeStampedModel):
succeeded = NullBooleanField(blank=True, null=True,
help_text=_('True, if the activity has '
'finished successfully.'))
result = TextField(verbose_name=_('result'), blank=True, null=True,
help_text=_('Human readable result of activity.'))
result_data = JSONField(verbose_name=_('result'), blank=True, null=True,
dump_kwargs={"cls": Encoder},
help_text=_('Human readable result of activity.'))
def __unicode__(self):
if self.parent:
......@@ -150,6 +176,29 @@ class ActivityModel(TimeStampedModel):
def has_failed(self):
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
"""Cache return value of decorated method to memcached and memory.
......@@ -299,3 +348,69 @@ try:
], patterns=['common\.models\.'])
except ImportError:
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):
skip_auth_check = auxargs.pop('system')
user = auxargs.pop('user')
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
argspec = getargspec(self._operation)
......@@ -72,7 +74,8 @@ class Operation(object):
self.check_auth(user)
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
......@@ -148,7 +151,7 @@ class Operation(object):
raise PermissionDenied("%s doesn't have the required permissions."
% user)
def create_activity(self, parent, user):
def create_activity(self, parent, user, kwargs):
raise NotImplementedError
def on_abort(self, activity, error):
......@@ -157,6 +160,18 @@ class Operation(object):
"""
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):
"""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):
"button",
AnyTag(
"i",
css_class="icon-play"
css_class="fa fa-play"
),
HTML(" Start"),
css_id="vm-create-customized-start",
......@@ -163,7 +163,7 @@ class VmCustomizeForm(forms.Form):
Div( # cpu priority
Div(
HTML('<label for="vm-cpu-priority-slider">'
'<i class="icon-trophy"></i> CPU priority'
'<i class="fa fa-trophy"></i> CPU priority'
'</label>'),
css_class="col-sm-3"
),
......@@ -182,7 +182,7 @@ class VmCustomizeForm(forms.Form):
Div( # cpu count
Div(
HTML('<label for="cpu-count-slider">'
'<i class="icon-cogs"></i> CPU count'
'<i class="fa fa-cogs"></i> CPU count'
'</label>'),
css_class="col-sm-3"
),
......@@ -201,7 +201,7 @@ class VmCustomizeForm(forms.Form):
Div( # ram size
Div(
HTML('<label for="ram-slider">'
'<i class="icon-ticket"></i> RAM amount'
'<i class="fa fa-ticket"></i> RAM amount'
'</label>'),
css_class="col-sm-3"
),
......@@ -313,7 +313,7 @@ class VmCustomizeForm(forms.Form):
"a",
AnyTag(
"i",
css_class="icon-plus-sign",
css_class="fa fa-plus-circle",
),
css_id=("vm-create-network-add"
"-button"),
......@@ -556,7 +556,7 @@ class NodeForm(forms.ModelForm):
"button",
AnyTag(
"i",
css_class="icon-play"
css_class="fa fa-play"
),
HTML("Start"),
css_id="node-create-submit",
......@@ -612,6 +612,9 @@ class TemplateForm(forms.ModelForm):
self.instance.ram_size = 512
self.instance.num_cores = 2
self.fields["lease"].queryset = Lease.get_objects_with_level(
"operator", self.user)
def clean_owner(self):
if self.instance.pk is not None:
return User.objects.get(pk=self.instance.owner.pk)
......@@ -888,6 +891,27 @@ class LeaseForm(forms.ModelForm):
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):
name = forms.CharField(max_length=100, label=_("Name"))
size = forms.CharField(
......@@ -934,7 +958,7 @@ class CircleAuthenticationForm(AuthenticationForm):
"span",
AnyTag(
"i",
css_class="icon-user",
css_class="fa fa-user",
),
css_class="input-group-addon",
),
......@@ -948,7 +972,7 @@ class CircleAuthenticationForm(AuthenticationForm):
"span",
AnyTag(
"i",
css_class="icon-lock",
css_class="fa fa-lock",
),
css_class="input-group-addon",
),
......@@ -976,7 +1000,7 @@ class CirclePasswordResetForm(PasswordResetForm):
"span",
AnyTag(
"i",
css_class="icon-envelope",
css_class="fa fa-envelope",
),
css_class="input-group-addon",
),
......
......@@ -30,16 +30,17 @@ from django.db.models import (
DateTimeField, permalink, BooleanField
)
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.utils.translation import ugettext_lazy as _, override, ugettext
from django.utils.translation import ugettext_lazy as _
from django_sshkey.models import UserKey
from jsonfield import JSONField
from model_utils.models import TimeStampedModel
from model_utils.fields import StatusField
from model_utils import Choices
from acl.models import AclBase
from common.models import HumanReadableObject, create_readable, Encoder
from vm.tasks.agent_tasks import add_keys, del_keys
......@@ -62,26 +63,39 @@ class Notification(TimeStampedModel):
status = StatusField()
to = ForeignKey(User)
subject = CharField(max_length=128)
message = TextField()
subject_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
message_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
valid_until = DateTimeField(null=True, default=None)
class Meta:
ordering = ['-created']
@classmethod
def send(cls, user, subject, template, context={}, valid_until=None):
try:
language = user.profile.preferred_language
except:
language = None
with override(language):
context['user'] = user
rendered = render_to_string(template, context)
subject = ugettext(unicode(subject))
return cls.objects.create(to=user, subject=subject, message=rendered,
def send(cls, user, subject, template, context,
valid_until=None, subject_context=None):
hro = create_readable(template, user=user, **context)
subject = create_readable(subject, subject_context or context)
return cls.objects.create(to=user,
subject_data=subject.to_dict(),
message_data=hro.to_dict(),
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):
user = OneToOneField(User)
......@@ -112,8 +126,11 @@ class Profile(Model):
default=2048,
help_text=_('Disk quota in mebibytes.'))
def notify(self, subject, template, context={}, valid_until=None):
return Notification.send(self.user, subject, template, context,
def notify(self, subject, template, context=None, valid_until=None,
**kwargs):
if context is not None:
kwargs.update(context)
return Notification.send(self.user, subject, template, kwargs,
valid_until)
def get_absolute_url(self):
......
......@@ -146,10 +146,10 @@
height: 26px;
margin-top: -4px!important;
margin-left: -6px !important;
border-radius: 0px;
-moz-border-radius: 0px;
-webkit-border-radius: 0px;
-webkit-border-radius: 0px;
text-shadow: 0 1px 0 #fff;
background-image: -webkit-gradient(linear, left 0%, left 100%, from(#428bca), to(#3071a9));
......@@ -196,10 +196,10 @@
}
.vm-slider {
width: 300px;
width: 300px;
}
.output {
padding-left: 10px;
font-weight: bold;
}
}
......@@ -145,13 +145,13 @@ html {
background-color: transparent;
}
.sub-timeline {
}
.sub-timeline {
}
.sub-activity {
margin-left: 30px;
padding-left: 10px;
border-left: 3px solid green;
border-left: 3px solid green;
}
.sub-activity-active {
......@@ -235,7 +235,7 @@ html {
#vm-details-rename, #vm-details-h1-name, #vm-details-rename ,
#node-details-rename, #node-details-rename *, #node-details-h1-name, #node-list-rename, #node-list-rename *#group-details-rename, #group-details-rename *, #group-details-h1-name, #group-list-rename, #group-list-rename * {
display: inline;
}
......@@ -302,8 +302,8 @@ html {
}
/* port add buttons */
.vm-details-network-port-add .input-group-addon, .vm-details-network-port-add .input-group-btn {
width: inherit ;
.vm-details-network-port-add .input-group-addon, .vm-details-network-port-add .input-group-btn {
width: inherit ;
}
/* vm-create */
......@@ -426,12 +426,12 @@ a.hover-black {
cursor: pointer;
}
#vm-migrate-node-list {
list-style: none;
}
#vm-migrate-node-list li {
padding-bottom: 10px;
#vm-migrate-node-list {
list-style: none;
}
#vm-migrate-node-list li {
padding-bottom: 10px;
}
.vm-migrate-node-property {
......@@ -446,7 +446,7 @@ a.hover-black {
/* fancy stuff
border: 1px solid #ccc;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
border-radius: 8px;
border-radius: 8px;
*/
}
......@@ -460,25 +460,25 @@ a.hover-black {
/* footer */
footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 30px;
background-color: #101010;
color: white;
font-size: 13px;
padding: 5px 5px 0 5px;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.4);
text-align: center;
}
footer a, footer a:hover, footer a:visited {
color: white;
footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 30px;
background-color: #101010;
color: white;
font-size: 13px;
padding: 5px 5px 0 5px;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.4);
text-align: center;
}
footer a, footer a:hover, footer a:visited {
color: white;
text-decoration: underline;
}
.template-disk-list {
list-style: none;
padding-left: 0;
......@@ -513,15 +513,15 @@ footer a, footer a:hover, footer a:visited {
}
/* template create vm help */
.alert-new-template {
background: #3071a9;
color: white;
font-size: 22px;
}
.alert-new-template ol {
.alert-new-template {
background: #3071a9;
color: white;
font-size: 22px;
}
.alert-new-template ol {
margin-left: 25px;
}
}
/* bootstrap tour */
.tour-template {
......@@ -542,11 +542,11 @@ footer a, footer a:hover, footer a:visited {
}
.index-vm-list-name {
display: inline-block;
max-width: 70%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
display: inline-block;
max-width: 70%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
float: left;
}
......@@ -555,11 +555,11 @@ footer a, footer a:hover, footer a:visited {
}
.index-template-list-name {
display: inline-block;
max-width: 50%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
display: inline-block;
max-width: 50%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
float: left;