#!/bin/bash

# Cluster Clone Script
# ------- ----- ------
# This script is designed to duplicate the existing configuration of a cluster. It is ideal for "cloning" a cluster. If you need to
# setup another cluster SIMILAR to the original cluster, you will need to pay special attention to numerous variables
# such as cluster/client/vserver/management/IPMI IPs and cluster name etc

# Changelog - 03/16 - changed cloud bucket variable.  It was "grep bucket" and is now "grep "\<bucket\>"" - Thanks KOber
# Changelog - 03/22 - added the command to upload the latest cluster_rebuild directory once this script finishes
# Changelog - 04/04 - when adding a junction with 'inheritPolicy' set to 'no', a policy needs provided - Thanks KOber
# Changelog - 05/07 - added Initial cluster object information
# Changelog - 05/24 - added Share ACL/ACE
# Changelog - 05/25 - Export rules with a "network" scope was not working
# Changelog - 07/03 - Removed licensing since the cluster ID changes.
# Changelog - 07/03 - Configured Atmos core filer creation so $SERVERNAME =  $NETWORK (prior to this $BUCKET.$NETWORK was not correct)
# Changelog - 07/03 - Changed Cloud snapshots so that it only queries cloud core filers which were configured for snapshots, not all cloud core filers.
# Changelog - 07/25 - Changed the cache policy search to explicitly exclude "Full Caching" so that cache policies named like "Full Caching with 1m WB" would not be excluded
# Changelog - 07/26 - Removed the commas for the corefiler.create section
# Changelog - 10/10 - use dbutil for any custom settings with note of "not in cluster" to keep things from being confusing
# Changelog - 10/31 - add simple vserver creation
# Changelog - 11/02 - added the ability to change a core filer's network. Thanks Bogacki
# to do - export rules for netgroups. This does not work due to an averecmd issue.  Engineering ticket 24116 was created to address this.  The script will output the netgroup rule
# Changelog - 04/25 - added functionality for AvereOS 4.6, 4.7 and 4.8
# Changelog - 05/02 - improved performance for getting CIFS shares
# Changelog - 05/14 - added the "createSubdir" setting when creating junctions
# Changelog - 05/30 - enabled mapping old vserver internal name to new vserver internal name (similar to mass custom settings). Thanks Griffith
# Changelog - 05/30 - modified commands to query syslog servers for code earlier than 4.7. Thanks Griffith
# Changelog - 06/07 - discovered custom settings for vserver and cpolicyActive/cpolicyAdmin would not be mapped properly on the new cluster. Fixed
# Changelog - 06/27 - added CIFS "Server Signing"
# Changelog - 08/10 - added 'force':'$FORCE' for NFS core filer creation
# Changelog - 08/17 - fixed a bug with cifs.addShare
# Changelog - 11/12 - changed "echo "# "`echo $CLUSTER|grep name|grep -v clusterIPs` >> $OUTPUT" to "echo "# "`echo $CLUSTER|grep name|grep -v clusterIP` >> $OUTPUT"
# Changelog - 11/12 - changed "echo "# "`echo $CLUSTER|grep clusterIPs` >> $OUTPUT " to "echo "# "`echo $CLUSTER|grep clusterIP0` >> $OUTPUT"
# Changelog - 11/13 - changed "if [ $S3TYPE = "ECS" ] || [ $S3TYPE = "ATMOS" ]; then" to "if [[ $S3TYPE = "ECS" ]] || [[ $S3TYPE = "ATMOS" ]]; then"
# Changelog - 11/13 - cleaned up old comments
# Changelog - 11/13 - added the ability to add a network that is not the cluster network
# Changelog - 11/13 - ignored the vserverX.smbacldMassids custom settings since they are added automatically
# Changelog - 11/19 - added a 3 second pause after each core filer creation
# Changelog - 11/19 - fixed the cluster.addNetworkAddressRange to eliminate the last '}' typo
# Changelog - 12/13 - Changed the way we poll the directory services for 'default' and 'login'
# Changelog - 12/14 - excluded nodeMgmtIP when trying to collect the name of the cluster with cluster.get
# Changelog - 05/02 - added the trailing '}' for cluster.addClusterIPs
# Changelog - 2019/08/15 - Modified the vserver add to support vlans.  Also moved the section AFTER the core filer creation because a simple vserver can not be created without an existing core filer.
#                          Added a section to indicate what version of config_restore is being executed
#                          Moved the custom setting section to be after the core filer creation to allow more time for the core filers to be added
#                          Removed some old comments
# Changelog - 2019/09/03 - To ignoreWarnings if a core filer already exists, the 'true' option was changed to 'True'
#                          When an Azure core filer is added, the Network name should match the Server Name (similar to ECS and ATMOS).  Added this conditional
# Changelog - 2019/09/26 - added corefiler.modify [corefiler_name] {'clientSuspend':'no'} after all core filers get created
# Changelog - 2020/03/19 - had to modify addJunction function because 'language' was an added option in newer code 6515549
#                          found a bug with generating the cache policy. it has been corrected
# Changelog - 2020/05/12 - when doing a cluster.get and DNSdomain is empty, it will return 'undefined'.  if doing a restore with this setting, it may cause
#                          problems with the DNS settings.  So the logic was added to convert 'undefined' to an empty set for cluster.modify
#                          Users other than 'admin' will have their passwords set to null. Meaning they do not have a password to log in. This should be
#                          set by the admin once the cluster is running

clear
TIMESTAMP=`date +%Y-%m-%d_%T`
OUTPUT_DIR="/support/Backups/cluster_rebuild_$TIMESTAMP"
mkdir -p $OUTPUT_DIR
export OUTPUT="$OUTPUT_DIR/1-config_restore.sh"
RELEASE=`averecmd cluster.get|grep activeImage|cut -c 51,53`

touch $OUTPUT_DIR/1-config_restore.sh
chmod 700 $OUTPUT_DIR/1-config_restore.sh

echo "#Initial cluster object information" >> $OUTPUT
IFS=$' ';CLUSTER=`averecmd --pretty cluster.get`
echo "# "`echo $CLUSTER|grep activeImage` >> $OUTPUT
echo "# "`echo $CLUSTER|grep name|egrep -v 'clusterIP|nodeMgmtIP'` >> $OUTPUT
echo "# "`echo $CLUSTER|grep mgmtIP` >> $OUTPUT
echo "# "`echo $CLUSTER|grep default_router` >> $OUTPUT
echo "# "`echo $CLUSTER|grep clusterIP0` >> $OUTPUT
echo "# "`echo $CLUSTER|grep DNSserver` >> $OUTPUT
echo "#  "`echo $CLUSTER|grep DNSdomain|cut -d { -f 2-` >> $OUTPUT
echo "# "`echo $CLUSTER|grep NTPservers` >> $OUTPUT
echo "#  "`grep "Changelog - " /support/Backups/config_restore.sh|tail -2|head -1|cut -d '-' -f 1,2` >> $OUTPUT
echo "" >> $OUTPUT

echo "#VLAN" |tee -a $OUTPUT
IFS=$'\n';for i in `lsu cluster.vlans|grep -v _|cut -d ':' -f 1`;do \
  if [ $i ]; then
    NAME=`lsu $i|grep "\<name\>"|cut -d ' ' -f 2`
    ROLES=`lsu $i|grep roles|cut -d ' ' -f 2`
    MTU=`lsu $i|grep mtu|cut -d ' ' -f 2`
    ROUTER=`averecmd --pretty cluster.getVLAN $NAME|grep router|cut -d "'" -f 4`
    TAG=`averecmd --pretty cluster.getVLAN $NAME|grep tag|cut -d "'" -f 4`
    echo "averecmd cluster.addVLAN $NAME $TAG $ROUTER $ROLES $MTU" >> $OUTPUT
  fi
done

echo |tee -a $OUTPUT
echo "#Cluster Networks - Address ranges" |tee -a $OUTPUT
echo |tee -a $OUTPUT
echo "#Cluster Networks - Non-cluster IP ranges, VLANS" |tee -a $OUTPUT
IFS=$'\n';for i in `averecmd cluster.listNetworks|awk '{print $1}'`; do \
  ADDRESSESPERNODE=`averecmd cluster.getNetwork $i|grep addressesPerNode|cut -d "'" -f 2`
  echo "averecmd cluster.addNetwork $i \"{'addressesPerNode':'$ADDRESSESPERNODE'}\"" >> $OUTPUT
  if [ $i = "cluster" ]; then \
    for j in `averecmd cluster.getNetwork $i|grep addressRange|grep -v addressRange0`; do \
      NETWORK_NAME=`echo $i`
      RANGE=`echo $j|cut -d "," -f 2-|rev|cut -c 3-|rev`
      echo "averecmd cluster.addClusterIPs \"{$RANGE}\"" >> $OUTPUT
      echo "averecmd cluster.addNetworkAddressRange $NETWORK_NAME \"{$RANGE}\"" >> $OUTPUT
    done
  else
    for j in `averecmd cluster.getNetwork $i|grep addressRange`; do \
      NETWORK_NAME=`echo $i`
      RANGE=`echo $j|cut -d "," -f 2-|rev|cut -c 3-|rev`
#      echo "averecmd cluster.addClusterIPs \"{$RANGE\"" >> $OUTPUT
      echo "averecmd cluster.addNetworkAddressRange $NETWORK_NAME \"{$RANGE}\"" >> $OUTPUT
    done
  fi
done

# Moving the VServer section after the core filer add section.  This is because simple vservers require a core filer name and a simple vserver can't be created without an exisiting core filer
#echo |tee -a $OUTPUT
#echo "#Manage VServers" |tee -a $OUTPUT
#IFS=$'\n';for i in `averecmd vserver.list`; do \
#  for j in `averecmd vserver.get $i`;do \
#    FIRST_IP=`echo $j |awk -F":" '{print $12}'|cut -d "'" -f 2`
#    LAST_IP=`echo $j|awk -F":" '{print $15}'|cut -d "'" -f 2`
#    NETMASK=`echo $j|awk -F":" '{print $13}'|cut -d "'" -f 2`
#    MASS=`echo $j|cut -d "'" -f 29`
#    TYPE=`echo $j|cut -d "'" -f 59`
#    if [ $TYPE == "simple" ]; then \
#      echo "averecmd vserver.create $i \"{'firstIP':'$FIRST_IP','netmask':'$NETMASK','lastIP':'$LAST_IP'}\" $MASS" >> $OUTPUT
#    else
#      echo "averecmd vserver.create $i \"{'firstIP':'$FIRST_IP','netmask':'$NETMASK','lastIP':'$LAST_IP'}\"" >> $OUTPUT
#    fi
#  done
#done

echo |tee -a $OUTPUT
echo "#Manage Caching Policies" |tee -a $OUTPUT
#IFS=$'\n';for i in `averecmd --pretty cachePolicy.list|egrep -v "'Clients Bypassing the Cluster'|'Read Caching'|'Read and Write Caching'|'Full Caching'|'Transitioning Clients Before or After a Migration'"|grep name|awk -F"'" '{print $4}'`;do \

IFS=$'\n';for i in `averecmd cachePolicy.list|egrep -v "'Clients Bypassing the Cluster'|'Read Caching'|'Read and Write Caching'|'Full Caching'|'Transitioning Clients Before or After a Migration'"|grep name|awk -F "name" '{print $2}'|cut -d "'" -f 3`; do \
  WRITEBACKDELAY=`averecmd cachePolicy.get $i|grep writebackDelay|awk -F"'" '{print $2}'`
  NAME=`averecmd cachePolicy.get $i|grep name|awk -F"'" '{print $2}'`
  LOCALDIRECTORIES=`averecmd cachePolicy.get $i|grep localDirectories|awk -F"'" '{print $2}'`
  CACHEMODE=`averecmd cachePolicy.get $i|grep cacheMode|awk -F"'" '{print $2}'`
  CHECKATTRIBUTES=`averecmd cachePolicy.get $i|grep checkAttributes|cut -d '{' -f 2|cut -d '}' -f 1`
  DESCRIPTION=`averecmd cachePolicy.get $i|grep description|awk -F"'" '{print $2}'`
  echo "averecmd cachePolicy.create \"$NAME\" $CACHEMODE $WRITEBACKDELAY \"{$CHECKATTRIBUTES}\" $LOCALDIRECTORIES {} \"\" \"$DESCRIPTION\"" >> $OUTPUT
done

echo |tee -a $OUTPUT
echo "#Manage Core Filers and Core Filer Details" |tee -a $OUTPUT

IFS=$'\n';for i in `averecmd corefiler.list`;do \
  TYPE=`averecmd --pretty corefiler.get $i|grep type|cut -d "'" -f 4`
  if [ $TYPE == "NFS" ]; then
    CORE_FILER=`averecmd --pretty corefiler.get $i|egrep "filerClass|\<name\>|\<network\>|\<networkName\>|policyName"`
    FILERCLASS=`echo $CORE_FILER|tr ',' '\n'|grep filerClass|cut -d "'" -f 4`
    NAME=`echo $CORE_FILER|tr ',' '\n'|grep '\<name\>'|cut -d "'" -f 4`
    NETWORK=`echo $CORE_FILER|tr ',' '\n'|grep '\<network\>'|cut -d "'" -f 4`
    NETWORKNAME=`echo $CORE_FILER|tr ',' '\n'|grep '\<networkName\>'|cut -d "'" -f 4`
    POLICYNAME=`echo $CORE_FILER|tr ',' '\n'|grep '\<policyName\>'|cut -d "'" -f 4`
    FORCE=True
    echo "averecmd corefiler.create \"$NAME\" \"$NETWORKNAME\" True \"{'filerClass':'$FILERCLASS', 'force':'$FORCE', 'cachePolicy':'$POLICYNAME'}\";sleep 3" >> $OUTPUT
	echo "averecmd corefiler.modify \"$NAME\" \"{'clientSuspend':'no'}\"">> $OUTPUT
  fi
  if [ "$NETWORK" != "cluster" ]; then
    echo "averecmd corefiler.modifyNetwork \"$NAME\" \"$NETWORK\"" >> $OUTPUT
  fi

  if  [ $TYPE == "cloud" ]; then
    CORE_FILER=`averecmd --pretty corefiler.get $i`
    NAME=`echo $i`
    NETWORKNAME=`echo $CORE_FILER|tr ',' '\n'|grep '\<networkName\>'|cut -d "'" -f 4`
    CLOUDTYPE=`echo $CORE_FILER|tr ',' '\n'|grep '\<cloudType\>'|cut -d "'" -f 4`
    FORCE=True
    BUCKET=`echo $CORE_FILER|tr ',' '\n'|grep '\<bucket\>'|cut -d "'" -f 4`
    CLOUDCREDENTIAL=`echo $CORE_FILER|tr ',' '\n'|grep '\<cloudCredential\>'|cut -d "'" -f 4`
    CRYPTOMODE=`echo $CORE_FILER|tr ',' '\n'|grep '\<cryptoMode\>'|cut -d "'" -f 4`
    COMPRESSMODE=`echo $CORE_FILER|tr ',' '\n'|grep '\<compressMode\>'|cut -d "'" -f 4`
    CACHEPOLICYNAME=`echo $CORE_FILER|tr ',' '\n'|grep '\<policyName\>'|cut -d "'" -f 4`
    SSLVERIFY=`echo $CORE_FILER|tr ',' '\n'|grep '\<sslVerifyMode\>'|cut -d "'" -f 4`
    PORT=`echo $CORE_FILER|tr ',' '\n'|grep '\<port\>'|cut -d "'" -f 4`
    HTTPS=`echo $CORE_FILER|tr ',' '\n'|grep '\<https\>'|cut -d "'" -f 4`
    NETWORK=`echo $CORE_FILER|tr ',' '\n'|grep '\<network\>'|cut -d "'" -f 4`
    REGION=`echo $CORE_FILER|tr ',' '\n'|grep '\<region\>'|cut -d "'" -f 4`
    S3TYPE=`echo $CORE_FILER|tr ',' '\n'|grep '\<s3Type\>'|cut -d "'" -f 4`
    if [[ $S3TYPE = "ECS" ]] || [[ $S3TYPE = "ATMOS" ]] || [[ $CLOUDTYPE = "azure" ]]; then
      SERVERNAME=$NETWORKNAME
    else
      SERVERNAME=$BUCKET.$NETWORKNAME
    fi
    echo "averecmd corefiler.createCloudFiler \"$NAME\" \"{'cloudType':'$CLOUDTYPE', 'force':'$FORCE', 'bucket':'$BUCKET', 'cloudCredential':'$CLOUDCREDENTIAL', 'cryptoMode':'$CRYPTOMODE','compressMode':'$COMPRESSMODE', 'cachePolicyName':'$CACHEPOLICYNAME', 'sslVerifyMode':'$SSLVERIFY', 'port':'$PORT', 'https':'$HTTPS', 'networkName':'$NETWORKNAME', 'bucketContents':'used', 'region':'$REGION', 's3Type':'$S3TYPE', 'serverName':'$SERVERNAME'}\";sleep 3" >> $OUTPUT
    echo "averecmd corefiler.modify \"$NAME\" \"{'clientSuspend':'no'}\"">> $OUTPUT

    if [ $NETWORK != "cluster" ]; then
      echo "averecmd corefiler.modifyNetwork \"$NAME\" \"$NETWORK\"" >> $OUTPUT
    fi
  fi
