forked from hashicorp/terraform-aws-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hashicorp#77 from hashicorp/ami-publishing
Change how we publish public AMIs
- Loading branch information
Showing
7 changed files
with
40 additions
and
128 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 |
---|---|---|
|
@@ -7,51 +7,35 @@ | |
|
||
set -e | ||
|
||
readonly PACKER_TEMPLATE_PATH="/home/ubuntu/$CIRCLE_PROJECT_REPONAME/examples/consul-ami/consul.json" | ||
readonly PACKER_TEMPLATE_DEFAULT_REGION="us-east-1" | ||
readonly AMI_PROPERTIES_FILE="/tmp/ami.properties" | ||
readonly AMI_LIST_MARKDOWN_DIR="/home/ubuntu/$CIRCLE_PROJECT_REPONAME/_docs" | ||
readonly GIT_COMMIT_MESSAGE="Add latest AMI IDs." | ||
readonly GIT_USER_NAME="gruntwork-ci" | ||
readonly GIT_USER_EMAIL="[email protected]" | ||
|
||
# In CircleCI, every build populates the branch name in CIRCLE_BRANCH except builds triggered by a new tag, for which | ||
# the CIRCLE_BRANCH env var is empty. We assume tags are only issued against the master branch. | ||
readonly BRANCH_NAME="${CIRCLE_BRANCH:-master}" | ||
|
||
readonly PACKER_BUILD_NAME="$1" | ||
|
||
if [[ -z "$PACKER_BUILD_NAME" ]]; then | ||
echo "ERROR: You must pass in the Packer build name as the first argument to this function." | ||
if [[ "$#" -ne 2 ]]; then | ||
echo "Usage: publish-amis.sh PACKER_TEMPLATE_PATH BUILDER_NAME" | ||
exit 1 | ||
fi | ||
|
||
echo "Checking out branch $BRANCH_NAME to make sure we do all work in a branch and not in detached HEAD state" | ||
git checkout "$BRANCH_NAME" | ||
|
||
# Build the example AMI. WARNING! In a production setting, you should build your own AMI to ensure it has exactly the | ||
# configuration you want. We build this example AMI solely to make initial use of this Module as easy as possible. | ||
build-packer-artifact \ | ||
--packer-template-path "$PACKER_TEMPLATE_PATH" \ | ||
--build-name "$PACKER_BUILD_NAME" \ | ||
--output-properties-file "$AMI_PROPERTIES_FILE" | ||
|
||
# Copy the AMI to all regions and make it public in each | ||
source "$AMI_PROPERTIES_FILE" | ||
publish-ami \ | ||
--all-regions \ | ||
--source-ami-id "$ARTIFACT_ID" \ | ||
--source-ami-region "$PACKER_TEMPLATE_DEFAULT_REGION" \ | ||
--output-markdown > "$AMI_LIST_MARKDOWN_DIR/$PACKER_BUILD_NAME-list.md" \ | ||
--markdown-title-text "$PACKER_BUILD_NAME: Latest Public AMIs" \ | ||
--markdown-description-text "**WARNING! Do NOT use these AMIs in a production setting.** They are meant only to make | ||
initial experiments with this module more convenient." | ||
|
||
# Git add, commit, and push the newly created AMI IDs as a markdown doc to the repo | ||
git-add-commit-push \ | ||
--path "$AMI_LIST_MARKDOWN_DIR/$PACKER_BUILD_NAME-list.md" \ | ||
--message "$GIT_COMMIT_MESSAGE" \ | ||
--user-name "$GIT_USER_NAME" \ | ||
--user-email "$GIT_USER_EMAIL" \ | ||
--git-push-behavior "current" \ | ||
--branch-name "$BRANCH_NAME" | ||
if [[ -z "$PUBLISH_AMI_AWS_ACCESS_KEY_ID" || -z "$PUBLISH_AMI_AWS_SECRET_ACCESS_KEY" ]]; then | ||
echo "The PUBLISH_AMI_AWS_ACCESS_KEY_ID and PUBLISH_AMI_AWS_SECRET_ACCESS_KEY environment variables must be set to the AWS credentials to use to publish the AMIs." | ||
exit 1 | ||
fi | ||
|
||
readonly packer_template_path="$1" | ||
readonly builder_name="$2" | ||
|
||
regions_response=$(aws ec2 describe-regions --region "us-east-1") | ||
all_aws_regions=$(echo "$regions_response" | jq -r '.Regions | map(.RegionName) | join(",")') | ||
|
||
echo "Building Packer template $packer_template_path (builder: $builder_name) and sharing it with all AWS accounts in the following regions: $all_aws_regions" | ||
|
||
# Copying AMIs to many regions can take longer than Packer's default wait timeouts, so we increase them here per | ||
# https://github.com/hashicorp/packer/issues/6536 | ||
export AWS_MAX_ATTEMPTS=240 | ||
export AWS_POLL_DELAY_SECONDS=15 | ||
|
||
# We publish the AMIs to a different AWS account, so set those credentials | ||
export AWS_ACCESS_KEY_ID="$PUBLISH_AMI_AWS_ACCESS_KEY_ID" | ||
export AWS_SECRET_ACCESS_KEY="$PUBLISH_AMI_AWS_SECRET_ACCESS_KEY" | ||
|
||
packer build \ | ||
--only="$builder_name" \ | ||
-var copy_ami_to_regions="$all_aws_regions" \ | ||
-var share_ami_with_groups="all" \ | ||
"$packer_template_path" |
This file was deleted.
Oops, something went wrong.
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
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