From 322d51de8417142c6ffbf024b198b26069a53f46 Mon Sep 17 00:00:00 2001 From: Vu Tran Date: Wed, 9 Apr 2014 19:59:09 -0400 Subject: swift: add setup package Introduce swift setup package. At boot time, this package setups a simple swift cluster including: * 3 zones * each zone has 1 storage device which are based on loopback devices which the backing files size is controlled by variable SWIFT_BACKING_FILE_SIZE The script /etc/swift/swift_setup.sh is also provided to ease the task of setting up a complicated Swift cluster. It reads a cluster config file, which describes what storage devices are included in what rings, and constructs the cluster. For details of how to use swift_setup.sh and the format of Swift cluster config file please refer to the script's help: $ swift_setup.sh Signed-off-by: Vu Tran --- .../python/python-swift/cluster.conf | 32 +++ .../python/python-swift/swift.init | 74 ++++++ .../python/python-swift/swift_setup.sh | 270 +++++++++++++++++++++ .../recipes-devtools/python/python-swift_git.bb | 55 ++++- 4 files changed, 429 insertions(+), 2 deletions(-) create mode 100644 meta-openstack/recipes-devtools/python/python-swift/cluster.conf create mode 100644 meta-openstack/recipes-devtools/python/python-swift/swift.init create mode 100644 meta-openstack/recipes-devtools/python/python-swift/swift_setup.sh (limited to 'meta-openstack/recipes-devtools/python') diff --git a/meta-openstack/recipes-devtools/python/python-swift/cluster.conf b/meta-openstack/recipes-devtools/python/python-swift/cluster.conf new file mode 100644 index 0000000..3cab8bd --- /dev/null +++ b/meta-openstack/recipes-devtools/python/python-swift/cluster.conf @@ -0,0 +1,32 @@ +# Swift cluster config file +# +# Each line specifies one storage device with the following format: +# +# , , , , +# +# : full path to a block device or full path to regular +# file which is used for backing up a loopback device. +# If is empty string then this device is +# backed up by a directory in local filesystem. +# : the disk format type, e.g. ext4, xfs... +# : the zone id which this block device belongs to +# : a list (separated by space) ring name ("account", "container", +# or "object") which this device is added to +# : weight of this device in Swift zone +# +# Examples: +# +# /dev/sda1, ext4, 1, account, 1 +# /dev/sda2, ext4, 1, container, 1 +# /dev/sda3, ext4, 1, object, 1 +# /dev/sdb1, ext4, 2, account container object, 1 +# /dev/sdb2, ext4, 2, account container object, 1 +# /dev/sdb3, ext4, 2, account container object, 1 +# , ext4, 1, account container object, 1 +# , ext4, 2, account container object, 1 +# , ext4, 3, account container object, 1 + + +%SWIFT_BACKING_FILE_1%, ext4, 1, account container object, 1 +%SWIFT_BACKING_FILE_2%, ext4, 2, account container object, 1 +%SWIFT_BACKING_FILE_3%, ext4, 3, account container object, 1 diff --git a/meta-openstack/recipes-devtools/python/python-swift/swift.init b/meta-openstack/recipes-devtools/python/python-swift/swift.init new file mode 100644 index 0000000..cf12066 --- /dev/null +++ b/meta-openstack/recipes-devtools/python/python-swift/swift.init @@ -0,0 +1,74 @@ +#!/bin/sh + +# chkconfig: 2345 20 20 + +### BEGIN INIT INFO +# Provides: swift-openstack +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 3 5 +# Default-Stop: 0 1 2 6 +# Short-Description: OpenStack Swift +# Description: OpenStack Swift +### END INIT INFO + + +DESC="Swift Cluster" +SWIFT_HOME=/etc/swift +SWIFT_INIT=/usr/bin/swift-init + +start () +{ + echo -n "Starting $DESC..." + if [ ! -e $SWIFT_HOME/account.builder -a ! -e $SWIFT_HOME/container.builder -a ! -e $SWIFT_HOME/object.builder ]; then + echo "no Swift cluster has been setup, failed." + exit 1 + fi + /etc/swift/swift_setup.sh mountdevs + $SWIFT_INIT proxy-server start + $SWIFT_INIT account-server start + $SWIFT_INIT container-server start + $SWIFT_INIT object-server start + echo "done." +} + + +stop () +{ + echo -n "Stopping $DESC..." + $SWIFT_INIT all stop + /etc/swift/swift_setup.sh unmountdevs + echo "done." +} + + +status () +{ + $SWIFT_INIT proxy-server status + $SWIFT_INIT account-server status + $SWIFT_INIT container-server status + $SWIFT_INIT object-server status +} + + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart|force-reload) + stop + start + ;; + status) + status + ;; + *) + echo "Usage: swift {start|stop|force-reload|restart|status}" + exit 1 + ;; +esac + +exit 0 diff --git a/meta-openstack/recipes-devtools/python/python-swift/swift_setup.sh b/meta-openstack/recipes-devtools/python/python-swift/swift_setup.sh new file mode 100644 index 0000000..c1d02ed --- /dev/null +++ b/meta-openstack/recipes-devtools/python/python-swift/swift_setup.sh @@ -0,0 +1,270 @@ +#!/bin/bash + +#set -x + +SWIFT_RING_BUILDER=/usr/bin/swift-ring-builder + + +for_each_dev_in_cluster () +{ + local cmd=$1 + + dev_id=0 + while read line; do + line=`echo $line | sed -e 's/^ *//g' -e 's/ *$//g'` + [ -z "$line" ] && continue + + dev=`echo $line | cut -d ',' -f 1 | sed -e 's/^ *//g' -e 's/ *$//g'` + dev_format=`echo $line | cut -d ',' -f 2 | sed -e 's/^ *//g' -e 's/ *$//g'` + zone=`echo $line | cut -d ',' -f 3 | sed -e 's/^ *//g' -e 's/ *$//g'` + ring_names=`echo $line | cut -d ',' -f 4 | sed -e 's/^ *//g' -e 's/ *$//g'` + weight=`echo $line | cut -d ',' -f 5 | sed -e 's/^ *//g' -e 's/ *$//g'` + + [[ "${dev:0:1}" == "#" ]] && continue + if [ ! -z $dev ] && [ ! -e $dev ]; then + continue + fi + dev_id=$((dev_id+1)) + $cmd "$dev" "$dev_format" "$dev_id" "$zone" "$ring_names" "$weight" $2 $3 $4 + done < $cluster_conf +} + + +mount_dev () +{ + local dev=$1 + local dev_id=$3 + local zone=$4 + + mkdir -p $swift_home/node/z${zone}d${dev_id} + [ -z "$dev" ] && return 1 + if [ -b $dev ]; then + mount $dev $swift_home/node/z${zone}d${dev_id} + else + loopdev=`losetup --show -f $dev` + mount $loopdev $swift_home/node/z${zone}d${dev_id} + fi +} + + +unmount_dev () +{ + local dev=$1 + + [ -z "$dev" ] && return 1 + if [ -b $dev ]; then + umount $dev + else + loopdev=`losetup -j $dev | cut -d ":" -f 1 | tail -1` + if [ ! -z $loopdev ]; then + umount $loopdev + losetup -d $loopdev + fi + fi +} + + +format_dev () +{ + local dev=$1 + local dev_format=$2 + + [ -z "$dev" ] && return 1 + if [ -b $dev ]; then + mkfs.${dev_format} $dev + else + loopdev=`losetup --show -f $dev` + mkfs.${dev_format} $loopdev + sleep 2 + losetup -d $loopdev + fi +} + + +add_dev () +{ + local dev_id=$3 + local zone=$4 + local ring_names=$5 + local weight=$6 + + account_port=6002 + container_port=6001 + object_port=6000 + + for ring in $ring_names; do + eval builder_port=\$${ring}_port + [ -z $builder_port ] && continue + $SWIFT_RING_BUILDER $swift_home/${ring}.builder add -r 1 -z $zone -i $host_ip -p $builder_port -d z${zone}d${dev_id} -w $weight + done +} + + +cluster_create_rings () +{ + if [ ! -f $SWIFT_RING_BUILDER ]; then + echo "ERROR: Swift packages are not installed on this machine" + return 1 + fi + + if [ -e $swift_home/account.builder -o -e $swift_home/container.builder -o -e $swift_home/object.builder ]; then + echo "ERROR: Swift cluster has been created" + return 1 + fi + + sed "s/^swift_hash_path_suffix =.*/swift_hash_path_suffix = `uuidgen`/" -i $swift_home/swift.conf + + # Create 3 ring builders: each ring has 2^12 partition + $SWIFT_RING_BUILDER $swift_home/account.builder create 12 $cluster_replica 1 + $SWIFT_RING_BUILDER $swift_home/container.builder create 12 $cluster_replica 1 + $SWIFT_RING_BUILDER $swift_home/object.builder create 12 $cluster_replica 1 +} + + +cluster_add_devs () +{ + for_each_dev_in_cluster add_dev + + $SWIFT_RING_BUILDER $swift_home/account.builder rebalance + $SWIFT_RING_BUILDER $swift_home/container.builder rebalance + $SWIFT_RING_BUILDER $swift_home/object.builder rebalance +} + + +cluster_delete_all () +{ + for_each_dev_in_cluster unmount_dev + rm -rf $swift_home/*.ring.gz + rm -rf $swift_home/*.builder + rm -rf $swift_home/node +} + + +help () +{ + cat << EOF +usage: swift_setup.sh [-hcirk] + + -h : View this help + -c : Swift cluster config file, default is /etc/swift/cluster.conf + -i : IP address of this machine, default is 127.0.0.1 + -r : The number of Swift replications + -k : Location where Swift builder and ring files stay, default is /etc/swift + + can be one of the followings: + + createrings : Create Swift account, container, and object builder + clean : Delete all Swift build and ring files + mountdevs : Mount all devices specified in cluster config file + unmountdevs : Un-mount all devices specified in cluster config file + formatdevs : Format all devices specified in cluster config file + adddevs : Add all devices specified in cluster config file into + Swift rings + +swift_setup.sh script helps to create or expand Swift cluster with +storage devices in this machine. + +The Swift cluster config file (default is /etc/swift/cluster.conf) describes +how storage devices in this machine can be added into Swift cluster. + +Each line of cluster config file specifies how one storage device can be +added into Swift cluster with the following format: + + , , , , + + : full path to a block device or full path to regular + file which is used for backing up a loopback device. + If is empty string then this device is + backed up by a directory in local filesystem. + : the disk format type, e.g. ext4, xfs... + : the zone id which this block device belongs to + : a list (separated by space) ring name ("account", "container", + or "object") which this device is added to + : weight of this device in Swift zone + + +Example 1: To add storage devices in this machine into an existing +Swift Cluster: + + $ /etc/init.d/swift stop + $ Sync the Swift builders and rings files into this machine + $ Modify /etc/cluster.conf to specify what storage devices + are added into cluster + $ swift_setup.sh formatdevs + $ swift_setup.sh mountdevs + $ swift_setup.sh -i adddevs + $ swift_setup.sh unmountdevs + $ /etc/init.d/swift start + +Example 2: To create an new Swift cluster: + + $ /etc/init.d/swift stop + $ Modify /etc/cluster.conf to reflect the desired cluster + $ swift_setup.sh clean + $ swift_setup.sh createrings + $ swift_setup.sh formatdevs + $ swift_setup.sh mountdevs + $ swift_setup.sh -i adddevs + $ swift_setup.sh unmountdevs + $ /etc/init.d/swift start +EOF +} + + +cluster_conf="/etc/swift/cluster.conf" +swift_home="/etc/swift" +host_ip="127.0.0.1" +cluster_replica="2" + +while getopts "?hc:r:i:k:" opt; do + case $opt in + c) + cluster_conf=$OPTARG + ;; + r) + cluster_replica=$OPTARG + ;; + i) + host_ip=$OPTARG + ;; + k) + swift_home=$OPTARG + ;; + ?|h) + help + exit 0 + ;; + esac +done + +# Move aside the optional arguments +shift $(( $OPTIND - 1 )) + + +case "$1" in + createrings) + cluster_create_rings + ;; + clean) + for_each_dev_in_cluster unmount_dev + cluster_delete_all + ;; + mountdevs) + for_each_dev_in_cluster mount_dev + ;; + unmountdevs) + for_each_dev_in_cluster unmount_dev + ;; + formatdevs) + for_each_dev_in_cluster format_dev + ;; + adddevs) + cluster_add_devs + ;; + *) + help + exit 0 + ;; +esac + +exit 0 diff --git a/meta-openstack/recipes-devtools/python/python-swift_git.bb b/meta-openstack/recipes-devtools/python/python-swift_git.bb index 6ab147d..680dbc3 100644 --- a/meta-openstack/recipes-devtools/python/python-swift_git.bb +++ b/meta-openstack/recipes-devtools/python/python-swift_git.bb @@ -11,18 +11,29 @@ SRC_URI = "git://github.com/openstack/${SRCNAME}.git;branch=stable/havana \ file://proxy-server.conf \ file://dispersion.conf \ file://test.conf \ + file://swift.init \ + file://swift_setup.sh \ + file://cluster.conf \ " SRCREV="2f3526c559fe53ce904b735a81dee6de46127176" PV="2013.2.2+git${SRCPV}" S = "${WORKDIR}/git" -inherit setuptools python-dir hosts identity +inherit setuptools python-dir update-rc.d hosts identity + +# The size of the backing file (in Gigabytes) of loopback devices +# which are used for setting up Swift storage devices. The value +# of "0G" is for disabling loopback devices and using local +# filesystem as Swift storage device. In this case, the local +# filesystem of must support xattrs. e.g ext4 +SWIFT_BACKING_FILE_SIZE ?= "2G" do_install_append() { SWIFT_CONF_DIR=${D}${sysconfdir}/swift install -d ${SWIFT_CONF_DIR} + install -d ${D}${sysconfdir}/init.d/ install -m 600 ${S}/etc/swift.conf-sample ${SWIFT_CONF_DIR}/swift.conf install -m 600 ${WORKDIR}/proxy-server.conf ${SWIFT_CONF_DIR}/proxy-server.conf @@ -31,6 +42,9 @@ do_install_append() { install -m 600 ${S}/etc/object-server.conf-sample ${SWIFT_CONF_DIR}/object-server.conf install -m 600 ${WORKDIR}/dispersion.conf ${SWIFT_CONF_DIR}/dispersion.conf install -m 600 ${WORKDIR}/test.conf ${SWIFT_CONF_DIR}/test.conf + install -m 755 ${WORKDIR}/swift.init ${D}${sysconfdir}/init.d/swift + install -m 700 ${WORKDIR}/swift_setup.sh ${SWIFT_CONF_DIR}/swift_setup.sh + install -m 600 ${WORKDIR}/cluster.conf ${SWIFT_CONF_DIR}/cluster.conf sed 's/^# bind_port =.*/bind_port = 6002/' -i ${SWIFT_CONF_DIR}/account-server.conf sed 's/^# user =.*/user = root/' -i ${SWIFT_CONF_DIR}/account-server.conf @@ -71,7 +85,32 @@ do_install_append() { grep -rl '^from test' ${D}/${PYTHON_SITEPACKAGES_DIR}/${SRCNAME}/test | xargs sed 's/^from test/from swift\.test/g' -i } -PACKAGES += "${SRCNAME}-tests ${SRCNAME}" +pkg_postinst_${SRCNAME}-setup () { + if [ "x$D" != "x" ]; then + exit 1 + fi + + CLUSTER_CONF=/etc/swift/cluster.conf + SWIFT_SETUP='/bin/bash /etc/swift/swift_setup.sh' + + for i in `seq 1 3`; do + BACKING_FILE=/etc/swift/swift_backing_$i + if [ "x${SWIFT_BACKING_FILE_SIZE}" != "x0G" ]; then + truncate -s ${SWIFT_BACKING_FILE_SIZE} $BACKING_FILE + sed "s:%SWIFT_BACKING_FILE_${i}%:$BACKING_FILE:g" -i $CLUSTER_CONF + else + sed "s:%SWIFT_BACKING_FILE_${i}%::g" -i $CLUSTER_CONF + fi + done + + $SWIFT_SETUP createrings + $SWIFT_SETUP formatdevs + $SWIFT_SETUP mountdevs + $SWIFT_SETUP -i "${CONTROLLER_IP}" adddevs + $SWIFT_SETUP unmountdevs +} + +PACKAGES += "${SRCNAME}-tests ${SRCNAME} ${SRCNAME}-setup" FILES_${PN} = "${libdir}/*\ " @@ -79,6 +118,11 @@ FILES_${PN} = "${libdir}/*\ FILES_${SRCNAME}-tests = "${sysconfdir}/${SRCNAME}/test.conf \ " +FILES_${SRCNAME}-setup = "${sysconfdir}/init.d/swift \ + ${sysconfdir}/${SRCNAME}/swift_setup.sh \ + ${sysconfdir}/${SRCNAME}/cluster.conf \ +" + FILES_${SRCNAME} = "${bindir}/* \ ${sysconfdir}/${SRCNAME}/* \ " @@ -101,3 +145,10 @@ RDEPENDS_${PN} += " \ " RDEPENDS_${SRCNAME} = "${PN}" + +RDEPENDS_${SRCNAME} = "${PN}" +RDEPENDS_${SRCNAME}-setup = "${SRCNAME}" + +INITSCRIPT_PACKAGES = "${SRCNAME}-setup" +INITSCRIPT_NAME_${SRCNAME}-setup = "swift" +INITSCRIPT_PARAMS_${SRCNAME}-setup = "defaults" -- cgit v1.2.3-54-g00ecf