#!/usr/bin/env python

# Copyright C 2002-2013 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
# LLC.  Legion is a trademark of Isilon Systems, LLC.  All other
# trademarks and registered trademarks are the property of their
# respective holders.
#
# This script is autorun at boottime and shouldn't be run again.


import os
import logging
import sys
import traceback

import isi.fs.lin as ifl
import isi.misc as misc
import isi.ui.lib.format as format
import isi.fs.lib as fslib

LOG_FILE        = '/var/log/isi_sstore.log'
FOUND_FILE      = '/ifs/.ifsvar/run/.found_sstores'
NOTFOUND_FILE   = '/ifs/.ifsvar/run/.found_no_sstores'
PROG            = os.path.basename(sys.argv[0])

def getMaxSin():
    return misc.sysctlbyname('efs.lin.max_sin')

def record_state(fpath):
    """
    Create the requested file.
    Expects a complete file path.
    Populate with max lin and max sin at the time of creation.
    """
    sin = getMaxSin()
    lin = ifl.getMaxLin()
    fd = os.open(fpath, os.O_CREAT | os.O_WRONLY | os.O_EXLOCK)
    os.write(fd, "%s\n" % format.fmtLin(sin))
    os.write(fd, "%s\n" % format.fmtLin(lin))
    os.close(fd)
    os.chmod(fpath, 0444)
    logging.warn("%s: Noted SIN %s, LIN %s in %s" %
            (PROG, format.fmtLin(sin), format.fmtLin(lin), fpath))

def main():
    """
    Record the max lin and max sin at the time of boot.
    Check the not-found-file to guard against accidental re-run of the script.
    """

    try:
        logging.basicConfig(filename=LOG_FILE)
        sin = getMaxSin()

        if os.path.exists(NOTFOUND_FILE):
            logging.warn("%s: %s exists.\n" % (PROG, NOTFOUND_FILE))
            sys.exit(0)
        elif sin == ifl.BASE_SIN:
            record_state(NOTFOUND_FILE)
            sys.exit(0)
        elif os.path.exists(FOUND_FILE):
            logging.warn("%s: %s exists.\n" % (PROG, FOUND_FILE))
            sys.exit(0)
        else:
            logging.warn("%s: Found pre-existing SIN %s" %
                    (PROG, format.fmtLin(sin)))
            record_state(FOUND_FILE)
            sys.exit(0)

    except Exception, e:
        dexitcodes = fslib.getProcDexitcodes()
        logging.exception("%s: Failed to run %s with %s: [%s]"
                % (misc.hostname(), PROG, traceback.print_exc(e), dexitcodes))
        sys.exit(1)

if __name__ == '__main__':
    main()
