github.com/m3db/m3@v1.5.0/scripts/vagrant/multi/start_vagrant.sh (about)

     1  #!/bin/bash
     2  
     3  set -xe
     4  
     5  export BOX="ubuntu/xenial64"
     6  PROVIDER=${PROVIDER:-virtualbox}
     7  
     8  if [[ "$PROVIDER" != "virtualbox" ]]; then
     9      if [[ "$USER" == "" ]]; then
    10          echo "USER env var not set"
    11          exit 1
    12      fi
    13      if [[ "$SSH_KEY" == "" ]]; then
    14          echo "SSH_KEY env var not set"
    15          exit 1
    16      fi
    17  fi
    18  
    19  if [[ "$PROVIDER" == "google" ]]; then
    20      export BOX="google/gce"
    21  
    22      if [[ "$GOOGLE_PROJECT_ID" == "" ]]; then
    23          echo "GOOGLE_PROJECT_ID env var not set"
    24          exit 1
    25      fi
    26      if [[ "$GOOGLE_JSON_KEY_LOCATION" == "" ]]; then
    27          echo "GOOGLE_JSON_KEY_LOCATION env var not set"
    28          exit 1
    29      fi
    30  
    31      # Create ingress rules if not already exists.
    32      MAYBE_M3COORDINATOR_INGRESS=$(gcloud --project=studious-saga-237001 compute firewall-rules list 2> /dev/null | tail -n +2 | awk '{ print $1 };' | grep default-allow-m3coordinator) || true
    33      if  [ "$MAYBE_M3COORDINATOR_INGRESS" != "default-allow-m3coordinator" ]; then
    34          gcloud --project=$GOOGLE_PROJECT_ID compute firewall-rules create default-allow-m3coordinator \
    35              --action=allow \
    36              --rules=tcp:7201 \
    37              --direction=ingress \
    38              --target-tags=network-m3coordinator
    39      fi
    40  fi
    41  
    42  if [[ "$PROVIDER" == "azure" ]]; then
    43      export BOX="azure"
    44  
    45      if [[ "$AZURE_TENANT_ID" == "" ]]; then
    46          echo "AZURE_TENANT_ID env var not set"
    47          exit 1
    48      fi
    49      if [[ "$AZURE_CLIENT_ID" == "" ]]; then
    50          echo "AZURE_CLIENT_ID env var not set"
    51          exit 1
    52      fi
    53      if [[ "$AZURE_CLIENT_SECRET" == "" ]]; then
    54          echo "AZURE_CLIENT_SECRET env var not set"
    55          exit 1
    56      fi
    57      if [[ "$AZURE_SUBSCRIPTION_ID" == "" ]]; then
    58          echo "AZURE_SUBSCRIPTION_ID env var not set"
    59          exit 1
    60      fi
    61  
    62      # Group numbers
    63      if [[ "$GROUP0" == "" ]]; then
    64          export GROUP0="0"
    65      fi
    66      if [[ "$GROUP1" == "" ]]; then
    67          export GROUP1="1"
    68      fi
    69      if [[ "$GROUP2" == "" ]]; then
    70          export GROUP2="2"
    71      fi
    72  
    73      # Create resource groups if not already exists. There should be three (primary/secondary/benchmarker).
    74      function create_resource_group_if_not_exists() {
    75          RESOURCE_GROUP=$1
    76          MAYBE_RESOURCE_GROUP=$(az group list -o table 2> /dev/null | tail -n +3 | awk '{ print $1 };' | grep "\<$RESOURCE_GROUP\>") || true
    77          if  [[ "$MAYBE_RESOURCE_GROUP" != "$RESOURCE_GROUP" ]]; then
    78              az group create -n $RESOURCE_GROUP -l eastus
    79          fi
    80      }
    81      create_resource_group_if_not_exists vagrant-dev$GROUP0
    82      create_resource_group_if_not_exists vagrant-dev$GROUP1
    83      create_resource_group_if_not_exists vagrant-dev$GROUP2
    84  fi
    85  
    86  if [[ "$FEATURE_DOCKER_IMAGE" == "" ]]; then
    87      echo "FEATURE_DOCKER_IMAGE env var not set"
    88      exit 1
    89  fi
    90  
    91  # Bring up boxes
    92  echo "Provision boxes"
    93  vagrant up --provider $PROVIDER
    94  
    95  # NB(bodu): We do this later because the network nsg gets automatically created by the vagrant plugin in the
    96  # form of `${azure.nsg_name}-vagrantNSG`.
    97  if [[ "$PROVIDER" == "azure" ]]; then
    98      function create_ingress_if_not_exists() {
    99          RESOURCE_GROUP=$1
   100          MAYBE_GROUP0_M3COORDINATOR_INGRESS=$(az network nsg rule list -g $RESOURCE_GROUP --nsg-name network-m3coordinator-vagrantNSG -o table 2> /dev/null | tail -n +3 | awk '{ print $1 };' | grep m3coordinator) || true
   101          if  [ "$MAYBE_GROUP0_M3COORDINATOR_INGRESS" != "default-allow-m3coordinator" ]; then
   102              az network nsg rule create -n default-allow-m3coordinator --nsg-name network-m3coordinator-vagrantNSG \
   103                  --access Allow \
   104                  --protocol Tcp \
   105                  --direction Inbound \
   106                  --destination-port-ranges 7201 \
   107                  --priority 100 \
   108                  -g $RESOURCE_GROUP
   109          fi
   110      }
   111      # Create m3coordinator ingress rules if not already exists.
   112      create_ingress_if_not_exists vagrant-dev$GROUP0
   113      create_ingress_if_not_exists vagrant-dev$GROUP1
   114  fi
   115  
   116  # Get primary/secondary external IP addresses
   117  if [[ "$PROVIDER" == "google" ]]; then
   118      M3COORDINATOR_PRIMARY_IP=$(gcloud --project=studious-saga-237001 compute instances list | grep "\<primary-$USER$GROUP0\>" | awk '{ print $5 }')
   119      M3COORDINATOR_SECONDARY_IP=$(gcloud --project=studious-saga-237001 compute instances list | grep "\<secondary-$USER$GROUP1\>" | awk '{ print $5 }')
   120  fi
   121  if [[ "$PROVIDER" == "azure" ]]; then
   122      M3COORDINATOR_PRIMARY_IP=$(az vm list-ip-addresses -o table | grep primary-$USER$GROUP0 | awk '{ print $2 }')
   123      M3COORDINATOR_SECONDARY_IP=$(az vm list-ip-addresses -o table | grep secondary-$USER$GROUP1 | awk '{ print $2 }')
   124  fi
   125  
   126  # Provision clusters
   127  echo "Provision k8s clusters"
   128  vagrant ssh benchmarker -c "cd provision && M3COORDINATOR_PRIMARY_IP=$M3COORDINATOR_PRIMARY_IP M3COORDINATOR_SECONDARY_IP=$M3COORDINATOR_SECONDARY_IP ./setup_kube_bench.sh" &
   129  vagrant ssh secondary -c "cd provision && MACHINE=secondary FEATURE_DOCKER_IMAGE=$FEATURE_DOCKER_IMAGE ./setup_kube.sh" &
   130  vagrant ssh primary -c "cd provision && MACHINE=primary ./setup_kube.sh"
   131  
   132  # Run rolling restart forever
   133  vagrant ssh secondary -c "cd provision && nohup ./rolling_restart_dbnodes.sh & sleep 1"
   134  vagrant ssh primary -c "cd provision && nohup ./rolling_restart_dbnodes.sh & sleep 1"