-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathec2-ubuntu
executable file
·62 lines (51 loc) · 1.54 KB
/
ec2-ubuntu
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
#! /usr/bin/env bash
if [ -z $1 ]; then
echo "ec2-ubuntu - easy ec2 ubuntu server creation"
echo "Usage: $0 instance-name [instance-type [ami [region]]]"
exit
fi
instancename=$1
instancetype=${2:-'t1.micro'}
ami=${3:-'ami-ccf405a5'}
region=${4:-'us-east-1'}
function run() {
echo "$@"
eval "$@"
}
if [ ! -f $HOME/.ec2/$instancename.pem ]; then
run "ec2-add-keypair --region $region $instancename > $HOME/.ec2/$instancename.pem"
run chmod 0600 $HOME/.ec2/$instancename.pem
fi
echo "starting instance..."
run 'instance=`ec2-run-instances '"$ami"' --instance-type '"$instancetype"' --region '"$region"' --key '"$instancename"' | grep INSTANCE`'
iid=`echo $instance | cut -d' ' -f2`
status='pending'
while [ $status != 'running' ]; do
sleep 3
run 'description=`ec2-describe-instances --region '"$region"' '"$iid"' | grep INSTANCE`'
status=`echo "$description" | cut -f6`
echo "waiting for instance to boot (status: $status)..."
done
hostname=`echo "$description" | cut -f4`
run "ec2-authorize --region $region default -p 80"
run "ec2-authorize --region $region default -p 22"
while true; do
nc -z $hostname 22 && break
echo 'waiting for ssh to start...'
sleep 3
done
sleep 10 # ssh needs a bit of time to warm up
echo
echo "setting up ~/.ssh/config..."
run 'cat <<END >> ~/.ssh/config
Host '"$instancename"'
HostName '"$hostname"'
HostKeyAlias '"$hostname"'
User ubuntu
ForwardAgent yes
StrictHostKeyChecking no
IdentityFile ~/.ec2/'"$instancename.pem"'
END'
echo
echo "setup complete, to get started:"
echo " > ssh $instancename"