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

Commit 94f2122

Browse files
committed
polish code style: single cleanup() function; identation
1 parent f0f6720 commit 94f2122

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

nancy_run.sh

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,6 @@ if [[ "$RUN_ON" = "localhost" ]]; then
284284
-dit "postgresmen/postgres-with-stuff:pg${PG_VERSION}" \
285285
)
286286
dockerConfig=""
287-
288-
function cleanup {
289-
echo "Remove docker container"
290-
docker container rm -f $containerHash
291-
echo "Remove temp files..."
292-
rm -f "$TMP_PATH/conf_$DOCKER_MACHINE.tmp"
293-
rm -f "$TMP_PATH/ddl_do_$DOCKER_MACHINE.sql"
294-
rm -f "$TMP_PATH/ddl_undo_$DOCKER_MACHINE.sql"
295-
rm -f "$TMP_PATH/queries_custom_$DOCKER_MACHINE.sql"
296-
rm -rf "$TMP_PATH/pg_nancy_home_${CURRENT_TS}"
297-
echo "Done."
298-
}
299287
elif [[ "$RUN_ON" = "aws" ]]; then
300288
## Get max price from history and apply multiplier
301289
prices=$(aws --region=us-east-1 ec2 describe-spot-price-history --instance-types $AWS_EC2_TYPE --no-paginate --start-time=$(date +%s) --product-descriptions="Linux/UNIX (Amazon VPC)" --query 'SpotPriceHistory[*].{az:AvailabilityZone, price:SpotPrice}')
@@ -312,54 +300,60 @@ elif [[ "$RUN_ON" = "aws" ]]; then
312300
status=$(waitEC2Ready "docker-machine create" "$DOCKER_MACHINE" 1)
313301
if [ "$status" == "price-too-low" ]
314302
then
315-
echo "Price $price is too low for $AWS_EC2_TYPE instance. Try detect actual."
316-
corrrectPriceForLastFailedRequest=$(aws ec2 describe-spot-instance-requests --filters="Name=launch.instance-type,Values=$AWS_EC2_TYPE" | jq '.SpotInstanceRequests[] | select(.Status.Code == "price-too-low") | .Status.Message' | grep -Eo '[0-9]+[.][0-9]+' | tail -n 1 &)
317-
if [ "$corrrectPriceForLastFailedRequest" != "" ] && [ "$corrrectPriceForLastFailedRequest" != "null" ]
318-
then
319-
EC2_PRICE=$corrrectPriceForLastFailedRequest
320-
#update docker machine name
321-
CURRENT_TS=$(date +%Y%m%d_%H%M%S%N_%Z)
322-
DOCKER_MACHINE="nancy-$CURRENT_TS"
323-
DOCKER_MACHINE="${DOCKER_MACHINE//_/-}"
324-
#try start docker machine name with new price
325-
echo "Attempt to create a new docker machine: $DOCKER_MACHINE with price: $EC2_PRICE."
326-
createDockerMachine;
327-
waitEC2Ready "docker-machine create" "$DOCKER_MACHINE" 0;
328-
else
329-
>&2 echo "ERROR: Cannot determine actual price for the instance $AWS_EC2_TYPE."
330-
exit 1;
331-
fi
303+
echo "Price $price is too low for $AWS_EC2_TYPE instance. Try detect actual."
304+
corrrectPriceForLastFailedRequest=$(aws ec2 describe-spot-instance-requests --filters="Name=launch.instance-type,Values=$AWS_EC2_TYPE" | jq '.SpotInstanceRequests[] | select(.Status.Code == "price-too-low") | .Status.Message' | grep -Eo '[0-9]+[.][0-9]+' | tail -n 1 &)
305+
if [ "$corrrectPriceForLastFailedRequest" != "" ] && [ "$corrrectPriceForLastFailedRequest" != "null" ]; then
306+
EC2_PRICE=$corrrectPriceForLastFailedRequest
307+
#update docker machine name
308+
CURRENT_TS=$(date +%Y%m%d_%H%M%S%N_%Z)
309+
DOCKER_MACHINE="nancy-$CURRENT_TS"
310+
DOCKER_MACHINE="${DOCKER_MACHINE//_/-}"
311+
#try start docker machine name with new price
312+
echo "Attempt to create a new docker machine: $DOCKER_MACHINE with price: $EC2_PRICE."
313+
createDockerMachine;
314+
waitEC2Ready "docker-machine create" "$DOCKER_MACHINE" 0;
315+
else
316+
>&2 echo "ERROR: Cannot determine actual price for the instance $AWS_EC2_TYPE."
317+
exit 1;
318+
fi
332319
fi
333320

334321
echo "Check a docker machine status."
335322
res=$(docker-machine status $DOCKER_MACHINE 2>&1 &)
336323
if [ "$res" != "Running" ]
337324
then
338-
>&2 echo "Failed: Docker $DOCKER_MACHINE is NOT running."
339-
exit 1;
325+
>&2 echo "Failed: Docker $DOCKER_MACHINE is NOT running."
326+
exit 1;
340327
fi
341328

342329
echo "Docker $DOCKER_MACHINE is running."
343330

344331
containerHash=$(docker `docker-machine config $DOCKER_MACHINE` run --name="pg_nancy_${CURRENT_TS}" \
345332
-v /home/ubuntu:/machine_home -dit "postgresmen/postgres-with-stuff:pg${PG_VERSION}")
346333
dockerConfig=$(docker-machine config $DOCKER_MACHINE)
347-
348-
function cleanup {
349-
cmdout=$(docker-machine rm --force $DOCKER_MACHINE)
350-
echo "Finished working with machine $DOCKER_MACHINE, termination requested, current status: $cmdout"
351-
echo "Remove temp files..."
352-
rm -f "$TMP_PATH/conf_$DOCKER_MACHINE.tmp"
353-
rm -f "$TMP_PATH/ddl_do_$DOCKER_MACHINE.sql"
354-
rm -f "$TMP_PATH/ddl_undo_$DOCKER_MACHINE.sql"
355-
rm -f "$TMP_PATH/queries_custom_$DOCKER_MACHINE.sql"
356-
echo "Done."
357-
}
358334
else
359335
>&2 echo "ASSERT: must not reach this point"
360336
exit 1
361337
fi
362338

339+
function cleanup {
340+
echo "Remove temp files..."
341+
rm -f "$TMP_PATH/conf_$DOCKER_MACHINE.tmp"
342+
rm -f "$TMP_PATH/ddl_do_$DOCKER_MACHINE.sql"
343+
rm -f "$TMP_PATH/ddl_undo_$DOCKER_MACHINE.sql"
344+
rm -f "$TMP_PATH/queries_custom_$DOCKER_MACHINE.sql"
345+
if [ "$RUN_ON" = "localhost" ]; then
346+
rm -rf "$TMP_PATH/pg_nancy_home_${CURRENT_TS}"
347+
echo "Remove docker container"
348+
docker container rm -f $containerHash
349+
elif [ "$RUN_ON" = "aws" ]; then
350+
cmdout=$(docker-machine rm --force $DOCKER_MACHINE)
351+
echo "Finished working with machine $DOCKER_MACHINE, termination requested, current status: $cmdout"
352+
else
353+
>&2 echo "ASSERT: must not reach this point"
354+
exit 1
355+
fi
356+
}
363357
trap cleanup EXIT
364358

365359
alias docker_exec='docker $dockerConfig exec -i pg_nancy_${CURRENT_TS} '

0 commit comments

Comments
 (0)