installation openmano service persistent. Clear other openmano folders 75/675/2
authortierno <alfonso.tiernosepulveda@telefonica.com>
Tue, 15 Nov 2016 17:33:13 +0000 (17:33 +0000)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Wed, 16 Nov 2016 08:43:00 +0000 (09:43 +0100)
Change-Id: I11f8d782e364fe0d004c2ae680c0fdfe280be97b
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
scripts/install-openmano-service.sh
scripts/install-openmano.sh

index 2b0e279..c5bccec 100755 (executable)
@@ -36,20 +36,20 @@ function usage(){
 }
 
 function uninstall(){
 }
 
 function uninstall(){
-    service openmano stop
-    for file in /opt/openmano /etc/default/openmanod.cfg /var/log/openmano /etc/systemd/system/openmano.service /usr/sbin/openmano
+    echo "systemctl disable openmano.service " &&  systemctl disable openmano.service 2>/dev/null || echo "  Already done"
+    echo "service openmano stop " && service openmano stop 2>/dev/null || echo "  Already done"
+    for file in /opt/openmano /etc/default/openmanod.cfg /var/log/openmano /etc/systemd/system/openmano.service /usr/bin/openmano /usr/sbin/service-openmano /usr/bin/openmano-report
     do
     do
+        echo rm $file
         rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
     done
     echo "Done"
 }
 
         rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
     done
     echo "Done"
 }
 
-BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
 GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
 USER_OWNER="root"
 QUIET_MODE=""
 FILE=""
 GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
 USER_OWNER="root"
 QUIET_MODE=""
 FILE=""
-DELETE=""
 while getopts ":u:f:hq-:" o; do
     case "${o}" in
         u)
 while getopts ":u:f:hq-:" o; do
     case "${o}" in
         u)
@@ -84,6 +84,7 @@ while getopts ":u:f:hq-:" o; do
             ;;
     esac
 done
             ;;
     esac
 done
+BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
 
 #check root privileges
 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
 
 #check root privileges
 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
@@ -109,28 +110,26 @@ else
 fi
 
 
 fi
 
 
-if [[ -z $FILE ]]
+if [[ -z "$FILE" ]]
 then
 then
