github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/docker/up.sh (about)

     1  #!/usr/bin/env bash
     2  # "To provide additional docker-compose args, set the COMPOSE var. Ex:
     3  # COMPOSE="-f FILE_PATH_HERE"
     4  
     5  set -e # exit on an error
     6  
     7  ERROR(){
     8      /bin/echo -e "\e[101m\e[97m[ERROR]\e[49m\e[39m $@"
     9  }
    10  
    11  WARNING(){
    12      /bin/echo -e "\e[101m\e[97m[WARNING]\e[49m\e[39m $@"
    13  }
    14  
    15  INFO(){
    16      /bin/echo -e "\e[104m\e[97m[INFO]\e[49m\e[39m $@"
    17  }
    18  
    19  exists() {
    20      type $1 > /dev/null 2>&1
    21  }
    22  
    23  POSITIONAL=()
    24  while [[ $# -gt 0 ]]
    25  do
    26      key="$1"
    27  
    28      case $key in
    29          --help)
    30              HELP=1
    31              shift # past argument
    32              ;;
    33          --init-only)
    34              INIT_ONLY=1
    35              shift # past argument
    36              ;;
    37          --dev)
    38              if [ ! "$CHAOS_ROOT" ]; then
    39                  export CHAOS_ROOT=$(cd ../ && pwd)
    40                  INFO "CHAOS_ROOT is not set, defaulting to: $CHAOS_ROOT"
    41              fi
    42              INFO "Running docker-compose with dev config"
    43              DEV="-f docker-compose.dev.yml"
    44              shift # past argument
    45              ;;
    46          --compose)
    47              COMPOSE="-f $2"
    48              shift # past argument
    49              shift # past value
    50              ;;
    51          -d|--daemon)
    52              INFO "Running docker-compose as daemon"
    53              RUN_AS_DAEMON=1
    54              shift # past argument
    55              ;;
    56          *)
    57              POSITIONAL+=("$1")
    58              ERROR "unknown option $1"
    59              shift # past argument
    60              ;;
    61      esac
    62  done
    63  set -- "${POSITIONAL[@]}" # restore positional parameters
    64  
    65  if [ "$HELP" ]; then
    66      echo "Usage: $0 [OPTION]"
    67      echo "  --help                                                Display this message"
    68      echo "  --init-only                                           Initializes ssh-keys, but does not call docker-compose"
    69      echo "  --daemon                                              Runs docker-compose in the background"
    70      echo "  --dev                                                 Mounts dir at host's CHAOS_ROOT to /chaos on chaos-control container, syncing files for development"
    71      echo "  --compose PATH                                        Path to an additional docker-compose yml config."
    72      echo "To provide multiple additional docker-compose args, set the COMPOSE var directly, with the -f flag. Ex: COMPOSE=\"-f FILE_PATH_HERE -f ANOTHER_PATH\" ./up.sh --dev"
    73      exit 0
    74  fi
    75  
    76  exists ssh-keygen || { ERROR "Please install ssh-keygen (apt-get install openssh-client)"; exit 1; }
    77  exists perl || { ERROR "Please install perl (apt-get install perl)"; exit 1; }
    78  
    79  # Generate SSH keys for the control node
    80  if [ ! -f ./secret/node.env ]; then
    81      INFO "Generating key pair"
    82      ssh-keygen -t rsa -N "" -f ./secret/id_rsa
    83  
    84      INFO "Generating ./secret/control.env"
    85      echo "# generated by chaos/docker/up.sh, parsed by chaos/docker/control/bashrc" > ./secret/control.env
    86      echo "# NOTE: \\n is expressed as ↩" >> ./secret/control.env
    87      echo SSH_PRIVATE_KEY="$(cat ./secret/id_rsa | perl -p -e "s/\n/↩/g")" >> ./secret/control.env
    88      echo SSH_PUBLIC_KEY=$(cat ./secret/id_rsa.pub) >> ./secret/control.env
    89  
    90      INFO "Generating ./secret/node.env"
    91      echo "# generated by chaos/docker/up.sh, parsed by the \"tutum/debian\" docker image entrypoint script" > ./secret/node.env
    92      echo ROOT_PASS=root >> ./secret/node.env
    93      echo AUTHORIZED_KEYS=$(cat ./secret/id_rsa.pub) >> ./secret/node.env
    94  else
    95      INFO "No need to generate key pair"
    96  fi
    97  
    98  # Make sure folders referenced in control Dockerfile exist and don't contain leftover files
    99  rm -rf ./control/chaos
   100  mkdir ./control/chaos/
   101  # Copy the chaos directory if we're not mounting the CHAOS_ROOT
   102  if [ ! "$DEV" ]; then
   103      # Dockerfile does not allow `ADD ..`. So we need to copy it here in setup.
   104      INFO "Copying .. to control/chaos"
   105      (
   106          (cd ..; tar --exclude=./docker -cf - .)  | tar Cxf ./control/chaos -
   107      )
   108  fi
   109  
   110  if [ "$INIT_ONLY" ]; then
   111      exit 0
   112  fi
   113  
   114  exists docker || { ERROR "Please install docker (https://docs.docker.com/engine/installation/)"; exit 1; }
   115  exists docker-compose || { ERROR "Please install docker-compose (https://docs.docker.com/compose/install/)"; exit 1; }
   116  
   117  INFO "Running \`docker-compose build\`"
   118  docker-compose -f docker-compose.yml $COMPOSE $DEV build
   119  
   120  INFO "Running \`docker-compose up\`"
   121  if [ "$RUN_AS_DAEMON" ]; then
   122      docker-compose -f docker-compose.yml $COMPOSE $DEV up -d
   123      INFO "All containers started, run \`docker ps\` to view"
   124      exit 0
   125  else
   126      INFO "Please run \`docker exec -it chaos-control bash\` in another terminal to proceed"
   127      docker-compose -f docker-compose.yml $COMPOSE $DEV up
   128  fi