forked from zalando-stups/taupage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share-ami.sh
executable file
·64 lines (52 loc) · 2.64 KB
/
share-ami.sh
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
CONFIG_FILE=$1
TAUPAGE_VERSION=$2
cd $(dirname $0)
# load configuration file
. $CONFIG_FILE
# get ami_id, ami_name and commitID
imageid=$(aws ec2 describe-images --region $region --filters Name=tag-key,Values=Version Name=tag-value,Values=$TAUPAGE_VERSION --query 'Images[*].{ID:ImageId}' --output text)
ami_name=$(aws ec2 describe-images --region $region --filters Name=tag-key,Values=Version Name=tag-value,Values=$TAUPAGE_VERSION --query 'Images[*].{ID:Name}' --output text)
commit_id=$(git log | head -n 1 | awk {'print $2'})
for account in $accounts; do
echo "Sharing AMI with account $account ..."
aws ec2 modify-image-attribute --region $region --image-id $imageid --launch-permission "{\"Add\":[{\"UserId\":\"$account\"}]}"
done
for target_region in $copy_regions; do
echo "Copying AMI to region $target_region ..."
result=$(aws ec2 copy-image --source-region $region --source-image-id $imageid --region $target_region --name $ami_name --description "$ami_description" --output json)
target_imageid=$(echo $result | jq .ImageId | sed 's/"//g')
state="no state yet"
while [ true ]; do
echo "Waiting for AMI creation in $target_region ... ($state)"
result=$(aws ec2 describe-images --region $target_region --output json --image-id $target_imageid)
state=$(echo $result | jq .Images\[0\].State | sed 's/"//g')
if [ "$state" = "failed" ]; then
echo "copying Image failed."
exit 1
elif [ "$state" = "available" ]; then
break
fi
sleep 10
done
for account in $accounts; do
echo "Sharing AMI with account $account ..."
aws ec2 modify-image-attribute --region $target_region --image-id $target_imageid --launch-permission "{\"Add\":[{\"UserId\":\"$account\"}]}"
# set tags in other account
aws ec2 create-tags --region $target_$region --resources $target_$imageid --tags Key=Version,Value=$TAUPAGE_VERSION
aws ec2 create-tags --region $target_$region --resources $target_$imageid --tags Key=CommitID,Value=$commit_id
done
done
#check if image creation/copy was successfull
if [ "$state" = "available" ]; then
#git add new release tag
# get AMI-name
ami_name=$(aws ec2 describe-images --region $region --filters Name=image-id,Values=$imageid --query 'Images[*].{ID:Name}' --output text)
git tag $ami_name
git push --tags
#tag image in Frankfurt with commitID
commit_id=$(git log | head -n 1 | awk {'print $2'})
aws ec2 create-tags --region $region --resources $imageid --tags Key=CommitID,Value=$commit_id
else
echo "Image creation/copy failed."
fi