blob: 589c4c3219b1abf17abc2606a945a9dc4006a48b [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
56
Pamela Dragosh0e16acf2017-02-14 19:45:48 -050057 # If 'system.properties' exists, convert it into JVM arguments.
58 # Note that the following also handles property values with spaces.
59 IFS=$'\n'
60 systemProperties=($(
61 if [[ -f $_DIR/config/system.properties ]] ; then
62 sed -n -e 's/^[ \t]*\([^ \t#]*\)[ \t]*=[ \t]*\(.*\)$/-D\1=\2/p' \
63 $_DIR/config/system.properties
64 fi
65 ))
66
67 cd $_DIR
68 (
69 if [[ "${cfg}" != "" ]] ; then
70 # need to make sure that we don't pass the lock file descriptor
71 # to subprocesses
72 exec {cfg}>&-
73 fi
Jorge Hernandez777131d2019-01-04 14:43:44 -060074 nohup $JAVA_HOME/bin/java -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 -050075
76 _PID=$!
77 echo $_PID > $_PIDFILE
78 )
79 sleep 5
80 status
81 echo $_STATUS
82 if [ "$_RUNNING" = "1" ]; then
83 RETVAL=0
84 else
85 echo "Failed to start"
86 remove_pid_file
87 RETVAL=1
88 fi
89}
90
91function stop() {
92 um_stop
93 update_monitor off
94}
95
96# unmonitored stop, does not change monitor status (immutable)
97function um_stop() {
98 status
99 if [ "$_RUNNING" = "0" ]; then
100 echo $_STATUS
101 remove_pid_file
102 else
Jorge Hernandez777131d2019-01-04 14:43:44 -0600103 if [[ -n ${TELEMETRY_PASSWORD} ]]; then
104 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 -0500105 else
Jorge Hernandez777131d2019-01-04 14:43:44 -0600106 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 -0500107 fi
108 sleep 5
109 echo "Stopping $SNAME..."
110 _PID_TO_KILL=$_PID;
111 echo "$SNAME (pid=${_PID_TO_KILL}) is stopping..."
112 kill -TERM $_PID_TO_KILL 2> /dev/null
113 sleep 5
114 check_status_of_pid $_PID_TO_KILL
115 if [ "$_RUNNING" = "1" ]; then
116 kill -TERM $_PID_TO_KILL
117 fi
118 while [ "$_RUNNING" = "1" ]; do
119 sleep 2
120 check_status_of_pid $_PID_TO_KILL
121 done
122 remove_pid_file
123 echo "$SNAME has stopped."
124 fi
125 RETVAL=0
126}
127
128function status() {
129 if [ -f "${_PIDFILE}" ]; then
130 _PID=`cat "${_PIDFILE}"`
131 check_status_of_pid $_PID
132 else
133 _STATUS="$SNAME (no pidfile) is NOT running"
134 _RUNNING=0
135 fi
136 if [[ $_RUNNING = 1 ]]; then
137 RETVAL=0
138 else
139 RETVAL=1
140 fi
141}
142
143
144function check_status_of_pid ()
145{
146 if [ -n "$1" ] && kill -0 $1 2>/dev/null ; then
147 _STATUS="$SNAME (pid $1) is running"
148 _RUNNING=1
149 else
150 _STATUS="$SNAME (pid $1) is NOT running"
151 _RUNNING=0
152 fi
153}
154
155function remove_pid_file ()
156{
157 if [ -f "${_PIDFILE}" ]; then
158 rm "${_PIDFILE}"
159 fi
160}
161
162function update_monitor() {
163 STATUS=$1
164 if [[ -f ${POLICY_HOME}/etc/monitor/monitor.cfg ]]; then
165 /bin/sed -i.bak \
166 -e "s/^${CONTROLLER}=.*/${CONTROLLER}=${STATUS}/g" \
167 ${POLICY_HOME}/etc/monitor/monitor.cfg
168 fi
169}
170
171
172# main
173
174_DIR=${POLICY_HOME}
Jorge Hernandez13e9aa82018-03-25 23:34:27 -0500175_LOGS=${POLICY_LOGS}
176
177if [[ -z ${POLICY_LOGS} ]]; then
178 _LOGS="${POLICY_HOME}"/logs
179fi
180
Pamela Dragosh0e16acf2017-02-14 19:45:48 -0500181CONTROLLER=policy-management-controller
182
183RETVAL=0
184
185_PIDFILE=${POLICY_HOME}/PID
186
187case "$1" in
188 status)
189 status
190 echo "$_STATUS"
191 ;;
192 start)
193 if flock ${cfg} ; then
194 start
195 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
196 ;;
197 umstart)
198 um_start
199 ;;
200 stop)
201 if flock ${cfg} ; then
202 stop
203 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
204 ;;
205 umstop)
206 um_stop
207 ;;
208 restart)
209 if flock ${cfg} ; then
210 stop
211 sleep 2
212 start
213 fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock
214 ;;
215 *)
216 echo "error: invalid option $@"
217 echo "Usage: $0 status|start|stop|restart"
218 RETVAL=1
219 ;;
220esac
221
222exit ${RETVAL}