Charm: ~openstack-ubuntu-testing:quantal/nova-volume   Revision: 16   Hook: config-changed
#!/bin/bash
set -e
CHARM_DIR=$(dirname $0)
ARG0=${0##*/}

if [[ -e $CHARM_DIR/nova-volume-common ]] ; then
  . $CHARM_DIR/nova-volume-common
else
  juju-log "$CHARM ERROR: Could not load nova-volume-common from $CHARM_DIR"
fi

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
  local rabbit_user=$(config-get rabbit-user)
  local rabbit_vhost=$(config-get rabbit-vhost)
  juju-log "$CHARM - 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 '/'
  local rabbit_host=$(relation-get private-address)
  local rabbit_password=$(relation-get password)

  if [[ -z $rabbit_host ]] || \
     [[ -z $rabbit_password ]] ; then
      juju-log "$CHARM - amqp_changed: rabbit_host||rabbit_password not set."
      exit 0
  fi

  local rabbit_user=$(config-get rabbit-user)
  local rabbit_vhost=$(config-get rabbit-vhost)
  juju-log "$CHARM - amqp_changed: Setting rabbit config in nova.conf: " \
           "$rabbit_user@$rabbit_host/$rabbit_vhost"
  set_or_update rabbit_host $rabbit_host
  set_or_update rabbit_userid $rabbit_user
  set_or_update rabbit_password $rabbit_password
  set_or_update rabbit_virtual_host $rabbit_vhost
  service_ctl "nova-volume" restart
}

function db_joined {
  # tell mysql provider which database we want. it will create it and give us
  # credentials
  local nova_db=$(config-get nova-db)
  local db_user=$(config-get db-user)
  local hostname=$(unit-get private-address)
  juju-log "$CHARM - db_joined: requesting database access to $nova_db for "\
           "$db_user@$hostname"
  relation-set database=$nova_db username=$db_user hostname=$hostname
}

function db_changed {
  local db_host=`relation-get private-address`
  local db_password=`relation-get password`

  if [[ -z $db_host ]] || [[ -z $db_password ]] ; then
    juju-log "$CHARM - db_changed: db_host||db_password set, will retry."
    exit 0
  fi

  local nova_db=$(config-get nova-db)
  local db_user=$(config-get db-user)
  juju-log "$CHARM - db_changed: Configuring nova.conf for access to $nova_db"

  set_or_update sql_connection "mysql://$db_user:$db_password@$db_host/$nova_db"
  service_ctl all restart
}

function install_hook() {
  juju-log "$CHARM: Installing nova-volume and related packages."
  apt-get -y install python-software-properties || exit 1
  configure_install_source "$(config-get openstack-origin)"
  apt-get update || exit 1
  DEBIAN_FRONTEND=noninteractive apt-get -y \
    install --no-install-recommends $PACKAGES
  service_ctl "nova-volume" stop
  configure_storage
  local vg=$(config-get volume-group)
  juju-log "$CHARM: Configuring nova-volume for volgroup $vg"
  set_or_update volume_group "$vg" 
  # Configure any flags specified in deployment config
  set_config_flags
  # Call any OpenStack QA lab specific stuff
  $CHARM_DIR/openstack-ubuntu-testing
}

function config_changed() {

  # Determine whether or not we should do an upgrade, based on whether or not
  # the version offered in openstack-origin is greater than what is installed.

  local install_src=$(config-get openstack-origin)
  local cur=$(get_os_codename_package "nova-common")
  local available=$(get_os_codename_install_source "$install_src")

  if dpkg --compare-versions $(get_os_version_codename "$cur") lt \
                             $(get_os_version_codename "$available") ; then
    juju-log "$CHARM: Upgrading OpenStack release: $cur -> $available."
    do_openstack_upgrade "$install_src" $PACKAGES
  fi

  set_config_flags
  service_ctl "nova-volume" restart

}

function volume-service_changed() {
  # nova-c-c will send a trigger (uuid) via this interface
  # to trigger a service restart. if this hook is firing, its
  # time to restart nova-volume.
  service_ctl "nova-volume" restart
}

function ceph_joined {
  mkdir -p /etc/ceph
  apt-get -y install ceph-common || exit 1
}

function ceph_changed {
  SERVICE_NAME=`echo $JUJU_UNIT_NAME | cut -d / -f 1`
  KEYRING=/etc/ceph/ceph.client.$SERVICE_NAME.keyring
  KEY=`relation-get key`
  if [ -n "$KEY" ]; then
    # But only once
    if [ ! -f $KEYRING ]; then
      ceph-authtool $KEYRING \
        --create-keyring --name=client.$SERVICE_NAME \
        --add-key="$KEY"
      chmod +r $KEYRING
    fi
  else
    # No key - bail for the time being
    exit 0
  fi

  MONS=`relation-list`
  mon_hosts=""
  for mon in $MONS; do
    mon_hosts="$mon_hosts `relation-get private-address $mon`:6789"
  done
  cat > /etc/ceph/ceph.conf << EOF
[global]
 auth supported = $(relation-get auth)
 keyring = /etc/ceph/\$cluster.\$name.keyring
 mon host = $mon_hosts
EOF

  # XXX: Horrid kludge to make nova-volume use
  # a different ceph username than admin
  echo "CEPH_ARGS=--id $SERVICE_NAME" >> /etc/environment

  # Create the nova pool if it does not already exist
  if ! rados --id $SERVICE_NAME lspools | grep -q nova; then
    rados --id $SERVICE_NAME mkpool nova
  fi

  # Reconfigure nova-volume
  set_or_update volume_driver nova.volume.driver.RBDDriver
  set_or_update rbd_pool nova
  service_ctl "nova-volume" restart
}

case $ARG0 in
  "start"|"stop") service_ctl nova-volume "$ARG0" ;;
  "install") install_hook ;;
  "config-changed") config_changed ;;
  "amqp-relation-joined") amqp_joined ;;
  "amqp-relation-changed") amqp_changed ;;
  "shared-db-relation-joined") db_joined ;;
  "shared-db-relation-changed") db_changed ;;
  "ceph-relation-joined") ceph_joined;;
  "ceph-relation-changed") ceph_changed;;
  "nova-volume-service-relation-joined") exit 0 ;;
  "nova-volume-service-relation-changed") volume-service_changed ;;
esac