|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +function usage() { |
| 4 | + printf "Usage: bash copy_blockchain_data_onprem.sh [staging|spring|mainnet] <Node Index> [download|upload]\n" |
| 5 | + printf "Example: bash copy_blockchain_data_onprem.sh staging 0 download\n" |
| 6 | + printf "Example: bash copy_blockchain_data_onprem.sh staging 1 upload\n" |
| 7 | + printf "\n" |
| 8 | + exit |
| 9 | +} |
| 10 | + |
| 11 | +if [[ $# -lt 3 ]] || [[ $# -gt 3 ]]; then |
| 12 | + usage |
| 13 | +fi |
| 14 | + |
| 15 | +if [[ "$1" = 'staging' ]] || [[ "$1" = 'spring' ]] || [[ "$1" = 'mainnet' ]]; then |
| 16 | + SEASON="$1" |
| 17 | +else |
| 18 | + printf "Invalid <Project/Season> argument: $1\n" |
| 19 | + exit |
| 20 | +fi |
| 21 | +printf "\n" |
| 22 | +printf "SEASON=$SEASON\n" |
| 23 | + |
| 24 | +ONPREM_USER="nvidia" |
| 25 | +printf "ONPREM_USER=$ONPREM_USER\n" |
| 26 | + |
| 27 | +number_re='^[0-9]+$' |
| 28 | +if ! [[ $2 =~ $number_re ]] ; then |
| 29 | + printf "\n" |
| 30 | + printf "Invalid <Node Index> argument: $2\n" |
| 31 | + exit |
| 32 | +fi |
| 33 | +NODE_INDEX=$2 |
| 34 | +if [[ $NODE_INDEX -lt 0 ]] || [[ $NODE_INDEX -gt 9 ]]; then |
| 35 | + printf "\n" |
| 36 | + printf "Out-of-range <Node Index> argument: $NODE_INDEX\n" |
| 37 | + exit |
| 38 | +fi |
| 39 | +printf "NODE_INDEX=$NODE_INDEX\n" |
| 40 | + |
| 41 | +if [[ "$3" = 'download' ]] || [[ "$3" = 'upload' ]]; then |
| 42 | + COMMAND="$3" |
| 43 | +else |
| 44 | + printf "\n" |
| 45 | + printf "Invalid <Command> argument: $3\n" |
| 46 | + printf "\n" |
| 47 | + usage |
| 48 | +fi |
| 49 | +printf "COMMAND=$COMMAND\n" |
| 50 | + |
| 51 | +# Get confirmation. |
| 52 | +if [[ "$SEASON" = "mainnet" ]]; then |
| 53 | + printf "\n" |
| 54 | + printf "Do you want to proceed for $SEASON? Enter [mainnet]: " |
| 55 | + read CONFIRM |
| 56 | + printf "\n\n" |
| 57 | + if [[ ! $CONFIRM = "mainnet" ]] |
| 58 | + then |
| 59 | + [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell |
| 60 | + fi |
| 61 | +else |
| 62 | + printf "\n" |
| 63 | + read -p "Do you want to proceed for $SEASON? [y/N]: " -n 1 -r |
| 64 | + printf "\n\n" |
| 65 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 66 | + [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell |
| 67 | + fi |
| 68 | +fi |
| 69 | + |
| 70 | +# Read node ip addresses and passwords |
| 71 | +IFS=$'\n' read -d '' -r -a NODE_IP_LIST < ./ip_addresses/${SEASON}_onprem_ip.txt |
| 72 | +IFS=$'\n' read -d '' -r -a NODE_PW_LIST < ./ip_addresses/${SEASON}_onprem_pw.txt |
| 73 | + |
| 74 | +function download_data() { |
| 75 | + local node_index="$1" |
| 76 | + local node_target_addr="${ONPREM_USER}@${NODE_IP_LIST[${node_index}]}" |
| 77 | + local node_login_pw="${NODE_PW_LIST[${node_index}]}" |
| 78 | + |
| 79 | + printf "\n* >> Downloading data from node $node_index ($node_target_addr) *********************************************************\n\n" |
| 80 | + |
| 81 | + printf "node_target_addr='$node_target_addr'\n" |
| 82 | + |
| 83 | + # 1. Create tgz file for node |
| 84 | + printf "\n\n<<< Creating tgz file for node $node_index >>>\n\n" |
| 85 | + TGZ_CMD="ssh $node_target_addr 'sudo -S ls -la; cd /home/${SEASON}/ain_blockchain_data; tar cvf - chains snapshots | gzip -c > ~/ain_blockchain_data.tar.gz'" |
| 86 | + printf "TGZ_CMD=$TGZ_CMD\n\n" |
| 87 | + eval "echo ${node_login_pw} | sshpass -f <(printf '%s\n' ${node_login_pw}) ${TGZ_CMD}" |
| 88 | + |
| 89 | + # 2. Copy tgz file from node |
| 90 | + printf "\n\n<<< Copying tgz file from node $node_index >>>\n\n" |
| 91 | + SCP_CMD="scp -r $node_target_addr:~/ain_blockchain_data.tar.gz ." |
| 92 | + printf "SCP_CMD=$SCP_CMD\n\n" |
| 93 | + eval "sshpass -f <(printf '%s\n' ${node_login_pw}) ${SCP_CMD}" |
| 94 | + |
| 95 | + # 3. Clean up tgz file for node |
| 96 | + printf "\n\n<<< Cleaning up tgz file for node $node_index >>>\n\n" |
| 97 | + CLEANUP_CMD="ssh $node_target_addr 'rm ~/ain_blockchain_data.tar.gz'" |
| 98 | + printf "CLEANUP_CMD=$CLEANUP_CMD\n\n" |
| 99 | + eval "sshpass -f <(printf '%s\n' ${node_login_pw}) ${CLEANUP_CMD}" |
| 100 | +} |
| 101 | + |
| 102 | +function upload_data() { |
| 103 | + local node_index="$1" |
| 104 | + local node_target_addr="${ONPREM_USER}@${NODE_IP_LIST[${node_index}]}" |
| 105 | + local node_login_pw="${NODE_PW_LIST[${node_index}]}" |
| 106 | + |
| 107 | + printf "\n* >> Uploading data from node $node_index ($node_target_addr) *********************************************************\n\n" |
| 108 | + |
| 109 | + printf "node_target_addr='$node_target_addr'\n" |
| 110 | + |
| 111 | + # 1. Copy tgz file to node |
| 112 | + printf "\n\n<<< Copying tgz file to node $node_index >>>\n\n" |
| 113 | + SCP_CMD="scp -r ./ain_blockchain_data.tar.gz $node_target_addr:~" |
| 114 | + printf "SCP_CMD=$SCP_CMD\n\n" |
| 115 | + eval "sshpass -f <(printf '%s\n' ${node_login_pw}) ${SCP_CMD}" |
| 116 | + |
| 117 | + # 2. Extract tgz file for node |
| 118 | + printf "\n\n<<< Extracting tgz file for node $node_index >>>\n\n" |
| 119 | + TGZ_CMD="ssh $node_target_addr 'sudo -S ls -la; cd /home; sudo mkdir -p ${SEASON}/ain_blockchain_data; sudo chown $ONPREM_USER:$ONPREM_USER ${SEASON} ${SEASON}/ain_blockchain_data; sudo chmod 777 ${SEASON} ${SEASON}/ain_blockchain_data; cd ${SEASON}/ain_blockchain_data; sudo rm -rf chains snapshots; gzip -dc ~/ain_blockchain_data.tar.gz | tar xvf -'" |
| 120 | + printf "TGZ_CMD=$TGZ_CMD\n\n" |
| 121 | + eval "echo ${node_login_pw} | sshpass -f <(printf '%s\n' ${node_login_pw}) ${TGZ_CMD}" |
| 122 | + |
| 123 | + # 3. Clean up tgz file for node |
| 124 | + printf "\n\n<<< Cleaning up tgz file for node $node_index >>>\n\n" |
| 125 | + CLEANUP_CMD="ssh $node_target_addr 'rm ~/ain_blockchain_data.tar.gz'" |
| 126 | + printf "CLEANUP_CMD=$CLEANUP_CMD\n\n" |
| 127 | + eval "sshpass -f <(printf '%s\n' ${node_login_pw}) ${CLEANUP_CMD}" |
| 128 | +} |
| 129 | + |
| 130 | +if [[ "$COMMAND" = 'upload' ]]; then |
| 131 | + upload_data "$NODE_INDEX" |
| 132 | +else |
| 133 | + download_data "$NODE_INDEX" |
| 134 | +fi |
0 commit comments