4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
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
25 #launch openmano inside a screen.
26 #or call service if it is installed on systemd
29 DIRNAME
=$
(readlink
-f ${BASH_SOURCE[0]})
30 DIRNAME
=$
(dirname $DIRNAME )
31 DIR_OM
=$
(dirname $DIRNAME )
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"
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
47 while [ $WAIT -gt 0 ] && ps
-o pid
-U $USER -u $USER |
grep -q $1
51 [ $WAIT -eq 0 ] && echo -n "sending SIGKILL... " && kill -9 $1 #kill when count reach 0
57 source ${DIRNAME}/get-options.sh
"screen-name:n= help:h --" $
* ||
exit 1
60 [ -n "$option_help" ] && usage
&& exit 0
65 #om_action="start" #uncoment to get a default action
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
74 echo "invalid argument '$param'? Type -h for help" >&2 && exit 1
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
81 #if no componenets supplied assume all
82 [ -z "$om_list" ] && om_list
="mano"
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}'`
87 scname
=$
(ps wwep
$process_id |
grep -o 'STY=\S*')
89 [[ -n "$2" ]] && [[ "${scname#*.}" != "$2" ]] && continue
90 echo -n "${process_id} "
95 for om_component
in $om_list
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)
105 if [ "$om_action" == "status" ]
108 for process_id
in $component_id
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 )
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
122 echo -n " $om_name not running" && [[ -n "$option_screen_name" ]] && echo " on screen '$option_screen_name'" ||
echo
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 $?
131 if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
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
138 if screen
-wipe |
grep -q -e "\.${screen_name}\b"
140 screen
-S $screen_name -p 0 -X stuff
"exit\n" ||
echo
146 if [ "$om_action" == "start" -o "$om_action" == "restart" ]
148 #calculates log file name
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"
157 pushd ${om_dir} > /dev
/null
158 screen
-dmS ${screen_name} bash
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"
167 #move old log file index one number up and log again in index 0
170 for index
in 8 7 6 5 4 3 2 1
172 [[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$
((index
+1))
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
178 #launch command to screen
179 screen
-S ${screen_name} -p 0 -X stuff "${om_cmd}${om_params}\n"
181 [[ -n $logfile ]] && timeout=120 #2 minute
182 [[ -z $logfile ]] && timeout=20
183 while [[ $timeout -gt 0 ]]
186 #echo timeout $timeout
187 #if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
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
196 [[ -n $logfile ]] && grep -q "open
${om_component}d ready
" ${logfile} && break
198 timeout=$((timeout -1))
200 if [[ -n $logfile ]] && [[ $timeout == 0 ]]
204 echo -n "running on
'screen -x ${screen_name}'.
"
206 [[ -n $logfile ]] && echo " Logging
at '${logfile}'" || echo