From 9af60f21a97a47394c0bb26d96f308d9c3ec73a1 Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Mon, 27 May 2013 15:23:06 +0000
Subject: [PATCH] OPENDJ-941 Package Upgrade: pre-install script should call stop-ds if the instance is not configured

---
 opends/resource/debian/control/preinst                         |   17 ++++++--
 opends/resource/debian/control/prerm                           |    3 +
 opends/resource/debian/control/postinst                        |    6 ++-
 opends/src/build-tools/org/opends/build/tools/GenerateRpm.java |   85 +++++++++++++++++++++++++-----------------
 4 files changed, 68 insertions(+), 43 deletions(-)

diff --git a/opends/resource/debian/control/postinst b/opends/resource/debian/control/postinst
index 29aceda..3fdaf98 100644
--- a/opends/resource/debian/control/postinst
+++ b/opends/resource/debian/control/postinst
@@ -14,12 +14,14 @@
     @prefix@/./upgrade -n
     echo
 # Upgrade fails - Requires mandatory user interaction.
+# Nevertheless, exits successfully of the pkg process.
     if [ "$?" -eq 2 ]
     then
       exit 0 
     fi
-# Restart the service if needed.
-# If server is stopped by upgrade process, the server will restart after upgrade.
+# Restarts the service if needed.
+# If server is stopped by upgrade process, 
+#  the server will restart after upgrade.
 # If server is stopped before the upgrade process 
 #  (eg. upgrade the new package), the server will not restart.
 # Uses the flag for determining server status at this point.
diff --git a/opends/resource/debian/control/preinst b/opends/resource/debian/control/preinst
index 538cf23..0fceba2 100644
--- a/opends/resource/debian/control/preinst
+++ b/opends/resource/debian/control/preinst
@@ -1,15 +1,22 @@
 #!/bin/bash
 set -e
 # Pre installation script
-# If the server is running before upgrade, creates a flag.
 if [ "$1" = "upgrade" ]
 then
-  if [ -f @prefix@/logs/server.pid ]
+# Only if the instance has been configured
+  if [ -f @prefix@/config/buildinfo ] && [ "$(ls -A @prefix@/config/archived-configs)" ]
   then
-    touch @prefix@/logs/status
+# If the server is running before upgrade, creates a flag.
+    if [ -f @prefix@/logs/server.pid ]
+    then
+      touch @prefix@/logs/status
+    fi
+    echo *Stopping OpenDJ server...
+    @prefix@/bin/./stop-ds
+  else
+    echo "Instance is not configured. Upgrade aborded."
+    exit -1
   fi
-  echo *Stopping OpenDJ server...
-  @prefix@/bin/./stop-ds
 fi
 echo 
 # End of the pre installation script
\ No newline at end of file
diff --git a/opends/resource/debian/control/prerm b/opends/resource/debian/control/prerm
index 7722e7b..b3c16eb 100644
--- a/opends/resource/debian/control/prerm
+++ b/opends/resource/debian/control/prerm
@@ -2,7 +2,8 @@
 set -e
 # Pre rm script
 # Stops the server
-if [ "$1" = "remove" ]
+# Only if the instance has been configured
+if [ "$1" = "remove" ] && ( [ -f @prefix@/config/buildinfo ] && [ "$(ls -A @prefix@/config/archived-configs)" ] )
 then
   echo *Stopping OpenDJ server...
   @prefix@/bin/./stop-ds
diff --git a/opends/src/build-tools/org/opends/build/tools/GenerateRpm.java b/opends/src/build-tools/org/opends/build/tools/GenerateRpm.java
index 362de91..70f117a 100644
--- a/opends/src/build-tools/org/opends/build/tools/GenerateRpm.java
+++ b/opends/src/build-tools/org/opends/build/tools/GenerateRpm.java
@@ -270,49 +270,60 @@
       sb.append("# Pre Install" + EOL);
       sb.append("%pre" + EOL);
       sb.append("if [ \"$1\" == \"1\" ]; then" + EOL);