-    git clone $GIT_URL __temp__ || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
-    #git checkout <tag version>
-    FILE=./__temp__
-    DELETE=y
+    FILE=__temp__${RANDOM}
+    git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
+else
+    [[ -d  "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1
 fi
 
 #make idempotent
 fi
 
 #make idempotent
-rm -rf /opt/openmano
-rm -f /etc/default/openmanod.cfg
-rm -f /var/log/openmano
-cp -r $FILE /opt/openmano         || ! echo $BAD_PATH_ERROR >&2 || exit 1
+uninstall
+#copy files
+cp -r "$FILE" /opt/openmano         || ! echo $BAD_PATH_ERROR >&2 || exit 1
 mkdir -p /opt/openmano/logs
 mkdir -p /opt/openmano/logs
-rm -rf /usr/sbin/openmano
-#cp ${FILE}/openmano /usr/sbin/    || ! echo $BAD_PATH_ERROR >&2 || exit 1
-ln -s /opt/openmano/openmanod.cfg /etc/default/openmanod.cfg  || echo "warning cannot create link '/etc/default/openmanod.cfg'"
-ln -s /opt/openmano/logs /var/log/openmano  || echo "warning cannot create link '/var/log/openmano'"
-ln -s /opt/openmano/openmano /usr/bin/openmano
-ln -s /opt/openmano/scripts/openmano-report.sh /usr/bin/openmano-report
-
-chown $USER_OWNER /opt/openmano/openmanod.cfg
+#makes links
+ln -s -v /opt/openmano/openmanod.cfg /etc/default/openmanod.cfg  || echo "warning cannot create link '/etc/default/openmanod.cfg'"
+ln -s -v /opt/openmano/logs /var/log/openmano  || echo "warning cannot create link '/var/log/openmano'"
+ln -s -v /opt/openmano/openmano /usr/bin/openmano
+ln -s -v /opt/openmano/scripts/service-openmano.sh /usr/sbin/service-openmano
+ln -s -v /opt/openmano/scripts/openmano-report.sh /usr/bin/openmano-report
+
 chown -R $USER_OWNER /opt/openmano
 
 mkdir -p /etc/systemd/system/
 chown -R $USER_OWNER /opt/openmano
 
 mkdir -p /etc/systemd/system/
@@ -147,9 +146,10 @@ Restart=always
 WantedBy=multi-user.target
 EOF
 
 WantedBy=multi-user.target
 EOF
 
-[[ -n $DELETE ]] && rm -rf $FILE
+rm -rf ${FILE}
 
 service openmano start
 
 service openmano start
+systemctl enable openmano.service
 
 echo Done
 exit
 
 echo Done
 exit
index ff627d3..f55851d 100755 (executable)
@@ -35,7 +35,8 @@ function usage(){
     echo -e "     -q --quiet: install in unattended mode"
     echo -e "     -h --help:  show this help"
     echo -e "     --develop:  install last version for developers, and do not configure as a service"
     echo -e "     -q --quiet: install in unattended mode"
     echo -e "     -h --help:  show this help"
     echo -e "     --develop:  install last version for developers, and do not configure as a service"
-    echo -e "     --forcedb:  reinstall mano_db DB, deleting previous database and creating a new one"
+    echo -e "     --forcedb:  reinstall mano_db DB, deleting previous database if exists and creating a new one"
+    echo -e "     --force:    makes idenpotent, delete previous installations folders if needed"
     echo -e "     --noclone:  assumes that openmano was cloned previously and that this script is run from the local repo"
     echo -e "     --no-install-packages: use this option to skip updating and installing the requires packages. This avoid wasting time if you are sure requires packages are present e.g. because of a previous installation"
 }
     echo -e "     --noclone:  assumes that openmano was cloned previously and that this script is run from the local repo"
     echo -e "     --no-install-packages: use this option to skip updating and installing the requires packages. This avoid wasting time if you are sure requires packages are present e.g. because of a previous installation"
 }
@@ -75,6 +76,7 @@ DBPASSWD_PARAM=""
 QUIET_MODE=""
 DEVELOP=""
 FORCEDB=""
 QUIET_MODE=""
 DEVELOP=""
 FORCEDB=""
+FORCE=""
 NOCLONE=""
 NO_PACKAGES=""
 while getopts ":u:p:hiq-:" o; do
 NOCLONE=""
 NO_PACKAGES=""
 while getopts ":u:p:hiq-:" o; do
@@ -97,6 +99,7 @@ while getopts ":u:p:hiq-:" o; do
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
             [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
             [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
+            [ "${OPTARG}" == "force" ]   && FORCEDB="y" && FORCE="y" && continue
             [ "${OPTARG}" == "noclone" ] && NOCLONE="y" && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
             [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
             [ "${OPTARG}" == "noclone" ] && NOCLONE="y" && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
             [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
@@ -165,6 +168,17 @@ else  #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red"
     [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
 fi
 
     [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
 fi
 
+#check if installed as a service
+INSTALL_AS_A_SERVICE=""
+[[ "$_DISTRO" == "Ubuntu" ]] &&  [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]] && INSTALL_AS_A_SERVICE="y"
+#Next operations require knowing OPENMANO_BASEFOLDER
+if [[ -z "$NOCLONE" ]]; then
+    OPENMANO_BASEFOLDER="${PWD}/openmano"
+    [[ -n "$FORCE" ]] && rm -rf $OPENMANO_BASEFOLDER #make idenpotent
+else
+    HERE=$(realpath $(dirname $0))
+    OPENMANO_BASEFOLDER=$(dirname $HERE)
+fi
 
 
 if [[ -z "$NO_PACKAGES" ]]
 
 
 if [[ -z "$NO_PACKAGES" ]]
@@ -263,9 +277,9 @@ if [[ -z $NOCLONE ]]; then
 #################################################################
 #####        DOWNLOAD SOURCE                                #####
 #################################################################'
 #################################################################
 #####        DOWNLOAD SOURCE                                #####
 #################################################################'
-    su $SUDO_USER -c 'git clone '"${GIT_URL}"' openmano'
-    su $SUDO_USER -c 'cp openmano/.gitignore-common openmano/.gitignore'
-    [[ -z $DEVELOP ]] && su $SUDO_USER -c 'cd openmano; git checkout tags/v1.0; cd ..' 
+    su $SUDO_USER -c "git clone ${GIT_URL} ${OPENMANO_BASEFOLDER}"
+    su $SUDO_USER -c "cp ${OPENMANO_BASEFOLDER}/.gitignore-common ${OPENMANO_BASEFOLDER}/.gitignore"
+    [[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C  ${OPENMANO_BASEFOLDER} checkout tags/v1.0.1"
 fi
 
 echo '
 fi
 
 echo '
@@ -276,11 +290,7 @@ echo -e "\nCreating temporary file form MYSQL installation and initialization"
 TEMPFILE="$(mktemp -q --tmpdir "installopenmano.XXXXXX")"
 trap 'rm -f "$TEMPFILE"' EXIT
 chmod 0600 "$TEMPFILE"
 TEMPFILE="$(mktemp -q --tmpdir "installopenmano.XXXXXX")"
 trap 'rm -f "$TEMPFILE"' EXIT
 chmod 0600 "$TEMPFILE"
-cat >"$TEMPFILE" <<EOF
-[client]
-user=$DBUSER
-password=$DBPASSWD
-EOF
+echo -e "[client]\n user='$DBUSER'\n password='$DBPASSWD'">"$TEMPFILE"
 
 if db_exists "mano_db" $TEMPFILE ; then
     if [[ -n $FORCEDB ]]; then
 
 if db_exists "mano_db" $TEMPFILE ; then
     if [[ -n $FORCEDB ]]; then
@@ -306,20 +316,11 @@ else
 fi
 
 
 fi
 
 
-#Next operations require knowing OPENMANO_BASEFOLDER
-HERE=$(realpath $(dirname $0))
-if [[ -z $NOCLONE ]]; then
-    OPENMANO_BASEFOLDER="${HERE}/openmano"
-else
-    OPENMANO_BASEFOLDER=$(dirname $HERE)
-fi
-
-
 echo '
 #################################################################
 #####        INIT DATABASE                                  #####
 #################################################################'
 echo '
 #################################################################
 #####        INIT DATABASE                                  #####
 #################################################################'
-su $SUDO_USER -c "${OPENMANO_BASEFOLDER}"'/database_utils/init_mano_db.sh -u mano -p manopw -d mano_db' || ! echo "Failed while initializing database" || exit 1
+su $SUDO_USER -c "${OPENMANO_BASEFOLDER}/database_utils/init_mano_db.sh -u mano -p manopw -d mano_db" || ! echo "Failed while initializing database" || exit 1
 
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
 
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
@@ -357,29 +358,32 @@ echo '
 #################################################################
 #####             CONFIGURE OPENMANO CLIENT                 #####
 #################################################################'
 #################################################################
 #####             CONFIGURE OPENMANO CLIENT                 #####
 #################################################################'
-#creates a link at ~/bin
-su $SUDO_USER -c 'mkdir -p ${HOME}/bin'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano-report'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openmano'
-su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/openmano ${HOME}/bin/openmano'
-su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/scripts/openmano-report.sh   ${HOME}/bin/openmano-report'
-su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/scripts/service-openmano.sh  ${HOME}/bin/service-openmano'
-
-#insert /home/<user>/bin in the PATH
-#skiped because normally this is done authomatically when ~/bin exist
-#if ! su $SUDO_USER -c 'echo $PATH' | grep -q "${HOME}/bin"
-#then
-#    echo "    inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
-#    su $SUDO_USER -c 'echo "PATH=\$PATH:\${HOME}/bin" >> ~/.bashrc'
-#fi
-if [[ $SUDO_USER == root ]] 
+#creates a link at ~/bin if not configured as a service
+if [[ -z "$INSTALL_AS_A_SERVICE" ]]
 then
 then
-    if ! echo $PATH | grep -q "${HOME}/bin"
+    su $SUDO_USER -c 'mkdir -p ${HOME}/bin'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano-report'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openmano'
+    su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/openmano ${HOME}/bin/openmano'
+    su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/scripts/openmano-report.sh   ${HOME}/bin/openmano-report'
+    su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/scripts/service-openmano.sh  ${HOME}/bin/service-openmano'
+
+    #insert /home/<user>/bin in the PATH
+    #skiped because normally this is done authomatically when ~/bin exist
+    #if ! su $SUDO_USER -c 'echo $PATH' | grep -q "${HOME}/bin"
+    #then
+    #    echo "    inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
+    #    su $SUDO_USER -c 'echo "PATH=\$PATH:\${HOME}/bin" >> ~/.bashrc'
+    #fi
+    if [[ $SUDO_USER == root ]] 
     then
     then
-        echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
-    fi
-fi 
+        if ! echo $PATH | grep -q "${HOME}/bin"
+        then
+            echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
+        fi
+    fi 
+fi
 
 #configure arg-autocomplete for this user
 #in case of minimal instalation this package is not installed by default
 
 #configure arg-autocomplete for this user
 #in case of minimal instalation this package is not installed by default
@@ -395,7 +399,7 @@ fi
 
 
 
 
 
 
-if [[ "$_DISTRO" == "Ubuntu" ]] &&  [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]]
+if [[ -n "$INSTALL_AS_A_SERVICE"  ]]
 then
 echo '
 #################################################################
 then
 echo '
 #################################################################
@@ -403,19 +407,20 @@ echo '
 #################################################################'
 
     ${OPENMANO_BASEFOLDER}/scripts/install-openmano-service.sh -f ${OPENMANO_BASEFOLDER} #-u $SUDO_USER
 #################################################################'
 
     ${OPENMANO_BASEFOLDER}/scripts/install-openmano-service.sh -f ${OPENMANO_BASEFOLDER} #-u $SUDO_USER
+#    rm -rf ${OPENMANO_BASEFOLDER}
 #    alias service-openmano="service openmano"
 #    echo 'alias service-openmano="service openmano"' >> ${HOME}/.bashrc
 
     echo
 #    alias service-openmano="service openmano"
 #    echo 'alias service-openmano="service openmano"' >> ${HOME}/.bashrc
 
     echo
-    echo "Done!  you may need to logout and login again for loading client configuration"
-    echo " Manage server with 'service openmano start|stop|status|...' "
+    echo "Done!  installed at /opt/openmano"
+    echo " Manage server with 'sudo service openmano start|stop|status|...' "
 
 
 else
 
     echo
     echo "Done!  you may need to logout and login again for loading client configuration"
 
 
 else
 
     echo
     echo "Done!  you may need to logout and login again for loading client configuration"
-    echo " Run './openmano/scripts/service-openmano.sh start' for starting openmano in a screen"
+    echo " Run './${OPENMANO_BASEFOLDER}/scripts/service-openmano.sh start' for starting openmano in a screen"
 
 fi
 
 
 fi