-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate_gentoo_32.sh
executable file
·262 lines (204 loc) · 8.8 KB
/
update_gentoo_32.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash
#-------------------------------------------------------------------------------
# update_gentoo_32.sh
#-------------------------------------------------------------------------------
# Copyright 2012 Dowd and Associates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# The region to install into
#region="us-east-1"
region=$1
# The security group to use. 22/tcp needs to be open
# Leave empty to have a group created
#group="default"
group=$2
# The ec2 key pair to use
# Leave empty to have a key created
#key="example"
key=$3
# The fully qualified path to private key of the ec2 key pair
# Leave empty to have a key created
#keyfile="$HOME/.ssh/example.pem"
keyfile=$4
#-----
building="Gentoo 32 EBS"
start_time=`date +%Y-%m-%dT%H:%M:%S`
architecture="i386"
instance_type="c1.medium"
if [[ $region == "" ]]; then
region="us-east-1"
fi
if [[ $group == "" ]]; then
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: set up group"
group="gentoo-bootstrap_64-bit"
group_exists=`ec2-describe-group \
--region $region \
--filter group-name=$group \
| wc -c`
if [ $group_exists -eq 0 ]; then
ec2-create-group --region $region $group --description "Gentoo Bootstrap 64-bit"
fi
ec2-authorize --region $region $group -P tcp -p 22 -s 0.0.0.0/0
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: group set up"
fi
if [[ $key == "" || $keyfile == "" ]]; then
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: set up key"
key="gentoo-bootstrap_64-bit_$region"
keyfile="gentoo-bootstrap_64-bit_$region.pem"
if [ ! -f $keyfile ]; then
ec2-add-keypair --region $region $key | sed 1d > $keyfile
fi
chmod 600 $keyfile
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: key set up"
fi
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: region = $region"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: group = $group"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: key = $key"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: keyfile = $keyfile"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: finding latest gentoo image"
boot_image=`ec2-describe-images \
--region $region \
--owner self \
--filter architecture=$architecture \
--filter image-type=machine \
--filter root-device-type=ebs \
--filter virtualization-type=paravirtual \
--filter manifest-location=902460189751/Gentoo_* \
--filter is-public=true \
| grep "^IMAGE" \
| awk '{ print $3 " " $2 }' \
| sort \
| tail -n 1 \
| awk '{ print $2 }'`
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: gentoo image = $boot_image"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: starting gentoo instance"
instance=`ec2-run-instances \
--region $region \
$boot_image \
--group $group \
--key $key \
--instance-type $instance_type \
| grep "^INSTANCE" \
| awk '{ print $2 }'`
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: bootstrap instance = $instance"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: start checking if instance is running"
running_check=0
while [ $running_check -eq 0 ]; do
sleep 10
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: checking if instance is running (10 second check)"
let running_check=`ec2-describe-instances \
--region $region \
$instance \
--filter instance-state-name=running \
| wc -c`
done
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: instance is running"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: getting hostname"
server=`ec2-describe-instances \
--region $region \
$instance \
--filter instance-state-name=running \
| grep "^INSTANCE" \
| awk '{ print $4 }'`
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: hostname = $server"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: Wait 120 seconds, just in case, for server to finish coming up"
sleep 120
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: copying files to remote server"
scp -o StrictHostKeyChecking=no -i $keyfile ${architecture}/* ${architecture}/.* ec2-user@$server:/tmp
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: setting update_gentoo.sh as executable on remote server"
ssh -o StrictHostKeyChecking=no -i $keyfile ec2-user@$server "chmod 755 /tmp/update_gentoo.sh"
ssh -o StrictHostKeyChecking=no -i $keyfile -t ec2-user@$server "sudo /tmp/update_gentoo.sh"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: checking if update is done"
stopped_check=0
while [ $stopped_check -eq 0 ]; do
sleep 60
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: checking if update is done (1 minute check)"
let stopped_check=`ec2-describe-instances \
--region $region \
$instance \
--filter instance-state-name=stopped \
| wc -c`
done
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: update is done"
name="Gentoo_32-bit-EBS-`date +%Y-%m-%d-%H-%M-%S`"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: creating image"
gentoo_image=`ec2-create-image \
--region $region \
--name $name \
--description "Gentoo 32-bit EBS" \
$instance \
| awk '{ print $2 }'`
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: checking if image is done"
completed_check=0
while [ $completed_check -eq 0 ]; do
sleep 60
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: checking if image is done (1 minute check)"
let completed_check=`ec2-describe-images \
--region $region \
$gentoo_image \
--filter state=available \
| wc -c`
done
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: image is done"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: image-id = $gentoo_image"
gentoo_instance=`ec2-run-instances \
--region $region \
$gentoo_image \
--group $group \
--key $key \
--instance-type t1.micro \
| grep "^INSTANCE" \
| awk '{ print $2 }'`
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: gentoo instance = $gentoo_instance"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: start checking if instance is running"
running_check=0
while [ $running_check -eq 0 ]; do
sleep 10
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: checking if instance is running (10 second check)"
let running_check=`ec2-describe-instances \
--region $region \
$gentoo_instance \
--filter instance-state-name=running \
| wc -c`
done
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: instance is running"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: getting hostname"
server=`ec2-describe-instances \
--region $region \
$gentoo_instance \
--filter instance-state-name=running \
| grep "^INSTANCE" \
| awk '{ print $4 }'`
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: hostname = $server"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: Wait 60 seconds, just in case, for server to finish coming up"
sleep 60
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: checking connection"
up_check=`ssh -o StrictHostKeyChecking=no -i $keyfile -t ec2-user@$server "uname -a" | wc -c`
if [ $up_check -ne 0 ]; then
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: connection successful"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: terminating instances"
ec2-terminate-instances --region $region $instance $gentoo_instance
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: instances terminated"
else
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: connection successful"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: stopping instance"
ec2-stop-instances --region $region $gentoo_instance
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: instance stopped"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: bootstrap instance: $instance"
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: gentoo instance: $gentoo_instance"
fi
echo "$building $start_time - `date +%Y-%m-%dT%H:%M:%S`: gentoo image-id = $gentoo_image"
echo "-----"
echo "ec2-modify-image-attribute --region $region $gentoo_image --launch-permission --add all"
echo "<tr><td>$region</td><td>$gentoo_image</td><td>$architecture</td><td>ebs</td><td>$latest_kernel</td><td>$name</td></tr>"