ea2a81d762013769efbb8b00b64e3fe82a91c34b
[dmaap/buscontroller.git] / misc / dmaapbc
1 #!/bin/bash
2 #
3 # ============LICENSE_START==========================================
4 # org.onap.dmaap
5 # ===================================================================
6 # Copyright © 2018 AT&T Intellectual Property. All rights reserved.
7 # ===================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #        http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END============================================
20 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 #
22 #
23
24 umask 0022
25 TZ=GMT0
26 COMPONENT=dmaapbc
27 APP_ROOT=/opt/app/$COMPONENT
28 USER=root
29 export TZ
30 PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/java/jdk/jdk180/bin
31 export PATH
32 CLASSPATH=`echo $APP_ROOT/etc $APP_ROOT/lib/*.jar | tr ' ' ':'` 
33 export CLASSPATH
34 CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config/conf}
35 CONFIGMAP_PROPS=${CONFIGMAP_PROPS:-/opt/app/config/conf/dmaapbc.properties}
36 CONTAINER_CONFIG=$CONFIGMAP_ROOT/buscontroller.env
37 MAIN=org.onap.dmaap.dbcapi.server.Main 
38
39
40
41 pids() {
42         set -x
43         ps -ef | grep java | grep $MAIN | sed -e 's/[^ ]* *//' -e 's/ .*//'
44         set +x
45 }
46
47 config() {
48         set -x
49         if [ ! -d $APP_ROOT ]
50         then
51                 echo "Expected app root directory $APP_ROOT does not exist"
52                 exit 1
53         fi
54         if [ !  -f $CONTAINER_CONFIG ]
55         then
56                 echo "WARNING: Expected env file $CONTAINER_CONFIG not found. Default behaviors in effect"
57                 find $CONTAINER_ROOT -type f
58         fi
59         cd $APP_ROOT
60         source $CONTAINER_CONFIG
61
62         if [ "$DMAAPBC_WAIT_TO_EXIT" != "Y" ]
63         then
64                 echo "Creating $APP_ROOT/ok_to_exit so no waiting..."
65                 > $APP_ROOT/ok_to_exit
66         else
67                 echo "Not creating $APP_ROOT/ok_to_exit"
68         fi      
69         
70         if [ ! -f $APP_ROOT/misc/cert-client-init.sh ]
71         then
72                 echo "Did not find $APP_ROOT/misc/cert-client-init.sh to append to truststore"
73                 exit 1
74         fi
75         $APP_ROOT/misc/cert-client-init.sh
76         . misc/havecert.tmpl > etc/havecert
77         chmod +x etc/havecert
78
79         # These files might be better provided in kubernetes configmaps
80         # so if they are there, use them
81         if [ -f $CONFIGMAP_PROPS ]
82         then
83                 PROPS=$CONFIGMAP_PROPS
84         else
85                 PROPS=etc/dmaapbc.properties
86                 . misc/dmaapbc.properties.tmpl > $PROPS
87         fi
88         if [ ! -f config/PolicyEngineApi.properties ]
89         then
90                 . misc/PolicyEngineApi.properties.tmpl > config/PolicyEngineApi.properties
91         fi
92         set +x
93 }
94
95 start() {
96         set -x
97         ID=`id -n -u`
98         GRP=`id -n -g`
99         if [ "$ID" != "$USER" ]
100         then
101                 echo $COMPONENT must be started as user $USER not $ID
102                 exit 1
103         fi
104         if [ "$GRP" != "$USER" ]
105         then
106                 echo $COMPONENT must be started as group $USER not $GRP
107                 exit 1
108         fi
109         cd $APP_ROOT
110
111         if etc/havecert
112         then
113                 echo >/dev/null
114         else
115                 echo No certificate file available.  Cannot start
116                 exit 0
117         fi
118         PIDS=`pids`
119         if [ "$PIDS" != "" ]
120         then
121                 echo $COMPONENT already running
122                 exit 0
123         fi
124         rm -f $APP_ROOT/etc/SHUTDOWN
125
126         # JVM flags
127 #old line from Dockerfile...keep for reference only
128         FLAGS="-cp etc:lib/* -Dlog4j.configuration=etc/log4j.properties -Ddmaapbc.properties=$PROPS  -Dlogback.configurationFile=etc/logback.xml -Dhttps.protocols=TLSv1.2 -Dhttps.cipherSuites=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"
129         #nohup java $FLAGS $MAIN </dev/null >/dev/null 2>&1 &
130         nohup java $FLAGS $MAIN </dev/null  &
131         sleep 5
132         PIDS=`pids`
133         set +x
134 }
135
136 stop() {
137         ID=`id -n -u`
138         GRP=`id -n -g`
139         if [ "$ID" != "$USER" ]
140         then
141                 echo $COMPONENT must be stopped as user $USER not $ID
142                 exit 1
143         fi
144         if [ "$GRP" != "$USER" ]
145         then
146                 echo $COMPONENT must be stopped as group $USER not $GRP
147                 exit 1
148         fi
149         touch $APP_ROOT/etc/SHUTDOWN
150         PIDS=`pids`
151         if [ "$PIDS" != "" ]
152         then
153                 sleep 5
154                 kill -9 $PIDS
155                 sleep 5
156                 echo $COMPONENT stopped
157         else
158                 echo $COMPONENT not running
159         fi
160 }
161
162 status() {
163         PIDS=`pids`
164         if [ "$PIDS" != "" ]
165         then
166                 echo $COMPONENT running
167         else
168                 echo $COMPONENT not running
169         fi
170 }
171
172 init() {
173         if [ ! -d $CONFIGMAP_ROOT ]
174         then
175                 echo $CONFIGMAP_ROOT does not exist
176                 return
177         fi
178
179         #loop on get /dmaap until we get a good response to indicate other provisioning can continue
180         rc=999
181         while [ $rc != "200" ]
182         do
183                 sleep 10
184                 rc=`curl -s -o /dev/null -I -w "%{http_code}" -X GET -H "Content-Type: application/json" http://dmaap-bc:8080/webapi/dmaap`
185                 echo "get dmaap response=${rc}"
186         done
187
188         cd $CONFIGMAP_ROOT
189         # order is important in this next list
190         for uri in dmaap dcaeLocations mr_clusters topics feeds
191         do
192                 if [ -d ${uri} ]
193                 then
194                         for j in `ls ${uri}/*.json`
195                         do
196                                 curl -v -X POST -H "Content-Type: application/json" -d @${j} http://dmaap-bc:8080/webapi/${uri}
197                         done
198                 fi
199         done
200 }
201
202 set -x
203 case "$1" in
204 'deploy')
205         config
206         start
207         init
208         wait
209         ;;
210 'start')
211         start
212         ;;
213 'stop')
214         stop
215         ;;
216 'restart')
217         stop
218         sleep 20
219         start
220         ;;
221 'status')
222         status
223         ;;
224 *)
225         echo "Usage: $0 { start | stop | restart }"
226         exit 1
227         ;;
228 esac
229                 ls -l $APP_ROOT/logs/ONAP
230                 echo "------------ tail -100 error.log ---------------"
231                 tail -100  $APP_ROOT/logs/ONAP/error.log
232                 echo "------------ tail -100 server.log ---------------"
233                 tail -100  $APP_ROOT/logs/ONAP/server.log
234                 echo "------------ tail -100 application.log ---------------"
235                 tail -100 $APP_ROOT/logs/ONAP/application.log
236
237                 echo "Check $APP_ROOT/ok_to_exit"
238                 while [ ! -f $APP_ROOT/ok_to_exit ]
239                 do
240                         echo "$APP_ROOT/ok_to_exit does not exist.  Sticking around for debugging..."
241                         sleep 10
242                 done
243 exit 0