-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsfunctions.sh
444 lines (389 loc) · 14.5 KB
/
awsfunctions.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# returns 0 if $1 has a value,
# if a second param is used it return 0 if $! == $2
function verifyarg {
# if we have a $2 check that the $1 equals it
local ARG=$1
local EXPECT=$2
if [ "X${EXPECT}" == "X" ]; then
# assert that it just has a value
[ "X${ARG}" != "X" ]
return $?
else
# check arg == expect
[ "X${ARG}" == "X${EXPECT}" ]
return $?
fi
}
function verifyexecutable {
# test that we have the tools installed
if which $1 >/dev/null ; then
return 0
else
return 1
fi
}
function verifytools {
# test that we have the tools installed
verifyexecutable ec2-describe-instances
return $?
}
function message {
local DO_DEBUG=
local MESSAGE=
while [ "$#" -ne 0 ]; do
case $1 in
-d | --debug ) DO_DEBUG="true"
shift
;;
* ) MESSAGE="${MESSAGE}${1}"
shift
;;
esac
done
if ! verifyarg "${DO_DEBUG}"; then
echo "$MESSAGE"
else
echo "$MESSAGE" >&2
fi
}
function error {
local MESSAGE="$1"
local -i ERRCODE=1
if verifyarg "$2"; then
ERRCODE=$2
fi
message -d "$MESSAGE"
exit $ERRCODE
}
function testtools {
if ! verifytools; then
error "Sorry cant find AWS tools like ec2-describe-instance"
fi
}
function testsettings {
local SETTINGS=$1
verifyarg "$SETTINGS" || error "jjjjjj filename not supplied to testsettings"
for PROP in $(cat $SETTINGS)
do
IFS='=' read -ra P <<<$(echo $PROP);
local KVS=(${P[@]})
verifyarg ${KVS[1]} || error "in $SETTINGS ${KVS[0]} has no value"
done
verifyarg $REGION || error "REGION is not set"
verifyarg $ZONE || error "ZONE is not set"
verifyarg $KEY || error "KEY is not set"
verifyarg $GROUP || error "GROUP is not set"
verifyarg $SERVERNAME || error "SERVERNAME is not set"
verifyarg $VOLNAME || error "VOLNAME is not set"
verifyarg $VOLDEV || error "VOLDEV is not set"
verifyarg $VOLSIZE || error "VOLSIZE is not set"
verifyarg $INSTANCE_TYPE || error "INSTANCE_TYPE is not set"
verifyarg $AMI || error "AMI is not set"
return 0
}
##########################################
# AWS funcs #
##########################################
#############################################################
# Thing descriptions #
#############################################################
# usage: describe_thing_by_tag $1 $2 $3 $4 #
# $1 = <INSTANCE|VOLUME|??> #
# $1 = OUTPUT section #
# $2 = <TAGVALUE> #
# $3 = <OUTPUT_COL_NUM> #
#############################################################
function describe_thing_by_tag {
local THING=$1
local OUTPUT=$2
local TAG=$3
local COLUMN=$4
if verifyarg $THING && verifyarg $TAG && verifyarg OUTPUT && verifyarg COLUMN; then
case "$THING" in
"INSTANCE" )
local INFO=`ec2-describe-instances -F tag-value=${TAG} --region $REGION | awk '/'${OUTPUT}'/ {print $'$COLUMN'}'`
;;
"VOLUME" )
local INFO=`ec2-describe-volumes -F tag-value=${TAG} --region $REGION | awk '/'${OUTPUT}'/ {print $'$COLUMN'}'`
;;
"GROUP" )
local INFO=`ec2-describe-group -F tag-value=${TAG} --region $REGION | awk '/'${OUTPUT}'/ {print $'$COLUMN'}'`
;;
* )
error "Oops: describe_thing_by_tag: unknown THING=$THING"
;;
esac
message "$INFO"
else
error "Oops: describe_thing_by_tag: THING=$THING OUTPUT=$OUTPUT TAG=$TAG COLUMN=$COLUMN"
fi
}
#############################################################
# usage: describe_thing_by_id $1 $2 $3 $4 #
# $1 = <INSTANCE|VOLUME|??> #
# $2 = OUTPUT section #
# $3 = <ID> #
# $4 = <OUTPUT_COL_NUM> #
#############################################################
function describe_thing_by_id {
local THING=$1
local OUTPUT=$2
local ID=$3
local COLUMN=$4
if verifyarg $THING && verifyarg $ID && verifyarg OUTPUT && verifyarg COLUMN; then
case "$THING" in
"INSTANCE" )
local INFO=`ec2-describe-instances $ID --region $REGION | awk '/'${OUTPUT}'/ {print $'$COLUMN'}'`
;;
"VOLUME" )
# echo "ec2-describe-volumes $ID --region $REGION | awk '/'${OUTPUT}'/ {print $'$COLUMN'}'"
local INFO=`ec2-describe-volumes $ID --region $REGION | awk '/'${OUTPUT}'/ {print $'$COLUMN'}'`
;;
"GROUP" )
local INFO=`ec2-describe-group $ID --region $REGION | awk '/'${OUTPUT}'/ {print $'$COLUMN'}'`
;;
* )
error "Oops: describe_thing_by_id: unknown THING=$THING"
;;
esac
message "$INFO"
else
error "Oops: describe_thing_by_id: THING=$THING OUTPUT=$OUTPUT ID=$ID COLUMN=$COLUMN"
fi
}
#############################################################
#############################################################
# Instance descriptions #
#############################################################
# usage: describe_instance_by_tag $1 $2 $3 #
# $1 = <RESERVATION|INSTANCE|BLOCKDEVICE|NIC|NICATTACHMENT> #
# $2 = <TAGVALUE> #
# $3 = <OUTPUT_COL_NUM> #
#############################################################
function describe_instance_by_tag {
describe_thing_by_tag "INSTANCE" "$1" "$2" "$3"
}
#############################################################
# usage: describe_instance_by_tag $1 $2 $3 #
# $1 = <RESERVATION|INSTANCE|BLOCKDEVICE|NIC|NICATTACHMENT> #
# $2 = <ID_Value> #
# $3 = <OUTPUT_COL_NUM> #
#############################################################
function describe_instance_by_id {
describe_thing_by_id "INSTANCE" "$1" "$2" "$3"
}
###
# Get the Instance DNS addresses via id
# usage: find_instance_dns_by_id <ID>
function desc_instance_dns_by_id {
describe_instance_by_id "INSTANCE" $1 4
}
# Get the instance ID via a id value
# usage: desc_instance_id_by_id <Tag>
function desc_instance_id_by_id {
describe_instance_by_id "INSTANCE" $1 2
}
# Get the instance AMI_ID via id
# usage: desc_instance_ami_by_id <id>
function desc_instance_ami_by_id {
describe_instance_by_id "INSTANCE" $1 3
}
function desc_instance_status_by_id {
describe_instance_by_id "INSTANCE" $1 6
}
function desc_instance_blockdevice_name_by_id {
describe_instance_by_id "BLOCKDEVICE" $1 2
}
function desc_instance_blockdevice_volid_by_id {
describe_instance_by_id "BLOCKDEVICE" $1 3
}
###
# Get the Instance DNS addresses via a tag name
# usage: find_instance_dns_by_tag <Tag>
function desc_instance_dns_by_tag {
describe_instance_by_tag "INSTANCE" $1 4
}
# Get the instance ID via a tag value
# usage: desc_instance_id_by_tag <Tag>
function desc_instance_id_by_tag {
describe_instance_by_tag "INSTANCE" $1 2
}
# Get the instance AMI_ID via a tag value
# usage: desc_instance_ami_by_tag <Tag>
function desc_instance_ami_by_tag {
describe_instance_by_tag "INSTANCE" $1 3
}
function desc_instance_status_by_tag {
describe_instance_by_tag "INSTANCE" $1 6
}
function desc_instance_blockdevice_name_by_tag {
describe_instance_by_tag "BLOCKDEVICE" $1 2
}
function desc_instance_blockdevice_volid_by_tag {
describe_instance_by_tag "BLOCKDEVICE" $1 3
}
#############################################################
# Volume descriptions #
#############################################################
# usage: describe_volume_by_id $1 $2 $3 #
# $1 = <VOLUME|ATTACHMENT|TAG> #
# $2 = <id> #
# $3 = <OUTPUT_COL_NUM> #
#############################################################
function describe_volume_by_id {
describe_thing_by_id "VOLUME" "$1" "$2" "$3"
}
function desc_volume_id_by_id {
describe_volume_by_id "VOLUME" $1 2
}
function desc_volume_size_by_id {
describe_volume_by_id "VOLUME" $1 3
}
function desc_volume_status_by_id {
describe_volume_by_id "VOLUME" $1 6
}
function describe_volume_attach_status {
local VOLUMEID=$1; shift;
local INSTANCEID=$1; shift;
verifyarg $VOLUMEID && verifyarg $INSTANCEID || error "Error:describe_volume_attach_status: VOLUMEID=$VOLUMEID INSTANCEID=$INSTANCEID"
describe_volume_by_id "ATTACHMENT.*${INSTANCEID}" ${VOLUMEID} 5
}
#############################################################
# Volume descriptions #
#############################################################
# usage: describe_volumes_by_tag $1 $2 $3 #
# $1 = <VOLUME|ATTACHMENT|TAG> #
# $2 = <TAGVALUE> #
# $3 = <OUTPUT_COL_NUM> #
#############################################################
function describe_volumes_by_tag {
describe_thing_by_tag "VOLUME" "$1" "$2" "$3"
}
function desc_volume_id_by_tag {
describe_volumes_by_tag "VOLUME" $1 2
}
function desc_volume_size_by_tag {
describe_volumes_by_tag "VOLUME" $1 3
}
function desc_volume_status_by_tag {
describe_volumes_by_tag "VOLUME" $1 6
}
#############################################################
# group descriptions #
#############################################################
# usage: describe_instance_by_tag $1 $2 $3 #
# $1 = <GROUP|PERMISSION> #
# $2 = <TAGVALUE> #
# $3 = <OUTPUT_COL_NUM> #
#############################################################
function describe_group_by_tag {
describe_thing_by_tag "GROUP" "$1" "$2" "$3"
}
#############################################################
# usage: describe_instance_by_id $1 $2 $3 #
# $1 = <RESERVATION|INSTANCE|BLOCKDEVICE|NIC|NICATTACHMENT> #
# $2 = <ID_Value> #
# $3 = <OUTPUT_COL_NUM> #
#############################################################
function describe_group_by_id {
describe_thing_by_id "GROUP" "$1" "$2" "$3"
}
function desc_group_id_by_tag {
describe_volumes_by_tag "GROUP" $1 2
}
function desc_group_name_by_tag {
describe_volumes_by_tag "GROUP" $1 4
}
function desc_group_desc_by_tag {
describe_volumes_by_tag "GROUP" $1 5
}
###########################################################
# Create things #
###########################################################
##
# Create Volume
# VOLUME=`ec2-create-volume -s 50 --region ${REGION} -z ${ZONE} | awk '/VOLUME/ {print $2}'`
#############################################################
# create volume and tag it. return the volID
function create_volume {
local TAG=$1; shift
local SIZE=$1; shift
verifyarg $TAG && verifyarg $SIZE || error "Error:create_volume: TAG=$TAG SIZE=$SIZE"
local VOLUME=`ec2-create-volume --size ${SIZE} --region ${REGION} -z ${ZONE} | awk '/VOLUME/ {print $2}'`
local TAGDETAILS=`ec2-create-tags ${VOLUME} -t Name=${TAG} --region ${REGION}`
message $VOLUME
}
function create_snapshot {
local TAG=$1; shift
local FROM=$1; shift
local DESC=$1; shift
verifyarg "$TAG" && verifyarg "$FROM" && verifyarg "$DESC" || error "Error:create_snapshot: TAG=$TAG FROM=$FROM DESC=\"$DESC\""
TAG="-${TAG}"
local FROMIDs=`desc_volume_id_by_tag $FROM`
for F in $FROMIDs; do
local SNAP=`ec2-create-snapshot --region ${REGION} -d "${DESC}" "${F}" | awk '/SNAPSHOT/ {print $2}'`
local TAGDETAILS=`ec2-create-tags ${SNAP} -t Name=snapshot-${F}${TAG} --region ${REGION}`
message "$SNAP"
done
}
function create_sized_volume {
local TAG=$1
local SIZE=$2
verifyarg $TAG && verifyarg $SIZE || error "Error:create_50G_volume: TAG=$TAG SIZE=$SIZE"
create_volume $TAG $SIZE
}
function create_50G_volume {
create_sized_volume $1 50
}
function delete_volume_by_id {
local ID=$1
verifyarg $ID || error "Error:delete_volume_by_id: No ID supplied"
local THEID=$(ec2-delete-volume --region ${REGION} $ID | awk '/VOLUME/ {print $2}')
message $THEID
}
function delete_volume_by_tag {
local TAG=$1
verifyarg $TAG || error "Error:delete_volumes_by_tag: No TAG supplied"
local IDs=`desc_volume_id_by_tag $TAG`
for id in $IDs; do
local i=`delete_volume_by_id $id`
message -d "delted volume $i"
done
}
#############################
# Attach volumes #
#############################
function attach_volume_to_instance_by_ids {
local VOLUME=$1; shift
local INSTANCE=$2; shift
local DEVICE=$3; shift
verifyarg $VOLUME && verifyarg $INSTANCE && verifyarg $DEVICE || error "Error:attach_volume_to_instance_by_ids: VOLUME=$VOLUME INSTANCE=$INSTANCE DEVICE=$DEVICE"
ec2-attache-volume "${VOLUME}" -i "${INSTANCE}" -d "${DEVICE}" --region ${REGION}
}
function detach_volume_to_instance_by_ids {
local VOLUME=$1; shift
local INSTANCE=$2; shift
local DEVICE=$3; shift
verifyarg $VOLUME && verifyarg $INSTANCE && verifyarg $DEVICE || error "Error:detach_volume_to_instance_by_ids: VOLUME=$VOLUME INSTANCE=$INSTANCE DEVICE=$DEVICE"
ec2-detache-volume "${VOLUME}" -i "${INSTANCE}" -d "${DEVICE}" --region ${REGION}
}
function start_instance {
local AMI=$1; shift;
local INSTANCE_TYPE=$1; shift;
local GROUP=$1; shift;
local TAG=$1; shift;
verifyarg $AMI && verifyarg $INSTANCE_TYPE && verifyarg $GROUP && verify $TAG|| error "Error:start_instance: AMI=$AMI INSTANCE_TYPE=$INSTANCE_TYPE GROUP=$GROUP TAG=$TAG"
local INSTANCE=`ec2-run-instances $AMI -n 1 -k $KEY --instance-type $INSTANCE_TYPE -g $GROUP --region $REGION -z $ZONE | grep INSTANCE | awk '{print $2}'`
local local TAGDETAILS=`ec2-create-tags ${INSTANCE} -t Name=${TAG} --region ${REGION}`
echo $INSTANCE
}
function create_group_basic {
local NAME=$1; shift;
local DESC=$1; shift;
verifyarg $NAME && verifyarg $DESC || error "Error:create_group: NAME=$NAME DESC=$DESC"
ec2-create-group $NAME -d "Security group for git server" --region $REGION
ec2-authorize-group $NAME -P tcp -p 80 --region $REGION
ec2-authorize-group $NAME -P tcp -p 443 --region $REGION
ec2-authorize-group $NAME -P tcp -p 22 --region $REGION
ec2-authorize-group $NAME -P tcp -p 9418 --region $REGION
}