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