blob: a9f8ce0d15620790e5d3f45ba9777ffe739f57f6 [file] [log] [blame]
Pamela Dragosh0e16acf2017-02-14 19:45:48 -05001#!/bin/bash
2
Jorge Hernandez508ad582017-05-31 13:58:27 -05003###
4# ============LICENSE_START=======================================================
5# ONAP POLICY
6# ================================================================================
7# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
8# ================================================================================
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20# ============LICENSE_END=========================================================
21##
22
Pamela Dragosh0e16acf2017-02-14 19:45:48 -050023SNAME="Policy Management"
24PNAME=policy-management
Guo Ruijing6abeb292017-07-28 08:23:01 +000025CLASS=org.onap.policy.drools.system.Main
Pamela Dragosh0e16acf2017-02-14 19:45:48 -050026
27
28function start() {
29 um_start
30 if [[ ${RETVAL} != 0 ]]; then
31 update_monitor off
32 else
33 update_monitor on
34 fi
35}
36
37# unmonitored start, does not change monitor status (immutable)
38function um_start() {
39 status
40 if [ "$_RUNNING" = "1" ]; then
41 echo $_STATUS
42 RETVAL=0
43 return
44 fi
45 mkdir -p $_DIR/logs
46 if [ -e $_DIR/logs/$PNAME.out.1 ]; then mv $_DIR/logs/$PNAME.out.1 $_DIR/logs/$PNAME.out.2; fi
47 if [ -e $_DIR/logs/$PNAME.err.1 ]; then mv $_DIR/logs/$PNAME.err.1 $_DIR/logs/$PNAME.err.2; fi
48 if [ -e $_DIR/logs/$PNAME.out ]; then mv $_DIR/logs/$PNAME.out $_DIR/logs/$PNAME.out.1; fi
49 if [ -e $_DIR/logs/$PNAME.err ]; then mv $_DIR/logs/$PNAME.err $_DIR/logs/$PNAME.err.1; fi
50 CP=$(ls $_DIR/lib/*.jar | xargs -I X printf ":%s" X)
51
52 # If 'system.properties' exists, convert it into JVM arguments.
53 # Note that the following also handles property values with spaces.
54 IFS=$'\n'
55 systemProperties=($(
56 if [[ -f $_DIR/config/system.properties ]] ; then
57 sed -n -e 's/^[ \t]*\([^ \t#]*\)[ \t]*=[ \t]*\(.*\)$/-D\1=\2/p' \
58 $_DIR/config/system.properties
59 fi
60 ))
61
62 cd $_DIR
63 (
64 if [[ "${cfg}" != "" ]] ; then
65 # need to make sure that we don't pass the lock file descriptor
66 # to subprocesses
67 exec {cfg}>&-
68 fi
69 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) &
70
71 _PID=$!
72 echo $_PID > $_PIDFILE
73 )
74 sleep 5
75 status
76 echo $_STATUS
77 if [ "$_RUNNING" = "1" ]; then
78 RETVAL=0
79 else
80 echo "Failed to start"
81 remove_pid_file
82 RETVAL=1
83 fi
84}
85
86function stop() {
87 um_stop
88 update_monitor off
89}
90
91# unmonitored stop, does not change monitor status (immutable)
92function um_stop() {
93 status
94 if [ "$_RUNNING" = "0" ]; then
95 echo $_STATUS
96 remove_pid_file
97 else
98 if [[ -n ${ENGINE_MANAGEMENT_PASSWORD} ]]; then
99 http_proxy= curl --silent --user ${ENGINE_MANAGEMENT_USER}:${ENGINE_MANAGEMENT_PASSWORD} -X DELETE http://localhost:${ENGINE_MANAGEMENT_PORT}/policy/pdp/engine -o /dev/null
100 else
101 http_proxy= curl --silent -X DELETE http://localhost:${ENGINE_MANAGEMENT_PORT}/policy/pdp/engine -o /dev/null
102 fi
103 sleep 5
104 echo "Stopping $SNAME..."
105 _PID_TO_KILL=$_PID;
106 echo "$SNAME (pid=${_PID_TO_KILL}) is stopping..."
107 kill -TERM $_PID_TO_KILL 2> /dev/null
108 sleep 5
109 check_status_of_pid $_PID_TO_KILL
110 if [ "$_RUNNING" = "1" ]; then
111 kill -TERM $_PID_TO_KILL
112 fi
113 while [ "$_RUNNING" = "1" ]; do
114 sleep 2
115 check_status_of_pid $_PID_TO_KILL
116 done
117 remove_pid_file
118 echo "$SNAME has stopped."
119 fi
120 RETVAL=0
121}
122
123function status() {
124 if [ -f "${_PIDFILE}" ]; then
125 _PID=`cat "${_PIDFILE}"`
126 check_status_of_pid $_PID
127 else
128 _STATUS="$SNAME (no pidfile) is NOT running"
129 _RUNNING=0
130 fi
131 if [[ $_RUNNING = 1 ]]; then
132 RETVAL=0
133 else
134 RETVAL=1
135 fi
136}
137
138
139function check_status_of_pid ()
140{
141 if [ -n "$1" ] && kill -0 $1 2>/dev/null ; then
142 _STATUS="$SNAME (pid $1) is running"
143 _RUNNING=1
144 else
145 _STATUS="$SNAME (pid $1) is NOT running"
146 _RUNNING=0
147 fi
148}
149
150function remove_pid_file ()
151{
152 if [ -f "${_PIDFILE}" ]; then
153 rm "${_PIDFILE}"
154 fi
155}
156
157function update_monitor() {
158 STATUS=$1
159 if [[ -f ${POLICY_HOME}/etc/monitor/monitor.cfg ]]; then
160 /bin/sed -i.bak \
161 -e "s/^${CONTROLLER}=.*/${CONTROLLER}=${STATUS}/g" \
162 ${POLICY_HOME}/etc/monitor/monitor.cfg
163 fi
164}
165
166
167# main
168
169_DIR=${POLICY_HOME}
170CONTROLLER=policy-management-controller
171
172RETVAL=0
173
174_PIDFILE=${POLICY_HOME}/PID
175
176case "$1" in
177 status)
178 status
179 echo "$_STATUS"
180 ;;
181 start)
182 if flock ${cfg} ; then
183 start
184 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
185 ;;
186 umstart)
187 um_start
188 ;;
189 stop)
190 if flock ${cfg} ; then
191 stop
192 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
193 ;;
194 umstop)
195 um_stop
196 ;;
197 restart)
198 if flock ${cfg} ; then
199 stop
200 sleep 2
201 start
202 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
203 ;;
204 *)
205 echo "error: invalid option $@"
206 echo "Usage: $0 status|start|stop|restart"
207 RETVAL=1
208 ;;
209esac
210
211exit ${RETVAL}