blob: 1f47f20879e2841a95f316e39b2c24711ef50a4b [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
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030037 if [ ! -z "$KEYPAIR_NAME" ] ; then
38 echo -n "Looking for keypair $KEYPAIR_NAME ... "
39 export keypair_id=$(nova keypair-list | grep " $KEYPAIR_NAME " | awk '{print $2}' )
40 if [ ! -z "$keypair_id" ] ; then
41 echo " Found"
42 else
43 echo " Not Found"
44 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -080045 fi
46
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030047 echo -n "Looking for security group $SECGROUP ... "
48 export secgroup_id=$(nova secgroup-list | grep " $SECGROUP " | awk '{print $2}' )
49 if [ ! -z "$secgroup_id" ] ; then
50 echo " Found"
51 else
52 echo " Not Found"
53 fi
54
koder aka kdanilov83cd7132015-02-14 21:37:14 -080055 set -e
56}
57
58function clean() {
59 lookup_for_objects
60
61 if [ ! -z "$img_id" ] ; then
62 echo "Deleting $IMAGE_NAME image"
63 nova image-delete "$img_id" >/dev/null
64 fi
65
66 if [ ! -z "$flavor_id" ] ; then
67 echo "Deleting $FLAVOR_NAME flavor"
68 nova flavor-delete "$flavor_id" >/dev/null
69 fi
70
koder aka kdanilovc368eb62015-04-28 18:22:01 +030071 for group_id in $groups_ids ; do
koder aka kdanilov83cd7132015-02-14 21:37:14 -080072 echo "Deleting server-group $SERV_GROUP"
73 nova server-group-delete "$group_id" >/dev/null
koder aka kdanilovc368eb62015-04-28 18:22:01 +030074 done
koder aka kdanilov83cd7132015-02-14 21:37:14 -080075
76 if [ ! -z "$keypair_id" ] ; then
77 echo "deleting keypair $KEYPAIR_NAME"
78 nova keypair-delete "$KEYPAIR_NAME" >/dev/null
79 fi
80
81 if [ -f "$KEY_FILE_NAME" ] ; then
82 echo "deleting keypair file $KEY_FILE_NAME"
83 rm -f "$KEY_FILE_NAME"
84 fi
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030085
86 if [ ! -z "$secgroup_id" ] ; then
87 nova secgroup-delete $SECGROUP >/dev/null
88 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -080089}
90
91function prepare() {
92 lookup_for_objects
93
94 if [ -z "$img_id" ] ; then
95 echo "Creating $IMAGE_NAME image"
koder aka kdanilov6ab4d432015-06-22 00:26:28 +030096
97 # opts="--disk-format qcow2 --container-format bare --is-public true"
98 # glance image-create --name "$IMAGE_NAME" $opts --copy-from "$IMAGE_URL" >/dev/null
99
100 IMAGE_FILE="/tmp/${IMAGE_NAME}.qcow"
101 if [ ! -f "$IMAGE_FILE" ] ; then
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300102 curl "$IMAGE_URL" -o "$IMAGE_FILE" 2>&1 >/dev/null
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300103 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800104 opts="--disk-format qcow2 --container-format bare --is-public true"
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300105 glance image-create --name "$IMAGE_NAME" $opts --file "$IMAGE_FILE" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800106 echo "Image created, but may need a time to became active"
107 fi
108
109 if [ -z "$flavor_id" ] ; then
110 echo "Creating flavor $FLAVOR_NAME"
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300111 nova flavor-create "$FLAVOR_NAME" "$FLAVOR_NAME" "$FLAVOR_RAM" "$FLAVOR_HDD" "$FLAVOR_CPU_COUNT" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800112 fi
113
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300114 for SERV_GROUP in $missed_groups ; do
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800115 echo "Creating server group $SERV_GROUP"
116 nova server-group-create --policy anti-affinity "$SERV_GROUP" >/dev/null
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300117 group_id=$(nova server-group-list | grep " $SERV_GROUP " | awk '{print $2}' )
118 export groups_ids="$groups_ids $group_id"
119 done
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800120
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300121 if [ ! -z "$KEYPAIR_NAME" ] ; then
122 if [ -z "$keypair_id" ] ; then
123 echo "Creating server group $SERV_GROUP. Key would be stored into $KEY_FILE_NAME"
124 nova keypair-add "$KEYPAIR_NAME" > "$KEY_FILE_NAME"
125 chmod og= "$KEY_FILE_NAME"
126 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800127 fi
128
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300129 if [ -z "$secgroup_id" ] ; then
130 echo "Adding rules for ping and ssh"
131 nova secgroup-create $SECGROUP $SECGROUP >/dev/null
132 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 >/dev/null
133 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 >/dev/null
134 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800135}
136
137if [ "$1" = "--clear" ] ; then
138 clean
139else
140 prepare
141fi