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 ... " |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 8 | export img_id=$(nova $INSECURE image-list | grep " $IMAGE_NAME " | awk '{print $2}') |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 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 ... " |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 16 | export flavor_id=$(nova $INSECURE flavor-list | grep " $FLAVOR_NAME " | awk '{print $2}') |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 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 ... " |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 27 | group_id=$(nova $INSECURE server-group-list | grep " $SERV_GROUP " | awk '{print $2}' ) |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 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 ... " |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 39 | export keypair_id=$(nova $INSECURE keypair-list | grep " $KEYPAIR_NAME " | awk '{print $2}' ) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 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 ... " |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 48 | export secgroup_id=$(nova $INSECURE secgroup-list | grep " $SECGROUP " | awk '{print $2}' ) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 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" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 63 | nova $INSECURE image-delete "$img_id" >/dev/null |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 64 | fi |
| 65 | |
| 66 | if [ ! -z "$flavor_id" ] ; then |
| 67 | echo "Deleting $FLAVOR_NAME flavor" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 68 | nova $INSECURE flavor-delete "$flavor_id" >/dev/null |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 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" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 73 | nova $INSECURE 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" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 78 | nova $INSECURE keypair-delete "$KEYPAIR_NAME" >/dev/null |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 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 |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 87 | nova $INSECURE secgroup-delete $SECGROUP >/dev/null |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 88 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | function prepare() { |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 92 | if [ "$OS_INSECURE" -eq "1" ] ; then |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 93 | export INSECURE="--insecure" |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 94 | fi |
| 95 | |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 96 | lookup_for_objects |
| 97 | |
| 98 | if [ -z "$img_id" ] ; then |
| 99 | echo "Creating $IMAGE_NAME image" |
koder aka kdanilov | 6ab4d43 | 2015-06-22 00:26:28 +0300 | [diff] [blame] | 100 | |
| 101 | # opts="--disk-format qcow2 --container-format bare --is-public true" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 102 | # glance $INSECURE image-create --name "$IMAGE_NAME" $opts --copy-from "$IMAGE_URL" >/dev/null |
koder aka kdanilov | 6ab4d43 | 2015-06-22 00:26:28 +0300 | [diff] [blame] | 103 | |
| 104 | IMAGE_FILE="/tmp/${IMAGE_NAME}.qcow" |
| 105 | if [ ! -f "$IMAGE_FILE" ] ; then |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 106 | curl "$IMAGE_URL" -o "$IMAGE_FILE" 2>&1 >/dev/null |
koder aka kdanilov | 6ab4d43 | 2015-06-22 00:26:28 +0300 | [diff] [blame] | 107 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 108 | opts="--disk-format qcow2 --container-format bare --is-public true" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 109 | glance $INSECURE image-create --name "$IMAGE_NAME" $opts --file "$IMAGE_FILE" >/dev/null |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 110 | 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 kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 115 | nova $INSECURE 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] | 116 | fi |
| 117 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 118 | for SERV_GROUP in $missed_groups ; do |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 119 | echo "Creating server group $SERV_GROUP" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 120 | 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 kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 122 | export groups_ids="$groups_ids $group_id" |
| 123 | done |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 124 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 125 | 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 kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 128 | nova $INSECURE keypair-add "$KEYPAIR_NAME" > "$KEY_FILE_NAME" |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 129 | chmod og= "$KEY_FILE_NAME" |
| 130 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 131 | fi |
| 132 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 133 | if [ -z "$secgroup_id" ] ; then |
| 134 | echo "Adding rules for ping and ssh" |
koder aka kdanilov | 8e9942e | 2015-07-15 00:48:16 +0300 | [diff] [blame] | 135 | 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 kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 138 | fi |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if [ "$1" = "--clear" ] ; then |
| 142 | clean |
| 143 | else |
| 144 | prepare |
| 145 | fi |