Skip to content

Commit

Permalink
Merge pull request #1 from JungleCatSoftware/v0.0.4
Browse files Browse the repository at this point in the history
This release fixes and issue where apt-get upgrade could fail on certain packages (such as grub) that will try and prompt for input and adds in the ability to wait for the AMI to complete and then shutdown.
  • Loading branch information
brwyatt committed Mar 29, 2015
2 parents 12ccc21 + 928e798 commit e20c615
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
# AWS-Toolkit
Tools for generating and bootstrapping EC2 AMIs

When used as below, the launched instance will download a release of
this repository and use it to load a base configuration and save it
as an AMI.
When used as described below, the launched instance will download a
release or branch of this repository and use it to load a base
configuration and save it as an AMI in your AWS account.

This allows for automation of installing core tools and dependencies
needed in order to spin up application VMs in your EC2 environmet. By
default, the scripts in this repository are made to update a stock
Ubuntu image, install Python 3.0 and pip, Puppet, and AWS command-line
tools.

## IAM Role
When using these scripts to create AMIs, they must be granted a role
with access to the following actions:
When using these scripts to create AMIs, the instance must be granted
a role with access to the following actions:

- ec2:CreateImage
- ec2:DescribeImages

## How to Use
There are two methods for using this code to bootstrap your EC2 AMIs.
Below are two methods for using this repo to bootstrap your EC2 AMIs.
For both methods, the instance must be given access to create an image
(see the "IAM Role" section above), and it is recommended to set the
instance's shutdown behavior to "Terminate" to allow the instance to
terminate itself and prevent additonal EC2 charges once the script
has finished configuring itself and kicked off the AMI generation.

### Full User Data
The complete contents of the `UserData` script can be pasted into the
"User data" section under "Advanced Details" on the "Configure
Instance Details" step when launching a new EC2 instance.
Instance Details" step when launching a new EC2 instance. This also
offers the greatest amount of control, allowing the script to be
modified to tweek tarball download settings.

### Curl
The following short script can be used in your instance's "User data"
instead of the full `UserData` script and will download and execute
the most recent version of the `UserData` script automatically and
will ensure that the most recent release archive from GitHub is used.
The URL can be modified to point to branches or tags other than
"master".

```bash
#!/bin/bash
Expand Down
2 changes: 1 addition & 1 deletion UserData
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
export maintainer=JungleCatSoftware
export repo=AWS-Toolkit
export release=0.0.3
export release=0.0.4
targetdir=/opt/${maintainer}
mkdir -p ${targetdir} &&\
curl -L https://github.com/${maintainer}/${repo}/archive/${release}.tar.gz |\
Expand Down
3 changes: 3 additions & 0 deletions scripts/buildAMI
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fi
export PATH

echo -e "\nStarting at $(date +"${dateformat}")"
export image_id_path="/tmp/ImageId"

upgradeSystemPackages
installSystemPackages python3 python3-pip puppet cloud-utils
Expand All @@ -43,3 +44,5 @@ installPythonPackages awscli
createAMI

echo -e "\nFinished at $(date +"${dateformat}")"

shutdownAfterAMIComplete
6 changes: 5 additions & 1 deletion scripts/global/createAMI
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ timestamp=$(date +"%s")
aminame="${maintainer}_${release}_${timestamp}"
description="Auto-generated at $(date --date="@${timestamp}" +"${dateformat}") from ${maintainer}/${repo}@${release}"

echo -e "\tSyncing disks..."
sync
sleep 10

echo -e "\tCreating AMI \"${aminame}\"..."
aws ec2 create-image --region ${region} --instance-id "${instanceid}" --name "${aminame}" --no-reboot &>/dev/null
aws ec2 create-image --region ${region} --instance-id "${instanceid}" --name "${aminame}" --no-reboot >$image_id_path 2>/dev/null
exval=$?

if [ $exval -eq 0 ]; then
Expand Down
27 changes: 27 additions & 0 deletions scripts/global/shutdownAfterAMIComplete
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
echo -e "\n==Waiting for AMI to Finish=="
if [[ "x${dateformat}" != "x" ]]; then
echo -e "Begin at $(date +"${dateformat}")"
fi

instanceid=$(ec2metadata --instance-id)
availabilityzone=$(ec2metadata --availability-zone)
region=${availabilityzone::-1}
image_id=$(cat $image_id_path | grep ImageId | sed -E 's/.*"ImageId":\s*"(ami-.*)".*/\1/' | head -n1)
image_state="pending"

while [[ "x$image_state" == "xpending" ]]; do
sleep 30
echo -e "\tChecking for instance state..."
res=$(aws ec2 describe-images --region "$region" --image-ids "$image_id" --query 'Images[*].{State:State}' 2>/dev/null)
image_state=$(echo $res | grep State | sed -E 's/.*"State":\s*"(pending|available|failed)".*/\1/' | head -n1)
echo -e "\t\tFound state \"$image_state\""
done

if [[ "x$image_state" == "xavailable" ]]; then
echo -e "\t[DONE]"
else
echo -e "\t[FAIL]"
fi

shutdown -h now
2 changes: 1 addition & 1 deletion scripts/global/upgradeSystemPackages
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if apt-get --version &>/dev/null; then
exval=$?
if [ $exval -eq 0 ]; then
echo -e "\tRunning apt-get upgrade..."
apt-get upgrade --assume-yes &>/dev/null
DEBIAN_FRONTEND=noninteractive apt-get --assume-yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade &>/dev/null
exval=$?
fi
fi
Expand Down

0 comments on commit e20c615

Please sign in to comment.