[AAI-2175] Change aai champ container processes to run as non-root on the host
[aai/champ.git] / champ-service / src / main / bin / start.sh
1 #!/bin/sh
2 #
3 # ============LICENSE_START==========================================
4 # org.onap.aai
5 # ===================================================================
6 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7 # Copyright © 2017 Amdocs
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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 #
23
24 APP_HOME="/opt/app/champ-service"
25 GRAPH_DEPS_HOME="${APP_HOME}/graph-deps"
26
27 if [ -z "$CONFIG_HOME" ]; then
28     echo "CONFIG_HOME must be set in order to start up process"
29     exit 1
30 fi
31
32 if [ -z "$KEY_STORE_PASSWORD" ]; then
33     echo "KEY_STORE_PASSWORD must be set in order to start up process"
34     exit 1
35 fi
36
37 if [ -z "$SERVICE_BEANS" ]; then
38     echo "SERVICE_BEANS must be set in order to start up process"
39     exit 1
40 fi
41
42 for dir in $( find ${GRAPH_DEPS_HOME}/* -maxdepth 0 -type d ); do
43     CURRIMPL=$(basename $dir)
44     if [ "x$GRAPHIMPL" = "x$CURRIMPL" ]; then
45         GRAPHIMPL_DEPS="${GRAPH_DEPS_HOME}/${GRAPHIMPL}"
46         echo "Setting up graph implementation to $GRAPHIMPL"
47     else
48         SUPPORTED_GRAPHIMPL="$SUPPORTED_GRAPHIMPL $CURRIMPL"
49     fi
50 done
51
52 if [ -z "$GRAPHIMPL_DEPS" ]; then
53     echo "Configured graph implementation '$GRAPHIMPL' is not supported. Acceptable implementations are one of: $SUPPORTED_GRAPHIMPL"
54     exit 1
55 fi
56
57 # Changes related to:AAI-2175
58 # Change aai champ container processes to run as non-root on the host
59 USER_ID=${LOCAL_USER_ID:-9001}
60 GROUP_ID=${LOCAL_GROUP_ID:-9001}
61 CHAMP_LOGS=/var/log/onap/AAI-CHAMP
62
63 if [ $(cat /etc/passwd | grep aaiadmin | wc -l) -eq 0 ]; then
64
65         groupadd aaiadmin -g ${GROUP_ID} || {
66                 echo "Unable to create the group id for ${GROUP_ID}";
67                 exit 1;
68         }
69         useradd --shell=/bin/bash -u ${USER_ID} -g ${GROUP_ID} -o -c "" -m aaiadmin || {
70                 echo "Unable to create the user id for ${USER_ID}";
71                 exit 1;
72         }
73 fi;
74
75 chown -R aaiadmin:aaiadmin ${MICRO_HOME}
76 chown -R aaiadmin:aaiadmin ${APP_HOME}
77 chown -R aaiadmin:aaiadmin ${CHAMP_LOGS}
78
79 find ${MICRO_HOME}  -name "*.sh" -exec chmod +x {} +
80
81 gosu aaiadmin ln -s /logs $MICRO_HOME/logs
82 JAVA_CMD="exec gosu aaiadmin java";
83 PROPS="-DAPP_HOME=$APP_HOME"
84 PROPS="$PROPS -DCONFIG_HOME=$CONFIG_HOME"
85 PROPS="$PROPS -Dlogging.config=$APP_HOME/bundleconfig/etc/logback.xml"
86 PROPS="$PROPS -DKEY_STORE_PASSWORD=$KEY_STORE_PASSWORD"
87
88 if [ ! -z "$TRUST_STORE_PASSWORD" ]; then
89    PROPS="$PROPS -DTRUST_STORE_PASSWORD=${TRUST_STORE_PASSWORD}"
90 fi
91
92 if [ ! -z "$TRUST_STORE_LOCATION" ]; then
93    PROPS="$PROPS -DTRUST_STORE_LOCATION=${TRUST_STORE_LOCATION}"
94 fi
95
96 JVM_MAX_HEAP=${MAX_HEAP:-1024}
97
98 set -x
99 ${JAVA_CMD} -Xmx${JVM_MAX_HEAP}m $PROPS -Dloader.path="${GRAPHIMPL_DEPS}" -jar "${APP_HOME}/champ-service.jar"