done

echo |tee -a $OUTPUT
echo "#Manage VServers" |tee -a $OUTPUT
for VSERVER in `lsu vservers|grep -v _|cut -d ':' -f 1`; do \
  for j in `lsu $VSERVER|grep dataIP|cut -d ' ' -f 2`; do \
    FIRST_IP=`echo $j|cut -d ',' -f 1`
    LAST_IP=`echo $j|cut -d ',' -f 2|cut -d ';' -f 1`
    NETMASK=`echo $j|cut -d ',' -f 2|cut -d ':' -f 2`
    FIB=`echo $j|cut -d ' ' -f2|cut -d ',' -f 3|cut -d ':' -f 2`
    NAME=`lsu $VSERVER name|cut -d ' ' -f 2`
    VLAN=`averecmd --pretty vserver.get $NAME|grep vlan|cut -d "'" -f 4`
    TYPE=`averecmd --pretty vserver.get $NAME|grep type|cut -d "'" -f 4`
    MASS=`averecmd --pretty vserver.get $NAME|grep mass|cut -d "'" -f 4`
#    echo $j
#    echo "Name:     " $NAME
#    echo "First IP: " $FIRST_IP
#    echo "Last IP:  " $LAST_IP
#    echo "Netmask:  " $NETMASK
#    echo "FIB:      " $FIB
#    echo "VLAN:     " $VLAN
#    echo "Type:     " $TYPE
#    echo "Mass:     " $MASS
    if [[ $TYPE == "simple" ]]; then \
	  if  [[ $VLAN ]]; then \
        echo "averecmd vserver.create $NAME \"{'firstIP':'$FIRST_IP','netmask':'$NETMASK','vlan':'$VLAN','lastIP':'$LAST_IP'}\" $MASS" >> $OUTPUT
	  else
	    echo "averecmd vserver.create $NAME \"{'firstIP':'$FIRST_IP','netmask':'$NETMASK','lastIP':'$LAST_IP'}\" $MASS" >> $OUTPUT
	  fi
    elif [[ $TYPE != "simple" ]]; then \
      if [[ $VLAN ]]; then \
        echo "averecmd vserver.create $NAME \"{'firstIP':'$FIRST_IP','netmask':'$NETMASK','vlan':'$VLAN','lastIP':'$LAST_IP'}\"" >> $OUTPUT
      else
        echo "averecmd vserver.create $NAME \"{'firstIP':'$FIRST_IP','netmask':'$NETMASK','lastIP':'$LAST_IP'}\"" >> $OUTPUT
	  fi
    fi
#  echo
  done
done

echo |tee -a $OUTPUT
echo "#Custom settings are addressed with \"2-run_on_new_cluster_mass_mappings.sh\", \"3-run_on_new_cluster_new_custom_settings.sh\", \"4-run_on_new_cluster_new_custom_settings.sh\"" |tee -a $OUTPUT

#IFS=$'\n';for i in `name_mapping.py --custom|egrep -v 'Custom Settings:|----------------'`;do \
IFS=$'\n';for i in `name_mapping.py --custom|egrep -v 'Custom Settings:|----------------|smbacldMassids'`;do \
  NAME=`echo $i|cut -d ':' -f 1|awk '{print $1}'`
  CHECKCODE=`averecmd support.checkCode $NAME|grep check|cut -d "'" -f 2`
  VALUE=`echo $i|awk '{print $2}'`
  NOTE=`echo $i|cut -d '(' -f 2`;
  if [[ $NOTE = *"NOT in cluster.settings"* ]]; then \
    NAME2=`echo $NAME|tr . ' '`
    echo "dbutil.py set $NAME2 $VALUE -x" >> $OUTPUT_DIR/original_custom_settings.txt
  else
    echo "averecmd support.setCustomSetting $NAME $CHECKCODE $VALUE \"($NOTE\"" >> $OUTPUT_DIR/original_custom_settings.txt
  fi
done

echo |tee -a $OUTPUT
echo "#Cloud Snapshot Policies" |tee -a $OUTPUT
# New in 4.7
for i in `averecmd --pretty snapshot.listPolicies|grep corefilers|cut -d "'" -f 4`; do \
  if [ `averecmd --pretty corefiler.get $i|grep type|cut -d "'" -f 4` = "cloud" ]; then
    DEFAULTWEEKDAY=`averecmd snapshot.getFilerPolicy $i|grep defaultWeekDay|cut -d "'" -f 2`
    NAME=`averecmd snapshot.getFilerPolicy $i|grep name|cut -d "'" -f 2`
    DEFAULTMONTHDAY=`averecmd snapshot.getFilerPolicy $i|grep defaultMonthDay|cut -d "'" -f 2`
    DEFAULTTIME=`averecmd snapshot.getFilerPolicy $i|grep defaultTime|cut -d "'" -f 2`
    NOTE=`averecmd snapshot.getFilerPolicy $i|grep note|cut -d "'" -f 2`
    POLICY=`averecmd snapshot.getFilerPolicy $i|grep policy|cut -d "'" -f 2-|rev|cut -d "'" -f 2-|rev`
    if [ ! `averecmd snapshot.getFilerPolicy $i|grep snapType|cut -d "'" -f 2` ]; then
      SNAPTYPE=strict
    else
      SNAPTYPE=`averecmd snapshot.getFilerPolicy $i|grep snapType|cut -d "'" -f 2`
    fi
    if [ "$POLICY" ]; then
      if [ "$RELEASE" -ge "48" ]; then
        echo "averecmd snapshot.createPolicy \"$NAME\" \"$POLICY\" \"{'note':'$NOTE','defaultTime':'$DEFAULTTIME', 'snapType':'$SNAPTYPE'}\"" >> $OUTPUT
      else
        echo "averecmd snapshot.createPolicy \"$NAME\" \"$POLICY\" \"{'note':'$NOTE','defaultTime':'$DEFAULTTIME'}\"" >> $OUTPUT
      fi
      echo "sleep 5" >> $OUTPUT
      echo "averecmd snapshot.modifyFilerPolicy $i \"$NAME\"" >> $OUTPUT
    fi
  fi
done

echo |tee -a $OUTPUT
echo "#NFS" |tee -a $OUTPUT
N=0;IFS=$'\n';for i in `averecmd vserver.list`;do \
  for j in `averecmd nfs.get $i`; do \
    A_NFS[$N]=`echo $j|cut -d "'" -f 2`
    let "N=$N+1"
  done
  RWSIZE=`echo ${A_NFS[0]}`
  KERBEROS=`echo ${A_NFS[1]}`
  EXTENDEDGROUPS=`echo ${A_NFS[2]}`
  echo "averecmd nfs.modify $i \"{'kerberos':'$KERBEROS','extendedGroups':'$EXTENDEDGROUPS'}\"" >> $OUTPUT
done

echo |tee -a $OUTPUT
echo "#Namespace" |tee -a $OUTPUT
echo "sleep 5" >> $OUTPUT
#IFS=$'\n';for i in `averecmd vserver.list`;do \
#  for j in `averecmd vserver.listJunctions $i`;do \
#    SHARESUBDIR=`echo $j|cut -d "'" -f 4`
#    INHERITPOLICY=`echo $j|cut -d "'" -f 8`
#    SHARENAME=`echo $j|cut -d "'" -f 16`
#    ACCESS=`echo $j|cut -d "'" -f 24`
#    EXPORT=`echo $j|cut -d "'" -f 28`
#    SUBDIR=`echo $j|cut -d "'" -f 32`
#    if [ $SUBDIR ]; then
#      CREATESUBDIRS=yes
#    else
#      CREATESUBDIRS=no
#    fi
#    JUNCTION_PATH=`echo $j|cut -d "'" -f 40`
#    COREFILER=`echo $j|cut -d "'" -f 44`
#    if [ $INHERITPOLICY == "no" ]; then
#      POLICY=`echo $j|cut -d "'" -f 36`
#      echo "averecmd vserver.addJunction $i $JUNCTION_PATH $COREFILER $EXPORT \"{'sharesubdir': '$SHARESUBDIR', 'inheritPolicy': '$INHERITPOLICY', 'sharename': '$SHARENAME', 'access': '$ACCESS', 'subdir': '$SUBDIR', 'createSubdirs':'$CREATESUBDIRS', 'policy':'$POLICY'}\"" >> $OUTPUT
#    else
#      echo "averecmd vserver.addJunction $i $JUNCTION_PATH $COREFILER $EXPORT \"{'sharesubdir': '$SHARESUBDIR', 'inheritPolicy': '$INHERITPOLICY', 'sharename': '$SHARENAME', 'access': '$ACCESS', 'subdir': '$SUBDIR', 'createSubdirs':'$CREATESUBDIRS'}\"" >> $OUTPUT
#    fi
#  done
#done

IFS=$'\n';for i in `averecmd vserver.list`;do \
  for j in `averecmd vserver.listJunctions $i`;do \
    SHARESUBDIR=`echo $j|tr ',' '\n'|egrep "\<sharesubdir\>"|awk -F"'" '{print $4}'`
    INHERITPOLICY=`echo $j|tr ',' '\n'|egrep "\<inheritPolicy\>"|awk -F"'" '{print $4}'`
    SHARENAME=`echo $j|tr ',' '\n'|egrep "\<sharename\>"|awk -F"'" '{print $4}'`
    ACCESS=`echo $j|tr ',' '\n'|egrep "\<access\>"|awk -F"'" '{print $4}'`
    EXPORT=`echo $j|tr ',' '\n'|egrep "\<export\>"|awk -F"'" '{print $4}'`
    SUBDIR=`echo $j|tr ',' '\n'|egrep "\<subdir\>"|awk -F"'" '{print $4}'`
    if [ $SUBDIR ]; then
      CREATESUBDIRS=yes
    else
      CREATESUBDIRS=no
    fi
    JUNCTION_PATH=`echo $j|tr ',' '\n'|egrep "\<path\>"|awk -F"'" '{print $4}'`
    COREFILER=`echo $j|tr ',' '\n'|egrep "\<corefiler\>"|awk -F"'" '{print $4}'`
    if [ $INHERITPOLICY == "no" ]; then
      POLICY=`echo $j|tr ',' '\n'|egrep "\<policy\>"|awk -F"'" '{print $4}'`
      echo "averecmd vserver.addJunction $i $JUNCTION_PATH $COREFILER $EXPORT \"{'sharesubdir': '$SHARESUBDIR', 'inheritPolicy': '$INHERITPOLICY', 'sharename': '$SHARENAME', 'access': '$ACCESS', 'subdir': '$SUBDIR', 'createSubdirs':'$CREATESUBDIRS', 'policy':'$POLICY'}\"" >> $OUTPUT
    else
      echo "averecmd vserver.addJunction $i $JUNCTION_PATH $COREFILER $EXPORT \"{'sharesubdir': '$SHARESUBDIR', 'inheritPolicy': '$INHERITPOLICY', 'sharename': '$SHARENAME', 'access': '$ACCESS', 'subdir': '$SUBDIR', 'createSubdirs':'$CREATESUBDIRS'}\"" >> $OUTPUT
    fi
  done
done

echo |tee -a $OUTPUT
echo "#Directory Services and Kerberos" |tee -a $OUTPUT
A_DIRSERVICES=()
N=0;IFS=$'\n';for i in `averecmd dirServices.get default`;do \
  A_DIRSERVICES[$N]=`echo $i\|`
  let "N = $N + 1"
done

M=0;
MAX=`expr ${#A_DIRSERVICES[@]} - 1`
for i in ${A_DIRSERVICES[@]}; do \
if [[ $i != "rev"* ]] && [[ $i != "id"* ]]; then \
  SETTING=`echo $i|awk '{print $1}'`;
  VALUE=`echo $i|cut -d "'" -f 2`;
  if [ $M = "0" ]; then \
    echo "averecmd dirServices.modify 'default' \"{'$SETTING'":"'$VALUE'","" >> $OUTPUT;
  elif [ $VALUE ]; then \
    echo "'$SETTING'":"'$VALUE'","" >> $OUTPUT;
  fi
fi
let "M=$M+1"
done
echo "}\"" >> $OUTPUT

A_DIRSERVICES=()
N=0;IFS=$'\n';for i in `averecmd dirServices.get login`;do \
  A_DIRSERVICES[$N]=`echo $i\|`
  let "N = $N + 1"
done

M=0;
MAX=`expr ${#A_DIRSERVICES[@]} - 1`
for i in ${A_DIRSERVICES[@]}; do \
if [[ $i != "rev"* ]] && [[ $i != "id"* ]]; then \
  SETTING=`echo $i|awk '{print $1}'`;
  VALUE=`echo $i|cut -d "'" -f 2`;
  if [ $M = "0" ]; then \
    echo "averecmd dirServices.modify 'login' \"{'$SETTING'":"'$VALUE'","" >> $OUTPUT;
  elif [ $VALUE ]; then \
    echo "'$SETTING'":"'$VALUE'","" >> $OUTPUT;
  fi
fi
let "M=$M+1"
done
echo "}\"" >> $OUTPUT

