#!/usr/bin/python

# Copyright C 2002-2010 by Isilon Systems, Inc. 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.
import sys
import syslog
import traceback
import time
import os.path as path

import isi.misc
import isi.error as ierr
import isi.smartpools.utils as sp_utils
import isi.config.licensing as lic
import isi.config.ifs as ifs
import isi.app.lib.cluster as clu
import isi.ui.shell.jobcmd as jobcmd
import isi.fs.job as job


# This script will start the SP job if the flag exists, and set the 
# efs.bam.layout.disk_pool_global_force_spill if needs be.

NOT_LICENSED_SYSCTL = "efs.bam.layout.disk_pool_global_force_spill"

def main():

    # All nodes need to set the sysctl if need be.
    try:
        current_value = clu.cluster_get_sysctl(NOT_LICENSED_SYSCTL)
    except Exception, e:
        syslog.syslog(syslog.LOG_ERR, "Error getting sysctl %s: %s" % 
                (NOT_LICENSED_SYSCTL, str(e)))
        return 1
    is_licensed = sp_utils.is_licensed()
    new_value = str(int(not is_licensed))
    if new_value != current_value:
        syslog.syslog(syslog.LOG_INFO, "Smartpools licensing status has "
                "changed. Setting sysctl %s from %s to %s" % 
                (NOT_LICENSED_SYSCTL, current_value, new_value))

        try:
            clu.cluster_set_sysctl(NOT_LICENSED_SYSCTL, new_value)
        except Exception, e:
            syslog.syslog(syslog.LOG_ERR, "Error setting sysctl %s: %s" % 
                (NOT_LICENSED_SYSCTL, str(e)))
    
    # Only one node needs to schedule or start the SP job.  Let the isi_boot_d 
    # master do it.
    try:
        cluster = clu.Cluster()
        local = cluster.local()
        master = cluster.master()
    except isi.config.ifs.error, e:
        syslog.syslog(syslog.LOG_ERR, "Error getting array.xml info" % 
                str(e))
        return 1
    job_type = (is_licensed and job.JobType.FILEPOLICY_JOB or 
            job.JobType.SETPROTECTPLUS_JOB)
    if local == master and isi.misc.ifs_ready():
        if (path.exists(sp_utils.RUN_SP_JOB_ONCE_FLAG) and
                jobcmd.get_running_job(job_type) is None):
            syslog.syslog(syslog.LOG_INFO, "Starting %s job" %
                    job_type == job.JobType.FILEPOLICY_JOB and "SmartPools"
                    or "SetProtectPlus")
            try:
                job.JobCmd.start(job_type, job.NULL_JOB_PRIORITY, None)
            except (ierr.IsiSystemError, ierr.IsiError), e:
                syslog.syslog(syslog.LOG_ERR, "Error starting SP job: %s" 
                        % str(e))
                return 1
    return 0


if __name__ == "__main__":
    syslog.openlog("isi_smartpools_cron")
    try:
        rc = main()
    except Exception, e:
        syslog.syslog(syslog.LOG_ERR, "Error running isi_smartpools_cron: %s" %
                traceback.format_exc())
        rc = 1
    syslog.closelog()
    sys.exit(rc)
