From 156da87245c210f68c0d2a7ff86a6c8b815dfb0e Mon Sep 17 00:00:00 2001
From: Akkela-Saikrishna <akkela.saikrishna@ibm.com>
Date: Sat, 24 Feb 2024 13:01:10 -0600
Subject: [PATCH] concurrent deploy and global IP assignment fixes

---
 cloud-init-22.1/cloudinit/distros/aix.py      | 95 +++++++++++++++++--
 cloud-init-22.1/cloudinit/distros/aix_util.py | 58 +++--------
 2 files changed, 97 insertions(+), 56 deletions(-)

diff --git a/cloud-init-22.1/cloudinit/distros/aix.py b/cloud-init-22.1/cloudinit/distros/aix.py
index 9d6f12d..1129890 100644
--- a/cloud-init-22.1/cloudinit/distros/aix.py
+++ b/cloud-init-22.1/cloudinit/distros/aix.py
@@ -136,11 +136,40 @@ class Distro(distros.Distro):
                             chdev_cmd.append('-anetmask=' + ipv4_netmask)
                             
                     if run_autoconf6:
-                         command = "/usr/sbin/autoconf6 -i en0"
+                         command = "/etc/ifconfig -a"
                          output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                         print("------------------------------------------------------------")
+                         print("Output of " + command)
+                         print(output)
+                         print("------------------------------------------------------------")
+                         command = ["/usr/sbin/rmdev", "-l", "en0"]
+                         subp.subp(command, logstring=command)
+                         print(" ".join(command) + "executed")
                          time.sleep(2)
-                         command = "stopsrc -s ndpd-host ; sleep 3 ; startsrc -s ndpd-host ;"
+                         command = "/etc/ifconfig -a"
                          output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                         print("------------------------------------------------------------")
+                         print("Output of " + command)
+                         print(output)
+                         print("------------------------------------------------------------")
+                         command = "/usr/bin/netstat -rn "
+                         output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                         print("------------------------------------------------------------")
+                         print("Output of " + command)
+                         print(output)
+                         print("------------------------------------------------------------")
+
+                         command = ["/usr/sbin/autoconf6", "-6", "-R", "-i", "en0"]
+                         subp.subp(command, logstring=command)
+                         print(" ".join(command) + "executed")
+                         util.append_file("/etc/rc.tcpip", "%s\n" % ("/usr/sbin/autoconf6 -6 -R -i en0          #cloud-init"))
+                         time.sleep(2)
+                         command = "/usr/bin/netstat -rn "
+                         output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                         print("------------------------------------------------------------")
+                         print("Output of " + command)
+                         print(output)
+                         print("------------------------------------------------------------")
 
                     if run_cmd:
                         try:
@@ -148,6 +177,14 @@ class Distro(distros.Distro):
                             subp.subp(chdev_cmd, logstring=chdev_cmd)
                             time.sleep(2)
 
+                            print(" ".join(chdev_cmd) + "executed")
+                            command = "/usr/bin/netstat -rn "
+                            output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                            print("------------------------------------------------------------")
+                            print("Output of " + command)
+                            print(output)
+                            print("------------------------------------------------------------")
+
                             if ipv6_present:
                                 util.append_file("/etc/rc.tcpip", "%s\n" % (" ".join(chdev_cmd)))
                             
@@ -166,10 +203,49 @@ class Distro(distros.Distro):
                         if aix_dev == "en0":
                             if run_autoconf6 is True:
                                 aix_util.add_route("ipv6", ipv6_info.get('gateway'))
