#!/usr/bin/python

"""
    This module contains the deployment script to create a RAID volume using hypercfg/HyperWin
"""

import string, os, os.path, shutil, sys, popen2, systemMgr
from logMgr import addLogRecord,CRITICAL,ERROR,WARNING,INFO,DEBUG
from config import DeploymentStore, DeployPath
from config import getstatusoutput
from deploymentData import ATSTATE,FAILURE,UPDATE,REFRESH,REBOOT,UNSUPPORTED
try:
    __file__
except:
    __file__ = __name__

def checkBIOS(restore=0):
    """
        checkBIOS() returns ATSTATE if at desired state
        checkBIOS() returns FAILURE if state can't be achieved
        checkBIOS() returns UPDATE if system info refresh is needed once configured
    """
    ret, value = systemMgr.queryProperty(0,'IntelBIOSID','BoardFamilyID','S3000',{})
    if not ret == 1:
        print 'hypercfg/HyperWin is supported on S3000 systems only'
        #addLogRecord(0,__file__, INFO,'raidcfg32 is supported on S3000 systems only')
        return UNSUPPORTED
    ret, value = systemMgr.queryProperty(0,'BIOSRAID','SWRAID','Enabled',{})
    if not ret == 1:
        #print 'BIOS setting for SWRAID is not enabled'
        #addLogRecord(0,__file__, INFO,'BIOS setting for SWRAID is not enabled')
        return UPDATE
    ret, value = systemMgr.queryProperty(0,'BIOSRAID','Vendor','LSI',{})
    if not ret == 1:
        #print 'LSI RAID is not configured in BIOS'
        #addLogRecord(0,__file__, INFO,'LSI RAID is not configured in BIOS')
        return UPDATE
    return ATSTATE


def CreateVolume(restore=0):
    state = checkBIOS(restore)
    if state == UNSUPPORTED:
        return UNSUPPORTED
    if state == UPDATE:
        from detectBIOSRAID import setSWRAID
        setSWRAID(1,1,0) # enable, SATA, LSI
        return REBOOT
    if sys.platform == 'win32':
        cmd = 'HyperWin'
    else:
        cmd = 'hypercfg'
    if restore:
        count = systemMgr.countProperty(0,'Disk','device',{"type":"SWRAID"})
        if count == 0:
            return ATSTATE
        # Clear logical volume
        args = '%s /DEL /l0'%cmd
        status, output = getstatusoutput("%s"%args)
        #if status:
        #    print "'%s' failed: status = 0x%x, %s"%(args,status,output)
        #    addLogRecord(0,__file__, ERROR,"'%s' failed: status = 0x%x, %s"%(args,status,output))
        #    return FAILURE
        print "SATA RAID logical volume 0 delete dusing hypercfg/HyperWin"
        addLogRecord(0,__file__, INFO,"SATA RAID logical volume 0 delete dusing hypercfg/HyperWin")
        from detectBIOSRAID import setSWRAID
        setSWRAID(0,1,0) # disable, SATA, LSI
        return ATSTATE

    print "running an example of configuring a RAID using hypercfg/HyperWin ..."

    # Clear previous RAID volumes and configuration information and creates RAID1
    # on ports #0 and #1 without asking for user input.
    args = '%s /AM:00,10 /S'%cmd
    status, output = getstatusoutput("%s"%args)
    if status == 10: # - Hard disks not found
        print "insufficent disks to demo RAID"
        addLogRecord(0,__file__, INFO,"insufficent disks to demo RAID")
        return ATSTATE
    if status:
        print "'%s' failed: status = 0x%x, %s"%(args,status,output)
        addLogRecord(0,__file__, ERROR,"'%s' failed: status = 0x%x, %s"%(args,status,output))
        return FAILURE
    # Initialize the New Volume
    args = '%s /INIT /l0'%cmd
    status, output = getstatusoutput("%s"%args)
    if status:
        print "'%s' failed: status = 0x%x, %s"%(args,status,output)
        addLogRecord(0,__file__, ERROR,"'%s' failed: status = 0x%x, %s"%(args,status,output))
        return FAILURE

    print "SATA RAID successfully created using hypercfg/HyperWin"
    addLogRecord(0,__file__, INFO,"SATA RAID successfully created using hypercfg/HyperWin")
    
    # Set Disk Cache options for Port#0 and Port #1
    args = '%s /SETPD /C0 /wc-on /ra-on'%cmd
    status, output = getstatusoutput("%s"%args)
    if status:
        print "'%s' failed: status = 0x%x, %s"%(args,status,output)
        addLogRecord(0,__file__, ERROR,"'%s' failed: status = 0x%x, %s"%(args,status,output))
        return FAILURE
    args = '%s /SETPD /C1 /wc-on /ra-on'%cmd
    status, output = getstatusoutput("%s"%args)
    if status:
        print "'%s' failed: status = 0x%x, %s"%(args,status,output)
        addLogRecord(0,__file__, ERROR,"'%s' failed: status = 0x%x, %s"%(args,status,output))
        return FAILURE

    return REFRESH

if __name__ == '__main__':
    """
    this script assumed these enviroment variables have been set:
    (see deploy.sh or deploy.cmd)
        DeployPath
        DeploymentStore
        PATH
        LD_LIBRARY_PATH (Linux)
        PYTHONPATH
    """
    if len(sys.argv) == 1:
        restore = 0
    elif len(sys.argv) == 2 and sys.argv[1] == '0':
        restore = 0
    else:
        restore = 1
    state = CreateVolume(restore)
    if state == REBOOT:
        print 'hypercfg/HyperWin requires a reboot to effect BIOS RAID configuration changes'
        addLogRecord(0,__file__, INFO,'hypercfg/HyperWin requires a reboot to effect BIOS RAID configuration changes')
    elif state == REFRESH:
        os.system('prepareDeployment refresh')
        state = ATSTATE
    if state == ATSTATE:
        print 'hypercfg/HyperWin CreateVolume succeeded'
        addLogRecord(0,__file__, INFO,'hypercfg/HyperWin CreateVolume succeeded')
    sys.exit(state)
