#!/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 create_glance() {
    local file=/tmp/glance.keyring
    rm -rf $file

    _a="allow class-read object_prefix rbd_children, "
    _b="allow rwx pool=${AUTH_GLANCE_POOL}"
    str=${_a}${_b}

    if [[ `ceph auth list|grep ${AUTH_GLANCE_USER}|wc -l` -eq 1 ]]; then
        old_str=`ceph auth get ${AUTH_GLANCE_USER}|grep "caps osd"|awk -F "\"" '{print $2}'|awk -F "allow class-read object_prefix rbd_children" '{print $2}'`
        str=${str}${old_str}
        ceph auth caps ${AUTH_GLANCE_USER} \
            mon 'allow r' \
            osd "$str"
    else
        ceph auth get-or-create ${AUTH_GLANCE_USER} \
            mon 'allow r' \
            osd "$str"
    fi

    ceph auth get-or-create ${AUTH_GLANCE_USER} -o $file

    mkdir -p $TOPDIR/files/glance/
    cp -rf /etc/ceph/ceph.conf $TOPDIR/files/glance/
    mv $file $TOPDIR/files/glance/

cat <<"EOF" >>$TOPDIR/files/glance/ceph.conf
[%AUTH_GLANCE_USER%]
keyring=/etc/glance/glance.keyring
EOF

sed -i "s,%AUTH_GLANCE_USER%,$AUTH_GLANCE_USER,g" $TOPDIR/files/glance/ceph.conf

}
