From 16886594b594bd17fadaee874b3968fb25f94585 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Tue, 8 Jan 2019 13:07:37 +0100
Subject: [PATCH] [fix] Fix start/stop bash scripts

---
 .../org/duniter/elasticsearch/PluginInit.java |  1 +
 src/scripts/start-cesium-plus-pod.sh          |  8 ++++-
 src/scripts/stop-cesium-plus-pod.sh           | 12 +++----
 src/scripts/update-cesium-plus-pod.sh         | 33 ++++++++++++++-----
 4 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/cesium-plus-pod-core/src/main/java/org/duniter/elasticsearch/PluginInit.java b/cesium-plus-pod-core/src/main/java/org/duniter/elasticsearch/PluginInit.java
index ff434bf0..a3d9b4fe 100644
--- a/cesium-plus-pod-core/src/main/java/org/duniter/elasticsearch/PluginInit.java
+++ b/cesium-plus-pod-core/src/main/java/org/duniter/elasticsearch/PluginInit.java
@@ -269,6 +269,7 @@ public class PluginInit extends AbstractLifecycleComponent<PluginInit> {
 
                 // Index peers (and listen if new peer appear)
                 if (pluginSettings.enableBlockchainPeerIndexation()) {
+                    logger.info(String.format("[%s] Indexing blockchain peers...", currencyName));
                     injector.getInstance(PeerService.class)
                             .listenAndIndexPeers(peer);
                 }
diff --git a/src/scripts/start-cesium-plus-pod.sh b/src/scripts/start-cesium-plus-pod.sh
index 8ac7b000..b41448d2 100755
--- a/src/scripts/start-cesium-plus-pod.sh
+++ b/src/scripts/start-cesium-plus-pod.sh
@@ -2,6 +2,7 @@
 
 CURRENCY=g1
 
+# Get PID
 PID=`ps -efl | grep cesium-plus-pod | grep ${CURRENCY}/lib | awk '/^([0-9]+) ([^\s]) ([a-zA-Z0-9]+) ([0-9]+).*/ {printf "%s", $4}'`
 
 if [[ "$PID" != "" ]];
@@ -9,9 +10,14 @@ then
         echo "Error: Cesium+ pod already started!"
         exit -1
 else
+
+        # Limit JVM heap size
+        ES_JAVA_OPTS="$ES_JAVA_OPTS -Xmx2g"
+        export ES_JAVA_OPTS
+
         cd /opt/cesium-plus-pod-${CURRENCY}/bin
         ./elasticsearch -d
         echo "Cesium+ pod started on ${CURRENCY} currency !"
-        echo "...to follow log: tail -f /opt/cesium-plus-pod-${CURRENCY}/logs/g1-es-data.log"
+        echo "...to follow log: tail -f /opt/cesium-plus-pod-${CURRENCY}/logs/${CURRENCY}-es-data.log"
 fi
 
diff --git a/src/scripts/stop-cesium-plus-pod.sh b/src/scripts/stop-cesium-plus-pod.sh
index ff21711f..04255db3 100755
--- a/src/scripts/stop-cesium-plus-pod.sh
+++ b/src/scripts/stop-cesium-plus-pod.sh
@@ -2,13 +2,13 @@
 
 CURRENCY=g1
 
-# Get ES PID
+# Get PID
 PID=`ps -efl | grep cesium-plus-pod | grep ${CURRENCY}/lib | awk '{printf "%s", $4}'`
 
 if [[ "$PID" != "" ]];
 then
-        echo "Stopping Cesium+ Pod running on PID $PID..."
-        sudo kill -15 $PID
+        echo "Stopping Cesium+ Pod ${CURRENCY} running on PID $PID..."
+        sudo kill -15 ${PID}
 
         sleep 5s
 
@@ -22,13 +22,13 @@ then
         PID=`ps -efl | grep cesium-plus-pod | grep ${CURRENCY}/lib | awk '{printf "%s", $4}'`
         if [[ "$PID" != "" ]];
         then 
-                echo "Error: Unable to stop Cesium+ Pod !"
+                echo "Error: Unable to stop Cesium+ Pod ${CURRENCY} !"
                 exit -1
         else
-                echo "Cesium+ Pod stopped"
+                echo "Cesium+ Pod ${CURRENCY} stopped"
         fi
 
 else
-        echo "Cesium+ Pod not running!"
+        echo "Cesium+ Pod ${CURRENCY} not running!"
 fi
 
diff --git a/src/scripts/update-cesium-plus-pod.sh b/src/scripts/update-cesium-plus-pod.sh
index a55f565f..80d06bdf 100755
--- a/src/scripts/update-cesium-plus-pod.sh
+++ b/src/scripts/update-cesium-plus-pod.sh
@@ -6,7 +6,7 @@ ASSET_BASENAME=cesium-plus-pod
 ASSET=${ASSET_BASENAME}-${VERSION}-standalone
 CURRENCY=g1
 REPO=duniter/cesium-plus-pod
-ASSET_ZIP_URL=https://github.com/${REPO}/releases/download/v${VERSION}/${ASSET}.zip
+ASSET_ZIP_URL=https://github.com/${REPO}/releases/download/${ASSET_BASENAME}-${VERSION}/${ASSET}.zip
 
 if [[ "${VERSION}" == "" ]]; then
         echo "ERROR: Missing version argument !"
@@ -39,7 +39,8 @@ if [[ -x "$READLINK" ]]; then
 fi 
 
 export BASEDIR=`dirname "$SCRIPT_LOCATION"`                                                                                                                                                                                                        
-cd $BASEDIR 
+cd $BASEDIR
+BASEDIR=`pwd`
 
 echo "--- Downloading ${ASSET} ... ----------------------"
 
@@ -74,20 +75,36 @@ mv ${ASSET_BASENAME}-${VERSION} ${ASSET_BASENAME}-${VERSION}-${CURRENCY}
 sudo mv ${ASSET_BASENAME}-${VERSION}-${CURRENCY} /opt/
 sudo rm /opt/${ASSET_BASENAME}-${CURRENCY}
 sudo ln -s /opt/${ASSET_BASENAME}-${VERSION}-${CURRENCY} /opt/${ASSET_BASENAME}-${CURRENCY}
-
-mkdir /opt/${ASSET_BASENAME}-${VERSION}-${CURRENCY}/data
 mv /opt/${ASSET_BASENAME}-${VERSION}-${CURRENCY}/config/elasticsearch.yml /opt/${ASSET_BASENAME}-${VERSION}-${CURRENCY}/config/elasticsearch.yml.ori
 
-stop-cesium-plus-pod.sh
+# Creating data + log directories
+mkdir -p ${BASEDIR}/data
+mkdir -p ${BASEDIR}/logs
+mkdir -p ${BASEDIR}/backup
+cd /opt/${ASSET_BASENAME}-${VERSION}-${CURRENCY}
+ln -s ${BASEDIR}/data data
+ln -s ${BASEDIR}/logs logs
+
+cd ${BASEDIR}
+./stop-cesium-plus-pod.sh
 
 if [[ "$OLD_VERSION" != "$VERSION" ]];
 then
-        echo "--- Restoring files (data+config) from previous version $OLD_VERSION... ---------------------"
-        tar -cvf /opt/${ASSET_BASENAME}-${OLD_VERSION}-${CURRENCY}/data/save.tar.gz /opt/${ASSET_BASENAME}-${OLD_VERSION}-${CURRENCY}/data/${CURRENCY}-*
-        mv /opt/${ASSET_BASENAME}-${OLD_VERSION}-${CURRENCY}/data/${CURRENCY}-* /opt/${ASSET_BASENAME}-${VERSION}-${CURRENCY}/data
+        echo "--- Removing symbolic links from previous version $OLD_VERSION... ---------------------"
+        cp /opt/${ASSET_BASENAME}-${OLD_VERSION}-${CURRENCY}
+        rm data
+        rm logs
+
+        echo "--- Restoring config file from previous version $OLD_VERSION... ---------------------"
         cp /opt/${ASSET_BASENAME}-${OLD_VERSION}-${CURRENCY}/config/elasticsearch.yml /opt/${ASSET_BASENAME}-${VERSION}-${CURRENCY}/config
+
+        echo "--- Creating backup file with data... ---------------------"
+        cd ${BASEDIR}/data
+        DATE=`date +"%F-%H%M"`
+        tar -cvf ${BASEDIR}/backup/${CURRENCY}-${DATE}.tar.gz ${CURRENCY}-*
 fi
 
+cd ${BASEDIR}
 #./start-es-nodes.sh
 
 echo "--- Successfully installed ${ASSET_BASENAME} v$VERSION ! -------------"
-- 
GitLab