Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / config / wait-for.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 SCRIPT_NAME=$(basename $0)
25
26 # Process the arguments passed to the script
27 usage()
28 {
29  _msg_="$@"
30  cat<<-EOF
31  Command Arguments:
32
33  -t, --timeout
34  Mandatory argument. time out value in seconds (must be number) 
35
36  -h --host
37  Mandatory argument. Host name or IP
38
39  -p, --port
40  Mandatory argument. Port of the host
41
42  --help
43  Optional argument.  Display this usage.
44
45 EOF
46  exit 1
47 }
48
49 current_timestamp()
50 {
51  date +"%Y-%m-%d %H:%M:%S"
52 }
53
54 # Called when script is executed with invalid arguments
55 invalid_arguments()
56 {
57  echo "Missing or invalid option(s):"
58  echo "$@"
59  echo "Try --help for more information"
60  exit 1
61 }
62
63 process_arguments()
64 {
65  SHORT_ARGS="t:h:p:"
66  LONG_ARGS="help,timeout:,host:,port:"
67
68  args=$(getopt -o $SHORT_ARGS -l $LONG_ARGS -n "$0"  -- "$@"  2>&1 )
69  [[ $? -ne 0 ]] && invalid_arguments $( echo " $args"| head -1 )
70  [[ $# -eq 0 ]] && invalid_arguments "No options provided"
71
72  eval set -- "$args"
73  cmd_arg="$0"
74
75  while true; do
76     case "$1" in
77          -t|--timeout)
78           TIME_OUT=$2
79           shift 2 ;;
80           -h|--host)
81           HOST=$2
82           shift 2 ;;
83           -p|--port)
84           PORT=$2
85           shift 2 ;;
86          --help)
87           usage
88           exit 0
89           ;;
90          --)
91           shift
92           break ;;
93          *)
94           echo BAD ARGUMENTS # perhaps error
95           break ;;
96       esac
97  done
98
99  regex='^[0-9]+$'
100  if ! [[ $TIME_OUT =~ $regex ]] ; then
101    echo "$SCRIPT_NAME $(current_timestamp): error: TIME_OUT must be number $TIME_OUT" >&2; exit 1
102  fi
103
104  if [ -z "$HOST" ]; then
105    echo "$SCRIPT_NAME $(current_timestamp): error: HOST must not be empty! $HOST" >&2; exit 1
106  fi
107
108  if ! [[ $PORT =~ $regex ]]; then
109    echo "$SCRIPT_NAME $(current_timestamp): error: PORT must be number! $PORT" >&2; exit 1
110  fi
111
112  SLEEP_TIME=5
113  START_TIME_IN_SECONDS=`date +%s`
114  TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT));
115
116  while [ `date +%s` -lt "$TIME_OUT_END_TIME_IN_SECONDS" ]; do
117     echo "$(current_timestamp): Waiting for $HOST:$PORT to startup ..."
118
119     nc -z "$HOST" "$PORT" > /dev/null 2>&1
120     result=$?
121     if [ $result -eq 0 ] ; then
122         echo "$SCRIPT_NAME $(current_timestamp): $HOST:$PORT is up and running"
123         break;
124     fi
125     echo "$SCRIPT_NAME $(current_timestamp): Sleeping for ${SLEEP_TIME} seconds"
126     sleep ${SLEEP_TIME}
127  done
128
129  if [ $result -ne 0 ]; then
130     echo "$SCRIPT_NAME $(current_timestamp): Time out: could not get any response from $HOST:$PORT . . ."
131     exit 1
132  fi
133
134  echo "$SCRIPT_NAME $(current_timestamp): finished successfully"
135 }
136
137 # main body
138 process_arguments $@