diff --git a/ec2-create-ce b/ec2-create-ce index 1449608..5a19bd4 100755 --- a/ec2-create-ce +++ b/ec2-create-ce @@ -36,12 +36,14 @@ Supported OPTIONS: (if unset defaults to ) --public Make AMI available to the public (if unset defaults to private) + --remove-existing-ami If an AMI with the according name already exists get + rid of it before trying to creating the AMI --region specify region that should be used (if unset defaults to the eu-west-1 zone) " } -CMDLINE_OPTS=base-ami:,help,instance-type:,ngcp-release:,public,region: +CMDLINE_OPTS=base-ami:,help,instance-type:,ngcp-release:,public,region:,remove-existing-ami _opt_temp=$(getopt --name $0 -o +bch --long $CMDLINE_OPTS -- "$@") if [ $? -ne 0 ]; then @@ -51,6 +53,7 @@ fi eval set -- "$_opt_temp" _opt_public=false +_opt_remove_existing_ami=false while :; do case "$1" in @@ -72,6 +75,9 @@ while :; do --region) shift; AWS_REGION="$1" ;; + --remove-existing-ami) + _opt_remove_existing_ami=true + ;; --) shift; break ;; @@ -209,6 +215,18 @@ else exit 1 fi +if ec2-describe-images --region "$AWS_REGION" --filter "name=${AMI_NAME}" | grep -q . ; then + echo "*** Warning, AMI with name ${AMI_NAME} exists already." + if $_opt_remove_existing_ami ; then + echo "*** Option --remove-existing-ami is set, removing existing AMI with name ${AMI_NAME} ***" + ec2-deregister --region "$AWS_REGION" "${AMI_NAME}" + if [ $? -ne 0 ] ; then + echo "Noticed problem when trying to delete AMI with name ${AMI_NAME}." >&2 + exit 1 + fi + fi +fi + LOGFILE_AMI=$(mktemp) ec2-create-image --region "$AWS_REGION" \ --name "$AMI_NAME" \