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 | |
| 35 | -n, --name |
| 36 | Mandatory argument. container name |
| 37 | |
| 38 | -p, --project-name |
| 39 | Mandatory argument. project name |
| 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 | { |
| 67 | SHORT_ARGS="n:p:t:" |
| 68 | LONG_ARGS="help,name:,project-name:,timeout:" |
| 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 |
| 79 | -n|--name) |
| 80 | NAME=$2 |
| 81 | shift 2 ;; |
| 82 | -p|project-name) |
| 83 | PROJECT_NAME=$2 |
| 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 | |
| 105 | if [ -z "$PROJECT_NAME" ]; then |
| 106 | echo "$SCRIPT_NAME $(current_timestamp): error: project name must not be empty! $PROJECT_NAME" >&2; exit 1 |
| 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 | |
| 121 | HOST_IP=$(docker inspect --format '{{ index .NetworkSettings.Networks "'$PROJECT_NAME'" "IPAddress"}}' $CONTAINER_NAME) |
| 122 | |
| 123 | if [ $? -ne 0 ]; then |
| 124 | echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find HOST IP using project name: $PROJECT_NAME and container name: $CONTAINER_NAME" |
| 125 | exit 1 |
| 126 | fi |
| 127 | |
| 128 | PORT=$(docker port $CONTAINER_NAME | cut -c1-$(docker port $CONTAINER_NAME | grep -aob '/' | grep -oE '[0-9]+')) |
| 129 | |
| 130 | if [ $? -ne 0 ]; then |
| 131 | echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find PORT using project name: $PROJECT_NAME and container name: $CONTAINER_NAME" |
| 132 | exit 1 |
| 133 | fi |
| 134 | |
| 135 | $WAIT_FOR_SCRIPT -t "$TIME_OUT" -h "$HOST_IP" -p "$PORT" |
| 136 | |
| 137 | if [ $? -ne 0 ]; then |
| 138 | echo "$SCRIPT_NAME $(current_timestamp) ERROR: wait-for.sh failed ..." |
| 139 | exit 1 |
| 140 | fi |
| 141 | |
| 142 | echo "$SCRIPT_NAME $(current_timestamp): finished successfully" |
| 143 | } |
| 144 | |
| 145 | # main body |
| 146 | process_arguments $@ |