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

Commit 5fc5178

Browse files
authored
Merge pull request #64 from startupturbo/dmius-ebs-size-option
ebs-disk-size and before-init-sql-code options added
2 parents 58ed2e2 + 97c70ad commit 5fc5178

File tree

4 files changed

+105
-16
lines changed

4 files changed

+105
-16
lines changed

nancy_run.sh

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
DEBUG=0
44
CURRENT_TS=$(date +%Y%m%d_%H%M%S%N_%Z)
5-
DOCKER_MACHINE="${DOCKER_MACHINE:-nancy-$CURRENT_TS}"
5+
DOCKER_MACHINE="nancy-$CURRENT_TS"
66
DOCKER_MACHINE="${DOCKER_MACHINE//_/-}"
77
DEBUG_TIMEOUT=0
88
EBS_SIZE_MULTIPLIER=15
@@ -198,6 +198,9 @@ while true; do
198198
--after-db-init-code )
199199
#s3 url|filename|content
200200
AFTER_DB_INIT_CODE="$2"; shift 2 ;;
201+
--before-db-init-code )
202+
#s3 url|filename|content
203+
BEFORE_DB_INIT_CODE="$2"; shift 2 ;;
201204
--workload-real )
202205
#s3 url
203206
WORKLOAD_REAL="$2"; shift 2 ;;
@@ -236,15 +239,16 @@ while true; do
236239
TMP_PATH="$2"; shift 2 ;;
237240
--debug-timeout )
238241
DEBUG_TIMEOUT="$2"; shift 2 ;;
239-
-- )
240-
>&2 echo "ERROR: Invalid option '$1'"
241-
exit 1;
242-
break ;;
242+
--ebs-volume-size )
243+
EBS_VOLUME_SIZE="$2"; shift 2 ;;
243244
* )
244-
if [ "${1:0:2}" == "--" ]; then
245+
option=$1
246+
option="${option##*( )}"
247+
option="${option%%*( )}"
248+
if [ "${option:0:2}" == "--" ]; then
245249
>&2 echo "ERROR: Invalid option '$1'. Please double-check options."
246250
exit 1
247-
elif [ "${1:0:2}" != "" ]; then
251+
elif [ "$option" != "" ]; then
248252
>&2 echo "ERROR: \"nancy run\" does not support payload (except \"help\"). Use options, see \"nancy run help\")"
249253
exit 1
250254
fi
@@ -276,6 +280,8 @@ if [ $DEBUG -eq 1 ]; then
276280
echo "s3-cfg-path: $S3_CFG_PATH"
277281
echo "tmp-path: $TMP_PATH"
278282
echo "after-db-init-code: $AFTER_DB_INIT_CODE"
283+
echo "before-db-init-code: $BEFORE_DB_INIT_CODE"
284+
echo "ebs-volume-size: $EBS_VOLUME_SIZE"
279285
fi
280286

