Commit 385ad3e3 by Kálmán Viktor

Merge remote-tracking branch 'origin/master' into feature-template-wizard

parents 5912d4e8 d9fb045f
...@@ -400,3 +400,6 @@ LOGIN_REDIRECT_URL = "/" ...@@ -400,3 +400,6 @@ LOGIN_REDIRECT_URL = "/"
LOCALE_PATHS = (join(SITE_ROOT, 'locale'), ) LOCALE_PATHS = (join(SITE_ROOT, 'locale'), )
COMPANY_NAME = "BME IK 2014" COMPANY_NAME = "BME IK 2014"
SOUTH_MIGRATION_MODULES = {
'taggit': 'taggit.south_migrations',
}
...@@ -134,8 +134,8 @@ class OperatedMixin(object): ...@@ -134,8 +134,8 @@ class OperatedMixin(object):
"""Yield Operations that match permissions of user and preconditions. """Yield Operations that match permissions of user and preconditions.
""" """
for name in getattr(self, operation_registry_name, {}): for name in getattr(self, operation_registry_name, {}):
try:
op = getattr(self, name) op = getattr(self, name)
try:
op.check_auth(user) op.check_auth(user)
op.check_precond() op.check_precond()
except: except:
......
{% extends "dashboard/operate.html" %}
{% load i18n %}
{% load sizefieldtags %}
{% block question %}
<p>
{% blocktrans %}
Choose a name for the new template.
{% endblocktrans %}
</p>
<p class="text-info">{{op.name}}: {{op.description}}</p>
{% endblock %}
{% block formfields %}
<div class="form-group">
<label for="input-name" class="col-sm-4">{% trans "Name of template" %}</label>
<div class="col-sm-8">
<input type="text" value="{{name}}" name="name" class="form-control" />
</div>
</div>
{% endblock %}
...@@ -112,6 +112,73 @@ class VmOperationViewTestCase(unittest.TestCase): ...@@ -112,6 +112,73 @@ class VmOperationViewTestCase(unittest.TestCase):
self.assertEquals( self.assertEquals(
view.as_view()(request, pk=1234).render().status_code, 200) view.as_view()(request, pk=1234).render().status_code, 200)
def test_save_as_wo_name(self):
request = FakeRequestFactory(POST={})
view = vm_ops['save_as_template']
with patch.object(view, 'get_object') as go, \
patch('dashboard.views.messages') as msg, \
patch('dashboard.views.get_object_or_404') as go4:
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.save_as_template.async = MagicMock()
inst.has_level.return_value = True
go.return_value = inst
go4.return_value = MagicMock()
assert view.as_view()(request, pk=1234)['location']
assert not msg.error.called
def test_save_as_w_name(self):
request = FakeRequestFactory(POST={'name': 'foobar'})
view = vm_ops['save_as_template']
with patch.object(view, 'get_object') as go, \
patch('dashboard.views.messages') as msg, \
patch('dashboard.views.get_object_or_404') as go4:
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.save_as_template.async = MagicMock()
inst.has_level.return_value = True
go.return_value = inst
go4.return_value = MagicMock()
assert view.as_view()(request, pk=1234)['location']
assert not msg.error.called
def test_save_as_failed(self):
request = FakeRequestFactory(POST={})
view = vm_ops['save_as_template']
with patch.object(view, 'get_object') as go, \
patch('dashboard.views.messages') as msg, \
patch('dashboard.views.get_object_or_404') as go4:
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.save_as_template.async = MagicMock()
inst.save_as_template.async.side_effect = Exception
inst.has_level.return_value = True
go.return_value = inst
go4.return_value = MagicMock()
assert view.as_view()(request, pk=1234)['location']
assert msg.error.called
def test_save_as_template(self):
request = FakeRequestFactory()
view = vm_ops['save_as_template']
with patch.object(view, 'get_object') as go:
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.name = 'foo'
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.has_level.return_value = True
go.return_value = inst
rend = view.as_view()(request, pk=1234).render()
self.assertEquals(rend.status_code, 200)
assert 'foo v1' in rend.content
def FakeRequestFactory(*args, **kwargs): def FakeRequestFactory(*args, **kwargs):
''' FakeRequestFactory, FakeMessages and FakeRequestContext are good for ''' FakeRequestFactory, FakeMessages and FakeRequestContext are good for
......
...@@ -513,7 +513,7 @@ class VmMigrateView(VmOperationView): ...@@ -513,7 +513,7 @@ class VmMigrateView(VmOperationView):
template_name = 'dashboard/_vm-migrate.html' template_name = 'dashboard/_vm-migrate.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
ctx = super(VmOperationView, self).get_context_data(**kwargs) ctx = super(VmMigrateView, self).get_context_data(**kwargs)
ctx['nodes'] = [n for n in Node.objects.filter(enabled=True) ctx['nodes'] = [n for n in Node.objects.filter(enabled=True)
if n.state == "ONLINE"] if n.state == "ONLINE"]
return ctx return ctx
...@@ -528,6 +528,26 @@ class VmMigrateView(VmOperationView): ...@@ -528,6 +528,26 @@ class VmMigrateView(VmOperationView):
return super(VmMigrateView, self).post(request, extra, *args, **kwargs) return super(VmMigrateView, self).post(request, extra, *args, **kwargs)
class VmSaveView(VmOperationView):
op = 'save_as_template'
icon = 'save'
template_name = 'dashboard/_vm-save.html'
def get_context_data(self, **kwargs):
ctx = super(VmSaveView, self).get_context_data(**kwargs)
ctx['name'] = self.get_op()._rename(self.object.name)
return ctx
def post(self, request, extra=None, *args, **kwargs):
if extra is None:
extra = {}
name = self.request.POST.get("name")
if name:
extra["name"] = name
return super(VmSaveView, self).post(request, extra, *args, **kwargs)
vm_ops = { vm_ops = {
'reset': VmOperationView.factory(op='reset', icon='bolt'), 'reset': VmOperationView.factory(op='reset', icon='bolt'),
'deploy': VmOperationView.factory(op='deploy', icon='play'), 'deploy': VmOperationView.factory(op='deploy', icon='play'),
...@@ -535,8 +555,7 @@ vm_ops = { ...@@ -535,8 +555,7 @@ vm_ops = {
'reboot': VmOperationView.factory(op='reboot', icon='refresh'), 'reboot': VmOperationView.factory(op='reboot', icon='refresh'),
'shut_off': VmOperationView.factory(op='shut_off', icon='ban-circle'), 'shut_off': VmOperationView.factory(op='shut_off', icon='ban-circle'),
'shutdown': VmOperationView.factory(op='shutdown', icon='off'), 'shutdown': VmOperationView.factory(op='shutdown', icon='off'),
'save_as_template': VmOperationView.factory( 'save_as_template': VmSaveView,
op='save_as_template', icon='save'),
'destroy': VmOperationView.factory(op='destroy', icon='remove'), 'destroy': VmOperationView.factory(op='destroy', icon='remove'),
'sleep': VmOperationView.factory(op='sleep', icon='moon'), 'sleep': VmOperationView.factory(op='sleep', icon='moon'),
'wake_up': VmOperationView.factory(op='wake_up', icon='sun'), 'wake_up': VmOperationView.factory(op='wake_up', icon='sun'),
...@@ -2342,7 +2361,7 @@ class DiskRemoveView(DeleteView): ...@@ -2342,7 +2361,7 @@ class DiskRemoveView(DeleteView):
disk = self.get_object() disk = self.get_object()
app = disk.get_appliance() app = disk.get_appliance()
app.disks.remove(disk) app.remove_disk(disk=disk, user=request.user)
disk.destroy() disk.destroy()
next_url = request.POST.get("next") next_url = request.POST.get("next")
......
...@@ -231,6 +231,9 @@ class ReloadTestCase(TestCase): ...@@ -231,6 +231,9 @@ class ReloadTestCase(TestCase):
self.rm.save() self.rm.save()
self.rt.save() self.rt.save()
def tearDown(self):
settings["default_host_groups"] = []
def test_bad_aaaa_record(self): def test_bad_aaaa_record(self):
self.assertRaises(AddrFormatError, ipv6_to_octal, self.rb.address) self.assertRaises(AddrFormatError, ipv6_to_octal, self.rb.address)
......
...@@ -111,8 +111,7 @@ class InstanceTemplate(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -111,8 +111,7 @@ class InstanceTemplate(AclBase, VirtualMachineDescModel, TimeStampedModel):
('operator', _('operator')), ('operator', _('operator')),
('owner', _('owner')), # superuser, can delete, delegate perms ('owner', _('owner')), # superuser, can delete, delegate perms
) )
name = CharField(max_length=100, unique=True, name = CharField(max_length=100, verbose_name=_('name'),
verbose_name=_('name'),
help_text=_('Human readable name of template.')) help_text=_('Human readable name of template.'))
description = TextField(verbose_name=_('description'), blank=True) description = TextField(verbose_name=_('description'), blank=True)
parent = ForeignKey('self', null=True, blank=True, parent = ForeignKey('self', null=True, blank=True,
...@@ -166,6 +165,9 @@ class InstanceTemplate(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -166,6 +165,9 @@ class InstanceTemplate(AclBase, VirtualMachineDescModel, TimeStampedModel):
def get_absolute_url(self): def get_absolute_url(self):
return ('dashboard.views.template-detail', None, {'pk': self.pk}) return ('dashboard.views.template-detail', None, {'pk': self.pk})
def remove_disk(self, disk, **kwargs):
self.disks.remove(disk)
class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
TimeStampedModel): TimeStampedModel):
......
...@@ -80,6 +80,26 @@ class AddInterfaceOperation(InstanceOperation): ...@@ -80,6 +80,26 @@ class AddInterfaceOperation(InstanceOperation):
register_operation(AddInterfaceOperation) register_operation(AddInterfaceOperation)
class AddDiskOperation(InstanceOperation):
activity_code_suffix = 'add_disk'
id = 'add_disk'
name = _("add disk")
description = _("Add the specified disk to the VM.")
def check_precond(self):
super(AddDiskOperation, self).check_precond()
# TODO remove check when hot-attach is implemented
if self.instance.status not in ['STOPPED']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, activity, user, system, disk):
# TODO implement with hot-attach when it'll be available
return self.instance.disks.add(disk)
register_operation(AddDiskOperation)
class DeployOperation(InstanceOperation): class DeployOperation(InstanceOperation):
activity_code_suffix = 'deploy' activity_code_suffix = 'deploy'
id = 'deploy' id = 'deploy'
...@@ -217,6 +237,26 @@ class RemoveInterfaceOperation(InstanceOperation): ...@@ -217,6 +237,26 @@ class RemoveInterfaceOperation(InstanceOperation):
register_operation(RemoveInterfaceOperation) register_operation(RemoveInterfaceOperation)
class RemoveDiskOperation(InstanceOperation):
activity_code_suffix = 'remove_disk'
id = 'remove_disk'
name = _("remove disk")
description = _("Remove the specified disk from the VM.")
def check_precond(self):
super(RemoveDiskOperation, self).check_precond()
# TODO remove check when hot-detach is implemented
if self.instance.status not in ['STOPPED']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, activity, user, system, disk):
# TODO implement with hot-detach when it'll be available
return self.instance.disks.remove(disk)
register_operation(RemoveDiskOperation)
class ResetOperation(InstanceOperation): class ResetOperation(InstanceOperation):
activity_code_suffix = 'reset' activity_code_suffix = 'reset'
id = 'reset' id = 'reset'
...@@ -249,7 +289,7 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -249,7 +289,7 @@ class SaveAsTemplateOperation(InstanceOperation):
v = 1 v = 1
return "%s v%d" % (name, v) return "%s v%d" % (name, v)
def _operation(self, activity, user, system, timeout=300, def _operation(self, activity, user, system, timeout=300, name=None,
with_shutdown=True, **kwargs): with_shutdown=True, **kwargs):
if with_shutdown: if with_shutdown:
try: try:
...@@ -266,7 +306,7 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -266,7 +306,7 @@ class SaveAsTemplateOperation(InstanceOperation):
'description': self.instance.description, 'description': self.instance.description,
'lease': self.instance.lease, # Can be problem in new VM 'lease': self.instance.lease, # Can be problem in new VM
'max_ram_size': self.instance.max_ram_size, 'max_ram_size': self.instance.max_ram_size,
'name': self._rename(self.instance.name), 'name': name or self._rename(self.instance.name),
'num_cores': self.instance.num_cores, 'num_cores': self.instance.num_cores,
'owner': user, 'owner': user,
'parent': self.instance.template, # Can be problem 'parent': self.instance.template, # Can be problem
......
...@@ -27,7 +27,7 @@ def garbage_collector(timeout=15): ...@@ -27,7 +27,7 @@ def garbage_collector(timeout=15):
now = timezone.now() now = timezone.now()
for i in Instance.objects.filter(destroyed_at=None).all(): for i in Instance.objects.filter(destroyed_at=None).all():
if i.time_of_delete and now > i.time_of_delete: if i.time_of_delete and now > i.time_of_delete:
i.destroy.async() i.destroy.async(system=True)
logger.info("Expired instance %d destroyed.", i.pk) logger.info("Expired instance %d destroyed.", i.pk)
try: try:
i.owner.profile.notify( i.owner.profile.notify(
...@@ -39,7 +39,7 @@ def garbage_collector(timeout=15): ...@@ -39,7 +39,7 @@ def garbage_collector(timeout=15):
i.pk, unicode(e)) i.pk, unicode(e))
elif (i.time_of_suspend and now > i.time_of_suspend and elif (i.time_of_suspend and now > i.time_of_suspend and
i.state == 'RUNNING'): i.state == 'RUNNING'):
i.sleep.async() i.sleep.async(system=True)
logger.info("Expired instance %d suspended." % i.pk) logger.info("Expired instance %d suspended." % i.pk)
try: try:
i.owner.profile.notify( i.owner.profile.notify(
......
...@@ -39,8 +39,8 @@ class TemplateTestCase(TestCase): ...@@ -39,8 +39,8 @@ class TemplateTestCase(TestCase):
class InstanceTestCase(TestCase): class InstanceTestCase(TestCase):
def test_is_running(self): def test_is_running(self):
inst = Mock(state='RUNNING') inst = MagicMock(status='RUNNING')
assert Instance.is_running.getter(inst) self.assertTrue(Instance.is_running.fget(inst))
def test_mon_stopped_while_activity_running(self): def test_mon_stopped_while_activity_running(self):
node = Mock() node = Mock()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment