Skip to content

Commit

Permalink
Merge pull request #1313 from ainblockchain/feature/platfowner/feature
Browse files Browse the repository at this point in the history
Add a tool script for downloading/uploading blockchain data
  • Loading branch information
platfowner authored Aug 28, 2024
2 parents 599a180 + b47f094 commit c8c6e08
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 11 deletions.
133 changes: 133 additions & 0 deletions copy_blockchain_data_onprem.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/bin/bash

function usage() {
printf "Usage: bash copy_blockchain_data_onprem.sh [dev|staging|sandbox|exp|spring|summer|mainnet] <Node Index> [download|upload]\n"
printf "Example: bash copy_blockchain_data_onprem.sh spring 5 download\n"
printf "\n"
exit
}

if [[ $# -lt 3 ]] || [[ $# -gt 3 ]]; then
usage
fi

if [[ "$1" = 'dev' ]] || [[ "$1" = 'staging' ]] || [[ "$1" = 'sandbox' ]] || [[ "$1" = 'exp' ]] || [[ "$1" = 'spring' ]] || [[ "$1" = 'summer' ]] || [[ "$1" = 'mainnet' ]]; then
SEASON="$1"
else
printf "Invalid <Project/Season> argument: $1\n"
exit
fi
printf "\n"
printf "SEASON=$SEASON\n"

ONPREM_USER="nvidia"
printf "ONPREM_USER=$ONPREM_USER\n"

number_re='^[0-9]+$'
if ! [[ $2 =~ $number_re ]] ; then
printf "\n"
printf "Invalid <Node Index> argument: $2\n"
exit
fi
NODE_INDEX=$2
if [[ $NODE_INDEX -lt 0 ]] || [[ $NODE_INDEX -gt 9 ]]; then
printf "\n"
printf "Out-of-range <Node Index> argument: $NODE_INDEX\n"
exit
fi
printf "NODE_INDEX=$NODE_INDEX\n"

if [[ "$3" = 'download' ]] || [[ "$3" = 'upload' ]]; then
COMMAND="$3"
else
printf "\n"
printf "Invalid <Command> argument: $3\n"
printf "\n"
usage
fi
printf "COMMAND=$COMMAND\n"

# Get confirmation.
if [[ "$SEASON" = "mainnet" ]]; then
printf "\n"
printf "Do you want to proceed for $SEASON? Enter [mainnet]: "
read CONFIRM
printf "\n\n"
if [[ ! $CONFIRM = "mainnet" ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
else
printf "\n"
read -p "Do you want to proceed for $SEASON? [y/N]: " -n 1 -r
printf "\n\n"
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
fi

# Read node ip addresses and passwords
IFS=$'\n' read -d '' -r -a NODE_IP_LIST < ./ip_addresses/${SEASON}_onprem_ip.txt
IFS=$'\n' read -d '' -r -a NODE_PW_LIST < ./ip_addresses/${SEASON}_onprem_pw.txt

function download_data() {
local node_index="$1"
local node_target_addr="${ONPREM_USER}@${NODE_IP_LIST[${node_index}]}"
local node_login_pw="${NODE_PW_LIST[${node_index}]}"

printf "\n* >> Downloading data from node $node_index ($node_target_addr) *********************************************************\n\n"

printf "node_target_addr='$node_target_addr'\n"

# 1. Create tgz file for node
printf "\n\n<<< Creating tgz file for node $node_index >>>\n\n"
TGZ_CMD="ssh -v $node_target_addr 'cd /home/${SEASON}_ain_blockchain_data; tar cvf - chains snapshots | gzip -c > ~/${SEASON}_ain_blockchain_data.tar.gz'"
printf "TGZ_CMD=$TGZ_CMD\n\n"
eval "echo ${node_login_pw} | sshpass -f <(printf '%s\n' ${node_login_pw}) ${TGZ_CMD}"

# 2. Copy tgz file from node
printf "\n\n<<< Copying tgz file from node $node_index >>>\n\n"
SCP_CMD="scp -rv $node_target_addr:~/${SEASON}_ain_blockchain_data.tar.gz ."
printf "SCP_CMD=$SCP_CMD\n\n"
eval "sshpass -f <(printf '%s\n' ${node_login_pw}) ${SCP_CMD}"

# 3. Clean up tgz file for node
printf "\n\n<<< Cleaning up tgz file for node $node_index >>>\n\n"
CLEANUP_CMD="ssh -v $node_target_addr 'rm ~/${SEASON}_ain_blockchain_data.tar.gz'"
printf "CLEANUP_CMD=$CLEANUP_CMD\n\n"
eval "echo ${node_login_pw} | sshpass -f <(printf '%s\n' ${node_login_pw}) ${CLEANUP_CMD}"
}

function upload_data() {
local node_index="$1"
local node_target_addr="${ONPREM_USER}@${NODE_IP_LIST[${node_index}]}"
local node_login_pw="${NODE_PW_LIST[${node_index}]}"

printf "\n* >> Uploading data from node $node_index ($node_target_addr) *********************************************************\n\n"

printf "node_target_addr='$node_target_addr'\n"

# 1. Copy tgz file to node
printf "\n\n<<< Copying tgz file to node $node_index >>>\n\n"
SCP_CMD="scp -rv ./${SEASON}_ain_blockchain_data.tar.gz $node_target_addr:~"
printf "SCP_CMD=$SCP_CMD\n\n"
eval "sshpass -f <(printf '%s\n' ${node_login_pw}) ${SCP_CMD}"

# 2. Extract tgz file for node
printf "\n\n<<< Extracting tgz file for node $node_index >>>\n\n"
TGZ_CMD="ssh -v $node_target_addr 'cd /home; sudo mkdir -p ${SEASON}_ain_blockchain_data; sudo chown runner:runner ${SEASON}_ain_blockchain_data; sudo chmod 777 ${SEASON}_ain_blockchain_data; cd ${SEASON}_ain_blockchain_data; gzip -dc ~/${SEASON}_ain_blockchain_data.tar.gz | tar xvf -'"
printf "TGZ_CMD=$TGZ_CMD\n\n"
eval "echo ${node_login_pw} | sshpass -f <(printf '%s\n' ${node_login_pw}) ${TGZ_CMD}"

# 3. Clean up tgz file for node
printf "\n\n<<< Cleaning up tgz file for node $node_index >>>\n\n"
CLEANUP_CMD="ssh -v $node_target_addr 'rm ~/${SEASON}_ain_blockchain_data.tar.gz'"
printf "CLEANUP_CMD=$CLEANUP_CMD\n\n"
eval "echo ${node_login_pw} | sshpass -f <(printf '%s\n' ${node_login_pw}) ${CLEANUP_CMD}"
}

if [[ "$COMMAND" = 'upload' ]]; then
upload_data "$NODE_INDEX"
else
download_data "$NODE_INDEX"
fi
8 changes: 0 additions & 8 deletions deploy_blockchain_genesis_onprem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@ printf "\n[[[[[ deploy_blockchain_genesis_onprem.sh ]]]]]\n\n"

if [[ "$1" = 'dev' ]] || [[ "$1" = 'staging' ]] || [[ "$1" = 'sandbox' ]] || [[ "$1" = 'exp' ]] || [[ "$1" = 'spring' ]] || [[ "$1" = 'summer' ]] || [[ "$1" = 'mainnet' ]]; then
SEASON="$1"
if [[ "$1" = 'mainnet' ]]; then
PROJECT_ID="mainnet-prod-ground"
elif [[ "$1" = 'spring' ]] || [[ "$1" = 'summer' ]]; then
PROJECT_ID="testnet-prod-ground"
else
PROJECT_ID="testnet-$1-ground"
fi
else
printf "Invalid project/season argument: $1\n"
exit
fi
printf "SEASON=$SEASON\n"
printf "PROJECT_ID=$PROJECT_ID\n"

ONPREM_USER="nvidia"
printf "ONPREM_USER=$ONPREM_USER\n"
Expand Down
6 changes: 3 additions & 3 deletions start_node_genesis_onprem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ fi

printf '\n'
printf 'Killing old jobs..\n'
sudo killall 'client/ain-blockchain-${SEASON}-index.js'
sudo killall "client/${SEASON}-ain-blockchain-index.js"
if [[ $KEEP_CODE_OPTION = "--no-keep-code" ]]; then
printf '\n'
printf 'Setting up new working directory..\n'
sudo rm -rf /home/${SEASON}-ain-blockchain*
# NOTE(platfowner): Add $SEASON to the node job name to be selectively killed in restarts.
CODE_CMD="cd ~; sudo mv ${SEASON}-ain-blockchain /home; sudo chmod -R 777 /home/${SEASON}-ain-blockchain; sudo chown -R $GCP_USER:$GCP_USER /home/${SEASON}-ain-blockchain; cd /home/${SEASON}-ain-blockchain; mv client/index.js client/ain-blockchain-${SEASON}-index.js"
CODE_CMD="cd ~; sudo mv ${SEASON}-ain-blockchain /home; sudo chmod -R 777 /home/${SEASON}-ain-blockchain; sudo chown -R $GCP_USER:$GCP_USER /home/${SEASON}-ain-blockchain; cd /home/${SEASON}-ain-blockchain; mv client/index.js client/${SEASON}-ain-blockchain-index.js"
printf "\nCODE_CMD=$CODE_CMD\n"
eval $CODE_CMD
else
Expand Down Expand Up @@ -339,7 +339,7 @@ else
fi

printf "\nStarting up Blockchain Node server..\n\n"
START_CMD="nohup node --async-stack-traces --max-old-space-size=$MAX_OLD_SPACE_SIZE_MB client/ain-blockchain-${SEASON}-index.js >/dev/null 2>error_logs.txt &"
START_CMD="nohup node --async-stack-traces --max-old-space-size=$MAX_OLD_SPACE_SIZE_MB client/${SEASON}-ain-blockchain-index.js >/dev/null 2>error_logs.txt &"
printf "\nSTART_CMD=$START_CMD\n"
printf "START_CMD=$START_CMD\n" >> start_commands.txt
eval $START_CMD
Expand Down

0 comments on commit c8c6e08

Please sign in to comment.