This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new channel-based release process (#486)
* Use new channel-based release process * More verbose AMI sharing
- Loading branch information
1 parent
f9033b2
commit 9ee2814
Showing
5 changed files
with
85 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,73 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
CONFIG_FILE=$1 | ||
CONFIG_DIR=$(dirname $CONFIG_FILE) | ||
CONFIG_DIR="$(dirname "$CONFIG_FILE")" | ||
TAUPAGE_VERSION=$2 | ||
CHANNEL=$3 | ||
|
||
cd $(dirname $0) | ||
cd "$(dirname "$0")" | ||
|
||
# load configuration file | ||
. $CONFIG_FILE | ||
. "$CONFIG_FILE" | ||
|
||
# get ami_id, ami_name and commitID | ||
result=$(aws ec2 describe-images --region $region --filters Name=tag-key,Values=Version Name=tag-value,Values=$TAUPAGE_VERSION --query 'Images[0]' --output json) | ||
imageid=$(echo $result | jq -r '.ImageId') | ||
ami_name=$(echo $result | jq -r '.Name') | ||
publish_date="$(date +%Y%m%d-%H%M%S)" | ||
ami_name="Taupage${CHANNEL}-AMI-${publish_date}" | ||
commit_id=$(git rev-parse HEAD) | ||
|
||
#share AMI in default region | ||
echo "Share AMI $imageid for $accounts" | ||
echo $accounts | xargs aws ec2 modify-image-attribute --region $region --image-id $imageid --attribute launchPermission --operation-type add --user-ids | ||
aws ec2 create-tags --region $region --resources $imageid --tags "Key=Shared,Value=Internal" | ||
|
||
for target_region in $copy_regions; do | ||
target_imageid=$(aws ec2 describe-images --region $target_region --filters Name=tag-key,Values=Version Name=tag-value,Values=$TAUPAGE_VERSION --query 'Images[*].{ID:ImageId}' --output text) | ||
if [ -z "$target_imageid" ]; then | ||
echo "Copying AMI to region $target_region ..." | ||
target_imageid=$(aws ec2 copy-image --source-region $region --source-image-id $imageid --region $target_region --name $ami_name --description "$ami_description" --output text) | ||
share_ami() { | ||
# share_ami region | ||
local region="$1" | ||
local ami_data=$(aws ec2 describe-images --region "$region" --filters "Name=name,Values=TaupageBuild-$TAUPAGE_VERSION" --query 'Images[0]' --output json) | ||
local imageid=$(echo "$ami_data" | jq -r '.ImageId') | ||
local build_date=$(echo "$ami_data" | jq -r '.Tags[] | select(.Key == "BuildDate") | .Value') | ||
if [[ -z "$build_date" ]]; then | ||
echo "BuildDate not set, cannot copy the image" >2 | ||
exit 1 | ||
fi | ||
|
||
state="no state yet" | ||
while [ true ]; do | ||
echo "Waiting for AMI creation in $target_region ... ($state)" | ||
# create an updated AMI skeleton. most of the properties we copy from the original AMI, but | ||
# we need to remove 'encrypted' from the EBS volumes and update the name | ||
local updated_ami="$(echo "$ami_data" | jq --arg name "$ami_name" '{Description, Architecture, RootDeviceName, VirtualizationType, SriovNetSupport, EnaSupport, Name: $name, BlockDeviceMappings: .BlockDeviceMappings | map(del(.Ebs.Encrypted))}')" | ||
|
||
state=$(aws ec2 describe-images --region $target_region --query 'Images[0].State' --output text --image-id $target_imageid) | ||
echo "Copying AMI $imageid in $region as $ami_name..." | ||
local target_imageid=$(aws ec2 register-image --region "$region" --cli-input-json "$updated_ami" --output text) | ||
|
||
if [ "$state" = "failed" ]; then | ||
echo "copying Image failed." | ||
exit 1 | ||
elif [ "$state" = "available" ]; then | ||
break | ||
fi | ||
# Wait until the image is available | ||
while true; do | ||
local state="$(aws ec2 describe-images --region "$region" --query 'Images[0].State' --output text --image-id "$target_imageid")" | ||
|
||
if [ "$state" = "failed" ]; then | ||
echo "Copying failed." | ||
exit 1 | ||
elif [ "$state" = "available" ]; then | ||
echo "AMI $region/$ami_name ($target_imageid) successfully created." | ||
break | ||
else | ||
echo "Waiting for AMI creation in $region ... ($state)" | ||
sleep 10 | ||
done | ||
fi | ||
done | ||
|
||
# set tags in other account | ||
aws ec2 create-tags --region $target_region --resources $target_imageid --tags "Key=Version,Value=$TAUPAGE_VERSION" "Key=CommitID,Value=$commit_id" "Key=Shared,Value=Internal" | ||
else | ||
echo "Image still exist ($target_imageid). Skip copy." | ||
state="available" | ||
fi | ||
# Update the image tags | ||
local tags="$(jq -n --arg build_date "$build_date" --arg version "$TAUPAGE_VERSION" --arg source_ami "$imageid" --arg commit_id "$commit_id" '[{Key: "BuildDate", Value: $build_date}, {Key: "SourceAMI", Value: $source_ami}, {Key: "CommitID", Value: $commit_id}, {Key: "Version", Value: $version}]')" | ||
aws ec2 create-tags --region "$region" --resources "$target_imageid" --tags "$tags" | ||
|
||
echo "Sharing the AMI with AWS accounts: $all_accounts" | ||
|
||
echo "Share AMI $target_imageid for $accounts" | ||
echo $accounts | xargs aws ec2 modify-image-attribute --region $target_region --image-id $target_imageid --attribute launchPermission --operation-type add --user-ids | ||
# Share the image | ||
echo $all_accounts | xargs aws ec2 modify-image-attribute --region "$region" --image-id "$target_imageid" --attribute launchPermission --operation-type add --user-ids | ||
} | ||
|
||
for target_rgn in $copy_regions; do | ||
share_ami $target_rgn | ||
done | ||
|
||
#check if image creation/copy was successfull | ||
if [ "$state" = "available" ]; then | ||
# git add new release tag | ||
git tag $ami_name | ||
# git add new release tag | ||
git tag $ami_name | ||
git push --tags | ||
if [ -d "$CONFIG_DIR" ]; then | ||
cd "$CONFIG_DIR" | ||
git tag "$ami_name" | ||
git push --tags | ||
if [ -d "$CONFIG_DIR" ]; then | ||
cd "$CONFIG_DIR" | ||
git tag $ami_name | ||
git push --tags | ||
cd - | ||
fi | ||
|
||
#tag image in Frankfurt with commitID | ||
aws ec2 create-tags --region $region --resources $imageid --tags Key=CommitID,Value=$commit_id | ||
else | ||
echo "Image creation/copy failed." | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.