-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-render-instance
More file actions
executable file
·99 lines (71 loc) · 2.49 KB
/
run-render-instance
File metadata and controls
executable file
·99 lines (71 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#! /bin/bash
# automatically quit after errors
set -e
echo "Hello."
date
# given or default environment
: ${INSTANCE_NAME:="test-instance"}
: ${COMMAND:="ls -al"}
: ${AMI_ID:="ami-16d57076"}
: ${AWS_USER:="ubuntu"}
: ${DISK_GB:=""}
: ${INSTANCE_TYPE:="m3.large"}
: ${IAM_PROFILE:="Name=ecsInstanceRole"}
: ${SECURITY_GROUPS:="default" "all-ssh"}
: ${KEY_NAME:="job-control"}
: ${KEY_FOLDER:="$HOME/aws"}
: ${TERMINATE:="yes"}
INSTANCE_DETAILS="/tmp/$INSTANCE_NAME.json"
# spin up a new instances
echo "Requesting <$INSTANCE_TYPE> for <$AMI_ID>."
RUN_ARGS="--image-id $AMI_ID --count 1 --instance-type $INSTANCE_TYPE --iam-instance-profile $IAM_PROFILE --security-groups $SECURITY_GROUPS"
if [[ -n $KEY_NAME ]]
then
RUN_ARGS="$RUN_ARGS --key-name $KEY_NAME"
fi
if [[ -n $DISK_GB ]]
then
RUN_ARGS="$RUN_ARGS --block-device-mappings "[{\"DeviceName\":\"/dev/sda1\",\"Ebs\":{\"VolumeSize\":$DISK_GB,\"DeleteOnTermination\":true,\"VolumeType\":\"gp2\"}}]""
fi
aws ec2 run-instances $RUN_ARGS > "$INSTANCE_DETAILS"
INSTANCE_ID=`jq -r ".Instances[0].InstanceId" $INSTANCE_DETAILS`
# tag the instance for fun
aws ec2 create-tags --resources $INSTANCE_ID --tags Key=Name,Value=$INSTANCE_NAME Key=Script,Value=run-render-instance
# wait until it's ready for ssh
echo "Waiting for instance <$INSTANCE_ID> to start..."
aws ec2 wait instance-status-ok --instance-ids "$INSTANCE_ID"
echo "...OK"
date
# update instance details
aws ec2 describe-instances \
--instance-id $INSTANCE_ID \
> "$INSTANCE_DETAILS"
INSTANCE_DNS_NAME=`jq -r ".Reservations[0].Instances[0].PublicDnsName" $INSTANCE_DETAILS`
# pre-accept ssh key for instance
ssh-keyscan -H $INSTANCE_DNS_NAME >> ~/.ssh/known_hosts
# fire off the command with or without identity file
if [[ -z $KEY_NAME ]]
then
echo "Running command <$COMMAND> at host <$INSTANCE_DNS_NAME with no identity>"
ssh "$AWS_USER@$INSTANCE_DNS_NAME" "$COMMAND"
else
echo "Running command <$COMMAND> at host <$INSTANCE_DNS_NAME> with identity <$KEY_NAME>"
ssh -i "$KEY_FOLDER/$KEY_NAME.pem" "$AWS_USER@$INSTANCE_DNS_NAME" "$COMMAND"
fi
echo "Done"
date
# shut down the instance?
if [ "yes" == $TERMINATE ];
then
echo "Terminating instance <$INSTANCE_ID>..."
aws ec2 terminate-instances \
--instance-ids "$INSTANCE_ID" \
>> "$INSTANCE_DETAILS"
aws ec2 wait instance-terminated --instance-ids "$INSTANCE_ID"
echo "...OK"
else
echo "Leaving instance running <$INSTANCE_ID>. Try:"
echo " ssh -i $KEY_FOLDER/$KEY_NAME.pem $AWS_USER@$INSTANCE_DNS_NAME"
fi
echo "Bye-bye."
date