blob: 9b2fed99177b68e722c9d142dfd71ce7997e43b2 [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 ... "
koder aka kdanilov8e9942e2015-07-15 00:48:16 +03008 export img_id=$(nova $INSECURE image-list | grep " $IMAGE_NAME " | awk '{print $2}')
koder aka kdanilov83cd7132015-02-14 21:37:14 -08009 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 ... "
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030016 export flavor_id=$(nova $INSECURE flavor-list | grep " $FLAVOR_NAME " | awk '{print $2}')
koder aka kdanilov83cd7132015-02-14 21:37:14 -080017 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 ... "
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030027 group_id=$(nova $INSECURE server-group-list | grep " $SERV_GROUP " | awk '{print $2}' )
koder aka kdanilovc368eb62015-04-28 18:22:01 +030028 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 ... "
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030039 export keypair_id=$(nova $INSECURE keypair-list | grep " $KEYPAIR_NAME " | awk '{print $2}' )
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030040 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 ... "
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030048 export secgroup_id=$(nova $INSECURE secgroup-list | grep " $SECGROUP " | awk '{print $2}' )
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030049 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"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030063 nova $INSECURE image-delete "$img_id" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -080064 fi
65
66 if [ ! -z "$flavor_id" ] ; then
67 echo "Deleting $FLAVOR_NAME flavor"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030068 nova $INSECURE flavor-delete "$flavor_id" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -080069 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"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030073 nova $INSECURE 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"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030078 nova $INSECURE keypair-delete "$KEYPAIR_NAME" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -080079 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
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030087 nova $INSECURE secgroup-delete $SECGROUP >/dev/null
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030088 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -080089}
90
91function prepare() {
koder aka kdanilovb7197432015-07-15 00:40:43 +030092 if [ "$OS_INSECURE" -eq "1" ] ; then
koder aka kdanilov8e9942e2015-07-15 00:48:16 +030093 export INSECURE="--insecure"
koder aka kdanilovb7197432015-07-15 00:40:43 +030094 fi
95
koder aka kdanilov83cd7132015-02-14 21:37:14 -080096 lookup_for_objects
97
98 if [ -z "$img_id" ] ; then
99 echo "Creating $IMAGE_NAME image"
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300100
101 # opts="--disk-format qcow2 --container-format bare --is-public true"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +0300102 # glance $INSECURE image-create --name "$IMAGE_NAME" $opts --copy-from "$IMAGE_URL" >/dev/null
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300103
104 IMAGE_FILE="/tmp/${IMAGE_NAME}.qcow"
105 if [ ! -f "$IMAGE_FILE" ] ; then
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300106 curl "$IMAGE_URL" -o "$IMAGE_FILE" 2>&1 >/dev/null
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300107 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800108 opts="--disk-format qcow2 --container-format bare --is-public true"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +0300109 glance $INSECURE image-create --name "$IMAGE_NAME" $opts --file "$IMAGE_FILE" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800110 echo "Image created, but may need a time to became active"
111 fi
112
113 if [ -z "$flavor_id" ] ; then
114 echo "Creating flavor $FLAVOR_NAME"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +0300115 nova $INSECURE flavor-create "$FLAVOR_NAME" "$FLAVOR_NAME" "$FLAVOR_RAM" "$FLAVOR_HDD" "$FLAVOR_CPU_COUNT" >/dev/null
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800116 fi
117
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300118 for SERV_GROUP in $missed_groups ; do
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800119 echo "Creating server group $SERV_GROUP"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +0300120 nova $INSECURE server-group-create --policy anti-affinity "$SERV_GROUP" >/dev/null
121 group_id=$(nova $INSECURE server-group-list | grep " $SERV_GROUP " | awk '{print $2}' )
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300122 export groups_ids="$groups_ids $group_id"
123 done
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800124
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300125 if [ ! -z "$KEYPAIR_NAME" ] ; then
126 if [ -z "$keypair_id" ] ; then
127 echo "Creating server group $SERV_GROUP. Key would be stored into $KEY_FILE_NAME"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +0300128 nova $INSECURE keypair-add "$KEYPAIR_NAME" > "$KEY_FILE_NAME"
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300129 chmod og= "$KEY_FILE_NAME"
130 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800131 fi
132
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300133 if [ -z "$secgroup_id" ] ; then
134 echo "Adding rules for ping and ssh"
koder aka kdanilov8e9942e2015-07-15 00:48:16 +0300135 nova $INSECURE secgroup-create $SECGROUP $SECGROUP >/dev/null
136 nova $INSECURE secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 >/dev/null
137 nova $INSECURE secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 >/dev/null
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300138 fi
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800139}
140
141if [ "$1" = "--clear" ] ; then
142 clean
143else
144 prepare
145fi