blob: c11487482018a0d3ba2a9296e97683416d489245 [file] [log] [blame]
#!/bin/bash
set -e
my_dir="$(dirname "$0")"
source "$my_dir/config.sh"
# settings
FL_RAM=1024
FL_HDD=20
FL_CPU=1
function lookup_for_objects() {
set +e
echo -n "Looking for image $IMAGE_NAME ... "
export img_id=$(nova image-list | grep " $IMAGE_NAME " | awk '{print $2}')
if [ ! -z "$img_id" ] ; then
echo " Found"
else
echo " Not Found"
fi
echo -n "Looking for flavor $FLAVOR_NAME ... "
export flavor_id=$(nova flavor-list | grep " $FLAVOR_NAME " | awk '{print $2}')
if [ ! -z "$flavor_id" ] ; then
echo " Found"
else
echo " Not Found"
fi
echo -n "Looking for server-group $SERV_GROUP ... "
export group_id=$(nova server-group-list | grep " $SERV_GROUP " | awk '{print $2}' )
if [ ! -z "$group_id" ] ; then
echo " Found"
else
echo " Not Found"
fi
echo -n "Looking for keypair $KEYPAIR_NAME ... "
export keypair_id=$(nova keypair-list | grep " $KEYPAIR_NAME " | awk '{print $2}' )
if [ ! -z "$keypair_id" ] ; then
echo " Found"
else
echo " Not Found"
fi
echo -n "Looking for security group $SECGROUP ... "
export secgroup_id=$(nova secgroup-list | grep " $SECGROUP " | awk '{print $2}' )
if [ ! -z "$secgroup_id" ] ; then
echo " Found"
else
echo " Not Found"
fi
set -e
}
function clean() {
lookup_for_objects
if [ ! -z "$img_id" ] ; then
echo "Deleting $IMAGE_NAME image"
nova image-delete "$img_id" >/dev/null
fi
if [ ! -z "$flavor_id" ] ; then
echo "Deleting $FLAVOR_NAME flavor"
nova flavor-delete "$flavor_id" >/dev/null
fi
if [ ! -z "$group_id" ] ; then
echo "Deleting server-group $SERV_GROUP"
nova server-group-delete "$group_id" >/dev/null
fi
if [ ! -z "$keypair_id" ] ; then
echo "deleting keypair $KEYPAIR_NAME"
nova keypair-delete "$KEYPAIR_NAME" >/dev/null
fi
if [ -f "$KEY_FILE_NAME" ] ; then
echo "deleting keypair file $KEY_FILE_NAME"
rm -f "$KEY_FILE_NAME"
fi
if [ ! -z "$secgroup_id" ] ; then
nova secgroup-delete $SECGROUP >/dev/null
fi
}
function prepare() {
lookup_for_objects
if [ -z "$img_id" ] ; then
echo "Creating $IMAGE_NAME image"
opts="--disk-format qcow2 --container-format bare --is-public true"
glance image-create --name "$IMAGE_NAME" $opts --copy-from "$IMAGE_URL" >/dev/null
echo "Image created, but may need a time to became active"
fi
if [ -z "$flavor_id" ] ; then
echo "Creating flavor $FLAVOR_NAME"
nova flavor-create "$FLAVOR_NAME" "$FLAVOR_NAME" "$FL_RAM" "$FL_HDD" "$FL_CPU" >/dev/null
fi
if [ -z "$group_id" ] ; then
echo "Creating server group $SERV_GROUP"
nova server-group-create --policy anti-affinity "$SERV_GROUP" >/dev/null
fi
if [ -z "$keypair_id" ] ; then
echo "Creating server group $SERV_GROUP. Key would be stored into $KEY_FILE_NAME"
nova keypair-add "$KEYPAIR_NAME" > "$KEY_FILE_NAME"
chmod og= "$KEY_FILE_NAME"
fi
if [ -z "$secgroup_id" ] ; then
echo "Adding rules for ping and ssh"
nova secgroup-create $SECGROUP $SECGROUP >/dev/null
nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 >/dev/null
nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 >/dev/null
fi
}
if [ "$1" = "--clear" ] ; then
clean
else
prepare
fi