898bba62e461a38da79b04081880a2c159a64398
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
24 #launch openvim (andr floodlight) inside a screen.
25 #It assumes a relative path '..' for openvim
26 #for floodlight, the variable FLOODLIGHT_PATH indicates the installation path
29 DIRNAME
=$
(readlink
-f ${BASH_SOURCE[0]})
30 DIRNAME
=$
(dirname $DIRNAME )
31 DIR_OM
=$
(dirname $DIRNAME )
32 #[[ -z $FLOODLIGHT_PATH ]] && FLOODLIGHT_PATH=$(dirname ${DIR_OM})/floodlight-1.1
33 #[[ -z $FLOODLIGHT_PATH ]] && FLOODLIGHT_PATH=$(dirname ${DIR_OM})/floodlight-0.90
36 echo -e "Usage: $0 [openvim/vim] [floodlight/flow] start|stop|restart|status"
37 echo -e " Launch|Removes|Restart|Getstatus openvim (by default) or/and floodlight on a screen"
38 echo -e " For floodlight variable FLOODLIGHT_PATH must indicate installation path"
42 #send TERM signal and wait 5 seconds and send KILL signal ir still running
43 #PARAMS: $1: PID of process to terminate
44 kill $1 #send TERM signal
46 while [ $WAIT -gt 0 ] && ps
-o pid
-U $USER -u $USER |
grep -q $1
50 [ $WAIT -eq 0 ] && echo -n "sending SIGKILL... " && kill -9 $1 #kill when count reach 0
58 #om_action="start" #uncoment to get a default action
61 [ "$param" == "start" -o "$param" == "stop" -o "$param" == "restart" -o "$param" == "status" ] && om_action
=$param && continue
62 [ "$param" == "openvim" -o "$param" == "vim" ] && om_list
="$om_list vim" && continue
63 [ "$param" == "openmano" -o "$param" == "mano" ] && continue #allow and ingore for backwards compatibility
64 [ "$param" == "openflow" -o "$param" == "flow" -o "$param" == "floodlight" ] && om_list
="flow $om_list" && continue
65 [ "$param" == "-h" -o "$param" == "--help" ] && usage
&& exit 0
66 #note flow that it must be the first element, because openvim relay on this
68 #if none of above, reach this line because a param is incorrect
69 echo "Unknown param '$param' type $0 --help" >&2
73 #check action is provided
74 [ -z "$om_action" ] && usage
>&2 && exit -1
76 #if no componenets supplied assume all
77 [ -z "$om_list" ] && om_list
="vim"
79 for om_component
in $om_list
81 [ "${om_component}" == "flow" ] && om_cmd
="floodlight.jar" && om_name
="floodlight" && om_dir
=$FLOODLIGHT_PATH
82 [ "${om_component}" == "vim" ] && om_cmd
="openvimd.py" && om_name
="openvim " && om_dir
=${DIR_OM}
83 #obtain PID of program
84 component_id
=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
87 if [ "$om_action" == "status" ]
89 [ -n "$component_id" ] && echo " $om_name running, pid $component_id"
90 [ -z "$component_id" ] && echo " $om_name stopped"
94 if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
97 [ -n "$component_id" ] && echo -n " stopping $om_name ... " && kill_pid
$component_id
100 if screen
-wipe |
grep -Fq .
$om_component
102 screen
-S $om_component -p 0 -X stuff
"exit\n"
108 if [ "$om_action" == "start" -o "$om_action" == "restart" ]
110 [[ -z $FLOODLIGHT_PATH ]] && [[ $om_component == flow
]] &&
111 echo "FLOODLIGHT_PATH shell variable must indicate floodlight installation path" >&2 && exit -1
112 #calculates log file name
114 mkdir
-p $DIR_OM/logs
&& logfile
=$DIR_OM/logs
/open
${om_component}.log ||
echo "can not create logs directory $DIR_OM/logs"
115 #check already running
116 [ -n "$component_id" ] && echo " $om_name is already running. Skipping" && continue
117 #create screen if not created
118 echo -n " starting $om_name ... "
119 if ! screen
-wipe |
grep -Fq .
${om_component}
121 pushd ${om_dir} > /dev
/null
122 screen
-dmS ${om_component} bash
126 echo -n " using existing screen '${om_component}' ... "
127 screen
-S ${om_component} -p 0 -X log off
128 screen
-S ${om_component} -p 0 -X stuff
"cd ${om_dir}\n"
131 #move old log file index one number up and log again in index 0
134 for index
in 8 7 6 5 4 3 2 1
136 [[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$
((index
+1))
138 [[ -f ${logfile} ]] && mv ${logfile} ${logfile}.1
139 screen
-S ${om_component} -p 0 -X logfile
${logfile}
140 screen
-S ${om_component} -p 0 -X log on
142 #launch command to screen
143 #[ "${om_component}" != "flow" ] && screen -S ${om_component} -p 0 -X stuff "cd ${DIR_OM}/open${om_component}\n" && sleep 1
144 [ "${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"
145 #[ "${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
146 [ "${om_component}" != "flow" ] && screen -S ${om_component} -p 0 -X stuff "./${om_cmd}\n"
148 [[ -n $logfile ]] && timeout
=120 #2 minute
149 [[ -z $logfile ]] && timeout
=20
150 while [[ $timeout -gt 0 ]]
153 #echo timeout $timeout
154 #if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
156 [[ -n $logfile ]] && log_lines
=`head ${logfile} | wc -l`
157 component_id
=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
158 if [[ -z $component_id ]]
159 then #process not started or finished
160 [[ $log_lines -ge 2 ]] && echo -n "ERROR, it has exited." && break
161 #started because writted serveral lines at log so report error
163 [[ -n $logfile ]] && [[ ${om_component} == flow
]] && grep -q "Listening for switch connections" ${logfile} && sleep 1 && break
164 [[ -n $logfile ]] && [[ ${om_component} != flow ]] && grep -q "open${om_component}d ready" ${logfile} && break
166 timeout
=$
((timeout
-1))
168 if [[ -n $logfile ]] && [[ $timeout == 0 ]]
172 echo -n "running on 'screen -x ${om_component}'."
174 [[ -n $logfile ]] && echo " Logging at '${logfile}'" ||
echo