From bd4745bc89f626c02eca216aa0603bcd03a5ae10 Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Sat, 29 Mar 2014 21:49:51 +0100 Subject: [PATCH] Support --terminate option --- ec2-stop-instance | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/ec2-stop-instance b/ec2-stop-instance index a8cad72..4c40b3a 100755 --- a/ec2-stop-instance +++ b/ec2-stop-instance @@ -22,10 +22,11 @@ Supported OPTIONS: --help display this help text --region specify region that should be used (if unset defaults to the eu-west-1 zone) + --terminate terminate instead of just stopping the instance " } -CMDLINE_OPTS=help,region: +CMDLINE_OPTS=help,region:,terminate _opt_temp=$(getopt --name $0 -o +bch --long $CMDLINE_OPTS -- "$@") if [ $? -ne 0 ]; then @@ -35,6 +36,7 @@ fi eval set -- "$_opt_temp" _opt_public=false +_opt_terminate=false while :; do case "$1" in @@ -44,6 +46,9 @@ while :; do --region) shift; AWS_REGION="$1" ;; + --terminate) + _opt_terminate=true + ;; --) shift; break ;; @@ -91,9 +96,18 @@ fi INSTANCE_ID="$1" -echo "Stopping Instance ID $INSTANCE_ID" -ec2-stop-instances --region "$AWS_REGION" "$INSTANCE_ID" -if [ $? -ne 0 ] ; then - echo "Noticed problem when trying to stop Instance with ID $INSTANCE_ID" >&2 - exit 1 +if $_opt_terminate ; then + echo "Terminating instance ID $INSTANCE_ID (as requested via --terminate)" + ec2-terminate-instances --region "$AWS_REGION" "$INSTANCE_ID" + if [ $? -ne 0 ] ; then + echo "Noticed problem when trying to terminate instance with ID $INSTANCE_ID" >&2 + exit 1 + fi +else + echo "Stopping Instance ID $INSTANCE_ID" + ec2-stop-instances --region "$AWS_REGION" "$INSTANCE_ID" + if [ $? -ne 0 ] ; then + echo "Noticed problem when trying to stop instance with ID $INSTANCE_ID" >&2 + exit 1 + fi fi