#!/usr/bin/bash
#
# This script is intended to be executed on all members of a gluster cluster, both the servers (currently there must be
# exactly 2 servers) and the clients which mount the service.
#

# Supported/required ng-kickstart variables:
#  * clusterprivateipaddrs - Comma seperated list of the private IP addresses of the gluster hosts (eg 1.2.3.4,5.6.7.8)
#  * glustermount          - The path which the gluster service (endpoint) should be mounted on (eg /srv/gluster)
#  * glusterbrickmount     - The path which the gluster brick is mounted on (eg /srv/gluster-bricks/xvdf)
#  * glustermountopts      - Optional additional mount flags for the gluster service (eg negative-timeout=1)
#

# Propagate any xtrace (set -x) setting to sub-scripts
export SHELLOPTS

if [ -f /etc/sysconfig/ng-kickstart ]; then
    source /etc/sysconfig/ng-kickstart

    # Split the cluster IP addresses and perform basic va		  | logger -t $(basename $0)lidation
    PRIVATEIPADDR1="${clusterprivateipaddrs%%,*}"
    PRIVATEIPADDR2="${clusterprivateipaddrs##*,}"
    if [[ -z "$PRIVATEIPADDR1" || -z "$PRIVATEIPADDR1" ]] ; then
        echo "ERROR: $(basename $0) - unable to determine cluster IPs from ng-kickstart (clusterprivateipaddrs)" >&2
        exit 1
    fi

    # Import legacy name (ebsmount) for the brick mount point
    glusterbrickmount=${glusterbrickmount:-$ebsmount}

    # Strip (potential) trailing slashes from the mount paths
    GLUSTERMOUNT="${glustermount%/}"
    BRICKMOUNT="${glusterbrickmount%/}"

    # Find our IP addresses
    IPADDRS=$(hostname --all-ip-addresses)
    # In case the network isn't up yet, try the AWS metadata URL as a fallback
    [ -z "$IPADDRS" ] && IPADDRS=$(curl --silent http://169.254.169.254/latest/meta-data/local-ipv4)
    if [ -z "$IPADDRS" ]; then
        echo "ERROR: $(basename $0) - unable to determine any local IP addresses for this host" >&2
        exit 1
    fi

    if [[ "$IPADDRS" =~ (^| )$PRIVATEIPADDR1( |$) ]]; then
        # Instantiate the service and volume (node 1)
        echo "This is gluster node 1, initializing gluster brick and daemon" >&2
        /usr/libexec/ng-server-config/ng-gluster-setup \
            -1 "$PRIVATEIPADDR1" \
            -2 "$PRIVATEIPADDR2" \
            -m "$BRICKMOUNT" \
            -p "$GLUSTERMOUNT" \
            -c \
            -o "$glustermountopts"
        # Tune the volume/service, but only on node 1
        /usr/libexec/ng-server-config/autotune-gluster
    elif [[  "$IPADDRS" =~ (^| )$PRIVATEIPADDR2( |$) ]]; then
        # Instantiate the service and volume (node 2)
        echo "This is gluster node 2, initializing gluster brick and daemon" >&2
        /usr/libexec/ng-server-config/ng-gluster-setup \
            -1 "$PRIVATEIPADDR2" \
            -2 "$PRIVATEIPADDR1" \
            -m "$BRICKMOUNT" \
            -p "$GLUSTERMOUNT" \
            -o "$glustermountopts"
    else
	    echo "This is a gluster client configuring gluster mount" >&2
        /usr/libexec/ng-server-config/ng-gluster-setup \
            -1 "$PRIVATEIPADDR1" \
            -2 "$PRIVATEIPADDR2" \
            -p "$GLUSTERMOUNT" \
            -o "$glustermountopts"
    fi
fi