-                                #aix_util.disable_ndpd_host()
-                                #aix_util.enable_ndpd_host()
-                                util.append_file("/etc/rc.tcpip", "%s\n" % ("stopsrc -s ndpd-host ; sleep 3 ; startsrc -s ndpd-host ; #To remove default gateway for cloud-init "))
-                                util.append_file("/etc/rc.tcpip", "%s\n" % (" ".join(chdev_cmd)))
+                                #Modified 'command' from list to string. And stopped using the routine subp.subp()
+                                #Since, it has issues in passing multiple arguments at once. It considers them as 
+                                #a single byte stream and thus throws the invalid arg errors.
+                                #command = ['startsrc', '-s', 'ndpd-host', '-a "-g -p"']
+                                command = 'startsrc -s ndpd-host -a "-g -p"'
+                                #(_out, err) = subp.subp(command)
+                                output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                                print("------------------------------------------------------------")
+                                print("Output of " + command)
+                                print(output)
+                                print("------------------------------------------------------------")
+                                LOG.debug("Ran command: %s ", command)
+                                #need this delay, because ndpd-host was showing active if checked as soon as startsrc is run
+                                time.sleep(10)
+                                tmp_cmd = "lssrc -s ndpd-host | grep ndpd-host"
+                                output = subprocess.check_output(tmp_cmd, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                                print("------------------------------------------------------------")
+                                print("Output of " + tmp_cmd)
+                                print(output)
+                                print("------------------------------------------------------------")
+                                if "inoperative" in output:
+                                    LOG.debug(
+                                        "Running %s resulted in stderr output",
+                                        command
+                                    )
+                                    LOG.debug("ndpd-host does not support option, p. Running it without option, p")
+                                    #command = ['startsrc', '-s', 'ndpd-host', '-a "-g -v"']
+                                    command = 'startsrc -s ndpd-host -a "-g -v"'
+                                    #subp.subp(command, logstring=command)
+                                    output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                                    print("------------------------------------------------------------")
+                                    print("Output of " + command)
+                                    print(output)
+                                    print("------------------------------------------------------------")
+                                    LOG.debug("Ran command: %s ", command)
+                                print(command + " executed")
+                                util.append_file("/etc/rc.tcpip", "%s\n" % (command + '          #cloud-init'))
+                                command = "/usr/bin/netstat -rn "
+                                output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+                                print("------------------------------------------------------------")
+                                print("Output of " + command)
+                                print(output)
+                                print("------------------------------------------------------------")
                             else:
                                 aix_util.add_route("ipv4", info.get('gateway'))
 
@@ -180,9 +256,10 @@ class Distro(distros.Distro):
 
         if run_dhcpcd:
             aix_util.enable_dhcpcd()
-        if run_autoconf6:
-            aix_util.enable_ndpd_host()
-            aix_util.enable_autoconf6(ipv6_interface)
+#As per the new tested order, we dont need these two steps here.
+#        if run_autoconf6:
+#            aix_util.enable_ndpd_host()
+#            aix_util.enable_autoconf6(ipv6_interface)
 
         if ((nameservers and len(nameservers) > 0) or (
             searchservers and len(searchservers) > 0)):
diff --git a/cloud-init-22.1/cloudinit/distros/aix_util.py b/cloud-init-22.1/cloudinit/distros/aix_util.py
index a7bb61a..7f4ecca 100644
--- a/cloud-init-22.1/cloudinit/distros/aix_util.py
+++ b/cloud-init-22.1/cloudinit/distros/aix_util.py
@@ -52,62 +52,26 @@ def add_route(network, route):
     # Add the route if there isn't already a default route
     cmd = ['/usr/sbin/chdev', '-l', 'inet0']
 
+    time.sleep(2)
     if route:
         if network == 'ipv4':
             cmd.extend(["-aroute=" + "net,-hopcount,0,,0," + route])
         elif network == 'ipv6':
-            cmd.extend(["-arout6=" + "net,-hopcount,0,,,::," + route])
+            cmd.extend(["-arout6=" + "net,-hopcount,0,-static,,,::," + route])
     cmd_string=' '.join(cmd)
     subp.subp(cmd, capture=False, rcs=[0, 1])
     time.sleep(2)
-
-    util.append_file("/etc/rc.tcpip", "%s\n" % (" ".join(cmd)))
-    if network == 'ipv6':
-       print("netstat -rn")
-       command = "/usr/bin/netstat -rn | grep default"
-       output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-       print(output)
-       print("netstat -in")
-       command = "/usr/bin/netstat -in "
-       output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-       print(output)
-       cmd_out = subprocess.check_output(cmd_string, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-
-       try:
-          print(cmd_string)
-          cmd_out = subprocess.check_output(cmd_string, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-          print(cmd_out)
-          time.sleep(2)
-          command = "/usr/bin/netstat -rn | grep default"
-          output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-          print(output)
-          # Run the command and capture its output
-          command = "netstat -rn | grep -v link | grep default"
-          output = subprocess.check_output(command, shell=True, text=True)
-
-
-       except subprocess.CalledProcessError as e:
-          cmd_out= subprocess.check_output(cmd_string, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-          print(cmd_out)
-          time.sleep(2)
-       else:
-          # Split the output into lines and count them
-          lines = output.split('\n')
-          non_empty_lines = [line for line in lines if line.strip() != ""]
-          line_count = len(non_empty_lines)
-          print("line_count :", line_count)
-          # Check if there are more than one line
-          if line_count > 1:
-             print("stopsrc -s ndpd-host ; sleep 3 ; startsrc -s ndpd-host ;")
-             command = "stopsrc -s ndpd-host ; sleep 3 ; startsrc -s ndpd-host ;"
-             output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-          else:
-             cmd_out= subprocess.check_output(cmd_string, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
-             print(cmd_out)
-             time.sleep(2)
-    command = "/usr/bin/netstat -rn | grep default"
+    print( cmd_string + " executed")
+    print("netstat -rn")
+    command = "/usr/bin/netstat -rn "
     output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
+    print("------------------------------------------------------------")
+    print("Output of " + command)
     print(output)
+    print("------------------------------------------------------------")
+
+    if network == 'ipv6':
+        util.append_file("/etc/rc.tcpip", "%s\n" % (" ".join(cmd)))
 
 
 
-- 
2.41.0

