devstack-plugin-glusterfs: Refactoring plugin code

This patch modifies the plugin code into more reusable manner.
Also adds a new file "devstack/gluster-functions.sh", which
contains gluster specific functions.

Change-Id: Ibe1231d5f2244ea7e9b7e7a5e1b138192e372206
This commit is contained in:
Bharat Kumar Kobagana 2015-07-15 12:29:03 +05:30
parent fb470e7c6c
commit d4bf5d44e3
2 changed files with 167 additions and 124 deletions

View File

@ -0,0 +1,154 @@
#!/bin/bash
# devstack/gluster-functions.sh
# Functions to control the installation and configuration of the **GlusterFS** storage
# Installs 3.6.x version of glusterfs
# Triggered from devstack/plugin.sh as part of devstack "pre-install"
function install_glusterfs {
if [[ ${DISTRO} =~ rhel7 ]] && [[ ! -f /etc/yum.repos.d/glusterfs-epel.repo ]]; then
sudo wget $GLUSTERFS_CENTOS_REPO -O /etc/yum.repos.d/glusterfs-epel.repo
elif is_ubuntu; then
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 3FE869A9
echo "deb http://ppa.launchpad.net/gluster/glusterfs-3.6/ubuntu trusty main" | sudo tee /etc/apt/sources.list.d/glusterfs-3_6-trusty.list
echo "deb-src http://ppa.launchpad.net/gluster/glusterfs-3.6/ubuntu trusty main" | sudo tee --append /etc/apt/sources.list.d/glusterfs-3_6-trusty.list
NO_UPDATE_REPOS=False
REPOS_UPDATED=False
fi
install_package glusterfs-server
install_package xfsprogs
if is_fedora; then
stop_glusterfs
_start_glusterfs
fi
_create_glusterfs_disk
# Changing file permissions of glusterfs logs.
# This avoids creation of zero sized glusterfs log files while running CI job (Bug: 1455951).
sudo chmod 755 -R /var/log/glusterfs/
}
# Start gluster service
function _start_glusterfs {
if is_ubuntu; then
sudo service glusterfs-server start
else
sudo service glusterd start
fi
}
# Stop running gluster service
# Triggered from devstack/plugin.sh as part of devstack "unstack"
function stop_glusterfs {
if is_ubuntu; then
sudo service glusterfs-server stop
else
sudo service glusterd stop
fi
}
# Clean Shares
function _umount_shares {
local shares=$1
local share
local mount_point
for share in $(echo $shares | sed "s/;/ /"); do
mount_point=$(grep $share /proc/mounts | awk '{print $2}')
if [[ -n $mount_point ]]; then
sudo umount $mount_point
fi
done
}
# Delete gluster volumes
function _delete_gluster_shares {
local shares=$1
local share
local gluster_volumes
_umount_shares $shares
for share in $(echo $shares | sed "s/;/ /"); do
gluster_volumes+=,$(echo $share | cut -d/ -f2);
done
for vol_name in $(echo $gluster_volumes | sed "s/,/ /g"); do
sudo gluster --mode=script volume stop $vol_name
sudo gluster --mode=script volume delete $vol_name
done
}
# Cleanup GlusterFS
# Triggered from devstack/plugin.sh as part of devstack "clean"
function cleanup_glusterfs {
local glusterfs_volumes
local vol_name
# Cleaning up Cinder GlusterFS shares
_delete_gluster_shares $CINDER_GLUSTERFS_SHARES
if [[ -e ${GLUSTERFS_DISK_IMAGE} ]]; then
sudo rm -f ${GLUSTERFS_DISK_IMAGE}
fi
if [[ "$OFFLINE" = "False" ]]; then
uninstall_package glusterfs-server
fi
if egrep -q ${GLUSTERFS_DATA_DIR} /proc/mounts; then
sudo umount ${GLUSTERFS_DATA_DIR}
fi
sudo rm -rf ${GLUSTERFS_DATA_DIR}
}
# Setting up glusterfs
function _create_glusterfs_disk {
# create a backing file disk
local disk_image_directory=$(dirname "${GLUSTERFS_DISK_IMAGE}")
mkdir -p $disk_image_directory
create_disk ${GLUSTERFS_DISK_IMAGE} ${GLUSTERFS_DATA_DIR} ${GLUSTERFS_LOOPBACK_DISK_SIZE}
}
function _create_gluster_volume {
local glusterfs_volume=$1
sudo mkdir -p ${GLUSTERFS_DATA_DIR}/$glusterfs_volume
sudo gluster --mode=script volume \
create $glusterfs_volume $(hostname):${GLUSTERFS_DATA_DIR}/$glusterfs_volume
sudo gluster --mode=script volume start $glusterfs_volume
sudo gluster --mode=script volume set $glusterfs_volume server.allow-insecure on
}
function _create_gluster_volumes {
local gluster_shares=$1
local share
local glusterfs_volumes
for share in $(echo $gluster_shares | sed "s/;/ /"); do
glusterfs_volumes+=,$(echo $share | cut -d/ -f2);
done
local vol_name
for vol_name in $(echo $glusterfs_volumes | sed "s/,/ /g"); do
_create_gluster_volume $vol_name
done
}
# Configure GlusterFS as a backend for Cinder
# Triggered from stack.sh
function configure_cinder_backend_glusterfs {
_create_gluster_volumes $CINDER_GLUSTERFS_SHARES
local be_name=$1
iniset $CINDER_CONF $be_name volume_backend_name $be_name
iniset $CINDER_CONF $be_name volume_driver "cinder.volume.drivers.glusterfs.GlusterfsDriver"
iniset $CINDER_CONF $be_name glusterfs_shares_config "$CINDER_CONF_DIR/glusterfs-shares-$be_name.conf"
if [[ -n "$CINDER_GLUSTERFS_SHARES" ]]; then
CINDER_GLUSTERFS_SHARES=$(echo $CINDER_GLUSTERFS_SHARES | tr ";" "\n")
echo "$CINDER_GLUSTERFS_SHARES" | tee "$CINDER_CONF_DIR/glusterfs-shares-$be_name.conf"
fi
}

