Charm: ~openstack-ubuntu-testing:precise/openstack-dashboard   Revision: 1004   Hook: horizon-common
#!/bin/bash
# vim: set ts=2:et

CHARM="openstack-dashboard"

PACKAGES="openstack-dashboard openstack-dashboard-ubuntu-theme python-keystoneclient python-memcache memcached haproxy"
LOCAL_SETTINGS="/etc/openstack-dashboard/local_settings.py"

if [[ -e "$CHARM_DIR/lib/openstack-common" ]] ; then
  . $CHARM_DIR/lib/openstack-common
else
  juju-log "ERROR: Couldn't load $CHARM_DIR/lib/openstack-common." && exit 1
fi

set_or_update() {
  # set a key = value option in $LOCAL_SETTINGS
  local key=$1 value=$2
  [[ -z "$key" ]] || [[ -z "$value" ]] &&
    juju-log "$CHARM set_or_update: ERROR - missing parameters" && return 1
  if [ "$value" == "True" ] || [ "$value" == "False" ]; then
    grep -q "^$key = $value" "$LOCAL_SETTINGS" &&
      juju-log "$CHARM set_or_update: $key = $value already set" && return 0
  else
    grep -q "^$key = \"$value\"" "$LOCAL_SETTINGS" &&
      juju-log "$CHARM set_or_update: $key = $value already set" && return 0
  fi
  if grep -q "^$key = " "$LOCAL_SETTINGS" ; then
    juju-log "$CHARM set_or_update: Setting $key = $value"
    cp "$LOCAL_SETTINGS" /etc/openstack-dashboard/local_settings.last
    if [ "$value" == "True" ] || [ "$value" == "False" ]; then
      sed -i "s|\(^$key = \).*|\1$value|g" "$LOCAL_SETTINGS" || return 1
    else
      sed -i "s|\(^$key = \).*|\1\"$value\"|g" "$LOCAL_SETTINGS" || return 1
    fi
  else
    juju-log "$CHARM set_or_update: Adding $key = $value"
    if [ "$value" == "True" ] || [ "$value" == "False" ]; then
      echo "$key = $value" >>$LOCAL_SETTINGS || return 1
    else
      echo "$key = \"$value\"" >>$LOCAL_SETTINGS || return 1
    fi
  fi
  return 0
}

do_openstack_upgrade() {
  local rel="$1"
  shift
  local packages=$@

  # Setup apt repository access and kick off the actual package upgrade.
  configure_install_source "$rel"
  apt-get update
  DEBIAN_FRONTEND=noninteractive apt-get --option Dpkg::Options::=--force-confnew -y \
     install $packages

  # Configure new config files for access to keystone, if a relation exists.
  r_id=$(relation-ids identity-service | head -n1)
  if [[ -n "$r_id" ]] ; then
    export JUJU_REMOTE_UNIT=$(relation-list -r $r_id | head -n1)
    export JUJU_RELATION="identity-service"
    export JUJU_RELATION_ID="$r_id"
    local service_host=$(relation-get -r $r_id service_host)
    local service_port=$(relation-get -r $r_id service_port)
    if [[ -n "$service_host" ]] && [[ -n "$service_port" ]] ; then
      service_url="http://$service_host:$service_port/v2.0"
      set_or_update OPENSTACK_KEYSTONE_URL "$service_url"
    fi
  fi
}

configure_apache() {
  # Reconfigure to listen on provided port
  a2ensite default-ssl || :
  a2enmod ssl || :
  for ports in $@; do
    from_port=$(echo $ports | cut -d : -f 1)
    to_port=$(echo $ports | cut -d : -f 2)
    sed -i -e "s/$from_port/$to_port/g" /etc/apache2/ports.conf
    for site in $(ls -1 /etc/apache2/sites-available); do
      sed -i -e "s/$from_port/$to_port/g" \
        /etc/apache2/sites-available/$site
    done
  done
}

configure_apache_cert() {
  cert=$1
  key=$2
  echo $cert | base64 -di > /etc/ssl/certs/dashboard.cert
  echo $key | base64 -di > /etc/ssl/private/dashboard.key
  chmod 0600 /etc/ssl/private/dashboard.key
  sed -i -e "s|\(.*SSLCertificateFile\).*|\1 /etc/ssl/certs/dashboard.cert|g" \
         -e "s|\(.*SSLCertificateKeyFile\).*|\1 /etc/ssl/private/dashboard.key|g" \
          /etc/apache2/sites-available/default-ssl
}