-
Notifications
You must be signed in to change notification settings - Fork 2
/
pave
executable file
·150 lines (125 loc) · 3.66 KB
/
pave
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
set -e
function usage
{
echo
echo "$(basename $0) manages your EC2 instances"
echo
echo "usage: $(basename $0) [launch environment] [--role rolename] | [terminate instance-id] | [list] | [--help]"
echo
echo " launch : launch a new EC2 instance"
echo " --role : use role when launching servers"
echo " terminate : terminate an existing EC2 instance"
echo " list : list existing EC2 instances"
echo " --help : displays the information you're reading now"
echo
}
while test $# != 0; do
case "$1" in
launch)
LAUNCH=1
if [ "$2" != "" ]; then
shift
ENVIRONMENT=$1
fi
;;
terminate)
shift
TERMINATE=$1
;;
list)
LIST=1
;;
--role)
shift
ROLE=$1
;;
--help)
usage
exit
;;
*)
usage
exit 1
;;
esac
shift
done
bail() {
echo -e "\033[31m failed: $1\033[0m"
exit 1
}
# Sets the name of the EC2 instance to the supplied name.
#
# $1 = ECS instance id
# $2 = name
set_instance_name() {
local INSTANCE_ID=$1
local NAME_TAG=$2
local TOTAL_TIME=0
local MAX_TIME=181
while [ $MAX_TIME -gt $TOTAL_TIME ] && ! ec2-create-tags $INSTANCE_ID --tag "Name=$NAME_TAG"; do
TOTAL_TIME=$(( $TOTAL_TIME + 5 ))
echo "$INSTANCE_ID not provisioned. Waiting 5 seconds before checking again..."
sleep 5
done
}
[[ $(which ec2-run-instances) ]] || bail "No EC2 AMI Tools found"
if [ $LAUNCH ]; then
if [ $ENVIRONMENT ]; then
ENV_FILE=.env.$ENVIRONMENT
else
ENV_FILE=.env
fi
[[ -f $ENV_FILE ]] || bail "no $ENV_FILE found"
source $ENV_FILE
[[ -n $INSTANCE_TYPE && $REGION && $KEYPAIR && $AMI ]] || bail "invalid config"
if [ ! $TAG ]; then
TAG=${PWD##*/}
[[ $ROLE ]] && TAG+="_$ROLE"
TAG+="_$ENVIRONMENT"
fi
# GROUP defaults to 'default' if not set
if [ ! $GROUP ]; then
GROUP="default"
fi
EC2_RUN_CMD="ec2-run-instances --instance-type $INSTANCE_TYPE --group $GROUP --region $REGION --key $KEYPAIR $AMI"
if [ $SUBNET ]; then
EC2_RUN_CMD="$EC2_RUN_CMD --subnet $SUBNET"
fi
EC2_RUN_RESULT=$($EC2_RUN_CMD)
INSTANCE_NAME=$(echo ${EC2_RUN_RESULT} | sed 's/RESERVATION.*INSTANCE //' | sed 's/ .*//')
set_instance_name $INSTANCE_NAME $TAG
TOTAL_TIME=0
MAX_TIME=181
while [ $MAX_TIME -gt $TOTAL_TIME ] && ! ec2-describe-instances $INSTANCE_NAME | grep -q "running"
do
TOTAL_TIME=$(( $TOTAL_TIME + 5 ))
echo "$INSTANCE_NAME not running. Waiting 5 seconds before checking again..."
sleep 5s
done
if [ $MAX_TIME -lt $TOTAL_TIME ]; then
bail "Instance $INSTANCE_NAME is taking too long to enter the running state. Exiting..."
fi
echo
echo "Instance $INSTANCE_NAME is now running."
DESCRIBE_INSTANCE=$(ec2-describe-instances $INSTANCE_NAME)
INSTANCE_FQDN=$(echo ${DESCRIBE_INSTANCE} | sed -E 's/RESERVATION.*ami-.{9}//' | sed -E 's/\ .*//')
echo "You can ssh into your new instance with the following command:"
echo "ssh -o StrictHostKeyChecking=no ubuntu@$INSTANCE_FQDN"
exit 0
fi
if [ $TERMINATE ]; then
echo "Getting $TERMINATE's Fully Qualified Domain Name..."
DESCRIBE_INSTANCE=$(ec2-describe-instances $TERMINATE)
INSTANCE_FQDN=$(echo ${DESCRIBE_INSTANCE} | sed -E 's/RESERVATION.*ami-.{9}//' | sed -E 's/\ .*//')
echo "Removing $INSTANCE_FQDN from known_hosts..."
ssh-keygen -R $INSTANCE_FQDN
echo "Terminating $INSTANCE_FQDN..."
ec2-terminate-instances $TERMINATE
exit 0
fi
if [ $LIST ]; then
ec2-describe-instances | grep 'INSTANCE' | grep 'running' | cut -f 2,4,17
exit 0
fi