blob: cc6a8c7d87d7a696d4979bacc4e0a56cffa96fa0 [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# ================================================================================
Jorge Hernandez13e9aa82018-03-25 23:34:27 -05007# Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
Jorge Hernandez508ad582017-05-31 13:58:27 -05008# ================================================================================
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
Jorge Hernandez13e9aa82018-03-25 23:34:27 -050045 mkdir -p $_LOGS
46 if [ -e $_LOGS/$PNAME.out.1 ]; then mv $_LOGS/$PNAME.out.1 $_LOGS/$PNAME.out.2; fi
47 if [ -e $_LOGS/$PNAME.err.1 ]; then mv $_LOGS/$PNAME.err.1 $_LOGS/$PNAME.err.2; fi
48 if [ -e $_LOGS/$PNAME.out ]; then mv $_LOGS/$PNAME.out $_LOGS/$PNAME.out.1; fi
49 if [ -e $_LOGS/$PNAME.err ]; then mv $_LOGS/$PNAME.err $_LOGS/$PNAME.err.1; fi
Pamela Dragosh0e16acf2017-02-14 19:45:48 -050050 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
Jorge Hernandez13e9aa82018-03-25 23:34:27 -050069 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 > $_LOGS/$PNAME.out) 2> >( while read line; do echo "$(date): ${line}"; done > $_LOGS/$PNAME.err) &
Pamela Dragosh0e16acf2017-02-14 19:45:48 -050070
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}
Jorge Hernandez13e9aa82018-03-25 23:34:27 -0500170_LOGS=${POLICY_LOGS}
171
172if [[ -z ${POLICY_LOGS} ]]; then
173 _LOGS="${POLICY_HOME}"/logs
174fi
175
Pamela Dragosh0e16acf2017-02-14 19:45:48 -0500176CONTROLLER=policy-management-controller
177
178RETVAL=0
179
180_PIDFILE=${POLICY_HOME}/PID
181
182case "$1" in
183 status)
184 status
185 echo "$_STATUS"
186 ;;
187 start)
188 if flock ${cfg} ; then
189 start
190 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
191 ;;
192 umstart)
193 um_start
194 ;;
195 stop)
196 if flock ${cfg} ; then
197 stop
198 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
199 ;;
200 umstop)
201 um_stop
202 ;;
203 restart)
204 if flock ${cfg} ; then
205 stop
206 sleep 2
207 start
208 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
209 ;;
210 *)
211 echo "error: invalid option $@"
212 echo "Usage: $0 status|start|stop|restart"
213 RETVAL=1
214 ;;
215esac
216
217exit ${RETVAL}