Initial openvim v0.4.6 upload
[osm/openvim.git] / scripts / service-openvim.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
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
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
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
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
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
27
28
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
34
35 function usage(){
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"
39 }
40
41 function kill_pid(){
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
45 WAIT=5
46 while [ $WAIT -gt 0 ] && ps -o pid -U $USER -u $USER | grep -q $1
47 do
48 sleep 1
49 WAIT=$((WAIT-1))
50 [ $WAIT -eq 0 ] && echo -n "sending SIGKILL... " && kill -9 $1 #kill when count reach 0
51 done
52 echo "done"
53
54 }
55
56 #obtain parameters
57 om_list=""
58 #om_action="start" #uncoment to get a default action
59 for param in $*
60 do
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
67
68 #if none of above, reach this line because a param is incorrect
69 echo "Unknown param '$param' type $0 --help" >&2
70 exit -1
71 done
72
73 #check action is provided
74 [ -z "$om_action" ] && usage >&2 && exit -1
75
76 #if no componenets supplied assume all
77 [ -z "$om_list" ] && om_list="vim"
78
79 for om_component in $om_list
80 do
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}'`
85
86 #status
87 if [ "$om_action" == "status" ]
88 then
89 [ -n "$component_id" ] && echo " $om_name running, pid $component_id"
90 [ -z "$component_id" ] && echo " $om_name stopped"
91 fi
92
93 #stop
94 if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
95 then
96 #terminates program
97 [ -n "$component_id" ] && echo -n " stopping $om_name ... " && kill_pid $component_id
98 component_id=""
99 #terminates screen
100 if screen -wipe | grep -Fq .$om_component
101 then
102 screen -S $om_component -p 0 -X stuff "exit\n"
103 sleep 1
104 fi
105 fi
106
107 #start
108 if [ "$om_action" == "start" -o "$om_action" == "restart" ]
109 then
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
113 logfile=""
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}
120 then
121 pushd ${om_dir} > /dev/null
122 screen -dmS ${om_component} bash
123 sleep 1
124 popd > /dev/null
125 else
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"
129 sleep 1
130 fi
131 #move old log file index one number up and log again in index 0
132 if [[ -n $logfile ]]
133 then
134 for index in 8 7 6 5 4 3 2 1
135 do
136 [[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$((index+1))
137 done
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
141 fi
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"
147 #check if is running
148 [[ -n $logfile ]] && timeout=120 #2 minute
149 [[ -z $logfile ]] && timeout=20
150 while [[ $timeout -gt 0 ]]
151 do
152 #check if is running
153 #echo timeout $timeout
154 #if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
155 log_lines=0
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
162 fi
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
165 sleep 1
166 timeout=$((timeout -1))
167 done
168 if [[ -n $logfile ]] && [[ $timeout == 0 ]]
169 then
170 echo -n "timeout!"
171 else
172 echo -n "running on 'screen -x ${om_component}'."
173 fi
174 [[ -n $logfile ]] && echo " Logging at '${logfile}'" || echo
175 fi
176 done
177
178
179
180