From: tierno Date: Fri, 26 Aug 2016 12:39:42 +0000 (+0200) Subject: scripts to install openmano as a service on Xenial, and enhancements on installation X-Git-Tag: v1.0.0~52 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=45a528522edd40c3cdf5a72a2f9aab6e5b95d899;p=osm%2FRO.git scripts to install openmano as a service on Xenial, and enhancements on installation Change-Id: I526e1bcd91a2e65c9be5dca372766181377f9e42 Signed-off-by: tierno --- diff --git a/openmanod.py b/openmanod.py index c3de0127..c2b61a1b 100755 --- a/openmanod.py +++ b/openmanod.py @@ -231,13 +231,15 @@ if __name__=="__main__": 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 + #logger.debug("moving logs to '%s'", global_config["log_file"]) + #remove initial stream handler logging.root.removeHandler(logging.root.handlers[0]) + print ("logging on '{}'".format(global_config["log_file"])) 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'])) + logger.critical("Starting openmano server command: '%s'", sys.argv[0]) # Initialize DB connection mydb = nfvo_db.nfvo_db(log_level=global_config["log_level_db"]); diff --git a/scripts/install-openmano-service.sh b/scripts/install-openmano-service.sh index a9d0c37e..d1a1fda3 100755 --- a/scripts/install-openmano-service.sh +++ b/scripts/install-openmano-service.sh @@ -28,21 +28,32 @@ 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 " -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 " -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 + do + rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1 + done + echo "Done" +} -USER="root" +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 case "${o}" in u) - export USER="$OPTARG" + export USER_OWNER="$OPTARG" ;; f) export FILE="$OPTARG" @@ -55,6 +66,7 @@ while getopts ":u:f:hq-:" o; do ;; -) [ "${OPTARG}" == "help" ] && usage && exit 0 + [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 exit 1 ;; @@ -73,18 +85,22 @@ while getopts ":u:f:hq-:" o; do esac done -#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 @@ -95,24 +111,33 @@ fi if [[ -z $FILE ]] then - git clone https://osm.etsi.org/gerrit/osm/RO.git openmano - FILE=./openmano + git clone $GIT_URL __temp__ || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1 + #git checkout + FILE=./__temp__ 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/ +#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 +mkdir /opt/openmano/logs +#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'" -cat >> /etc/systemd/system/openmano.service << EOF +chown $USER_OWNER /opt/openmano/openmanod.cfg +chown -R $USER_OWNER /opt/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 +User=${USER_OWNER} +ExecStart=/opt/openmano/openmanod.py -c /opt/openmano/openmanod.cfg --log-file=/opt/openmano/logs/openmano.log Restart=always [Install] diff --git a/scripts/install-openmano.sh b/scripts/install-openmano.sh index e9398b76..f57439e8 100755 --- a/scripts/install-openmano.sh +++ b/scripts/install-openmano.sh @@ -29,12 +29,14 @@ function usage(){ echo -e "usage: sudo $0 [OPTIONS]" - echo -e "Install source code in ./openmano and the needed packages" + echo -e "Install last stable source code in ./openmano and the needed packages" + echo -e "On a Ubuntu 16.04 it configures openmano as a service" echo -e " OPTIONS" - echo -e " -u USER database admin user. 'root' by default. Prompts if needed" - echo -e " -p PASS database admin password to be used or installed.Prompts if needed" - echo -e " -q: install in an unattended mode" - echo -e " -h: show this help" + echo -e " -u USER: database admin user. 'root' by default. Prompts if needed" + echo -e " -p PASS: database admin password to be used or installed. Prompts if needed" + echo -e " -q --quiet: install in an unattended mode" + echo -e " -h --help: show this help" + echo -e " --develop: install last version for developers, and do not configure as a service" } function install_packages(){ @@ -55,10 +57,12 @@ function install_packages(){ done } +GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git DBUSER="root" DBPASSWD="" DBPASSWD_PARAM="" QUIET_MODE="" +DEVELOP="" while getopts ":u:p:hiq-:" o; do case "${o}" in u) @@ -77,6 +81,8 @@ while getopts ":u:p:hiq-:" o; do ;; -) [ "${OPTARG}" == "help" ] && usage && exit 0 + [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue + [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 exit 1 ;; @@ -111,11 +117,12 @@ fi [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null) if [ "$_DISTRO" == "Ubuntu" ] then - _RELEASE="14" - if ! lsb_release -rs | grep -q "14." + _RELEASE=$(lsb_release -rs) + if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]] then - [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Ubuntu version. Continue assuming a '$_RELEASE' type? (y/N)" KK + [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Ubuntu version. Continue assuming a trusty (14.XX)'? (y/N)" KK [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1 + _RELEASE = 14 fi elif [ "$_DISTRO" == "CentOS" ] then @@ -223,7 +230,8 @@ echo ' ################################################################# ##### DOWNLOAD SOURCE ##### #################################################################' -su $SUDO_USER -c 'git clone https://osm.etsi.org/gerrit/osm/openmano.git openmano' +su $SUDO_USER -c 'git clone '"${GIT_URL}"' openmano' +#[[ -z $DEVELOP ]] && su $SUDO_USER -c 'git checkout ' echo ' ################################################################# @@ -306,9 +314,32 @@ then su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc' fi -echo -echo "Done! you may need to logout and login again for loading the configuration" -echo " Run './openmano/scripts/service-openmano.sh start' for starting openmano in a screen" + + + +if [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]] +then +echo ' +################################################################# +##### CONFIGURE OPENMANO SERVICE ##### +#################################################################' + + ./openmano/scripts/install-service-openmano.sh -f openmano #-u $SUDO_USER +# 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|...' " + + +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" + +fi diff --git a/scripts/openmano-report.sh b/scripts/openmano-report.sh index c7bfe7ee..9402facc 100755 --- a/scripts/openmano-report.sh +++ b/scripts/openmano-report.sh @@ -36,8 +36,8 @@ echo "-------------------------------" echo "-------------------------------" echo "OPENMANO" echo "-------------------------------" -echo "cat $DIRNAME/../logs/openmano.?" -cat $DIRNAME/../logs/openmano.? +echo "cat $DIRNAME/../logs/openmano.log*" +cat $DIRNAME/../logs/openmano.log* echo echo "-------------------------------" echo "OPENVIM" diff --git a/scripts/service-openmano.sh b/scripts/service-openmano.sh index ba2bc036..08e929e1 100755 --- a/scripts/service-openmano.sh +++ b/scripts/service-openmano.sh @@ -99,7 +99,7 @@ do then #calculates log file name logfile="" - mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/open${om_component} || echo "can not create logs directory $DIR_OM/logs" + mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/open${om_component}.log || echo "can not create logs directory $DIR_OM/logs" #check already running [ -n "$component_id" ] && echo " $om_name is already running. Skipping" && continue #create screen if not created @@ -119,11 +119,12 @@ do #move old log file index one number up and log again in index 0 if [[ -n $logfile ]] then - for index in 8 7 6 5 4 3 2 1 0 + for index in 8 7 6 5 4 3 2 1 do [[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$((index+1)) done - screen -S ${om_component} -p 0 -X logfile ${logfile}.0 + [[ -f ${logfile} ]] && mv ${logfile} ${logfile}.1 + screen -S ${om_component} -p 0 -X logfile ${logfile} screen -S ${om_component} -p 0 -X log on fi #launch command to screen @@ -137,14 +138,14 @@ do #echo timeout $timeout #if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd} log_lines=0 - [[ -n $logfile ]] && log_lines=`head ${logfile}.0 | wc -l` + [[ -n $logfile ]] && log_lines=`head ${logfile} | wc -l` component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'` if [[ -z $component_id ]] then #process not started or finished [[ $log_lines -ge 2 ]] && echo -n "ERROR, it has exited." && break #started because writted serveral lines at log so report error fi - [[ -n $logfile ]] && grep -q "open${om_component}d ready" ${logfile}.0 && break + [[ -n $logfile ]] && grep -q "open${om_component}d ready" ${logfile} && break sleep 1 timeout=$((timeout -1)) done @@ -154,7 +155,7 @@ do else echo -n "running on 'screen -x ${om_component}'." fi - [[ -n $logfile ]] && echo " Logging at '${logfile}.0'" || echo + [[ -n $logfile ]] && echo " Logging at '${logfile}'" || echo fi done