#!/bin/bash

# set -x

# create-cluster.sh 
# @todo: 1) Retrieve valid cluster version from GCP to prepopulate a valid version
#           as these versions are highly dynamic and change frequently.
#        2) Display available ZONE/Region values to select from
#        3) Display available Machine Types to select from
#        4) Check if resources exist before attempting to create. Exit if so.
#        5) Check for successful operation of each step and exit if any steps fail
#           (As subsequent steps will likely fail anyway as the generally require
#            the failed resources that should have been created)
#        6) Eventually allow script to delete resources so the console isn't required


# create_vm "VM Name" "VM Node Machine Type" "VM Disk Size" "VM Disk Type" IP_VARIABLE
# VM Node Machine Type: e2-custom-4-8192 | e2-custom-16-32768 | e2-standard-4 | ...
# VM Disk Type: pd-balanced | pd-ssd
# EXT_IP_VARIABLE: pass the env variable that will be updated with the VM's external IP
# INT_IP_VARIABLE: pass the env variable that will be updated with the VM's internal IP
create_vm() {
	VM_IP=$(gcloud compute instances create ${1} \
	  --format='get(networkInterfaces[0].accessConfigs[0].natIP)' \
	  --project=${CLUSTER_PROJECT} \
	  --zone=${CLUSTER_ZONE1}-a --machine-type=${2} \
	  --subnet=${CLUSTER_NAME}-subnet-${CLUSTER_ZONE1} \
	  --network-tier=PREMIUM --maintenance-policy=MIGRATE \
	  --service-account=958456156385-compute@developer.gserviceaccount.com \
	  --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append \
	  --labels=owner=${USER},ec-src=vm_add-gcloud \
	  --create-disk=auto-delete=yes,boot=yes,device-name=disk-root,image=${CLUSTER_DISK_IMAGE},mode=rw,size=20,type=projects/${CLUSTER_PROJECT}/zones/${CLUSTER_ZONE1}-a/diskTypes/pd-balanced \
	  --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring \
	  --reservation-affinity=any \
	  --metadata=ssh-keys=${USER}:"${SSH_KEY}",startup-script='yum -q -y install numactl irqbalance && service irqbalance start')
	eval "$5=${VM_IP}"
	VM_INT_IP=$(gcloud compute instances list --filter="name:( ${1} )" --format='get(networkInterfaces[0].networkIP)')
	eval "$6=${VM_INT_IP}"
}

# dependencies_vm "hostname" "disk_size in gb [0=no data disk needed]"
# Node Type: PD/DB/KV/Flash
# Node Type Num: 1, 2, 5, etc
# VM_INT_IP will be set to the node's internal IP address on exit (to be used for tiup topography file)
dependencies_vm() {
  HOSTNAME=${1}
  HOST_DISK_SIZE=${2}
  if [ ${HOST_DISK_SIZE} -gt 0 ]; then
    gcloud compute disks create ${HOSTNAME}-disk-data --size ${HOST_DISK_SIZE} --zone ${CLUSTER_ZONE1}-a --type pd-ssd
  fi
  # gcloud compute ssh ${HOSTNAME} --zone ${CLUSTER_ZONE1}-a --command="sudo yum -q -y install numactl irqbalance && sudo service irqbalance start"
  if [ ${HOST_DISK_SIZE} -gt 0 ]; then
    gcloud compute instances attach-disk ${HOSTNAME} --disk ${HOSTNAME}-disk-data --zone ${CLUSTER_ZONE1}-a
    gcloud compute ssh ${HOSTNAME} --zone ${CLUSTER_ZONE1}-a --command="sudo mkfs.ext4 -F -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sdb && sudo mkdir /data && sudo mount -o discard,defaults,noatime,nodelalloc /dev/sdb /data"
  fi
  gcloud compute ssh ${HOSTNAME} --zone ${CLUSTER_ZONE1}-a --command="mkdir -p -m 700 ~/.ssh ; echo ${TIUP_SSH_KEY} >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys"
}

CLUSTER_PROJECT="gcp-na-emea-presales"
CLUSTER_NAME="${USER}-tiup-demo"
CLUSTER_ZONE1="us-west1"
CLUSTER_ZONE2="us-east1"
unset CLUSTER_ZONE2
MY_IP=$(curl -s ifconfig.me)
SSH_KEY=$(cat ~/.ssh/id_rsa.pub)

# VM Operating Systems
# CentOS 7
# CLUSTER_NODE_IMAGE=projects/centos-cloud/global/images/centos-7-v20230711
# CLUSTER_NODE_IMAGE=$(gcloud compute images list --filter="family:centos-7" --format='get(name)')
# CLUSTER_NODE_IMAGE=$(gcloud compute images list --filter="family~rocky-linux-9-optimized-gcp$" --format='get(name)')
CLUSTER_DISK_IMAGE=$(gcloud compute images list --filter="family~rocky-linux-9-optimized-gcp$" --format=json | jq -r .[].selfLink | sed "s-https://www..*/v[0-9]/projects-projects-")

