github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/tools/integration/config.sh (about)

     1  #!/bin/bash
     2  # NB only to be sourced
     3  
     4  set -e
     5  
     6  DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
     7  
     8  # Protect against being sourced multiple times to prevent
     9  # overwriting assert.sh global state
    10  if ! [ -z "$SOURCED_CONFIG_SH" ]; then
    11      return
    12  fi
    13  SOURCED_CONFIG_SH=true
    14  
    15  # these ought to match what is in Vagrantfile
    16  N_MACHINES=${N_MACHINES:-3}
    17  IP_PREFIX=${IP_PREFIX:-192.168.48}
    18  IP_SUFFIX_BASE=${IP_SUFFIX_BASE:-10}
    19  
    20  if [ -z "$HOSTS" ]; then
    21      for i in $(seq 1 "$N_MACHINES"); do
    22          IP="${IP_PREFIX}.$((IP_SUFFIX_BASE + i))"
    23          HOSTS="$HOSTS $IP"
    24      done
    25  fi
    26  
    27  # these are used by the tests
    28  # shellcheck disable=SC2034
    29  HOST1=$(echo "$HOSTS" | cut -f 1 -d ' ')
    30  # shellcheck disable=SC2034
    31  HOST2=$(echo "$HOSTS" | cut -f 2 -d ' ')
    32  # shellcheck disable=SC2034
    33  HOST3=$(echo "$HOSTS" | cut -f 3 -d ' ')
    34  
    35  # shellcheck disable=SC1090
    36  . "$DIR/assert.sh"
    37  
    38  SSH_DIR=${SSH_DIR:-$DIR}
    39  SSH=${SSH:-ssh -l vagrant -i \"$SSH_DIR/insecure_private_key\" -o \"UserKnownHostsFile=$SSH_DIR/.ssh_known_hosts\" -o CheckHostIP=no -o StrictHostKeyChecking=no}
    40  
    41  SMALL_IMAGE="alpine"
    42  # shellcheck disable=SC2034
    43  TEST_IMAGES="$SMALL_IMAGE"
    44  
    45  # shellcheck disable=SC2034
    46  PING="ping -nq -W 1 -c 1"
    47  DOCKER_PORT=2375
    48  
    49  remote() {
    50      rem=$1
    51      shift 1
    52      "$@" > >(while read -r line; do echo -e $'\e[0;34m'"$rem>"$'\e[0m'" $line"; done)
    53  }
    54  
    55  colourise() {
    56      ([ -t 0 ] && echo -ne $'\e['"$1"'m') || true
    57      shift
    58      # It's important that we don't do this in a subshell, as some
    59      # commands we execute need to modify global state
    60      "$@"
    61      ([ -t 0 ] && echo -ne $'\e[0m') || true
    62  }
    63  
    64  whitely() {
    65      colourise '1;37' "$@"
    66  }
    67  
    68  greyly() {
    69      colourise '0;37' "$@"
    70  }
    71  
    72  redly() {
    73      colourise '1;31' "$@"
    74  }
    75  
    76  greenly() {
    77      colourise '1;32' "$@"
    78  }
    79  
    80  run_on() {
    81      host=$1
    82      shift 1
    83      [ -z "$DEBUG" ] || greyly echo "Running on $host:" "$@" >&2
    84      # shellcheck disable=SC2086
    85      remote "$host" $SSH "$host" "$@"
    86  }
    87  
    88  docker_on() {
    89      host=$1
    90      shift 1
    91      [ -z "$DEBUG" ] || greyly echo "Docker on $host:$DOCKER_PORT:" "$@" >&2
    92      docker -H "tcp://$host:$DOCKER_PORT" "$@"
    93  }
    94  
    95  weave_on() {
    96      host=$1
    97      shift 1
    98      [ -z "$DEBUG" ] || greyly echo "Weave on $host:$DOCKER_PORT:" "$@" >&2
    99      DOCKER_HOST=tcp://$host:$DOCKER_PORT $WEAVE "$@"
   100  }
   101  
   102  exec_on() {
   103      host=$1
   104      container=$2
   105      shift 2
   106      docker -H "tcp://$host:$DOCKER_PORT" exec "$container" "$@"
   107  }
   108  
   109  rm_containers() {
   110      host=$1
   111      shift
   112      [ $# -eq 0 ] || docker_on "$host" rm -f "$@" >/dev/null
   113  }
   114  
   115  start_suite() {
   116      for host in $HOSTS; do
   117          [ -z "$DEBUG" ] || echo "Cleaning up on $host: removing all containers and resetting weave"
   118          # shellcheck disable=SC2046
   119          rm_containers "$host" $(docker_on "$host" ps -aq 2>/dev/null)
   120          run_on "$host" "docker network ls | grep -q ' weave ' && docker network rm weave" || true
   121          weave_on "$host" reset 2>/dev/null
   122      done
   123      whitely echo "$@"
   124  }
   125  
   126  end_suite() {
   127      whitely assert_end
   128  }
   129  
   130  WEAVE=$DIR/../../integration/weave