--- /dev/null
+#!/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
+##
+
+
+#Utility for getting options, must be call with source
+#for every <option> it sets a variable 'option_<option>="-"'
+#if the option appears more than once, it concatenates a "-"
+#if the option contains an argument: 'option_<option>="argument"'
+#if the long option name contains "-" they are converted to "_"
+#params that are not options are stored in 'params'
+#the options to look for is received in the first argument,
+#a blank separator list with short and long options without the leading - or --
+#options to be stored in the same variable must appear in the same word separated by ':'
+#insert a trailing = if the option requires an argument
+#insert a trailing ? if the option may have an argument NOT IMPLEMENTED
+#option -- means get the rest of argument returned as 'option__=$*'
+
+#example: to allow options -h --help -j -k(with argument) --my-long-option(with argument)
+# and other parameters after -- provide
+# "help:h j k= my-long-option="
+#parsing "-h -karg pepe --my-long-option=otherar -- -s" will set variables
+# option_help="-"
+# option_k="arg"
+# option_my_long_option="otherarg"
+# params=" pepe"
+# option__="-s"
+
+
+#detect if is called with a source to use the 'exit'/'return' command for exiting
+[[ ${BASH_SOURCE[0]} != $0 ]] && ___exit="return" || ___exit="exit"
+
+options="$1"
+shift
+
+get_argument=""
+#reset variables
+params=""
+for option_group in $options
+do
+ _name=${option_group%%:*}
+ _name=${_name%=}
+ _name=${_name//-/_}
+ eval option_${_name}='""'
+done
+
+while [[ $# -gt 0 ]]
+do
+ argument="$1"
+ shift
+ if [[ -n $get_argument ]]
+ then
+ [[ ${argument:0:1} == "-" ]] && echo "option '-$option' requires an argument" >&2 && $___exit 1
+ eval ${get_argument}='"$argument"'
+ #echo option $get_argument with argument
+ get_argument=""
+ continue
+ fi
+
+
+ #short options
+ if [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
+ then
+ index=0
+ while index=$((index+1)) && [[ $index -lt ${#argument} ]]
+ do
+ option=${argument:$index:1}
+ bad_option=y
+ for option_group in $options
+ do
+ _name=""
+ for o in $(echo $option_group | tr ":=" " ")
+ do
+ [[ -z "$_name" ]] && _name=${o//-/_}
+ #echo option $option versus $o
+ if [[ "$option" == "${o}" ]]
+ then
+ eval option_${_name}='${option_'${_name}'}-'
+ bad_option=n
+ if [[ ${option_group:${#option_group}-1} != "=" ]]
+ then
+ continue
+ fi
+ if [[ ${#argument} -gt $((index+1)) ]]
+ then
+ eval option_${_name}='"${argument:$((index+1))}"'
+ index=${#argument}
+ else
+ get_argument=option_${_name}
+ #echo next should be argument $argument
+ fi
+
+ break
+ fi
+ done
+ done
+ [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
+ done
+ elif [[ ${argument:0:2} == "--" ]] && [[ ${#argument} -ge 3 ]]
+ then
+ option=${argument:2}
+ option_argument=${option#*=}
+ option_name=${option%%=*}
+ [[ "$option_name" == "$option" ]] && option_argument=""
+ bad_option=y
+ for option_group in $options
+ do
+ _name=""
+ for o in $(echo $option_group | tr ":=" " ")
+ do
+ [[ -z "$_name" ]] && _name=${o//-/_}
+ #echo option $option versus $o
+ if [[ "$option_name" == "${o}" ]]
+ then
+ bad_option=n
+ if [[ ${option_group:${#option_group}-1} != "=" ]]
+ then #not an argument
+ [[ -n "${option_argument}" ]] && echo "option '--${option%%=*}' do not accept an argument " >&2 && $___exit 1
+ eval option_${_name}='"${option_'${_name}'}-"'
+ elif [[ -n "${option_argument}" ]]
+ then
+ eval option_${_name}='"${option_argument}"'
+ else
+ get_argument=option_${_name}
+ #echo next should be argument $argument
+ fi
+ break
+ fi
+ done
+ done
+ [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
+ elif [[ ${argument:0:2} == "--" ]]
+ then
+ option__="$*"
+ bad_option=y
+ for o in $options
+ do
+ if [[ "$o" == "--" ]]
+ then
+ bad_option=n
+ option__=" $*"
+ break
+ fi
+ done
+ [[ $bad_option == y ]] && echo "invalid argument '--'? Type -h for help" >&2 && $___exit 1
+ break
+ else
+ params="$params ${argument}"
+ fi
+
+done
+
+[[ -n "$get_argument" ]] && echo "option '-$option' requires an argument" >&2 && $___exit 1
+$___exit 0
+#echo params $params
+
#stopping on an error
#WARNING: It destroy the database content
+DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
+DIRNAME=$(dirname $DIRNAME )
function usage(){
echo -e "usage: ${BASH_SOURCE[0]} [OPTIONS] <action>\n Deletes openvim content and add fake hosts, networks"
echo -e " OPTIONS:"
echo -e " -f --force : does not prompt for confirmation"
echo -e " -d --delete : same to action delete-all"
+ echo -e " -p --port PORT : port to start openvim service"
+ echo -e " -P --admin-port PORT : administrator port to start openvim service"
+ echo -e " --screen-name NAME : screen name to launch openvim (default vim)"
+ echo -e " --dbname NAME : database name to use (default vim_db)"
echo -e " --insert-bashrc insert the created tenant variables at"
echo -e " ~/.bashrc to be available by openvim CLI"
echo -e " -h --help : shows this help"
#detect if is called with a source to use the 'exit'/'return' command for exiting
[[ ${BASH_SOURCE[0]} != $0 ]] && _exit="return" || _exit="exit"
+
+#process options
+source ${DIRNAME}/get-options.sh "force:f delete:d delete-all port:p= admin-port:P= screen-name= help:h dbname= insert-bashrc" $* || $_exit 1
+
+
#check correct arguments
-force=""
action_list=""
-insert_bashrc=""
-
-while [[ $# -gt 0 ]]
+for param in $params
do
- argument="$1"
- shift
- if [[ $argument == reset ]] || [[ $argument == create ]] || [[ $argument == delete ]] || [[ $argument == delete-all ]]
+ if [[ "$param" == reset ]] || [[ "$param" == create ]] || [[ "$param" == delete ]] || [[ "$param" == delete-all ]]
then
action_list="$action_list $argument"
continue
- #short options
- elif [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
- then
- index=0
- while index=$((index+1)) && [[ $index -lt ${#argument} ]]
- do
- [[ ${argument:$index:1} == h ]] && usage && $_exit 0
- [[ ${argument:$index:1} == f ]] && force=y && continue
- [[ ${argument:$index:1} == d ]] && action_list="delete-all $action_list" && continue
- echo "invalid option '${argument:$index:1}'? Type -h for help" >&2 && $_exit 1
- done
- continue
+ else
+ echo "invalid argument '$param'? Type -h for help" >&2 && $_exit 1
fi
- #long options
- [[ $argument == --help ]] && usage && $_exit 0
- [[ $argument == --force ]] && force=y && continue
- [[ $argument == --delete ]] && action_list="delete-all $action_list" && continue
- [[ $argument == --insert-bashrc ]] && insert_bashrc=y && continue
- echo "invalid argument '$argument'? Type -h for help" >&2 && $_exit 1
done
+#help
+[[ -n "$option_help" ]] && usage && $_exit 0
+
+#check numeric values for port
+[[ -n "$option_port" ]] && ( [[ "$option_port" -lt 1 ]] || [[ "$option_port" -gt 65535 ]] ) && echo "Option '-p' or '--port' requires a valid numeric argument" >&2 && $_exit 1
+[[ -n "$option_admin_port" ]] && ( [[ "$option_admin_port" -lt 1 ]] || [[ "$option_admin_port" -gt 65535 ]] ) && echo "Option '-P' or '--admin-port' requieres a valid numeric argument" >&2 && $_exit 1
+
+[[ -n "$option_screen_name" ]] && screen_name="$option_screen_name" && screen_name_param=" --screen-name $screen_name"
+[[ -z "$option_screen_name" ]] && screen_name=vim && screen_name_param="" #default value
+
+[[ -n "$option_delete" ]] && action_list="delete-all $action_list"
+
+openvim_param=" --"
+[[ -n "$option_port" ]] && openvim_param="$openvim_param -p $option_port"
+[[ -n "$option_admin_port" ]] && openvim_param="$openvim_param -P $option_admin_port"
+[[ -n "$option_dbname" ]] && openvim_param="$openvim_param --dbname $option_dbname"
+[[ $openvim_param = " --" ]] && openvim_param=""
+db_name=vim_db #default value
+[[ -n "$option_dbname" ]] && db_name="$option_dbname"
+
DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
DIRvim=$(dirname $DIRNAME)
export OPENVIM_HOST=localhost
-export OPENVIM_PORT=9080
-[[ $insert_bashrc == y ]] && echo -e "\nexport OPENVIM_HOST=localhost" >> ~/.bashrc
-[[ $insert_bashrc == y ]] && echo -e "\nexport OPENVIM_PORT=9080" >> ~/.bashrc
+[[ -n "$option_port" ]] && export OPENVIM_PORT=$option_port
+[[ -n "$option_admin_port" ]] && export OPENVIM_ADMIN_PORT=$option_admin_port
+
+[[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_HOST=localhost" >> ~/.bashrc
+[[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_PORT=9080" >> ~/.bashrc
#by default action should be reset and create
-[[ -z $action_list ]] && action_list="reset create"
+[[ -z "$action_list" ]] && action_list="reset create"
for action in $action_list
then
#ask for confirmation if argument is not -f --force
force_="y"
- [[ $force != y ]] && read -e -p "WARNING: openvim database content will be lost!!! Continue(y/N)" force_
+ [[ -z "$option_force" ]] && read -e -p "WARNING: openvim database content will be lost!!! Continue(y/N)" force_
[[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
echo "deleting deployed vm"
${DIRvim}/openvim vm-delete -f | grep -q deleted && sleep 10 #give some time to get virtual machines deleted
- echo "Stopping openvim"
- $DIRNAME/service-openvim.sh stop
- echo "Initializing databases"
- $DIRvim/database_utils/init_vim_db.sh -u vim -p vimpw
- echo "Starting openvim"
- $DIRNAME/service-openvim.sh start
+ echo "Stopping openvim${screen_name_param}${openvim_param}"
+ $DIRNAME/service-openvim.sh stop${screen_name_param}${openvim_param}
+ echo "Initializing databases $db_name"
+ $DIRvim/database_utils/init_vim_db.sh -u vim -p vimpw -d $db_name
+ echo "Starting openvim${screen_name_param}${openvim_param}"
+ $DIRNAME/service-openvim.sh start${screen_name_param}${openvim_param}
elif [[ $action == delete-all ]]
then
items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
if [[ -n $items ]]
then
- [[ $force == y ]] && echo deleting openvim ${what}s from tenant ${t_name}
- [[ $force != y ]] && read -e -p "Delete openvim ${what}s from tenant ${t_name}?(y/N) " force_
+ [[ $option_force == "-" ]] && echo deleting openvim ${what}s from tenant ${t_name}
+ [[ $option_force != "-" ]] && read -e -p "Delete openvim ${what}s from tenant ${t_name}?(y/N) " force_
[[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
for item in $items
do
items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
if [[ -n $items ]]
then
- [[ $force == y ]] && echo deleting openvim ${what}s
- [[ $force != y ]] && read -e -p "Delete openvim ${what}s?(y/N) " force_
+ [[ $option_force == "-" ]] && echo deleting openvim ${what}s
+ [[ $option_force != "-" ]] && read -e -p "Delete openvim ${what}s?(y/N) " force_
[[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
for item in $items
do
! is_valid_uuid $vimtenant && echo "FAIL" && echo " $result" && $_exit 1
echo " $vimtenant"
export OPENVIM_TENANT=$vimtenant
- [[ $insert_bashrc == y ]] && echo -e "\nexport OPENVIM_TENANT=$vimtenant" >> ~/.bashrc
+ [[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_TENANT=$vimtenant" >> ~/.bashrc
echo
#echo "Check virtual machines are deployed"
# contact with: nfvlabs@tid.es
##
-#launch openvim (andr floodlight) inside a screen.
+#launch openvim (and/or floodlight) inside a screen.
+#or call service if it is installed on systemd
#It assumes a relative path '..' for openvim
#for floodlight, the variable FLOODLIGHT_PATH indicates the installation path
function usage(){
echo -e "Usage: $0 [openvim/vim] [floodlight/flow] start|stop|restart|status"
- echo -e " Launch|Removes|Restart|Getstatus openvim (by default) or/and floodlight on a screen"
+ echo -e " Launch|Removes|Restart|Getstatus openvim (by default) or/and floodlight on a screen/service"
echo -e " For floodlight variable FLOODLIGHT_PATH must indicate installation path"
+ echo -e " -h --help: shows this help"
+ echo -e " -n --screen-name NAME : name of screen to launch openvim (default vim)"
+ echo -e " -- PARAMS use to separate PARAMS that will be send to the service. e.g. -pPORT -PADMINPORT --dbname=DDBB"
}
function kill_pid(){
}
+#process options
+source ${DIRNAME}/get-options.sh "screen-name:n= help:h --" $* || exit 1
+
+#help
+[[ -n "$option_help" ]] && usage && exit 0
+
+
#obtain parameters
om_list=""
#om_action="start" #uncoment to get a default action
-for param in $*
+action_list=""
+om_params="$option__"
+
+for param in $params
do
[ "$param" == "start" -o "$param" == "stop" -o "$param" == "restart" -o "$param" == "status" ] && om_action=$param && continue
[ "$param" == "openvim" -o "$param" == "vim" ] && om_list="$om_list vim" && continue
[ "$param" == "openmano" -o "$param" == "mano" ] && continue #allow and ingore for backwards compatibility
[ "$param" == "openflow" -o "$param" == "flow" -o "$param" == "floodlight" ] && om_list="flow $om_list" && continue
- [ "$param" == "-h" -o "$param" == "--help" ] && usage && exit 0
- #note flow that it must be the first element, because openvim relay on this
-
- #if none of above, reach this line because a param is incorrect
- echo "Unknown param '$param' type $0 --help" >&2
- exit -1
+ echo "invalid argument '$param'? Type -h for help" >&2 && exit 1
done
+[[ -n $option_screen_name ]] && option_screen_name=${option_screen_name#*.} #allow the format 'pid.name' and keep only name
+
#check action is provided
[ -z "$om_action" ] && usage >&2 && exit -1
#if no componenets supplied assume all
[ -z "$om_list" ] && om_list="vim"
+function find_process_id(){ #PARAMS: command screen-name
+ for process_id in `ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep "${1}" | awk '{print $1}'`
+ do
+ scname=$(ps wwep $process_id | grep -o 'STY=\S*')
+ scname=${scname#STY=}
+ [[ -n "$2" ]] && [[ "${scname#*.}" != "$2" ]] && continue
+ echo -n "${process_id} "
+ done
+ echo
+}
+
+
for om_component in $om_list
do
+ screen_name="${om_component}"
+ [[ -n "$option_screen_name" ]] && screen_name=$option_screen_name
[ "${om_component}" == "flow" ] && om_cmd="floodlight.jar" && om_name="floodlight" && om_dir=$FLOODLIGHT_PATH
- [ "${om_component}" == "vim" ] && om_cmd="openvimd.py" && om_name="openvim " && om_dir=${DIR_OM}
+ [ "${om_component}" == "vim" ] && om_cmd="./openvimd.py" && om_name="openvim " && om_dir=${DIR_OM}
#obtain PID of program
- component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
+ component_id=`find_process_id "${om_cmd}" $option_screen_name`
+ processes=$(echo $component_id | wc -w)
#status
if [ "$om_action" == "status" ]
then
- [ -n "$component_id" ] && echo " $om_name running, pid $component_id"
- [ -z "$component_id" ] && echo " $om_name stopped"
+ running=""
+ for process_id in $component_id
+ do
+ scname=$(ps wwep $process_id | grep -o 'STY=\S*')
+ scname=${scname#STY=}
+ [[ -n "$option_screen_name" ]] && [[ "${scname#*.}" != "$option_screen_name" ]] && continue
+ printf "%-15s" "pid: ${process_id},"
+ [[ -n "$scname" ]] && printf "%-25s" "screen: ${scname},"
+ echo cmd: $(ps -o cmd p $process_id | tail -n1 )
+ running=y
+ done
+ #if installed as a service and it is not provided a screen name call service
+ [[ -f /etc/systemd/system/openvim.service ]] && [[ -z $option_screen_name ]] && running=y #&& service openvim status
+ if [ -z "$running" ]
+ then
+ echo -n " $om_name not running" && [[ -n "$option_screen_name" ]] && echo " on screen '$option_screen_name'" || echo
+ fi
fi
+ #if installed as a service and it is not provided a screen name call service
+ [[ -f /etc/systemd/system/openvim.service ]] && [[ -z $option_screen_name ]] && service openvim $om_action && ( [[ $om_action == status ]] || sleep 5 ) && exit $?
+
#stop
if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
then
#terminates program
- [ -n "$component_id" ] && echo -n " stopping $om_name ... " && kill_pid $component_id
+ [ $processes -gt 1 ] && echo "$processes processes are running, specify with --screen-name" && continue
+ [ $processes -eq 1 ] && echo -n " stopping $om_name ... " && kill_pid $component_id
component_id=""
#terminates screen
- if screen -wipe | grep -Fq .$om_component
+ if screen -wipe | grep -q -e "\.${screen_name}\b"
then
- screen -S $om_component -p 0 -X stuff "exit\n"
+ screen -S $screen_name -p 0 -X stuff "exit\n" || echo
sleep 1
fi
fi
echo "FLOODLIGHT_PATH shell variable must indicate floodlight installation path" >&2 && exit -1
#calculates log file name
logfile=""
- mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/open${om_component}.log || echo "can not create logs directory $DIR_OM/logs"
+ mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/open${screen_name}.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
echo -n " starting $om_name ... "
- if ! screen -wipe | grep -Fq .${om_component}
+ if ! screen -wipe | grep -q -e "\.${screen_name}\b"
then
pushd ${om_dir} > /dev/null
- screen -dmS ${om_component} bash
+ screen -dmS ${screen_name} bash
sleep 1
popd > /dev/null
else
- echo -n " using existing screen '${om_component}' ... "
- screen -S ${om_component} -p 0 -X log off
- screen -S ${om_component} -p 0 -X stuff "cd ${om_dir}\n"
+ echo -n " using existing screen '${screen_name}' ... "
+ screen -S ${screen_name} -p 0 -X log off
+ screen -S ${screen_name} -p 0 -X stuff "cd ${om_dir}\n"
sleep 1
fi
#move old log file index one number up and log again in index 0
[[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$((index+1))
done
[[ -f ${logfile} ]] && mv ${logfile} ${logfile}.1
- screen -S ${om_component} -p 0 -X logfile ${logfile}
- screen -S ${om_component} -p 0 -X log on
+ screen -S ${screen_name} -p 0 -X logfile ${logfile}
+ screen -S ${screen_name} -p 0 -X log on
fi
#launch command to screen
- #[ "${om_component}" != "flow" ] && screen -S ${om_component} -p 0 -X stuff "cd ${DIR_OM}/open${om_component}\n" && sleep 1
- [ "${om_component}" == "flow" ] && screen -S flow -p 0 -X stuff "java -Dlogback.configurationFile=${DIRNAME}/flow-logback.xml -jar ./target/floodlight.jar -cf ${DIRNAME}/flow.properties_v0.9\n"
- #[ "${om_component}" == "flow" ] && screen -S flow -p 0 -X stuff "java -Dlogback.configurationFile=${DIRNAME}/flow-logback.xml -jar ./target/floodlight.jar -cf ${DIRNAME}/flow.properties_v1.1\n" && sleep 5
- [ "${om_component}" != "flow" ] && screen -S ${om_component} -p 0 -X stuff "./${om_cmd}\n"
+ #[ "${om_component}" != "flow" ] && screen -S ${screen_name} -p 0 -X stuff "cd ${DIR_OM}/open${om_component}\n" && sleep 1
+ [ "${om_component}" == "flow" ] && screen -S ${screen_name} -p 0 -X stuff "java -Dlogback.configurationFile=${DIRNAME}/flow-logback.xml -jar ./target/floodlight.jar -cf ${DIRNAME}/flow.properties_v0.9\n"
+ #[ "${om_component}" == "flow" ] && screen -S ${screen_name} -p 0 -X stuff "java -Dlogback.configurationFile=${DIRNAME}/flow-logback.xml -jar ./target/floodlight.jar -cf ${DIRNAME}/flow.properties_v1.1\n" && sleep 5
+ [ "${om_component}" != "flow" ] && screen -S ${screen_name} -p 0 -X stuff "${om_cmd}${om_params}\n"
#check if is running
[[ -n $logfile ]] && timeout=120 #2 minute
[[ -z $logfile ]] && timeout=20
#if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
log_lines=0
[[ -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}'`
+ component_id=`find_process_id "${om_cmd}${om_params}" $screen_name`
if [[ -z $component_id ]]
then #process not started or finished
[[ $log_lines -ge 2 ]] && echo -n "ERROR, it has exited." && break
then
echo -n "timeout!"
else
- echo -n "running on 'screen -x ${om_component}'."
+ echo -n "running on 'screen -x ${screen_name}'."
fi
[[ -n $logfile ]] && echo " Logging at '${logfile}'" || echo
fi