echo |tee -a $OUTPUT
for i in `averecmd vserver.list`; do \
  if [[ `averecmd cifs.getConfig $i|grep enabled|cut -d "'" -f 2` = "True" ]]; then \
    ENABLE_CIFS=True
    CIFSSERVERNAME=`averecmd cifs.getConfig $i|grep CIFSServerName|cut -d "'" -f 2`;
    echo "#CIFS for vServer \"$i\"" |tee -a $OUTPUT_DIR/1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh
    chmod 700 $OUTPUT_DIR/1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh
    CLIENT_NTLMSSP_DISABLE=`averecmd cifs.getOptions $i|grep client_ntlmssp_disable|cut -d "'" -f 2`;
    GUEST_ACCOUNT=`averecmd cifs.getOptions $i|grep guest_account|cut -d "'" -f 2`;
    SMB2=`averecmd cifs.getOptions $i|grep smb2|cut -d "'" -f 2`;
    SMB1=`averecmd cifs.getOptions $i|grep smb1|cut -d "'" -f 2`;
    NATIVE_IDENTITY=`averecmd cifs.getOptions $i|grep native_identity|cut -d "'" -f 2`;
    READ_ONLY_OPTIMIZED=`averecmd cifs.getOptions $i|grep read_only_optimized|cut -d "'" -f 2`;
    SERVER_SIGNING=`averecmd cifs.getOptions $i|grep server_signing|cut -d "'" -f 2`;
    echo "echo """ >> $OUTPUT
    echo "echo "We have identified vserver \\\"$i\\\" has CIFS enabled however the Avere does not retain passwords so we are unable to automatically enable CIFS."" >> $OUTPUT
    echo "echo "You will need to join the vserver \\\"$i\\\" to the domain with the following information and then run the \\\"1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh\\\" script."" >> $OUTPUT
    echo "echo """ >> $OUTPUT
    echo "echo "vserver is: "$i" >> $OUTPUT
    echo "echo "Enable CIFS: "$ENABLE_CIFS" >> $OUTPUT
    if [ $SMB1 ]; then \
      echo "echo "Enable SMB1: "$SMB1" >> $OUTPUT
    fi
    echo "echo "Enable SMB2: "$SMB2" >> $OUTPUT
    echo "echo "CIFS Server Name: "$CIFSSERVERNAME" >> $OUTPUT
    echo "echo "Enable Native Identity: "$NATIVE_IDENTITY" >> $OUTPUT
    echo "echo "Enable Read-only Optimized: "$READ_ONLY_OPTIMIZED" >> $OUTPUT
    echo "echo "Client NTLMSSP Disable: "$CLIENT_NTLMSSP_DISABLE" >> $OUTPUT
    if [ $SERVER_SIGNING ]; then \
      echo "echo "Server Signing "$SERVER_SIGNING" >> $OUTPUT
    fi
    echo "echo "Guest Account: "$GUEST_ACCOUNT" >> $OUTPUT
    echo "echo """ >> $OUTPUT
    echo "read -p \"Press enter AFTER \\\"$i\\\" has joined the domain\"" >> $OUTPUT
  else
    echo "echo """ >> $OUTPUT
    echo "#CIFS is not enabled for vServer \"$i\"" |tee -a $OUTPUT
  fi
done

echo |tee -a $OUTPUT

# This method of getting CIFS shares cut time down
IFS=$'\n';for i in `averecmd vserver.list`; do \
  for SHARENAME in `averecmd --pretty cifs.listShares $i|grep shareName|cut -d "'" -f 4`; do \
    IFS=$' '
    CIFS_SHARE=`averecmd cifs.getShare $i $SHARENAME`
    ACCESSCONTROL=`echo $CIFS_SHARE|grep accessControl|cut -d "'" -f 2`
    HOMEDIR=`echo $CIFS_SHARE|grep homeDir|cut -d "'" -f 2`
    EXPORT=`echo $CIFS_SHARE|grep export|cut -d "'" -f 2`
    SUFFIX=`echo $CIFS_SHARE|grep suffix|cut -d "'" -f 2`
    NAMESPACE=`averecmd --pretty vserver.get $i|grep type|cut -d "'" -f 4`
    if [ $HOMEDIR == "yes" ]; then \
      HOMEDIR=True
    fi
    if [ $NAMESPACE == "gns" ]; then \
      ACCESSCONTROL="''"
    fi
    if [ -z "$SUFFIX" ]; then \
      SUFFIX="''"
    fi
    FORCE_USER=`echo $CIFS_SHARE|grep "force user"|cut -d "'" -f 2`
    LEVEL2_OPLOCKS=`echo $CIFS_SHARE|grep "level2 oplocks"|cut -d "'" -f 2`
    STRICT_LOCKING=`echo $CIFS_SHARE|grep "strict locking"|cut -d "'" -f 2`
    READ_ONLY_OPTIMIZED=`echo $CIFS_SHARE|grep "read only optimized"|cut -d "'" -f 2`
    INHERIT_PERMISSIONS=`echo $CIFS_SHARE|grep "inherit permissions"|cut -d "'" -f 2`
    CREATE_MASK=`echo $CIFS_SHARE|grep "create mask"|cut -d "'" -f 2`
    BROWSEABLE=`echo $CIFS_SHARE|grep "browseable"|cut -d "'" -f 2`
    SECURITY_MASK=`echo $CIFS_SHARE|grep "security mask"|grep -v directory|cut -d "'" -f 2`
    DIRECTORY_MASK=`echo $CIFS_SHARE|grep "directory mask"|cut -d "'" -f 2`
    GUEST_OK=`echo $CIFS_SHARE|grep "guest ok"|cut -d "'" -f 2`
    FORCE_CREATE_MODE=`echo $CIFS_SHARE|grep "force create mode"|cut -d "'" -f 2`
    HIDE_UNREADABLE=`echo $CIFS_SHARE|grep "hide unreadable"|cut -d "'" -f 2`
    FORCE_DIRECTORY_MODE=`echo $CIFS_SHARE|grep "force directory mode"|cut -d "'" -f 2`
    READ_ONLY=`echo $CIFS_SHARE|grep "read only"|grep -v optimized|cut -d "'" -f 2`
    DIRECTORY_SECURITY_MASK=`echo $CIFS_SHARE|grep "\<directory security mask\>"|cut -d "'" -f 2`
    FORCE_DIRECTORY_SECURITY_MODE=`echo $CIFS_SHARE|grep "force directory security mode"|cut -d "'" -f 2`
    OPLOCKS=`echo $CIFS_SHARE|grep oplocks|grep -v level2|cut -d "'" -f 2`
    FORCE_GROUP=`echo $CIFS_SHARE|grep "force group"|cut -d "'" -f 2`
    FORCE_SECURITY_MODE=`echo $CIFS_SHARE|grep "force security mode"|cut -d "'" -f 2`
    echo "averecmd cifs.addShare $i $SHARENAME $EXPORT $SUFFIX $ACCESSCONTROL $HOMEDIR \"{'browseable':'$BROWSEABLE', 'inherit permissions':'$INHERIT_PERMISSIONS', 'read only':'$READ_ONLY', 'hide unreadable':'$HIDE_UNREADABLE', 'strict locking':'$STRICT_LOCKING', 'oplocks':'$OPLOCKS', 'level2 oplocks':'$LEVEL2_OPLOCKS', 'read only optimized':'$READ_ONLY_OPTIMIZED', 'guest ok':'$GUEST_OK', 'create mask':'$CREATE_MASK', 'security mask':'$SECURITY_MASK', 'directory mask':'$DIRECTORY_MASK', 'directory security mask':'$DIRECTORY_SECURITY_MASK', 'force create mode':'$FORCE_CREATE_MODE', 'force security mode':'$FORCE_SECURITY_MODE', 'force directory mode':'$FORCE_DIRECTORY_MODE', 'force directory security mode':'$FORCE_DIRECTORY_SECURITY_MODE', 'force user':'$FORCE_USER', 'force group':'$FORCE_GROUP'}\"" >> $OUTPUT_DIR/1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh
  done
done
echo "sleep 5" >> $OUTPUT_DIR/1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh

if [ $ENABLE_CIFS == "True" ]; then
  echo "#CIFS shares" |tee -a $OUTPUT_DIR/1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh
  IFS=$'\n';for i in `averecmd vserver.list`; do \
    for j in `averecmd --pretty cifs.listShares $i|grep shareName|cut -d "'" -f 4`; do \
      HOMEDIR=`averecmd --pretty cifs.getShare $i $j|grep homeDir|cut -d "'" -f 4`
      if [ $HOMEDIR == "no" ]; then
        echo "averecmd cifs.removeShareAce $i $j \"{'id':'Everyone', 'type':'ALLOW', 'perm':'FULL'}\"" >> $OUTPUT_DIR/1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh
        for SHARE in `averecmd cifs.getShareAcl $i $j`; do \
          TYPE=`echo $SHARE|cut -d "'" -f 4`
          ID=`echo $SHARE|cut -d "'" -f 8`
          PERM=`echo $SHARE|cut -d "'" -f 12`
          echo "averecmd cifs.addShareAce $i $j \"{'id':'$ID', 'type':'$TYPE', 'perm':'$PERM'}\"" >> $OUTPUT_DIR/1.5-run_on_new_cluster_before_custom_settings_restore_cifs.sh
        done
      fi
    done
  done
fi

echo |tee -a $OUTPUT
echo "#Vserver Details" |tee -a $OUTPUT
for i in `averecmd vserver.list`; do \
  HOTCLIENTENABLED=`averecmd --pretty vserver.get $i|grep hotClientEnabled|cut -d "'" -f 4`
  HOTCLIENTPERIOD=`averecmd --pretty vserver.get $i|grep hotClientPeriod|cut -d "'" -f 4`
  HOTCLIENTLIMIT=`averecmd --pretty vserver.get $i|grep hotClientLimit|cut -d "'" -f 4`
  CLIENTSUSPENDSTATUS=`averecmd --pretty vserver.get $i|grep clientSuspendStatus|cut -d "'" -f 4`
  if [ $HOTCLIENTENABLED == yes ]; then \
    echo "averecmd stats.enableHotCollection $i $HOTCLIENTLIMIT $HOTCLIENTPERIOD" >> $OUTPUT
  else
    echo "#Nothing to do for $i" >> $OUTPUT
  fi
done

echo |tee -a $OUTPUT
echo "#Export Policies" |tee -a $OUTPUT
for i in `averecmd vserver.list`; do \
  for j in `averecmd nfs.listPolicies $i`; do \
    if [ $j != "default" ]; then \
      echo "averecmd nfs.addPolicy $i $j" >> $OUTPUT
    fi
  done
done

echo |tee -a $OUTPUT
echo "#Export Rules" |tee -a $OUTPUT
IFS=$'\n';for VSERVER in `averecmd vserver.list`; do \
  for POLICY in `averecmd nfs.listPolicies $VSERVER`; do \
    for RULE in `averecmd nfs.listRules $VSERVER $POLICY`; do \
      if [ `echo $RULE|grep netgroup` ]; then
        echo "" >> $OUTPUT
        echo "#We can't restore netgroup policies at this time so please note the following..." >> $OUTPUT
        echo "#  "`echo $RULE|grep netgroup` >> $OUTPUT
        echo "" >> $OUTPUT
      else
        if [ `echo $RULE |grep -v netgroup|grep '*'` ]; then \
          FILTER=0.0.0.0
        else
          FILTER=`echo $RULE|tr ',' '\n'|grep filter|cut -d "'" -f 4`
        fi
        if [[ $FILTER = *"/"* ]]; then \
          NETMASK=/`echo $FILTER|cut -d '/' -f 2`
          FILTER=`echo $FILTER|cut -d '/' -f 1`
        fi
        ACCESS=`echo $RULE|tr ',' '\n'|grep access|cut -d "'" -f 4`
        ANONUID=`echo $RULE|tr ',' '\n'|grep anonuid|cut -d "'" -f 4`
        if [ -z $ANONUID ]; then \
          ANONUID=0;
        fi
        PERMISSION=`echo $RULE|tr ',' '\n'|grep access|cut -d "'" -f 4`
        AUTHKRB=`echo $RULE|tr ',' '\n'|grep authKrb|cut -d "'" -f 4`
        AUTHSYS=`echo $RULE|tr ',' '\n'|grep authSys|cut -d "'" -f 4`
        SCOPE=`echo $RULE|tr ',' '\n'|grep scope|cut -d "'" -f 4`
        SQUASH=`echo $RULE|tr ',' '\n'|grep squash|cut -d "'" -f 4`
        SUBDIR=`echo $RULE|tr ',' '\n'|grep subdir|cut -d "'" -f 4`
        SUID=`echo $RULE|tr ',' '\n'|grep suid|cut -d "'" -f 4`
        if [[ $FILTER == "0.0.0.0" ]]; then \
          FILTER="*"
        fi
        if [[ $POLICY == "default" && $SCOPE == "default" && $FILTER == "*" ]]; then \
          echo "NEW_ID=\`averecmd --pretty nfs.listRules $VSERVER default 0.0.0.0 |grep \"\\<id\\>\"|cut -d \"'\" -f 4\`" >> $OUTPUT
          echo "averecmd nfs.modifyRule \$NEW_ID \"{'scope':'$SCOPE' , 'access':'$PERMISSION' , 'anonuid':'$ANONUID'}\"" >> $OUTPUT
        elif [ $SCOPE = "network" ]; then \
          echo "averecmd nfs.addRule '$VSERVER' '$POLICY' '$SCOPE' '$FILTER$NETMASK' '$PERMISSION' '$SQUASH' '$ANONUID' '$SUID' '$SUBDIR' \"{'authKrb':'$AUTHKRB', 'authSys':'$AUTHSYS'}\"" >> $OUTPUT
        else
          echo "averecmd nfs.addRule '$VSERVER' '$POLICY' '$SCOPE' '$FILTER' '$PERMISSION' '$SQUASH' '$ANONUID' '$SUID' '$SUBDIR' \"{'authKrb':'$AUTHKRB', 'authSys':'$AUTHSYS'}\"" >> $OUTPUT
        fi
      fi
    done
  done
done

echo |tee -a $OUTPUT
echo "#Cloud Encryption Settings" |tee -a $OUTPUT
echo "#Nothing to do here since we already added the encryption type when creating the core filer" |tee -a $OUTPUT

echo |tee -a $OUTPUT
echo "#General Setup, Administrative Network, Proxy Configuration, FXT Nodes" |tee -a $OUTPUT

#Unable to set the following options: advancedNetworking, MTU, ha, default router, dirmgrDistributionState, clusterIPNumPerNode, proxyuser, proxyurl
#Also excluding "mgmtIP" and "ClusterIPs" because those values are part of the initial setup process

# This will empty the array
A_CLUSTER=();
# This will fill up the array with values
#N=0;IFS=$'\n';for i in `averecmd cluster.get|egrep -v 'alternateImage|activeImage|dirmgrDistributionState|avereDataParameters|clusterIPs|mgmtIP'`;do   A_CLUSTER[$N]=`echo $i`;   let "N = $N + 1"; done
N=0;IFS=$'\n';for i in `averecmd cluster.get|egrep -v 'alternateImage|activeImage|dirmgrDistributionState|avereDataParameters|clusterIPs|mgmtIP|advancedNetworking|default_mtu|proxyurl|default_router|proxyuser|netmask|\<ha\>|clusterIPNumPerNode'`;do   A_CLUSTER[$N]=`echo $i`;   let "N = $N + 1"; done
N=0; MAX=`expr ${#A_CLUSTER[@]} - 1`;
for i in ${A_CLUSTER[@]}; do \
if [[ $i != "rev"* ]] && [[ $i != "id"* ]]; then \
  SETTING=`echo $i|awk '{print $1}'`;
  VALUE=`echo $i|awk -F \= '{print $2}'`;

  if [[ $SETTING = "DNSdomain" ]] && [[ $VALUE = *"undefined"* ]]; then \
    VALUE="''"
  fi

  if [ $N = "0" ]; then \
    echo "averecmd cluster.modify \"{'$SETTING'":"$VALUE",""  >> $OUTPUT;
  elif [ $N = "$MAX" ]; then \
    echo "'$SETTING'":"$VALUE}\""  >> $OUTPUT;
  else
    echo "'$SETTING'":"$VALUE","" >> $OUTPUT;
  fi
