blob: 35ee2641dd8d25089620262384052995c65c9043 [file] [log] [blame]
waqas.ikram4d813752019-08-13 11:24:45 +00001#!/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
24SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
25SCRIPT_NAME=$(basename $0)
26WAIT_FOR_SCRIPT=$SCRIPT_HOME/wait-for.sh
27
28# Process the arguments passed to the script
29usage()
30{
31 _msg_="$@"
32 cat<<-EOF
33 Command Arguments:
34
waqas.ikram50e7ec92019-08-14 14:27:41 +000035 -c, --container-name
waqas.ikram4d813752019-08-13 11:24:45 +000036 Mandatory argument. container name
37
waqas.ikram50e7ec92019-08-14 14:27:41 +000038 -n, --network-name
39 Mandatory argument. network name
waqas.ikram4d813752019-08-13 11:24:45 +000040
41 -t, --timeout
42 Mandatory argument. time out value in seconds (must be number)
43
44 --help
45 Optional argument. Display this usage.
46
47EOF
48 exit 1
49}
50
51current_timestamp()
52{
53 date +"%Y-%m-%d %H:%M:%S"
54}
55
56# Called when script is executed with invalid arguments
57invalid_arguments()
58{
59 echo "Missing or invalid option(s):"
60 echo "$@"
61 echo "Try --help for more information"
62 exit 1
63}
64
65process_arguments()
66{
waqas.ikram50e7ec92019-08-14 14:27:41 +000067 SHORT_ARGS="c:n:t:"
68 LONG_ARGS="help,container-name:,network-name:,timeout:"
waqas.ikram4d813752019-08-13 11:24:45 +000069
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"
73
74 eval set -- "$args"
75 cmd_arg="$0"
76
77 while true; do
78 case "$1" in
waqas.ikram50e7ec92019-08-14 14:27:41 +000079 -c|--container-name)
waqas.ikram4d813752019-08-13 11:24:45 +000080 NAME=$2
81 shift 2 ;;
waqas.ikram50e7ec92019-08-14 14:27:41 +000082 -n|--network-name)
83 NETWORK_NAME=$2
waqas.ikram4d813752019-08-13 11:24:45 +000084 shift 2 ;;
85 -t|--timeout)
86 TIME_OUT=$2
87 shift 2 ;;
88 --help)
89 usage
90 exit 0
91 ;;
92 --)
93 shift
94 break ;;
95 *)
96 echo BAD ARGUMENTS # perhaps error
97 break ;;
98 esac
99 done
100
101 if [ -z "$NAME" ]; then
102 echo "$SCRIPT_NAME $(current_timestamp): error: Container name must not be empty! $NAME" >&2; exit 1
103 fi
104
waqas.ikram50e7ec92019-08-14 14:27:41 +0000105 if [ -z "$NETWORK_NAME" ]; then
106 echo "$SCRIPT_NAME $(current_timestamp): error: network name must not be empty! $NETWORK_NAME" >&2; exit 1
waqas.ikram4d813752019-08-13 11:24:45 +0000107 fi
108
109 regex='^[0-9]+$'
110 if ! [[ $TIME_OUT =~ $regex ]] ; then
111 echo "$SCRIPT_NAME $(current_timestamp): error: TIME_OUT must be number $TIME_OUT" >&2; exit 1
112 fi
113
114 CONTAINER_NAME=$(docker ps -aqf "name=$NAME" --format "{{.Names}}")
115
116 if [ $? -ne 0 ]; then
117 echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find container using $NAME"
118 exit 1
119 fi
120
waqas.ikram2deca5d2019-09-10 16:02:32 +0100121 result=$(docker inspect --format '{{.State.Running}}' $CONTAINER_NAME)
122
123 if [ $result != "true" ] ; then
124 echo "$SCRIPT_NAME $(current_timestamp) ERROR: $CONTAINER_NAME container is not running"
125 exit 1
126 fi
127
waqas.ikram50e7ec92019-08-14 14:27:41 +0000128 HOST_IP=$(docker inspect --format '{{ index .NetworkSettings.Networks "'$NETWORK_NAME'" "IPAddress"}}' $CONTAINER_NAME)
waqas.ikram4d813752019-08-13 11:24:45 +0000129
waqas.ikram2deca5d2019-09-10 16:02:32 +0100130 if [ $? -ne 0 ] || [ -z $HOST_IP ] ; then
waqas.ikram50e7ec92019-08-14 14:27:41 +0000131 echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find HOST IP using network name: $NETWORK_NAME and container name: $CONTAINER_NAME"
waqas.ikram4d813752019-08-13 11:24:45 +0000132 exit 1
133 fi
134
135 PORT=$(docker port $CONTAINER_NAME | cut -c1-$(docker port $CONTAINER_NAME | grep -aob '/' | grep -oE '[0-9]+'))
136
waqas.ikram2deca5d2019-09-10 16:02:32 +0100137 if [ $? -ne 0 ] || [ -z $PORT ] ; then
waqas.ikram4d813752019-08-13 11:24:45 +0000138 echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find PORT using project name: $PROJECT_NAME and container name: $CONTAINER_NAME"
139 exit 1
140 fi
141
142 $WAIT_FOR_SCRIPT -t "$TIME_OUT" -h "$HOST_IP" -p "$PORT"
143
144 if [ $? -ne 0 ]; then
145 echo "$SCRIPT_NAME $(current_timestamp) ERROR: wait-for.sh failed ..."
146 exit 1
147 fi
148
149 echo "$SCRIPT_NAME $(current_timestamp): finished successfully"
150}
151
152# main body
153process_arguments $@