rg9975 commented on code in PR #10376:
URL: https://github.com/apache/cloudstack/pull/10376#discussion_r1971767387


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java:
##########
@@ -47,15 +47,33 @@ public Answer execute(final GetVmIpAddressCommand command, 
final LibvirtComputin
         }
         String sanitizedVmName = sanitizeBashCommandArgument(vmName);
         String networkCidr = command.getVmNetworkCidr();
-        List<String[]> commands = new ArrayList<>();
         final String virt_ls_path = 
Script.getExecutableAbsolutePath("virt-ls");
         final String virt_cat_path = 
Script.getExecutableAbsolutePath("virt-cat");
         final String virt_win_reg_path = 
Script.getExecutableAbsolutePath("virt-win-reg");
         final String tail_path = Script.getExecutableAbsolutePath("tail");
         final String grep_path = Script.getExecutableAbsolutePath("grep");
         final String awk_path = Script.getExecutableAbsolutePath("awk");
         final String sed_path = Script.getExecutableAbsolutePath("sed");
-        if(!command.isWindows()) {
+        final String virsh_path = Script.getExecutableAbsolutePath("virsh");
+
+        // first run virsh domiflist to get the network interface name from 
libvirt.  This is set if qemu guest agent is running in the VM
+        // and is the most reliable and least intrusive
+        List<String[]> commands = new ArrayList<>();
+        commands.add(new String[]{virsh_path, "domifaddr", sanitizedVmName, 
"--source", "agent"});
+        String output = Script.executePipedCommands(commands, 0).second();
+        if (output != null) {
+            String[] lines = output.split("\n");
+            for (String line : lines) {
+                ip = parseDomIfListOutput(line, networkCidr);
+                if (ip != null) {
+                    break;
+                }
+            }
+        }

Review Comment:
   modularized better, fixed a few issues, and added test cases.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java:
##########
@@ -47,15 +47,33 @@ public Answer execute(final GetVmIpAddressCommand command, 
final LibvirtComputin
         }
         String sanitizedVmName = sanitizeBashCommandArgument(vmName);
         String networkCidr = command.getVmNetworkCidr();
-        List<String[]> commands = new ArrayList<>();
         final String virt_ls_path = 
Script.getExecutableAbsolutePath("virt-ls");
         final String virt_cat_path = 
Script.getExecutableAbsolutePath("virt-cat");
         final String virt_win_reg_path = 
Script.getExecutableAbsolutePath("virt-win-reg");
         final String tail_path = Script.getExecutableAbsolutePath("tail");
         final String grep_path = Script.getExecutableAbsolutePath("grep");
         final String awk_path = Script.getExecutableAbsolutePath("awk");
         final String sed_path = Script.getExecutableAbsolutePath("sed");
-        if(!command.isWindows()) {
+        final String virsh_path = Script.getExecutableAbsolutePath("virsh");
+
+        // first run virsh domiflist to get the network interface name from 
libvirt.  This is set if qemu guest agent is running in the VM
+        // and is the most reliable and least intrusive
+        List<String[]> commands = new ArrayList<>();
+        commands.add(new String[]{virsh_path, "domifaddr", sanitizedVmName, 
"--source", "agent"});
+        String output = Script.executePipedCommands(commands, 0).second();
+        if (output != null) {
+            String[] lines = output.split("\n");
+            for (String line : lines) {
+                ip = parseDomIfListOutput(line, networkCidr);
+                if (ip != null) {
+                    break;
+                }
+            }
+        }
+
+        commands.clear();
+        commands.add(new String[]{grep_path, ".*\\*"});
+        if(ip == null && !command.isWindows()) {

Review Comment:
   modularized better, fixed a few issues, and added test cases.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to