Merge "Fix component startup"
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / deployment / src / main / resources / docker-entrypoint.sh
1 #!/bin/bash
2 #
3 # Copyright 2017, Nokia Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #
18
19 CONFIG_LOCATION=/service/application.properties
20
21 function shutdown(){
22   echo "Recieved sutdown signal"
23   if [ ! -e /proc/$PID ] ; then
24     echo "The process already exited $PID"
25     exit
26   fi
27   echo "Sending shutdown signal to $PID"
28   kill -15 $PID
29   for i in `seq 0 20`; do
30     if [ ! -e /proc/$PID ] ; then
31       echo "The driver exited normally $PID"
32       exit 0
33     fi
34     sleep 10
35   done
36   echo "Terminating the driver forcefully $PID"
37   kill -9 $PID
38 }
39
40 function switchLine(){
41   PATTERN=$1
42   REPLACEMENT=$2
43   if [ ! -z $REPLACEMENT ]; then
44     sed -i "s/${PATTERN}.*/${PATTERN}/g" $CONFIG_LOCATION
45     sed -i "s|${PATTERN}|${PATTERN}=${REPLACEMENT}|g" $CONFIG_LOCATION
46   fi
47 }
48
49 function configure(){
50   if [ ! -z "$CONFIGURE" ] ; then
51      switchLine driverMsbExternalIp $EXTERNAL_IP
52      switchLine driverVnfmExternalIp $EXTERNAL_IP
53      switchLine messageBusIp $MSB_IP
54      switchLine vnfmId $VNFM_ID
55      switchLine ipMap $IP_MAP
56   fi 
57 }
58
59
60 #during shutdown signal the ervice to stop
61 trap shutdown SIGINT SIGTERM 
62
63 #configure if required
64 configure
65
66 #start the service
67 cd /service
68
69 while true ; do
70   echo "Starting server"
71   java -jar driver.war --spring.config.location=file:/service/application.properties &>> /service/service.log &
72   export PID=$!
73   echo "Server process started in background with id $PID"
74   while test -e /proc/$PID; do
75     sleep 10
76   done
77   echo "Server quit (unexpected)"
78 done