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"]);
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"
;;
-)
[ "${OPTARG}" == "help" ] && usage && exit 0
+ [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
exit 1
;;
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
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 <tag version>
+ 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]
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(){
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)
;;
-)
[ "${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
;;
[ -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
#################################################################
##### 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 <tag version>'
echo '
#################################################################
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
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"
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
#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
#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
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