v0.4.44 new command line option for log file configuration at openmanod server. New...
authortierno <alfonso.tiernosepulveda@telefonica.com>
Thu, 21 Jul 2016 09:26:22 +0000 (11:26 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 22 Jul 2016 13:23:12 +0000 (15:23 +0200)
Change-Id: I8464caa0d537842011e31e3a380ded1942896f3a
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
openmanod.py
scripts/install-openmano-service.sh [new file with mode: 0755]
test/basictest.sh

index bedcba8..dbd7737 100755 (executable)
@@ -33,7 +33,7 @@ It loads the configuration file and launches the http_server thread that will li
 '''
 __author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
 __date__ ="$26-aug-2014 11:09:29$"
-__version__="0.4.43-r481"
+__version__="0.4.44-r482"
 version_date="Jul 2016"
 database_version="0.11"      #expected database schema version
 
@@ -125,6 +125,7 @@ def usage():
     #print( "      -V|--vnf-repository: changes the path of the vnf-repository and overrides the path in the configuration file")
     print( "      --log-socket-host: send logs to this host")
     print( "      --log-socket-port: send logs using this port (default: 9022)")
+    print( "      --log-file: send logs to this file")
     return
     
 if __name__=="__main__":
@@ -146,11 +147,12 @@ if __name__=="__main__":
     # Read parameters and configuration file 
     try:
         #load parameters and configuration
-        opts, args = getopt.getopt(sys.argv[1:], "hvc:V:p:P:", ["config", "help", "version", "port", "vnf-repository", "adminport", "log-socket-host"])
+        opts, args = getopt.getopt(sys.argv[1:], "hvc:V:p:P:", ["config", "help", "version", "port", "vnf-repository", "adminport", "log-socket-host=", "log-socket-port=", "log-file="])
         port=None
         port_admin = None
         config_file = 'openmanod.cfg'
         vnf_repository = None
+        log_file = None
         log_socket_host = None
         log_socket_port = None
         
@@ -174,6 +176,8 @@ if __name__=="__main__":
                 log_socket_port = a
             elif o == "--log-socket-port":
                 log_socket_host = a
+            elif o == "--log-file":
+                log_file = a
             else:
                 assert False, "Unhandled option"
         global_config = load_configuration(config_file)
@@ -187,6 +191,8 @@ if __name__=="__main__":
             global_config['log_socket_host'] = log_socket_host
         if log_socket_port:
             global_config['log_socket_port'] = log_socket_port
+        if log_file:
+            global_config['log_file'] = log_file
 #         if vnf_repository is not None:
 #             global_config['vnf_repository'] = vnf_repository
 #         else:
@@ -207,22 +213,25 @@ if __name__=="__main__":
         global_config["console_ports"]={}
         
         #Configure logging STEP 2
-        logging.basicConfig(level = getattr(logging, global_config.get('log_level',"debug")))
-        logger.setLevel(getattr(logging, global_config['log_level']))
         if "log_host" in global_config:
             socket_handler= log_handlers.SocketHandler(global_config["log_socket_host"], global_config["log_socket_port"])
             socket_handler.setFormatter(log_formatter_complete)
             if global_config.get("log_socket_level") and global_config["log_socket_level"] != global_config["log_level"]: 
                 socket_handler.setLevel(global_config["log_socket_level"])
             logger.addHandler(socket_handler)
-        logger.addHandler(log_handlers.SysLogHandler())
+        #logger.addHandler(log_handlers.SysLogHandler())
         if "log_file" in global_config:
             try:
                 file_handler= logging.handlers.RotatingFileHandler(global_config["log_file"], maxBytes=100e6, backupCount=9, delay=0)
                 file_handler.setFormatter(log_formatter_simple)
                 logger.addHandler(file_handler)
+                logger.debug("moving logs to '%s'", global_config["log_file"])
+                #remove initial strema handler
+                logging.root.removeHandler(logging.root.handlers[0])
             except IOError as e:
                 raise LoadConfigurationException("Cannot open logging file '{}': {}. Check folder exist and permissions".format(global_config["log_file"], str(e)) ) 
+        #logging.basicConfig(level = getattr(logging, global_config.get('log_level',"debug")))
+        logger.setLevel(getattr(logging, global_config['log_level']))
         
         # Initialize DB connection
         mydb = nfvo_db.nfvo_db(log_level=global_config["log_level_db"]);
@@ -247,6 +256,7 @@ if __name__=="__main__":
             httpthreadadmin.start()
         time.sleep(1)      
         logger.info('Waiting for http clients')
+        print('Waiting for http clients')
         print('openmanod ready')
         print('====================')
         time.sleep(20)
diff --git a/scripts/install-openmano-service.sh b/scripts/install-openmano-service.sh
new file mode 100755 (executable)
index 0000000..a9d0c37
--- /dev/null
@@ -0,0 +1,127 @@
+#!/bin/bash
+
+##
+# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
+# This file is part of openmano
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact with: nfvlabs@tid.es
+##
+
+#ONLY TESTED for Ubuntu 16.04
+#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 "  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 "     -h:  show this help"
+}
+
+
+USER="root"
+QUIET_MODE=""
+FILE=""
+DELETE=""
+while getopts ":u:f:hq-:" o; do
+    case "${o}" in
+        u)
+            export USER="$OPTARG"
+            ;;
+        f)
+            export FILE="$OPTARG"
+            ;;
+        q)
+            export QUIET_MODE=yes
+            ;;
+        h)
+            usage && exit 0
+            ;;
+        -)
+            [ "${OPTARG}" == "help" ] && usage && exit 0
+            echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 
+            exit 1
+            ;; 
+        \?)
+            echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
+            exit 1
+            ;;
+        :)
+            echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
+            exit 1
+            ;;
+        *)
+            usage >&2
+            exit -1
+            ;;
+    esac
+done
+
+#check root privileges and non a root user behind
+[ "$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" ]
+then
+    _RELEASE=`lsb_release -rs`
+    if ! lsb_release -rs | grep -q -e "16.04"
+    then 
+        echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
+    fi
+else
+    echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
+fi
+
+
+if [[ -z $FILE ]]
+then
+    git clone https://osm.etsi.org/gerrit/osm/RO.git openmano
+    FILE=./openmano
+    DELETE=y
+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 
+[Unit]
+Description=openmano server
+
+[Service]
+User=${USER}
+ExecStart=/opt/openmano/openmanod.py -c /etc/default/openmanod.cfg --log-file=/var/log/openmano/openmano.log
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
+EOF
+
+[[ -n $DELETE ]] && rm -rf $FILE
+
+service openmano start
+
+echo Done
+exit
index 91e87ec..ad243c6 100755 (executable)
@@ -56,27 +56,33 @@ force=""
 action_list=""
 insert_bashrc=""
 init_openvim=""
-for param in $*
+
+while [[ $# -gt 0 ]]
 do
-    if [[ $param == reset ]] || [[ $param == create ]] || [[ $param == delete ]]
-    then
-        action_list="$action_list $param"
-    elif [[ $param == -h ]] || [[ $param == --help ]]
-    then
-        usage
-        $_exit 0
-    elif [[ $param == -f ]] || [[ $param == --force ]]
-    then
-        force="-f"
-    elif [[ $param == --insert-bashrc ]]
+    argument="$1"
+    shift
+    if [[ $argument == reset ]] || [[ $argument == create ]] || [[ $argument == delete ]]
     then
-        insert_bashrc=y
-    elif [[ $param == --init-openvim ]]
+        action_list="$action_list $argument"
+        continue
+    #short options
+    elif [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
     then
-        init_openvim=y
-    else
-        echo "invalid argument '$param'?  Type -h for help" >&2 && $_exit 1
+        index=0
+        while index=$((index+1)) && [[ $index -lt ${#argument} ]]
+        do
+            [[ ${argument:$index:1} == h ]]  && usage   && $_exit 0
+            [[ ${argument:$index:1} == f ]]  && force="-f" && continue
+            echo "invalid option '${argument:$index:1}'?  Type -h for help" >&2 && $_exit 1
+        done
+        continue
     fi
+    #long options
+    [[ $argument == --help ]]   && usage   && $_exit 0
+    [[ $argument == --force ]]  && force="-f" && continue
+    [[ $argument == --insert-bashrc ]] && insert_bashrc="--insert-bashrc" && continue
+    [[ $argument == --init-openvim ]] && init_openvim="y" && continue
+    echo "invalid argument '$argument'?  Type -h for help" >&2 && $_exit 1
 done
 
 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
@@ -84,13 +90,13 @@ DIRmano=$(dirname $DIRNAME)
 DIRscript=${DIRmano}/scripts
 export OPENMANO_HOST=localhost
 export OPENMANO_PORT=9090
-[[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_HOST=localhost"  >> ~/.bashrc
-[[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_PORT=9090"  >> ~/.bashrc
+[[ -n "$insert_bashrc" ]] && echo -e "\nexport OPENMANO_HOST=localhost"  >> ~/.bashrc
+[[ -n "$insert_bashrc" ]] && echo -e "\nexport OPENMANO_PORT=9090"  >> ~/.bashrc
 
 
 #by default action should be reset and create
 [[ -z $action_list ]]  && action_list="reset create delete"
-[[ -z $init_openvim ]] || initopenvim $force || echo "WARNING openvim cannot be initialized. The rest of test can fail!"
+[[ -z $init_openvim ]] || initopenvim $force $insert_bashrc || echo "WARNING openvim cannot be initialized. The rest of test can fail!"
 
 #check openvim client variables are set
 #fail=""
@@ -155,7 +161,7 @@ then
     #check a valid uuid is obtained
     ! is_valid_uuid $nfvotenant && echo "FAIL" && echo "    $result" && $_exit 1
     export OPENMANO_TENANT=$nfvotenant
-    [[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_TENANT=$nfvotenant"  >> ~/.bashrc
+    [[ -n "$insert_bashrc" ]] && echo -e "\nexport OPENMANO_TENANT=$nfvotenant"  >> ~/.bashrc
     echo $nfvotenant
 
     printf "%-50s" "Creating datacenter 'TEST-dc' in openmano:"
@@ -169,7 +175,7 @@ then
     ! is_valid_uuid $datacenter && echo "FAIL" && echo "    $result" && $_exit 1
     echo $datacenter
     export OPENMANO_DATACENTER=$datacenter
-    [[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_DATACENTER=$datacenter"  >> ~/.bashrc
+    [[ -n "$insert_bashrc" ]] && echo -e "\nexport OPENMANO_DATACENTER=$datacenter"  >> ~/.bashrc
 
     printf "%-50s" "Attaching openmano tenant to the datacenter:"
     result=`${DIRmano}/openmano datacenter-attach TEST-dc`