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