From f4d6f8d97e211f07edf7e0304d09e1a1e56bba8d Mon Sep 17 00:00:00 2001
From: "Ruben S. Montero" <rsmontero@opennebula.org>
Date: Fri, 6 Mar 2020 12:10:42 +0100
Subject: [PATCH] F #3859: Fixes for monitoring

- empty VM list
- Use rsync for host sync by default
- Prevent race condition between monitord and IM module
---
 src/cli/one_helper/onehost_helper.rb |  9 +++++----
 src/cli/onehost                      | 15 +++++++--------
 src/im_mad/im_exec/one_im_exec.rb    |  9 +++++----
 src/im_mad/remotes/lib/kvm.rb        |  2 ++
 src/im_mad/remotes/lib/probe_db.rb   |  1 +
 src/nebula/Nebula.cc                 | 20 +++++++++++---------
 6 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/src/cli/one_helper/onehost_helper.rb b/src/cli/one_helper/onehost_helper.rb
index d16626e9c2..d6d1018266 100644
--- a/src/cli/one_helper/onehost_helper.rb
+++ b/src/cli/one_helper/onehost_helper.rb
@@ -359,14 +359,15 @@ class OneHostHelper < OpenNebulaHelper::OneHelper
 
                     print_update_info(total - size, total, host['NAME'])
 
-                    if options[:rsync]
-                        sync_cmd = "rsync -Laz --delete #{REMOTES_LOCATION}" \
-                                   " #{host['NAME']}:#{remote_dir}"
-                    else
+                    if options[:ssh]
                         sync_cmd = "ssh #{host['NAME']}" \
+                                   " rm -rf '#{remote_dir}' 2>/dev/null;" \
                                    " mkdir -p '#{remote_dir}' 2>/dev/null &&" \
                                    " scp -rp #{REMOTES_LOCATION}/*" \
                                    " #{host['NAME']}:#{remote_dir} 2> /dev/null"
+                    else
+                        sync_cmd = "rsync -Laz --delete #{REMOTES_LOCATION}" \
+                                   " #{host['NAME']}:#{remote_dir}"
                     end
 
                     retries = 3
diff --git a/src/cli/onehost b/src/cli/onehost
index 7fe190d297..616813a300 100755
--- a/src/cli/onehost
+++ b/src/cli/onehost
@@ -77,13 +77,12 @@ CommandParser::CmdParser.new(ARGV) do
         :description => 'Force probe upgrade in onehost sync'
     }
 
-    RSYNC = {
-        :name => 'rsync',
-        :large => '--rsync',
-        :description => 'Use rsync to synchronize remotes. In case some '\
-                        'probes are no longer in the fronted ithey will be '\
-                        'deleted in the hosts. rsync command must be '\
-                        'installed in the frontend and nodes.'
+    SSH = {
+        :name => 'ssh',
+        :large => '--ssh',
+        :description => 'Use SSH to synchronize remotes. In case some '\
+                        'probes are no longer in the fronted they will be '\
+                        'deleted in the hosts.'
     }
 
     TYPE = {
@@ -95,7 +94,7 @@ CommandParser::CmdParser.new(ARGV) do
     }
 
     CREAT_OPTIONS = [IM, VMM, OneClusterHelper::CLUSTER, TYPE]
-    SYNC_OPTIONS  = [OneClusterHelper::CLUSTER, FORCE, RSYNC]
+    SYNC_OPTIONS  = [OneClusterHelper::CLUSTER, FORCE, SSH]
 
     ########################################################################
     # Formatters for arguments
diff --git a/src/im_mad/im_exec/one_im_exec.rb b/src/im_mad/im_exec/one_im_exec.rb
index 27d8c65821..6d8fd51d66 100755
--- a/src/im_mad/im_exec/one_im_exec.rb
+++ b/src/im_mad/im_exec/one_im_exec.rb
@@ -146,12 +146,13 @@ class InformationManagerDriver < OpenNebulaDriver
         end
 
         # Use SCP to sync:
-        sync_cmd = "scp -r #{@local_scripts_base_path}/* " \
-            "#{hostname}:#{@remote_scripts_base_path}"
+        #sync_cmd = "scp -r #{@local_scripts_base_path}/* " \
+        #    "#{hostname}:#{@remote_scripts_base_path}"
 
         # Use rsync to sync:
-        # sync_cmd = "rsync -Laz #{REMOTES_LOCATION} " \
-        #   #{hostname}:#{@remote_dir}"
+        sync_cmd = "rsync -Laz --delete #{@local_scripts_base_path}" \
+                   " #{hostname}:#{@remote_scripts_base_path}"
+
         cmd = LocalCommand.run(sync_cmd, log_method(hostid))
 
         if cmd.code != 0
diff --git a/src/im_mad/remotes/lib/kvm.rb b/src/im_mad/remotes/lib/kvm.rb
index a202ac1348..91fe7d3f09 100644
--- a/src/im_mad/remotes/lib/kvm.rb
+++ b/src/im_mad/remotes/lib/kvm.rb
@@ -558,6 +558,8 @@ module DomainList
 
             lines = text.split(/\n/)[2..-1]
 
+            return @vms if lines.nil?
+
             names = lines.map do |line|
                 line.split(/\s+/).delete_if {|d| d.empty? }[1]
             end
diff --git a/src/im_mad/remotes/lib/probe_db.rb b/src/im_mad/remotes/lib/probe_db.rb
index c1d587bfe3..9a90dd247f 100644
--- a/src/im_mad/remotes/lib/probe_db.rb
+++ b/src/im_mad/remotes/lib/probe_db.rb
@@ -153,6 +153,7 @@ class VirtualMachineDB
         # check missing VMs
         # ----------------------------------------------------------------------
         uuids = @db.execute("SELECT uuid FROM #{@dataset}").flatten!
+        uuids ||= []
 
         (uuids - monitor_ids).each do |uuid|
             vm_db = @db.execute(
diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc
index 34e4a97d29..e909519a59 100644
--- a/src/nebula/Nebula.cc
+++ b/src/nebula/Nebula.cc
@@ -936,12 +936,6 @@ void Nebula::start(bool bootstrap_only)
             goto error_mad;
         }
 
-        rc = im->start();
-
-        if ( rc != 0 )
-        {
-            throw runtime_error("Could not start the Information Manager");
-        }
     }
 
     // ---- Transfer Manager ----
@@ -1195,11 +1189,19 @@ void Nebula::start(bool bootstrap_only)
         marketm->init_managers();
     }
 
-    // ---- Start the Request Manager ----
+    // ---- Start the Request Manager & Information Manager----
+    // This modules recevie request from users / monitor and need to be
+    // started in last place when all systems are up
 
-    rc = rm->start();
+    if (!cache)
+    {
+        if ( im->start() != 0 )
+        {
+            throw runtime_error("Could not start the Information Manager");
+        }
+    }
 
-    if ( rc != 0 )
+    if ( rm->start() != 0 )
     {
        throw runtime_error("Could not start the Request Manager");
     }