Pamela Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Jorge Hernandez | 508ad58 | 2017-05-31 13:58:27 -0500 | [diff] [blame] | 3 | ### |
| 4 | # ============LICENSE_START======================================================= |
| 5 | # ONAP POLICY |
| 6 | # ================================================================================ |
Jorge Hernandez | c6f2da8 | 2019-02-13 13:12:37 -0600 | [diff] [blame] | 7 | # Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. |
Jorge Hernandez | 508ad58 | 2017-05-31 13:58:27 -0500 | [diff] [blame] | 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 Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 23 | SNAME="Policy Management" |
| 24 | PNAME=policy-management |
Guo Ruijing | 6abeb29 | 2017-07-28 08:23:01 +0000 | [diff] [blame] | 25 | CLASS=org.onap.policy.drools.system.Main |
Pamela Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 26 | |
| 27 | |
| 28 | function 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) |
| 38 | function um_start() { |
| 39 | status |
| 40 | if [ "$_RUNNING" = "1" ]; then |
| 41 | echo $_STATUS |
| 42 | RETVAL=0 |
| 43 | return |
| 44 | fi |
Jorge Hernandez | 13e9aa8 | 2018-03-25 23:34:27 -0500 | [diff] [blame] | 45 | 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 Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 50 | CP=$(ls $_DIR/lib/*.jar | xargs -I X printf ":%s" X) |
| 51 | |
Jorge Hernandez | c6f2da8 | 2019-02-13 13:12:37 -0600 | [diff] [blame] | 52 | # 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 Hernandez | b001c1a | 2019-02-28 10:10:49 -0600 | [diff] [blame] | 56 | JVM_OPTS=(${JVM_OPTIONS}) |
Jorge Hernandez | c6f2da8 | 2019-02-13 13:12:37 -0600 | [diff] [blame] | 57 | |
Jorge Hernandez | b001c1a | 2019-02-28 10:10:49 -0600 | [diff] [blame] | 58 | # If 'system.properties' exists, convert it into "-D" JVM arguments. |
Pamela Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 59 | # 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 Hernandez | b001c1a | 2019-02-28 10:10:49 -0600 | [diff] [blame] | 75 | 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 Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 76 | |
| 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 | |
| 92 | function stop() { |
| 93 | um_stop |
| 94 | update_monitor off |
| 95 | } |
| 96 | |
| 97 | # unmonitored stop, does not change monitor status (immutable) |
| 98 | function um_stop() { |
| 99 | status |
| 100 | if [ "$_RUNNING" = "0" ]; then |
| 101 | echo $_STATUS |
| 102 | remove_pid_file |
| 103 | else |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 104 | if [[ -n ${TELEMETRY_PASSWORD} ]]; then |
Jorge Hernandez | d9bea32 | 2019-03-15 00:26:53 -0500 | [diff] [blame] | 105 | http_proxy= timeout 10 curl -k --silent --user ${TELEMETRY_USER}:${TELEMETRY_PASSWORD} -X DELETE https://localhost:${TELEMETRY_PORT}/policy/pdp/engine -o /dev/null |
Pamela Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 106 | else |
Jorge Hernandez | d9bea32 | 2019-03-15 00:26:53 -0500 | [diff] [blame] | 107 | http_proxy= timeout 10 curl -k --silent -X DELETE https://localhost:${TELEMETRY_PORT}/policy/pdp/engine -o /dev/null |
Pamela Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 108 | 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 | |
| 129 | function 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 | |
| 145 | function 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 | |
| 156 | function remove_pid_file () |
| 157 | { |
| 158 | if [ -f "${_PIDFILE}" ]; then |
| 159 | rm "${_PIDFILE}" |
| 160 | fi |
| 161 | } |
| 162 | |
| 163 | function 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 Hernandez | 13e9aa8 | 2018-03-25 23:34:27 -0500 | [diff] [blame] | 176 | _LOGS=${POLICY_LOGS} |
| 177 | |
| 178 | if [[ -z ${POLICY_LOGS} ]]; then |
| 179 | _LOGS="${POLICY_HOME}"/logs |
| 180 | fi |
| 181 | |
Pamela Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 182 | CONTROLLER=policy-management-controller |
| 183 | |
| 184 | RETVAL=0 |
| 185 | |
| 186 | _PIDFILE=${POLICY_HOME}/PID |
| 187 | |
| 188 | case "$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 | ;; |
| 221 | esac |
| 222 | |
| 223 | exit ${RETVAL} |