Skip to content

Instantly share code, notes, and snippets.

@kotreshhr
Created October 20, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotreshhr/dd16c5fca425b417c097 to your computer and use it in GitHub Desktop.
Save kotreshhr/dd16c5fca425b417c097 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to verify the Master and Slave Gluster compatibility.
# To use ./gverify <master volume> <slave host> <slave volume>
# Returns 0 if master and slave compatible.
# Considering buffer_size 100MB
BUFFER_SIZE=104857600;
SSH_PORT=52022;
slave_log_file=`gluster --print-logdir`/geo-replication-slaves/slave.log
function SSHM()
{
ssh -p ${SSH_PORT} -q \
-oPasswordAuthentication=no \
-oStrictHostKeyChecking=no \
-oControlMaster=yes \
"$@";
}
function get_inode_num()
{
local os
case `uname -s` in
NetBSD) os="NetBSD";;
Linux) os="Linux";;
*) os="Default";;
esac
if [[ "X$os" = "XNetBSD" ]]; then
echo $(stat -f "%i" "$1")
else
echo $(stat -c "%i" "$1")
fi
}
function umount_lazy()
{
local os
case `uname -s` in
NetBSD) os="NetBSD";;
Linux) os="Linux";;
*) os="Default";;
esac
if [[ "X$os" = "XNetBSD" ]]; then
umount -f -R "$1"
else
umount -l "$1"
fi;
}
function disk_usage()
{
local os
case `uname -s` in
NetBSD) os="NetBSD";;
Linux) os="Linux";;
*) os="Default";;
esac
if [[ "X$os" = "XNetBSD" ]]; then
echo $(df -P "$1")
else
echo $(df -P -B1 "$1")
fi;
}
function cmd_slave()
{
local cmd_line;
cmd_line=$(cat <<EOF
function do_verify() {
ver=\$(gluster --version | head -1 | cut -f2 -d " ");
echo \$ver;
};
source /etc/profile && do_verify;
EOF
);
echo $cmd_line;
}
function master_stats()
{
MASTERVOL=$1;
local d;
local i;
local disk_size;
local used_size;
local ver;
local m_status;
d=$(mktemp -d -t ${0##*/}.XXXXXX 2>/dev/null);
glusterfs -s localhost --xlator-option="*dht.lookup-unhashed=off" --volfile-id $MASTERVOL -l $slave_log_file $d;
i=$(get_inode_num $d);
if [[ "$i" -ne "1" ]]; then
echo 0:0;
exit 1;
fi;
cd $d;
disk_size=$(disk_usage $d | tail -1 | awk "{print \$2}");
used_size=$(disk_usage $d | tail -1 | awk "{print \$3}");
umount_lazy $d;
rmdir $d;
ver=$(gluster --version | head -1 | cut -f2 -d " ");
m_status=$(echo "$disk_size:$used_size:$ver");
echo $m_status
}
function slave_stats()
{
SLAVEUSER=$1;
SLAVEHOST=$2;
SLAVEVOL=$3;
local cmd_line;
local ver;
local status;
d=$(mktemp -d -t ${0##*/}.XXXXXX 2>/dev/null);
glusterfs --xlator-option="*dht.lookup-unhashed=off" --volfile-server $SLAVEHOST --volfile-id $SLAVEVOL -l $slave_log_file $d;
i=$(get_inode_num $d);
if [[ "$i" -ne "1" ]]; then
echo 0:0;
exit 1;
fi;
cd $d;
disk_size=$(disk_usage $d | tail -1 | awk "{print \$2}");
used_size=$(disk_usage $d | tail -1 | awk "{print \$3}");
no_of_files=$(find $d -maxdepth 1 -path "$d/.trashcan" -prune -o -path "$d" -o -print0 -quit);
umount_lazy $d;
rmdir $d;
cmd_line=$(cmd_slave);
ver=`SSHM $SLAVEUSER@$SLAVEHOST bash -c "'$cmd_line'"`;
status=$disk_size:$used_size:$ver:$no_of_files;
echo $status
}
function ping_host ()
{
### Use bash internal socket support
{
exec 100<>/dev/tcp/$1/$2
if [ $? -ne '0' ]; then
return 1;
else
exec 100>&-
return 0;
fi
} 1>&2 2>/dev/null
}
function main()
{
log_file=$5
> $log_file
# Use FORCE_BLOCKER flag in the error message to differentiate
# between the errors which the force command should bypass
# Test tcp connection to port 22, this is necessary since `ping`
# does not work on all environments where 'ssh' is allowed but
# ICMP is filterd
ping_host $3 ${SSH_PORT}
if [ $? -ne 0 ]; then
echo "FORCE_BLOCKER|$3 not reachable." > $log_file
exit 1;
fi;
ssh -p ${SSH_PORT} -oNumberOfPasswordPrompts=0 -oStrictHostKeyChecking=no $2@$3 "echo Testing_Passwordless_SSH";
if [ $? -ne 0 ]; then
echo "FORCE_BLOCKER|Passwordless ssh login has not been setup with $3 for user $2." > $log_file
exit 1;
fi;
ERRORS=0;
master_data=$(master_stats $1);
slave_data=$(slave_stats $2 $3 $4);
master_disk_size=$(echo $master_data | cut -f1 -d':');
slave_disk_size=$(echo $slave_data | cut -f1 -d':');
master_used_size=$(echo $master_data | cut -f2 -d':');
slave_used_size=$(echo $slave_data | cut -f2 -d':');
master_version=$(echo $master_data | cut -f3 -d':');
slave_version=$(echo $slave_data | cut -f3 -d':');
slave_no_of_files=$(echo $slave_data | cut -f4 -d':');
if [[ "x$master_disk_size" = "x" || "x$master_version" = "x" || "$master_disk_size" -eq "0" ]]; then
echo "FORCE_BLOCKER|Unable to fetch master volume details. Please check the master cluster and master volume." > $log_file;
exit 1;
fi;
if [[ "x$slave_disk_size" = "x" || "x$slave_version" = "x" || "$slave_disk_size" -eq "0" ]]; then
echo "FORCE_BLOCKER|Unable to fetch slave volume details. Please check the slave cluster and slave volume." > $log_file;
exit 1;
fi;
# The above checks are mandatory and force command should be blocked
# if they fail. The checks below can be bypassed if force option is
# provided hence no FORCE_BLOCKER flag.
if [ "$slave_disk_size" -lt "$master_disk_size" ]; then
echo "Total disk size of master is greater than disk size of slave." >> $log_file;
ERRORS=$(($ERRORS + 1));
fi
effective_master_used_size=$(( $master_used_size + $BUFFER_SIZE ))
slave_available_size=$(( $slave_disk_size - $slave_used_size ))
master_available_size=$(( $master_disk_size - $effective_master_used_size ));
if [ "$slave_available_size" -lt "$master_available_size" ]; then
echo "Total available size of master is greater than available size of slave" >> $log_file;
ERRORS=$(($ERRORS + 1));
fi
if [ ! -z $slave_no_of_files ]; then
echo "$3::$4 is not empty. Please delete existing files in $3::$4 and retry, or use force to continue without deleting the existing files." >> $log_file;
ERRORS=$(($ERRORS + 1));
fi;
if [[ $master_version > $slave_version ]]; then
echo "Gluster version mismatch between master and slave." >> $log_file;
ERRORS=$(($ERRORS + 1));
fi;
exit $ERRORS;
}
main "$@";
#!/bin/bash
SSH_PORT=52022;
key_val_pair1=`echo $2 | cut -d ',' -f 1`
key_val_pair2=`echo $2 | cut -d ',' -f 2`
key_val_pair3=`echo $2 | cut -d ',' -f 3`
key_val_pair4=`echo $2 | cut -d ',' -f 4`
key_val_pair5=`echo $2 | cut -d ',' -f 5`
mastervol=`echo $1 | cut -d '=' -f 2`
if [ "$mastervol" == "" ]; then
exit;
fi
key=`echo $key_val_pair1 | cut -d '=' -f 1`
val=`echo $key_val_pair1 | cut -d '=' -f 2`
if [ "$key" != "is_push_pem" ]; then
exit;
fi
if [ "$val" != '1' ]; then
exit;
fi
key=`echo $key_val_pair2 | cut -d '=' -f 1`
val=`echo $key_val_pair2 | cut -d '=' -f 2`
if [ "$key" != "pub_file" ]; then
exit;
fi
if [ "$val" == "" ]; then
exit;
fi
pub_file=`echo $val`
pub_file_bname="$(basename $pub_file)"
pub_file_dname="$(dirname $pub_file)"
pub_file_tmp=`echo $val`_tmp
key=`echo $key_val_pair3 | cut -d '=' -f 1`
val=`echo $key_val_pair3 | cut -d '=' -f 2`
if [ "$key" != "slave_user" ]; then
exit;
fi
if [ "$val" == "" ]; then
exit;
fi
slave_user=`echo $val`
key=`echo $key_val_pair4 | cut -d '=' -f 1`
val=`echo $key_val_pair4 | cut -d '=' -f 2`
if [ "$key" != "slave_ip" ]; then
exit;
fi
if [ "$val" == "" ]; then
exit;
fi
slave_ip=`echo $val`
key=`echo $key_val_pair5 | cut -d '=' -f 1`
val=`echo $key_val_pair5 | cut -d '=' -f 2`
if [ "$key" != "slave_vol" ]; then
exit;
fi
if [ "$val" == "" ]; then
exit;
fi
slavevol=`echo $val`
if [ -f $pub_file ]; then
# For a non-root user copy the pub file to the user's home directory
# For a root user copy the pub files to priv_dir->geo-rep.
if [ "$slave_user" != "root" ]; then
slave_user_home_dir=`ssh -p ${SSH_PORT} $slave_user@$slave_ip "getent passwd $slave_user | cut -d ':' -f 6"`
scp -P ${SSH_PORT} $pub_file $slave_user@$slave_ip:$slave_user_home_dir/common_secret.pem.pub_tmp
ssh -p ${SSH_PORT} $slave_user@$slave_ip "mv $slave_user_home_dir/common_secret.pem.pub_tmp $slave_user_home_dir/${mastervol}_${slavevol}_common_secret.pem.pub"
else
scp -P ${SSH_PORT} $pub_file $slave_ip:$pub_file_tmp
ssh -p ${SSH_PORT} $slave_ip "mv $pub_file_tmp ${pub_file_dname}/${mastervol}_${slavevol}_${pub_file_bname}"
ssh -p ${SSH_PORT} $slave_ip "gluster system:: copy file /geo-replication/${mastervol}_${slavevol}_common_secret.pem.pub > /dev/null"
ssh -p ${SSH_PORT} $slave_ip "gluster system:: execute add_secret_pub root geo-replication/${mastervol}_${slavevol}_common_secret.pem.pub > /dev/null"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment