#!/usr/bin/env python
#
# Copyright (C) EMC Corporation. All rights reserved.
#
# This script parses the output of "isi upgrade nodes list" for errors,
# parses the agent log for hook names, makes assumptions about the
# corresponding node local log names, and copies the logs to a known
# location on /ifs.  If anything changes in these areas, the script is
# likely to fail as it is highly sensitive to the format of the data.
#

import sys
import subprocess
import shutil
import os
import getopt
import glob
import json
import tarfile
from isi.misc import sysctlbyname
import isi.config.arrcfg as acfg

BASEPATH = '/ifs/.ifsvar/upgrade'
DEST = '/ifs/upgrade_error_logs/'
LOGPATH = '/var/ifs/upgrade/logs/'
HOOKPATH = LOGPATH + 'hooklogs/'
AGENTPATH = LOGPATH + 'agentlogs/'
PACKAGEPATH = LOGPATH + 'packagelogs/'
ADIR = "/ifs/.ifsvar/upgrade/assessments"
ARCHDIR = "/ifs/.ifsvar/upgrade/archive"

logs = {'InstallFile': AGENTPATH + 'isi_Upgrade_Agent.py-InstallFile.log',
        'Reboot': AGENTPATH + 'isi_Upgrade_Agent.py-Reboot.log',
        'InstallFirmware': AGENTPATH + 'isi_Upgrade_Agent.py-InstallFirmware.log',
        'VerifyFirmware': AGENTPATH + 'isi_Upgrade_Agent.py-VerifyFirmware.log',
        'AssessFirmware': AGENTPATH + 'isi_Upgrade_Agent.py-AssessFirmware.log',
        'InitPackage': PACKAGEPATH + 'isi_patch_agent-INIT.log',
        'CheckPackage': PACKAGEPATH + 'isi_patch_agent-CHECKS.log',
        'ExecutePackage': PACKAGEPATH + 'isi_patch_agent-EXECUTE.log',
        'CommitPackage': PACKAGEPATH + 'isi_patch_agent-COMMIT.log',
        'ServicesStop': PACKAGEPATH + 'isi_patch_agent-SVCSTOP.log',
        'ServicesStart': PACKAGEPATH + 'isi_patch_agent-SVCSTART.log',
        'RollbackPackage': PACKAGEPATH + 'isi_patch_agent-ROLLBACK.log',
        'AbortPackage': PACKAGEPATH + 'isi_patch_agent-ABORT.log',
        'SyncPackage': PACKAGEPATH + 'isi_patch_agent-SYNC.log',
        'RepairPackage': PACKAGEPATH + 'isi_patch_agent-REPAIR.log',
}

def copy_log(node, src, dest):
    cmd = ["/usr/bin/isi_for_array", "-n", node, "/bin/cp",  src, dest]
    if sysctlbyname('isi.compliance') == 1:
        cmd = ['sudo'] + cmd
    rc = subprocess.call(cmd)
    if rc == 0:
        print "Copied log to", dest
    else:
        print "Failed to copy log from node ", node, "at ", src

def parse_hook(hook_line):
    hook = None
    path = None

    idx = hook_line.find("Hook Failed")
    # print "line: ", hook_line
    if hook_line != None:
        hook_line = hook_line[idx:]
        idx = hook_line.find("[")
        if idx != -1:
            hook_line = hook_line[idx+1:]
            idx = hook_line.find("]")
            if idx != -1:
                hook_line = hook_line[:idx]
                # print hook_line
                idx = hook_line.rfind("/")
                if idx != -1:
                    hook = hook_line[idx+1:]
                    path = hook_line[:idx]
                    # print "path: ", path
                    idx = path.rfind("/")
                    if idx != -1:
                        path = path[idx+1:]
                        if len(hook) == 0 or len(path) == 0:
                            hook = None
                            path = None
                        # else:
                        #     print "hook: ", hook
                        #     print "path: ", path
    return (hook, path)

def process_hook_error_file(log):
    rval = dict()
    hook = None
    path = None
    f = open(log, 'r')
    for log_line in f:
        fullpath = None
        destination = None
        hook = None
        path = None
        # print "line: ", log_line
        if log_line.find("Hook Failed") != -1:
            (hook, path) = parse_hook(log_line)
        if hook != None and path != None:
            rval[hook] = path
    if len(rval) > 0:
        return rval

def process_error(node, step, log):
    # print "node: ", node
    # print "step:", step
    # print "log:", log
    fullpath = None
    destination = None
    found = False

    if step[:4] == 'Hook':
        # print "is a hook"
        hook = None
        path = None
        f = open(log, 'r')
        for log_line in f:
            fullpath = None
            destination = None
            hook = None
            path = None
            # print "line: ", log_line
            if log_line.find("Hook Failed") != -1:
                (hook, path) = parse_hook(log_line)
            if hook != None and path != None:
                fullpath = HOOKPATH + path + "/" + hook + ".log"
                destination = DEST + node + "_" + path + "_" + hook + ".log"
                # print "fullpath: ", fullpath
                # print "destination: ", destination
                found = True
                copy_log(node, fullpath, destination)
    else:
        # print "not a hook"
        if step in logs:
            fullpath = logs[step]
            idx = fullpath.rfind("/")
            if idx != -1:
                log = fullpath[idx+1:]
                destination = DEST + node + "_" + log
                found = True
                copy_log(node, fullpath, destination)

    if found == False:
        print "Cannot locate log for: node " + node + ", step " + step

def usage(name):
    print "Usage: " + name + " [-a]";
    print "       This utility will pull error logs for an upgrade process"
    print "       -a - to look at assessment logs instead of looking for the current process' errors"

# Used to grovel through the Assessment reports and try and put them and the
# associated logs all in DEST
def preupgrade():
    founderrors = set()
    if not os.path.exists(ADIR):
        print "No Assessments Directory Found"
        sys.exit()

    # Find the latest Assessment Report
    alist = glob.glob(os.path.join(ADIR ,"Assessment-*.report"))
    alist.sort(reverse=True)
    if len(alist) == 0:
        print "No Assessments File Found"
        sys.exit()

    # Pull the Assessment Report into a Json object
    rawreport = [line.rstrip('\n') for line in open(alist[0])]
    rawreport.remove("Summary:")
    report = json.loads(''.join(rawreport).split("----------")[0])

    # Look for a failure in the Report
    try:
        upgradeid = report["Cluster"]["UpgradeUID"]
        print "For Assessment with Upgrade ID [" + upgradeid +"]"
        for node in report["Nodes"]:
            nodedevid = node["Details"]["Devid"]
            nodeguid = node["Details"]["Guid"]
            for k, v in node["Details"]["Commands"].items():
                if v == "Failed":
                    founderrors.add(nodedevid+"-"+nodeguid)
    except KeyError:
        print "Bad JSON document"
        sys.exit()

    if len(founderrors) == 0:
        print "Assessment was successful"
        sys.exit()

    print "Assessment errors found attempting to find and extract logs from archive"

    if not os.path.exists(ARCHDIR):
        print "Assessment Error with no Archive Directory Found"
        sys.exit()

    # Get a list of the Upgrade Archives
    archlist = glob.glob(os.path.join(ARCHDIR, "UpgradeArchive-*.tgz"))

    # Add a mark to so we won't look through all of the archives only the newer ones
    splingstring = ARCHDIR + "/UpgradeArchive-" + upgradeid + ".tgz.MARK"
    archlist.append(splingstring)
    archlist.sort(reverse=True)

    for archfname in archlist:
        if archfname.endswith(".MARK"):
            print "Assessment Error with no Archive file found"
            sys.exit()
        if tarfile.is_tarfile(archfname):
            archbasedir = os.path.basename(archfname).replace(".tgz", "")
            archreportfname = archbasedir + "/Assessment-summary.report"
            archtar = tarfile.open(archfname)
            try:
                archraw = [line.rstrip('\n') for line in archtar.extractfile(archreportfname)]
            except:
                print "Mandatory file [" + archreportfname + "] not found in archive [" + archfname + "], the archive may still be in the process of creation, please try again later"
                sys.exit()

            archraw.remove("Summary:")
            arechreport = json.loads(''.join(archraw).split("----------")[0])
            try:
                Found = False
                if upgradeid == arechreport["Cluster"]["UpgradeUID"]:
                    print "Assessment Archive File found here [" + archfname + "]"
                    Found = True
            except KeyError:
                print "Bad JSON document"
                sys.exit()

            if Found:
                # prepare destination
                shutil.rmtree(DEST, True)
                os.mkdir(DEST, 0775)

                # time to find and extract the error log files

                for entry in archtar.getnames():
                    thisone = False
                    if entry.endswith("Assessment-summary.report"):
                        thisone = True
                    for indicator in founderrors:
                        if entry.endswith("Agent-"+indicator+".log"):
                            systemid = indicator
                            thisone = True

                    if thisone:
                        tarentry = archtar.getmember(entry)
                        tarentry.name = os.path.basename(tarentry.name)
                        print "Extracting [" + entry + "] to [" + DEST + tarentry.name + "]"
                        archtar.extract(tarentry, DEST)
                        values = process_hook_error_file(DEST + tarentry.name)
                        if values != None:
                            for k, v in values.iteritems():
                                nameextract=archbasedir+"/"+systemid+"/logs/hooklogs/"+v+"/"+k+".log"
                                tarentry = archtar.getmember(nameextract)
                                ignore1, ignore2, tarentry.name = tarentry.name.partition("/")
                                print "Extracting [" + nameextract + "] to [" + DEST + tarentry.name + "]"
                                archtar.extract(tarentry, DEST)

                archtar.close()

                print "Suggest running 'grep -ilr error " + DEST + "*' to find the log files with errors"
                setRights()
                sys.exit()

            archtar.close()

