From 0cbc891cfbc064765851aa20a4d8439a7968d9f5 Mon Sep 17 00:00:00 2001 From: Emiliano Vavassori Date: Mon, 9 Sep 2024 17:23:10 +0200 Subject: [PATCH] Tentativo di fix della chiamata a funzione per indirizzi IP. Rimosso codice per Ansible per testing. --- pvm-ansible/__main__.py | 38 +++++++++++++++++++------------------- pvm-ansible/pvewrapper.py | 29 +++++++++++++++++------------ 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/pvm-ansible/__main__.py b/pvm-ansible/__main__.py index 313d9a3..f9595d1 100644 --- a/pvm-ansible/__main__.py +++ b/pvm-ansible/__main__.py @@ -85,25 +85,25 @@ vm = proxmox.vm.VirtualMachine(vm_name, ipv4_addresses = vm.vm_id.apply(lambda vm_id: pve.ipv4_addresses(f"{vm_id}")) -# Creating the inventory file -inventory = command.local.Command("a-inventory", - create = vm.ipv4_addresses.apply(lambda ipaddr: - f"echo '{vm_name} ansible_host={ipaddr[0]['ipv4_address']} ansible_user={vm_username}' >./inventory" - ), - delete = "rm -f ./inventory", - opts = pulumi.ResourceOptions(depends_on = [ ipv4_addresses ]) -) - -# Applying the command -for playbook in additional_config.playbooks: - shortname: str = playbook.split(".")[0] - command.local.Command(f"ap-{shortname}", - create = vm.ipv4_addresses.apply( - lambda run: f"ansible-playbook {run[0]['ipv4_address']}" - ), - delete = "rm -f ./ansible.log", - opts = pulumi.ResourceOptions(depends_on = [ inventory ]) - ) +## Creating the inventory file +#inventory = command.local.Command("a-inventory", +# create = vm.ipv4_addresses.apply(lambda ipaddr: +# f"echo '{vm_name} ansible_host={ipaddr[0]['ipv4_address']} ansible_user={vm_username}' >./inventory" +# ), +# delete = "rm -f ./inventory", +# opts = pulumi.ResourceOptions(depends_on = [ vm, ipv4_addresses ]) +#) +# +## Applying the command +#for playbook in additional_config.playbooks: +# shortname: str = playbook.split(".")[0] +# command.local.Command(f"ap-{shortname}", +# create = vm.ipv4_addresses.apply( +# lambda run: f"ansible-playbook {run[0]['ipv4_address']}" +# ), +# delete = "rm -f ./ansible.log", +# opts = pulumi.ResourceOptions(depends_on = [ inventory ]) +# ) # Outputs pulumi.export("ipv4_addresses", vm.ipv4_addresses) diff --git a/pvm-ansible/pvewrapper.py b/pvm-ansible/pvewrapper.py index 0d57e52..30ff008 100644 --- a/pvm-ansible/pvewrapper.py +++ b/pvm-ansible/pvewrapper.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # encoding: utf-8 -from typing import List, Optional +from typing import List from proxmoxer import ProxmoxAPI import time @@ -20,27 +20,32 @@ class PveWrapper: self.instance = self.conn.nodes(nodename) def ipv4_addresses(self, vmid: str) -> List[str]: - """Returns the list of ipv4_addresses.""" + """Returns the list of ipv4_addresses of the VM.""" - netints = {} while True: try: netints = self.instance.qemu(int(vmid)).agent.get("network-get-interfaces") except: - time.sleep(10) + time.sleep(5) continue else: - # When QEMU agent is running, return the array. + # When QEMU agent is running, all the needed informations are + # retrieved. No need to check each possible outcome. + # All non-lo interfaces filtered = [ x for x in netints['result'] if x['name'] != 'lo' ] - + retval = [] for interface in filtered: ipv4_addresses = [ x['ip-address'] for x in interface['ip-addresses'] if x['ip-address-type'] == 'ipv4' ] - # We assume a new machine has just been spawned, so it has just 1 ipv4 address. - ipv4_address = ipv4_addresses[0] - - retval.append({ "name": interface['name'], - "ipv4_address": ipv4_address }) - + # We assume a new machine has just been spawned, so it has just 1 ipv4 address per interface (if any). + ipv4_address = '' + if len(ipv4_addresses) >= 1: + ipv4_address = ipv4_addresses[0] + + retval.append({ + "name": interface['name'], + "address": ipv4_address + }) + return retval