|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +function usage() { |
| 4 | + printf "Usage: bash copy_blockchain_data_gcp.sh [dev|staging|sandbox|exp|spring|summer|mainnet] <Node Index> [download|upload]\n" |
| 5 | + printf "Example: bash copy_blockchain_data_gcp.sh spring 5 download\n" |
| 6 | + printf "\n" |
| 7 | + exit |
| 8 | +} |
| 9 | + |
| 10 | +if [[ $# -lt 3 ]] || [[ $# -gt 3 ]]; then |
| 11 | + usage |
| 12 | +fi |
| 13 | + |
| 14 | +if [[ "$1" = 'dev' ]] || [[ "$1" = 'staging' ]] || [[ "$1" = 'sandbox' ]] || [[ "$1" = 'exp' ]] || [[ "$1" = 'spring' ]] || [[ "$1" = 'summer' ]] || [[ "$1" = 'mainnet' ]]; then |
| 15 | + SEASON="$1" |
| 16 | + if [[ "$1" = 'mainnet' ]]; then |
| 17 | + PROJECT_ID="mainnet-prod-ground" |
| 18 | + elif [[ "$1" = 'spring' ]] || [[ "$1" = 'summer' ]]; then |
| 19 | + PROJECT_ID="testnet-prod-ground" |
| 20 | + else |
| 21 | + PROJECT_ID="testnet-$1-ground" |
| 22 | + fi |
| 23 | +else |
| 24 | + printf "Invalid <Project/Season> argument: $1\n" |
| 25 | + exit |
| 26 | +fi |
| 27 | +printf "\n" |
| 28 | +printf "SEASON=$SEASON\n" |
| 29 | +printf "PROJECT_ID=$PROJECT_ID\n" |
| 30 | + |
| 31 | +GCP_USER="runner" |
| 32 | +printf "GCP_USER=$GCP_USER\n" |
| 33 | + |
| 34 | +number_re='^[0-9]+$' |
| 35 | +if ! [[ $2 =~ $number_re ]] ; then |
| 36 | + printf "\n" |
| 37 | + printf "Invalid <Node Index> argument: $2\n" |
| 38 | + exit |
| 39 | +fi |
| 40 | +NODE_INDEX=$2 |
| 41 | +if [[ $NODE_INDEX -lt 0 ]] || [[ $NODE_INDEX -gt 9 ]]; then |
| 42 | + printf "\n" |
| 43 | + printf "Out-of-range <Node Index> argument: $NODE_INDEX\n" |
| 44 | + exit |
| 45 | +fi |
| 46 | +printf "NODE_INDEX=$NODE_INDEX\n" |
| 47 | + |
| 48 | +if [[ "$3" = 'download' ]] || [[ "$3" = 'upload' ]]; then |
| 49 | + COMMAND="$3" |
| 50 | +else |
| 51 | + printf "\n" |
| 52 | + printf "Invalid <Command> argument: $3\n" |
| 53 | + printf "\n" |
| 54 | + usage |
| 55 | +fi |
| 56 | +printf "COMMAND=$COMMAND\n" |
| 57 | + |
| 58 | +# Get confirmation. |
| 59 | +if [[ "$SEASON" = "mainnet" ]]; then |
| 60 | + printf "\n" |
| 61 | + printf "Do you want to proceed for $SEASON? Enter [mainnet]: " |
| 62 | + read CONFIRM |
| 63 | + printf "\n\n" |
| 64 | + if [[ ! $CONFIRM = "mainnet" ]] |
| 65 | + then |
| 66 | + [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell |
| 67 | + fi |
| 68 | +else |
| 69 | + printf "\n" |
| 70 | + read -p "Do you want to proceed for $SEASON? [y/N]: " -n 1 -r |
| 71 | + printf "\n\n" |
| 72 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 73 | + [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell |
| 74 | + fi |
| 75 | +fi |
| 76 | + |
| 77 | +NODE_TARGET_ADDR_LIST=( |
| 78 | + "${GCP_USER}@${SEASON}-node-0-taiwan" \ |
| 79 | + "${GCP_USER}@${SEASON}-node-1-oregon" \ |
| 80 | + "${GCP_USER}@${SEASON}-node-2-singapore" \ |
| 81 | + "${GCP_USER}@${SEASON}-node-3-iowa" \ |
| 82 | + "${GCP_USER}@${SEASON}-node-4-netherlands" \ |
| 83 | + "${GCP_USER}@${SEASON}-node-5-taiwan" \ |
| 84 | + "${GCP_USER}@${SEASON}-node-6-oregon" \ |
| 85 | + "${GCP_USER}@${SEASON}-node-7-singapore" \ |
| 86 | + "${GCP_USER}@${SEASON}-node-8-iowa" \ |
| 87 | + "${GCP_USER}@${SEASON}-node-9-netherlands" \ |
| 88 | +) |
| 89 | +NODE_ZONE_LIST=( |
| 90 | + "asia-east1-b" \ |
| 91 | + "us-west1-b" \ |
| 92 | + "asia-southeast1-b" \ |
| 93 | + "us-central1-a" \ |
| 94 | + "europe-west4-a" \ |
| 95 | + "asia-east1-b" \ |
| 96 | + "us-west1-b" \ |
| 97 | + "asia-southeast1-b" \ |
| 98 | + "us-central1-a" \ |
| 99 | + "europe-west4-a" \ |
| 100 | +) |
| 101 | + |
| 102 | +function download_data() { |
| 103 | + local node_index="$1" |
| 104 | + local node_target_addr=${NODE_TARGET_ADDR_LIST[${node_index}]} |
| 105 | + local node_zone=${NODE_ZONE_LIST[${node_index}]} |
| 106 | + |
| 107 | + printf "\n* >> Downloading data from node $node_index ($node_target_addr) *********************************************************\n\n" |
| 108 | + |
| 109 | + printf "node_target_addr='$node_target_addr'\n" |
| 110 | + printf "node_zone='$node_zone'\n" |
| 111 | + |
| 112 | + # 1. Create tgz file for node |
| 113 | + printf "\n\n<<< Creating tgz file for node $node_index >>>\n\n" |
| 114 | + TGZ_CMD="gcloud compute ssh $node_target_addr --command 'cd /home/ain_blockchain_data; tar cvf - chains snapshots | gzip -c > ~/ain_blockchain_data.tar.gz' --project $PROJECT_ID --zone $node_zone" |
| 115 | + printf "TGZ_CMD=$TGZ_CMD\n\n" |
| 116 | + eval $TGZ_CMD |
| 117 | + |
| 118 | + # 2. Copy tgz file from node |
| 119 | + printf "\n\n<<< Copying tgz file from node $node_index >>>\n\n" |
| 120 | + SCP_CMD="gcloud compute scp $node_target_addr:~/ain_blockchain_data.tar.gz . --project $PROJECT_ID --zone $node_zone" |
| 121 | + printf "SCP_CMD=$SCP_CMD\n\n" |
| 122 | + eval $SCP_CMD |
| 123 | + |
| 124 | + # 3. Clean up tgz file for node |
| 125 | + printf "\n\n<<< Cleaning up tgz file for node $node_index >>>\n\n" |
| 126 | + CLEANUP_CMD="gcloud compute ssh $node_target_addr --command 'rm ~/ain_blockchain_data.tar.gz' --project $PROJECT_ID --zone $node_zone" |
| 127 | + printf "CLEANUP_CMD=$CLEANUP_CMD\n\n" |
| 128 | + eval $CLEANUP_CMD |
| 129 | +} |
| 130 | + |
| 131 | +function upload_data() { |
| 132 | + local node_index="$1" |
| 133 | + local node_target_addr=${NODE_TARGET_ADDR_LIST[${node_index}]} |
| 134 | + local node_zone=${NODE_ZONE_LIST[${node_index}]} |
| 135 | + |
| 136 | + printf "\n* >> Uploading data from node $node_index ($node_target_addr) *********************************************************\n\n" |
| 137 | + |
| 138 | + printf "node_target_addr='$node_target_addr'\n" |
| 139 | + printf "node_zone='$node_zone'\n" |
| 140 | + |
| 141 | + # 1. Copy tgz file to node |
| 142 | + printf "\n\n<<< Copying tgz file to node $node_index >>>\n\n" |
| 143 | + SCP_CMD="gcloud compute scp ./ain_blockchain_data.tar.gz $node_target_addr:~ --project $PROJECT_ID --zone $node_zone" |
| 144 | + printf "SCP_CMD=$SCP_CMD\n\n" |
| 145 | + eval $SCP_CMD |
| 146 | + |
| 147 | + # 2. Extract tgz file for node |
| 148 | + printf "\n\n<<< Extracting tgz file for node $node_index >>>\n\n" |
| 149 | + TGZ_CMD="gcloud compute ssh $node_target_addr --command 'cd /home; sudo mkdir -p ain_blockchain_data; sudo chown runner:runner ain_blockchain_data; sudo chmod 777 ain_blockchain_data; cd ain_blockchain_data; gzip -dc ~/ain_blockchain_data.tar.gz | tar xvf -' --project $PROJECT_ID --zone $node_zone" |
| 150 | + printf "TGZ_CMD=$TGZ_CMD\n\n" |
| 151 | + eval $TGZ_CMD |
| 152 | + |
| 153 | + # 3. Clean up tgz file for node |
| 154 | + printf "\n\n<<< Cleaning up tgz file for node $node_index >>>\n\n" |
| 155 | + CLEANUP_CMD="gcloud compute ssh $node_target_addr --command 'rm ~/ain_blockchain_data.tar.gz' --project $PROJECT_ID --zone $node_zone" |
| 156 | + printf "CLEANUP_CMD=$CLEANUP_CMD\n\n" |
| 157 | + eval $CLEANUP_CMD |
| 158 | +} |
| 159 | + |
| 160 | +if [[ "$COMMAND" = 'upload' ]]; then |
| 161 | + upload_data "$NODE_INDEX" |
| 162 | +else |
| 163 | + download_data "$NODE_INDEX" |
| 164 | +fi |
0 commit comments