|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +if [[ $# -lt 2 ]]; then |
| 4 | + printf "Usage: bash deploy_test_gcp.sh <GCP Username> <Instatnce Index> [--setup] [--keep-code|--no-keep-code] [--bg] [--cat-log] [--stop-only] <Testing Option>\n" |
| 5 | + printf "Example: bash deploy_test_gcp.sh my_username 0 --setup test_unit\n" |
| 6 | + printf "Example: bash deploy_test_gcp.sh my_username 0 --keep-code test_unit\n" |
| 7 | + printf "Example: bash deploy_test_gcp.sh my_username 0 --keep-code test_unit \"-g 'matchFunction NOT'\"\n" |
| 8 | + printf "Example: bash deploy_test_gcp.sh my_username 0 --keep-code --bg test_unit\n" |
| 9 | + printf "Example: bash deploy_test_gcp.sh my_username 0 --cat-log\n" |
| 10 | + printf "Example: bash deploy_test_gcp.sh my_username 0 --stop-only\n" |
| 11 | + printf "\n" |
| 12 | + exit |
| 13 | +fi |
| 14 | +printf "\n[[[[[ deploy_test_gcp.sh ]]]]]\n\n" |
| 15 | + |
| 16 | +GCP_USER="$1" |
| 17 | +printf "GCP_USER=$GCP_USER\n" |
| 18 | + |
| 19 | +number_re='^[0-9]+$' |
| 20 | +if ! [[ $2 =~ $number_re ]] ; then |
| 21 | + printf "Invalid <Instance Index> argument: $2\n" |
| 22 | + exit |
| 23 | +fi |
| 24 | +INSTANCE_INDEX=$2 |
| 25 | +printf "INSTANCE_INDEX=$INSTANCE_INDEX\n" |
| 26 | +printf "\n" |
| 27 | + |
| 28 | +function parse_options() { |
| 29 | + local option="$1" |
| 30 | + if [[ $option = '--setup' ]]; then |
| 31 | + SETUP_OPTION="$option" |
| 32 | + elif [[ $option = '--keep-code' ]]; then |
| 33 | + KEEP_CODE_OPTION="$option" |
| 34 | + elif [[ $option = '--no-keep-code' ]]; then |
| 35 | + KEEP_CODE_OPTION="$option" |
| 36 | + elif [[ $option = '--bg' ]]; then |
| 37 | + BACKGROUND_OPTION="$option" |
| 38 | + elif [[ $option = '--cat-log' ]]; then |
| 39 | + CAT_LOG_OPTION="$option" |
| 40 | + elif [[ $option = '--stop-only' ]]; then |
| 41 | + STOP_ONLY_OPTION="$option" |
| 42 | + else |
| 43 | + TESTING_OPTION="$TESTING_OPTION $option" |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +# Parse options. |
| 48 | +SETUP_OPTION="" |
| 49 | +CAT_LOG_OPTION="" |
| 50 | +STOP_ONLY_OPTION="" |
| 51 | +KEEP_CODE_OPTION="--no-keep-code" |
| 52 | +BACKGROUND_OPTION="" |
| 53 | +TESTING_OPTION="" |
| 54 | + |
| 55 | +ARG_INDEX=3 |
| 56 | +while [ $ARG_INDEX -le $# ]; do |
| 57 | + parse_options "${!ARG_INDEX}" |
| 58 | + ((ARG_INDEX++)) |
| 59 | +done |
| 60 | +printf "SETUP_OPTION=$SETUP_OPTION\n" |
| 61 | +printf "CAT_LOG_OPTION=$CAT_LOG_OPTION\n" |
| 62 | +printf "STOP_ONLY_OPTION=$STOP_ONLY_OPTION\n" |
| 63 | +printf "KEEP_CODE_OPTION=$KEEP_CODE_OPTION\n" |
| 64 | +printf "BACKGROUND_OPTION=$BACKGROUND_OPTION\n" |
| 65 | +printf "TESTING_OPTION=$TESTING_OPTION\n" |
| 66 | + |
| 67 | +if [[ $CAT_LOG_OPTION != "--cat-log" ]]; then |
| 68 | + # Get confirmation. |
| 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 | +function stop_servers() { |
| 78 | + printf "\n* >> Stopping tests on instance ${TEST_TARGET_ADDR} *********************************************************\n\n" |
| 79 | + STOP_CMD="cd ./ain-blockchain; . stop_servers_local.sh" |
| 80 | + printf "\nSTOP_CMD=$STOP_CMD\n\n" |
| 81 | + gcloud compute ssh ${TEST_TARGET_ADDR} --command "$STOP_CMD" --project $PROJECT_ID --zone ${TEST_ZONE} |
| 82 | +} |
| 83 | + |
| 84 | +# deploy files |
| 85 | +FILES_FOR_TEST="afan_client/ blockchain/ blockchain-configs/ block-pool/ client/ common/ consensus/ db/ event-handler/ json_rpc/ logger/ node/ p2p/ test/ tools/ tracker-server/ traffic/ tx-pool/ package.json setup_blockchain_ubuntu.sh stop_servers_local.sh" |
| 86 | + |
| 87 | +printf "\n" |
| 88 | +SEASON="dev" |
| 89 | +printf "SEASON=$SEASON\n" |
| 90 | + |
| 91 | +PROJECT_ID="testnet-dev-ground" |
| 92 | +printf "PROJECT_ID=$PROJECT_ID\n" |
| 93 | + |
| 94 | +TEST_TARGET_ADDR="${GCP_USER}@${SEASON}-test-${INSTANCE_INDEX}" |
| 95 | +printf "TEST_TARGET_ADDR=$TEST_TARGET_ADDR\n" |
| 96 | + |
| 97 | +TEST_ZONE="asia-east1-b" |
| 98 | +printf "TEST_ZONE=$TEST_ZONE\n" |
| 99 | +printf "\n" |
| 100 | + |
| 101 | +# stop test servers and exit |
| 102 | +if [[ $STOP_ONLY_OPTION = "--stop-only" ]]; then |
| 103 | + stop_servers |
| 104 | + printf "\n" |
| 105 | + exit 0 |
| 106 | +fi |
| 107 | + |
| 108 | +# cat-log test log and exit |
| 109 | +if [[ $CAT_LOG_OPTION = "--cat-log" ]]; then |
| 110 | + printf "\n* >> Cat-logging test log from instance ${TEST_TARGET_ADDR} *********************************************************\n\n" |
| 111 | + gcloud compute ssh ${TEST_TARGET_ADDR} --command "cd ./ain-blockchain; cat test_log.txt" --project $PROJECT_ID --zone ${TEST_ZONE} |
| 112 | + printf "\n" |
| 113 | + exit 0 |
| 114 | +fi |
| 115 | +# deploy files to GCP instances |
| 116 | +if [[ $KEEP_CODE_OPTION = "--no-keep-code" ]]; then |
| 117 | + printf "\n* >> Deploying files for instance ${TEST_TARGET_ADDR} *********************************************************\n\n" |
| 118 | + gcloud compute ssh ${TEST_TARGET_ADDR} --command "sudo rm -rf ~/ain-blockchain; mkdir ~/ain-blockchain" --project $PROJECT_ID --zone ${TEST_ZONE} |
| 119 | + gcloud compute scp --recurse $FILES_FOR_TEST ${TEST_TARGET_ADDR}:~/ain-blockchain/ --project $PROJECT_ID --zone ${TEST_ZONE} |
| 120 | +fi |
| 121 | + |
| 122 | +# ssh into each instance, set up the ubuntu VM instance (ONLY NEEDED FOR THE FIRST TIME) |
| 123 | +if [[ $SETUP_OPTION = "--setup" ]]; then |
| 124 | + printf "\n* >> Setting up instance ${TEST_TARGET_ADDR} *********************************************************\n\n" |
| 125 | + gcloud compute ssh ${TEST_TARGET_ADDR} --command "cd ./ain-blockchain; . setup_blockchain_ubuntu.sh" --project $PROJECT_ID --zone ${TEST_ZONE} |
| 126 | +fi |
| 127 | + |
| 128 | +if [[ $KEEP_CODE_OPTION = "--no-keep-code" ]]; then |
| 129 | + printf "\n* >> Installing node modules on instance ${TEST_TARGET_ADDR} *********************************************************\n\n" |
| 130 | + gcloud compute ssh ${TEST_TARGET_ADDR} --command "cd ./ain-blockchain; yarn install --ignore-engines" --project $PROJECT_ID --zone ${TEST_ZONE} |
| 131 | +fi |
| 132 | + |
| 133 | +# stop test servers first |
| 134 | +stop_servers |
| 135 | + |
| 136 | +printf "\n* >> Running tests on instance ${TEST_TARGET_ADDR} *********************************************************\n\n" |
| 137 | +if [[ $BACKGROUND_OPTION = "--bg" ]]; then |
| 138 | + TEST_CMD="cd ./ain-blockchain; nohup yarn run ${TESTING_OPTION} > test_log.txt &" |
| 139 | +else |
| 140 | + TEST_CMD="cd ./ain-blockchain; yarn run ${TESTING_OPTION}" |
| 141 | +fi |
| 142 | +printf "\nTEST_CMD=$TEST_CMD\n\n" |
| 143 | +gcloud compute ssh ${TEST_TARGET_ADDR} --command "$TEST_CMD" --project $PROJECT_ID --zone ${TEST_ZONE} |
0 commit comments