Charm: ~gandelman-a:oneiric/glance   Revision: 31   Hook: glance-common
#!/bin/bash

SERVICES="glance-api glance-registry"
GLANCE_REGISTRY_CONF=$(config-get registry-config)
GLANCE_API_CONF=$(config-get api-config)

DB_USER=$(config-get db-user)
NOVA_DB=$(config-get nova-db)
PPA=$(config-get glance-release)

HOSTNAME=`hostname -f`

function glance_ctl_status {
  SERVICE=$1
  # workaround upstarts lack of scriptable return codes
  STATUS=$(service $SERVICE status | cut -d/ -f1 | awk '{ print $2 }')
  case $STATUS in
    "start") return 0 ;;
    "stop") return 1 ;;
    *) echo "ERROR: Unexpected status of service $SERVICE: $STATUS" && exit 1 ;;
  esac
}

function glance_ctl {
  if [[ $1 == "all" ]] ; then
    CTL=$SERVICES
  else
    CTL=$1
  fi
  ACTION=$2
  if [[ -z $CTL ]] || [[ -z $ACTION ]] ; then
    juju-log "ERROR glance_ctl: Not enough arguments"
    exit 1
  fi
  for i in $CTL ; do
    case $ACTION in
      "start")
        glance_ctl_status $i || service $i start ;;
      "stop")
        glance_ctl_status $i && service $i stop || return 0 ;;
      "restart")
        glance_ctl_status $i && service $i restart || service $i start ;;
    esac
    if [[ $? != 0 ]] ; then
      juju-log "glance_ctl: ERROR - Service $i failed to $ACTION"
    fi
  done
}

function set_or_update {
  # This handles configuration of both api and registry server
  # until LP #806241 is resolved.  Until then, $3 is either
  # 'api' or 'registry' to specify which
  # set or update a key=value config option in glance.conf
  KEY=$1
  VALUE=$2
  [[ $3 == "api" ]] && CONF=$GLANCE_API_CONF
  [[ $3 == "registry" ]] && CONF=$GLANCE_REGISTRY_CONF
  [[ -z $KEY ]] && exit 1
  [[ -z $VALUE ]] && exit 1
  cat $CONF | grep "$KEY = $VALUE" >/dev/null \
   && juju-log "glance: $KEY = $VALUE already set" exit 0
  if cat $CONF | grep "$KEY =" >/dev/null ; then
    sed -i "s|\($KEY = \).*|\1$VALUE|" $CONF
  else
    echo "$KEY = $VALUE" >>$CONF
  fi
}

function add_ppa {
  # Install from archive instead of PPA.
  [[ $PPA == "distro" ]] && return 0
  . /etc/lsb-release
  juju-log "glance: Setting up PPA for $PPA"
  [[ -z $PPA ]] && return 0
  PPA_URL="deb http://ppa.launchpad.net/nova-core/$PPA/ubuntu $DISTRIB_CODENAME main"
  add-apt-repository "$PPA_URL" || exit 1
}