Merge branch 'feature628'
[osm/RO.git] / scripts / install-openmano-service.sh
index c1b3f37..54783ad 100755 (executable)
 
 function usage(){
     echo -e "usage: sudo $0 [OPTIONS]"
-    echo -e "Configures openmano to run as a service"
+    echo -e "Configures openmano to run as a service at /opt"
     echo -e "  OPTIONS"
     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 "     -q:  install in an unattended mode"
+    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(){
-    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
+        echo rm $file
         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=""
 DELETE=""
-while getopts ":u:f:hq-:" o; do
+while getopts ":u:f:hdq-:" o; do
     case "${o}" in
         u)
             export USER_OWNER="$OPTARG"
@@ -64,10 +65,14 @@ while getopts ":u:f:hq-:" o; do
         h)
             usage && exit 0
             ;;
+        d)
+            DELETE=y
+            ;;
         -)
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
-            echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 
+            [ "${OPTARG}" == "delete" ] && DELETE=y && continue
+            echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
             exit 1
             ;; 
         \?)
@@ -84,6 +89,7 @@ while getopts ":u:f:hq-:" o; do
             ;;
     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
@@ -109,30 +115,30 @@ else
 fi
 
 
-if [[ -z $FILE ]]
+if [[ -z "$FILE" ]]
 then
-    git clone $GIT_URL __temp__ || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
-    #git checkout <tag version>
-    FILE=./__temp__
+    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
 
-#make idenpotent
-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
+#make idempotent
+uninstall
+#copy files
+cp -r "$FILE" /opt/openmano         || ! echo $BAD_PATH_ERROR >&2 || exit 1
 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/sbin/openmano
+#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 $USER_OWNER /opt/openmano/openmanod.cfg
-chown -R $USER_OWNER /opt/openmano
+chown -R $SUDO_USER /opt/openmano
 
-mkdir -p etc/systemd/system/
+mkdir -p /etc/systemd/system/
 cat  > /etc/systemd/system/openmano.service  << EOF 
 [Unit]
 Description=openmano server
@@ -146,9 +152,10 @@ Restart=always
 WantedBy=multi-user.target
 EOF
 
-[[ -n $DELETE ]] && rm -rf $FILE
+[[ -n $DELETE ]] && rm -rf ${FILE}
 
 service openmano start
+systemctl enable openmano.service
 
 echo Done
 exit