fi
let "N=$N+1"
done

echo  |tee -a $OUTPUT
echo "#Cluster networks are addressed in the cluster section" |tee -a $OUTPUT

echo |tee -a $OUTPUT
echo "#Proxy configuration is addressed in the cluster section" |tee -a $OUTPUT

echo |tee -a $OUTPUT
echo "#High Availability" |tee -a $OUTPUT
echo "#We are always enabling cluster HA because we don't support non-HA clusters"
N=0;IFS=$'\n';for i in `averecmd cluster.getHADataParameters`; do \
  A_HADATA[$N]=`echo $i|cut -d "'" -f 2`
  let "N=$N+1"
done
DATAEXPORT=`echo ${A_HADATA[0]}`
COREFILER=`echo ${A_HADATA[1]}`
MASS=`echo ${A_HADATA[2]}`
DATADIR=`echo ${A_HADATA[3]}`
echo "averecmd cluster.enableHA" >> $OUTPUT
if [ $MASS ]; then
  echo "averecmd cluster.setHADataParameters $COREFILER $DATAEXPORT $DATADIR" >> $OUTPUT
fi

echo |tee -a $OUTPUT
echo "#Monitoring" |tee -a $OUTPUT
N=0;IFS=$'\n';for i in `averecmd monitoring.emailSettings`; do \
  A_EMAILMONITORING[$N]=`echo $i|cut -d "'" -f 2-|rev|cut -d "'" -f 2-|rev`
  let "N=$N+1"
done
RECIPIENTS=`echo ${A_EMAILMONITORING[0]}`
ENABLED=`echo ${A_EMAILMONITORING[1]}`
REV=`echo ${A_EMAILMONITORING[2]}`
MORECONTEXT=`echo ${A_EMAILMONITORING[3]}`
MAILFROMADDRESS=`echo ${A_EMAILMONITORING[4]}`
ID=`echo ${A_EMAILMONITORING[5]}`
MAILSERVER=`echo ${A_EMAILMONITORING[6]}`
if [ -z $MAILSERVER ]; then \
  echo "#Monitoring is not configured" >> $OUTPUT
else
  echo "averecmd monitoring.modifyEmailSettings \"{'mailFromAddress':'$MAILFROMADDRESS', 'mailServer':'$MAILSERVER', 'recipients':'$RECIPIENTS'}\"" >> $OUTPUT
fi

echo |tee -a $OUTPUT
echo "#Client Facing Network" |tee -a $OUTPUT
IFS=$'\n';for i in `averecmd vserver.list`;do \
  for j in `averecmd vserver.listClientIPHomes $i`; do \
    NODE=`echo $j|awk -F"'" '{print $4}'`
    IP=`echo $j|awk -F"'" '{print $8}'`
    HOME=`echo $j|awk -F"'" '{print $12}'`
    if [ $HOME != None ]; then \
      echo "averecmd vserver.modifyClientIPHomes $i \"{'$IP':'$HOME'}\"" >> $OUTPUT
    fi
  done
done

echo |tee -a $OUTPUT
echo "#Monitoring - Logs" |tee -a $OUTPUT

if [ $RELEASE -lt "47" ]; then \
#  PRE47
  SYSLOGSERVER=`averecmd monitoring.syslogServer`
  if [ $SYSLOGSERVER ]; then \
    echo "averecmd monitoring.setSyslogServer $SYSLOGSERVER" >> $OUTPUT
  else
    echo "#A syslog server has not been set" >> $OUTPUT
  fi
else
#  POST47
  if [ `averecmd monitoring.getSyslogServer` ]; then \
    REMOVESYSLOGSERVER=`averecmd monitoring.getSyslogSettings|grep remoteSyslogServer|cut -d "'" -f 2`
    FORWARDING_OPTIONS=`averecmd monitoring.getSyslogSettings|grep forwarding_options|cut -d "'" -f 2-8`
    echo "averecmd monitoring.setSyslogServer $REMOVESYSLOGSERVER \"$FORWARDING_OPTIONS\"" >> $OUTPUT
  else
    FORWARDING_OPTIONS=`averecmd monitoring.getSyslogSettings|grep forwarding_options|cut -d "'" -f 2-8`
    echo "averecmd monitoring.setSyslogServer '' \"$FORWARDING_OPTIONS\"" >> $OUTPUT
  fi
fi

echo |tee -a $OUTPUT
echo "#Monitoring - SNMP monitoring" |tee -a $OUTPUT
N=0;IFS=$'\n';for i in `averecmd monitoring.snmpSettings|awk '{print $3}'`;do \
  A_SNMP[$N]=`echo $i`
  let "N=$N+1"
done
if [[ ${A_SNMP[0]} == *"yes"* ]]; then \
  ENABLE=`echo ${A_SNMP[0]}`
  READCOMMUNITYSTRING=`echo ${A_SNMP[1]}`
  TRAPHOST=`echo ${A_SNMP[2]}`
  REV=`echo ${A_SNMP[3]}`
  CONTACT=`echo ${A_SNMP[4]}`
  LOCATION=`echo ${A_SNMP[5]}`
  TRAPPORT=`echo ${A_SNMP[6]}`
  ID=`echo ${A_SNMP[7]}`
  echo "averecmd monitoring.modifySnmpSettings \"{'enable':$ENABLE, 'contact':$CONTACT, 'location':$LOCATION, 'trapHost':$TRAPHOST, 'trapPort':$TRAPPORT, 'readCommunityString':$READCOMMUNITYSTRING}\"" >> $OUTPUT
else
  echo "#SNMP monitoring has not been set" >> $OUTPUT
fi

echo |tee -a $OUTPUT
echo "#Schedules" |tee -a $OUTPUT
IFS=$'\n';for i in `averecmd cluster.listSchedules`; do \
  NAME=`echo $i|cut -d "'" -f 18`
  MINUTES=`echo $i|cut -d "'" -f 10`
  HOURS=`echo $i|cut -d "'" -f 6`
  DAYS=`echo $i|cut -d "'" -f 14`
  echo "averecmd cluster.addSchedule $NAME \"[{'hours':'$HOURS', 'minutes':'$MINUTES', 'days':'$DAYS'}]\"" >> $OUTPUT
done

echo |tee -a $OUTPUT
echo "#IPMI" |tee -a $OUTPUT
echo "#Note: If you see something like \"Caught Exception: Couldn't fetch attribute\" that means IPMI is not configured"
IFS=$'\n';for i in `lsu nodes|grep -v _|cut -d ':' -f 1`; do \
  for j in `lsu $i ipmi`;do \
    if [[ $j != *"Couldn't fetch attribute"* ]]; then \
      MODE=`echo $j|awk '{print $3}'|cut -d '=' -f 2`
      ADDRESS=`echo $j|awk '{print $4}'|cut -d '=' -f 2`
      NETMASK=`echo $j|awk '{print $5}'|cut -d '=' -f 2`
      ROUTER=`echo $j|awk '{print $6}'|cut -d '=' -f 2`
      NAME=`lsu $i name`
      echo "averecmd node.modifyIPMI $NAME $MODE \"{'address':'$ADDRESS', 'netmask':'$NETMASK', 'router':'$ROUTER'}\"" >> $OUTPUT
    else
      echo "IPMI is not configured for node "`lsu $i name` >> $OUTPUT
    fi
  done
done

echo |tee -a $OUTPUT
echo "#Support - Customer Info and Secure Proactive Support (SPS)" |tee -a $OUTPUT
# This will empty the array
A_SUPPORT=();
# This will fill up the array with values
N=0;IFS=$'\n';for i in `averecmd support.get|egrep -v 'remoteCommandEnabledTime|tested|priorRemoteCommandLevel'`;do   A_SUPPORT[$N]=`echo $i`;   let "N = $N + 1"; done

N=0; MAX=`expr ${#A_SUPPORT[@]} - 1`;
for i in ${A_SUPPORT[@]}; do \
if [[ $i != "rev"* ]] && [[ $i != "id"* ]]; then \
  SETTING=`echo $i|awk '{print $1}'`;
  VALUE=`echo $i|awk -F \' '{print $2}'`;
  if [ $N = "0" ]; then \
    echo "averecmd support.modify \"{'$SETTING'":"'$VALUE'","" >> $OUTPUT;
  elif [ $N = "$MAX" ]; then \
    echo "'$SETTING'":"'$VALUE'}\"" >> $OUTPUT;
  else
    echo "'$SETTING'":"'$VALUE'","" >> $OUTPUT;
  fi
fi
let "N=$N+1"
done

# Moving cusom settings section after core filer add to give the core filers more time to be created
#echo |tee -a $OUTPUT
#echo "#Custom settings are addressed with \"2-run_on_new_cluster_mass_mappings.sh\", \"3-run_on_new_cluster_new_custom_settings.sh\", \"4-run_on_new_cluster_new_custom_settings.sh\"" |tee -a $OUTPUT
#
##IFS=$'\n';for i in `name_mapping.py --custom|egrep -v 'Custom Settings:|----------------'`;do \
#IFS=$'\n';for i in `name_mapping.py --custom|egrep -v 'Custom Settings:|----------------|smbacldMassids'`;do \
#  NAME=`echo $i|cut -d ':' -f 1|awk '{print $1}'`
#  CHECKCODE=`averecmd support.checkCode $NAME|grep check|cut -d "'" -f 2`
#  VALUE=`echo $i|awk '{print $2}'`
#  NOTE=`echo $i|cut -d '(' -f 2`;
#  if [[ $NOTE = *"NOT in cluster.settings"* ]]; then \
#    NAME2=`echo $NAME|tr . ' '`
#    echo "dbutil.py set $NAME2 $VALUE -x" >> $OUTPUT_DIR/original_custom_settings.txt
#  else
#    echo "averecmd support.setCustomSetting $NAME $CHECKCODE $VALUE \"($NOTE\"" >> $OUTPUT_DIR/original_custom_settings.txt
#  fi
#done

#vserver mapping
# Convert current mass to new mass
for i in `lsu masses|grep -v _|cut -d ':' -f 1`; do \
  NAME=`lsu $i name|cut -d ' ' -f 2`
  if [[ $NAME != "127.0.0"* ]]; then \
    echo "********$i:$NAME" >> $OUTPUT_DIR/masses.txt
  fi
done

for VSERVER in `lsu vservers|grep -v _|cut -d ':' -f 1`; do \
  VSERVER_NAME=`lsu $VSERVER name|cut -d ' ' -f 2`
  echo "********$VSERVER:$VSERVER_NAME" >> $OUTPUT_DIR/masses.txt
done

# Read core filer name and the new mass number....THESE STEP NEEDS TO BE PERFORMED ON THE NEW CLUSTER AND RUN FROM THE SAME DIR WHERE masses.txt EXISTS.
echo "N=0;while read -r line; do \\" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "ARRAY[\$N]=\`echo \$line|cut -c 9-\`" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "INTERNAL_NAME=\`echo \${ARRAY[\$N]}|cut -d ':' -f 1\`" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "ADMINISTRATIVE_NAME=\`echo \${ARRAY[\$N]}|cut -d ':' -f 2\`" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "if [[ \$INTERNAL_NAME = \"mass\"* ]]; then \\" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "NEW_VALUE=\`averecmd --pretty corefiler.get \$ADMINISTRATIVE_NAME|grep internalName|cut -d \"'\" -f 4\`" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "elif [[ \$INTERNAL_NAME = \"vserver\"* ]]; then \\" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "for i in \`lsu vservers|grep -v _|cut -d ':' -f 1\`; do \\" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "if [[ \`lsu \$i|grep \$ADMINISTRATIVE_NAME\` ]]; then \\" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "NEW_VALUE=\`lsu \$i __name|cut -d ' ' -f 2\`" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "fi" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "done" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "fi" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "let \"N=\$N+1\"" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "echo \$INTERNAL_NAME:\$ADMINISTRATIVE_NAME:\$NEW_VALUE >> mass_mapping.txt" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
echo "done < masses.txt" >> $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh
chmod 700 $OUTPUT_DIR/2-run_on_new_cluster_mass_mappings.sh

