From 8a6d558402bddfc9ab62812b3116e8f467467e09 Mon Sep 17 00:00:00 2001
From: mirsal <mirsal@mirsal.fr>
Date: Wed, 4 Aug 2021 11:32:32 +0000
Subject: [PATCH] Retrieve IPv6 addresses from VMs

This commit allows the model to fetch IPv6 addresses from running VMs
and populate VirtualMachine objects with the value if it was retrieved
successfully
---
 capsulflask/shell_scripts/get.sh |  5 +++--
 capsulflask/spoke_model.py       | 18 ++++++++++++------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/capsulflask/shell_scripts/get.sh b/capsulflask/shell_scripts/get.sh
index 6d57a35..ee579ef 100755
--- a/capsulflask/shell_scripts/get.sh
+++ b/capsulflask/shell_scripts/get.sh
@@ -29,7 +29,8 @@ if virsh domuuid "$vmname" | grep -vqE '^[\t\s\n]*$'; then
   esac
 fi
 
-# this gets the ipv4
+# this gets the vm ip addresses
 ipv4="$(virsh domifaddr "$vmname" | awk '/ipv4/ {print $4}' | cut -d'/' -f1)"
+ipv6="$(virsh domifaddr "$vmname" | awk '/ipv6/ {print $4}' | cut -d'/' -f1)"
 
-echo "$exists $state $ipv4"
\ No newline at end of file
+echo "$exists $state $ipv4 $ipv6"
diff --git a/capsulflask/spoke_model.py b/capsulflask/spoke_model.py
index 9ab0d3a..25107b0 100644
--- a/capsulflask/spoke_model.py
+++ b/capsulflask/spoke_model.py
@@ -114,24 +114,30 @@ class ShellScriptSpoke(VirtualizationInterface):
     if len(fields) < 3:
       return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], state=state)
 
-    ipaddr = fields[2]
+    ip4addr = fields[2]
 
-    if not re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", ipaddr):
+    if not re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", ip4addr):
       return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], state=state)
 
     if get_ssh_host_keys:
       try:
-        completedProcess2 = run([join(current_app.root_path, 'shell_scripts/ssh-keyscan.sh'), ipaddr], capture_output=True)
+        completedProcess2 = run([join(current_app.root_path, 'shell_scripts/ssh-keyscan.sh'), ip4addr], capture_output=True)
         self.validate_completed_process(completedProcess2)
         ssh_host_keys = json.loads(completedProcess2.stdout.decode("utf-8"))
-        return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], state=state, ipv4=ipaddr, ssh_host_keys=ssh_host_keys)
+        return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], state=state, ipv4=ip4addr, ssh_host_keys=ssh_host_keys)
       except:
         current_app.logger.warning(f"""
-          failed to ssh-keyscan {id} at {ipaddr}:
+          failed to ssh-keyscan {id} at {ip4addr}:
           {my_exec_info_message(sys.exc_info())}"""
         )
 
-    return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], state=state, ipv4=ipaddr)
+    if len(fields) < 4:
+        return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], state=state, ipv4=ip4addr)
+
+    ip6addr = fields[3]
+
+    return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], state=state, ipv4=ip4addr, ipv6=ip6addr)
+
 
   def list_ids(self) -> list:
     completedProcess = run([join(current_app.root_path, 'shell_scripts/list-ids.sh')], capture_output=True)