blob: 7151b2fe20e808af4a0ea65bc1f88350a87b0316 [file] [log] [blame]
koder aka kdanilov83cd7132015-02-14 21:37:14 -08001#!/bin/bash
2set -e
3
4my_dir="$(dirname "$0")"
5source "$my_dir/config.sh"
6
7# settings
8FL_RAM=256
koder aka kdanilov8ad6e812015-03-22 14:42:18 +02009FL_HDD=20
koder aka kdanilov83cd7132015-02-14 21:37:14 -080010FL_CPU=1
11
12
13function lookup_for_objects() {
14 set +e
15
16 echo -n "Looking for image $IMAGE_NAME ... "
17 export img_id=$(nova image-list | grep " $IMAGE_NAME " | awk '{print $2}')
18 if [ ! -z "$img_id" ] ; then
19 echo " Found"
20 else
21 echo " Not Found"
22 fi
23
24 echo -n "Looking for flavor $FLAVOR_NAME ... "
25 export flavor_id=$(nova flavor-list | grep " $FLAVOR_NAME " | awk '{print $2}')
26 if [ ! -z "$flavor_id" ] ; then
27 echo " Found"
28 else
29 echo " Not Found"
30 fi
31
32 echo -n "Looking for server-group $SERV_GROUP ... "
33 export group_id=$(nova server-group-list | grep " $SERV_GROUP " | awk '{print $2}' )
34 if [ ! -z "$group_id" ] ; then
35 echo " Found"
36 else
37 echo " Not Found"
38 fi
39
40 echo -n "Looking for keypair $KEYPAIR_NAME ... "
41 export keypair_id=$(nova keypair-list | grep " $KEYPAIR_NAME " | awk '{print $2}' )
42 if [ ! -z "$keypair_id" ] ; then
43 echo " Found"
44 else
45 echo " Not Found"
46 fi
47
48 set -e
49}
50
51function clean() {
52 lookup_for_objects
53
54 if [ ! -z "$img_id" ] ; then
55 echo "Deleting $IMAGE_NAME image"
56 nova image-delete "$img_id" >/dev/null
57 fi
58
59 if [ ! -z "$flavor_id" ] ; then
60 echo "Deleting $FLAVOR_NAME flavor"
61 nova flavor-delete "$flavor_id" >/dev/null
62 fi
63
64 if [ ! -z "$group_id" ] ; then
65 echo "Deleting server-group $SERV_GROUP"
66 nova server-group-delete "$group_id" >/dev/null
67 fi
68
69 if [ ! -z "$keypair_id" ] ; then
70 echo "deleting keypair $KEYPAIR_NAME"
71 nova keypair-delete "$KEYPAIR_NAME" >/dev/null
72 fi
73
74 if [ -f "$KEY_FILE_NAME" ] ; then
75 echo "deleting keypair file $KEY_FILE_NAME"
76 rm -f "$KEY_FILE_NAME"
77 fi
78}
79
80function prepare() {
81 lookup_for_objects
82
83 if [ -z "$img_id" ] ; then
84 echo "Creating $IMAGE_NAME image"
85 opts="--disk-format qcow2 --container-format bare --is-public true"
86 glance image-create --name "$IMAGE_NAME" $opts --copy-from "$IMAGE_URL" >/dev/null
87 echo "Image created, but may need a time to became active"
88 fi
89
90 if [ -z "$flavor_id" ] ; then
91 echo "Creating flavor $FLAVOR_NAME"
92 nova flavor-create "$FLAVOR_NAME" "$FLAVOR_NAME" "$FL_RAM" "$FL_HDD" "$FL_CPU" >/dev/null
93 fi
94
95 if [ -z "$group_id" ] ; then
96 echo "Creating server group $SERV_GROUP"
97 nova server-group-create --policy anti-affinity "$SERV_GROUP" >/dev/null
98 fi
99
100 if [ -z "$keypair_id" ] ; then
101 echo "Creating server group $SERV_GROUP. Key would be stored into $KEY_FILE_NAME"
102 nova keypair-add "$KEYPAIR_NAME" > "$KEY_FILE_NAME"
koder aka kdanilov8ad6e812015-03-22 14:42:18 +0200103 chmod og= "$KEY_FILE_NAME"
koder aka kdanilov83cd7132015-02-14 21:37:14 -0800104 fi
105
106 echo "Adding rules for ping and ssh"
107 set +e
108 nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0 >/dev/null
109 nova secgroup-add-rule default tcp 22 22 0.0.0.0/0 >/dev/null
110 set -e
111}
112
113if [ "$1" = "--clear" ] ; then
114 clean
115else
116 prepare
117fi