#!/bin/bash

# Copyright 2014 Intel Corporation, All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the"License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#  http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.


function _set_default_nova() {
    local key=`echo $@ | awk -F "=" '{print $1}'`
    local value=`echo $@ | awk -F "=" '{print $2}'`
    crudini --set /etc/nova/nova.conf DEFAULT $key $value
}

function configure_nova() {
    local nova_file=/etc/nova/nova.conf
    local ceph_file=/etc/nova/ceph.conf
    local libvirt_auth_key=""

    # If we find the nova.conf, we change the configuration file.
    # Otherwise, skip.
    # NOTE: If do not find nova.conf file, do not report error.
    if [[ -e $nova_file ]]; then
        # It's notable that, cinder just create rdb devices.
        # So, if nova wants to access these rbd devices in pool.
        # The ${AUTH_CINDER_USER} must have right to access these
        # pools.
        # For libvirt, we use information generated by CINDER to
        # register secret key.
        #cp -rf $TOPDIR/files/nova/* /etc/nova/
        #_set_default_nova libvirt_images_type=rbd
        #_set_default_nova libvirt_images_rbd_pool=${AUTH_CINDER_POOL}
        #_set_default_nova libvirt_images_rbd_ceph_conf=$ceph_file
        _set_default_nova rbd_user=${AUTH_CINDER_USER#*.}
        _set_default_nova rbd_secret_uuid=${AUTH_CINDER_UUID}

        libvirt_auth_key=`cat $TOPDIR/files/cinder/cinder.keyring  | grep key`
        libvirt_auth_key=${libvirt_auth_key#*=}
        add_auth_user ${AUTH_CINDER_USER} $libvirt_auth_key ${AUTH_CINDER_UUID}
        if [[ `is_service_running openstack-nova-compute` -gt 0 ]]; then
            service openstack-nova-compute restart
        fi

        if [[ `is_service_running nova-compute` -gt 0 ]]; then
            service nova-compute restart
        fi
    fi
}