281287
function checkPath() {
@@ -467,6 +473,17 @@ function checkParams() {
467473
fi
468474
fi
469475

476+
if [ ! -z ${BEFORE_DB_INIT_CODE+x} ]; then
477+
checkPath BEFORE_DB_INIT_CODE
478+
if [ "$?" -ne "0" ]; then
479+
#>&2 echo "WARNING: Value given as after_db_init_code: '$AFTER_DB_INIT_CODE' not found as file will use as content"
480+
echo "$BEFORE_DB_INIT_CODE" > $TMP_PATH/before_db_init_code_tmp.sql
481+
BEFORE_DB_INIT_CODE="$TMP_PATH/before_db_init_code_tmp.sql"
482+
else
483+
[ "$DEBUG" -eq "1" ] && echo "DEBUG: Value given as before_db_init_code will use as filename"
484+
fi
485+
fi
486+
470487
if [ ! -z ${TARGET_DDL_DO+x} ]; then
471488
checkPath TARGET_DDL_DO
472489
if [ "$?" -ne "0" ]; then
@@ -499,32 +516,50 @@ function checkParams() {
499516
[ "$DEBUG" -eq "1" ] && echo "DEBUG: Value given as target_config will use as filename"
500517
fi
501518
fi
519+
520+
if [ ! -z ${EBS_VOLUME_SIZE+x} ]; then
521+
if [ "$RUN_ON" == "localhost" ] || [ ${AWS_EC2_TYPE:0:2} == 'i3' ]; then
522+
>&2 echo "WARNING: ebs-volume-size is not required for aws i3 aws instances and local execution."
523+
fi;
524+
re='^[0-9]+$'
525+
if ! [[ $EBS_VOLUME_SIZE =~ $re ]] ; then
526+
>&2 echo "ERROR: ebs-volume-size must be numeric integer value."
527+
exit 1;
528+
fi
529+
else
530+
if [ ! ${AWS_EC2_TYPE:0:2} == 'i3' ]; then
531+
>&2 echo "WARNING: ebs-volume-size is not given, will be calculate on base of dump size."
532+
fi
533+
fi
502534
}
503535

504536
checkParams;
505537

506538
# Determine dump file size
507-
if [ ! -z ${DB_DUMP_PATH+x} ]; then
539+
if ([ "$RUN_ON" == "aws" ] && [ ! ${AWS_EC2_TYPE:0:2} == "i3" ] && \
540+
[ -z ${EBS_VOLUME_SIZE+x} ] && [ ! -z ${DB_DUMP_PATH+x} ]); then
541+
echo "Calculate EBS volume size."
508542
dumpFileSize=0
509543
if [[ $DB_DUMP_PATH =~ "s3://" ]]; then
510544
dumpFileSize=$(s3cmd info $DB_DUMP_PATH | grep "File size:" )
511545
dumpFileSize=${dumpFileSize/File size:/}
512546
dumpFileSize=${dumpFileSize/\t/}
513547
dumpFileSize=${dumpFileSize// /}
514-
#echo "S3 FILESIZE: $dumpFileSize"
548+
[ $DEBUG -eq 1 ] && echo "S3 FILESIZE: $dumpFileSize"
515549
else
516550
dumpFileSize=$(stat -c%s "$DB_DUMP_PATH")
517551
fi
518-
[ $DEBUG -eq 1 ] && echo "Dump filesize: $dumpFileSize bytes"
552+
let dumpFileSize=dumpFileSize*$EBS_SIZE_MULTIPLIER
519553
KB=1024
520554
let minSize=300*$KB*$KB*$KB
521555
ebsSize=$minSize # 300 GB
522556
if [ "$dumpFileSize" -gt "$minSize" ]; then
523557
let ebsSize=$dumpFileSize
524-
let ebsSize=$ebsSize*$EBS_SIZE_MULTIPLIER
525558
ebsSize=$(numfmt --to-unit=G $ebsSize)
526-
EBS_SIZE=$ebsSize
527-
[ $DEBUG -eq 1 ] && echo "EBS Size: $EBS_SIZE Gb"
559+
EBS_VOLUME_SIZE=$ebsSize
560+
[ $DEBUG -eq 1 ] && echo "EBS volume size: $EBS_VOLUME_SIZE Gb"
561+
else
562+
echo "EBS volume is not require."
528563
fi
529564
fi
530565

@@ -594,6 +629,7 @@ function cleanupAndExit {
594629
echo "Remove temp files..." # if exists
595630
docker $dockerConfig exec -i ${containerHash} sh -c "sudo rm -rf $MACHINE_HOME"
596631
rm -f "$TMP_PATH/after_db_init_code_tmp.sql"
632+
rm -f "$TMP_PATH/before_db_init_code_tmp.sql"
597633
rm -f "$TMP_PATH/workload_custom_sql_tmp.sql"
598634
rm -f "$TMP_PATH/target_ddl_do_tmp.sql"
599635
rm -f "$TMP_PATH/target_ddl_undo_tmp.sql"
@@ -716,10 +752,10 @@ elif [[ "$RUN_ON" = "aws" ]]; then
716752
else
717753
echo "Attempt use external disk"
718754
# Create new volume and attach them for non i3 instances if needed
719-
if [ ! -z ${EBS_SIZE+x} ]; then
755+
if [ ! -z ${EBS_VOLUME_SIZE+x} ]; then
720756
echo "Create and attach EBS volume"
721-
[ $DEBUG -eq 1 ] && echo "Create volume with size: $EBS_SIZE Gb"
722-
VOLUME_ID=$(aws ec2 create-volume --size $EBS_SIZE --region us-east-1 --availability-zone us-east-1a --volume-type gp2 | jq -r .VolumeId)
757+
[ $DEBUG -eq 1 ] && echo "Create volume with size: $EBS_VOLUME_SIZE Gb"
758+
VOLUME_ID=$(aws ec2 create-volume --size $EBS_VOLUME_SIZE --region us-east-1 --availability-zone us-east-1a --volume-type gp2 | jq -r .VolumeId)
723759
INSTANCE_ID=$(docker-machine ssh $DOCKER_MACHINE curl -s http://169.254.169.254/latest/meta-data/instance-id)
724760
sleep 10 # wait to volume will ready
725761
attachResult=$(aws ec2 attach-volume --device /dev/xvdf --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --region us-east-1)
@@ -792,6 +828,13 @@ function copyFile() {
792828
# Dump
793829
sleep 2 # wait for postgres up&running
794830

831+
echo "Apply sql code before db init"
832+
if ([ ! -z ${BEFORE_DB_INIT_CODE+x} ] && [ "$BEFORE_DB_INIT_CODE" != "" ])
833+
then
834+
BEFORE_DB_INIT_CODE_FILENAME=$(basename $BEFORE_DB_INIT_CODE)
835+
copyFile $BEFORE_DB_INIT_CODE
836+
docker_exec bash -c "psql --set ON_ERROR_STOP=on -U postgres test -b -f $MACHINE_HOME/$BEFORE_DB_INIT_CODE_FILENAME"
837+
fi
795838
echo "Restore database dump"
796839
case "$DB_DUMP_EXT" in
797840
sql)

tests/nancy_run_before_init_code.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
output=$(${BASH_SOURCE%/*}/../nancy run \
4+
--before-db-init-code "select abs from beforeinittable;" \
5+
--workload-custom-sql "file://$srcDir/custom.sql" \
6+
--db-dump-path "file://$srcDir/test.dump.bz2" \
7+
--tmp-path $srcDir/tmp \
8+
2>&1)
9+
10+
if [[ $output =~ "ERROR: relation \"beforeinittable\" does not exist" ]]; then
11+
echo -e "\e[36mOK\e[39m"
12+
else
13+
>&2 echo -e "\e[31mFAILED\e[39m"
14+
>&2 echo -e "Output: $output"
15+
exit 1
16+
fi

tests/nancy_run_ebs_disk_size.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
output=$(${BASH_SOURCE%/*}/../nancy run \
4+
--debug \
5+
--ebs-volume-size 37 \
6+
2>&1)
7+
8+
if [[ $output =~ "ebs-volume-size: 37" ]]; then
9+
echo -e "\e[36mOK\e[39m"
10+
else
11+
>&2 echo -e "\e[31mFAILED\e[39m"
12+
>&2 echo -e "Output: $output"
13+
exit 1
14+
fi
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
output=$(${BASH_SOURCE%/*}/../nancy run \
4+
--ebs-volume-size sa \
5+
--workload-custom-sql "file://$srcDir/custom.sql" \
6+
--db-dump-path "file://$srcDir/test.dump.bz2" \
7+
--tmp-path $srcDir/tmp \
8+
2>&1)
9+
10+
if [[ $output =~ "WARNING: ebs-volume-size is not required for aws i3 aws instances and local execution." ]]; then
11+
echo -e "\e[36mOK\e[39m"
12+
else
13+
>&2 echo -e "\e[31mFAILED\e[39m"
14+
>&2 echo -e "Output: $output"
15+
exit 1
16+
fi

0 commit comments

Comments
 (0)