#!/bin/sh

# Copyright C 2002-2011 by Isilon Systems LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# This script should be run only from isi_post_ifs and all commands run
# in it must be idempotent, as a failure could occur midway through execution
# and this script will be re-run during the next boot.

# Pre-conditions: ifs is mounted, node has quorum, /ifs/.ifsvar is created

if [ -f /etc/ifs/isi_first_post_ifs_already_ran ]; then
    exit 1
fi

# Check if ifsvar is ready

/usr/bin/isi_ifsvar_ready
ifsvar_ready=$?

# If ifsvar isn't ready then wait 60 seconds before checking again for
# readiness. 60 seconds is no magical number but we expect to loop couple of
# times before ifsvar is ready. If ifsvar isn't ready in 10 minutes we
# exit out. Related to bug 44576.
count=0
while [ $ifsvar_ready -ne 0 ] && [ $count -lt 10 ]
do
    /usr/bin/logger -i -p ifs.err "Ifsvar ready yet: sleeping for 60
    seconds"
    /bin/sleep 60
    /usr/bin/isi_ifsvar_ready
    ifsvar_ready=$?
    count=$(($count + 1 ))
done

if [ $ifsvar_ready -ne 0 ]; then
    exit 1
fi

# Merge this build's driveconfig information with that already on the cluster
if [ -f /ifs/.ifsvar/modules/hardware/drive_config.gc ]; then
    echo "Merging driveconfig info with cluster's"
    # Ordering shouldn't matter at all here, given these entries are all
    # versioned, but I'll merge the build's files first, then udpate from ifs.
    /usr/bin/isi_hwtools/isi_dsptool merge /etc/gconfig/drive_config.gc
    /usr/bin/isi_hwtools/isi_dsptool reinstall
fi

# Create the file to let drive_d know to begin alerting for drive firmware
touch /etc/ifs/isi_first_post_ifs_merged_configs
chmod 640 /etc/ifs/isi_first_post_ifs_merged_configs
# Now that configs are up-to-date, have drive_d check firmware
python -c "import isi.hw.drived as dd; dd.driveAlertCheckAll()"

# Record existing shadow store status
SSTORE_CHECK_RAN_FILE=/ifs/.ifsvar/run/.isi_sstores_exist_already_ran
if  [ ! -f $SSTORE_CHECK_RAN_FILE ] ; then
    /usr/libexec/isilon/isi_sstores_exist
    if [ $? -eq 0 ]; then
        touch $SSTORE_CHECK_RAN_FILE
        chmod 444 $SSTORE_CHECK_RAN_FILE
    fi
fi
# In 7.0, cancel any running mark and update block history. See bug 103358.
if [ -f /usr/libdata/patchmgr/mavericks-bug-103358 ]; then
    /usr/libdata/patchmgr/mavericks-bug-103358
fi

# Confirm folder for shared Samba state exists
if [ ! -d /ifs/.ifsvar/samba ]; then
    /bin/mkdir /ifs/.ifsvar/samba
fi

if [ -f /usr/libdata/patchmgr/waikiki-isi_job_d-upgrade ]; then
    /usr/libdata/patchmgr/waikiki-isi_job_d-upgrade
fi

if [ -f /usr/libdata/patchmgr/jaws_je_phase1_upgrade ]; then
    /usr/libdata/patchmgr/jaws_je_phase1_upgrade
fi

# In 6.5, lsassd changed config format.
if [ -f /usr/libdata/patchmgr/chopu-auth-upgrade ]; then
    /usr/libdata/patchmgr/chopu-auth-upgrade 1
fi

# In 7.0, lsass/pstore gconfig changes
if [ -f /usr/sbin/isi_auth_upgrade ]; then
   /usr/sbin/isi_auth_upgrade
fi
# Run mavericks python script to handle other changes
if [ -f /usr/libdata/patchmgr/mavericks-auth-upgrade ]; then
    /usr/libdata/patchmgr/mavericks-auth-upgrade
fi

# Second pass for chopu-auth-upgrade.  Changed idmap/pstore
# Relies on lsassd starting up, therefore on isi_auth_upgrade
if [ -f /usr/libdata/patchmgr/chopu-auth-upgrade ]; then
    /usr/libdata/patchmgr/chopu-auth-upgrade 2
fi

# In 6.5, smbd was replaced by lwiod as the SMB implementation
if [ -f /usr/libdata/patchmgr/chopu-smb-upgrade ]; then
    /usr/libdata/patchmgr/chopu-smb-upgrade
fi

# In 6.5, quota email transform changed config format
if [ -f /usr/libdata/patchmgr/chopu-email-transform-upgrade ]; then
    /usr/libdata/patchmgr/chopu-email-transform-upgrade
fi

# In 6.5, some nfs sysctls were moved to default export options
if [ -f /usr/libdata/patchmgr/chopu-nfs-upgrade ]; then
    /usr/libdata/patchmgr/chopu-nfs-upgrade
fi

# In 7.0, many sysctls were converted to gconfig
if [ -f /usr/libdata/patchmgr/mavericks-nfs-upgrade ]; then
    /usr/libdata/patchmgr/mavericks-nfs-upgrade
fi

# In 7.0, some share permissions bugs need to be fixed
# Execute with 'isi_run -z' so we have a valid zone while running
if [ -f /usr/libdata/patchmgr/mavericks-smb-upgrade ]; then
    isi_run -z 1 "/usr/libdata/patchmgr/mavericks-smb-upgrade"
fi

# In 6.0, CELOG was introduced, and all configuration moved to gconfig.
if [ -f /usr/libdata/patchmgr/isi_notification_upgrade ]; then
    /usr/libdata/patchmgr/isi_notification_upgrade
