Charm: oneiric/nova-compute   Revision: 49   Hook: nova-compute-common
#!/bin/bash
SERVICES="nova-compute nova-network"
NOVA_RELEASE=$(config-get nova-release)
NOVA_CONF=$(config-get nova-config)
RABBIT_USER=$(config-get rabbit-user)
RABBIT_VHOST=$(config-get rabbit-vhost)
DB_USER=$(config-get db-user)
NOVA_DB=$(config-get nova-db)
NETWORK_MANAGER=$(config-get network-manager)
NETWORK_BRIDGE=$(config-get bridge-interface)
BRIDGE_IP=$(config-get bridge-ip)
BRIDGE_NETMASK=$(config-get bridge-netmask)
PPA=$(config-get nova-release)
VIRT_TYPE=$(config-get virt-type)

function set_or_update {
  # set or update a key=value config option in nova.conf
  KEY=$1
  VALUE=$2
  [[ -z $KEY ]] && exit 1
  [[ -z $VALUE ]] && exit 1
  cat $NOVA_CONF | grep "\-\-$KEY=$VALUE" >/dev/null \
   && juju-log "nova-compute: $KEY=$VALUE already set" exit 0
  if cat $NOVA_CONF | grep "\-\-$KEY=" >/dev/null ; then
    sed -i "s|\(--$KEY=\).*|\1$VALUE|" $NOVA_CONF
  else
    echo "--$KEY=$VALUE" >>$NOVA_CONF
  fi
}

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

function setup_bridge {
  # XXX This is required by nova-network and will likely move somewhere else
  # once we can split these services up into seperate formulas.
  br=$1
  ip=$2
  netmask=$3
  [[ -z $br ]] && br="br100"
  [[ -z $ip ]] && ip="11.0.0.1"
  [[ -z $netmask ]] && netmask="255.255.255.0"

  apt-get -y install bridge-utils augeas-lenses augeas-tools
  echo "Configuring bridge $br ($ip $netmask)"
  context="/files/etc/network/interfaces"
  augtool <<EOF
  set $context/auto[child::1 = "$br"]/1 $br
  set $context/iface[. = '$br'] $br
  set $context/iface[. = '$br']/family inet
  set $context/iface[. = '$br']/method static
  set $context/iface[. = '$br']/address $ip
  set $context/iface[. = '$br']/netmask $netmask
  set $context/iface[. = '$br']/bridge_ports none 
  save
EOF
  ifdown -a ; ifup -a
}

function configure_network_manager {
  # needed by the nova-network bits
  # to be expanded later to cover flatDhcp and VLAN
  echo "$0: configuring $1 network manager"

  NETWORK_BRIDGE=$(config-get bridge-interface)

  case $1 in
    "FlatManager")
      setup_bridge $NETWORK_BRIDGE $BRIDGE_IP $BRIDGE_NETMASK
      set_or_update network_manager nova.network.manager.FlatManager
      set_or_update flat_network_bridge $NETWORK_BRIDGE
      ;;
    "FlatDHCPManager")
      FLAT_INTERFACE=$(config-get flat-interface)
      EC2_HOST=$(relation-get ec2_host)
      [[ -z $EC2_HOST ]] && juju-log "nova-compute: Missing EC2_HOST" \
        && exit 0
      set_or_update network_manager nova.network.manager.FlatDHCPManager
      # the interface on which bridge is built
      set_or_update flat_interface $FLAT_INTERFACE
      # address of API server to forward requests
      EC2_HOST_IP=$(dig +short $EC2_HOST)
      set_or_update ec2_dmz_host $EC2_HOST_IP
      ;;
    *) echo "ERROR: Invalid network manager $1" && exit 1 ;;
  esac
}

function add_ppa {
  # don't setup PPA, install from archive
  [[ $PPA == "distro" ]] && return 0
  juju-log "nova-compute: Configuring PPA access for $PPA"
  . /etc/lsb-release
  PPA_URL="deb http://ppa.launchpad.net/nova-core/$PPA/ubuntu $DISTRIB_CODENAME main"
  echo "Executing: add-apt-repository \"$PPA_URL\"" || exit 1
  add-apt-repository "$PPA_URL" || exit 1
}