blob: 79fab581ec689cca119626b099a10f9e7aa1c66e [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 Hernandezc6f2da82019-02-13 13:12:37 -06007# Copyright (C) 2017-2019 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
Jorge Hernandezc6f2da82019-02-13 13:12:37 -060052 # pick up any new changes in the environment every time we start
53 source ${POLICY_HOME}/etc/profile.d/env.sh
54
55 ${POLICY_HOME}/bin/configure-maven
Jorge Hernandezb001c1a2019-02-28 10:10:49 -060056 JVM_OPTS=(${JVM_OPTIONS})
Jorge Hernandezc6f2da82019-02-13 13:12:37 -060057
Jorge Hernandezb001c1a2019-02-28 10:10:49 -060058 # If 'system.properties' exists, convert it into "-D" JVM arguments.
Pamela Dragosh0e16acf2017-02-14 19:45:48 -050059 # Note that the following also handles property values with spaces.
60 IFS=$'\n'
61 systemProperties=($(
62 if [[ -f $_DIR/config/system.properties ]] ; then
63 sed -n -e 's/^[ \t]*\([^ \t#]*\)[ \t]*=[ \t]*\(.*\)$/-D\1=\2/p' \
64 $_DIR/config/system.properties
65 fi
66 ))
67
68 cd $_DIR
69 (
70 if [[ "${cfg}" != "" ]] ; then
71 # need to make sure that we don't pass the lock file descriptor
72 # to subprocesses
73 exec {cfg}>&-
74 fi
Jorge Hernandezb001c1a2019-02-28 10:10:49 -060075 nohup $JAVA_HOME/bin/java "${JVM_OPTS[@]}" -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 -050076
77 _PID=$!
78 echo $_PID > $_PIDFILE
79 )
80 sleep 5
81 status
82 echo $_STATUS
83 if [ "$_RUNNING" = "1" ]; then
84 RETVAL=0
85 else
86 echo "Failed to start"
87 remove_pid_file
88 RETVAL=1
89 fi
90}
91
92function stop() {
93 um_stop
94 update_monitor off
95}
96
97# unmonitored stop, does not change monitor status (immutable)
98function um_stop() {
99 status
100 if [ "$_RUNNING" = "0" ]; then
101 echo $_STATUS
102 remove_pid_file
103 else
Jorge Hernandez777131d2019-01-04 14:43:44 -0600104 if [[ -n ${TELEMETRY_PASSWORD} ]]; then
105 http_proxy= timeout 30 curl -k --silent --user ${TELEMETRY_USER}:${TELEMETRY_PASSWORD} -X DELETE https://localhost:${TELEMETRY_PORT}/policy/pdp/engine -o /dev/null
Pamela Dragosh0e16acf2017-02-14 19:45:48 -0500106 else
Jorge Hernandez777131d2019-01-04 14:43:44 -0600107 http_proxy= timeout 30 curl -k --silent -X DELETE https://localhost:${TELEMETRY_PORT}/policy/pdp/engine -o /dev/null
Pamela Dragosh0e16acf2017-02-14 19:45:48 -0500108 fi
109 sleep 5
110 echo "Stopping $SNAME..."
111 _PID_TO_KILL=$_PID;
112 echo "$SNAME (pid=${_PID_TO_KILL}) is stopping..."
113 kill -TERM $_PID_TO_KILL 2> /dev/null
114 sleep 5
115 check_status_of_pid $_PID_TO_KILL
116 if [ "$_RUNNING" = "1" ]; then
117 kill -TERM $_PID_TO_KILL
118 fi
119 while [ "$_RUNNING" = "1" ]; do
120 sleep 2
121 check_status_of_pid $_PID_TO_KILL
122 done
123 remove_pid_file
124 echo "$SNAME has stopped."
125 fi
126 RETVAL=0
127}
128
129function status() {
130 if [ -f "${_PIDFILE}" ]; then
131 _PID=`cat "${_PIDFILE}"`
132 check_status_of_pid $_PID
133 else
134 _STATUS="$SNAME (no pidfile) is NOT running"
135 _RUNNING=0
136 fi
137 if [[ $_RUNNING = 1 ]]; then
138 RETVAL=0
139 else
140 RETVAL=1
141 fi
142}
143
144
145function check_status_of_pid ()
146{
147 if [ -n "$1" ] && kill -0 $1 2>/dev/null ; then
148 _STATUS="$SNAME (pid $1) is running"
149 _RUNNING=1
150 else
151 _STATUS="$SNAME (pid $1) is NOT running"
152 _RUNNING=0
153 fi
154}
155
156function remove_pid_file ()
157{
158 if [ -f "${_PIDFILE}" ]; then
159 rm "${_PIDFILE}"
160 fi
161}
162
163function update_monitor() {
164 STATUS=$1
165 if [[ -f ${POLICY_HOME}/etc/monitor/monitor.cfg ]]; then
166 /bin/sed -i.bak \
167 -e "s/^${CONTROLLER}=.*/${CONTROLLER}=${STATUS}/g" \
168 ${POLICY_HOME}/etc/monitor/monitor.cfg
169 fi
170}
171
172
173# main
174
175_DIR=${POLICY_HOME}
Jorge Hernandez13e9aa82018-03-25 23:34:27 -0500176_LOGS=${POLICY_LOGS}
177
178if [[ -z ${POLICY_LOGS} ]]; then
179 _LOGS="${POLICY_HOME}"/logs
180fi
181
Pamela Dragosh0e16acf2017-02-14 19:45:48 -0500182CONTROLLER=policy-management-controller
183
184RETVAL=0
185
186_PIDFILE=${POLICY_HOME}/PID
187
188case "$1" in
189 status)
190 status
191 echo "$_STATUS"
192 ;;
193 start)
194 if flock ${cfg} ; then
195 start
196 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
197 ;;
198 umstart)
199 um_start
200 ;;
201 stop)
202 if flock ${cfg} ; then
203 stop
204 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
205 ;;
206 umstop)
207 um_stop
208 ;;
209 restart)
210 if flock ${cfg} ; then
211 stop
212 sleep 2
213 start
214 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
215 ;;
216 *)
217 echo "error: invalid option $@"
218 echo "Usage: $0 status|start|stop|restart"
219 RETVAL=1
220 ;;
221esac
222
223exit ${RETVAL}