#!/usr/bin/python

"""
    This module contains the deployment script to create a RAID volume in WinPE and Linux
"""

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

def createS3000Volume(HW, restore):
    STATEVAR='BIOSState'
    SLSIraid = 4
    SMatrixraid = 5
    # temporarily avoid testing Matrix RAID
    os.system("persistVariable update %s=%x"%(STATEVAR,SLSIraid))
    BIOSstate = os.system("persistVariable read %s"%STATEVAR)
    if BIOSstate < 0 or BIOSstate == 0xff00:
        os.system("persistVariable update %s=%x"%(STATEVAR,SMatrixraid))
        BIOSstate = SMatrixraid
    elif sys.platform == 'linux2':
        BIOSstate = BIOSstate >> 8
    if BIOSstate == SMatrixraid:
        import raidcfg32.raidcfg32
        state = raidcfg32.raidcfg32.CreateVolume(restore)
        if state == REFRESH:
            os.system('prepareDeployment refresh')
            state = ATSTATE
        if state != ATSTATE:
            return state
        os.system("persistVariable update %s=%x"%(STATEVAR,SLSIraid))
    else:
        import hypercfg.hypercfg
        state = hypercfg.hypercfg.CreateVolume(restore)
        if state == REFRESH:
            os.system('prepareDeployment refresh')
            state = ATSTATE
        if state != ATSTATE:
            return state
        os.system("persistVariable delete %s"%(STATEVAR))
    return state

def createS5000Volume(HW,restore):
    import cmdtool2.cmdtool2
    state = cmdtool2.cmdtool2.CreateVolume(HW,restore)
    if state == REFRESH:
        os.system('prepareDeployment refresh')
        state = ATSTATE
    return state

def runState(restore=0):
    HW, value = systemMgr.queryProperty(0,'BIOSRAID','HWRAID','Enabled',{})
    count = systemMgr.countProperty(0,'Disk','device',{})
    if count <= 1:
        if not restore:
            print "insufficent disks to demo RAID or a RAID already exists"
            addLogRecord(0,__file__, INFO,"insufficent disks to demo RAID or a RAID already exists")
        return ATSTATE
    ret, system = systemMgr.queryProperty(0,'IntelBIOSID','BoardFamilyID','',{})
    if not system or ret:
        print 'Intel board family unavailable: ret = %d'%ret
        addLogRecord(0,__file__, ERROR,'Intel board family unavailable: ret = %d'%ret)
        return FAILURE
    if system == 'S3000':
        if HW:
            # HW RAID is not avaliable on S3000 systems
            return ATSTATE
        state = createS3000Volume(HW, restore)
    elif system == 'S5000':
        state = createS5000Volume(HW, restore)
    else:
        return FAILURE
    if state == REFRESH:
        import time
        time.sleep(5)
        os.system('prepareDeployment refresh')
    return state

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
    and this script has been run:
        prepareDeployment
    and BIOS RAID settings are set
    """
    
    if len(sys.argv) == 1:
        restore = 0
    elif len(sys.argv) == 2 and sys.argv[1] == '0':
        restore = 0
    else:
        restore = 1
    while 1:
        print "This script will create a RAID on the first 2 disks found in the system"
        print "Data will be lost. Do you want to continue? (Y)"
        next = sys.stdin.read(1)            # read a one-character string
        if next == 'Y' or next == 'y':
            break
        sys.exit()
    state = runState(restore)
    if state == REBOOT:
        print "System must be reset to complete the RAID volume"
    sys.exit(state)
