Commit 1b86ea7f by Dudás Ádám

vm: implement saving VM instance as template

parent adbea233
......@@ -206,7 +206,7 @@ class Disk(TimeStampedModel):
local_tasks.restore.apply_async(args=[self, user],
def save_as(self, name, user=None, task_uuid=None):
def save_as(self, user=None, task_uuid=None):
mapping = {
'qcow2-snap': ('qcow2-norm', self.base),
......@@ -226,7 +226,7 @@ class Disk(TimeStampedModel):
new_type, new_base = mapping[self.type]
disk = Disk.objects.create(base=new_base, datastore=self.datastore,
filename=filename, name=name,
size=self.size, type=new_type)
queue_name = self.datastore.hostname + ".storage"
......@@ -359,9 +359,10 @@ class Instance(BaseResourceConfigModel, TimeStampedModel):
inst = cls(**kwargs)
# save instance
# create related entities
for disk in template.disks.all():
# create related entities
for iftmpl in template.interface_set.all():
i = Interface.create_from_template(instance=inst,
......@@ -689,6 +690,39 @@ class Instance(BaseResourceConfigModel, TimeStampedModel):
return local_tasks.reboot.apply_async(args=[self, user],
def save_as_template(self, name, **kwargs):
# prepare parameters
kwargs.setdefault('name', name)
kwargs.setdefault('description', self.description)
kwargs.setdefault('parent', self.template)
kwargs.setdefault('num_cores', self.num_cores)
kwargs.setdefault('ram_size', self.ram_size)
kwargs.setdefault('max_ram_size', self.max_ram_size)
kwargs.setdefault('arch', self.arch)
kwargs.setdefault('priority', self.priority)
kwargs.setdefault('boot_menu', self.boot_menu)
kwargs.setdefault('raw_data', self.raw_data)
kwargs.setdefault('access_method', self.access_method)
kwargs.setdefault('system', self.template.system
if self.template else None)
# create template and do additional setup
tmpl = InstanceTemplate(**kwargs)
# save template
# create related entities
for disk in self.disks.all():
d = disk.save_as()
except Disk.WrongDiskTypeError:
d = disk
for i in self.interface_set.all():
return tmpl
class InstanceActivity(ActivityModel):
......@@ -800,3 +834,11 @@ class Interface(Model):
iface = cls(vlan=template.vlan, host=host, instance=instance)
return iface
def save_as_template(self, instance_template):
"""Create a template based on this interface.
i = InterfaceTemplate(vlan=self.vlan, is not None,
return i
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