#!/bin/sh -e

##########################################################################
#   Script description:
#       Enable remote access via IPMI on specified nodes.
#       
#   History:
#   Date        Name        Modification
#   2021-11022  Jason Bacon Begin
##########################################################################

usage()
{
    cat << EOM
Usage: $0 [-f first-host] [-l last-host] head|backup|io|compute|vis|all
EOM
    exit 1
}


##########################################################################
#   Function description:
#       Pause until user presses return
##########################################################################

pause()
{
    local junk
    
    printf "Press return to continue..."
    read junk
}


##########################################################################
#   Main
##########################################################################

if [ $# -lt 1 ]; then
    usage
fi

auto-root-check $0 "Sets IPMI password"

cat << EOM

Note:

For security reasons, you may need to manually enable remote IPMI in the
BIOS settings in addition to running $(basename $0).

Examples:

Dell PowerEdge: Enter BMC management (Ctrl+e) during boot.

EOM
pause

while [ 0`printf '%s' "$1" | cut -c 1,1` = 0'-' ]; do
    if [ $1 = '-f' ]; then
	shift
	first_node=$1
	shift
    elif [ $1 = '-l' ]; then
	shift
	last_node=$1
	shift
    else
	usage $0
    fi
done

# FIXME: Factor out redundant node selection code from here and
# cluster-ipmi-power-on
while [ $# -ge 1 ]; do
    node_type=$1
    
    if [ $node_type = 'backup' ] || [ $node_type = 'all' ]; then
	nodes=" `cluster-backup-nodes`"
    fi
    
    if [ $node_type = 'io' ] || [ $node_type = 'all' ]; then
	nodes="$nodes `cluster-file-servers`"
    fi
    
    if [ $node_type = 'vis' ] || [ $node_type = 'all' ]; then
	nodes="$nodes `cluster-vis-nodes`"
    fi
    
    if [ $node_type = 'compute' ] || [ $node_type = 'all' ]; then
	nodes="$nodes `cluster-compute-nodes`"
    fi
    shift
done

# If blank or only spaces
if [ -z "$(echo "$nodes" | sed -e 's| ||g')" ]; then
    printf "No nodes selected.\n"
    exit
fi

if [ -z $first_node ]; then
    first_node=`echo $nodes | awk '{ print $1 }'`
fi
if [ -z $last_node ]; then
    last_node=`echo $nodes | awk '{ print $NF }'`
fi
if ! echo "$nodes" | fgrep -q $first_node; then
    printf "$first_node is not among the selected nodes.\n"
    exit 1
fi
if ! echo "$nodes" | fgrep -q $last_node; then
    printf "$last_node is not among the selected nodes.\n"
    exit 1
fi

in_range=0
for node in $nodes; do
    if [ $node = $first_node ]; then
	in_range=1
    fi
    if [ $in_range = 1 ]; then
	selected_nodes="$selected_nodes $node "
    fi
    if [ $node = $last_node ]; then
	in_range=0
    fi
done

pw_file=/root/.ipmipw
if [ ! -e $pw_file ]; then
    # FIXME: Factor out redundant code from here and auto-idrac-passwd
    pw=''
    pw2=2
    while [ 0"$pw" != 0"$pw2" ]; do
	while [ 0"$pw" = 0 ]; do
	    printf "Password? "
	    stty -echo
	    read pw
	    stty echo
	    if [ 0"$pw" = 0 ]; then
		printf "\nPassword cannot be blank.\n"
	    fi
	done
	printf "\nAgain? "
	stty -echo
	read pw2
	stty echo
	if [ 0"$pw" != 0"$pw2" ]; then
	    printf "\nPasswords do not match.\n"
	    pw=''
	    pw2=2
	fi
    done
    
    # Set perms before creating
    umask 027
    printf "$pw\n" > $pw_file
else
    printf "Using existing $pw_file...\n"
fi

printf "IPMI user ID? [2] "
read userid
if [ 0$userid = 0 ]; then
    userid=2
fi

for node in $selected_nodes; do
    printf "\n============================================================\n"
    printf "Configuring $node...\n"
    printf "============================================================\n\n"
    
    ssh $node auto-ipmi-remote-access on
    pw=$(cat $pw_file)
    ssh -t $node auto-ipmi-passwd --user $userid --passwd $pw
done
