Charm: oneiric/nova-cloud-controller   Revision: 55   Hook: shared-db-relation-changed
#!/bin/bash

FORMULA_DIR=$(dirname $0)
ARG0=${0##*/}

if [[ -e $FORMULA_DIR/nova-cloud-controller-common ]] ; then
  . $FORMULA_DIR/nova-cloud-controller-common
else
  echo "ERROR: Could not load nova-cloud-controller-common from $FORMULA_DIR"
fi

function install_hook {
  juju-log "Installing nova packages"
  apt-get -y install python-software-properties || exit 1
  add_ppa
  apt-get update || exit 1
  DEBIAN_FRONTEND=noninteractive apt-get -y \
    install --no-install-recommends $PACKAGES || exit 1
  configure_network_manager $NETWORK_MANAGER
  nova_ctl all stop
}

function amqp_joined {
  # we request a username on the rabbit queue
  # and store it in nova.conf. our response is its IP + PASSWD
  # but we configure that in _changed
  juju-log "amqp_joined: requesting credentials for $RABBIT_USER"
  echo "amqp_joined: requesting credentials for $RABBIT_USER"
  relation-set username=$RABBIT_USER
  relation-set vhost=$RABBIT_VHOST
}

function amqp_changed {
  # server creates our credentials and tells us where
  # to connect.  for now, using default vhost '/'
  RABBIT_HOST=`relation-get private-address`
  RABBIT_PASSWORD=`relation-get password`
  if [[ -z $RABBIT_HOST ]] || \
     [[ -z $RABBIT_PASSWORD ]] ; then 
      echo "amqp_changed: RABBIT_HOST||RABBIT_PASSWORD not set."
      exit 0
  fi
  echo "amqp_changed: Setting rabbit config in nova.conf: $RABBIT_HOST $RABBIT_USER $RABBIT_PASSWORD"
  set_or_update rabbit_host $RABBIT_HOST
  set_or_update rabbit_user $RABBIT_USER
  set_or_update rabbit_password $RABBIT_PASSWORD
  set_or_update rabbit_virtual_host $RABBIT_VHOST
  nova_ctl all restart
}

function db_joined {
  # tell mysql provider which database we want. it will create it and give us
  # credentials
  juju-log "db_joined: requesting database access to $NOVA_DB for $DB_USER@$HOSTNAME"
  relation-set database=$NOVA_DB
  relation-set username=$DB_USER
  relation-set hostname=`unit-get private-address`
}

function db_changed {
  DB_HOST=`relation-get private-address`
  DB_PASSWORD=`relation-get password`
  if [[ -z $DB_HOST ]] || [[ -z $DB_PASSWORD ]] ; then
    echo "db_changed: DB_HOST || DB_PASSWORD not yet set. Exit 0 and retry"
    exit 0
  fi
  echo "db_changed: Configuring nova.conf for access to $NOVA_DB"
  set_or_update sql_connection "mysql://$DB_USER:$DB_PASSWORD@$DB_HOST/$NOVA_DB"
  nova_ctl all restart
  sleep 1
  /usr/bin/nova-manage db sync
}

function image-service_changed {
  API_SERVER=`relation-get glance-api-server`
  [[ -z $API_SERVER ]] && echo "image-service_changed: Peer not ready?" && exit 0
  set_or_update glance_api_servers $API_SERVER
  set_or_update image_service "nova.image.glance.GlanceImageService"
  nova_ctl all restart
}

function nova-network_joined {
  # this will be moved to its own nova-network formula when the
  # time comes.  for now, tell peer what network manager we are
  # using, and let them configure accordingly. we may want to also
  # take care of assigning non-conflicting IPs to compute nodes
  manager=$(cat $NOVA_CONF | grep network_manager | cut -d= -f2)
  manager=$(echo $manager | sed -e 's/\./ /g' | awk '{ print $4 }')
  relation-set network_manager=$manager ec2_host=$(unit-get private-address)
}

case $ARG0 in
  "start"|"stop") nova_ctl all $ARG0 ;;
  "install") install_hook ;;
  "amqp-relation-joined") amqp_joined ;;
  "amqp-relation-changed") amqp_changed ;;
  "shared-db-relation-joined") db_joined ;;
  "shared-db-relation-changed") db_changed ;;
  "image-service-relation-joined") exit 0 ;;
  "image-service-relation-changed") image-service_changed ;;
  "nova-network-relation-joined") nova-network_joined ;;
  *) exit 0 ;;
esac