#
# Ubuntu 20
# CLUSTER_NODE_IMAGE=projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20230302
# CLUSTER_NODE_IMAGE=$(gcloud compute images list --filter="family:ubuntu-2004-lts" --format='get(name)')
#
# Ubuntu 18
# CLUSTER_NODE_IMAGE=projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-v20230405
#
# Fedora Cloud Base 37/38
# CLUSTER_NODE_IMAGE=projects/fedora-cloud/global/images/fedora-cloud-base-gcp-37-1-7-x86-64
# CLUSTER_NODE_IMAGE=$(gcloud compute images list --filter="family:fedora-cloud-37" --format='get(name)')
# CLUSTER_NODE_IMAGE=$(gcloud compute images list --filter="family:fedora-cloud-38" --format='get(name)')


CLUSTER_PD_COUNT=3
# CLUSTER_PD_MACHINE_TYPE="e2-custom-4-8192"
CLUSTER_PD_MACHINE_TYPE="e2-standard-4"
CLUSTER_PD_DISK_SIZE=0
CLUSTER_PD_DISK_TYPE=pd-balanced
CLUSTER_TIDB_COUNT=2
# CLUSTER_TIDB_MACHINE_TYPE="e2-custom-16-32768"
CLUSTER_TIDB_MACHINE_TYPE="e2-standard-16"
CLUSTER_TIDB_DISK_SIZE=0
CLUSTER_TIDB_DISK_TYPE=pd-balanced
CLUSTER_TIKV_COUNT=3
# CLUSTER_TIKV_MACHINE_TYPE="e2-custom-16-32768"
CLUSTER_TIKV_MACHINE_TYPE="e2-standard-16"
CLUSTER_TIKV_DISK_SIZE=50
CLUSTER_TIKV_DISK_TYPE=pd-ssd
CLUSTER_TIFLASH_COUNT=0
# CLUSTER_TIFLASH_MACHINE_TYPE="e2-custom-32-65536"
CLUSTER_TIFLASH_MACHINE_TYPE="e2-standard-32"
CLUSTER_TIFLASH_DISK_SIZE=200
CLUSTER_TIFLASH_DISK_TYPE=pd-ssd
CLUSTER_MONITOR_COUNT=1
# CLUSTER_MONITOR_MACHINE_TYPE="e2-custom-4-8192"
CLUSTER_MONITOR_MACHINE_TYPE="e2-standard-4"
CLUSTER_MONITOR_DISK_SIZE=0
CLUSTER_MONITOR_DISK_TYPE=pd-balanced
CLUSTER_PUMP_COUNT=0
CLUSTER_PUMP_MACHINE_TYPE="e2-standard-8"
CLUSTER_PUMP_DISK_SIZE=100
CLUSTER_PUMP_DISK_TYPE=pd-balanced
CLUSTER_DRAINER_COUNT=0
CLUSTER_DRAINER_TYPE="e2-standard-8"
CLUSTER_DRAINER_MACHINE_TYPE="e2-standard-8"
CLUSTER_DRAINER_DISK_SIZE=100
CLUSTER_DRAINER_DISK_TYPE=pd-balanced


# CLUSTER_TIUP_MACHINE_TYPE="e2-medium"
CLUSTER_TIUP_MACHINE_TYPE="e2-medium"
CLUSTER_TIUP_DISK_SIZE=20
CLUSTER_TIUP_DISK_TYPE=pd-balanced

