144bba9dbf8400be0b54314d9af9a0e690be0eaa
[osm/openvim.git] / scripts / service-opendaylight.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 opendaylight inside a screen. It assumes shell variable $OPENDAYLIGHT_PATH
25 # contain the installation path
26
27
28 DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
29 DIRNAME=$(dirname $DIRNAME )
30 DIR_OM=$(dirname $DIRNAME )
31
32 function usage(){
33 echo -e "Usage: $0 start|stop|restart|status"
34 echo -e " Launch|Removes|Restart|Getstatus opendaylight on a screen"
35 echo -e " Shell variable OPENDAYDLIGHT_PATH must indicate opendaylight installation path"
36 }
37
38 function kill_pid(){
39 #send TERM signal and wait 5 seconds and send KILL signal ir still running
40 #PARAMS: $1: PID of process to terminate
41 kill $1 #send TERM signal
42 WAIT=5
43 while [ $WAIT -gt 0 ] && ps -o pid -U $USER -u $USER | grep -q $1
44 do
45 sleep 1
46 WAIT=$((WAIT-1))
47 [ $WAIT -eq 0 ] && echo -n "sending SIGKILL... " && kill -9 $1 #kill when count reach 0
48 done
49 echo "done"
50
51 }
52
53 #obtain parameters
54 #om_action="start" #uncoment to get a default action
55 for param in $*
56 do
57 [ "$param" == "start" -o "$param" == "stop" -o "$param" == "restart" -o "$param" == "status" ] && om_action=$param && continue
58 [ "$param" == "openflow" -o "$param" == "flow" -o "$param" == "opendaylight" ] && continue
59 [ "$param" == "-h" -o "$param" == "--help" ] && usage && exit 0
60
61 #if none of above, reach this line because a param is incorrect
62 echo "Unknown param '$param' type $0 --help" >&2
63 exit -1
64 done
65
66 #check action is provided
67 [ -z "$om_action" ] && usage >&2 && exit -1
68
69 om_cmd="./karaf"
70 om_name="opendaylight"
71
72 #obtain PID of program
73 component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
74
75 #status
76 if [ "$om_action" == "status" ]
77 then
78 [ -n "$component_id" ] && echo " $om_name running, pid $component_id"
79 [ -z "$component_id" ] && echo " $om_name stopped"
80 fi
81
82 #stop
83 if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
84 then
85 #terminates program
86 [ -n "$component_id" ] && echo -n " stopping $om_name ... " && kill_pid $component_id
87 component_id=""
88 #terminates screen
89 if screen -wipe | grep -Fq .flow
90 then
91 screen -S flow -p 0 -X stuff "exit\n"
92 sleep 1
93 fi
94 fi
95
96 #start
97 if [ "$om_action" == "start" -o "$om_action" == "restart" ]
98 then
99 [[ -z $OPENDAYDLIGHT_PATH ]] && echo "OPENDAYDLIGHT_PATH shell variable must indicate opendaylight installation path" >&2 && exit -1
100 #calculates log file name
101 logfile=""
102 mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/openflow.log && logfile_console=$DIR_OM/logs/openflow_console.log || echo "can not create logs directory $DIR_OM/logs"
103 #check already running
104 [ -n "$component_id" ] && echo " $om_name is already running. Skipping" && continue
105 #create screen if not created
106 echo -n " starting $om_name ... "
107 if ! screen -wipe | grep -Fq .flow
108 then
109 pushd ${OPENDAYDLIGHT_PATH}/bin > /dev/null
110 screen -dmS flow bash
111 sleep 1
112 popd > /dev/null
113 else
114 echo -n " using existing screen 'flow' ... "
115 screen -S flow -p 0 -X log off
116 screen -S flow -p 0 -X stuff "cd ${OPENDAYDLIGHT_PATH}/bin\n"
117 sleep 1
118 fi
119 #move old log file index one number up and log again in index 0
120 if [[ -n $logfile ]]
121 then
122 for index in .9 .8 .7 .6 .5 .4 .3 .2 .1 ""
123 do
124 rm -f ${logfile}${index}
125 ln -s ${OPENDAYDLIGHT_PATH}/data/log/karaf.log${index} ${logfile}${index}
126 done
127 rm -rf ${logfile_console}
128 screen -S flow -p 0 -X logfile ${logfile_console}
129 screen -S flow -p 0 -X log on
130 fi
131 #launch command to screen
132 screen -S flow -p 0 -X stuff "${om_cmd}\n"
133 #check if is running
134 [[ -n $logfile ]] && timeout=120 #2 minute
135 [[ -z $logfile ]] && timeout=20
136 while [[ $timeout -gt 0 ]]
137 do
138 #check if is running
139 #echo timeout $timeout
140 #if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
141 log_lines=0
142 [[ -n $logfile_console ]] && log_lines=`head ${logfile_console} | wc -l`
143 component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
144 if [[ -z $component_id ]]
145 then #process not started or finished
146 [[ $log_lines -ge 2 ]] && echo -n "ERROR, it has exited." && break
147 #started because writted serveral lines at log so report error
148 fi
149 [[ -n $logfile_console ]] && grep -q "Listening on port" ${logfile_console} && sleep 1 && break
150 sleep 1
151 timeout=$((timeout -1))
152 done
153 if [[ -n $logfile_console ]] && [[ $timeout == 0 ]]
154 then
155 echo -n "timeout!"
156 else
157 echo -n "running on 'screen -x flow'."
158 fi
159 [[ -n $logfile ]] && echo " Logging at '${logfile}'" || echo
160 fi
161
162
163
164