Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 126 lines (115 sloc) 3.28 KB
#!/bin/bash
OPTIND=1
# -----------------------------
# Defaults
# -----------------------------
instance_type="c4.2xlarge"
cesm_vars_file="cesm_vars.toml"
ami="ami-42569022"
volume_size=100
key_name="UNSET"
subnet="UNSET"
dry_run="--no-dry-run"
region="us-west-2"
availability_zone="us-west-2b"
function show_help {
echo "launch"
echo ""
echo " Creates a new EC2 instance and executes a CESM run"
echo ""
echo " -a <availability zone> Which AZ to use [default $availability_zone]"
echo " -d Perform a dry run [default no]"
echo " -f <cesm vars file> File containing CESM variable settings "
echo " -k <keypair name> The AWS keypair to use on the instance [required]"
echo " -n <subnet id> The subnet to place the instance in [required]"
echo " -r <region> The AWS region [default $region]"
echo " -s <volume size> Size of the EBS volume to create [default $volume_size]"
echo " -t <instance type> c4.{2,4,8}xlarge [default $instance_type]"
}
# -----------------------------
# Process Arguments
# -----------------------------
while getopts "ha:dk:t:f:n:" opt; do
case "$opt" in
h)
show_help
exit 0
;;
a) availability_zone=$OPTARG
;;
d) dry_run="--dry-run"
;;
f) cesm_vars_file=$OPTARG
;;
k) key_name=$OPTARG
;;
n) subnet=$OPTARG
;;
r) region=$OPTARG
;;
s) volume_size=$OPTARG
;;
t) instance_type=$OPTARG
;;
esac
done
if [ "$key_name" = "UNSET" ]
then
echo "launch: key name must be specified with -k"
exit 1
fi
if [ "$subnet" = "UNSET" ]
then
echo "launch: subnet must be specified with -n"
exit 1
fi
# -----------------------------
# Compose user data file
# -----------------------------
cat scripts/cloud-init-pre > user-data.sh
cat $cesm_vars_file >> user-data.sh
cat scripts/cloud-init-post >> user-data.sh
# -----------------------------
# Launch Instance
# -----------------------------
echo "Launching $instance_type instance"
if aws ec2 run-instances \
$dry_run \
--image-id $ami \
--instance-type $instance_type \
--key-name $key_name \
--subnet-id $subnet \
--placement AvailabilityZone=$availability_zone \
--iam-instance-profile Name="Sunshine-Executor" \
--block-device-mappings \
"{ \
\"DeviceName\":\"/dev/sda1\", \
\"Ebs\":{\
\"DeleteOnTermination\":true, \
\"SnapshotId\":\"snap-cdd16037\", \
\"VolumeSize\":$volume_size, \
\"VolumeType\":\"gp2\"}
}" \
--user-data file://user-data.sh > instance_info.json
then
instance_id=`jq .Instances[0].InstanceId instance_info.json`
echo "InstanceId $instance_id"
aws dynamodb put-item \
--table-name CESM_Instances \
--item \
"{ \
\"InstanceId\": { \"S\": $instance_id }, \
\"State\": { \"S\": \"STARTED\" }, \
\"InstanceType\": { \"S\": \"$instance_type\" }, \
\"KeyName\": { \"S\": \"$key_name\" }, \
\"AMI\": { \"S\": \"$ami\" }, \
\"SubnetId\": { \"S\": \"$subnet\" }, \
\"VolumeSize\": { \"N\": \"$volume_size\" }, \
\"Region\": { \"S\": \"$region\" }, \
\"AvailabilityZone\": { \"S\": \"$availability_zone\" } \
}"
rm instance_info.json
else
echo "Could not create instance; exiting"
exit 1
fi