Commit 39025b6e by Szabolcs Gelencser

Divide SaveAsTemplateOperation to suboperations

parent dceb22e2
......@@ -718,15 +718,22 @@ class SaveAsTemplateOperation(InstanceOperation):
description = _("Save virtual machine as a template so they can be shared "
"with users and groups. Anyone who has access to a "
"template (and to the networks it uses) will be able to "
"start an instance of it.")
"start an instance of it.\n"
"For linux run 'sudo waagent -deprovision+user' "
"before this operation")
required_perms = ('vm.create_template', )
accept_states = ('RUNNING', 'STOPPED')
async_queue = "localhost.man.slow"
accept_states = ('RUNNING')
def is_preferred(self):
return (self.instance.is_base and
self.instance.status == self.instance.STATUS.RUNNING)
def on_abort(self, activity, error):
activity.resultant_state = 'RUNNING'
def on_commit(self, activity):
activity.resultant_state = 'STOPPED'
@staticmethod
def _rename(name):
m = search(r" v(\d+)$", name)
......@@ -737,86 +744,96 @@ class SaveAsTemplateOperation(InstanceOperation):
v = 1
return "%s v%d" % (name, v)
def on_abort(self, activity, error):
if hasattr(self, 'disks'):
for disk in self.disks:
disk.destroy()
def _operation(self, activity, user, system, name=None,
with_shutdown=True, clone=False, task=None, **kwargs):
try:
self.instance._cleanup(parent_activity=activity, user=user)
except:
pass
self.instance._deprovision(parent_activity=activity)
self.instance._for_template_shutdown(parent_activity=activity)
self.instance._capture(parent_activity=activity, name=name,
user=user, kwargs=kwargs, clone=clone)
if with_shutdown:
try:
self.instance.shutdown(parent_activity=activity,
user=user, task=task)
except Instance.WrongStateError:
pass
@register_operation
class DeprovisionOperation(SubOperationMixin, InstanceOperation):
id = '_deprovision'
name = _("deprovision")
# prepare parameters
params = {
'access_method': self.instance.access_method,
'arch': self.instance.arch,
'boot_menu': self.instance.boot_menu,
'description': self.instance.description,
'lease': self.instance.lease, # Can be problem in new VM
'max_ram_size': self.instance.max_ram_size,
'name': name or self._rename(self.instance.name),
'num_cores': self.instance.num_cores,
'owner': user,
'parent': self.instance.template or None, # Can be problem
'priority': self.instance.priority,
'ram_size': self.instance.ram_size,
'raw_data': self.instance.raw_data,
'system': self.instance.system,
'azure_vm_size': self.instance.azure_vm_size,
'azure_template': self.instance.azure_template
}
params.update(kwargs)
params.pop("parent_activity", None)
#TODO: create data disks
# create template and do additional setup
tmpl = InstanceTemplate(**params)
tmpl.full_clean() # Avoiding database errors.
tmpl.save()
# Copy traits from the VM instance
tmpl.req_traits.add(*self.instance.req_traits.all())
if clone:
tmpl.clone_acl(self.instance.template)
# Add permission for the original owner of the template
tmpl.set_level(self.instance.template.owner, 'owner')
tmpl.set_level(user, 'owner')
try:
#TODO: add data disks
# create interface templates
for i in self.instance.interface_set.all():
i.save_as_template(tmpl)
# create template in azure
def _operation(self):
agent_tasks.deprovision.apply_async(
args=[self.instance.name], queue="localhost.localdomain.agent"
).get(timeout=1200)
@register_operation
class ShutdownOperation(SubOperationMixin, InstanceOperation):
id = '_for_template_shutdown'
name = _("Shutdown")
def _operation(self):
vm_tasks.shutdown.apply_async(
args=[self.instance.name], queue="localhost.vm.fast"
).get(timeout=1200)
@register_operation
class CaptureOperation(SubOperationMixin, InstanceOperation):
id = '_capture'
name = _("capture")
def _operation(self, name, user, kwargs, clone):
# prepare parameters
params = {
'access_method': self.instance.access_method,
'arch': self.instance.arch,
'boot_menu': self.instance.boot_menu,
'description': self.instance.description,
'lease': self.instance.lease, # Can be problem in new VM
'max_ram_size': self.instance.max_ram_size,
'name': name or self._rename(self.instance.name),
'num_cores': self.instance.num_cores,
'owner': user,
'parent': self.instance.template or None, # Can be problem
'priority': self.instance.priority,
'ram_size': self.instance.ram_size,
'raw_data': self.instance.raw_data,
'system': self.instance.system,
'azure_vm_size': self.instance.azure_vm_size,
'azure_template': self.instance.azure_template
}
params.update(kwargs)
params.pop("parent_activity", None)
# create template and do additional setup
tmpl = InstanceTemplate(**params)
tmpl.full_clean() # Avoiding database errors.
tmpl.save()
# Copy traits from the VM instance
tmpl.req_traits.add(*self.instance.req_traits.all())
if clone:
tmpl.clone_acl(self.instance.template)
# Add permission for the original owner of the template
tmpl.set_level(self.instance.template.owner, 'owner')
tmpl.set_level(user, 'owner')
try:
vhd_uri = vm_tasks.save_as_template.apply_async(
args=[self.instance.name, self.instance.pk],
queue="localhost.vm.fast"
).get(timeout=1200)
tmpl.vhd_uri = vhd_uri
tmpl.save()
#TODO: delete VM (cannot start VM in Azure after generalization)
#self.instance.destroy()
# create interface templates
for i in self.instance.interface_set.all():
i.save_as_template(tmpl)
# create template in azure
try:
vhd_uri = vm_tasks.save_as_template.apply_async(
args=[self.instance.name, self.instance.pk],
queue="localhost.vm.fast"
).get(timeout=1200)
tmpl.vhd_uri = vhd_uri
tmpl.save()
#TODO: delete VM (cannot start VM in Azure after generalization)
#self.instance.destroy()
except:
raise
except:
tmpl.delete()
raise
except:
tmpl.delete()
raise
else:
return create_readable(
ugettext_noop("New template: %(template)s"),
template=reverse('dashboard.views.template-detail',
kwargs={'pk': tmpl.pk}))
else:
return create_readable(
ugettext_noop("New template: %(template)s"),
template=reverse('dashboard.views.template-detail',
kwargs={'pk': tmpl.pk}))
@register_operation
class ShutdownOperation(InstanceOperation):
......
......@@ -17,6 +17,10 @@
from manager.mancelery import celery
@celery.task(name='agent.deprovision')
def deprovision(vm_name):
pass
@celery.task(name='agent.install_diagnostics_extension')
def install_diagnostics_extension(vm_name):
pass
......
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