echo "================================================================================"
echo -n "Retrieving region list..."
# regions=($(gcloud compute regions list | sed "1d;s/ ..*//"))
regions=($(gcloud compute regions list --format='get(name)'))
echo " done"
echo "--------------------------------------------------------------------------------"
echo -n "** Getting recent versions of TiDB..."
TIDB_VERSIONS=($(tiup list tidb | egrep -v 'alpha|beta' | tail -12 | sed 's/ ..*//'))
num_ver=$((${#TIDB_VERSIONS[@]}))
TIDB_VERSION="${TIDB_VERSIONS[$((num_ver - 1))]}"
echo " done"
echo "--------------------------------------------------------------------------------"

echo -n "GCP Project Name (${CLUSTER_PROJECT}): "
read USER_CLUSTER_PROJECT
if [ "x${USER_CLUSTER_PROJECT}" != "x" ]; then
	CLUSTER_PROJECT=${USER_CLUSTER_PROJECT}
fi

echo -n "GKE Cluster Name (${CLUSTER_NAME}): "
read USER_CLUSTER_NAME
if [ "x${USER_CLUSTER_NAME}" != "x" ]; then
	CLUSTER_NAME=${USER_CLUSTER_NAME}
fi

# Set up project and create network resources
gcloud config set project ${CLUSTER_PROJECT}

echo -e "\n--------------------------------------------------------------------------------"
echo "Region list..."
echo "--------------------------------------------------------------------------------"

reg_num=0
while [ ${reg_num} -le ${#regions[@]} ]; do
	printf "  %-27s%-27s%-27s\n" \
		"${regions[$((reg_num + 0))]}" \
		"${regions[$((reg_num + 1))]}" \
		"${regions[$((reg_num + 2))]}"
	reg_num=$((reg_num+3))
done

echo -n "Zone 1 (${CLUSTER_ZONE1}): "
read USER_CLUSTER_ZONE1
if [ "x${USER_CLUSTER_ZONE1}" != "x" ]; then
	CLUSTER_ZONE1=${USER_CLUSTER_ZONE1}
fi

echo "If a second cluster (i.e. for TiCDC demo) is required, "
echo "    uncomment CLUSTER_ZONE2 in script."


echo -e "\n--------------------------------------------------------------------------------"
echo "TiDB version list..."
echo "--------------------------------------------------------------------------------"

ver_num=0
while [ ${ver_num} -le $num_ver ]; do
	printf "  %-27s%-27s%-27s\n" \
		"${TIDB_VERSIONS[$((ver_num + 0))]}" \
		"${TIDB_VERSIONS[$((ver_num + 1))]}" \
		"${TIDB_VERSIONS[$((ver_num + 2))]}"
	ver_num=$((ver_num+3))
done

echo -n "Select TiDB Version to install (${TIDB_VERSION}): "
read USER_TIDB_VERSION
if [ "x${USER_TIDB_VERSION}" != "x" ]; then
  TIDB_VERSION=${USER_TIDB_VERSION}
fi

# echo -n "GKE Cluster Version (${CLUSTER_VERSION}): "
# read USER_CLUSTER_VERSION
# if [ "x${USER_CLUSTER_VERSION}" != "x" ]; then
# 	CLUSTER_VERSION=${USER_CLUSTER_VERSION}
# fi
# 
# echo -n "GKE Cluster Node Count (${CLUSTER_NODE_COUNT}): "
# read USER_CLUSTER_NODE_COUNT
# if [ "x${USER_CLUSTER_NODE_COUNT}" != "x" ]; then
# 	CLUSTER_NODE_COUNT=${USER_CLUSTER_NODE_COUNT}
# fi
# 
# echo -n "GKE Cluster Disk Size (GB) (${CLUSTER_NODE_DISK_SIZE}): "
# read USER_CLUSTER_NODE_DISK_SIZE
# if [ "x${USER_CLUSTER_NODE_DISK_SIZE}" != "x" ]; then
# 	CLUSTER_NODE_DISK_SIZE=${USER_CLUSTER_NODE_DISK_SIZE}
# fi
# 
# echo -n "GKE Cluster Machine Type (${CLUSTER_NODE_MACHINE_TYPE}): "
# read USER_CLUSTER_NODE_MACHINE_TYPE
# if [ "x${USER_CLUSTER_NODE_MACHINE_TYPE}" != "x" ]; then
# 	CLUSTER_NODE_MACHINE_TYPE=${USER_CLUSTER_NODE_MACHINE_TYPE}
# fi

echo "CLUSTER_PROJECT: ${CLUSTER_PROJECT}"
echo "CLUSTER_NAME: ${CLUSTER_NAME}"
echo "CLUSTER_ZONE1: ${CLUSTER_ZONE1}"
echo "CLUSTER_ZONE2: ${CLUSTER_ZONE2}"
# echo "CLUSTER_VERSION: ${CLUSTER_VERSION}"
echo "MY_IP: ${MY_IP}"

# echo "CLUSTER_NODE_COUNT: ${CLUSTER_NODE_COUNT}"
# echo "CLUSTER_NODE_DISK_SIZE: ${CLUSTER_NODE_DISK_SIZE}"
# echo "CLUSTER_NODE_MACHINE_TYPE: ${CLUSTER_NODE_MACHINE_TYPE}"

echo " "
echo "** Creating Network..."
printf '**     Checking to see if %s-network exists...' "${CLUSTER_NAME}"
gcloud compute networks list --filter="name=(${CLUSTER_NAME}-network)" 2>/dev/null | grep ${CLUSTER_NAME}-network >/dev/null
if [ $? -ne 0 ]; then
  echo "  Network does not exist. Creating..."
  gcloud compute networks create ${CLUSTER_NAME}-network --subnet-mode custom --no-user-output-enabled

  echo "** Creating Network Subnet..."
  gcloud compute networks subnets create ${CLUSTER_NAME}-subnet-${CLUSTER_ZONE1} \
    --network ${CLUSTER_NAME}-network \
    --region ${CLUSTER_ZONE1} \
    --no-user-output-enabled \
    --range 10.16.0.0/12

  echo "** Creating Firewall Rule 1 of 5..."
  gcloud compute firewall-rules create ${CLUSTER_NAME}-network-allow-all-private-ingress \
    --network ${CLUSTER_NAME}-network \
    --direction INGRESS \
    --source-ranges 10.0.0.0/8 \
    --no-user-output-enabled \
    --allow all

  echo "** Creating Firewall Rule 2 of 5..."
  gcloud compute firewall-rules create ${CLUSTER_NAME}-network-allow-all-private-egress \
    --network ${CLUSTER_NAME}-network \
    --direction EGRESS \
    --source-ranges 10.0.0.0/8 \
    --no-user-output-enabled \
    --allow all

  # Now open all ports from your IP and open ports within the GCP network with firewall rules
  echo "** Creating Firewall Rule 3 of 5..."
  gcloud compute --project=${CLUSTER_PROJECT} firewall-rules \
    create ${CLUSTER_NAME}-app-firewall-rule-ingress \
    --direction=INGRESS --priority=1000 --network=${CLUSTER_NAME}-network \
    --no-user-output-enabled \
    --action=ALLOW --rules=all --source-ranges=${MY_IP}

  echo "** Creating Firewall Rule 4 of 5..."
  gcloud compute --project=${CLUSTER_PROJECT} firewall-rules \
    create ${CLUSTER_NAME}-app-firewall-rule-egress \
    --direction=EGRESS --priority=1000 --network=${CLUSTER_NAME}-network \
    --no-user-output-enabled \
    --action=ALLOW --rules=all --source-ranges=${MY_IP}

  echo "** Creating Firewall Rule 5 of 5..."
  gcloud compute --project=${CLUSTER_PROJECT} firewall-rules create ${CLUSTER_NAME}-tidb-rule-ingress \
    --direction=INGRESS --priority=1000 --network=${CLUSTER_NAME}-network --action=ALLOW \
    --no-user-output-enabled \
    --rules=tcp:2379-2380,tcp:3000,tcp:3930,tcp:4000,tcp:8123,tcp:8234,tcp:8249-8250,tcp:8300,tcp:9000,tcp:9090,tcp:9093-9094,tcp:9100,tcp:9115,tcp:10080,tcp:12020,tcp:20160,tcp:20170,tcp:20180,tcp:20292 \
    --source-ranges=0.0.0.0/0
  gcloud compute --project=${CLUSTER_PROJECT} firewall-rules create ${CLUSTER_NAME}-tidb-rule-egress \
    --direction=EGRESS --priority=1000 --network=${CLUSTER_NAME}-network --action=ALLOW \
    --no-user-output-enabled \
    --rules=tcp:2379-2380,tcp:3000,tcp:3930,tcp:4000,tcp:8123,tcp:8234,tcp:8249-8250,tcp:8300,tcp:9000,tcp:9090,tcp:9093-9094,tcp:9100,tcp:9115,tcp:10080,tcp:12020,tcp:20160,tcp:20170,tcp:20180,tcp:20292 \
    --destination-ranges=0.0.0.0/0
else
  echo " Network exists. Skipping creation..."
fi

echo "** Generating topology template..."
TIUP_YAML=tiup_topology.yaml
cat > ${TIUP_YAML} <<++
global:
  user: "tidb"
  ssh_port: 22
  deploy_dir: "/data/tidb-deploy"
  data_dir: "/data/tidb-data"

++

echo "** Creating TiUP VM..."
create_vm ${CLUSTER_NAME}-tiup-vm-1 ${CLUSTER_TIUP_MACHINE_TYPE} ${CLUSTER_TIUP_DISK_SIZE} ${CLUSTER_TIUP_DISK_TYPE} TIUP_VM_EXT_IP TIUP_VM_INT_IP

VM_NUM=1
if [ ${CLUSTER_PD_COUNT} -gt 0 ]; then
	echo "pd_servers:" >> ${TIUP_YAML}
	while [ ${VM_NUM} -le ${CLUSTER_PD_COUNT} ]; do
		echo "** Creating PD VM Instance ${VM_NUM} of ${CLUSTER_PD_COUNT} **"
		create_vm ${CLUSTER_NAME}-pd-vm-${VM_NUM} ${CLUSTER_PD_MACHINE_TYPE} ${CLUSTER_PD_DISK_SIZE} ${CLUSTER_PD_DISK_TYPE} VM_EXT_IP VM_IP
		echo "   PD ${VM_NUM} IP: ${VM_IP}"
		echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
		VM_NUM=$(expr ${VM_NUM} + 1)
	done
	echo "" >> ${TIUP_YAML}
fi

VM_NUM=1
if [ ${CLUSTER_TIDB_COUNT} -gt 0 ]; then
	echo "tidb_servers:" >> ${TIUP_YAML}
	while [ ${VM_NUM} -le ${CLUSTER_TIDB_COUNT} ]; do
		echo "** Creating TiDB VM Instance ${VM_NUM} of ${CLUSTER_TIDB_COUNT} **"
		create_vm ${CLUSTER_NAME}-tidb-vm-${VM_NUM} ${CLUSTER_TIDB_MACHINE_TYPE} ${CLUSTER_TIDB_DISK_SIZE} ${CLUSTER_TIDB_DISK_TYPE} VM_EXT_IP VM_IP
		echo "   TiDB ${VM_NUM} IP: ${VM_IP}"
		echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
		VM_NUM=$(expr ${VM_NUM} + 1)
	done
	echo "" >> ${TIUP_YAML}
fi

VM_NUM=1
if [ ${CLUSTER_TIKV_COUNT} -gt 0 ]; then
	echo "tikv_servers:" >> ${TIUP_YAML}
	while [ ${VM_NUM} -le ${CLUSTER_TIKV_COUNT} ]; do
		echo "** Creating TiKV VM Instance ${VM_NUM} of ${CLUSTER_TIKV_COUNT} **"
		create_vm ${CLUSTER_NAME}-tikv-vm-${VM_NUM} ${CLUSTER_TIKV_MACHINE_TYPE} ${CLUSTER_TIKV_DISK_SIZE} ${CLUSTER_TIKV_DISK_TYPE} VM_EXT_IP VM_IP
		echo "   TiKV ${VM_NUM} IP: ${VM_IP}"
		echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
		VM_NUM=$(expr ${VM_NUM} + 1)
	done
	echo "" >> ${TIUP_YAML}
fi

VM_NUM=1
if [ ${CLUSTER_MONITOR_COUNT} -gt 0 ]; then
	echo "monitoring_servers:" >> ${TIUP_YAML}
	while [ ${VM_NUM} -le ${CLUSTER_MONITOR_COUNT} ]; do
		echo "** Creating Monitor VM Instance ${VM_NUM} of ${CLUSTER_MONITOR_COUNT} **"
		create_vm ${CLUSTER_NAME}-monitor-vm-${VM_NUM} ${CLUSTER_MONITOR_MACHINE_TYPE} ${CLUSTER_MONITOR_DISK_SIZE} ${CLUSTER_MONITOR_DISK_TYPE} VM_EXT_IP VM_IP
		echo "   Monitor ${VM_NUM} IP: ${VM_IP}"
		echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
		VM_NUM=$(expr ${VM_NUM} + 1)
	done
	echo "" >> ${TIUP_YAML}
	echo "grafana_servers:"  >> ${TIUP_YAML}
	echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
	echo "" >> ${TIUP_YAML}
	echo "alertmanager_servers:"  >> ${TIUP_YAML}
	echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
	echo "" >> ${TIUP_YAML}
fi

VM_NUM=1
if [ ${CLUSTER_TIFLASH_COUNT} -gt 0 ]; then
	echo "tiflash_servers:" >> ${TIUP_YAML}
	while [ ${VM_NUM} -le ${CLUSTER_TIFLASH_COUNT} ]; do
		echo "** Creating TiFLASH VM Instance ${VM_NUM} of ${CLUSTER_TIFLASH_COUNT} **"
		create_vm ${CLUSTER_NAME}-tiflash-vm-${VM_NUM} ${CLUSTER_TIFLASH_MACHINE_TYPE} ${CLUSTER_TIFLASH_DISK_SIZE} ${CLUSTER_TIFLASH_DISK_TYPE} VM_EXT_IP VM_IP
		echo "   TiFlash ${VM_NUM} IP: ${VM_IP}"
		echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
		# echo "    data_dir: /tidb-data/tiflash-9000" >> ${TIUP_YAML}
		# echo "    deploy_dir: /tidb-data/tiflash-9000" >> ${TIUP_YAML}
		VM_NUM=$(expr ${VM_NUM} + 1)
	done
	echo "" >> ${TIUP_YAML}
fi

VM_NUM=1
if [ ${CLUSTER_PUMP_COUNT} -gt 0 ]; then
	echo "pump_servers:" >> ${TIUP_YAML}
	while [ ${VM_NUM} -le ${CLUSTER_PUMP_COUNT} ]; do
		echo "** Creating TiDB Binlog PUMP VM Instance ${VM_NUM} of ${CLUSTER_PUMP_COUNT} **"
		create_vm ${CLUSTER_NAME}-pump-vm-${VM_NUM} ${CLUSTER_PUMP_MACHINE_TYPE} ${CLUSTER_PUMP_DISK_SIZE} ${CLUSTER_PUMP_DISK_TYPE} VM_EXT_IP VM_IP
		echo "   TiBinLog Pump ${VM_NUM} IP: ${VM_IP}"
		echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
		# echo "    data_dir: /tidb-data/tiflash-9000" >> ${TIUP_YAML}
		# echo "    deploy_dir: /tidb-data/tiflash-9000" >> ${TIUP_YAML}
		VM_NUM=$(expr ${VM_NUM} + 1)
	done
	echo "" >> ${TIUP_YAML}
fi

VM_NUM=1
if [ ${CLUSTER_DRAINER_COUNT} -gt 0 ]; then
	echo "drainer_servers:" >> ${TIUP_YAML}
	while [ ${VM_NUM} -le ${CLUSTER_DRAINER_COUNT} ]; do
		echo "** Creating Ti-binlog Drainer VM Instance ${VM_NUM} of ${CLUSTER_DRAINER_COUNT} **"
		create_vm ${CLUSTER_NAME}-drainer-vm-${VM_NUM} ${CLUSTER_DRAINER_MACHINE_TYPE} ${CLUSTER_DRAINER_DISK_SIZE} ${CLUSTER_DRAINER_DISK_TYPE} VM_EXT_IP VM_IP
		echo "   Ti-BinLog VM ${VM_NUM} IP: ${VM_IP}"
		echo "  - host: ${VM_IP}" >> ${TIUP_YAML}
		VM_NUM=$(expr ${VM_NUM} + 1)
	done
	echo "    data_dir: \"/data\"" >> ${TIUP_YAML}
	echo "    config:" >> ${TIUP_YAML}
	echo "      syncer.db-type: \"file\"" >> ${TIUP_YAML}
	echo "" >> ${TIUP_YAML}
fi

echo "** CLUSTER INFRASTRUCTURE BUILD COMPLETE **"
echo "* * * * * * * * * * * * * * * * * * * * * *"

printf "** Generating TiUP SSH Key..."
gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone ${CLUSTER_ZONE1}-a --command='ssh-keygen -f ~/.ssh/id_rsa -t rsa -q -N ""'
TIUP_SSH_KEY=$(gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone ${CLUSTER_ZONE1}-a --command='cat ~/.ssh/id_rsa.pub')
printf "\nTiUP SSH KEY: %s\n\n" "${TIUP_SSH_KEY}"

echo "** Mounting disks on Nodes and installing dependencies"

echo "** PD Nodes..."
VM_NUM=1
if [ ${CLUSTER_PD_COUNT} -gt 0 ]; then
  while [ ${VM_NUM} -le ${CLUSTER_PD_COUNT} ]; do
    echo "** Working PD VM Instance ${VM_NUM} of ${CLUSTER_PD_COUNT} **"
    HOSTNAME=${CLUSTER_NAME}-pd-vm-${VM_NUM}
    dependencies_vm ${HOSTNAME} ${CLUSTER_PD_DISK_SIZE} ${CLUSTER_PD_DISK_TYPE}
    VM_NUM=$(expr ${VM_NUM} + 1)
  done
fi

echo "** TiDB Nodes..."
VM_NUM=1
if [ ${CLUSTER_TIDB_COUNT} -gt 0 ]; then
  while [ ${VM_NUM} -le ${CLUSTER_TIDB_COUNT} ]; do
    echo "** Working TiDB VM Instance ${VM_NUM} of ${CLUSTER_TIDB_COUNT} **"
    HOSTNAME=${CLUSTER_NAME}-tidb-vm-${VM_NUM}
    dependencies_vm ${HOSTNAME} ${CLUSTER_TIDB_DISK_SIZE} ${CLUSTER_TIDB_DISK_TYPE}
    VM_NUM=$(expr ${VM_NUM} + 1)
  done
fi

echo "** TiKV Nodes..."
VM_NUM=1
if [ ${CLUSTER_TIKV_COUNT} -gt 0 ]; then
  while [ ${VM_NUM} -le ${CLUSTER_TIKV_COUNT} ]; do
    echo "** Working TiKV VM Instance ${VM_NUM} of ${CLUSTER_TIKV_COUNT} **"
    HOSTNAME=${CLUSTER_NAME}-tikv-vm-${VM_NUM}
    dependencies_vm ${HOSTNAME} ${CLUSTER_TIKV_DISK_SIZE} ${CLUSTER_TIKV_DISK_TYPE}
    VM_NUM=$(expr ${VM_NUM} + 1)
  done
fi

echo "** TiFlash Nodes..."
VM_NUM=1
if [ ${CLUSTER_TIFLASH_COUNT} -gt 0 ]; then
  while [ ${VM_NUM} -le ${CLUSTER_TIFLASH_COUNT} ]; do
    echo "** Working TiFlash VM Instance ${VM_NUM} of ${CLUSTER_TIFLASH_COUNT} **"
    HOSTNAME=${CLUSTER_NAME}-tiflash-vm-${VM_NUM}
    dependencies_vm ${HOSTNAME} ${CLUSTER_TIFLASH_DISK_SIZE} ${CLUSTER_TIFLASH_DISK_TYPE}
    VM_NUM=$(expr ${VM_NUM} + 1)
  done
fi

echo "** Monitor Nodes..."
VM_NUM=1
if [ ${CLUSTER_MONITOR_COUNT} -gt 0 ]; then
  while [ ${VM_NUM} -le ${CLUSTER_MONITOR_COUNT} ]; do
    echo "** Working Monitor VM Instance ${VM_NUM} of ${CLUSTER_MONITOR_COUNT} **"
    HOSTNAME=${CLUSTER_NAME}-monitor-vm-${VM_NUM}
    dependencies_vm ${HOSTNAME} ${CLUSTER_MONITOR_DISK_SIZE} ${CLUSTER_MONITOR_DISK_TYPE}
    VM_NUM=$(expr ${VM_NUM} + 1)
  done
fi

echo "** Pump Nodes..."
VM_NUM=1
if [ ${CLUSTER_PUMP_COUNT} -gt 0 ]; then
  while [ ${VM_NUM} -le ${CLUSTER_PUMP_COUNT} ]; do
    echo "** Working Pump VM Instance ${VM_NUM} of ${CLUSTER_PUMP_COUNT} **"
    HOSTNAME=${CLUSTER_NAME}-pump-vm-${VM_NUM}
    dependencies_vm ${HOSTNAME} ${CLUSTER_PUMP_DISK_SIZE} ${CLUSTER_PUMP_DISK_TYPE}
    VM_NUM=$(expr ${VM_NUM} + 1)
  done
fi

echo "** Drainer Nodes..."
VM_NUM=1
if [ ${CLUSTER_DRAINER_COUNT} -gt 0 ]; then
  while [ ${VM_NUM} -le ${CLUSTER_DRAINER_COUNT} ]; do
    echo "** Working Drainer VM Instance ${VM_NUM} of ${CLUSTER_DRAINER_COUNT} **"
    HOSTNAME=${CLUSTER_NAME}-drainer-vm-${VM_NUM}
    dependencies_vm ${HOSTNAME} ${CLUSTER_DRAINER_DISK_SIZE} ${CLUSTER_DRAINER_DISK_TYPE}
    VM_NUM=$(expr ${VM_NUM} + 1)
  done
fi




echo "** CLUSTER TOPOGRAPHY BUILD COMPLETE **"
echo "* * * * * * * * * * * * * * * * * * * *"
echo "** Installing TiUP & MySQL Client"
gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone ${CLUSTER_ZONE1}-a --command="mkdir -p ~/tiup && curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh"
gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone ${CLUSTER_ZONE1}-a --command=". .bash_profile ; tiup update --self && tiup update cluster"
gcloud compute scp ${TIUP_YAML} ${CLUSTER_NAME}-tiup-vm-1:tiup/ --zone ${CLUSTER_ZONE1}-a
gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone ${CLUSTER_ZONE1}-a --command="sudo yum -q -y install mysql"
echo "* * * * * * * * * * * * * * * * * * * *"
# echo "** Getting recent versions of TiDB..."
# TIDB_VERSIONS=$(tiup list tidb | egrep -v 'alpha|beta' | tail -12 | sed 's/ ..*//')
# TIDB_VERSION=$(echo $TIDB_VERSIONS | sed "s/..* //")

echo "====================================================================================="
echo "GCP Cluster Deployment Complete. To deploy TiDB using tiup, please run the following:"
echo "====================================================================================="
echo " "
echo "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${USER}@${TIUP_VM_EXT_IP}"
echo "cd ~/tiup"
echo "tiup cluster check ./${TIUP_YAML} --apply --user ${USER} -i ~/.ssh/id_rsa"
echo "tiup cluster check ./${TIUP_YAML} --user ${USER} -i ~/.ssh/id_rsa"
echo "tiup cluster deploy tidb-test ${TIDB_VERSION} ./${TIUP_YAML} --user ${USER} -i ~/.ssh/id_rsa"
echo " "
echo "tiup cluster start tidb-test"
echo "====================================================================================="
printf "Collecting cluster IP addresses and building demo scripts..."

PD1_EXT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-pd-vm-1 )" --format='get(networkInterfaces[0].accessConfigs[0].natIP)')
printf "."
MON1_EXT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-monitor-vm-1 )" --format='get(networkInterfaces[0].accessConfigs[0].natIP)')
printf "."
DB1_INT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-tidb-vm-1 )" --format='get(networkInterfaces[0].networkIP)')
printf "."
DB2_INT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-tidb-vm-2 )" --format='get(networkInterfaces[0].networkIP)')
printf "."
DB3_INT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-tidb-vm-3 )" --format='get(networkInterfaces[0].networkIP)')
printf "."
KV1_INT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-tikv-vm-1 )" --format='get(networkInterfaces[0].networkIP)')
printf "."
KV2_INT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-tikv-vm-2 )" --format='get(networkInterfaces[0].networkIP)')
printf "."
KV3_INT_IP=$(gcloud compute instances list --filter="name:( ${CLUSTER_NAME}-tikv-vm-3 )" --format='get(networkInterfaces[0].networkIP)')

