fix: avoid recreation of net mac addrs during VM update
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Robert Kaussow 2021-12-06 21:12:30 +01:00
parent d1f6175719
commit cf36ca664a
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0

View File

@ -777,7 +777,6 @@ def node_check(proxmox, node):
def get_vminfo(module, proxmox, node, vmid, **kwargs): def get_vminfo(module, proxmox, node, vmid, **kwargs):
global results # noqa global results # noqa
results = {} results = {}
mac = {}
try: try:
vm = proxmox.nodes(node).qemu(vmid).config.get() vm = proxmox.nodes(node).qemu(vmid).config.get()
except Exception as e: except Exception as e:
@ -795,8 +794,8 @@ def get_vminfo(module, proxmox, node, vmid, **kwargs):
kwargs.update(kwargs[k]) kwargs.update(kwargs[k])
del kwargs[k] del kwargs[k]
results["mac"] = mac results["nets"] = _extract_nets(vm)
results["devices"] = _extract_devices(vm) results["disks"] = _extract_disks(vm)
results["vmid"] = int(vmid) results["vmid"] = int(vmid)
results["_raw"] = vm results["_raw"] = vm
@ -866,16 +865,28 @@ def create_vm(
urlencoded_ssh_keys = quote(kwargs["sshkeys"], safe="") urlencoded_ssh_keys = quote(kwargs["sshkeys"], safe="")
kwargs["sshkeys"] = str(urlencoded_ssh_keys) kwargs["sshkeys"] = str(urlencoded_ssh_keys)
disks = {}
for item in [kwargs[i] for i in ["scsi", "virtio", "ide", "sata"] if i in kwargs]: for item in [kwargs[i] for i in ["scsi", "virtio", "ide", "sata"] if i in kwargs]:
devices = _extract_devices(item) disks = _extract_disks(item)
nets = {}
for item in [kwargs[i] for i in ["net"] if i in kwargs]:
nets = _extract_nets(item)
# If update, ensure existing disks are not recreated. # If update, ensure existing disks are not recreated.
if update: if update:
for k, v in devices.items(): for k, v in disks.items():
if results["devices"].get(k): if results["disks"].get(k):
kwargs[k.rstrip(string.digits)][k] = "{0}:{1},{2}".format( kwargs[k.rstrip(string.digits)][k] = "{0}:{1},{2}".format(
results["devices"][k]["storage_id"], results["devices"][k]["storage_opts"], results["disks"][k]["storage_id"], results["disks"][k]["storage_opts"],
",".join(devices[k]["opts"]) ",".join(disks[k]["opts"])
)
for k, v in nets.items():
if results["nets"].get(k):
kwargs[k.rstrip(string.digits)][k] = "{0}={1},{2}".format(
results["nets"][k]["net_id"], results["nets"][k]["net_opts"],
",".join(nets[k]["opts"])
) )
# Convert all dict in kwargs to elements. # Convert all dict in kwargs to elements.
@ -1528,20 +1539,37 @@ def main():
) )
def _extract_devices(item): def _extract_disks(item):
devices = defaultdict(dict) disks = defaultdict(dict)
for k, v in item.items(): for k, v in item.items():
if re.match(r"(scsi|virtio|ide|sata)[0-9]", k): if re.match(r"(scsi|virtio|ide|sata)[0-9]", k):
devices[k]["opts"] = [] disks[k]["opts"] = []
for val in v.split(","): for val in v.split(","):
if len(val.split(":")) == 2: if len(val.split(":")) == 2:
storage = val.split(":") storage = val.split(":")
devices[k]["storage_id"] = storage[0] disks[k]["storage_id"] = storage[0]
devices[k]["storage_opts"] = storage[1] disks[k]["storage_opts"] = storage[1]
else: else:
devices[k]["opts"].append(val) disks[k]["opts"].append(val)
return devices return disks
def _extract_nets(item):
nets = defaultdict(dict)
for k, v in item.items():
if re.match(r"net[0-9]", k):
nets[k]["opts"] = []
for val in v.split(","):
if any(val.startswith(s) for s in ["e1000", "rtl8139", "virtio", "vmxnet3"]):
if len(val.split("=")) == 2:
net = val.split("=")
nets[k]["net_id"] = net[0]
nets[k]["net_opts"] = net[1]
else:
nets[k]["opts"].append(val)
return nets
if __name__ == "__main__": if __name__ == "__main__":