echo "while read line; do \\" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  NAME=\`echo \$line|cut -d ' ' -f 3|cut -d '.' -f 1\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  if [[ \$NAME != *\"mass\"* ]] && [[ \$NAME != \"vserver\"* ]]; then \\" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    line2=\`echo \$line\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  fi" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  if [[ \$NAME = \"cloudmass\"* ]]; then \\" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    NAME=\`echo \$NAME| cut -c 6-\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  fi" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  if [ \"\$line\" != \"\$line2\" ]; then \\" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    NEW=\`grep \"\$NAME:\" mass_mapping.txt |cut -d ':' -f 3\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    line2=\`echo \$line|sed \"s/\$NAME/\$NEW/\"\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  fi" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  if [[ \$NAME = \"cpolicyAdmin\"* ]]; then \\" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    tmp=\`echo \$NAME|cut -c 13-\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    NEW=\`grep \"mass\$tmp:\" mass_mapping.txt |cut -d ':' -f 3\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    NEW_ADMIN=cpolicyAdmin\`echo \$NEW|cut -c 5-\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    line2=\`echo \$line|sed \"s/\$NAME/\$NEW_ADMIN/\"\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  fi" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  if [[ \$NAME = \"cpolicyActive\"* ]]; then \\" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    tmp=\`echo \$NAME|cut -c 14-\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    NEW=\`grep \"mass\$tmp:\" mass_mapping.txt |cut -d ':' -f 3\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    NEW_ACTIVE=cpolicyActive\`echo \$NEW|cut -c 5-\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "    line2=\`echo \$line|sed \"s/\$NAME/\$NEW_ACTIVE/\"\`" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  fi" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  echo \$line2 >> 4-run_on_new_cluster_new_custom_settings.sh" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "  line2=''" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "done < original_custom_settings.txt" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
echo "chmod 700 4-run_on_new_cluster_new_custom_settings.sh" >> $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh
chmod 700 $OUTPUT_DIR/3-run_on_new_cluster_new_custom_settings.sh

echo |tee -a $OUTPUT
echo "#Certificates" |tee -a $OUTPUT
echo "#Nothing to do here since we don't store the certificates" |tee -a $OUTPUT

echo |tee -a $OUTPUT
echo "#KMIP Servers" |tee -a $OUTPUT
echo "#Nothing to do here due to password requirments" |tee -a $OUTPUT

echo |tee -a $OUTPUT
echo "#Users" |tee -a $OUTPUT
echo "#Note: recreated users will all have the their password set to null so please log into the GUI and set them appropriately" |tee -a $OUTPUT
N=0;IFS=$'\n';for i in `averecmd admin.listUsers`; do \
  NAME=`echo $i|cut -d "'" -f 4`
  PERMISSION=`echo $i|cut -d "'" -f 8`
  if [ $NAME != "admin" ]; then
#    echo "averecmd admin.addUser $NAME $PERMISSION tiered2010" >> $OUTPUT
    echo "averecmd admin.addUser $NAME $PERMISSION ''" >> $OUTPUT
  fi
done

echo |tee -a $OUTPUT
echo "#Login Services" |tee -a $OUTPUT
echo "#Nothing to do here since it was addressed when completing the \"Directory Services\" section" |tee -a $OUTPUT
echo |tee -a $OUTPUT

echo "#Hidden Alerts" |tee -a $OUTPUT
echo "#Nothing to do here since there are no xmlrpc commands to find this" |tee -a $OUTPUT
echo |tee -a $OUTPUT

echo "####Administration section...DONE####" |tee -a $OUTPUT
/support/lib/python/support/data_upload.py -C $OUTPUT_DIR


#!/bin/bash -x
set -x

function yum_install() {
    retries=$1; wait_sleep=$2; timeout=$3; shift && shift && shift
    for i in $(seq 1 $retries); do
        yum install -y ${@}
        [ $? -eq 0  ] && break || \
        if [ $i -eq $retries ]; then
            echo "failed"
            touch /opt/installnfs.failed
            exit 1
        else
            sleep $wait_sleep
        fi
    done
    echo "completed"
    echo Executed yum install -y \"$@\" $i times;

    yum install -y nfs-utils
}

function config_linux() {
    # try for 20 minutes
    yum_install 120 10 180 nfs-utils
}

function update_hostname() {
    OCTET3=$((`hostname -i | sed -e 's/^.*\.\([0-9]*\)\.[0-9]*/\1/'  | sed 's/[^0-9]*//g'`))
    OCTET4=$((`hostname -i | sed -e 's/^.*\.\([0-9]*\)/\1/'  | sed 's/[^0-9]*//g'`))

    hostnamectl set-hostname "vm${OCTET3}_${OCTET4}"
}

function update_search_domain() {
    if [[ ! -z "$SEARCH_DOMAIN" ]]; then
        NETWORK_FILE=/etc/sysconfig/network-scripts/ifcfg-eth0
        if grep --quiet "DOMAIN=" $NETWORK_FILE; then
            sed -i 's/^#\s*DOMAIN=/DOMAIN=/g' $NETWORK_FILE
            sed -i "s/^DOMAIN=.*$/DOMAIN=\"${SEARCH_DOMAIN}\"/g"  $NETWORK_FILE
        else
            echo "DOMAIN=\"${SEARCH_DOMAIN}\"" >> $NETWORK_FILE
        fi
        # restart network to take effect
        systemctl restart network
    fi
}

function main() {
    echo "config Linux"
    config_linux

    echo "update hostname"
    update_hostname

    echo "update search domain"
    update_search_domain

    echo "installation complete"

    touch /opt/installnfs.complete
}

main


function SetServiceAccount {
  accountName=$1
  accountPassword=$2
  if ! id -u $accountName &> /dev/null; then
    useradd --system --password $accountPassword $accountName
  fi
}

function GetEncodedValue {
  echo $1 | base64 -d | jq -r $2
}

function SetFileSystemMount {
  fileSystemMount="$1"
  mountDirectory=$(cut -d " " -f 2 <<< "$fileSystemMount")
  if [ $(grep -c $mountDirectory /etc/fstab) ]; then
    mkdir -p $mountDirectory
    echo "$fileSystemMount" >> /etc/fstab
  fi
}

function EnableClientApp {
  renderManager="$1"
  serviceAccountName=$2
  serviceAccountPassword=$3
  curl http://data.artist.studio:14000/dist/v1/install | sh
  if [[ $renderManager == *RoyalRender* ]]; then
    installType="royal-render-client"
    rrWorkstation_installer -plugins -service -rrUser $serviceAccountName -rrUserPW $serviceAccountPassword -fwOut 2>&1 | tee $installType-service.log
    sed -i "s|BLENDER_PATH|local/blender|" /RoyalRender/render_apps/_install_paths/Blender.cfg
  fi
}


#!/bin/bash -ex

binPaths=""
binDirectory="/usr/local/bin"
cd $binDirectory

aaaProfile="/etc/profile.d/aaa.sh"
touch $aaaProfile

echo "Customize (Start): Image Build Parameters"
dnf -y install jq
buildConfig=$(echo $buildConfigEncoded | base64 -d)
machineType=$(echo $buildConfig | jq -r .machineType)
gpuProvider=$(echo $buildConfig | jq -r .gpuProvider)
renderManager=$(echo $buildConfig | jq -r .renderManager)
renderEngines=$(echo $buildConfig | jq -c .renderEngines)
binStorageHost=$(echo $buildConfig | jq -r .binStorageHost)
binStorageAuth=$(echo $buildConfig | jq -r .binStorageAuth)
servicePassword=$(echo $buildConfig | jq -r .servicePassword)
echo "Machine Type: $machineType"
echo "GPU Provider: $gpuProvider"
echo "Render Manager: $renderManager"
echo "Render Engines: $renderEngines"
echo "Customize (End): Image Build Parameters"

echo "Customize (Start): Image Build Platform"
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
dnf -y install gcc gcc-c++ perl elfutils-libelf-devel # openssl-devel bison flex
installFile="kernel-devel-4.18.0-372.16.1.el8_6.0.1.x86_64.rpm"
downloadUrl="$binStorageHost/Linux/Rocky/$installFile$binStorageAuth"
curl -o $installFile -L $downloadUrl
rpm -i $installFile
dnf -y install python3-devel bc git lsof unzip
if [ $machineType == Workstation ]; then
  dnf -y group install Workstation
  dnf -y module install nodejs:18
fi

versionInfo="3.26.4"
installType="cmake"
installFile="cmake-$versionInfo-linux-x86_64.sh"
downloadUrl="https://github.com/Kitware/CMake/releases/download/v$versionInfo/cmake-$versionInfo-linux-x86_64.sh"
curl -o $installFile -L $downloadUrl
chmod +x $installFile
mkdir -p $installType
./$installFile --skip-license --prefix=$installType 2>&1 | tee $installType.log
binPathCMake="$binDirectory/$installType/bin"
binPaths="$binPaths:$binPathCMake"
echo "Customize (End): Image Build Platform"

if [ "$gpuProvider" == NVIDIA ]; then
  dnf -y install epel-release
  dnf -y install dkms

  echo "Customize (Start): NVIDIA GPU (GRID)"
  installType="nvidia-gpu-grid"
  installFile="$installType.run"
  downloadUrl="https://go.microsoft.com/fwlink/?linkid=874272"
  curl -o $installFile -L $downloadUrl
  chmod +x $installFile
  ./$installFile --silent --dkms 2>&1 | tee $installType.log
  echo "Customize (End): NVIDIA GPU (GRID)"

  echo "Customize (Start): NVIDIA GPU (CUDA)"
  installType="nvidia-cuda"
  dnf config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel8/x86_64/cuda-rhel8.repo
  dnf -y module install nvidia-driver:latest-dkms 2>&1 | tee $installType-dkms.log
  dnf -y install cuda 2>&1 | tee $installType.log
  echo "Customize (End): NVIDIA GPU (CUDA)"

  echo "Customize (Start): NVIDIA OptiX"
  dnf -y install mesa-libGL
  dnf -y install mesa-libGL-devel
  dnf -y install libXrandr-devel
  dnf -y install libXinerama-devel
  dnf -y install libXcursor-devel
  versionInfo="7.7.0"
  installType="nvidia-optix"
  installFile="NVIDIA-OptiX-SDK-$versionInfo-linux64-x86_64.sh"
  downloadUrl="$binStorageHost/NVIDIA/OptiX/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  chmod +x $installFile
  mkdir -p $installType
  ./$installFile --skip-license --prefix=$installType 2>&1 | tee $installType.log
  buildDirectory="$binDirectory/$installType/build"
  mkdir -p $buildDirectory
  $binPathCMake/cmake -B $buildDirectory -S $binDirectory/$installType/SDK 2>&1 | tee $installType-cmake.log
  make -C $buildDirectory 2>&1 | tee $installType-make.log
  binPaths="$binPaths:$buildDirectory/bin"
  echo "Customize (End): NVIDIA OptiX"
fi

if [[ $renderEngines == *Maya* ]]; then
  echo "Customize (Start): Maya"
  dnf -y install mesa-libGL
  dnf -y install mesa-libGLU
  dnf -y install alsa-lib
  dnf -y install libXxf86vm
  dnf -y install libXmu
  dnf -y install libXpm
  dnf -y install libnsl
  dnf -y install gtk3
  versionInfo="2024_0_1"
  installType="autodesk-maya"
  installFile="Autodesk_Maya_${versionInfo}_Update_Linux_64bit.tgz"
  downloadUrl="$binStorageHost/Maya/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  mkdir -p $installType
  tar -xzf $installFile -C $installType
  ./$installType/Setup --silent 2>&1 | tee $installType.log
  binPaths="$binPaths:/usr/autodesk/maya/bin"
  echo "Customize (End): Maya"
fi

if [[ $renderEngines == *PBRT* ]]; then
  echo "Customize (Start): PBRT v3"
  versionInfo="v3"
  installType="pbrt-$versionInfo"
  installPath="/usr/local/pbrt"
  installPathV3="$installPath/$versionInfo"
  git clone --recursive https://github.com/mmp/$installType.git 2>&1 | tee $installType-git.log
  mkdir -p $installPathV3
  $binPathCMake/cmake -B $installPathV3 -S $binDirectory/$installType 2>&1 | tee $installType-cmake.log
  make -C $installPathV3 2>&1 | tee $installType-make.log
  ln -s $installPathV3/pbrt $installPath/pbrt3
  echo "Customize (End): PBRT v3"

  echo "Customize (Start): PBRT v4"
  dnf -y install mesa-libGL-devel
  dnf -y install libXrandr-devel
  dnf -y install libXinerama-devel
  dnf -y install libXcursor-devel
  dnf -y install libXi-devel
  versionInfo="v4"
  installType="pbrt-$versionInfo"
  installPathV4="$installPath/$versionInfo"
  git clone --recursive https://github.com/mmp/$installType.git 2>&1 | tee $installType-git.log
  mkdir -p $installPathV4
  $binPathCMake/cmake -B $installPathV4 -S $binDirectory/$installType 2>&1 | tee $installType-cmake.log
  make -C $installPathV4 2>&1 | tee $installType-make.log
  ln -s $installPathV4/pbrt $installPath/pbrt4
  echo "Customize (End): PBRT v4"

  binPaths="$binPaths:$installPath"
fi

if [[ $renderEngines == *Houdini* ]]; then
  echo "Customize (Start): Houdini"
  dnf -y install mesa-libGL
  dnf -y install libXcomposite
  dnf -y install libXdamage
  dnf -y install libXrandr
  dnf -y install libXcursor
  dnf -y install libXi
  dnf -y install libXtst
  dnf -y install libXScrnSaver
  dnf -y install alsa-lib
  dnf -y install libnsl
  dnf -y install avahi
  versionInfo="19.5.569"
  versionEULA="2021-10-13"
  installType="houdini"
  installFile="$installType-$versionInfo-linux_x86_64_gcc9.3.tar.gz"
  downloadUrl="$binStorageHost/Houdini/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  tar -xzf $installFile
  [[ $machineType == Workstation ]] && desktopMenus=--install-menus || desktopMenus=--no-install-menus
  [[ $renderEngines == *Maya* ]] && mayaPlugIn=--install-engine-maya || mayaPlugIn=--no-install-engine-maya
  [[ $renderEngines == *Unreal* ]] && unrealPlugIn=--install-engine-unreal || unrealPlugIn=--no-install-engine-unreal
  ./houdini*/houdini.install --auto-install --make-dir --no-install-license --accept-EULA $versionEULA $desktopMenus $mayaPlugIn $unrealPlugIn 2>&1 | tee $installType.log
  binPaths="$binPaths:/opt/hfs$versionInfo/bin"
  echo "Customize (End): Houdini"
fi

if [[ $renderEngines == *Blender* ]]; then
  echo "Customize (Start): Blender"
  dnf -y install mesa-libGL
  dnf -y install libXxf86vm
  dnf -y install libXfixes
  dnf -y install libXi
  dnf -y install libSM
  versionInfo="3.6.0"
  versionType="linux-x64"
  installType="blender"
  installPath="/usr/local/$installType"
  installFile="$installType-$versionInfo-$versionType.tar.xz"
  downloadUrl="$binStorageHost/Blender/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  tar -xJf $installFile
  mkdir -p $installPath
  mv $installType-$versionInfo-$versionType/* $installPath
  binPaths="$binPaths:$installPath"
  echo "Customize (End): Blender"
fi

if [[ $renderEngines == *Unreal* ]] || [[ $renderEngines == *Unreal+PixelStream* ]]; then
  echo "Customize (Start): Unreal Engine Setup"
  dnf -y install libicu
  versionInfo="5.2.0"
  installType="unreal-engine"
  installPath="/usr/local/unreal"
  installFile="UnrealEngine-$versionInfo-release.tar.gz"
  downloadUrl="$binStorageHost/Unreal/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  tar -xzf $installFile
  mkdir -p $installPath
  mv UnrealEngine-$versionInfo-release/* $installPath
  $installPath/Setup.sh 2>&1 | tee $installType-setup.log
  echo "Customize (End): Unreal Engine Setup"

  echo "Customize (Start): Unreal Project Files Generate"
  $installPath/GenerateProjectFiles.sh 2>&1 | tee unreal-project-files-generate.log
  echo "Customize (End): Unreal Project Files Generate"

  echo "Customize (Start): Unreal Engine Build"
  make -C $installPath 2>&1 | tee $installType-make.log
  echo "Customize (End): Unreal Engine Build"

  if [[ $renderEngines == *Unreal+PixelStream* ]]; then
    echo "Customize (Start): Unreal Pixel Streaming"
    dnf -y install coturn
    unrealVersion=$versionInfo
    versionInfo="UE5.2-0.6.2"
    installType="unreal-stream"
    installFile="PixelStreamingInfrastructure-$versionInfo.tar.gz"
    downloadUrl="$binStorageHost/Unreal/$unrealVersion/$installFile$binStorageAuth"
    curl -o $installFile -L $downloadUrl
    tar -xzf $installFile
    installFile="PixelStreamingInfrastructure-$versionInfo/SignallingWebServer/platform_scripts/bash/setup.sh"
    chmod +x $installFile
    ./$installFile 2>&1 | tee $installType-signalling.log
    installFile="PixelStreamingInfrastructure-$versionInfo/Matchmaker/platform_scripts/bash/setup.sh"
    chmod +x $installFile
    ./$installFile 2>&1 | tee $installType-matchmaker.log
    installFile="PixelStreamingInfrastructure-$versionInfo/SFU/platform_scripts/bash/setup.sh"
    chmod +x $installFile
    ./$installFile 2>&1 | tee $installType-sfu.log
    echo "Customize (End): Unreal Pixel Streaming"
  fi

  binPaths="$binPaths:$installPath/Engine/Binaries/Linux"
fi

if [[ $renderEngines == *MoonRay* ]]; then
  echo "Customize (Start): MoonRay"
  dnf -y install mesa-libGL
  dnf -y install mesa-libGL-devel
  dnf -y --enablerepo=devel install libcgroup-devel
  dnf -y --enablerepo=devel install giflib-devel
  dnf -y install libtiff-devel
  dnf -y install libjpeg-devel
  dnf -y install libuuid-devel
  dnf -y install libcurl-devel
  dnf -y install openssl-devel
  dnf -y install libmng-devel
  dnf -y install libatomic
  dnf -y install patch
  dnf -y install bison
  dnf -y install flex
  if [ $machineType == Workstation ]; then
    dnf -y install qt5-qtbase-devel
    dnf -y install qt5-qtscript-devel
  fi

  git clone --recurse-submodules https://github.com/dreamworksanimation/openmoonray.git /openmoonray

  mkdir -p /installs/bin
  mkdir -p /installs/lib
  mkdir -p /installs/include
  mkdir -p /installs/openmoonray

  dnf -y install gcc-toolset-9 python2
  source /opt/rh/gcc-toolset-9/enable
  ln -s /bin/python2 /bin/python

  mkdir -p /openmoonray/build
  cd /openmoonray/build
  ln -s /openmoonray/building /building
  installType="openmoonray-prereq"
  $binPathCMake/cmake ../building 2>&1 | tee $installType.log
  $binPathCMake/cmake --build . -- -j 64 2>&1 | tee $installType-build.log

  cd /openmoonray
  installType="openmoonray"
  [[ "$gpuProvider" == NVIDIA ]] && useCUDA=YES || useCUDA=NO
  [[ $machineType == Workstation ]] && uiApps=YES || uiApps=NO
  $binPathCMake/cmake --preset container-release -D MOONRAY_USE_CUDA=$useCUDA -D BUILD_QT_APPS=$uiApps 2>&1 | tee $installType-preset.log
  $binPathCMake/cmake --build --preset container-release -- -j 64 2>&1 | tee $installType-preset-build.log
  $binPathCMake/cmake --install /build --prefix /installs/openmoonray 2>&1 | tee $installType-install.log
  echo "source /installs/openmoonray/scripts/setup.sh" >> $aaaProfile
  cd $binDirectory
  echo "Customize (End): MoonRay"
fi

if [ $machineType == Scheduler ]; then
  echo "Customize (Start): Azure CLI"
  installType="azure-cli"
  rpm --import https://packages.microsoft.com/keys/microsoft.asc
  dnf -y install https://packages.microsoft.com/config/rhel/8/packages-microsoft-prod.rpm
  dnf -y install $installType 2>&1 | tee $installType.log
  echo "Customize (End): Azure CLI"

  if [[ $renderManager == *Deadline* || $renderManager == *RoyalRender* ]]; then
    echo "Customize (Start): NFS Server"
    systemctl --now enable nfs-server
    echo "Customize (End): NFS Server"
  fi
fi

if [[ $renderManager == *Deadline* ]]; then
  versionInfo="10.2.1.0"
  installRoot="/Deadline"
  serverMount="/DeadlineServer"
  databaseHost=$(hostname)
  databasePort=27017
  databaseUser="dbService"
  databaseName="deadline10db"
  binPathScheduler="$installRoot/bin"

  echo "Customize (Start): Deadline Download"
  installFile="Deadline-$versionInfo-linux-installers.tar"
  installPath=$(echo ${installFile%.tar})
  downloadUrl="$binStorageHost/Deadline/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  mkdir -p $installPath
  tar -xzf $installFile -C $installPath
  echo "Customize (End): Deadline Download"

  if [ $machineType == Scheduler ]; then
    echo "Customize (Start): Mongo DB Service"
    repoPath="/etc/yum.repos.d/mongodb.repo"
    echo "[mongodb-org-4.4]" > $repoPath
    echo "name=MongoDB 4.4" >> $repoPath
    echo "baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.4/x86_64/" >> $repoPath
    echo "gpgcheck=1" >> $repoPath
    echo "enabled=1" >> $repoPath
    echo "gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc" >> $repoPath
    dnf -y install mongodb-org
    configFile="/etc/mongod.conf"
    sed -i "s/bindIp: 127.0.0.1/bindIp: 0.0.0.0/" $configFile
    sed -i "/bindIp: 0.0.0.0/a\  tls:" $configFile
    sed -i "/tls:/a\    mode: disabled" $configFile
    systemctl --now enable mongod
    sleep 10s
    echo "Customize (End): Mongo DB Service"

    echo "Customize (Start): Mongo DB User"
    installType="mongo-create-user"
    createUserScript="$installType.js"
    echo "db = db.getSiblingDB(\"$databaseName\");" > $createUserScript
    echo "db.createUser({" >> $createUserScript
    echo "user: \"$databaseUser\"," >> $createUserScript
    echo "pwd: \"$servicePassword\"," >> $createUserScript
    echo "roles: [" >> $createUserScript
    echo "{ role: \"dbOwner\", db: \"$databaseName\" }" >> $createUserScript
    echo "]})" >> $createUserScript
    mongo $createUserScript 2>&1 | tee $installType.log
    if grep --silent --ignore-case exception $installType.log; then
      exit 1
    fi
    echo "Customize (End): Mongo DB User"

    echo "Customize (Start): Deadline Server"
    export DB_PASSWORD=$servicePassword
    installFile="DeadlineRepository-$versionInfo-linux-x64-installer.run"
    $installPath/$installFile --mode unattended --dbLicenseAcceptance accept --prefix $installRoot --dbhost $databaseHost --dbport $databasePort --dbname $databaseName --installmongodb false --dbauth true --dbuser $databaseUser --dbpassword env:DB_PASSWORD
    mv /tmp/installbuilder_installer.log $binDirectory/deadline-repository.log
    echo "$installRoot *(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
    exportfs -r
    echo "Customize (End): Deadline Server"
  fi

  echo "Customize (Start): Deadline Client"
  installFile="DeadlineClient-$versionInfo-linux-x64-installer.run"
  installArgs="--mode unattended --prefix $installRoot --repositorydir $serverMount"
  if [ $machineType == Scheduler ]; then
    installArgs="$installArgs --slavestartup false --launcherdaemon false"
  else
    [ $machineType == Farm ] && workerStartup=true || workerStartup=false
    installArgs="$installArgs --slavestartup $workerStartup --launcherdaemon true"
  fi
  $installPath/$installFile $installArgs
  cp /tmp/installbuilder_installer.log $binDirectory/deadline-client.log
  echo "$binPathScheduler/deadlinecommand -StoreDatabaseCredentials $databaseUser $servicePassword" >> $aaaProfile
  echo "Customize (End): Deadline Client"

  binPaths="$binPaths:$binPathScheduler"
fi

if [[ $renderManager == *RoyalRender* ]]; then
  versionInfo="9.0.06"
  installRoot="/RoyalRender"
  binPathScheduler="$installRoot/bin/lx64"

  echo "Customize (Start): Royal Render Download"
  installFile="RoyalRender__${versionInfo}__installer.zip"
  downloadUrl="$binStorageHost/RoyalRender/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  unzip -q $installFile
  echo "Customize (End): Royal Render Download"

  dnf -y install xcb-util-wm
  dnf -y install xcb-util-image
  dnf -y install xcb-util-keysyms
  dnf -y install xcb-util-renderutil
  dnf -y install libxkbcommon-x11
  dnf -y install qt5-qtbase
  if [ $machineType == Scheduler ]; then
    echo "Customize (Start): Royal Render Server"
    installPath="RoyalRender*"
    installFile="rrSetup_linux"
    chmod +x ./$installPath/$installFile
    mkdir -p $installRoot
    ./$installPath/$installFile -console -rrRoot $installRoot 2>&1 | tee royal-render.log
    echo "$installRoot *(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
    exportfs -r
    echo "Customize (End): Royal Render Server"
  fi

  binPaths="$binPaths:$binPathScheduler"
fi

if [[ $renderManager == *Qube* ]]; then
  versionInfo="8.0-0"
  installRoot="/usr/local/pfx/qube"
  binPathScheduler="$installRoot/bin"

  echo "Customize (Start): Qube Core"
  dnf -y install perl
  dnf -y install xinetd
  installType="qube-core"
  installFile="$installType-$versionInfo.CENTOS_8.2.x86_64.rpm"
  downloadUrl="$binStorageHost/Qube/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  rpm -i $installType-*.rpm 2>&1 | tee $installType.log
  echo "Customize (End): Qube Core"

  if [ $machineType == Scheduler ]; then
    echo "Customize (Start): Qube Supervisor"
    installType="qube-supervisor"
    installFile="$installType-${versionInfo}.CENTOS_8.2.x86_64.rpm"
    downloadUrl="$binStorageHost/Qube/$versionInfo/$installFile$binStorageAuth"
    curl -o $installFile -L $downloadUrl
    rpm -i $installType-*.rpm 2>&1 | tee $installType.log
    echo "Customize (End): Qube Supervisor"

    echo "Customize (Start): Qube Data Relay Agent (DRA)"
    installType="qube-dra"
    installFile="$installType-$versionInfo.CENTOS_8.2.x86_64.rpm"
    downloadUrl="$binStorageHost/Qube/$versionInfo/$installFile$binStorageAuth"
    curl -o $installFile -L $downloadUrl
    rpm -i $installType-*.rpm 2>&1 | tee $installType.log
    echo "Customize (End): Qube Data Relay Agent (DRA)"
  else
    echo "Customize (Start): Qube Worker"
    installType="qube-worker"
    installFile="$installType-$versionInfo.CENTOS_8.2.x86_64.rpm"
    downloadUrl="$binStorageHost/Qube/$versionInfo/$installFile$binStorageAuth"
    curl -o $installFile -L $downloadUrl
    rpm -i $installType-*.rpm 2>&1 | tee $installType.log
    echo "Customize (End): Qube Worker"

    echo "Customize (Start): Qube Client"
    installType="qube-client"
    installFile="$installType-$versionInfo.CENTOS_8.2.x86_64.rpm"
    downloadUrl="$binStorageHost/Qube/$versionInfo/$installFile$binStorageAuth"
    curl -o $installFile -L $downloadUrl
    rpm -i $installType-*.rpm 2>&1 | tee $installType.log
    echo "Customize (End): Qube Client"

    configFile="/etc/qb.conf"
    sed -i "s/#qb_supervisor =/qb_supervisor = scheduler.artist.studio/" $configFile
    sed -i "s/#worker_cpus = 0/worker_cpus = 1/" $configFile
  fi

  binPaths="$binPaths:$binPathScheduler:$installRoot/sbin"
fi

if [ $machineType == Workstation ]; then
  echo "Customize (Start): Teradici PCoIP"
  versionInfo="23.04.1"
  [ "$gpuProvider" == "" ] && installType=pcoip-agent-standard || installType=pcoip-agent-graphics
  installFile="pcoip-agent-offline-rocky8.6_$versionInfo-1.el8.x86_64.tar.gz"
  downloadUrl="$binStorageHost/Teradici/$versionInfo/$installFile$binStorageAuth"
  curl -o $installFile -L $downloadUrl
  mkdir -p $installType
  tar -xzf $installFile -C $installType
  cd $installType
  ./install-pcoip-agent.sh $installType usb-vhci 2>&1 | tee ../$installType.log
  cd $binDirectory
  echo "Customize (End): Teradici PCoIP"
fi

echo "PATH=$PATH$binPaths" >> $aaaProfile


#!/bin/bash -ex

scheduledEvents=$(curl -H Metadata:true "http://169.254.169.254/metadata/scheduledevents?api-version=2020-07-01" | jq -c .Events)
for scheduledEvent in $(echo $scheduledEvents | jq -r '.[] | @base64'); do
  _jq() {
    echo $scheduledEvent | base64 -d | jq -r $1
  }
  eventType=$(_jq .EventType)

  if [[ $eventType == Preempt || $eventType == Terminate ]]; then
    eventScope=$(_jq .Resources[0])
    instanceName=$(curl -H Metadata:true "http://169.254.169.254/metadata/instance/compute/name?api-version=2021-05-01&format=text")

    if [ $eventScope == $instanceName ]; then
      if [[ $renderManager == *Deadline* ]]; then
        deadlineworker -shutdown
        deadlinecommand -DeleteSlave $(hostname)
      fi
      if [[ $renderManager == *RoyalRender* ]]; then
        :
      fi
      if [[ $renderManager == *Qube* ]]; then
        qbadmin worker --remove $(hostname)
      fi
    fi
  fi
done


#!/bin/bash -ex

cd ${binDirectory}

if [ "${wekaClusterName}" != "" ]; then
  volumeLabel="weka-iosw"
  installDisk="/dev/$(lsblk | grep ${wekaDataDiskSize}G | awk '{print $1}')"
  mkfs.ext4 -L $volumeLabel $installDisk 2>&1 | tee weka-mkfs.log
  installPath="/opt/weka"
  mkdir -p $installPath
  echo "LABEL=$volumeLabel $installPath ext4 defaults 0 2" >> /etc/fstab
  mount -a

  curl https://${wekaApiToken}@get.weka.io/dist/v1/install/${wekaVersion}/${wekaVersion} | sh

  coreIdsScript=${wekaCoreIdsScript}
  echo 'coreCountDrives=$(echo $machineSpec | jq -r .coreDrives)' > $coreIdsScript
  echo 'coreCountCompute=$(echo $machineSpec | jq -r .coreCompute)' >> $coreIdsScript
  echo 'coreCountFrontend=$(echo $machineSpec | jq -r .coreFrontend)' >> $coreIdsScript
  echo 'computeMemory=$(echo $machineSpec | jq -r .computeMemory)' >> $coreIdsScript
  echo '' >> $coreIdsScript
  echo 'coreIds="$(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | cut --delimiter - --fields 1 | sort --unique | tr \\n -)"' >> $coreIdsScript
  echo 'coreIds="$${coreIds:2}"' >> $coreIdsScript
  echo 'IFS="-" read -a coreIds <<< "$coreIds"' >> $coreIdsScript
  echo '' >> $coreIdsScript
  echo 'function GetCoreIds {' >> $coreIdsScript
  echo '  coreCount=$1' >> $coreIdsScript
  echo '  coreIdStart=$2' >> $coreIdsScript
  echo '  coreIdEnd=$(($coreIdStart + $coreCount))' >> $coreIdsScript
  echo '  containerCoreIds=""' >> $coreIdsScript
  echo '  for (( i=$coreIdStart; i<$coreIdEnd; i++ )); do' >> $coreIdsScript
  echo '    if [ "$containerCoreIds" != "" ]; then' >> $coreIdsScript
  echo '      containerCoreIds="$containerCoreIds,"' >> $coreIdsScript
  echo '    fi' >> $coreIdsScript
  echo '    containerCoreIds="$containerCoreIds$${coreIds[i]}"' >> $coreIdsScript
  echo '  done' >> $coreIdsScript
  echo '  echo $containerCoreIds' >> $coreIdsScript
  echo '}' >> $coreIdsScript
  echo '' >> $coreIdsScript
  echo 'coreIdsDrives=$(GetCoreIds $coreCountDrives 0)' >> $coreIdsScript
  echo 'coreIdsCompute=$(GetCoreIds $coreCountCompute $coreCountDrives)' >> $coreIdsScript
  echo 'coreIdsFrontend=$(GetCoreIds $coreCountFrontend $(($coreCountDrives + $coreCountCompute)))' >> $coreIdsScript

  machineSpec='${wekaMachineSpec}'
  source $coreIdsScript

  driveDisksScript=${wekaDriveDisksScript}
  echo 'nvmeDisks=/dev/nvme0n1' > $driveDisksScript
  echo 'for (( d=1; d<$(echo $machineSpec | jq -r .nvmeDisks); d++ )); do' >> $driveDisksScript
  echo '  nvmeDisks="$nvmeDisks /dev/nvme$(echo $d)n1"' >> $driveDisksScript
  echo 'done' >> $driveDisksScript

  fileSystemScript=${wekaFileSystemScript}
  echo "fileSystemName=${wekaFileSystemName}" > $fileSystemScript
  echo 'fileSystemDriveBytes=$(weka status --json | jq -r .capacity.total_bytes)' >> $fileSystemScript
  echo 'fileSystemTotalBytes=$(echo "$fileSystemDriveBytes * 100 / (100 - ${wekaObjectTierPercent})" | bc)' >> $fileSystemScript

  containerName="default"
  weka local stop --force $containerName
  weka local rm --force $containerName

  az login --identity
  failureDomain=$(hostname)
  drivesContainerName=drives0
  vmScaleSetState=$(az vmss show --resource-group ${wekaResourceGroupName} --name ${wekaClusterName} --query provisioningState --output tsv)
  if [ "$vmScaleSetState" == Updating ]; then
    joinIps=$(az vmss nic list --resource-group ${wekaResourceGroupName} --vmss-name ${wekaClusterName} --query [].ipConfigurations[0].privateIPAddress --output tsv | tr \\n ',')
    weka local setup container --name $drivesContainerName --base-port 14000 --failure-domain $failureDomain --join-ips $${joinIps::-1} --cores $coreCountDrives --drives-dedicated-cores $coreCountDrives --core-ids $coreIdsDrives --dedicate --no-frontends &> weka-container-setup-$drivesContainerName.log
    weka local setup container --name compute0 --base-port 15000 --failure-domain $failureDomain --join-ips $${joinIps::-1} --cores $coreCountCompute --compute-dedicated-cores $coreCountCompute --core-ids $coreIdsCompute --dedicate --memory $computeMemory --no-frontends &> weka-container-setup-compute0.log
    weka local setup container --name frontend0 --base-port 16000 --failure-domain $failureDomain --join-ips $${joinIps::-1} --cores $coreCountFrontend --frontend-dedicated-cores $coreCountFrontend --core-ids $coreIdsFrontend --dedicate &> weka-container-setup-frontend0.log
    weka user login admin ${wekaAdminPassword}
    source $driveDisksScript
    containerId=$(weka cluster container --filter container=$drivesContainerName,ips=$(hostname -i) --output id --no-header)
    weka cluster drive add $containerId $nvmeDisks &> weka-cluster-drive-add.log
    if [ ${wekaFileSystemAutoScale} == true ]; then
      source $fileSystemScript
      weka fs update $fileSystemName --ssd-capacity "$fileSystemDriveBytes"B --total-capacity "$fileSystemTotalBytes"B &> weka-fs-update.log
    fi
    dnsRecordQuery="aRecords[?ipv4Address=='$(hostname -i)']"
    while [ -z "$dnsRecordAddress" ]; do
      az network private-dns record-set a add-record --resource-group ${dnsResourceGroupName} --zone-name ${dnsZoneName} --record-set-name ${dnsRecordSetName} --ipv4-address $(hostname -i)
      sleep 5s
      dnsRecordAddress=$(az network private-dns record-set a show --resource-group ${dnsResourceGroupName} --zone-name ${dnsZoneName} --name ${dnsRecordSetName} --query $dnsRecordQuery --output tsv)
    done
  else
    weka local setup container --name $drivesContainerName --base-port 14000 --failure-domain $failureDomain --cores $coreCountDrives --drives-dedicated-cores $coreCountDrives --core-ids $coreIdsDrives --dedicate --no-frontends &> weka-container-setup-$drivesContainerName.log
  fi

  if [ $(hostname) == ${wekaClusterName}000000 ]; then
    systemctl --now enable nfs-server
    mkdir -p ${binDirectory}/log
    echo "${binDirectory}/log *(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
    exportfs -r
  fi

  dataFilePath="/var/lib/waagent/ovf-env.xml"
  dataFileText=$(xmllint --xpath "//*[local-name()='Environment']/*[local-name()='ProvisioningSection']/*[local-name()='LinuxProvisioningConfigurationSet']/*[local-name()='CustomData']/text()" $dataFilePath)
  codeFilePath="${binDirectory}/terminate.sh"
  echo $dataFileText | base64 -d > $codeFilePath
  chmod +x $codeFilePath

  if [ ${wekaTerminateNotification.enable} == true ]; then
    cronFilePath="/tmp/crontab"
    echo "* * * * * $codeFilePath" > $cronFilePath
    crontab $cronFilePath
  fi
fi


#!/bin/bash -ex

rootHost=${wekaClusterName}000000

logDirectory=/mnt/log
if ! mountpoint -q $logDirectory; then
  mkdir -p $logDirectory
  mount $rootHost:${binDirectory}/log $logDirectory
fi

metadataUrl="http://169.254.169.254/metadata"
eventsUrl="$metadataUrl/scheduledevents?api-version=2020-07-01"
vmNameUrl="$metadataUrl/instance/compute/name?api-version=2021-12-13&format=text"

instanceName=$(curl --silent --header Metadata:true $vmNameUrl)
scheduledEvents=$(curl --silent --header Metadata:true $eventsUrl | jq -c .Events)

function GetEncodedValue {
  echo $scheduledEvent | base64 -d | jq -r $1
}

for scheduledEvent in $(echo $scheduledEvents | jq -r '.[] | @base64'); do
  eventType=$(GetEncodedValue .EventType)
  eventScope=$(GetEncodedValue .Resources[0])

  if [[ $eventType == Terminate && $eventScope == $instanceName ]]; then
    az login --identity
    dnsRecordQuery="aRecords[?ipv4Address=='$(hostname -i)']"
    dnsRecordAddress=$(az network private-dns record-set a show --resource-group ${dnsResourceGroupName} --zone-name ${dnsZoneName} --name ${dnsRecordSetName} --query $dnsRecordQuery --output tsv)
    if [ -n "$dnsRecordAddress" ]; then
      az network private-dns record-set a remove-record --resource-group ${dnsResourceGroupName} --zone-name ${dnsZoneName} --record-set-name ${dnsRecordSetName} --ipv4-address $dnsRecordAddress --keep-empty-record-set
    fi

    weka user login admin ${wekaAdminPassword}
    instanceName="$instanceName-$(date +%T)"

    drivesRemoved=false
    driveIds=$(weka cluster drive --filter hostname=$(hostname),status=ACTIVE --output uuid --no-header | tr \\n ' ')
    driveIds=$${driveIds::-1}
    if [ "$driveIds" != "" ]; then
      weka cluster drive deactivate --force $driveIds &> $logDirectory/$instanceName-weka-cluster-drive-deactivate.log

      read -a driveIds <<< "$driveIds"
      for (( i=0; i<$${#driveIds[@]}; i++ )); do
        driveId=$${driveIds[i]}
        while
          driveStatus=$(weka cluster drive --filter uuid=$driveId --output status --no-header)
          [ $driveStatus != INACTIVE ]
        do
          sleep 3
        done
        weka cluster drive remove --force $driveId &> $logDirectory/$instanceName-weka-cluster-drive-remove-$driveId.log
      done
      drivesRemoved=true
    fi

    containersRemoved=false
    containerIds=$(weka cluster container --filter ips=$(hostname -i),status=UP --output id --no-header | tr \\n ' ')
    containerIds=$${containerIds::-1}
    if [ "$containerIds" != "" ]; then
      weka cluster container deactivate $containerIds &> $logDirectory/$instanceName-weka-cluster-container-deactivate.log

      read -a containerIds <<< "$containerIds"
      for (( i=0; i<$${#containerIds[@]}; i++ )); do
        containerId=$${containerIds[i]}
        while
          containerStatus=$(weka cluster container --HOST $rootHost --filter id=$containerId --output status --no-header)
          [ $containerStatus != INACTIVE ]
        do
          sleep 3
        done
        weka cluster container remove $containerId --HOST $rootHost &> $logDirectory/$instanceName-weka-cluster-container-remove-$containerId.log
      done
      containersRemoved=true
    fi

    if [[ $drivesRemoved == true && $containersRemoved == true ]]; then
      eventId=$(GetEncodedValue .EventId)
      eventData="{\"StartRequests\":[{\"EventId\":\"$eventId\"}]}"
      curl --silent --request POST --header Metadata:true --header Content-Type:application/json --data $eventData $eventsUrl &> $logDirectory/$instanceName-event-$eventId.log
    fi
  fi
done


#!/bin/bash -ex

source /etc/profile.d/aaa.sh

binDirectory="/usr/local/bin"
cd $binDirectory

useradd --system --password ${serviceAccountPassword} ${serviceAccountName}

if [[ ${renderManager} == *RoyalRender* ]]; then
  installType="royal-render-server"
  rrServerconsole -initAndClose 2>&1 | tee $installType-init.log
  rrWorkstation_installer -serviceServer -rrUser ${serviceAccountName} -rrUserPW ${serviceAccountPassword} 2>&1 | tee $installType-service.log
fi

if [ "${qubeLicense.userName}" != "" ]; then
  configFilePath="/etc/qube/dra.conf"
  sed -i "s/# mls_user =/mls_user = ${qubeLicense.userName}/" $configFilePath
  sed -i "s/# mls_password =/mls_password = ${qubeLicense.userPassword}/" $configFilePath
  systemctl restart dra.service
fi

serviceFile="aaaAutoScaler"
dataFilePath="/var/lib/waagent/ovf-env.xml"
dataFileText=$(xmllint --xpath "//*[local-name()='Environment']/*[local-name()='ProvisioningSection']/*[local-name()='LinuxProvisioningConfigurationSet']/*[local-name()='CustomData']/text()" $dataFilePath)
codeFilePath="$binDirectory/$serviceFile.sh"
echo $dataFileText | base64 -d > $codeFilePath
chmod +x $codeFilePath

serviceName="AAA Auto Scaler"
servicePath="/etc/systemd/system/$serviceFile.service"
echo "[Unit]" > $servicePath
echo "Description=$serviceName Service" >> $servicePath
echo "After=network-online.target" >> $servicePath
echo "" >> $servicePath
echo "[Service]" >> $servicePath
echo "Environment=renderManager=${renderManager}" >> $servicePath
echo "Environment=resourceGroupName=${autoScale.resourceGroupName}" >> $servicePath
echo "Environment=scaleSetName=${autoScale.scaleSetName}" >> $servicePath
echo "Environment=scaleSetMachineCountMax=${autoScale.scaleSetMachineCountMax}" >> $servicePath
echo "Environment=jobWaitThresholdSeconds=${autoScale.jobWaitThresholdSeconds}" >> $servicePath
echo "Environment=workerIdleDeleteSeconds=${autoScale.workerIdleDeleteSeconds}" >> $servicePath
echo "ExecStart=/bin/bash $codeFilePath" >> $servicePath
echo "" >> $servicePath

timerPath="/etc/systemd/system/$serviceFile.timer"
echo "[Unit]" > $timerPath
echo "Description=$serviceName Timer" >> $timerPath
echo "" >> $timerPath
echo "[Timer]" >> $timerPath
echo "OnUnitActiveSec=${autoScale.detectionIntervalSeconds}" >> $timerPath
echo "AccuracySec=1us" >> $timerPath
echo "" >> $timerPath
echo "[Install]" >> $timerPath
echo "WantedBy=timers.target" >> $timerPath

if [ ${autoScale.enable} == true ]; then
  systemctl --now enable $serviceFile
fi


#!/bin/bash -ex

az login --identity

queuedTasks=0
if [[ $renderManager == *Deadline* ]]; then
  activeJobIds=$(deadlinecommand -GetJobIdsFilter Status=Active)
  for jobId in $(echo $activeJobIds); do
    jobDetails=$(deadlinecommand -GetJobDetails $jobId)
    jobProperty="SubmitDate="
    jobSubmitDate=$(echo "$jobDetails" | grep $jobProperty)
    jobSubmitDate=$(echo $${jobSubmitDate#$jobProperty})
    jobWaitSecondsStart=$(date -u +%s --date="$jobSubmitDate")
    jobWaitSecondsEnd=$(date -u +%s)
    jobWaitSeconds=$(($jobWaitSecondsEnd - $jobWaitSecondsStart))
    if [ $jobWaitSeconds -gt $jobWaitThresholdSeconds ]; then
      taskIds=$(deadlinecommand -GetJobTaskIds $jobId)
      for taskId in $(echo $taskIds); do
        task=$(deadlinecommand -GetJobTask $jobId $taskId)
        taskStatus=$(echo "$task" | grep "TaskStatus=")
        taskStatus=$(echo $${taskStatus#*=})
        if [ $taskStatus == Queued ]; then
          ((queuedTasks++))
        fi
      done
    fi
  done
fi

if [ $queuedTasks -gt 0 ]; then # Scale Up
  scaleSetNodeCount=$(az vmss show --resource-group $resourceGroupName --name $scaleSetName --query "sku.capacity")
  if [[ $scaleSetMachineCountMax > 0 && $(($scaleSetNodeCount + $queuedTasks)) > $scaleSetMachineCountMax ]]; then
    scaleSetNodeCount=$scaleSetMachineCountMax
  else
    scaleSetNodeCount=$(($farmNodeCount + $queuedTasks))
  fi
  az vmss scale --resource-group $resourceGroupName --name $scaleSetName --new-capacity $scaleSetNodeCount
else # Scale Down
  if [[ $renderManager == *Deadline* ]]; then
    workerNames=$(deadlinecommand -GetSlaveNames)
    for workerName in $(echo $workerNames); do
      workerState=$(deadlinecommand -GetSlaveInfo $workerName SlaveState)
      if [ $workerState == Idle ]; then
        worker=$(deadlinecommand -GetSlave $workerName)
        workerProperty="WorkerLastRenderFinishedTime="
        workerIdleStartTime=$(echo "$worker" | grep $workerProperty)
        workerIdleStartTime=$(echo $${workerIdleStartTime#$workerProperty})
        if [ "$workerIdleStartTime" != "" ]; then
          workerIdleSecondsStart=$(date -u +%s --date="$workerIdleStartTime")
          workerIdleSecondsEnd=$(date -u +%s)
          workerIdleSeconds=$(($workerIdleSecondsEnd - $workerIdleSecondsStart))
        else
          workerIdleSeconds=$(deadlinecommand -GetSlaveInfo $workerName UpTimeSeconds)
        fi
        if [ $workerIdleSeconds -gt $workerIdleDeleteSeconds ]; then
          instanceId=$(az vmss list-instances --resource-group $resourceGroupName --name $scaleSetName --query "[?osProfile.computerName=='$workerName'].instanceId" --output tsv)
          az vmss delete-instances --resource-group $resourceGroupName --name $scaleSetName --instance-ids $instanceId
        fi
      fi
    done
  fi
fi


#!/bin/bash -ex

source /etc/profile.d/aaa.sh

binDirectory="/usr/local/bin"
cd $binDirectory

functionsCode="functions.sh"
functionsData="${filebase64("../0.Global.Foundation/functions.sh")}"
echo $functionsData | base64 --decode > $functionsCode
source $functionsCode

SetServiceAccount ${serviceAccountName} ${serviceAccountPassword}

fileSystemMounts='${jsonencode(fileSystemMounts)}'
for fileSystemMount in $(echo $fileSystemMounts | jq -r '.[] | @base64'); do
  if [ $(GetEncodedValue $fileSystemMount .enable) == true ]; then
    SetFileSystemMount "$(GetEncodedValue $fileSystemMount .mount)"
  fi
done
mount -a

EnableClientApp "${renderManager}" ${serviceAccountName} ${serviceAccountPassword}

if [ ${terminateNotification.enable} == true ]; then
  cronFilePath="/tmp/crontab"
  echo "* * * * * /tmp/terminate.sh" > $cronFilePath
  crontab $cronFilePath
fi


#!/bin/bash -ex

source /etc/profile.d/aaa.sh

binDirectory="/usr/local/bin"
cd $binDirectory

functionsCode="functions.sh"
functionsData="${filebase64("../0.Global.Foundation/functions.sh")}"
echo $functionsData | base64 --decode > $functionsCode
source $functionsCode

SetServiceAccount ${serviceAccountName} ${serviceAccountPassword}

fileSystemMounts='${jsonencode(fileSystemMounts)}'
for fileSystemMount in $(echo $fileSystemMounts | jq -r '.[] | @base64'); do
  if [ $(GetEncodedValue $fileSystemMount .enable) == true ]; then
    SetFileSystemMount "$(GetEncodedValue $fileSystemMount .mount)"
  fi
done
mount -a

EnableClientApp "${renderManager}" ${serviceAccountName} ${serviceAccountPassword}

if [ "${teradiciLicenseKey}" != "" ]; then
  /sbin/pcoip-register-host --registration-code=${teradiciLicenseKey} 2>&1 | tee pcoip-agent-license.log
fi


#!/bin/bash
# Copyright (C) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE-CODE in the project root for license information.

set -x

function fix_mod_probe() {
    FILENAME=/etc/modprobe.d/blacklist-nouveau.conf
    /bin/cat <<EOM >$FILENAME
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
# Eliminate conflicts with nVIDIA drivers for GPU instances using stock kernels
blacklist nouveau
options nouveau modeset=0
EOM
}

function fix_cloud_config() {
    rm -f /etc/cloud/ds-identify.cfg
    rm -f /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg
    rm -f /etc/cloud/cloud.cfg.d/curtin-preserve-sources.cfg

    FILENAME=/etc/cloud/cloud.cfg.d/10-azure-kvp.cfg
    /bin/cat <<EOM >$FILENAME
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
reporting:
  logging:
    type: log
  telemetry:
    type: hyperv
EOM

    FILENAME=/etc/cloud/cloud.cfg.d/90_dpkg.cfg
    /bin/cat <<EOM >$FILENAME
# to update this file, run dpkg-reconfigure cloud-init
datasource_list: [ Azure ]
EOM
    FILENAME=/etc/cloud/cloud.cfg.d/90-azure.cfg
    /bin/cat <<EOM >$FILENAME
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
system_info:
   package_mirrors:
     - arches: [i386, amd64]
       failsafe:
         primary: http://archive.ubuntu.com/ubuntu
         security: http://security.ubuntu.com/ubuntu
       search:
         primary:
           - http://azure.archive.ubuntu.com/ubuntu/
         security: []
     - arches: [armhf, armel, default]
       failsafe:
         primary: http://ports.ubuntu.com/ubuntu-ports
         security: http://ports.ubuntu.com/ubuntu-ports
EOM
    # after systemd changes
}

function fix_cloud_init() {
    mkdir -p /lib/systemd/system/cloud-init-local.service.d
    FILENAME=/lib/systemd/system/cloud-init-local.service.d/50-azure-clear-persistent-obj-pkl.conf
    /bin/cat <<EOM >$FILENAME
[Service]
ExecStartPre=-/bin/sh -xc 'if [ -e /var/lib/cloud/instance/obj.pkl ]; then echo "cleaning persistent cloud-init object"; rm /var/lib/cloud/instance/obj.pkl; fi; exit 0'
EOM
    systemctl daemon-reload
}

function fix_grub() {
    FILENAME=/etc/default/grub.d/50-cloudimg-settings.cfg
    /bin/cat <<EOM >$FILENAME
# Windows Azure specific grub settings
# CLOUD_IMG: This file was created/modified by the Cloud Image build process

# Set the default commandline
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0 earlyprintk=ttyS0 rootdelay=300"

# Set the grub console type
GRUB_TERMINAL=serial

# Set the serial command
GRUB_SERIAL_COMMAND="serial --speed=9600 --unit=0 --word=8 --parity=no --stop=1"

# Set the recordfail timeout
GRUB_RECORDFAIL_TIMEOUT=0

# Do not wait on grub prompt
GRUB_TIMEOUT=0
EOM

    FILENAME=/etc/default/grub
    /bin/cat <<EOM >$FILENAME
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

# Set the default commandline
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0 earlyprintk=ttyS0 rootdelay=300"

# Set the grub console type
GRUB_TERMINAL=serial

# Set the serial command
GRUB_SERIAL_COMMAND="serial --speed=9600 --unit=0 --word=8 --parity=no --stop=1"

# Set the recordfail timeout
GRUB_RECORDFAIL_TIMEOUT=0

# Do not wait on grub prompt
GRUB_TIMEOUT=0

GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_DISTRIBUTOR=\`lsb_release -i -s 2> /dev/null || echo Debian\`

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command \`vbeinfo\`
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"
EOM
    update-grub
}

function fix_initramfs() {
    FILENAME=/etc/initramfs-tools/modules
    /bin/cat <<EOM >$FILENAME
# List of modules that you want to include in your initramfs.
# They will be loaded at boot time in the order below.
#
# Syntax:  module_name [args ...]
#
# You must run update-initramfs(8) to effect this change.
#
# Examples:
#
# raid1
# sd_mod

# CLOUD_IMG: This file was created/modified by the Cloud Image build process
# This fix is important on virtual machines, because when hyperv_fb.ko is
# missing, the "setfont" stage for the console (part of the boot process)
# will add 2-4 minutes to initialization on Ubuntu. Apparently this operation
# does a lot of work with the emulated device and its generic VGA driver.
hyperv_fb
EOM
}

function main() {
    fix_mod_probe
    fix_cloud_config
    fix_initramfs
    fix_grub
    fix_cloud_init
}

main


#!/bin/bash

# report all lines
set -x

function retrycmd_if_failure() {
    retries=$1; wait_sleep=$2; shift && shift
    for i in $(seq 1 $retries); do
        ${@}
        [ $? -eq 0  ] && break || \
        if [ $i -eq $retries ]; then
            echo Executed \"$@\" $i times;
            return 1
        else
            sleep $wait_sleep
        fi
    done
    echo Executed \"$@\" $i times;
}

function apt_get_install() {
    retries=$1; wait_sleep=$2; shift && shift
    for i in $(seq 1 $retries); do
        apt-get install --no-install-recommends -y ${@}
        [ $? -eq 0  ] && break || \
        if [ $i -eq $retries ]; then
            return 1
        else
            sleep $wait_sleep
            retrycmd_if_failure 12 5 apt-get update
        fi
    done
    echo "completed"
    echo Executed apt-get install --no-install-recommends -y \"$@\" $i times;
}

function update_linux() {
    retrycmd_if_failure 12 5 apt-get update
    apt_get_install 12 5 nfs-kernel-server iotop iperf3 bwm-ng
}

function main() {
    touch /opt/install.started

    echo "update linux"
    update_linux

    echo "installation complete"
    touch /opt/install.completed
}

main


#!/bin/bash
set -x

# always source to get go in the path
. $HOME/.profile

# install golang
if ! command -v go &> /dev/null ; then
    GO_DL_FILE=go1.16.6.linux-amd64.tar.gz
    wget --tries=12 --wait=5 https://dl.google.com/go/$GO_DL_FILE
    sudo -E tar -C /usr/local -xzf $GO_DL_FILE
    rm -f $GO_DL_FILE
    echo "export PATH=$PATH:/usr/local/go/bin" >> $HOME/.profile
    . $HOME/.profile
fi

# checkout and build CacheWarmer
cd
RELEASE_DIR=$HOME/release
mkdir -p $RELEASE_DIR
git clone https://github.com/Azure/Avere.git
# build the cache warmer
cd $HOME/Avere/src/go/cmd/cachewarmer/cachewarmer-jobsubmitter
go build
mv cachewarmer-jobsubmitter $RELEASE_DIR/.
cd $HOME/Avere/src/go/cmd/cachewarmer/cachewarmer-manager
go build
mv cachewarmer-manager $RELEASE_DIR/.
cd $HOME/Avere/src/go/cmd/cachewarmer/cachewarmer-worker
go build
mv cachewarmer-worker $RELEASE_DIR/.
cd

# set the path
export CACHEWARMER_MANAGER_PATH=$RELEASE_DIR/cachewarmer-manager
export CACHEWARMER_WORKER_PATH=$RELEASE_DIR/cachewarmer-worker
export CACHEWARMER_JOBSUBMITTER_PATH=$RELEASE_DIR/cachewarmer-jobsubmitter

set +x


#!/bin/bash

# before running this script define the following env vars
# LOCAL_MOUNT_DIR=/b
# BOOTSTRAP_MOUNT_ADDRESS=FILE_ADDRESS
# BOOTSTRAP_MOUNT_EXPORT=/data
# BOOTSTRAP_SUBDIR=/bootstrap
#
# # leave CACHEWARMER_MANAGER_PATH empty if you did not build your own cachewarmer binaries
# CACHEWARMER_MANAGER_PATH=""
# CACHEWARMER_WORKER_PATH=""
# CACHEWARMER_JOBSUBMITTER_PATH=""

if [ -z $LOCAL_MOUNT_DIR ]; then
    echo "LOCAL_MOUNT_DIR is not set"
    return
fi
if [ -z $BOOTSTRAP_MOUNT_ADDRESS ]; then
    echo "BOOTSTRAP_MOUNT_ADDRESS is not set"
    return
fi
if [ -z $BOOTSTRAP_MOUNT_EXPORT ]; then
    echo "BOOTSTRAP_MOUNT_EXPORT is not set"
    return
fi
if [ -z $BOOTSTRAP_SUBDIR ]; then
    echo "BOOTSTRAP_SUBDIR is not set"
    return
fi

set -x

# go home
cd

# "sudo -E" required to pass environment variables including proxy settings

# remove a previous install if one exists
sudo -E rm -f $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/bootstrap.cachewarmer-manager.sh
sudo -E rm -f $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/bootstrap.cachewarmer-worker.sh
sudo -E rm -f $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/rsyslog/35-cachewarmer-manager.conf
sudo -E rm -f $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/rsyslog/36-cachewarmer-worker.conf
sudo -E rm -f $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/systemd/cachewarmer-manager.service
sudo -E rm -f $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/systemd/cachewarmer-worker.service

# create the bootstrap directory
sudo -E mkdir -p $LOCAL_MOUNT_DIR
sudo -E mount -o 'hard,nointr,proto=tcp,mountproto=tcp,retry=30' $BOOTSTRAP_MOUNT_ADDRESS:$BOOTSTRAP_MOUNT_EXPORT $LOCAL_MOUNT_DIR
sudo -E mkdir -p $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/cachewarmerbin
sudo -E mkdir -p $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/rsyslog
sudo -E mkdir -p $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/systemd

# download the content
sudo -E curl --retry 5 --retry-delay 5 -L -o $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/bootstrap.cachewarmer-manager.sh https://raw.githubusercontent.com/Azure/Avere/main/src/go/cmd/cachewarmer/deploymentartifacts/bootstrap/bootstrap.cachewarmer-manager.sh
sudo -E curl --retry 5 --retry-delay 5 -L -o $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/bootstrap.cachewarmer-worker.sh https://raw.githubusercontent.com/Azure/Avere/main/src/go/cmd/cachewarmer/deploymentartifacts/bootstrap/bootstrap.cachewarmer-worker.sh
sudo -E curl --retry 5 --retry-delay 5 -L -o $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/rsyslog/35-cachewarmer-manager.conf https://raw.githubusercontent.com/Azure/Avere/main/src/go/cmd/cachewarmer/deploymentartifacts/bootstrap/rsyslog/35-cachewarmer-manager.conf
sudo -E curl --retry 5 --retry-delay 5 -L -o $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/rsyslog/36-cachewarmer-worker.conf https://raw.githubusercontent.com/Azure/Avere/main/src/go/cmd/cachewarmer/deploymentartifacts/bootstrap/rsyslog/36-cachewarmer-worker.conf
sudo -E curl --retry 5 --retry-delay 5 -L -o $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/systemd/cachewarmer-manager.service https://raw.githubusercontent.com/Azure/Avere/main/src/go/cmd/cachewarmer/deploymentartifacts/bootstrap/systemd/cachewarmer-manager.service
sudo -E curl --retry 5 --retry-delay 5 -L -o $LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/systemd/cachewarmer-worker.service https://raw.githubusercontent.com/Azure/Avere/main/src/go/cmd/cachewarmer/deploymentartifacts/bootstrap/systemd/cachewarmer-worker.service

TARGET_PATH=$LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/cachewarmerbin/cachewarmer-manager
if [ -z "$CACHEWARMER_MANAGER_PATH" ]; then
    browser_download_url=$(curl -s https://api.github.com/repos/Azure/Avere/releases/latest | jq -r .assets[].browser_download_url | grep -e "cachewarmer-manager$")
    sudo -E curl --retry 5 --retry-delay 5 -L -o $TARGET_PATH $browser_download_url
    sudo -E chmod +x $TARGET_PATH
else
    sudo -E cp $CACHEWARMER_MANAGER_PATH $TARGET_PATH
fi
TARGET_PATH=$LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/cachewarmerbin/cachewarmer-worker
if [ -z "$CACHEWARMER_WORKER_PATH" ]; then
    browser_download_url=$(curl -s https://api.github.com/repos/Azure/Avere/releases/latest | jq -r .assets[].browser_download_url | grep -e "cachewarmer-worker$")
    sudo -E curl --retry 5 --retry-delay 5 -L -o $TARGET_PATH $browser_download_url
    sudo -E chmod +x $TARGET_PATH
else
    sudo -E cp $CACHEWARMER_WORKER_PATH $TARGET_PATH
fi
TARGET_PATH=$LOCAL_MOUNT_DIR/$BOOTSTRAP_SUBDIR/cachewarmerbin/cachewarmer-jobsubmitter
if [ -z "$CACHEWARMER_JOBSUBMITTER_PATH" ]; then
    browser_download_url=$(curl -s https://api.github.com/repos/Azure/Avere/releases/latest | jq -r .assets[].browser_download_url | grep -e "cachewarmer-jobsubmitter$")
    sudo -E curl --retry 5 --retry-delay 5 -L -o $TARGET_PATH $browser_download_url
    sudo -E chmod +x $TARGET_PATH
else
    sudo -E cp $CACHEWARMER_JOBSUBMITTER_PATH $TARGET_PATH
fi

# umount and remove the local mount directory
sudo -E umount $LOCAL_MOUNT_DIR
sudo -E rmdir $LOCAL_MOUNT_DIR

set +x