Charm: ~openstack-ubuntu-testing:quantal/horizon   Revision: 18   Hook: shared-db-relation-joined
#!/bin/bash
set -e

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

if [[ -e $CHARM_DIR/horizon-common ]] ; then
  . $CHARM_DIR/horizon-common
else
  echo "ERROR: Could not load horizon-common from $CHARM_DIR"
fi

function install_hook {
  configure_install_source "$(config-get openstack-origin)"
  apt-get update
  juju-log "$CHARM: Installing $PACKAGES."
  DEBIAN_FRONTEND=noninteractive apt-get -y install $PACKAGES
  set_or_update CACHE_BACKEND "memcached://127.0.0.1:11211/"
  open-port 80
  # Call any OpenStack QA lab specific stuff
  $CHARM_DIR/openstack-ubuntu-testing
}

function db_joined {
  # TODO
  # relation-set database, username, hostname 
  return 0
}

function db_changed {
  # TODO
  # relation-get password, private-address
  return 0
}

function keystone_joined {
  # service=None lets keystone know we don't need anything entered
  # into the service catalog.  we only really care about getting the
  # private-address from the relation
  relation-set service="None" region="None" public_url="None" \
               admin_url="None" internal_url="None"
}

function keystone_changed {
  service_url="http://$(relation-get private-address):5000/v2.0"
  juju-log "$CHARM: Configuring Horizon to access keystone @ $service_url."
  set_or_update OPENSTACK_KEYSTONE_URL "$service_url"
  service apache2 restart
}

function config_changed {
  local install_src=$(config-get openstack-origin)
  local cur=$(get_os_codename_package "openstack-dashboard")
  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

  # update the web root for the horizon app.
  local web_root=$(config-get webroot)
  juju-log "$CHARM: Setting web root for Horizon to $web_root".
  cp /etc/apache2/conf.d/openstack-dashboard.conf \
    /var/lib/juju/openstack-dashboard.conf.last
  awk -v root="$web_root" \
    '/^WSGIScriptAlias/{$2 = root }'1 \
    /var/lib/juju/openstack-dashboard.conf.last \
    >/etc/apache2/conf.d/openstack-dashboard.conf
  set_or_update LOGIN_URL "$web_root/auth/login"
  set_or_update LOGIN_REDIRECT_URL "$web_root"
  service apache2 reload

}

juju-log "$CHARM: Running hook $ARG0."
case $ARG0 in
  "install") install_hook ;;
  "start") exit 0 ;;
  "stop") exit 0 ;;
  "shared-db-relation-joined") db_joined ;;
  "shared-db-relation-changed") db_changed;;
  "identity-service-relation-joined") keystone_joined;;
  "identity-service-relation-changed") keystone_changed;;
  "config-changed") config_changed;;
esac