Commit 39025b6e by Szabolcs Gelencser

Divide SaveAsTemplateOperation to suboperations

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