#!/bin/bash

set -eu

SERVER_1=10.228.17.100
SERVER_2=10.228.18.100
VOLUME="volume-1"

# 2 identical servers
# Hardware: Dual Socket Xeon CPU E5-2640 v3 (8 cores, 2.60GHz, HT), 128GB DDR4 ECC (8x16GB)
# Storage: 2TB Intel P3520 PCIe-NVMe-SSD
# Network: Gluster: 10GB/s direct connect (no switch), external: 1Gbit/s
# OS: CentOS 7.7, Installed with "Minimal" ISO, everything: Default
# Up2Date as of: 2020-01-21 (Kernel: 3.10.0-1062.9.1.el7.x86_64)
# SELinux: Disabled
# SSH-Key for 1 -> 2 exchanged

# Gluster 6.7 packages via 'centos-release-gluster6'

# If missing: Install Gluster-Packages. Disable Firewall. Enable Glusterd. Probe Server-2
if [ ! -e /usr/sbin/glusterfsd ]
then
    # Note: SELinux is disabled

    yum install -y centos-release-gluster6.noarch
    yum install -y glusterfs glusterfs-api glusterfs-cli glusterfs-client-xlators glusterfs-fuse glusterfs-geo-replication glusterfs-libs glusterfs-server python2-gluster
    ssh root@$SERVER_2 yum install -y centos-release-gluster6.noarch
    ssh root@$SERVER_2 yum install -y glusterfs glusterfs-api glusterfs-cli glusterfs-client-xlators glusterfs-fuse glusterfs-geo-replication glusterfs-libs glusterfs-server python2-gluster

    systemctl disable firewalld
    systemctl stop firewalld
    ssh root@$SERVER_2 systemctl disable firewalld
    ssh root@$SERVER_2 systemctl stop firewalld

    systemctl enable glusterd
    systemctl start glusterd
    ssh root@$SERVER_2 systemctl enable glusterd
    ssh root@$SERVER_2 systemctl start glusterd

    gluster peer probe $SERVER_2
fi

# Create replica-2 volume and setup Test-Case
if [ ! -e /tmp/bricks/volume-1/test0001 ]
then
    # Create
    mkdir /tmp/bricks ; ssh root@$SERVER_2 mkdir /tmp/bricks
    gluster volume create $VOLUME replica 2 transport tcp "$SERVER_1":/tmp/bricks/$VOLUME "$SERVER_2":/tmp/bricks/$VOLUME force
    gluster volume set $VOLUME nfs.disable on
    gluster volume set $VOLUME auth.allow "$SERVER_1","$SERVER_2"
    gluster volume start $VOLUME

    # Mount
    mkdir /tmp/mnt-volume
    mount -n -t glusterfs localhost:$VOLUME /tmp/mnt-volume

    # Setup Test-Case, Create 1000 files
    cd /tmp/mnt-volume
    for dat in $(seq --format=%04.0f 1000)
    do
        echo "Hallo" >test$dat
    done
    cd
    umount /tmp/mnt-volume
fi

# Profile "ls . . ." with 1 and 10 times "."
if [ 1 = 1 ]
then
    for dots in 1 10
    do
        mount -n -t glusterfs localhost:$VOLUME /tmp/mnt-volume

        gluster volume profile $VOLUME start
        gluster volume profile $VOLUME info clear

        cd /tmp/mnt-volume
        ls $(for dat in $(seq ${dots}); do echo . ; done) &>/dev/null
        cd

        gluster volume profile $VOLUME info | tee profile-${dots}-times
        gluster volume profile $VOLUME stop

        umount /tmp/mnt-volume
    done
fi

# Destroy/Delete
if [ 0 = 1 ]
then
    umount /tmp/mnt-volume
    gluster volume stop $VOLUME force
    gluster volume delete $VOLUME
    rm -rf /tmp/bricks /tmp/mnt-volume
    ssh root@$SERVER_2 rm -rf /tmp/bricks
fi
