waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 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_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 25 | SCRIPT_NAME=$(basename $0) |
| 26 | WAIT_FOR_SCRIPT=$SCRIPT_HOME/wait-for.sh |
| 27 | |
| 28 | # Process the arguments passed to the script |
| 29 | usage() |
| 30 | { |
| 31 | _msg_="$@" |
| 32 | cat<<-EOF |
| 33 | Command Arguments: |
| 34 | |
waqas.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 35 | -c, --container-name |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 36 | Mandatory argument. container name |
| 37 | |
waqas.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 38 | -n, --network-name |
| 39 | Mandatory argument. network name |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 40 | |
| 41 | -t, --timeout |
| 42 | Mandatory argument. time out value in seconds (must be number) |
| 43 | |
| 44 | --help |
| 45 | Optional argument. Display this usage. |
| 46 | |
| 47 | EOF |
| 48 | exit 1 |
| 49 | } |
| 50 | |
| 51 | current_timestamp() |
| 52 | { |
| 53 | date +"%Y-%m-%d %H:%M:%S" |
| 54 | } |
| 55 | |
| 56 | # Called when script is executed with invalid arguments |
| 57 | invalid_arguments() |
| 58 | { |
| 59 | echo "Missing or invalid option(s):" |
| 60 | echo "$@" |
| 61 | echo "Try --help for more information" |
| 62 | exit 1 |
| 63 | } |
| 64 | |
| 65 | process_arguments() |
| 66 | { |
waqas.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 67 | SHORT_ARGS="c:n:t:" |
| 68 | LONG_ARGS="help,container-name:,network-name:,timeout:" |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 69 | |
| 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.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 79 | -c|--container-name) |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 80 | NAME=$2 |
| 81 | shift 2 ;; |
waqas.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 82 | -n|--network-name) |
| 83 | NETWORK_NAME=$2 |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 84 | 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.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 105 | if [ -z "$NETWORK_NAME" ]; then |
| 106 | echo "$SCRIPT_NAME $(current_timestamp): error: network name must not be empty! $NETWORK_NAME" >&2; exit 1 |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 107 | 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.ikram | 2deca5d | 2019-09-10 16:02:32 +0100 | [diff] [blame] | 121 | 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.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 128 | HOST_IP=$(docker inspect --format '{{ index .NetworkSettings.Networks "'$NETWORK_NAME'" "IPAddress"}}' $CONTAINER_NAME) |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 129 | |
waqas.ikram | 2deca5d | 2019-09-10 16:02:32 +0100 | [diff] [blame] | 130 | if [ $? -ne 0 ] || [ -z $HOST_IP ] ; then |
waqas.ikram | 50e7ec9 | 2019-08-14 14:27:41 +0000 | [diff] [blame] | 131 | echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find HOST IP using network name: $NETWORK_NAME and container name: $CONTAINER_NAME" |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 132 | 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.ikram | 2deca5d | 2019-09-10 16:02:32 +0100 | [diff] [blame] | 137 | if [ $? -ne 0 ] || [ -z $PORT ] ; then |
waqas.ikram | 4d81375 | 2019-08-13 11:24:45 +0000 | [diff] [blame] | 138 | 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 |
| 153 | process_arguments $@ |