koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -e |
| 3 | |
| 4 | my_dir="$(dirname "$0")" |
| 5 | source "$my_dir/config.sh" |
| 6 | |
| 7 | # settings |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 8 | FL_RAM=1024 |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 9 | FL_HDD=20 |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 10 | FL_CPU=1 |
| 11 | |
| 12 | |
| 13 | function 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 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 48 | echo -n "Looking for security group $SECGROUP ... " |
| 49 | export secgroup_id=$(nova secgroup-list | grep " $SECGROUP " | awk '{print $2}' ) |
| 50 | if [ ! -z "$secgroup_id" ] ; then |
| 51 | echo " Found" |
| 52 | else |
| 53 | echo " Not Found" |
| 54 | fi |
| 55 | |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 56 | set -e |
| 57 | } |
| 58 | |
| 59 | function clean() { |
| 60 | lookup_for_objects |
| 61 | |
| 62 | if [ ! -z "$img_id" ] ; then |
| 63 | echo "Deleting $IMAGE_NAME image" |
| 64 | nova image-delete "$img_id" >/dev/null |
| 65 | fi |
| 66 | |
| 67 | if [ ! -z "$flavor_id" ] ; then |
| 68 | echo "Deleting $FLAVOR_NAME flavor" |
| 69 | nova flavor-delete "$flavor_id" >/dev/null |
| 70 | fi |
| 71 | |
| 72 | if [ ! -z "$group_id" ] ; then |
| 73 | echo "Deleting server-group $SERV_GROUP" |
| 74 | nova server-group-delete "$group_id" >/dev/null |
| 75 | fi |
| 76 | |
| 77 | if [ ! -z "$keypair_id" ] ; then |
| 78 | echo "deleting keypair $KEYPAIR_NAME" |
| 79 | nova keypair-delete "$KEYPAIR_NAME" >/dev/null |
| 80 | fi |
| 81 | |
| 82 | if [ -f "$KEY_FILE_NAME" ] ; then |
| 83 | echo "deleting keypair file $KEY_FILE_NAME" |
| 84 | rm -f "$KEY_FILE_NAME" |
| 85 | fi |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 86 | |
| 87 | if [ ! -z "$secgroup_id" ] ; then |
| 88 | nova secgroup-delete $SECGROUP >/dev/null |
| 89 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | function prepare() { |
| 93 | lookup_for_objects |
| 94 | |
| 95 | if [ -z "$img_id" ] ; then |
| 96 | echo "Creating $IMAGE_NAME image" |
| 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 | echo "Image created, but may need a time to became active" |
| 100 | fi |
| 101 | |
| 102 | if [ -z "$flavor_id" ] ; then |
| 103 | echo "Creating flavor $FLAVOR_NAME" |
| 104 | nova flavor-create "$FLAVOR_NAME" "$FLAVOR_NAME" "$FL_RAM" "$FL_HDD" "$FL_CPU" >/dev/null |
| 105 | fi |
| 106 | |
| 107 | if [ -z "$group_id" ] ; then |
| 108 | echo "Creating server group $SERV_GROUP" |
| 109 | nova server-group-create --policy anti-affinity "$SERV_GROUP" >/dev/null |
| 110 | fi |
| 111 | |
| 112 | if [ -z "$keypair_id" ] ; then |
| 113 | echo "Creating server group $SERV_GROUP. Key would be stored into $KEY_FILE_NAME" |
| 114 | nova keypair-add "$KEYPAIR_NAME" > "$KEY_FILE_NAME" |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 115 | chmod og= "$KEY_FILE_NAME" |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 116 | fi |
| 117 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 118 | if [ -z "$secgroup_id" ] ; then |
| 119 | echo "Adding rules for ping and ssh" |
| 120 | nova secgroup-create $SECGROUP $SECGROUP >/dev/null |
| 121 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 >/dev/null |
| 122 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 >/dev/null |
| 123 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if [ "$1" = "--clear" ] ; then |
| 127 | clean |
| 128 | else |
| 129 | prepare |
| 130 | fi |