+#!/bin/bash
+
+# Configure a simple NAS cluster as generated by autocluster..
+
+set -e
+
+ad_admin_pass="@@AD_ADMIN_PASS@@"
+domain_auth="administrator${ad_admin_pass:+%}${ad_admin_pass}"
+
+conf_file="/root/scripts/nas.conf"
+
+wait_until_healthy ()
+{
+ local timeout="${1:-120}"
+
+ echo -n "Wait until healthy [<${timeout}] "
+
+ local count=0
+ while [ $count -lt $timeout ] ; do
+ if ctdb nodestatus all >/dev/null ; then
+ echo "[${count}]"
+ return 0
+ fi
+ echo -n "."
+ count=$(($count + 1))
+ sleep 1
+ done
+
+ echo "[TIMEOUT]"
+ return 1
+}
+
+####################
+# show program usage
+usage ()
+{
+ cat >&2 <<EOF
+Usage: setup_samba.sh [OPTION]
+ options:
+ -x enable script debugging
+ -c config_file config file to load
+ -U username%password domain authentication
+EOF
+ exit 1
+}
+
+
+############################
+# parse command line options
+temp=$(getopt -n "$prog" -o "axhU:c:" -l help -- "$@")
+[ $? != 0 ] && usage
+eval set -- "$temp"
+while true ; do
+ case "$1" in
+ -x) set -x; shift ;;
+ -c) conf_file="$2" ; shift 2 ;;
+ --) shift ; break ;;
+ -h|--help|*) usage ;;
+ esac
+done
+
+auth_type=$(sed -r -n -e 's@^auth_method[[:space:]]*=[[:space:]]*(files|winbind)[[:space:]]*$@\1@p' "$conf_file")
+
+case "$auth_type" in
+ winbind|files) : ;;
+ *)
+ echo "Unsupported authentication type \"${auth_type}\""
+ usage
+esac
+
+domain=$(sed -r -n -e 's@^domain[[:space:]]*=[[:space:]]*([^ ]*)[[:space:]]*$@\1@p' "$conf_file" | tr '[a-z]' '[A-Z]')
+
+kdc=$(sed -r -n -e 's@^kdc[[:space:]]*=[[:space:]]*([^ ]*)[[:space:]]*$@\1@p' "$conf_file")
+
+echo "Enabling ctdb..."
+onnode -p all chkconfig ctdb on
+
+case "$auth_type" in
+ winbind)
+ echo "Setting up NSS, PAM, KRB5..."
+ onnode -p all authconfig --update --nostart \
+ --enablewinbindauth --enablewinbind \
+ --disablekrb5 --krb5kdc=$kdc.$domain --krb5realm=$domain
+ ;;
+ files)
+ echo "Setting up NSS, PAM, KRB5..."
+ onnode -p all authconfig --update --nostart \
+ --disablewinbindauth --disablewinbind \
+ --disablekrb5 --krb5kdc=$kdc.$domain --krb5realm=$domain
+ ;;
+esac
+
+echo "Configuring services..."
+onnode -p all "(cd scripts/cluster_configure && \
+ ./cluster-configure.py -t 'templates/rhel/' -vv '$conf_file')"
+
+case "$auth_type" in
+ winbind)
+ echo "Joining domain"
+ net ads join -U"$domain_auth"
+ ;;
+esac
+
+echo "Restarting ctdb"
+onnode -p all "service ctdb restart"
+
+# let's hit this with a big hammer...
+for i in $(seq 1 5) ; do
+ wait_until_healthy 60 && break
+ echo "Nope, restart ctdb..."
+ onnode -p all "service ctdb restart"
+done
+
+wait_until_healthy
+
+echo "Setup done"