Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
wleepang committed Mar 14, 2020
1 parent c7ccdd2 commit 4cf51ac
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions bin/create-ebs-volume
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,12 @@ fi
alphabet=( {b..z} )

function get_next_logical_device() {
for letter in alphabet; do
if [ ! -e "/dev/sd${letter}" ]; then
for letter in ${alphabet[@]}; do
if [ ! -b "/dev/sd${letter}" ]; then
echo "/dev/sd${letter}"
return 0
break
fi
done
return 1
}

function get_metadata() {
Expand All @@ -133,12 +132,12 @@ function create_and_attach_volume() {
--filters Name=attachment.instance-id,Values=$instance_id
)

if [ "`echo $attached_volumes | jq '.Volumes | length'`" -eq MAX_ATTACHED_VOLUMES ]; then
if [ "`echo $attached_volumes | jq '.Volumes | length'`" -eq "$MAX_ATTACHED_VOLUMES" ]; then
error "maximum number of attached volumes reached ($MAX_ATTACHED_VOLUMES)"
fi

local device=$(get_next_logical_device)
if [ ! "$?" -eq 0 ]; then
if [ -z "$device" ]; then
error "could not find unused device"
fi

Expand All @@ -152,7 +151,7 @@ function create_and_attach_volume() {
)
local volume_id=`echo $volume | jq -r '.VolumeId'`

aws ec2 wait volume-available --volume-ids $volume_id
aws ec2 wait volume-available --region $region --volume-ids $volume_id

# Need to assure that the created volume is successfully attached to be
# cost efficient. If attachment fails, delete the volume.
Expand All @@ -161,13 +160,15 @@ function create_and_attach_volume() {
--region $region \
--device $device \
--instance-id $instance_id \
--volume-id $volume_id
--volume-id $volume_id \
> /dev/null

status="$?"
if [ ! "$status" -eq 0 ]; then
aws ec2 delete-volume \
--region $region \
--volume-id $volume_id
--volume-id $volume_id \
> /dev/null

error "could not attach volume to instance"
fi
Expand All @@ -183,7 +184,9 @@ function create_and_attach_volume() {
# set volume delete on termination
aws ec2 modify-instance-attribute \
--region $region \
--block-device-mappings DeviceName=$device,Ebs={DeleteOnTermination=true,VolumeId=$volume_id}
--instance-id $instance_id \
--block-device-mappings "DeviceName=$device,Ebs={DeleteOnTermination=true,VolumeId=$volume_id}" \
> /dev/null

echo $device
}
Expand Down

0 comments on commit 4cf51ac

Please sign in to comment.