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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
22 # @author Waqas Ikram (waqas.ikram@est.tech)
24 SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
25 SCRIPT_NAME=$(basename $0)
26 WAIT_FOR_SCRIPT=$SCRIPT_HOME/wait-for.sh
28 # Process the arguments passed to the script
36 Mandatory argument. container name
39 Mandatory argument. network name
42 Mandatory argument. time out value in seconds (must be number)
45 Optional argument. Display this usage.
53 date +"%Y-%m-%d %H:%M:%S"
56 # Called when script is executed with invalid arguments
59 echo "Missing or invalid option(s):"
61 echo "Try --help for more information"
68 LONG_ARGS="help,container-name:,network-name:,timeout:"
70 args=$(getopt -o $SHORT_ARGS -l $LONG_ARGS -n "$0" -- "$@" 2>&1 )
71 [[ $? -ne 0 ]] && invalid_arguments $( echo " $args"| head -1 )
72 [[ $# -eq 0 ]] && invalid_arguments "No options provided"
96 echo BAD ARGUMENTS # perhaps error
101 if [ -z "$NAME" ]; then
102 echo "$SCRIPT_NAME $(current_timestamp): error: Container name must not be empty! $NAME" >&2; exit 1
105 if [ -z "$NETWORK_NAME" ]; then
106 echo "$SCRIPT_NAME $(current_timestamp): error: network name must not be empty! $NETWORK_NAME" >&2; exit 1
110 if ! [[ $TIME_OUT =~ $regex ]] ; then
111 echo "$SCRIPT_NAME $(current_timestamp): error: TIME_OUT must be number $TIME_OUT" >&2; exit 1
114 CONTAINER_NAME=$(docker ps -aqf "name=$NAME" --format "{{.Names}}")
116 if [ $? -ne 0 ]; then
117 echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find container using $NAME"
121 result=$(docker inspect --format '{{.State.Running}}' $CONTAINER_NAME)
123 if [ $result != "true" ] ; then
124 echo "$SCRIPT_NAME $(current_timestamp) ERROR: $CONTAINER_NAME container is not running"
128 HOST_IP=$(docker inspect --format '{{ index .NetworkSettings.Networks "'$NETWORK_NAME'" "IPAddress"}}' $CONTAINER_NAME)
130 if [ $? -ne 0 ] || [ -z $HOST_IP ] ; then
131 echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find HOST IP using network name: $NETWORK_NAME and container name: $CONTAINER_NAME"
135 PORT=$(docker port $CONTAINER_NAME | cut -c1-$(docker port $CONTAINER_NAME | grep -aob '/' | grep -oE '[0-9]+'))
137 if [ $? -ne 0 ] || [ -z $PORT ] ; then
138 echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find PORT using project name: $PROJECT_NAME and container name: $CONTAINER_NAME"
142 $WAIT_FOR_SCRIPT -t "$TIME_OUT" -h "$HOST_IP" -p "$PORT"
144 if [ $? -ne 0 ]; then
145 echo "$SCRIPT_NAME $(current_timestamp) ERROR: wait-for.sh failed ..."
149 echo "$SCRIPT_NAME $(current_timestamp): finished successfully"