
#!/bin/sh
# File	:  set_nfs_ro.sh
# By	: Maarten.deBoer@Atos.net, 191014
# Subject	: Script to set a NFS-export RO
#(0.2),191017	: Added Vfilers
PGM=`basename $0|cut -d\. -f1`
VER="0.2"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}.log"
ETC="${HOME}/etc/set_nfs.exports"
SSH="/usr/bin/ssh"
DATADIR="${HOME}/data/${PGM}"
DATI=`date +%Y%d%m-%H%M%S`

if [ ! -f ${ETC} ]; then
  echo "  Input-file (${ETC}) NOT found. Exiting..."
  exit 2
fi
if [ ! -d ${DATADIR} ]; then
  echo "  DATA-dir (${DATADIR}) not available. Please create. Exiting..."
  exit 3
fi

echo "  Saving exports data first (into data/${PGM}/<filer>_<vfiler>_${DATI}) ..."
cat ${ETC}|grep -v ^#|awk '{print $1}'|sort -u|while read FILER
do
  cat ${ETC}|grep -v ^#|awk '{print $2}'|sort -u|while read VFILER
  do
    echo "  ${FILER}/${VFILER} ..."
    VSTATUS=`${SSH} ${FILER} "vfiler status ${VFILER}"|awk '{print $2}'`
    if [ "${VSTATUS}" != "running" ]; then
      echo "  ${FILER}/${VFILER} NOT running. Exiting ..."
      exit 4
    else
      ${SSH} ${FILER} "vfiler run -q ${VFILER} exportfs" > ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-save
#      ls -l ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-save
    fi
  done  # VFILER
done  # FILER

touch ${TMP}
# A 3 round (FILER, VFILER, EXPORTS) is needed
cat ${ETC}|grep -v ^#|awk '{print $1}'|sort -u|while read FILER
do
  cat ${ETC}|grep -v ^#|awk '{print $2}'|sort -u|while read VFILER
  do
    echo "  ${FILER}/${VFILER} ..."
    VSTATUS=`${SSH} ${FILER} "vfiler status ${VFILER}"|awk '{print $2}'`
    if [ "${VSTATUS}" != "running" ]; then
      echo "  ${FILER}/${VFILER} NOT running. Exiting ..."
      exit 4
    else
      cp ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-save ${TMP}
      cat ${ETC}|grep -v ^#|grep ${FILER}|awk '{print $3}'|while read EXPORT REST
      do
        echo "|${FILER}|${VFILER}|${EXPORT}|"
        cat ${TMP}|grep -v "${EXPORT}" > ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-new
        cat ${TMP}|grep "${EXPORT}" |sed 's/rw=/ro=/g' >> ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-new
        cp ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-new ${TMP}
      done  # LINE
      cp ${TMP} ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-new
    fi  # VSTATUS

  done  # VFILER

done # FILER

echo ""
echo "  NEW exports to ..."
cat ${ETC}|grep -v ^#|awk '{print $1}'|sort -u|while read FILER
do
  cat ${ETC}|grep -v ^#|awk '{print $2}'|sort -u|while read VFILER
  do
    echo "  ${FILER}/${VFILER} ..."
    VSTATUS=`${SSH} ${FILER} "vfiler status ${VFILER}"|awk '{print $2}'`
    if [ "${VSTATUS}" != "running" ]; then
      echo "  ${FILER}/${VFILER} NOT running. Exiting ..."
      exit 4
    else
# Get ETC-dir for Vfiler
# When vfiler0, then ETCDIR=""
      if [ "${VFILER}" = "vfiler0" ]; then
        ETCVOL=""
      else
        ETCVOL=`${SSH} ${FILER} "vfiler status -a ${VFILER}"|grep etc|awk '{print $2}'`
      fi
      echo "  Writing ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-new to ${FILER}:${ETCVOL}/etc/exports ..."
#      cat ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-new
      cat ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-new|${SSH} ${FILER} "wrfile ${ETCVOL}/etc/exports"
      echo "  rdfile ${ETCVOL}/etc/exports ..."
#      ${SSH} ${FILER} "rdfile ${ETCVOL}/etc/exports"
      echo "  exportfs -a ..."
      ${SSH} ${FILER} "vfiler run -q ${VFILER} exportfs -a"
    fi  # VSTATUS
  done  # VFILER
done  # FILER

echo ""
echo ""
echo "  Checking LIVE at Vfilers ..."
cat ${ETC}|grep -v ^#|while read LINE
do
  FILER=`echo ${LINE}|awk '{print $1}'`
  VFILER=`echo ${LINE}|awk '{print $2}'`
  EXPORT=`echo ${LINE}|awk '{print $3}'`
  echo "  ${FILER}/${VFILER}:${EXPORT} ... "
  VSTATUS=`${SSH} -n ${FILER} "vfiler status ${VFILER}"|awk '{print $2}'`
  if [ "${VSTATUS}" != "running" ]; then
    echo "  ${FILER}/${VFILER} NOT running. Exiting ..."
    exit 4
  else
#  cat ${DATADIR}/${FILER}_${DATI}.exports-new|grep ${EXPORT}
    ${SSH} -n ${FILER} "vfiler run -q ${VFILER} exportfs"|grep ${EXPORT}
  fi  # VSTATUS
done  # cat ETC


exit 0

