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,25 +744,39 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -737,25 +744,39 @@ 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
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 # prepare parameters
params = { params = {
'access_method': self.instance.access_method, 'access_method': self.instance.access_method,
...@@ -778,8 +799,6 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -778,8 +799,6 @@ class SaveAsTemplateOperation(InstanceOperation):
params.update(kwargs) params.update(kwargs)
params.pop("parent_activity", None) params.pop("parent_activity", None)
#TODO: create data disks
# create template and do additional setup # create template and do additional setup
tmpl = InstanceTemplate(**params) tmpl = InstanceTemplate(**params)
tmpl.full_clean() # Avoiding database errors. tmpl.full_clean() # Avoiding database errors.
...@@ -792,7 +811,6 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -792,7 +811,6 @@ class SaveAsTemplateOperation(InstanceOperation):
tmpl.set_level(self.instance.template.owner, 'owner') tmpl.set_level(self.instance.template.owner, 'owner')
tmpl.set_level(user, 'owner') tmpl.set_level(user, 'owner')
try: try:
#TODO: add data disks
# create interface templates # create interface templates
for i in self.instance.interface_set.all(): for i in self.instance.interface_set.all():
i.save_as_template(tmpl) i.save_as_template(tmpl)
...@@ -817,7 +835,6 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -817,7 +835,6 @@ class SaveAsTemplateOperation(InstanceOperation):
template=reverse('dashboard.views.template-detail', template=reverse('dashboard.views.template-detail',
kwargs={'pk': tmpl.pk})) kwargs={'pk': tmpl.pk}))
@register_operation @register_operation
class ShutdownOperation(InstanceOperation): class ShutdownOperation(InstanceOperation):
id = 'shutdown' id = 'shutdown'
......
...@@ -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