7eefa8e1e541047a2afbbd1fc7f054f922f1b481
[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 openvim
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 (and/or floodlight) inside a screen.
25 #or call service if it is installed on systemd
26 #It assumes a relative path '..' for openvim
27 #for floodlight, the variable FLOODLIGHT_PATH indicates the installation path
28
29
30 DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
31 DIRNAME=$(dirname $DIRNAME )
32 DIR_OM=$(dirname $DIRNAME )
33 #[[ -z $FLOODLIGHT_PATH ]] && FLOODLIGHT_PATH=$(dirname ${DIR_OM})/floodlight-1.1
34 #[[ -z $FLOODLIGHT_PATH ]] && FLOODLIGHT_PATH=$(dirname ${DIR_OM})/floodlight-0.90
35
36 function usage(){
37 echo -e "Usage: $0 [openvim/vim] [floodlight/flow] start|stop|restart|status"
38 echo -e " Launch|Removes|Restart|Getstatus openvim (by default) or/and floodlight on a screen/service"
39 echo -e " For floodlight variable FLOODLIGHT_PATH must indicate installation path"
40 echo -e " -h --help: shows this help"
41 echo -e " -n --screen-name NAME : name of screen to launch openvim (default vim)"
42 echo -e " -- PARAMS use to separate PARAMS that will be send to the service. e.g. -pPORT -PADMINPORT --dbname=DDBB"
43 }
44
45 function kill_pid(){
46 #send TERM signal and wait 5 seconds and send KILL signal ir still running
47 #PARAMS: $1: PID of process to terminate
48 kill $1 #send TERM signal
49 WAIT=5
50 while [ $WAIT -gt 0 ] && ps -o pid -U $USER -u $USER | grep -q $1
51 do
52 sleep 1
53 WAIT=$((WAIT-1))
54 [ $WAIT -eq 0 ] && echo -n "sending SIGKILL... " && kill -9 $1 #kill when count reach 0
55 done
56 echo "done"
57
58 }
59
60 #process options
61 source ${DIRNAME}/get-options.sh "screen-name:n= help:h --" $* || exit 1
62
63 #help
64 [[ -n "$option_help" ]] && usage && exit 0
65
66
67 #obtain parameters
68 om_list=""
69 #om_action="start" #uncoment to get a default action
70 action_list=""
71 om_params="$option__"
72
73 for param in $params
74 do
75 [ "$param" == "start" -o "$param" == "stop" -o "$param" == "restart" -o "$param" == "status" ] && om_action=$param && continue
76 [ "$param" == "openvim" -o "$param" == "vim" ] && om_list="$om_list vim" && continue
77 [ "$param" == "openmano" -o "$param" == "mano" ] && continue #allow and ingore for backwards compatibility
78 [ "$param" == "openflow" -o "$param" == "flow" -o "$param" == "floodlight" ] && om_list="flow $om_list" && continue
79 echo "invalid argument '$param'? Type -h for help" >&2 && exit 1
80 done
81
82 [[ -n $option_screen_name ]] && option_screen_name=${option_screen_name#*.} #allow the format 'pid.name' and keep only name
83
84 #check action is provided
85 [ -z "$om_action" ] && usage >&2 && exit -1
86
87 #if no componenets supplied assume all
88 [ -z "$om_list" ] && om_list="vim"
89
90 function find_process_id(){ #PARAMS: command screen-name
91 for process_id in `ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep "${1}" | awk '{print $1}'`
92 do
93 scname=$(ps wwep $process_id | grep -o 'STY=\S*')
94 scname=${scname#STY=}
95 [[ -n "$2" ]] && [[ "${scname#*.}" != "$2" ]] && continue
96 echo -n "${process_id} "
97 done
98 echo
99 }
100
101
102 for om_component in $om_list
103 do
104 screen_name="${om_component}"
105 [[ -n "$option_screen_name" ]] && screen_name=$option_screen_name
106 [ "${om_component}" == "flow" ] && om_cmd="floodlight.jar" && om_name="floodlight" && om_dir=$FLOODLIGHT_PATH
107 [ "${om_component}" == "vim" ] && om_cmd="./openvimd.py" && om_name="openvim " && om_dir=${DIR_OM}
108 #obtain PID of program
109 component_id=`find_process_id "${om_cmd}" $option_screen_name`
110 processes=$(echo $component_id | wc -w)
111
112 #status
113 if [ "$om_action" == "status" ]
114 then
115 running=""
116 for process_id in $component_id
117 do
118 scname=$(ps wwep $process_id | grep -o 'STY=\S*')
119 scname=${scname#STY=}
120 [[ -n "$option_screen_name" ]] && [[ "${scname#*.}" != "$option_screen_name" ]] && continue
121 printf "%-15s" "pid: ${process_id},"
122 [[ -n "$scname" ]] && printf "%-25s" "screen: ${scname},"
123 echo cmd: $(ps -o cmd p $process_id | tail -n1 )
124 running=y
125 done
126 #if installed as a service and it is not provided a screen name call service
127 [[ -f /etc/systemd/system/openvim.service ]] && [[ -z $option_screen_name ]] && running=y #&& service openvim status
128 if [ -z "$running" ]
129 then
130 echo -n " $om_name not running" && [[ -n "$option_screen_name" ]] && echo " on screen '$option_screen_name'" || echo
131 fi
132 fi
133
134 #if installed as a service and it is not provided a screen name call service
135 [[ -f /etc/systemd/system/openvim.service ]] && [[ -z $option_screen_name ]] && service openvim $om_action && ( [[ $om_action == status ]] || sleep 5 ) && exit $?
136
137 #stop
138 if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
139 then
140 #terminates program
141 [ $processes -gt 1 ] && echo "$processes processes are running, specify with --screen-name" && continue
142 [ $processes -eq 1 ] && echo -n " stopping $om_name ... " && kill_pid $component_id
143 component_id=""
144 #terminates screen
145 if screen -wipe | grep -q -e "\.${screen_name}\b"
146 then
147 screen -S $screen_name -p 0 -X stuff "exit\n" || echo
148 sleep 1
149 fi
150 fi
151
152 #start
153 if [ "$om_action" == "start" -o "$om_action" == "restart" ]
154 then
155 [[ -z $FLOODLIGHT_PATH ]] && [[ $om_component == flow ]] &&
156 echo "FLOODLIGHT_PATH shell variable must indicate floodlight installation path" >&2 && exit -1
157 #calculates log file name
158 logfile=""
159 mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/open${screen_name}.log || echo "can not create logs directory $DIR_OM/logs"
160 #check already running
161 [ -n "$component_id" ] && echo " $om_name is already running. Skipping" && continue
162 #create screen if not created
163 echo -n " starting $om_name ... "
164 if ! screen -wipe | grep -q -e "\.${screen_name}\b"
165 then
166 pushd ${om_dir} > /dev/null
167 screen -dmS ${screen_name} bash
168 sleep 1
169 popd > /dev/null
170 else
171 echo -n " using existing screen '${screen_name}' ... "
172 screen -S ${screen_name} -p 0 -X log off
173 screen -S ${screen_name} -p 0 -X stuff "cd ${om_dir}\n"
174 sleep 1
175 fi
176 #move old log file index one number up and log again in index 0
177 if [[ -n $logfile ]]
178 then
179 for index in 8 7 6 5 4 3 2 1
180 do
181 [[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$((index+1))
182 done
183 [[ -f ${logfile} ]] && mv ${logfile} ${logfile}.1
184 screen -S ${screen_name} -p 0 -X logfile ${logfile}
185 screen -S ${screen_name} -p 0 -X log on
186 fi
187 #launch command to screen
188 #[ "${om_component}" != "flow" ] && screen -S ${screen_name} -p 0 -X stuff "cd ${DIR_OM}/open${om_component}\n" && sleep 1
189 [ "${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"
190 #[ "${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
191 [ "${om_component}" != "flow" ] && screen -S ${screen_name} -p 0 -X stuff "${om_cmd}${om_params}\n"
192 #check if is running
193 [[ -n $logfile ]] && timeout=120 #2 minute
194 [[ -z $logfile ]] && timeout=20
195 while [[ $timeout -gt 0 ]]
196 do
197 #check if is running
198 #echo timeout $timeout
199 #if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
200 log_lines=0
201 [[ -n $logfile ]] && log_lines=`head ${logfile} | wc -l`
202 component_id=`find_process_id "${om_cmd}${om_params}" $screen_name`
203 if [[ -z $component_id ]]
204 then #process not started or finished
205 [[ $log_lines -ge 2 ]] && echo -n "ERROR, it has exited." && break
206 #started because writted serveral lines at log so report error
207 fi
208 [[ -n $logfile ]] && [[ ${om_component} == flow ]] && grep -q "Listening for switch connections" ${logfile} && sleep 1 && break
209 [[ -n $logfile ]] && [[ ${om_component} != flow ]] && grep -q "open${om_component}d ready" ${logfile} && break
210 sleep 1
211 timeout=$((timeout -1))
212 done
213 if [[ -n $logfile ]] && [[ $timeout == 0 ]]
214 then
215 echo -n "timeout!"
216 else
217 echo -n "running on 'screen -x ${screen_name}'."
218 fi
219 [[ -n $logfile ]] && echo " Logging at '${logfile}'" || echo
220 fi
221 done
222
223
224
225