View File

@ -1,163 +1,52 @@
# 60-glusterfs.sh - DevStack extras script to install GlusterFS
# Functions to control the configuration and operation of the **GlusterFS** storage service
#!/bin/bash
# devstack/plugin.sh
# Triggers glusterfs specific functions to install and configure GlusterFS
# Dependencies:
#
# - ``functions`` file
# - ``GLUSTERFS_DATA_DIR`` or ``DATA_DIR`` must be defined
# - ``DATA_DIR`` must be defined
# ``stack.sh`` calls the entry points in this order:
#
# - install_glusterfs
# - configure_glusterfs_cinder & configure_privileged_user
# - start_glusterfs
# - configure_cinder_backend_glusterfs
# - stop_glusterfs
# - cleanup_glusterfs
# Defaults
# --------
# GLUSTERFS_PLUGIN_DIR contains the path to devstack-plugin-glusterfs/devstack directory
GLUSTERFS_PLUGIN_DIR=$(readlink -f $(dirname ${BASH_SOURCE[0]}))
# Set ``GLUSTERFS_DATA_DIR`` to the location of GlusterFS drives.
# Default is the common DevStack data directory.
# Default is /var/lib/glusterfs.
GLUSTERFS_DATA_DIR=${GLUSTERFS_DATA_DIR:-/var/lib/glusterfs}
GLUSTERFS_DISK_IMAGE=${DATA_DIR}/cinder/glusterfs.img
# DevStack will create a loop-back disk formatted as XFS to store the
# GlusterFS data. Set ``GLUSTERFS_LOOPBACK_DISK_SIZE`` to the disk size in
# kilobytes.
# GB.
# Default is 4 gigabyte. But we can configure through localrc.
GLUSTERFS_LOOPBACK_DISK_SIZE=${GLUSTERFS_LOOPBACK_DISK_SIZE:-4G}
# Devstack will create GlusterFS shares to store Cinder volumes.
# Those shares can be configured by seting CINDER_GLUSTERFS_SHARES.
# By default CINDER_GLUSTERFS_SHARES="127.0.0.1:/vol1"
CINDER_GLUSTERFS_SHARES=${CINDER_GLUSTERFS_SHARES:-"127.0.0.1:/vol1"}
# Glusterfs volume provisioned type, allowed values are 'thin' or 'thick'.
# Since we only support raw volumes for backup, using 'thick' as default value.
GLUSTERFS_VOLUME_PROV_TYPE=${GLUSTERFS_VOLUME_PROV_TYPE:-"thick"}
# Adding GlusterFS repo to CentOS / RHEL 7 platform.
GLUSTERFS_CENTOS_REPO=${GLUSTERFS_CENTOS_REPO:-"http://download.gluster.org/pub/gluster/glusterfs/LATEST/CentOS/glusterfs-epel.repo"}
# Functions
# ------------
# cleanup_glusterfs() - Remove residual data files, anything left over from previous
# runs that a clean run would need to clean up
function cleanup_glusterfs {
for share in $(echo $CINDER_GLUSTERFS_SHARES | sed "s/;/ /"); do
local mount_point=$(grep $share /proc/mounts | awk '{print $2}')
if [[ -n $mount_point ]]; then
sudo umount $mount_point
fi
done
if [[ -e ${GLUSTERFS_DISK_IMAGE} ]]; then
sudo rm -f ${GLUSTERFS_DISK_IMAGE}
fi
for share in $(echo $CINDER_GLUSTERFS_SHARES | sed "s/;/ /"); do
GLUSTERFS_VOLUMES+=,$(echo $share | cut -d/ -f2);
done
for vol_name in $(echo $GLUSTERFS_VOLUMES | sed "s/,/ /g"); do
sudo gluster --mode=script volume stop $vol_name
sudo gluster --mode=script volume delete $vol_name
done
if [[ "$OFFLINE" = "False" ]]; then
uninstall_package glusterfs-server
fi
if egrep -q ${GLUSTERFS_DATA_DIR} /proc/mounts; then
sudo umount ${GLUSTERFS_DATA_DIR}
fi
sudo rm -rf ${GLUSTERFS_DATA_DIR}
}
# configure_glusterfs_cinder() - Create GlusterFS volumes
function configure_glusterfs_cinder {
for share in $(echo $CINDER_GLUSTERFS_SHARES | sed "s/;/ /"); do
GLUSTERFS_VOLUMES+=,$(echo $share | cut -d/ -f2);
done
if is_fedora; then
stop_glusterfs
start_glusterfs
fi
# create a backing file disk
create_disk ${GLUSTERFS_DISK_IMAGE} ${GLUSTERFS_DATA_DIR} ${GLUSTERFS_LOOPBACK_DISK_SIZE}
for vol_name in $(echo $GLUSTERFS_VOLUMES | sed "s/,/ /g"); do
sudo mkdir -p ${GLUSTERFS_DATA_DIR}/$vol_name
sudo gluster --mode=script volume \
create $vol_name $(hostname):${GLUSTERFS_DATA_DIR}/$vol_name
sudo gluster --mode=script volume start $vol_name
sudo gluster --mode=script volume set $vol_name server.allow-insecure on
done
# Changing file permissions of glusterfs logs.
# This avoids creation of zero sized glusterfs log files while running CI job (Bug: 1455951).
sudo chmod 755 -R /var/log/glusterfs/
}
# this modifies the cinder.conf file and create glusterfs_shares_config file.
function configure_cinder_backend_glusterfs {
local be_name=$1
iniset $CINDER_CONF $be_name volume_backend_name $be_name
iniset $CINDER_CONF $be_name volume_driver "cinder.volume.drivers.glusterfs.GlusterfsDriver"
iniset $CINDER_CONF $be_name glusterfs_shares_config "$CINDER_CONF_DIR/glusterfs-shares-$be_name.conf"
#iniset $CINDER_CONF $be_name volume_prov_type $GLUSTERFS_VOLUME_PROV_TYPE
if [[ -n "$CINDER_GLUSTERFS_SHARES" ]]; then
CINDER_GLUSTERFS_SHARES=$(echo $CINDER_GLUSTERFS_SHARES | tr ";" "\n")
echo "$CINDER_GLUSTERFS_SHARES" | tee "$CINDER_CONF_DIR/glusterfs-shares-$be_name.conf"
fi
}
# install_glusterfs() - Collect source and prepare
function install_glusterfs {
if [[ ${DISTRO} =~ rhel7 ]] && [[ ! -f /etc/yum.repos.d/glusterfs-epel.repo ]]; then
sudo wget $GLUSTERFS_CENTOS_REPO -O /etc/yum.repos.d/glusterfs-epel.repo
elif is_ubuntu; then
sudo wget -O - http://download.gluster.org/pub/gluster/glusterfs/3.6/3.6.2/Debian/wheezy/pubkey.gpg | sudo apt-key add -
sudo echo deb http://download.gluster.org/pub/gluster/glusterfs/3.6/3.6.2/Debian/wheezy/apt wheezy main | sudo tee /etc/apt/sources.list.d/gluster.list
NO_UPDATE_REPOS=False
REPOS_UPDATED=False
fi
install_package glusterfs-server
install_package xfsprogs
}
# start_glusterfs() - Start running processes
function start_glusterfs {
if is_ubuntu; then
sudo service glusterfs-server start
else
sudo service glusterd start
fi
}
# stop_glusterfs() - Stop running processes
function stop_glusterfs {
if is_ubuntu; then
sudo service glusterfs-server stop
else
sudo service glusterd stop
fi
}
# Initializing gluster specific functions
source $GLUSTERFS_PLUGIN_DIR/gluster-functions.sh
if [[ "$1" == "stack" && "$2" == "pre-install" ]]; then
echo_summary "Installing GlusterFS"
install_glusterfs
elif [[ "$1" == "stack" && "$2" == "post-config" ]]; then
if is_service_enabled cinder; then
echo_summary "Configuring Cinder for GlusterFS"
configure_glusterfs_cinder
fi
fi
if [[ "$1" == "unstack" ]]; then