github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/deploy/dev/docker/stop_docker.sh (about)

     1  #!/bin/bash
     2  NAME=`basename "$0"`
     3  SETUP_FILE="/tmp/docker_ais/deploy.env"
     4  DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
     5  
     6  usage() {
     7      echo "============================================== Usage: =============================================="
     8      echo "$name removes the containers, networks and images used by services defined in a compose file"
     9      echo "$name -s or --single              --> to stop a single network docker configuration"
    10      echo "$name -m or --multi               --> to stop a multi network docker configuration"
    11      echo "$name -c=NUM or--clustered=NUM    --> to stop multiple cluster docker configuration with NUM clusters"
    12      echo "$name -qs or --quickstart         --> stop the quickstart docker AIS deployment"
    13      echo "$name -l or --last                --> Uses your last saved docker configuration defined in ${SETUP_FILE} to stop docker"
    14      echo "Note adding the --rmi flag to any of the commands above will also removes images."
    15      echo "Note providing multiple flags will result in the last flag taking precedence."
    16      echo
    17      exit 1;
    18  }
    19  
    20  is_number() {
    21      if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
    22        echo "Error: '$1' is not a number"; exit 1
    23      fi
    24  }
    25  
    26  get_setup() {
    27      if [ -f $"${SETUP_FILE}" ]; then
    28          source ${SETUP_FILE}
    29      else
    30          echo "No setup configuration found for your last docker deployment. Exiting..."
    31          exit 1
    32      fi
    33  }
    34  
    35  determine_config() {
    36      if [ "${NETWORK}" == "single" ]; then
    37          composer_file="${DIR}/docker-compose.singlenet.yml"
    38      elif [ "${NETWORK}" == "multi" ]; then
    39          composer_file="${DIR}/docker-compose.singlenet.yml -f ${DIR}/docker-compose.multinet.yml"
    40      else
    41          echo "ERROR: No docker configuration selected."
    42          usage
    43      fi
    44  }
    45  
    46  stop_quickstart() {
    47      container_id=`docker ps | grep ais-quickstart | awk '{ print $1 }'`
    48      if docker ps | grep ais-quickstart > /dev/null 2>&1; then
    49          docker rm --force $container_id
    50          echo "AIStore quickstart terminated"
    51      fi
    52      exit 1
    53  }
    54  
    55  if ! [ -x "$(command -v docker-compose)" ]; then
    56    echo 'Error: docker-compose is not installed.' >&2
    57    exit 1
    58  fi
    59  
    60  CLUSTER_CNT=1
    61  NETWORK=""
    62  REMOVE_IMAGES=false
    63  for i in "$@"
    64  do
    65  case $i in
    66      -c=*|--clustered=*)
    67          CLUSTER_CNT="${i#*=}"
    68          is_number CLUSTER_CNT
    69          NETWORK="multi"
    70          shift # past argument=value
    71          ;;
    72  
    73      -l|--last)
    74          get_setup
    75          shift
    76          ;;
    77  
    78      -s|--single)
    79          NETWORK="single"
    80          shift # past argument=value
    81          ;;
    82  
    83      -m|--multi)
    84          NETWORK="multi"
    85          shift # past argument=value
    86          ;;
    87  
    88      -qs|--quickstart)
    89          stop_quickstart
    90          shift
    91          ;;
    92  
    93      --rmi)
    94          REMOVE_IMAGES=true
    95          shift # past argument=value
    96          valid_log_file_type $LOG_TYPE
    97          ;;
    98  
    99      -s|--single)
   100          network="single"
   101          shift # past argument=value
   102          ;;
   103  
   104  
   105      *)
   106          usage
   107          ;;
   108  esac
   109  done
   110  
   111  determine_config
   112  
   113  if [ "$CLUSTER_CNT" -gt 1 ]; then
   114      echo "Removing connections between clusters..."
   115      for container_name in $(docker ps --format "{{.Names}}"); do
   116          container_id=$(docker ps -aqf "name=${container_name}")
   117          for ((i=0; i<${CLUSTER_CNT}; i++)); do
   118              if [[ $container_name != ais${i}_* ]]; then
   119                  docker network disconnect -f ais${i}_public $container_id
   120                  if [[ $container_name == *"_target_"* ]]; then
   121                      docker network disconnect -f ais${i}_internal_data $container_id
   122                  fi
   123              fi
   124          done
   125      done
   126  fi
   127  
   128  for ((i=0; i<${CLUSTER_CNT}; i++)); do
   129      export PUB_SUBNET="172.5$((0 + ($i * 3))).0.0/24"
   130      export INT_CONTROL_SUBNET="172.5$((1 + ($i * 3))).0.0/24"
   131      export INT_DATA_SUBNET="172.5$((2 + ($i * 3))).0.0/24"
   132      if [ "$REMOVE_IMAGES" == true ]; then
   133          docker-compose -p ais${i} -f $composer_file down -v --rmi all --remove-orphans
   134      else
   135          docker-compose -p ais${i} -f $composer_file down -v --remove-orphans
   136      fi
   137  done
   138  
   139  if [ "$remove_images" = TRUE ]; then
   140      docker-compose -f $composer_file down -v --rmi all --remove-orphans
   141  else
   142      docker-compose -f $composer_file down -v --remove-orphans
   143  fi
   144  
   145  echo "Removing volumes..."
   146  docker volume prune -f
   147  echo "Removing volumes folders..."
   148  for ((i=0; i<${CLUSTER_CNT}; i++)); do
   149      rm -rf /tmp/ais/${i}
   150  done
   151  
   152  # Remove CLI
   153  rm -f ${GOPATH}/bin/ais