blob: 4d28d941fb46fcd0714b219c69e4f6154e0355bf [file] [log] [blame]
Pamela Dragosh0e16acf2017-02-14 19:45:48 -05001#!/bin/bash
2
3SNAME="Policy Management"
4PNAME=policy-management
5CLASS=org.openecomp.policy.drools.system.Main
6
7
8function start() {
9 um_start
10 if [[ ${RETVAL} != 0 ]]; then
11 update_monitor off
12 else
13 update_monitor on
14 fi
15}
16
17# unmonitored start, does not change monitor status (immutable)
18function um_start() {
19 status
20 if [ "$_RUNNING" = "1" ]; then
21 echo $_STATUS
22 RETVAL=0
23 return
24 fi
25 mkdir -p $_DIR/logs
26 if [ -e $_DIR/logs/$PNAME.out.1 ]; then mv $_DIR/logs/$PNAME.out.1 $_DIR/logs/$PNAME.out.2; fi
27 if [ -e $_DIR/logs/$PNAME.err.1 ]; then mv $_DIR/logs/$PNAME.err.1 $_DIR/logs/$PNAME.err.2; fi
28 if [ -e $_DIR/logs/$PNAME.out ]; then mv $_DIR/logs/$PNAME.out $_DIR/logs/$PNAME.out.1; fi
29 if [ -e $_DIR/logs/$PNAME.err ]; then mv $_DIR/logs/$PNAME.err $_DIR/logs/$PNAME.err.1; fi
30 CP=$(ls $_DIR/lib/*.jar | xargs -I X printf ":%s" X)
31
32 # If 'system.properties' exists, convert it into JVM arguments.
33 # Note that the following also handles property values with spaces.
34 IFS=$'\n'
35 systemProperties=($(
36 if [[ -f $_DIR/config/system.properties ]] ; then
37 sed -n -e 's/^[ \t]*\([^ \t#]*\)[ \t]*=[ \t]*\(.*\)$/-D\1=\2/p' \
38 $_DIR/config/system.properties
39 fi
40 ))
41
42 cd $_DIR
43 (
44 if [[ "${cfg}" != "" ]] ; then
45 # need to make sure that we don't pass the lock file descriptor
46 # to subprocesses
47 exec {cfg}>&-
48 fi
49 nohup $JAVA_HOME/bin/java -Dkie.maven.settings.custom=$_DIR/config/kie_settings.xml -Dlog4j.configuration=file:$_DIR/config/log4j.properties -cp $_DIR/config:$_DIR/lib:$CP "${systemProperties[@]}" "$@" $CLASS > >( while read line; do echo "$(date): ${line}"; done > $_DIR/logs/$PNAME.out) 2> >( while read line; do echo "$(date): ${line}"; done > $_DIR/logs/$PNAME.err) &
50
51 _PID=$!
52 echo $_PID > $_PIDFILE
53 )
54 sleep 5
55 status
56 echo $_STATUS
57 if [ "$_RUNNING" = "1" ]; then
58 RETVAL=0
59 else
60 echo "Failed to start"
61 remove_pid_file
62 RETVAL=1
63 fi
64}
65
66function stop() {
67 um_stop
68 update_monitor off
69}
70
71# unmonitored stop, does not change monitor status (immutable)
72function um_stop() {
73 status
74 if [ "$_RUNNING" = "0" ]; then
75 echo $_STATUS
76 remove_pid_file
77 else
78 if [[ -n ${ENGINE_MANAGEMENT_PASSWORD} ]]; then
79 http_proxy= curl --silent --user ${ENGINE_MANAGEMENT_USER}:${ENGINE_MANAGEMENT_PASSWORD} -X DELETE http://localhost:${ENGINE_MANAGEMENT_PORT}/policy/pdp/engine -o /dev/null
80 else
81 http_proxy= curl --silent -X DELETE http://localhost:${ENGINE_MANAGEMENT_PORT}/policy/pdp/engine -o /dev/null
82 fi
83 sleep 5
84 echo "Stopping $SNAME..."
85 _PID_TO_KILL=$_PID;
86 echo "$SNAME (pid=${_PID_TO_KILL}) is stopping..."
87 kill -TERM $_PID_TO_KILL 2> /dev/null
88 sleep 5
89 check_status_of_pid $_PID_TO_KILL
90 if [ "$_RUNNING" = "1" ]; then
91 kill -TERM $_PID_TO_KILL
92 fi
93 while [ "$_RUNNING" = "1" ]; do
94 sleep 2
95 check_status_of_pid $_PID_TO_KILL
96 done
97 remove_pid_file
98 echo "$SNAME has stopped."
99 fi
100 RETVAL=0
101}
102
103function status() {
104 if [ -f "${_PIDFILE}" ]; then
105 _PID=`cat "${_PIDFILE}"`
106 check_status_of_pid $_PID
107 else
108 _STATUS="$SNAME (no pidfile) is NOT running"
109 _RUNNING=0
110 fi
111 if [[ $_RUNNING = 1 ]]; then
112 RETVAL=0
113 else
114 RETVAL=1
115 fi
116}
117
118
119function check_status_of_pid ()
120{
121 if [ -n "$1" ] && kill -0 $1 2>/dev/null ; then
122 _STATUS="$SNAME (pid $1) is running"
123 _RUNNING=1
124 else
125 _STATUS="$SNAME (pid $1) is NOT running"
126 _RUNNING=0
127 fi
128}
129
130function remove_pid_file ()
131{
132 if [ -f "${_PIDFILE}" ]; then
133 rm "${_PIDFILE}"
134 fi
135}
136
137function update_monitor() {
138 STATUS=$1
139 if [[ -f ${POLICY_HOME}/etc/monitor/monitor.cfg ]]; then
140 /bin/sed -i.bak \
141 -e "s/^${CONTROLLER}=.*/${CONTROLLER}=${STATUS}/g" \
142 ${POLICY_HOME}/etc/monitor/monitor.cfg
143 fi
144}
145
146
147# main
148
149_DIR=${POLICY_HOME}
150CONTROLLER=policy-management-controller
151
152RETVAL=0
153
154_PIDFILE=${POLICY_HOME}/PID
155
156case "$1" in
157 status)
158 status
159 echo "$_STATUS"
160 ;;
161 start)
162 if flock ${cfg} ; then
163 start
164 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
165 ;;
166 umstart)
167 um_start
168 ;;
169 stop)
170 if flock ${cfg} ; then
171 stop
172 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
173 ;;
174 umstop)
175 um_stop
176 ;;
177 restart)
178 if flock ${cfg} ; then
179 stop
180 sleep 2
181 start
182 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
183 ;;
184 *)
185 echo "error: invalid option $@"
186 echo "Usage: $0 status|start|stop|restart"
187 RETVAL=1
188 ;;
189esac
190
191exit ${RETVAL}