fi

# In 7.0, http configuration moved to gconfig.
if [ -f /usr/libdata/patchmgr/mavericks-http-upgrade ]; then
    /usr/libdata/patchmgr/mavericks-http-upgrade
fi

# In 7.0, permissions on some files were made more restrictive
if [ -f /usr/libdata/patchmgr/mavericks-perm-harden ]; then
    /usr/libdata/patchmgr/mavericks-perm-harden
fi

# In 7.0, there is user/group/perm weirdness for the compadmin user since
# there is no root.
if [ -f /usr/libdata/patchmgr/mavericks-compliance-upgrade ]; then
    /usr/libdata/patchmgr/mavericks-compliance-upgrade
fi

# In 7.2, smb config store switched to a new backend, move the config
# Execute with 'isi_run -z' so we have a valid zone while running
if [ -f /usr/libdata/patchmgr/jaws-smb-upgrade ]; then
    isi_run -z 1 "/usr/libdata/patchmgr/jaws-smb-upgrade"
fi

# In 7.2, ntp configuration moved to gconfig
if [ -f /usr/libdata/patchmgr/jaws-ntp-upgrade ]; then
    /usr/libdata/patchmgr/jaws-ntp-upgrade
fi

# In 7.2, nfs configuration and exports moved to gconfig
if [ -f /usr/libdata/patchmgr/moby-nfs-upgrade ]; then
    /usr/libdata/patchmgr/moby-nfs-upgrade
fi

# Install tools if appropriate
if [ -x /usr/bin/isi_tools ] ; then
    # run this in the background, because it waits for
    # the external network to be available.
    /usr/sbin/daemon -cf /usr/bin/isi_tools -ifnwqq
fi

# If no node has done so yet, copy new patches globally.
if [ ! -f /ifs/.ifsvar/db/already_copied_patches ] ; then
    touch /ifs/.ifsvar/db/already_copied_patches
    chmod 640 /ifs/.ifsvar/db/already_copied_patches
    # Only do the copy if there is anything to copy
    if [ "$(ls -A /var/db/isilon-pkg | head)" ] ; then
        /bin/cp -Rf /var/db/isilon-pkg/* /ifs/.ifsvar/db/pkg/
    fi
fi

 # Make sure we're not in an auto-suspend pool
/usr/bin/isi_sc_auto_resume -f > /dev/null 2>&1

# Upgrade SyncIQ Policies
/usr/libdata/patchmgr/siq-upgrade

# Record existing snapshot status.
PRE_DSBT_SNAPS_FILE=/ifs/.ifsvar/modules/isi_snapshots/.pre_dsbt_snapshots_chk
if  [ ! -f $PRE_DSBT_SNAPS_FILE ] ; then
    if [ -f /usr/libdata/patchmgr/waikiki-snapshot-upgrade ]; then
        /usr/libdata/patchmgr/waikiki-snapshot-upgrade
    fi
    if [ $? -eq 0 ]; then
        touch $PRE_DSBT_SNAPS_FILE
        chmod 444 $PRE_DSBT_SNAPS_FILE
    fi
fi

#Check and create required VASA Configuration
if [ ! -d /ifs/.ifsvar/tmp ] ; then
    /bin/mkdir -p /ifs/.ifsvar/tmp
    /bin/chmod 755 /ifs/.ifsvar/tmp
fi
/usr/bin/lockf /ifs/.ifsvar/tmp/isi_vasa_init_running /usr/sbin/isi_vasa_init

# Check and create audit directory
if  [ ! -d /ifs/.ifsvar/modules/audit/ ] ; then
    /bin/mkdir -p /ifs/.ifsvar/modules/audit/
    /bin/chmod 700 /ifs/.ifsvar/modules/audit/
fi

# Upgrade job engine configuration
if [ -f /ifs/.ifsvar/modules/jobengine/status.gc ] ; then
    # devid_set must be quoted in 7.0
    lockf -k /ifs/.ifsvar/modules/jobengine/status.gc.lck sh -c "
      sed 's/\(participants[^=]*= *\)\({[^}]*}\)/\1\"\2\"/' \
        /ifs/.ifsvar/modules/jobengine/status.gc \
        >/ifs/.ifsvar/modules/jobengine/status.gc.7

      mv -f /ifs/.ifsvar/modules/jobengine/status.gc.7 \
        /ifs/.ifsvar/modules/jobengine/status.gc"
fi

# Check and create volcopy directory
if  [ ! -d /ifs/.ifsvar/modules/isi_vol_copy/ ] ; then
    /bin/mkdir -p /ifs/.ifsvar/modules/isi_vol_copy/
    /bin/chmod 755 /ifs/.ifsvar/modules/isi_vol_copy/
fi

# Check and create volcopy_vnx directory
if  [ ! -d /ifs/.ifsvar/modules/isi_vol_copy_vnx/ ] ; then
    /bin/mkdir -p /ifs/.ifsvar/modules/isi_vol_copy_vnx/
    /bin/chmod 755 /ifs/.ifsvar/modules/isi_vol_copy_vnx/
fi

# we need to modify zone config, so we need an actual zone
if [ -f /usr/libdata/patchmgr/moby-auth-upgrade ]; then
    # Migrate old Kerberos config/keys into providers
    isi_run -z 1 /usr/libdata/patchmgr/moby-auth-upgrade
fi

touch /etc/ifs/isi_first_post_ifs_already_ran
chmod 640 /etc/ifs/isi_first_post_ifs_already_ran

exit 0