cd script_templates
mkdir ../scripts/
for script in $(ls -1); do
  sed -e "s/DB1_INT_IP/${DB1_INT_IP}/g" \
          -e "s/DB2_INT_IP/${DB2_INT_IP}/g" \
          -e "s/DB3_INT_IP/${DB3_INT_IP}/g" \
          -e "s/KV1_INT_IP/${KV1_INT_IP}/g" \
          -e "s/KV2_INT_IP/${KV2_INT_IP}/g" \
          -e "s/KV3_INT_IP/${KV3_INT_IP}/g" ${script} > ../scripts/${script}
  printf "."
done
cd ..
tar -c --no-xattrs -f - scripts | gzip -9c > scripts.tar.gz
echo " Done."
echo "Copying scripts to tiup host..."
gcloud compute scp scripts.tar.gz ${CLUSTER_NAME}-tiup-vm-1:. --zone=${CLUSTER_ZONE1}-a
gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone=${CLUSTER_ZONE1}-a \
  --command="tar zxf scripts.tar.gz ; rm scripts.tar.gz"
rm -rf scripts scripts.tar.gz

echo " Done."
printf "\n\n================================================================================\nStarting deployment of TiDB Version %s using TiUP...\n" ${TIDB_VERSION}
gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone=${CLUSTER_ZONE1}-a \
  --command="export PATH=~/.tiup/bin:$PATH && cd ~/tiup && tiup cluster check ./${TIUP_YAML} --apply --user ${USER} -i ~/.ssh/id_rsa && tiup cluster deploy tidb-test ${TIDB_VERSION} ./${TIUP_YAML} --user ${USER} -i ~/.ssh/id_rsa --yes && tiup cluster start tidb-test"
