Skip to content

Instantly share code, notes, and snippets.

@grrywlsn
Created January 4, 2015 21:16
Show Gist options
  • Save grrywlsn/efc91c88acfaee90a9fd to your computer and use it in GitHub Desktop.
Save grrywlsn/efc91c88acfaee90a9fd to your computer and use it in GitHub Desktop.
attach-ebs
#! /bin/bash
USERDATA=`ec2metadata --user-data`
AVAILABILITY_ZONE=`ec2metadata --availability-zone`
INSTANCE_ID=`ec2metadata --instance-id`
SPLIT_CHAR="|"
attach_volume() { # $1=instance Id, $2=device path, $3=volume Id
echo "Attaching volume $3 to instance $1 on path $2"
ATTACH_RESULT=`euca-attach-volume $3 -i $1 -d $2`
sleep 5
echo $ATTACH_RESULT
}
find_snapshot() {
echo "Finding snapshot for ${EBS_PARAMS[0]} on path ${EBS_PARAMS[3]}..."
SNAPSHOT_DETAILS=`euca-describe-snapshots --filter "tag:Name=${EBS_PARAMS[0]}" --filter "tag:Device=${EBS_PARAMS[3]}" --filter "status=completed" `
SNAPSHOT_ID=`echo $SNAPSHOT_DETAILS | awk '{print $2}'`
if [ $SNAPSHOT_ID != "" ]; then
echo "Found $SNAPSHOT_ID - creating a volume from it..."
make_volume_from_snapshot $SNAPSHOT_ID
else
echo "Hmm, no snapshots can be found to match the user-data! Skipping to next EBS..."
fi
}
make_volume_from_snapshot() { # $1=snapshot id
echo "Making ${EBS_PARAMS[2]}GB volume from snapshot $1 in $AVAILABILITY_ZONE"
VOLUME_DETAILS=`euca-create-volume --size ${EBS_PARAMS[2]} --snapshot $1 -z $AVAILABILITY_ZONE`
VOLUME_ID=`echo $VOLUME_DETAILS | awk '{print $2}'`
AVAILABLE=`euca-describe-volumes $VOLUME_ID | grep available`
until [ -n "$AVAILABLE" ]; do
echo "waiting..."
sleep 5
AVAILABLE=`euca-describe-volumes $VOLUME_ID | grep available`
done
echo "Volume $VOLUME_ID created!"
echo `euca-create-tags $VOLUME_ID --tag Name=${EBS_PARAMS[0]} --tag Device=${EBS_PARAMS[3]}`
attach_volume $INSTANCE_ID ${EBS_PARAMS[3]} $VOLUME_ID
}
echo "Going to attach some EBS volumes in $AVAILABILITY_ZONE..."
if [[ $USERDATA == *$SPLIT_CHAR* ]]; then
saveIFS=$IFS
IFS=$SPLIT_CHAR
PARAMS=($USERDATA)
IFS=$saveIFS
declare -A USERDATA_KEYVALUE
for ((i=0; i<${#PARAMS[@]}-1; i+=2))
do
USERDATA_KEYVALUE[${PARAMS[i]}]=${PARAMS[i+1]}
done
for i in {1..4}
do
if [[ -n "${USERDATA_KEYVALUE[EBS${i}]}" ]]; then
echo "Current EBS volume is ${i}: user-data is ${USERDATA_KEYVALUE[EBS${i}]}"
saveIFS=$IFS
IFS=","
EBS_PARAMS=(${USERDATA_KEYVALUE[EBS${i}]})
IFS=$saveIFS
CHECK_ATTACHED=`euca-describe-volumes --filter "attachment.instance-id=$INSTANCE_ID" --filter "attachment.device=${EBS_PARAMS[3]}"`
ALREADY_ATTACHED=`echo $CHECK_ATTACHED | awk '{print $2}'`
if [[ $ALREADY_ATTACHED != "" ]]; then
echo "It looks like $ALREADY_ATTACHED is mounted to $INSTANCE_ID on ${EBS_PARAMS[3]} already; skipping to next EBS..."
else
case ${EBS_PARAMS[1]} in
attach)
VOLUME_DETAILS=`euca-describe-volumes --filter "tag:Name=${EBS_PARAMS[0]}" --filter "tag:Device=${EBS_PARAMS[3]}" --filter "availability-zone=$AVAILABILITY_ZONE"`
VOLUME_ID=`echo $VOLUME_DETAILS | awk '{print $2}'`
if [[ $VOLUME_DETAILS == *available* ]]; then
echo "Volume $VOLUME_ID is available, attaching..."
attach_volume $INSTANCE_ID ${EBS_PARAMS[3]} $VOLUME_ID
else
echo "There doesn't seem to be a matching EBS volume available in this availability zone. Trying to find a snapshot instead..."
find_snapshot ${EBS_PARAMS[0]} ${EBS_PARAMS[3]}
fi
break
;;
snapshot)
find_snapshot
break
;;
create)
echo "Making ${EBS_PARAMS[2]}GB volume in $AVAILABILITY_ZONE"
VOLUME_DETAILS=`euca-create-volume --size ${EBS_PARAMS[2]} -z $AVAILABILITY_ZONE`
VOLUME_ID=`echo $VOLUME_DETAILS | awk '{print $2}'`
AVAILABLE=`euca-describe-volumes $VOLUME_ID | grep available`
until [ -n "$AVAILABLE" ]; do
echo "waiting..."
sleep 5
AVAILABLE=`euca-describe-volumes $VOLUME_ID | grep available`
done
echo "Volume $VOLUME_ID created!"
echo `euca-create-tags $VOLUME_ID --tag Name=${EBS_PARAMS[0]} --tag Device=${EBS_PARAMS[3]}`
attach_volume $INSTANCE_ID ${EBS_PARAMS[3]} $VOLUME_ID
break
;;
*)
echo "action is unknown.. is the user-data correct?"
exit 1
;;
esac
fi
fi
done
echo "All EBS volumes in user-data have been processed."
else
echo "Hmm, there doesn't seem to be any user-data. Nothing to do."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment