#!/usr/bin/env python
#==============================================================
#
#  Abs:   Script for checking the consistency of screeniocs
#
#  Name:  checkIOCDirs.py
#
#  Rem:   This script looks at HIOCs, VIOCs, and SIOCs in
#         screeniocs and ensures that their corresponding
#         directories exist in $EPICS_IOCS and $IOC_DATA.
#
#  Facility: SLAC Controls
#
#  Auth:  19-Aug-2019, A. Le    (anle)
#--------------------------------------------------------------
#  Mods:  (Latest to oldest)
#
#==============================================================
#
import os
import re
import subprocess
import smtplib
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

scriptPath = os.path.expandvars("$TOOLS/script")
sys.path.append(scriptPath)
from parseScreenIOCS import parseScreenIOCs

# IOC format
iocPattern = r'^(?:[a-z]?ioc|cpu)-[a-z0-9]+-[a-z0-9]+$'
iocTypes = [
    'HIOC',
    'VIOC',
    'SIOC'
]

# Email
facility = os.path.expandvars("$FACILITY")
sender = 'anle@slac.stanford.edu'
recipient = 'controls-system-admins@slac.stanford.edu;controls-deputy-reports@slac.stanford.edu'
subject = "IOC directory consistency check - {}".format(facility)
emailTemplate = 'checkIOCDirsTemplate'
conditionDelimiter = '!@#$'

# Paths
iocOwner = os.path.expandvars("$IOC_OWNER")
iocScreenPath = os.path.expandvars("$IOC_SCREEN")
epicsIOCsPath = os.path.expandvars("$EPICS_IOCS")
iocDataPath = os.path.expandvars("$IOC_DATA")
screenIOCsFileName = os.path.join(iocScreenPath, 'screeniocs')
startupCmd = 'startup.cmd'
iocSpecificRelease = 'iocSpecificRelease'
iocDataReqDirs = [
    'iocInfo',
    'autosave',
    'autosave-req',
    'archive',
    'restore',
    'yaml'
]
iocMainDirs = [
    epicsIOCsPath,
    iocDataPath
]
etcDirs = [
    '/etc/init.d',
    '/etc/rc3.d'
]
epicsIOCsLinks = [
    startupCmd,
    iocSpecificRelease
]

# Other
checkMark = '<font color="green">&#10004;</font>'
xMark = '<font color="red">&#10008;</font>'

def remoteFileReadLink(user, host, fileName):
    '''Returns the file that <fileName> points to on <host> via SSH'''
    pythonCommand = 'python -c "import os\nif os.path.islink(\'{0}\'):\n\tprint(os.readlink(\'{0}\'))"'.format(fileName)
    return execRemoteCommand(user, host, pythonCommand).rstrip()

def remoteFileExists(user, host, fileName):
    '''Checks if <fileName> exists on <host> via SSH'''
    pythonCommand = 'python -c "import os\nprint(os.path.exists(\'{}\'))"'.format(fileName)
    return execRemoteCommand(user, host, pythonCommand).rstrip()