printf "\n\n================================================================================\nTiDB Deployment Complete.\n================================================================================\n"
echo "Useful URLs:"
echo "    TiDB Console: http://${PD1_EXT_IP}:2379/dashboard/#/overview"
echo "        Login: root (empty password)"
echo "    Grafana     : http://${MON1_EXT_IP}:3000"
echo "        Login: admin/admin"
echo " "
echo "Useful Commands:"
echo "    Send traffic to TiDB[1-3]:"
echo "        mysqlslap -u root -h ${DB1_INT_IP} --port=4000 --password=[password] --number-int-cols=2 --number-char-cols=3 --create-schema=slapme1 --auto-generate-sql --concurrency=50 --iterations=2000 &"
echo "        mysqlslap -u root -h ${DB2_INT_IP} --port=4000 --password=[password] --number-int-cols=2 --number-char-cols=3 --create-schema=slapme2 --auto-generate-sql --concurrency=50 --iterations=2000 &"
echo "        mysqlslap -u root -h ${DB3_INT_IP} --port=4000 --password=[password] --number-int-cols=2 --number-char-cols=3 --create-schema=slapme3 --auto-generate-sql --concurrency=50 --iterations=2000 &"
echo " "
echo "    Kill TiKV Node[1]: tiup cluster stop tidb-test -N ${KV1_INT_IP}:20160"
echo "    Kill TiKV Node[2]: tiup cluster stop tidb-test -N ${KV2_INT_IP}:20160"
echo "    Kill TiKV Node[3]: tiup cluster stop tidb-test -N ${KV3_INT_IP}:20160"
echo " "
echo "    Start TiKV Node[1]: tiup cluster start tidb-test -N ${KV1_INT_IP}:20160"
echo "    Start TiKV Node[2]: tiup cluster start tidb-test -N ${KV2_INT_IP}:20160"
echo "    Start TiKV Node[3]: tiup cluster start tidb-test -N ${KV3_INT_IP}:20160"
echo " "
echo "Connect to Cluster:"
echo "    gcloud compute ssh ${CLUSTER_NAME}-tiup-vm-1 --zone=${CLUSTER_ZONE1}-a"
echo " "
echo "Once connected to tiup VM, you can access the cluster using the following command:"
echo "    mysql -u root -h ${DB1_INT_IP} --port=4000 --password=''"
echo " "
echo "====================================================================================="
echo " "
echo " "

exit 0

# mysqlslap -u root -h 10.16.0.6 --port=4000 --password=t798*is340+KZYM^W- --number-int-cols=2 --number-char-cols=3 --auto-generate-sql --concurrency=50 --iterations=20
# mysqlslap -u root -h 10.16.0.6 --port=4000 --password=t798*is340+KZYM^W- --number-int-cols=2 --number-char-cols=3 --auto-generate-sql --concurrency=50 --iterations=2000

# http://34.169.77.32:2379/dashboard/#/overview
# http://104.196.238.144:3000 (admin/admin)
