koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -e |
| 3 | |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 4 | function 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 kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 23 | 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 kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 36 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame^] | 37 | 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 kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 45 | fi |
| 46 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 47 | 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 kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 55 | set -e |
| 56 | } |
| 57 | |
| 58 | function 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 kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 71 | for group_id in $groups_ids ; do |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 72 | echo "Deleting server-group $SERV_GROUP" |
| 73 | nova server-group-delete "$group_id" >/dev/null |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 74 | done |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 75 | |
| 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 kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 85 | |
| 86 | if [ ! -z "$secgroup_id" ] ; then |
| 87 | nova secgroup-delete $SECGROUP >/dev/null |
| 88 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | function prepare() { |
| 92 | lookup_for_objects |
| 93 | |
| 94 | if [ -z "$img_id" ] ; then |
| 95 | echo "Creating $IMAGE_NAME image" |
koder aka kdanilov | 6ab4d43 | 2015-06-22 00:26:28 +0300 | [diff] [blame] | 96 | |
| 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 kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame^] | 102 | curl "$IMAGE_URL" -o "$IMAGE_FILE" 2>&1 >/dev/null |
koder aka kdanilov | 6ab4d43 | 2015-06-22 00:26:28 +0300 | [diff] [blame] | 103 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 104 | opts="--disk-format qcow2 --container-format bare --is-public true" |
koder aka kdanilov | 6ab4d43 | 2015-06-22 00:26:28 +0300 | [diff] [blame] | 105 | glance image-create --name "$IMAGE_NAME" $opts --file "$IMAGE_FILE" >/dev/null |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 106 | 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 kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 111 | nova flavor-create "$FLAVOR_NAME" "$FLAVOR_NAME" "$FLAVOR_RAM" "$FLAVOR_HDD" "$FLAVOR_CPU_COUNT" >/dev/null |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 112 | fi |
| 113 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 114 | for SERV_GROUP in $missed_groups ; do |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 115 | echo "Creating server group $SERV_GROUP" |
| 116 | nova server-group-create --policy anti-affinity "$SERV_GROUP" >/dev/null |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 117 | group_id=$(nova server-group-list | grep " $SERV_GROUP " | awk '{print $2}' ) |
| 118 | export groups_ids="$groups_ids $group_id" |
| 119 | done |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 120 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame^] | 121 | 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 kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 127 | fi |
| 128 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 129 | 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 kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if [ "$1" = "--clear" ] ; then |
| 138 | clean |
| 139 | else |
| 140 | prepare |
| 141 | fi |