Jump to content

User:Dzahn/swift-add

From Wikitech
#!/bin/bash
# add devices (drives) to swift rings
# http://wikitech.wikimedia.org/view/Swift/How_To#Add_a_device_.28drive.29_to_a_ring
# dzahn - 20120307


DRY_RUN=1

# uncomment set -x to watch what's executed as it runs #'I guess GeSHi has a bug…
#set -x

declare -a devices=({c..l}) #(upper bound is letter l not # 1!)
declare -a rings=( account container object)

read -p "Which zone to add to?" zone
# zone=5
read -p "Weight to use? (100 for 2TB drives; scale up or down linearly)" weight
# weight=100
myIP="$(ip addr show eth0 | awk '/ inet /{split($2,A,"/"); print A[1]; q}')"

builder="$(which swift-ring-builder)"


for ring in "${rings[@]}" ; do

        case "$ring" in
                object) myPort=6000;;
                container) myPort=6001;;
                account) myPort=6002;;
        esac

        for device in "${devices[@]}" ; do
                cmd=("$builder" /etc/swift/${ring}.builder add "z${zone}-${myIP}:${myPort}/sd${device}1" "${weight}")
                if [ "${DRY_RUN}" -ge 1 ]; then
                        printf '%s ' "${cmd[@]}" | perl -pe 'chomp;s/\s*$/\n/;' # echo can eat some params; printf is more generic/covers corner cases
                else
                        "${cmd[@]}"
                fi
                        
        done

        if [ ! "${DRY_RUN}" -ge 1 ]; then
                "$builder" /etc/swift/${ring}.builder search "${myIP}"
                "$builder" /etc/swift/${ring}.builder rebalance
        fi
done