Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / config / wait-for-aai-config-job.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 Nordix Foundation.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20 #
21
22 # @author Waqas Ikram (waqas.ikram@est.tech)
23
24 SLEEP_TIME=5
25 SUCCESSFUL_TEXT="AAI Simulator Populated Successfully"
26 FAILURE_TEXT="ERROR:"
27 TIME_OUT_TEXT="Time out"
28 CONTAINER_NAME=$(docker ps -aqf "name=populate-aai-config" --format "{{.Names}}")
29 SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
30 SCRIPT_NAME=$(basename $0)
31
32 current_timestamp()
33 {
34  date +"%Y-%m-%d %H:%M:%S"
35 }
36
37 # main body
38 if [ -z $TIME_OUT_DEFAULT_VALUE_SEC ]; then
39     echo "$SCRIPT_NAME $(current_timestamp): ERROR: Undefined value for TIME_OUT_DEFAULT_VALUE_SEC attribute"
40     exit 1
41 fi
42
43 if [ -z $CONTAINER_NAME ]; then
44    echo "$SCRIPT_NAME $(current_timestamp): Unable to find docker container id "
45    exit 1
46 fi
47
48 START_TIME_IN_SECONDS=`date +%s`
49 TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_DEFAULT_VALUE_SEC));
50
51
52 echo echo "$SCRIPT_NAME $(current_timestamp): $SCRIPT_NAME script Start Time `date -d @$START_TIME_IN_SECONDS`"
53 echo echo "$SCRIPT_NAME $(current_timestamp): $SCRIPT_NAME will time out at `date -d @$TIME_OUT_END_TIME_IN_SECONDS`"
54
55 while [ `date +%s` -lt "$TIME_OUT_END_TIME_IN_SECONDS" ]; do
56     echo "$(current_timestamp): Waiting for $CONTAINER_NAME to finish ..."
57
58     result=$(docker logs $CONTAINER_NAME 2>&1 | grep -E "$SUCCESSFUL_TEXT|$FAILURE_TEXT|$TIME_OUT_TEXT")
59     if [ ! -z "$result" ]; then
60         echo "$SCRIPT_NAME $(current_timestamp): Found result: $result"
61         break;
62     fi
63     echo "$(current_timestamp): Sleeping for ${SLEEP_TIME} seconds"
64     sleep ${SLEEP_TIME}
65 done
66
67 if [ -z "$result" ]; then
68    echo "$SCRIPT_NAME $(current_timestamp): ERROR: failed to populate AAI Simulator . . . "
69    echo "-------------- $CONTAINER_NAME logs -------------"
70    docker logs $CONTAINER_NAME
71    echo "------------------------------------------------------------"
72    exit 1
73 fi
74
75 if echo "$result" | grep -E "$FAILURE_TEXT|$TIME_OUT_TEXT"; then
76     echo "$SCRIPT_NAME $(current_timestamp): populate-aai-simulator.sh failed"
77     echo "-------------- $CONTAINER_NAME logs -------------"
78     docker logs $CONTAINER_NAME
79     echo "------------------------------------------------------------"
80     exit 1
81 fi
82
83 echo "$SCRIPT_NAME $(current_timestamp): Successfully populated AAI Simulator . . ."
84 exit 0