blob: e8813f19e93d6cc3d1ed4e2ad93e03af7cf6d3ad [file] [log] [blame]
koder aka kdanilov83cd7132015-02-14 21:37:14 -08001#!/bin/bash
2set -e
3
koder aka kdanilov83cd7132015-02-14 21:37:14 -08004function lookup_for_objects() {
5 set +e
6
7 echo -n "Looking for image $IMAGE_NAME ... "
8 export img_id=$(nova image-list | grep " $IMAGE_NAME " | awk '{print $2}')
9 if [ ! -z "$img_id" ] ; then
10 echo " Found"
11 else
12 echo " Not Found"
13 fi
14
15 echo -n "Looking for flavor $FLAVOR_NAME ... "
16 export flavor_id=$(nova flavor-list | grep " $FLAVOR_NAME " | awk '{print $2}')
17 if [ ! -z "$flavor_id" ] ; then
18 echo " Found"
19 else
20 echo " Not Found"
21 fi
22
koder aka kdanilovc368eb62015-04-28 18:22:01 +030023 groups_ids=""
24 export missed_groups=""
25 for SERV_GROUP in $SERV_GROUPS ; do
26 echo -n "Looking for server-group $SERV_GROUP ... "
27 group_id=$(nova server-group-list | grep " $SERV_GROUP " | awk '{print $2}' )
28 if [ ! -z "$group_id" ] ; then
29 echo " Found"
30 export groups_ids="$groups_ids $group_id"
31 else
32 echo " Not Found"
33 export missed_groups="$missed_groups $SERV_GROUP"
34 fi
35 done
koder aka kdanilov83cd7132015-02-14 21:37:14 -080036
37 echo -n "Looking for keypair $KEYPAIR_NAME ... "
38 export keypair_id=$(nova keypair-list | grep " $KEYPAIR_NAME " | awk '{print $2}' )
39 if [ ! -z "$keypair_id" ] ; then
40 echo " Found"
41 else
42 echo " Not Found"
43 fi
44
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030045 echo -n "Looking for security group $SECGROUP ... "
46 export secgroup_id=$(nova secgroup-list | grep " $SECGROUP " | awk '{print $2}' )
47 if [ ! -z "$secgroup_id" ] ; then
48 echo " Found"
49 else
50 echo " Not Found"
51 fi
52
koder aka kdanilov83cd7132015-02-14 21:37:14 -080053 set -e
54}
55
56function clean() {
57 lookup_for_objects
58
59 if [ ! -z "$img_id" ] ; then
60 echo "Deleting $IMAGE_NAME image"
61 nova image-delete "$img_id" >/dev/null
62 fi
63
64 if [ ! -z "$flavor_id" ] ; then
65 echo "Deleting $FLAVOR_NAME flavor"
66 nova flavor-delete "$flavor_id" >/dev/null
67 fi
68
koder aka kdanilovc368eb62015-04-28 18:22:01 +030069 for group_id in $groups_ids ; do
koder aka kdanilov83cd7132015-02-14 21:37:14 -080070 echo "Deleting server-group $SERV_GROUP"
71 nova server-group-delete "$group_id" >/dev/null
koder aka kdanilovc368eb62015-04-28 18:22:01 +030072 done
koder aka kdanilov83cd7132015-02-14 21:37:14 -080073
74 if [ ! -z "$keypair_id" ] ; then
75 echo "deleting keypair $KEYPAIR_NAME"
76 nova keypair-delete "$KEYPAIR_NAME" >/dev/null
77 fi
78
79 if [ -f "$KEY_FILE_NAME" ] ; then
80 echo "deleting keypair file $KEY_FILE_NAME"
81 rm -f "$KEY_FILE_NAME"
82 fi
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030083
84 if [ ! -z "$secgroup_id" ] ; then
85 nova secgroup-delete $SECGROUP >/dev/null
86 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -080087}
88
89function prepare() {
90 lookup_for_objects
91
92 if [ -z "$img_id" ] ; then
93 echo "Creating $IMAGE_NAME image"
koder aka kdanilov6ab4d432015-06-22 00:26:28 +030094
95 # opts="--disk-format qcow2 --container-format bare --is-public true"
96 # glance image-create --name "$IMAGE_NAME" $opts --copy-from "$IMAGE_URL" >/dev/null
97
98 IMAGE_FILE="/tmp/${IMAGE_NAME}.qcow"
99 if [ ! -f "$IMAGE_FILE" ] ; then
koder aka kdanilovf236b9c2015-06-24 18:17:22 +0300100 curl "$IMAGE_URL" -o "$IMAGE_FILE" >/dev/null
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300101 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800102 opts="--disk-format qcow2 --container-format bare --is-public true"
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300103 glance image-create --name "$IMAGE_NAME" $opts --file "$IMAGE_FILE" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800104 echo "Image created, but may need a time to became active"
105 fi
106
107 if [ -z "$flavor_id" ] ; then
108 echo "Creating flavor $FLAVOR_NAME"
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300109 nova flavor-create "$FLAVOR_NAME" "$FLAVOR_NAME" "$FLAVOR_RAM" "$FLAVOR_HDD" "$FLAVOR_CPU_COUNT" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800110 fi
111
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300112 for SERV_GROUP in $missed_groups ; do
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800113 echo "Creating server group $SERV_GROUP"
114 nova server-group-create --policy anti-affinity "$SERV_GROUP" >/dev/null
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300115 group_id=$(nova server-group-list | grep " $SERV_GROUP " | awk '{print $2}' )
116 export groups_ids="$groups_ids $group_id"
117 done
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800118
119 if [ -z "$keypair_id" ] ; then
120 echo "Creating server group $SERV_GROUP. Key would be stored into $KEY_FILE_NAME"
121 nova keypair-add "$KEYPAIR_NAME" > "$KEY_FILE_NAME"
koder aka kdanilov8ad6e812015-03-22 14:42:18 +0200122 chmod og= "$KEY_FILE_NAME"
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800123 fi
124
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300125 if [ -z "$secgroup_id" ] ; then
126 echo "Adding rules for ping and ssh"
127 nova secgroup-create $SECGROUP $SECGROUP >/dev/null
128 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 >/dev/null
129 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 >/dev/null
130 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800131}
132
133if [ "$1" = "--clear" ] ; then
134 clean
135else
136 prepare
137fi