Commit b81b0298 by Dudás Ádám

vm: add option to override network interfaces when creating VM from template

parent 5182afaa
...@@ -341,7 +341,8 @@ class Instance(VirtualMachineDescModel, TimeStampedModel): ...@@ -341,7 +341,8 @@ class Instance(VirtualMachineDescModel, TimeStampedModel):
return " ".join([s for s in parts if s != ""]) return " ".join([s for s in parts if s != ""])
@classmethod @classmethod
def create_from_template(cls, template, owner, disks=None, **kwargs): def create_from_template(cls, template, owner, disks=None, networks=None,
**kwargs):
"""Create a new instance based on an InstanceTemplate. """Create a new instance based on an InstanceTemplate.
Can also specify parameters as keyword arguments which should override Can also specify parameters as keyword arguments which should override
...@@ -349,6 +350,9 @@ class Instance(VirtualMachineDescModel, TimeStampedModel): ...@@ -349,6 +350,9 @@ class Instance(VirtualMachineDescModel, TimeStampedModel):
""" """
disks = template.disks.all() if disks is None else disks disks = template.disks.all() if disks is None else disks
networks = (template.interface_set.all() if networks is None
else networks)
# prepare parameters # prepare parameters
kwargs['template'] = template kwargs['template'] = template
kwargs['owner'] = owner kwargs['owner'] = owner
...@@ -366,10 +370,10 @@ class Instance(VirtualMachineDescModel, TimeStampedModel): ...@@ -366,10 +370,10 @@ class Instance(VirtualMachineDescModel, TimeStampedModel):
for disk in disks: for disk in disks:
inst.disks.add(disk.get_exclusive()) inst.disks.add(disk.get_exclusive())
for iftmpl in template.interface_set.all(): for net in networks:
i = Interface.create_from_template(instance=inst, i = Interface.create(instance=inst, vlan=net.vlan, owner=owner,
template=iftmpl, managed=net.managed)
owner=owner)
if i.host: if i.host:
i.host.enable_net() i.host.enable_net()
port, proto = ACCESS_PROTOCOLS[i.instance.access_method][1:3] port, proto = ACCESS_PROTOCOLS[i.instance.access_method][1:3]
...@@ -829,18 +833,17 @@ class Interface(Model): ...@@ -829,18 +833,17 @@ class Interface(Model):
queue=self.instance.get_remote_queue_name('net')) queue=self.instance.get_remote_queue_name('net'))
@classmethod @classmethod
def create_from_template(cls, instance, template, owner=None): def create(cls, instance, vlan, managed, owner=None):
"""Create a new interface for an instance based on an """Create a new interface for a VM instance to the specified VLAN.
InterfaceTemplate.
""" """
if template.managed: if managed:
host = Host() host = Host()
host.vlan = template.vlan host.vlan = vlan
# TODO change Host's mac field's type to EUI in firewall # TODO change Host's mac field's type to EUI in firewall
host.mac = str(cls.generate_mac(instance, template.vlan)) host.mac = str(cls.generate_mac(instance, vlan))
host.hostname = instance.vm_name host.hostname = instance.vm_name
# Get adresses from firewall # Get adresses from firewall
addresses = template.vlan.get_new_address() addresses = vlan.get_new_address()
host.ipv4 = addresses['ipv4'] host.ipv4 = addresses['ipv4']
host.ipv6 = addresses['ipv6'] host.ipv6 = addresses['ipv6']
host.owner = owner host.owner = owner
...@@ -848,7 +851,7 @@ class Interface(Model): ...@@ -848,7 +851,7 @@ class Interface(Model):
else: else:
host = None host = None
iface = cls(vlan=template.vlan, host=host, instance=instance) iface = cls(vlan=vlan, host=host, instance=instance)
iface.save() iface.save()
return iface return iface
......
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