-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
186 lines (168 loc) · 3.97 KB
/
bootstrap.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash
SELF=$(readlink -f "$(dirname ${BASH_SOURCE[0]})")
source $SELF/config.sh
function sg_name()
{
echo linux-dev-sg
}
function create_security_group()
{
local name=$(sg_name)
aws ec2 create-security-group --group-name $name --description ' -- '
for ip in ${WHITELIST_IPADDRESSES[@]}; do
aws ec2 authorize-security-group-ingress --group-name $name \
--protocol tcp --port 22 --cidr $ip/32
done
}
function key_name()
{
echo linux-dev-key
}
function key_file()
{
echo $SELF/${@:-"$(key_name)"}.pem
}
function create_key_pair()
{
local name=$(key_name)
local file=$(key_file $name)
aws ec2 create-key-pair --key-name $name --query KeyMaterial --output text > $file
chmod 400 $file
}
function prepare()
{
aws ec2 describe-security-groups |& fgrep -q $(sg_name) || create_security_group
aws ec2 describe-key-pairs |& fgrep -q $(key_name) || create_key_pair
}
function create_instance()
{
local key=$(key_name)
local sg=$(sg_name)
aws ec2 run-instances \
--image-id $EC2_AMI \
--count 1 \
--instance-type $EC2_INSTANCE_TYPE \
--key-name $key \
--security-groups $sg \
--query 'Instances[0].InstanceId' \
"$@" \
| tr -d '"'
}
function wait_instance()
{
local t=1
local address=None
while [ "$address" = None ]; do
address=$(aws ec2 describe-instances \
--instance-ids "$@" \
--query 'Reservations[0].Instances[0].PublicIpAddress' \
2>/dev/null)
sleep $t
done
echo $address
}
function create_wait_instance()
{
local instance=$(create_instance)
local ip=$(wait_instance $instance)
wait_command $ip uname -a >& /dev/null
echo $instance $ip
}
function terminate_instance()
{
aws ec2 terminate-instances --instance-ids "$@"
}
function reboot_instance()
{
aws ec2 reboot-instances --instance-ids "$@"
}
function reboot_wait_instance()
{
local instance=$1
local ip=$2
# XXX: Or just execute_command 'shutdown -r now', but with this instance
# will not reboot right now, but after sometime, and we could have stalled
# instance
execute_command $ip sync
reboot_instance $instance
wait_command $ip uname -a >& /dev/null
}
function console_instance()
{
local t=1
while ! aws ec2 get-console-output --instance-id "$@" 2>/dev/null; do
sleep $t
done
}
function ssh_user()
{
echo ubuntu
}
function ssh_options()
{
local key=$(key_file)
local t=300
echo \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o ConnectTimeout=$t \
-i $key
}
function wait_command()
{
local ip=$1
shift
local retries=10
local t=1
local i=0
local status=0
while [ $i -lt $retries ]; do
let ++i
sleep 1
execute_command $ip "$@" && return 0
status=$?
done
return $status
}
function execute_command()
{
local ip=$1
shift
ssh $(ssh_options) -t $(ssh_user)@$ip "$@"
}
function copy()
{
local from="$1"
local to="$2"
scp $(ssh_options) $from $(ssh_user)@$to
}
function ec2_date()
{
local format="%Y-%m-%dT%H:%I:%S"
TZ=EDT date +$format "$@"
}
function monitoring_instance()
{
local instance=$1
local metric=$2
shift 2
aws cloudwatch get-metric-statistics --namespace AWS/EC2 --period 600 --statistics Average --metric-name $metric --dimensions Name=InstanceId,Value=$instance "$@"
}
function monitoring_instance_current()
{
local from=$(ec2_date -d'now -1 day')
local to=$(ec2_date -d'now +1 day')
monitoring_instance "$@" --start-time $from --end-time $to
}
function instance_status()
{
local id=$1
aws ec2 describe-instance-status --instance-id $id "$@"
}
function instance_status_ok()
{
test $(instance_status "$@" --filter \
Name=instance-status.reachability,Values=passed \
Name=system-status.reachability,Values=passed \
| wc -l) -gt 0
}