-      sb.append("	echo \"Pre Install - initial install\"" + EOL);
+      sb.append("    echo \"Pre Install - initial install\"" + EOL);
       sb.append("else if [ \"$1\" == \"2\" ] ; then" + EOL);
-      sb.append("	echo \"Pre Install - upgrade install\"" + EOL);
+      sb.append("# Only if the instance has been configured" + EOL);
+      sb.append("    if [ -f %{_prefix}/config/buildinfo ] && [ \"$(ls -A %{_prefix}/config/archived-configs)\" ]" + EOL);
+      sb.append("    then" + EOL);
+      sb.append("        echo \"Pre Install - upgrade install\"" + EOL);
       sb.append("# If the server is running before upgrade, creates a file flag" + EOL);
-      sb.append("           if [ -f %{_prefix}/logs/server.pid ] " + EOL);
-      sb.append("           then" + EOL);
-      sb.append("                   touch %{_prefix}/logs/status" + EOL);
-      sb.append("           fi" + EOL);
-      sb.append("       stopds=$(%{_prefix}/bin/stop-ds)" + EOL);
-      sb.append("       echo $stopds" + EOL);
-      sb.append("     fi" + EOL);
+      sb.append("        if [ -f %{_prefix}/logs/server.pid ] " + EOL);
+      sb.append("        then" + EOL);
+      sb.append("            touch %{_prefix}/logs/status" + EOL);
+      sb.append("        fi" + EOL);
+      sb.append("        stopds=$(%{_prefix}/bin/stop-ds)" + EOL);
+      sb.append("        echo $stopds" + EOL);
+      sb.append("        fi" + EOL);
+      sb.append("    fi" + EOL);
       sb.append("fi" + EOL);
       sb.append(EOL);
       sb.append("# Post Install" + EOL);
       sb.append("%post" + EOL);
       sb.append("if [ \"$1\" == \"1\" ] ; then" + EOL);
