Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit 751c405

Browse files
dmiusDmitry
authored andcommitted
Region param added to nancy run
1 parent 4ed208b commit 751c405

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

nancy_run.sh

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function msg() {
7171
# 1 if the input is empty,
7272
# -1 otherwise.
7373
#######################################
74-
function checkPath() {
74+
function check_path() {
7575
if [[ -z $1 ]]; then
7676
return 1
7777
fi
@@ -112,7 +112,8 @@ function checkPath() {
112112
# (text) [5] AWS keypair to use
113113
# (text) [6] Path to Private Key file to use for instance
114114
# Matching public key with .pub extension should exist
115-
# (text) [7] The AWS zone to launch the instance in (one of a,b,c,d,e)
115+
# (text) [7] The AWS region to launch the instance (for example: us-east-1, eu-central-1)
116+
# (text) [8] The AWS zone to launch the instance in (one of a,b,c,d,e)
116117
# Returns:
117118
# None
118119
#######################################
@@ -125,7 +126,8 @@ function create_ec2_docker_machine() {
125126
--amazonec2-block-duration-minutes=$4 \
126127
--amazonec2-keypair-name="$5" \
127128
--amazonec2-ssh-keypath="$6" \
128-
--amazonec2-zone $7 \
129+
--amazonec2-region="$7" \
130+
--amazonec2-zone="$8" \
129131
$1 2> >(grep -v "failed waiting for successful resource state" >&2) &
130132
}
131133

@@ -162,7 +164,7 @@ function destroy_docker_machine() {
162164
# None
163165
#######################################
164166
function wait_ec2_docker_machine_ready() {
165-
machine=$1
167+
local machine=$1
166168
local check_price=$2
167169
while true; do
168170
sleep 5;
@@ -171,7 +173,7 @@ function wait_ec2_docker_machine_ready() {
171173
((stop_now==1)) && return 0
172174
if $check_price ; then
173175
status=$( \
174-
aws ec2 describe-spot-instance-requests \
176+
aws --region=$AWS_REGION ec2 describe-spot-instance-requests \
175177
--filters="Name=launch.instance-type,Values=$AWS_EC2_TYPE" \
176178
| jq '.SpotInstanceRequests | sort_by(.CreateTime) | .[] | .Status.Code' \
177179
| tail -n 1
@@ -204,7 +206,7 @@ function cleanup_and_exit {
204206
if [ ! -z ${VOLUME_ID+x} ]; then
205207
msg "Wait and delete volume $VOLUME_ID"
206208
sleep 60 # wait for the machine to be removed
207-
delvolout=$(aws ec2 delete-volume --volume-id $VOLUME_ID)
209+
delvolout=$(aws --region=$AWS_REGION ec2 delete-volume --volume-id $VOLUME_ID)
208210
msg "Volume $VOLUME_ID deleted"
209211
fi
210212
else
@@ -498,7 +500,8 @@ while [ $# -gt 0 ]; do
498500
AWS_SSH_KEY_PATH="$2"; shift 2 ;;
499501
--aws-ebs-volume-size )
500502
AWS_EBS_VOLUME_SIZE="$2"; shift 2 ;;
501-
503+
--aws-region )
504+
AWS_REGION="$2"; shift 2 ;;
502505
--s3cfg-path )
503506
S3_CFG_PATH="$2"; shift 2 ;;
504507
* )
@@ -556,12 +559,16 @@ if [[ "$RUN_ON" == "aws" ]]; then
556559
err "ERROR: AWS keypair name and ssh key file must be specified to run on AWS EC2."
557560
exit 1
558561
else
559-
checkPath AWS_SSH_KEY_PATH
562+
check_path AWS_SSH_KEY_PATH
560563
fi
561564
if [[ -z ${AWS_EC2_TYPE+x} ]]; then
562565
err "ERROR: AWS EC2 Instance type not given."
563566
exit 1
564567
fi
568+
if [[ -z ${AWS_REGION+x} ]]; then
569+
err "ERROR: AWS EC2 region not given."
570+
exit 1
571+
fi
565572
elif [[ "$RUN_ON" == "localhost" ]]; then
566573
if [[ ! -z ${AWS_KEYPAIR_NAME+x} ]] || [[ ! -z ${AWS_SSH_KEY_PATH+x} ]] ; then
567574
err "ERROR: options '--aws-keypair-name' and '--aws-ssh-key-path' must be used with '--run on aws'."
@@ -575,6 +582,10 @@ elif [[ "$RUN_ON" == "localhost" ]]; then
575582
err "ERROR: option '--aws-ebs-volume-size' must be used with '--run on aws'."
576583
exit 1
577584
fi
585+
if [[ ! -z ${AWS_REGION+x} ]]; then
586+
err "ERROR: option '--aws-region' must be used with '--run on aws'."
587+
exit 1
588+
fi
578589
else
579590
err "ERROR: incorrect value for option --run-on"
580591
exit 1
@@ -626,7 +637,7 @@ if [[ ! -z ${DB_PREPARED_SNAPSHOT+x} ]] && [[ ! -z ${DB_DUMP+x} ]]; then
626637
fi
627638

628639
if [[ ! -z ${DB_DUMP+x} ]]; then
629-
checkPath DB_DUMP
640+
check_path DB_DUMP
630641
if [[ "$?" -ne "0" ]]; then
631642
echo "$DB_DUMP" > $TMP_PATH/db_dump_tmp.sql
632643
DB_DUMP="$TMP_PATH/db_dump_tmp.sql"
@@ -639,7 +650,7 @@ if [[ -z ${PG_CONFIG+x} ]]; then
639650
err "NOTICE: No PostgreSQL config is provided. Will use default."
640651
# TODO(NikolayS) use "auto-tuning" – shared_buffers=1/4 RAM, etc
641652
else
642-
checkPath PG_CONFIG
653+
check_path PG_CONFIG
643654
if [[ "$?" -ne "0" ]]; then # TODO(NikolayS) support file:// and s3://
644655
#err "WARNING: Value given as pg_config: '$PG_CONFIG' not found as file will use as content"
645656
echo "$PG_CONFIG" > $TMP_PATH/pg_config_tmp.sql
@@ -665,18 +676,18 @@ if [[ -z ${ARTIFACTS_FILENAME+x} ]]; then
665676
ARTIFACTS_FILENAME=$DOCKER_MACHINE
666677
fi
667678

668-
if [[ ! -z ${WORKLOAD_REAL+x} ]] && ! checkPath WORKLOAD_REAL; then
679+
if [[ ! -z ${WORKLOAD_REAL+x} ]] && ! check_path WORKLOAD_REAL; then
669680
err "ERROR: workload file '$WORKLOAD_REAL' not found."
670681
exit 1
671682
fi
672683

673-
if [[ ! -z ${WORKLOAD_BASIS+x} ]] && ! checkPath WORKLOAD_BASIS; then
684+
if [[ ! -z ${WORKLOAD_BASIS+x} ]] && ! check_path WORKLOAD_BASIS; then
674685
err "ERROR: workload file '$WORKLOAD_BASIS' not found."
675686
exit 1
676687
fi
677688

678689
if [[ ! -z ${WORKLOAD_CUSTOM_SQL+x} ]]; then
679-
checkPath WORKLOAD_CUSTOM_SQL
690+
check_path WORKLOAD_CUSTOM_SQL
680691
if [[ "$?" -ne "0" ]]; then
681692
#err "WARNING: Value given as workload-custom-sql: '$WORKLOAD_CUSTOM_SQL' not found as file will use as content"
682693
echo "$WORKLOAD_CUSTOM_SQL" > $TMP_PATH/workload_custom_sql_tmp.sql
@@ -685,7 +696,7 @@ if [[ ! -z ${WORKLOAD_CUSTOM_SQL+x} ]]; then
685696
fi
686697

687698
if [[ ! -z ${COMMANDS_AFTER_CONTAINER_INIT+x} ]]; then
688-
checkPath COMMANDS_AFTER_CONTAINER_INIT
699+
check_path COMMANDS_AFTER_CONTAINER_INIT
689700
if [[ "$?" -ne "0" ]]; then
690701
#err "WARNING: Value given as after_db_init_code: '$COMMANDS_AFTER_CONTAINER_INIT' not found as file will use as content"
691702
echo "$COMMANDS_AFTER_CONTAINER_INIT" > $TMP_PATH/after_docker_init_code_tmp.sh
@@ -694,15 +705,15 @@ if [[ ! -z ${COMMANDS_AFTER_CONTAINER_INIT+x} ]]; then
694705
fi
695706

696707
if [[ ! -z ${SQL_AFTER_DB_RESTORE+x} ]]; then
697-
checkPath SQL_AFTER_DB_RESTORE
708+
check_path SQL_AFTER_DB_RESTORE
698709
if [[ "$?" -ne "0" ]]; then
699710
echo "$SQL_AFTER_DB_RESTORE" > $TMP_PATH/after_db_init_code_tmp.sql
700711
SQL_AFTER_DB_RESTORE="$TMP_PATH/after_db_init_code_tmp.sql"
701712
fi
702713
fi
703714

704715
if [[ ! -z ${SQL_BEFORE_DB_RESTORE+x} ]]; then
705-
checkPath SQL_BEFORE_DB_RESTORE
716+
check_path SQL_BEFORE_DB_RESTORE
706717
if [[ "$?" -ne "0" ]]; then
707718
#err "WARNING: Value given as before_db_init_code: '$SQL_BEFORE_DB_RESTORE' not found as file will use as content"
708719
echo "$SQL_BEFORE_DB_RESTORE" > $TMP_PATH/before_db_init_code_tmp.sql
@@ -711,23 +722,23 @@ if [[ ! -z ${SQL_BEFORE_DB_RESTORE+x} ]]; then
711722
fi
712723

713724
if [[ ! -z ${DELTA_SQL_DO+x} ]]; then
714-
checkPath DELTA_SQL_DO
725+
check_path DELTA_SQL_DO
715726
if [[ "$?" -ne "0" ]]; then
716727
echo "$DELTA_SQL_DO" > $TMP_PATH/target_ddl_do_tmp.sql
717728
DELTA_SQL_DO="$TMP_PATH/target_ddl_do_tmp.sql"
718729
fi
719730
fi
720731

721732
if [[ ! -z ${DELTA_SQL_UNDO+x} ]]; then
722-
checkPath DELTA_SQL_UNDO
733+
check_path DELTA_SQL_UNDO
723734
if [[ "$?" -ne "0" ]]; then
724735
echo "$DELTA_SQL_UNDO" > $TMP_PATH/target_ddl_undo_tmp.sql
725736
DELTA_SQL_UNDO="$TMP_PATH/target_ddl_undo_tmp.sql"
726737
fi
727738
fi
728739

729740
if [[ ! -z ${DELTA_CONFIG+x} ]]; then
730-
checkPath DELTA_CONFIG
741+
check_path DELTA_CONFIG
731742
if [[ "$?" -ne "0" ]]; then
732743
echo "$DELTA_CONFIG" > $TMP_PATH/target_config_tmp.conf
733744
DELTA_CONFIG="$TMP_PATH/target_config_tmp.conf"
@@ -803,7 +814,7 @@ elif [[ "$RUN_ON" == "aws" ]]; then
803814
## Get max price from history and apply multiplier
804815
# TODO detect region and/or allow to choose via options
805816
prices=$(
806-
aws --region=us-east-1 ec2 \
817+
aws --region=$AWS_REGION ec2 \
807818
describe-spot-price-history --instance-types $AWS_EC2_TYPE --no-paginate \
808819
--start-time=$(date +%s) --product-descriptions="Linux/UNIX (Amazon VPC)" \
809820
--query 'SpotPriceHistory[*].{az:AvailabilityZone, price:SpotPrice}'
@@ -821,26 +832,26 @@ elif [[ "$RUN_ON" == "aws" ]]; then
821832
msg "Increased price: $price"
822833
EC2_PRICE=$price
823834
if [ -z $zone ]; then
824-
region='a' #default zone
835+
zone='a' #default zone
825836
fi
826837

827838
create_ec2_docker_machine $DOCKER_MACHINE $AWS_EC2_TYPE $EC2_PRICE \
828-
60 $AWS_KEYPAIR_NAME $AWS_SSH_KEY_PATH $zone;
839+
60 $AWS_KEYPAIR_NAME $AWS_SSH_KEY_PATH $AWS_REGION $zone;
829840
status=$(wait_ec2_docker_machine_ready "$DOCKER_MACHINE" true)
830841
if [[ "$status" == "price-too-low" ]]; then
831842
msg "Price $price is too low for $AWS_EC2_TYPE instance. Getting the up-to-date value from the error message..."
832843

833844
#destroy_docker_machine $DOCKER_MACHINE
834845
# "docker-machine rm" doesn't work for "price-too-low" spot requests,
835846
# so we need to clean up them via aws cli interface directly
836-
aws ec2 describe-spot-instance-requests \
847+
aws --region=$AWS_REGION ec2 describe-spot-instance-requests \
837848
--filters 'Name=status-code,Values=price-too-low' \
838849
| grep SpotInstanceRequestId | awk '{gsub(/[,"]/, "", $2); print $2}' \
839-
| xargs aws ec2 cancel-spot-instance-requests \
850+
| xargs aws --region=$AWS_REGION ec2 cancel-spot-instance-requests \
840851
--spot-instance-request-ids || true
841852

842853
corrrectPriceForLastFailedRequest=$( \
843-
aws ec2 describe-spot-instance-requests \
854+
aws --region=$AWS_REGION ec2 describe-spot-instance-requests \
844855
--filters="Name=launch.instance-type,Values=$AWS_EC2_TYPE" \
845856
| jq '.SpotInstanceRequests[] | select(.Status.Code == "price-too-low") | .Status.Message' \
846857
| grep -Eo '[0-9]+[.][0-9]+' | tail -n 1 &
@@ -853,7 +864,7 @@ elif [[ "$RUN_ON" == "aws" ]]; then
853864
DOCKER_MACHINE="${DOCKER_MACHINE//_/-}"
854865
#try start docker machine name with new price
855866
create_ec2_docker_machine $DOCKER_MACHINE $AWS_EC2_TYPE $EC2_PRICE \
856-
60 $AWS_KEYPAIR_NAME $AWS_SSH_KEY_PATH $zone
867+
60 $AWS_KEYPAIR_NAME $AWS_SSH_KEY_PATH $AWS_REGION $zone
857868
wait_ec2_docker_machine_ready "$DOCKER_MACHINE" false;
858869
else
859870
err "$(date "+%Y-%m-%d %H:%M:%S") ERROR: Cannot determine actual price for the instance $AWS_EC2_TYPE."
@@ -896,10 +907,10 @@ elif [[ "$RUN_ON" == "aws" ]]; then
896907
# Create new volume and attach them for non i3 instances if needed
897908
if [ ! -z ${AWS_EBS_VOLUME_SIZE+x} ]; then
898909
msg "Create and attach a new EBS volume (size: $AWS_EBS_VOLUME_SIZE GB)"
899-
VOLUME_ID=$(aws ec2 create-volume --size $AWS_EBS_VOLUME_SIZE --region us-east-1 --availability-zone us-east-1a --volume-type gp2 | jq -r .VolumeId)
910+
VOLUME_ID=$(aws --region=$AWS_REGION ec2 create-volume --size $AWS_EBS_VOLUME_SIZE --availability-zone us-east-1a --volume-type gp2 | jq -r .VolumeId)
900911
INSTANCE_ID=$(docker-machine ssh $DOCKER_MACHINE curl -s http://169.254.169.254/latest/meta-data/instance-id)
901912
sleep 10 # wait to volume will ready
902-
attachResult=$(aws ec2 attach-volume --device /dev/xvdf --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --region us-east-1)
913+
attachResult=$(aws --region=$AWS_REGION ec2 attach-volume --device /dev/xvdf --volume-id $VOLUME_ID --instance-id $INSTANCE_ID)
903914
docker-machine ssh $DOCKER_MACHINE sudo mkfs.ext4 /dev/xvdf
904915
docker-machine ssh $DOCKER_MACHINE sudo mount /dev/xvdf /home/storage
905916
fi

0 commit comments

Comments
 (0)