github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/test/check_or_start_kbweb.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  KBWEB_PORT="3000"
     4  QUOTA_PORT="44003"
     5  # use different names for the container and image, so docker inspect
     6  # will be clear about when a container exists, rather than just the
     7  # image
     8  KBWEB_CONTAINER_NAME="kbweb$KBWEB_PORT"
     9  KBWEB_IMAGE_NAME="kbweb"
    10  TIMEOUT=90
    11  
    12  function check_server() {
    13      timeout=$1
    14      tries=$2
    15      wget -O - --timeout=$timeout --tries=$tries localhost:$KBWEB_PORT > /dev/null 2>&1
    16  }
    17  
    18  # If there's already a server listening on port 3000, just use it
    19  if check_server 1 1; then
    20      echo "Keybase server already running"
    21      exit 0
    22  fi
    23  
    24  # If docker's not running, bail out
    25  # TODO: If not, launch the server from the command line?
    26  if ! docker version > /dev/null 2>&1; then
    27      echo "No server running, and docker is unavailable"
    28      exit -1
    29  fi
    30  
    31  # Unpause a paused container, if possible
    32  paused=`docker inspect --format='{{.State.Paused}}' $KBWEB_CONTAINER_NAME 2> /dev/null`
    33  if [ $? -eq 0 ]; then
    34      if [ "$paused" = "true" ]; then
    35          set -e
    36          echo "Unpausing existing container"
    37          docker unpause $KBWEB_CONTAINER_NAME
    38          check_server 1 $TIMEOUT
    39          exit 0
    40      fi
    41  fi
    42  
    43  # Start a stopped container, if possible
    44  running=`docker inspect --format='{{.State.Running}}' $KBWEB_CONTAINER_NAME 2> /dev/null`
    45  if [ $? -eq 0 ]; then
    46      if [ "$running" = "false" ]; then
    47          set -e
    48          echo "Starting existing container"
    49          docker start $KBWEB_CONTAINER_NAME
    50          check_server 1 $TIMEOUT
    51          exit 0
    52      else
    53          # if it exists, but is already unpaused and running, then maybe it's
    54          # still in the process of launching; just wait
    55          echo "Using existing container"
    56          check_server 1 $TIMEOUT
    57          exit 0
    58      fi
    59  fi
    60  
    61  # Otherwise, make sure we have a kbweb image, and launch a brand new container
    62  image=`docker images $KBWEB_IMAGE_NAME 2> /dev/null`
    63  if [ $? -eq 0 ]; then
    64      set -e
    65      echo "Launching new container"
    66      docker run -d -p $KBWEB_PORT:$KBWEB_PORT -p $QUOTA_PORT:$QUOTA_PORT -d --name=$KBWEB_CONTAINER_NAME $KBWEB_IMAGE_NAME
    67      t=0
    68      while ! curl -s -o /dev/null "http://localhost:$KBWEB_PORT"
    69      do
    70          if [ "$t" -gt "$TIMEOUT" ]; then
    71              echo "Timed out waiting for kbweb to start"
    72              docker-compose down
    73              exit 2
    74          fi
    75          sleep 1
    76          ((t++))
    77      done
    78      exit 0
    79  fi
    80  
    81  >&2 echo "Couldn't find or start the Keybase web server"
    82  exit -1