Charm: ~openstack-ubuntu-testing:oneiric/glance   Revision: 1   Hook: start
#!/bin/bash

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

if [[ -e $FORMULA_DIR/glance-common ]] ; then
  . $FORMULA_DIR/glance-common
else
  echo "ERROR: Could nto load glance-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
  # retry after a hash mismatch
  if [[ $? != 1 ]] ; then
    juju-log "WARN: First apt-get update failed, trying again...."
    rm -rf /var/cache/apt/*
    rm -rf /var/lib/apt/lists/*
    sleep 5
    apt-get update
    [[ $? != 0 ]] && juju-log"ERROR: Second attempt to apt-get update FAILED." && exit 1
  fi

  apt-get -y install glance python-mysqldb python-swift || exit 1

  glance_ctl all stop

  # work around LP #784837 until its fixed.
  [[ ! -d /var/log/glance ]] && mkdir /var/log/glance
  chown glance /var/log/glance
  [[ ! -e /var/log/glance/api.log ]] && touch /var/log/glance/api.log
  [[ ! -e /var/log/glance/registry.log ]] && touch /var/log/glance/registry.log
  chown glance /var/log/glance/*.log

  # XXX turn up the logging for now.
  set_or_update verbose True api
  set_or_update debug True api
  set_or_update verbose True registry
  set_or_update debug True registry

  # give ubuntu user passwd-less sudo for log access 
  echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/ubuntu-qa
  chmod 0440 /etc/sudoers.d/ubuntu-qa

}

function db_joined {
  juju-log "glance: db_joined: requesting access to $NOVA_DB for $DB_USER@`unit-get private-address`"
  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
  else
    echo "db_changed: Received password from $DB_HOST"
  fi
  juju-log "glance: db_changed: Configuring registry for access to $NOVA_DB@$DB_HOST"
  set_or_update sql_connection "mysql://$DB_USER:$DB_PASSWORD@$DB_HOST/$NOVA_DB" registry
  glance_ctl all restart
}

function image-service_joined {
  bind_host=$(cat /etc/glance/glance-api.conf | grep bind_host | cut -d= -f2 | sed -e 's/ //g')
  bind_port=$(cat /etc/glance/glance-api.conf | grep bind_port | cut -d= -f2 | sed -e 's/ //g')
  [[ $bind_host == "0.0.0.0" ]] && bind_host=`unit-get private-address`
  juju-log "glance: image-service_joined: To peer glance-api-server=$bind_host:$bind_port"
  relation-set glance-api-server="$bind_host:$bind_port"
}

function object-store_changed {
  URL=`relation-get url`
  USER=`relation-get user`
  PASSWORD=`relation-get password`
  [[ -z $URL ]] || [[ -z $USER ]] || [[ -z $PASSWORD ]] && \
    echo "URL||USER||PASSWORD not set, peer not ready?" && exit 0
  set_or_update default_store swift api
  set_or_update swift_store_user system:$USER api
  set_or_update swift_store_key $PASSWORD api
  set_or_update swift_store_auth_address $URL api
  set_or_update swift_store_create_container_on_put true api
  glance_ctl glance-api restart
}

case $ARG0 in
  "start"|"stop") glance_ctl all $ARG0 ;;
  "install") install_hook ;;
  "shared-db-relation-joined") db_joined ;;
  "shared-db-relation-changed") db_changed;;
  "image-service-relation-joined") image-service_joined ;;
  "image-service-relation-changed") exit 0 ;;
  "object-store-relation-joined") exit 0 ;;
  "object-store-relation-changed") object-store_changed ;;
esac