-      sb.append("	echo \"Post Install - initial install\"" + EOL);
-      sb.append("	[[ `java -version 2>&1 | /bin/sed 's/java version \"\\(.*\\)\\.\\(.*\\)\\.\\(.*\\)\\_\\(.*\\)\\.*\"/\\1\\2\\3\\4/; 1q'` < 16022 ]] && echo \"WARNING - For best server performance, use at least Java 1.6.0_22, which includes a major security fix for TLS.\""
+      sb.append("    echo \"Post Install - initial install\"" + EOL);
+      sb.append("    [[ `java -version 2>&1 | /bin/sed 's/java version \"\\(.*\\)\\.\\(.*\\)\\.\\(.*\\)\\_\\(.*\\)\\.*\"/\\1\\2\\3\\4/; 1q'` < 16022 ]] && echo \"WARNING - For best server performance, use at least Java 1.6.0_22, which includes a major security fix for TLS.\""
           + EOL);
-      sb.append("       echo \"\"" + EOL);
+      sb.append("    echo \"\"" + EOL);
       sb.append("else if [ \"$1\" == \"2\" ] ; then" + EOL);
-      sb.append("	echo \"Post Install - upgrade install\"" + EOL);
+      sb.append("    echo \"Post Install - upgrade install\"" + EOL);
+      sb.append("# Only if the instance has been configured" + EOL);
+      sb.append("    if [ -f %{_prefix}/config/buildinfo ] && [ \"$(ls -A %{_prefix}/config/archived-configs)\" ]" + EOL);
+      sb.append("    then" + EOL);
       // Starts the upgrade. The new files are automatically imported
       // by rpm manager, which compares files between last & actual version.
       // Copies / deletes files depending of new package.
-      sb.append("       %{_prefix}/./upgrade -n" + EOL);
+      sb.append("        %{_prefix}/./upgrade -n" + EOL);
       sb.append("# Upgrade ok " + EOL);
-      sb.append("         if [ \"$?\" == \"0\" ] ; then " + EOL);
+      sb.append("        if [ \"$?\" == \"0\" ] ; then " + EOL);
       sb.append("# Checks the server status flag for restart. " + EOL);
-      sb.append("             if [ -f %{_prefix}/logs/status ] " + EOL);
-      sb.append("             then" + EOL);
-      sb.append("                 echo \"\"" + EOL);
-      sb.append("                 echo \"Restarting server...\" " + EOL);
-      sb.append("                 %{_prefix}/./bin/start-ds " + EOL);
-      sb.append("                 echo \"\"" + EOL);
-      sb.append("                 rm -f %{_prefix}/logs/status " + EOL);
-      sb.append("             fi" + EOL);
-      sb.append("         fi" + EOL);
+      sb.append("            if [ -f %{_prefix}/logs/status ] " + EOL);
+      sb.append("            then" + EOL);
+      sb.append("                echo \"\"" + EOL);
+      sb.append("                echo \"Restarting server...\" " + EOL);
+      sb.append("                %{_prefix}/./bin/start-ds " + EOL);
+      sb.append("                echo \"\"" + EOL);
+      sb.append("                rm -f %{_prefix}/logs/status " + EOL);
+      sb.append("            fi" + EOL);
+      sb.append("        fi" + EOL);
       sb.append("# Upgrade fails, needs user interaction (eg. manual mode)" + EOL);
-      sb.append("         if [ \"$?\" == \"2\" ] ; then " + EOL);
-      sb.append("           exit \"0\" " + EOL);
-      sb.append("         fi " + EOL);
-      sb.append("	fi " + EOL);
+      sb.append("        if [ \"$?\" == \"2\" ] ; then " + EOL);
+      sb.append("            exit \"0\" " + EOL);
+      sb.append("        fi " + EOL);
+      sb.append("    else" + EOL);
+      sb.append("        echo \"Instance is not configured. Upgrade aborded.\"" + EOL);
+      sb.append("        exit -1" + EOL);
+      sb.append("    fi" + EOL);
+      sb.append("    fi " + EOL);
       sb.append("fi" + EOL);
       sb.append(EOL);
       sb.append("# =========================" + EOL);
@@ -326,20 +337,24 @@
       sb.append("# Pre Uninstall" + EOL);
       sb.append("%preun" + EOL);
       sb.append("if [ \"$1\" == \"0\" ] ; then" + EOL);
-      sb.append("	echo \"Pre Uninstall - uninstall\"" + EOL);
+      sb.append("    echo \"Pre Uninstall - uninstall\"" + EOL);
+      sb.append("# Only if the instance has been configured" + EOL);
+      sb.append("    if [ -f %{_prefix}/config/buildinfo ] && [ \"$(ls -A %{_prefix}/config/archived-configs)\" ]" + EOL);
+      sb.append("    then" + EOL);
       sb.append("	%{_prefix}/bin/stop-ds" + EOL);
+      sb.append("    fi" + EOL);
       sb.append("else if [ \"$1\" == \"1\" ] ; then" + EOL);
-      sb.append("	echo \"Pre Uninstall - upgrade uninstall\"" + EOL);
-      sb.append("	fi" + EOL);
+      sb.append("    echo \"Pre Uninstall - upgrade uninstall\"" + EOL);
+      sb.append("    fi" + EOL);
       sb.append("fi" + EOL);
       sb.append("# Post Uninstall" + EOL);
       sb.append("%postun" + EOL);
       sb.append("if [ \"$1\" == \"0\" ] ; then" + EOL);
-      sb.append("	echo \"Post Uninstall - uninstall\"" + EOL);
-      sb.append("       echo \"OpenDJ successfully removed.\"" + EOL);
+      sb.append("    echo \"Post Uninstall - uninstall\"" + EOL);
+      sb.append("    echo \"OpenDJ successfully removed.\"" + EOL);
       sb.append("else if [ \"$1\" == \"1\" ] ; then" + EOL);
-      sb.append("	echo \"Post Uninstall - upgrade uninstall\"" + EOL);
-      sb.append("	fi" + EOL);
+      sb.append("    echo \"Post Uninstall - upgrade uninstall\"" + EOL);
+      sb.append("    fi" + EOL);
       sb.append("fi" + EOL);
       sb.append(EOL);
       sb.append("# =========================" + EOL);

--
Gitblit v1.10.0