#!/bin/bash
#
# This script is meant to be run once after running start for the first
# time.  This script downloads a cirros image and registers it.  Then it
# configures networking and nova quotas to allow 40 m1.small instances
# to be created.

# Sanitize language settings to avoid commands bailing out
# with "unsupported locale setting" errors.
unset LANG
unset LANGUAGE
LC_ALL=C
export LC_ALL
for i in curl nova neutron openstack; do
  if [[ ! $(which $i) ]]; then
    echo "$i not installed. Please install $i before proceeding"
    exit 1
  fi
done
# Move to top level directory
REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
cd "$(dirname "$REAL_PATH")/.."

NETWORK_MANAGER="neutron"

# Test for credentials set
if [[ "${OS_USERNAME}" == "" ]]; then
    echo "No Keystone credentials specified.  Try running source openrc"
    exit
fi

# Test to ensure configure script is run only once
if glance image-list | grep -q cirros; then
    echo "This tool should only be run once per deployment."
    exit
fi

echo Downloading glance image.
IMAGE_URL=http://download.cirros-cloud.net/0.3.4/
IMAGE=cirros-0.3.4-x86_64-disk.img
if ! [ -f "$IMAGE" ]; then
    curl -L -o ./$IMAGE $IMAGE_URL/$IMAGE
fi
echo Creating glance image.
glance image-create --name cirros --progress --disk-format qcow2 --container-format bare --progress --file ./$IMAGE

if [[ "${NETWORK_MANAGER}" == "nova" ]] ; then
    echo Configuring nova networking.
    nova secgroup-add-rule default tcp 22 22 0.0.0.0/0
    nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0
    nova network-create vmnet --fixed-range-v4=10.0.0.0/24 --bridge=br100 --multi-host=T
else
    echo Configuring neutron.
    neutron net-create public1 --router:external --provider:physical_network physnet1 --provider:network_type flat
    neutron subnet-create --name 1-subnet --disable-dhcp --allocation-pool start=10.0.2.150,end=10.0.2.199 public1 10.0.2.0/24 --gateway 10.0.2.1
    neutron net-create demo-net --provider:network_type vxlan
    neutron subnet-create demo-net 10.0.0.0/24 --name demo-subnet --gateway 10.0.0.1 --dns-nameservers list=true 8.8.8.8
    neutron router-create demo-router
    neutron router-interface-add demo-router demo-subnet
    neutron router-gateway-set demo-router public1

    # Sec Group Config
    neutron security-group-rule-create default --direction ingress --ethertype IPv4 --protocol icmp --remote-ip-prefix 0.0.0.0/0
    neutron security-group-rule-create default --direction ingress --ethertype IPv4 --protocol tcp --port-range-min 22 --port-range-max 22 --remote-ip-prefix 0.0.0.0/0
    # Open heat-cfn so it can run on a different host
    neutron security-group-rule-create default --direction ingress --ethertype IPv4 --protocol tcp --port-range-min 8000 --port-range-max 8000 --remote-ip-prefix 0.0.0.0/0
    neutron security-group-rule-create default --direction ingress --ethertype IPv4 --protocol tcp --port-range-min 8080 --port-range-max 8080 --remote-ip-prefix 0.0.0.0/0
fi

if [ -r ~/.ssh/id_rsa.pub ]; then
    echo Configuring nova public key and quotas.
    nova keypair-add --pub-key ~/.ssh/id_rsa.pub mykey
fi

# Increase the quota to allow 40 m1.small instances to be created

# Get admin user and tenant IDs
ADMIN_USER_ID=$(openstack user list | awk '/ admin / {print $2}')
ADMIN_PROJECT_ID=$(openstack project list | awk '/ admin / {print $2}')

# 40 instances
nova quota-update --instances 40 $ADMIN_PROJECT_ID
nova quota-update --user $ADMIN_USER_ID --instances 40 $ADMIN_PROJECT_ID

# 40 cores
nova quota-update --cores 40 $ADMIN_PROJECT_ID
nova quota-update --user $ADMIN_USER_ID --cores 40 $ADMIN_PROJECT_ID

# 96GB ram
nova quota-update --ram 96000 $ADMIN_PROJECT_ID
nova quota-update --user $ADMIN_USER_ID --ram 96000 $ADMIN_PROJECT_ID

# add default flavors, if they don't already exist
if ! openstack flavor list | grep -q m1.tiny; then
        openstack flavor create --id 1 --ram 512 --disk 1 --vcpus 1 m1.tiny
        openstack flavor create --id 2 --ram 2048 --disk 20 --vcpus 1 m1.small
        openstack flavor create --id 3 --ram 4096 --disk 40 --vcpus 2 m1.medium
        openstack flavor create --id 4 --ram 8192 --disk 80 --vcpus 4 m1.large
        openstack flavor create --id 5 --ram 16384 --disk 160 --vcpus 8 m1.xlarge
fi