This tool is generic enough to be used in a non EC2 environment, but in this article, we focus only on EC2.
About SmartEC2 Tool
This tool has bunch of ant tasks which can be used in the EC2 environment.
BaseTask - This is the base for all the other tasks. Has a method to save files to a given location.
S3Download - Helps to download objects from S3 buckets. This tool uses Jets3t for this task.
HttpDownload - Simple HTTP file download task. It does not support HTTPS and HTTP based authentication. Used only for public URL's.
MetadataDownload - This downloads all metadata related to the instance and stores it as a property file in the given location. EC2 Meta data is available as a tree structure. This task downloads all the metadata in one short and makes it available as a linear property file.
A typical EC2 Metadata request would provide the following list.
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-id
instance-type
local-hostname
local-ipv4
placement/
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
The translated Metadata file contains data in the following fashion.
ami-launch-index = 0
ami-manifest-path = ?
block-device-mapping/0 = ?
block-device-mapping/1 = ?
hostname = ec2-67-202-43-212.compute-1.amazonaws.com
instance-id = i-qwewq132
instance-type = ?
local-hostname = ec2-67-202-43-212.compute-1.local
local-ipv4 = ?
public-hostname = ?
public-ipv4 = ?
public-keys/0 = sdfdsfsd-sdfsdf34543-#
reservation-id = ?
ReplaceProperty - This task helps you to replace any properties in the corresponding target file. At this point it can replace any one of the properties at a time.
Smart EC2 tool contains all of the tasks listed above and a default build.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Ec2Ant" basedir="." default="execBuild">
<property name="localFolder" value="${basedir}"/>
<property name="s3buildPath" value="${localFolder}/s3build"/>
<property environment="env"/>
<path id="csslab.lib">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<typedef name="s3download" classname="com.csslab.s3ant.s3.S3Download" classpathref="csslab.lib"/>
<typedef name="HttpDownload" classname="com.csslab.s3ant.core.HttpDownload" classpathref="csslab.lib"/>
<typedef name="ec2metadata" classname="com.csslab.ec2.MetadataDownload" classpathref="csslab.lib"/>
<typedef name="replaceproperty" classname="com.csslab.ec2.ReplaceProperty" classpathref="csslab.lib"/>
<target name="execBuild">
<mkdir dir="${s3buildPath}"/>
<echo>Using build path ${s3buildPath}</echo>
<HttpDownload fileUrl="http://169.254.169.254/1.0/user-data" localFolder="${s3buildPath}"/> <loadproperties srcFile="${s3buildPath}/user-data"/>
<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${bucketName}" S3ObjectPath="${buildFile}" localFolder="${s3buildPath}" saveFileAs="build.xml"/><ant antfile="${s3buildPath}/build.xml" dir="${s3buildPath}" inheritrefs="true"/>
<delete dir="${s3buildPath}"/>
<echo>Build completed successfully.</echo>
</target>
</project>
First section contains declaration for some of the properties used by the targets.
Next you will find the definition for all the four ant tasks.
Then you will see a target that gets executed when the ANT starts.
Deploying Smart EC2 on your AMI
Pre-requisite
Please download the following pre-requisites.
Setup your path to point to the Java and ANT binaries.
How does it work?
EC2 has a provision to launch AMI's by providing user-defined data as an input file. The build process expects certain important parameters like Access Key, Secure Key, build file path and bucket name where the build file is available. The user defined data should be in the below given format. The key names listed below are mandatory. But you are free to add your own keys for further build process.
secretKey={your secret key}
bucketName={your bucket name}
buildFile={your build file path}
A typical user-defined data file looks like the below one.
secretKey=sce3434343#$$$$vvxcvx/sdfsdfs
bucketName=mybucket
buildFile=mybuild.xml
Download the sample user data file here and store it locally. But you need to replace properties with the appropriate values.
You can use putty to login to the instance once it is launched and change your directory to where you have installed Smart EC2. For the first time users, you need to install the tool and bundle your AMI. If you are not familiar with building AMI’s , you can use our Jboss AMI which is available for public. Please find the URL below.
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1450
Let’s assume you have installed Smart EC2 under /var/local. This is where the tool is installed on the above mentioned AMI.
You will use the following command to go to the tools home.
Cd /var/local/SmartEC2
Execute the command ant.
This will download the user defined data from the EC2 server and make it available as ANT properties. And then it will download the build file created by you for your project and executes it.
How can you benefit out of this?
As we know ant has a wide variety of tasks which can ease your deployment process such as executing shell scripts, deploying war, ear files on to the container etc. So sky is the limit.
We have created couple of sample build files(Sample1 , Sample2) in our S3 bucket for your reference as well.
Subscribe to this user group to get more updates.
You can download the source and build on top of it.
For your convenience we have created a step by step screen shots to showcase as how we deploy our applications using this solution.