def execRemoteCommand(user, host, command):
    '''Executes <command> on <host> via SSH'''
    sshArgs = ['ssh', '-x', '-o', 'BatchMode=yes', '-l', user, host]
    
    # Connect to remote server
    proc = subprocess.Popen(sshArgs,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, stderr = proc.communicate(command)
    return stdout.decode('utf-8')

def checkAutoRestart(siocInfo):
    '''Verifies that the following conditions hold on the daemon:

         - /etc/init.d/st.<siocName> exists
         - /etc/rc3.d/S98st.<siocName> exists and points to /etc/init.d/st.<siocName>

       where <siocName> = <siocInfo>['name'].

       Returns a dict whose keys are the files and values are whether they exist.
    '''
    # Make sure input is an SIOC
    if (siocInfo['type'] != 'SIOC'):
        raise TypeError("Not an SIOC")

    # Verify auto-restart scripts exist
    result = {}
    siocName = siocInfo['name']
    siocHost = siocInfo['screenhost']
    initdScriptPath = os.path.join('/etc/init.d', 'st.{}'.format(siocName))
    rc3dScriptLink = os.path.join('/etc/rc3.d', 'S98st.{}'.format(siocName))
    rc3dScriptLinkTo = remoteFileReadLink(iocOwner, siocHost, rc3dScriptLink)
    result['/etc/init.d'] = remoteFileExists(iocOwner, siocHost, initdScriptPath)
    result['/etc/rc3.d'] = rc3dScriptLinkTo == initdScriptPath

    return result

def checkIOCDataReqDirs(iocName):
    '''Verifies that the following directories exist in $IOC_DATA/<iocName>:

         - iocInfo
         - autosave
         - autosave-req 
         - restore
         - yaml

       Returns a dict whose keys are the folders and values are whether they exist.
    '''
    result = {}
    for reqDir in iocDataReqDirs:
        iocDataSubFolder = os.path.join(iocDataPath, iocName, reqDir)
        result[reqDir] = os.path.isdir(iocDataSubFolder)

    return result

def checkIOCMainDirs(iocName):
    '''Verifies that the following directories exist:
         - $EPICS_IOCS/<iocName>
         - $IOC_DATA/<iocName>

       Returns a dict whose keys are the folders and values are whether they exist.
    '''
    result = {}
    for mainDir in iocMainDirs:
        iocMainFolder = os.path.join(mainDir, iocName)
        result[mainDir] = os.path.isdir(iocMainFolder)

    return result

def checkEpicsIOCsLinks(iocName):
    '''Verifies that the following items exist in $EPICS_IOCS/<iocName>:

         - startup.cmd
         - epicsSpecificRelease
         - iocSpecificRelease

       Returns a dict whose keys are the files and values are whether they exist.
    '''
    result = {}
    startupCmdPath = os.path.join(epicsIOCsPath, iocName, startupCmd)
    iocSpecificReleaseLink = os.path.join(epicsIOCsPath, iocName, iocSpecificRelease)
    result[startupCmd] = os.path.isfile(startupCmdPath)
    result[iocSpecificRelease] = os.path.islink(iocSpecificReleaseLink)

    return result

def makeTableRow(data):
    '''Generates a table row in HTML from a list of data'''
    rowStr = "<tr>\n"
    for i, entry in enumerate(data):
        # Do not center the first cell
        if i == 0:
            rowStr += '<td>{}</td>\n'.format(entry)
        else:
            rowStr += '<td align="center">{}</td>\n'.format(entry)

    rowStr += "</tr>\n"
    return rowStr

def sendEmail(fromEmailAddr, toEmailAddr, subject, message):
    '''Sends an email'''
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['To'] = toEmailAddr
    msg['From'] = fromEmailAddr
    msg.attach(MIMEText(message, 'html'))
    if facility == 'Dev':
        smtpHost = 'smtp.slac.stanford.edu'
    else:
        smtpHost = 'localhost'
    s = smtplib.SMTP(smtpHost)
    s.sendmail(fromEmailAddr, toEmailAddr, msg.as_string())
    s.quit()

def sendReportToControls(iocMainDirTable, iocDataReqDirTable, iocsNotInScrenIOCsTable, iocTotalsTable, iocsNotInScreenIOCsCount):
    '''Send controls-cd an email containing

         - a table of IOCs in screeniocs that are missing $EPICS_IOCS
             directories, $IOC_DATA directories, or auto-restart scripts
             (SIOCs only)
         - a table of IOCs in screeniocs that are missing iocInfo, autosave,
             autosave-req, restore, or yaml in their corresponding $IOC_DATA
             directories
         - a table of IOCs NOT in screeniocs that still have directories
             in $EPICS_IOCS and/or $IOC_DATA
         - a table of IOC counts in screeniocs by type and status
    '''
    substDict = {
        'FACILITY': facility,
        'IOC_TOTALS_TABLE': iocTotalsTable,
        'IOC_MAIN_DIRS_TABLE': iocMainDirTable,
        'IOC_DATA_REQ_DIRS_TABLE': iocDataReqDirTable,
        'IOCS_NOT_IN_SCREENIOCS_TABLE': iocsNotInScreenIOCsTable,
        'IOCS_NOT_IN_SCREENIOCS_COUNT': iocsNotInScreenIOCsCount
    }
    condDict = {
        'MAIN_DIRS_TEST_FAILED': iocMainDirTable,
        'IOC_DATA_TEST_FAILED': iocDataReqDirTable,
        'IOCS_NOT_IN_SCREENIOCS_EXIST': iocsNotInScreenIOCsTable
    }
    content = parseTemplate(emailTemplate, condDict)

    body = content.format(**substDict)
    sendEmail(sender, recipient, subject, body)

def makeIOCCountTable(iocCounts):
    '''Generates a table of IOC counts by type and status'''
    totalPassed = 0
    totalFailed = 0
    for iocType in iocTypes:
        rowData = [iocType]
        passed = iocCounts[iocType]['passed']
        failed = iocCounts[iocType]['failed']
        total = passed + failed
        rowData.append(passed)
        rowData.append(failed)
        rowData.append(total)
        tableRow = makeTableRow(rowData)
        iocCountsRows.append(tableRow)
        totalPassed += passed
        totalFailed += failed

    grandTotal = totalPassed + totalFailed
    totalRowData = ['Total', totalPassed, totalFailed, grandTotal]
    tableRow = makeTableRow(totalRowData)
    iocCountsRows.append(tableRow)

    return ''.join(iocCountsRows)

def listIOCsInDir(pattern, folder):
    '''Lists all the IOC directories matching <pattern> in <folder>'''
    allDirs = next(os.walk(folder))[1]
    matchingDirs = [name for name in allDirs if pattern.match(name)]

    return matchingDirs

def parseTemplate(templateFileName, condDict):
    '''Parses a template file and filters out certain text when directed.
       Example template:

           This text is always visible.
           !@#$CONDITION
           This text is only visible when condDict[CONDITION] is true.
           $#@!
    '''
    content = ""
    with open(templateFileName, 'r') as f:
        condMode = False
        key = ''
        for line in f:
            if condMode:
                if line.startswith(conditionDelimiter[::-1]):
                    condMode = False
                elif key in condDict and condDict[key]:
                    content += line
            elif line.startswith(conditionDelimiter):
                condMode = True
                key = line.split(conditionDelimiter)[1].rstrip()
            else:
                content += line

    return content

if __name__ == '__main__':
    # Grab all the IOCs
    iocMainDirRows = []
    iocDataReqDirRows = []
    iocCountsRows = []
    iocCounts = {}
    iocInfos = parseScreenIOCs(screenIOCsFileName)
    pattern = re.compile(iocPattern)

    # Initialize IOC counts
    for iocType in iocTypes:
        iocCounts[iocType] = {}
        iocCounts[iocType]['passed'] = 0
        iocCounts[iocType]['failed'] = 0

    # Get IOCs in $EPICS_IOCS and $IOC_DATA
    iocsInEpicsIOCs = listIOCsInDir(pattern, epicsIOCsPath)
    iocsInIOCData = listIOCsInDir(pattern, iocDataPath)

    # Iterate through the IOCs
    for iocName in sorted(iocInfos):
        iocInfo = iocInfos[iocName]
        iocType = iocInfo['type']
        if pattern.match(iocName):
            # Check if directories exist in $EPICS_IOCS and $IOC_DATA
            mainFolders = [os.path.join(mainDir, iocName) for mainDir in iocMainDirs]
            subFolders = [os.path.join(iocDataPath, iocName, subDir) for subDir in iocDataReqDirs]
            mainFoldersExist = [os.path.isdir(mainFolder) for mainFolder in mainFolders]
            subFoldersExist = [os.path.isdir(subFolder) for subFolder in subFolders]

            # Check if startup.cmd and iocSpecificRelease exist in
            # $EPICS_IOCS/<iocName>
            linksDict = checkEpicsIOCsLinks(iocName)
            linksExist = [linksDict[link] for link in epicsIOCsLinks]

            if facility != 'Dev' and iocType == 'SIOC':
                # Additionally, verify that SIOC auto-restart scripts exist
                autoRestartDict = checkAutoRestart(iocInfo)
                mainTestsPassed = all(mainFoldersExist + linksExist + autoRestartDict.values())
            else:
                mainTestsPassed = all(mainFoldersExist + linksExist)
            if not mainTestsPassed:
                rowData = [iocName] + [checkMark if exists else xMark for exists in mainFoldersExist]
                rowData.extend(checkMark if exists else xMark for exists in linksExist)
                if facility != 'Dev' and iocType == 'SIOC':
                    autoRestartExists = [autoRestartDict[etcDir] for etcDir in etcDirs]
                    rowData.extend(checkMark if exists else xMark for exists in autoRestartExists)
                else:
                    rowData.append("N/A")
                    rowData.append("N/A")
                tableRow = makeTableRow(rowData)
                iocMainDirRows.append(tableRow)

            # Check if iocInfo, autosave, autosave-req, restore, and yaml
            # exist in $EPICS_DATA/<iocName>
            subTestsPassed = all(subFoldersExist)
            if not subTestsPassed:
                rowData = [iocName] + [checkMark if exists else xMark for exists in subFoldersExist]
                tableRow = makeTableRow(rowData)
                iocDataReqDirRows.append(tableRow)

            # Update IOC counts
            if mainTestsPassed and subTestsPassed:
                iocCounts[iocType]['passed'] += 1
            else:
                iocCounts[iocType]['failed'] += 1

    # Find IOCs in $EPICS_IOCS and $IOC_DATA that are not in screeniocs
    iocsNotInScreenIOCsRows = []
    iocsNotInScreenIOCs = set(iocsInEpicsIOCs + iocsInIOCData) - set(iocInfos)
    iocsNotInScreenIOCsCount = len(iocsNotInScreenIOCs)
    for iocName in sorted(iocsNotInScreenIOCs):
        extraFoldersExist = [iocName in iocList for iocList in [iocsInEpicsIOCs, iocsInIOCData]]
        rowData = [iocName] + [checkMark if exists else xMark for exists in extraFoldersExist]
        tableRow = makeTableRow(rowData)
        iocsNotInScreenIOCsRows.append(tableRow)

    # Put rows together into tables
    iocMainDirTable = ''.join(iocMainDirRows)
    iocDataReqDirTable = ''.join(iocDataReqDirRows)
    iocsNotInScreenIOCsTable = ''.join(iocsNotInScreenIOCsRows)

    # Make table of IOC counts
    iocCountTable = makeIOCCountTable(iocCounts)

    # Send email to controls-cd
    sendReportToControls(iocMainDirTable, iocDataReqDirTable, iocsNotInScreenIOCsTable, iocCountTable, iocsNotInScreenIOCsCount)
