#!/bin/bash
source /opt/rb/bin/rb_manager_functions.sh

echo ""
e_title "redborder - Pre-update script to 3.1.80-6"

echo "Preparing the system to be updated. Please wait..."
echo ""
yum install -y patch --disablerepo=redBorder &>/dev/null
cat > /root/rb_preupdate_80-6.patch <<- \EOF
--- /opt/rb/bin/rb_cluster_update.sh	2019-11-06 10:04:48.742734859 +0000
+++ /opt/rb/bin/rb_cluster_update_patched.sh	2019-11-06 13:32:52.111323362 +0000
@@ -288,7 +288,7 @@
         if [ $rbinstall -eq 1 ]; then
           rb_manager_ssh.sh $n 'rb_chef start; rb_service start postgresql pgpool; sleep 1; rb_pcp.sh up; rb_upload_cookbooks.sh'
           rb_manager_ssh.sh $n 'rb_run_chef_once.sh'
-          rb_manager_ssh.sh $n 'pushd /opt/rb/var/www/rb-rails &>/dev/null; env NO_MODULES=1 rake db:migrate; RAILS_ENV=production rake db:migrate:modules; rake db:seed; rake redBorder:update; popd &>/dev/null'
+          rb_manager_ssh.sh $n 'pushd /opt/rb/var/www/rb-rails &>/dev/null; env NO_MODULES=1 rake db:migrate:modules MODULE=monitor; env NO_MODULES=1 rake db:migrate; RAILS_ENV=production rake db:migrate:modules; rake db:seed; RAILS_ENV=production rake db:seed:modules MODULE=monitor; rake redBorder:update; popd &>/dev/null'
           rb_manager_ssh.sh $n 'pushd /opt/rb/var/www/rb-rails &>/dev/null; rake assets:precompile; chown -R rb-webui:rb-webui /opt/rb/var/www/rb-rails/lib/assets/; popd &>/dev/null'
           rb_manager_ssh.sh $n 'rb_service restart rb-webui rb-workers'
         fi
EOF

cat /opt/rb/bin/rb_cluster_update.sh | grep "RAILS_ENV=production rake db:migrate:modules; env NO_MODULES=1 rake db:migrate;" &>/dev/null
if [ "x$?" == "x1" ]; then
  patch -p0 --ignore-whitespace -i /root/rb_preupdate_80-6.patch --force
  rm -rf /root/rb_preupdate_80-6.patch
else
  echo "file /opt/rb/bin/rb_cluster_update.sh already patched"
fi

echo ""
e_title "Cluster ready to be updated"

echo ""
echo "Please run this command to start update process: rb_cluster_update.sh"
echo ""