def setRights():
    os.chmod(DEST, 0775)
    for root, dirs, files in os.walk(DEST):
        for dir in [os.path.join(root, d) for d in dirs]:
            os.chmod(dir, 0775)
        for file in [os.path.join(root, f) for f in files]:
            os.chmod(file, 0664)

if __name__ == "__main__":
    if not os.path.isdir( BASEPATH ):
        sys.exit()
    if sys.stdin.isatty():
        if len(sys.argv) > 1:
            try:
                opts, args = getopt.getopt(sys.argv[1:], ":a")
            except getopt.GetoptError:
                usage(sys.argv[0])
                sys.exit()

            for opt, arg in opts:
                if opt == '-a':
                    preupgrade()
                    setRights()
                    sys.exit()

            usage(sys.argv[0])
            sys.exit()
        else:
            # print "call myself"
            a = subprocess.Popen(["/usr/bin/isi", "upgrade", "nodes", "list"], stdout=subprocess.PIPE)
            b = subprocess.check_call([sys.argv[0]], stdin=a.stdout)
            a.wait()
    else:
        ErrorFound = False
        node = None
        state = None
        step = None
        log = None

        # prepare destination
        shutil.rmtree(DEST, True)
        os.mkdir(DEST, 0775)
        cluster_cfg = acfg.config_load()

        # print "parse data"
        while True:
            line = sys.stdin.readline()
            if len(line) == 0:
                break
            line = line.rstrip('\n')
            line = line.lstrip()
            # print "line: ", line
            if line.find(":") != -1:
                (left, right) = line.split(':', 1)
                left = left.lstrip()
                left = left.rstrip()
                right = right.lstrip()
                right = right.rstrip()
                # print "left", left, "::", "right", right
                if left == "Node LNN":
                    node = right
                    state = None
                    step = None
                    log = None
                if left == "Node Upgrade State":
                    state = right
                if left == "Error Details":
                    if right.find(":") != -1:
                        (right, step) = right.split(':', 1)
                        step = step.lstrip()
                if left == "Error Log":
                    log = BASEPATH + "/Agent-"
                    for device_cfg in cluster_cfg.devices:
                        if device_cfg.lnn == int(node):
                            log = BASEPATH + "/Agent-" + str( device_cfg.devid ) + "-" + device_cfg.guid + ".log";
                if node != None and state == "error" and step != None and log != None:
                    ErrorFound = True
                    # print "do stuff"
                    process_error(node, step, log)
                    setRights()
                    # reset node so we don't process this one again
                    node = None
        if not ErrorFound:
            print("No Upgrade Process Errors Found, if you ran an assessment"
                  " please use the -a option")
