fix bug 878
[osm/RO.git] / scripts / install-openmano-service.sh
index a9d0c37..c19b345 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 ##
-# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
+# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
 # This file is part of openmano
 # All Rights Reserved.
 #
 #it configures openmano to run as a service
 
 function usage(){
-    echo -e "usage: sudo $0 [OPTIONS]"
-    echo -e "Configures openmano to run as a service"
+    echo -e "usage: sudo -E $0 [OPTIONS]"
+    echo -e "Configures openmano to run as a service at /opt"
     echo -e "  OPTIONS"
-    echo -e "     -u USER  user to run openmano, 'root' by default"
-    echo -e "     -f PATH  path where openmano source is located. If missing it download from git"
-    echo -e "     -q:  install in an unattended mode"
+    echo -e "     -u USER_OWNER  user owner of the service, 'root' by default"
+    echo -e "     -f PATH  path where openmano source is located. If missing it is downloaded from git"
+    echo -e "     -d --delete:  if -f is provided, delete this path after copying to /opt"
     echo -e "     -h:  show this help"
+    echo -e "     --uninstall: remove created service and files"
 }
 
+function uninstall(){
+    echo "systemctl disable openmano.service " &&  systemctl disable openmano.service 2>/dev/null ||
+        echo "  Already done"
+    echo "systemctl disable osm-ro.service " &&  systemctl disable osm-ro.service 2>/dev/null ||
+        echo "  Already done"
+    echo "service openmano stop " && service openmano stop 2>/dev/null || echo "  Already done"
+    echo "service osm-ro stop " && service osm-ro stop 2>/dev/null || echo "  Already done"
+    for file in /opt/openmano /etc/default/openmanod.cfg /etc/osm/openmanod.cfg /var/log/openmano /var/log/osm/openmano* \
+        /etc/systemd/system/openmano.service /etc/systemd/system/osm-ro.service /usr/bin/openmano /usr/sbin/service-openmano \
+        /usr/bin/openmano-report
+    do
+        echo rm $file
+        rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
+    done
+    echo "Done"
+}
 
-USER="root"
+GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
+USER_OWNER="root"
 QUIET_MODE=""
 FILE=""
 DELETE=""
-while getopts ":u:f:hq-:" o; do
+while getopts ":u:f:hdq-:" o; do
     case "${o}" in
         u)
-            export USER="$OPTARG"
+            export USER_OWNER="$OPTARG"
             ;;
         f)
             export FILE="$OPTARG"
@@ -53,9 +71,14 @@ while getopts ":u:f:hq-:" o; do
         h)
             usage && exit 0
             ;;
+        d)
+            DELETE=y
+            ;;
         -)
             [ "${OPTARG}" == "help" ] && usage && exit 0
-            echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 
+            [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
+            [ "${OPTARG}" == "delete" ] && DELETE=y && continue
+            echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
             exit 1
             ;; 
         \?)
@@ -72,19 +95,24 @@ while getopts ":u:f:hq-:" o; do
             ;;
     esac
 done
+BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
 
-#check root privileges and non a root user behind
+#check root privileges
 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
 
 #Discover Linux distribution
 #try redhat type
-[ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut  -d" " -f1) 
-#if not assuming ubuntu type
-[ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is  2>/dev/null)            
-if [ "$_DISTRO" == "Ubuntu" ]
+if [[ -f /etc/redhat-release ]]
+then 
+    _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut  -d" " -f1)
+else 
+    #if not assuming ubuntu type
+    _DISTRO=$(lsb_release -is  2>/dev/null)
+fi            
+if [[ "$_DISTRO" == "Ubuntu" ]]
 then
-    _RELEASE=`lsb_release -rs`
-    if ! lsb_release -rs | grep -q -e "16.04"
+    _RELEASE=$(lsb_release -rs)
+    if [[ ${_RELEASE%%.*} != 16 ]] 
     then 
         echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
     fi
@@ -93,35 +121,48 @@ else
 fi
 
 
-if [[ -z $FILE ]]
+if [[ -z "$FILE" ]]
 then
-    git clone https://osm.etsi.org/gerrit/osm/RO.git openmano
-    FILE=./openmano
+    FILE=__temp__${RANDOM}
+    git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
     DELETE=y
+else
+    [[ -d  "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1
 fi
-cp -r $FILE /opt/openmano
-cp ${FILE}/openmano /usr/sbin/
-mv /opt/openmano/openmanod.cfg /etc/default/openmanod.cfg
-mkdir -p /var/log/openmano/
-mkdir -p etc/systemd/system/
-
 
-cat  >> /etc/systemd/system/openmano.service  << EOF 
+#make idempotent
+uninstall
+#copy files
+cp -r "$FILE" /opt/openmano         || ! echo $BAD_PATH_ERROR >&2 || exit 1
+mkdir -p /etc/osm  || echo "warning cannot create config folder '/etc/osm'"
+cp /opt/openmano/osm_ro/openmanod.cfg /etc/osm/openmanod.cfg  ||
+    echo "warning cannot create file '/etc/osm/openmanod.cfg'"
+mkdir -p /var/log/osm  || echo "warning cannot create log folder '/var/log/osm'"
+#makes links
+ln -s -v /opt/openmano/openmano /usr/bin/openmano
+ln -s -v /opt/openmano/scripts/service-openmano /usr/sbin/service-openmano
+ln -s -v /opt/openmano/scripts/openmano-report /usr/bin/openmano-report
+
+chown -R $SUDO_USER /opt/openmano
+
+mkdir -p /etc/systemd/system/
+cat  > /etc/systemd/system/osm-ro.service  << EOF 
 [Unit]
 Description=openmano server
 
 [Service]
-User=${USER}
-ExecStart=/opt/openmano/openmanod.py -c /etc/default/openmanod.cfg --log-file=/var/log/openmano/openmano.log
+User=${USER_OWNER}
+ExecStart=/opt/openmano/openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log
 Restart=always
 
 [Install]
 WantedBy=multi-user.target
 EOF
 
-[[ -n $DELETE ]] && rm -rf $FILE
+[[ -n $DELETE ]] && rm -rf "${FILE}"
 
-service openmano start
+service osm-ro start
+systemctl enable osm-ro.service
 
 echo Done
 exit