-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebs-add-volume
executable file
·64 lines (53 loc) · 2.75 KB
/
ebs-add-volume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /bin/bash
VOLUME_NAME=$1; shift
VOLUME_SIZE=$1; shift
VOLUME_DEVICE=$1; shift
VOLUME_OPTS=$@
REGION=$(wget -qO- http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)
AZ=$(wget -qO- http://169.254.169.254/latest/meta-data/placement/availability-zone)
ID=$(wget -qO- http://169.254.169.254/latest/meta-data/instance-id)
volume=$(aws ec2 --region $REGION describe-volumes \
--filter Name=AvailabilityZone,Values=${AZ} \
--filter Name=Size,Values=${VOLUME_SIZE} \
--filter Name=tag:Name,Values=${VOLUME_NAME})
VOLUME_ID=$(echo "$volume" | jq -r '.Volumes[0].VolumeId | select(. != null)' 2>/dev/null)
if [[ "$VOLUME_ID" == "" ]]; then
volume=$(aws ec2 create-volume --region $REGION --size $VOLUME_SIZE --availability-zone $AZ $VOLUME_OPTS)
VOLUME_ID=$(echo "$volume" | jq -r .VolumeId)
VOLUME_STATE=$(echo "$volume" | jq -r .State)
printf "Waiting for volume $VOLUME_ID..."
while [[ "$VOLUME_STATE" != "available" ]]; do
printf "."
sleep 10
VOLUME_STATE=$(aws ec2 describe-volumes --region $REGION --volume-ids $VOLUME_ID | jq -r '.Volumes[0] | .State' 2>/dev/null)
done
printf "\n"
echo "tagging volume..."
aws ec2 create-tags --region $REGION --resources $VOLUME_ID --tags Key=Name,Value=$VOLUME_NAME
fi
volume=$(aws ec2 describe-volumes --region $REGION --volume-ids $VOLUME_ID)
ATTACHED_TO_ME=$(echo "$volume" | jq -r '.Volumes[0].Attachments[] | select(.InstanceId == "'$ID'" and .State == "attached")' | jq -rs 'length > 0' 2>/dev/null)
ATTACHED_AT_ALL=$(echo "$volume" | jq -r '.Volumes[0].State != "available"' 2>/dev/null)
if [[ "$ATTACHED_AT_ALL" == "true" && "$ATTACHED_TO_ME" != "true" ]]; then
printf "Force-detaching volume $VOLUME_ID..."
# Ensure the volume is not attached to another instance
while aws ec2 detach-volume --volume-id $VOLUME_ID --region $REGION --force &> /dev/null; do
printf "."
sleep 10
done
printf "\n"
fi
if [[ "$ATTACHED_AT_ALL" != "true" || "$ATTACHED_TO_ME" != "true" ]]; then
attachment=$(aws ec2 attach-volume --region $REGION --volume-id $VOLUME_ID --instance-id $ID --device $VOLUME_DEVICE)
ATTACHMENT_STATE=$(echo "$attachment" | jq -r .State)
printf "Attaching volume $VOLUME_ID..."
while [[ "$ATTACHMENT_STATE" != "attached" ]]; do
sleep 10
printf "."
ATTACHMENT_STATE=$(aws ec2 describe-volumes --region $REGION --volume-ids $VOLUME_ID | jq -r '.Volumes[0] | .Attachments[] | select(.InstanceId == "'$ID'" and .Device == "'$VOLUME_DEVICE'") | .State' 2>/dev/null)
done
printf "\n"
fi
volume=$(aws ec2 describe-volumes --region $REGION --volume-ids $VOLUME_ID)
device=$(echo "$volume" | jq -r '.Volumes[0].Attachments[] | select(.InstanceId == "'$ID'" and .State == "attached") | .Device' 2>/dev/null)
echo "Attached volume $VOLUME_ID at $device"