#!/usr/local/bin/python
#
# Copyright C 2017 Dell Inc. or its subsidiaries. All Rights Reserved.

"""Isilon PSI Tool utility for manipulating PSI files on node"""

import argparse
import json
import re
import os
import pprint
import shutil
import sys

import isi.hw.bmc_nvram as bmc_nvram
import isi.hw.bootstrap as bootstrap
import isi.hw.util as hwutil

# NB: "By definition," do not use PSI or PSI vars in this tool!

# TRIAL_RUN: Do not actually _do_ anything, just walk through the logic
# and report to console _what_ we would normally do, for debug purposes.
# pylint: disable=W0603
TRIAL_RUN = False

# UNIT_TEST: Do not ever set this. EXCEPT for the unit test itself.
# This enables alternate mfg partition files.
UNIT_TEST = False

def set_trial_run(value):
    """Sets the TRIAL_RUN debug flag"""
    global TRIAL_RUN
    TRIAL_RUN = value
    bmc_nvram.set_trial_run(value)

def trial_print(msg, newline=True, pobj=None, indent=4, level=1):
    """Optionally displays a debug/trial run message, depending on level"""
    if not (TRIAL_RUN >= level):
        return
    if newline > 0:
        print("TRIAL: %s" % msg)
    elif newline < 0:
        # End previous newline=False messaging...
        print("%s" % msg)
    elif pobj:
        sys.stdout.write("%s" % msg)
    else:
        sys.stdout.write("TRIAL: %s" % msg)
    if pobj:
        pprint.pprint(pobj, indent=indent)

# The major/minor value for PSI on this branch of OneFS is 1.0
# (Note: During UNIT_TEST, only, use special case: 0, 0)
MAJOR_VALUE = 1
MINOR_VALUE = 0

def get_tool_version():
    if not UNIT_TEST:
        return (MAJOR_VALUE, MINOR_VALUE)
    return (0, 0)

def get_tool_version_string():
    return "%s.%s" % get_tool_version()

def filter_files_tool_version(files, filename):
    major, minor = get_tool_version()
    def ffunc(finfo):
        return (finfo.get('file') == filename and
                finfo.get('major') == major and
                finfo.get('minor' ) == minor)
    return filter(ffunc, files or [])

CONF_REGEX = re.compile("^\d{3}-(?P<major>\d+)\.(?P<minor>\d+)-"
                        "(?P<top_dir>[a-zA-Z]*)-"
                        "(?P<secondary_dir>[a-zA-Z0-9]*)-.*")

MODEL_BOM_REGEX = re.compile("^\d{3}-(?P<top_dir>[a-zA-Z]*)-"
                        "(?P<secondary_dir>[a-zA-Z]*)-.*")
PSI_CONF_SUFFIX = "-psi"

ROOT_DIR = os.path.sep

MFG_PSI_DIR_PATH = hwutil.mfg_dir_path("psi")
MFG_PSI_RECEIPT_PATH = hwutil.mfg_dir_path("psi", "receipt.json")

PSI_CONF_D_DIR_PATH = os.path.join(ROOT_DIR, 'etc', 'psi.conf.d')
ONEFS_CONF_DIR_PATH = os.path.join(ROOT_DIR, 'etc', 'psi')
UTILITY_DIR = "utility"

RECEIPT_REQUIRED_KEYS = [
    "BE",
    "FE",
    "compute",
    "sled",
    "powersupply",
]

RECEIPT_MODEL_OPTIONAL_KEYS = [
    "ssd",
]

RECEIPT_OPTIONAL_KEYS = [
    "CHASSIS",
    "ssd",
]

ONEFS_INSTALL = False

# The regexes in FILENAME_REGEX are intended to match model names and
# model subcomponents. From each of these filename formats, we want to
# extract the relevant information to commonize our PSI config filename.
#
# Model:
#
# 001-1.0-iBEIO-10GBE-psi.conf
# 001-1.0-iBEIO-40GBE-psi.conf
# 001-1.0-iBEIO-IBQDR-psi.conf
# ...
# For these files, we will want to extract the 'BEIO' piece.
#
# Model subcomponent:
#
# 001-1.0-infinity-compute-high-psi.conf
# 001-1.0-infinity-compute-med-psi.conf
# 001-1.0-infinity-compute-low-cDVT-psi.conf
# 001-1.0-infinity-compute-turbo-psi.conf
# ...
# For these files, we will want to extract 'compute'
#
# Once the relevant piece is extracted from the string, we repiece this
# to look as such:
# 001-<value>-psi.conf
#
# For example:
#
# 001-compute-psi.conf
# 001-BEIO-psi.conf
FILENAME_REGEX = [
    # Intended to match: 001-1.0-iBEIO-10GBE-psi.conf
    re.compile("^(?P<prefix>\d+)-(?P<major>\d+)\.(?P<minor>\d+)-"
               "[a-zA-Z]{1}(?P<name>[^-]+(IO|io))-.+-psi.conf$"),
    # Intended to match: 001-1.0-infinity-utility-discretetable-psi.conf
    # This regex comes before the last "generic" one because it
    # would match on "utility" files
    re.compile("^(?P<prefix>\d+)-(?P<major>\d+)\.(?P<minor>\d+)-"
               "[^-]+-utility-(?P<name>[^-]+)-.*-?psi.conf$"),
    # Intended to match: 001-1.0-infinity-compute-high-psi.conf
    re.compile("^(?P<prefix>\d+)-(?P<major>\d+)\.(?P<minor>\d+)"
               "-[^-]+-(?P<name>[^-]+)-.+-psi.conf$"),
    # Intended to match: 001-1.0-ICH-DEEP-psi.conf
    re.compile("^(?P<prefix>\d+)-(?P<major>\d+)\.(?P<minor>\d+)-"
               "[a-zA-Z]{1}(?P<name>[^-]+)-.+-psi.conf$"),
]

def copy_config_file(src_path, dst_path, filename, filerev):
    """Copy the given PSI config file to the desired location"""
    if not os.path.exists(src_path):
        print("Failed to write config file '%s' - does not exist" % src_path)
        return 1
    dirname = os.path.dirname(dst_path)
    if hwutil.makedirs(dirname):
        print("Failed to write config file '%s' - cannot create dirs" %
              dst_path)
        return 1
    basename = os.path.basename(dst_path)
    print("Writing config file '%s' (revision %s) as '%s' to %s" %
          (filename, filerev, basename, dirname))
    if TRIAL_RUN:
        trial_print("copy_config_file: copy(src=%s, dst=%s)" %
                    (src_path, dst_path))
    else:
        shutil.copy(src_path, dst_path)
    return 0

def read_json(filepath):
    """Return the given JSON file info as a dictionary"""
    trial_print("read_json(%s)" % (filepath), level=2)
    obj = {}
    with open(filepath, "r") as filep:
        obj = json.load(filep)
    return obj

def get_key_from_file(path, key):
    """Return the value from the given file path from the given key"""
    trial_print("get_key_from_file(%s, %s): " % (path, key),
                newline=False, level=2)
    value = None
    value_regex = re.compile("^#\s*%s\s*=\s*(?P<value>\d+)$" % key)
    if os.path.exists(path):
        with open(path, "r") as fdesc:
            lines = fdesc.readlines()
        for line in lines:
            match = value_regex.match(line)
            if match:
                value = int(match.group("value"))
                break
    trial_print("%s" % (value), newline=-1, level=2)
    return value

def receipt_path_on_mfg_partition(receipt_path, strict=True):
    """Returns True if Receipt file path is on Mfg partition"""
    if not receipt_path:
        return False
    # This is the original "stricter" check, explicit equality:
    if strict:
        return receipt_path == MFG_PSI_RECEIPT_PATH
    # Here is a looser check, verifying that the receipt path is on the
    # mfg partition and dir (/mfg/psi), but allow alt receipt filenames
    return receipt_path.startswith(MFG_PSI_DIR_PATH)

def valid_receipt_path(receipt_path):
    """Return True if the given receipt path is valid"""
    # If and only if UNIT TESTING, allow non-mfg partition file paths
    if not receipt_path:
        print("Failed to get PSI files for %s" % (receipt_path))
        return False
    if not UNIT_TEST:
        if not receipt_path_on_mfg_partition(receipt_path):
            if hwutil.is_mfg_dir_path(receipt_path):
                print("Failed to get PSI files for %s: "
                      "non-standard PSI receipt files not supported" %
                      (receipt_path))
            else:
                print("Failed to get PSI files for %s: "
                      "files not on the mfg partition not supported" %
                      (receipt_path))
            return False
    return True

def get_receipt_info(path):
    """Return the given PSI Receipt file info as a dictionary,
    or None if unable to get receipt info"""
    trial_print("get_receipt_info(%s)" % (path), level=2)
    if not valid_receipt_path(path):
        print("Invalid PSI receipt path %s" % path)
        return None
    if receipt_path_on_mfg_partition(path, strict=False):
        # Normally we expect mfg partition to be mounted when called,
        # but if not, go ahead and attempt mount anyway
        if not hwutil.mfg_is_mounted():
            if hwutil.mount_mfg() or not hwutil.mfg_is_mounted():
                print("PSI receipt path on unmounted mfg partition; "
                      "mount attempt failed")
                return None
        if not os.path.exists(path):
            print("PSI receipt path does not exist in mfg partition")
            return None
    if not os.path.exists(path):
        print("PSI receipt path %s does not exist" % path)
        return None
    obj = read_json(path)
    return obj

def get_path_info(path, conf_file, top_dir, sec_dir=None):
    """Return the given file path information as a dictionary"""
    return {
        "dir" : top_dir,
        "sec_dir" : sec_dir,
        "path" : path,
        "revision" : get_key_from_file(path, "revision"),
        "major" : get_key_from_file(path, "major"),
        "minor" : get_key_from_file(path, "minor"),
        "file" : conf_file,
    }

def get_platform_utility_filepaths(base_dir, receipt):
    """Get the platform utility filepaths for the given receipt"""
    trial_print("get_platform_utility_filepaths(%s, %s):" % (base_dir, receipt))
    paths = []
    utility_dirs = []
    # Utility files will live in the same directory as the
    # components under the MODEL BOM.
    model_bom = receipt.get("MODEL_BOM")
    if model_bom:
        for _, filename in model_bom.iteritems():
            dir_to_add = {}
            match = MODEL_BOM_REGEX.match(filename)
            if match:
                # top_dir should point to a platform directory.
                # i.e "infinity". Under the platform directory, we
                # should have a utility directory which will contain
                # the necessary platform specific PSI config files.
                top_dir = match.group("top_dir")
                dir_to_add = {
                    'path' : os.path.join(base_dir, top_dir, UTILITY_DIR),
                    'top_dir' : top_dir,
                }
                if dir_to_add not in utility_dirs:
                    utility_dirs.append(dir_to_add)
    # Given each of the possible utility directorys, look for all the
    # files in the directory and build a dictionary similar to the one
    # generated in get_psi_config_filepaths(). This should be enough
    # information for get_psi_file_copy_info() and move_psi_files() to
    # do the right thing.
    for util_dir in utility_dirs:
        path = util_dir.get("path")
        try:
            files = os.listdir(path)
        except OSError:
            print "Unable to list files in directory: %s" % path
            continue
        for fname in files:
            filepath = os.path.join(path, fname)
            if validate_file(filepath):
                paths.append(get_path_info(filepath, fname,
                                           util_dir.get("top_dir")))
    trial_print("get_platform_utility_filepaths(%s, %s): paths:" %
                (base_dir, receipt), pobj=paths, indent=6, level=3)
    return paths

def validate_file(path):
    """Validate the given file path is a valid PSI config file"""
    trial_print("validate_file(%s)" % (path), level=2)
    _, filename = os.path.split(path)
    if get_key_from_file(path, "major") is None:
        return False
    if get_key_from_file(path, "minor") is None:
        return False
    if not CONF_REGEX.match(filename):
        return False
    return True

def convert_to_real_filename(filename):
    """Convert the given filename to a real PSI config filename"""
    trial_print("convert_to_real_filename(%s)" % (filename), level=2)
    # The real filename includes the major/minor revision for the
    # file.
    # The filename from the receipt will look like the following:
    # 001-infinity-compute-low-cDVT-psi
    # This needs to be converted to look as such:
    # 001-1.0-infinity-compute-low-cDVT-psi
    filename = filename.split("-")
    filename.insert(1, "%s" % get_tool_version_string())
    filename = '-'.join(filename)
    return filename

def get_psi_config_filepaths(base_dir, receipt, skip_utility=False):
    """Get PSI config file filepaths for given PSI receipt"""
    trial_print("get_psi_config_filepaths(%s, %s):" % (base_dir, receipt))
    # invalid/noexist are indexed by name (i.e. conf without trailing ".conf")
    paths = {'valid': [], 'invalid': {}, 'noexist': {}}
    if not receipt:
        return paths
    for model, info in receipt.iteritems():
        if isinstance(info, dict):
            for sec_dir, conf in info.iteritems():
                if not len(conf):
                    continue
                top_dir = None
                conf = convert_to_real_filename("%s.conf" % conf)
                name = conf[:-5]
                match = CONF_REGEX.match(conf)
                if not match:
                    continue
                top_dir = match.group("top_dir")
                path = os.path.join(base_dir, top_dir, sec_dir, conf)
                if os.path.exists(path):
                    if validate_file(path):
                        paths['valid'].append(get_path_info(path, conf,
                                                            top_dir, sec_dir))
                    else:
                        paths['invalid'][name] = path
                else:
                    paths['noexist'][name] = path
        elif len(info):
            conf = convert_to_real_filename("%s.conf" % info)
            name = conf[:-5]
            path = os.path.join(base_dir, model, conf)
            if os.path.exists(path):
                if validate_file(path):
                    paths['valid'].append(get_path_info(path, conf, model))
                else:
                    paths['invalid'][name] = path
            else:
                paths['noexist'][name] = path
    if not skip_utility:
        util_paths = get_platform_utility_filepaths(base_dir, receipt)
        paths['valid'].extend(util_paths)
    trial_print("get_psi_config_filepaths(%s, %s): paths:" %
                (base_dir, receipt), pobj=paths['valid'], indent=6, level=2)
    return paths

def print_invalid_noexist(paths, names):
    # The same names that were displayed to the user were also used
    # to index the invalid/noexist dicts of paths.
    msg = '%16s path: %s'
    for name in names:
        invalid_path = paths['invalid'].get(name)
        noexist_path = paths['noexist'].get(name)
        if invalid_path:
            print msg % ('Invalid', invalid_path)
        if noexist_path:
            print msg % ('Nonexistent', noexist_path)

def replace_path(prefix, path, key="psi"):
    """Get new modified PSI path"""
    trial_print("replace_path(%s, %s, %s): " % (prefix, path, key),
                newline=False, level=2)
    new_path = ''
    basename = os.path.basename(path)
    while True:
        path, base = os.path.split(path)
        if base == basename:
            continue
        new_path = os.path.join(base, new_path)
        if base == key:
            break
    result = os.path.join(prefix, new_path, basename)
    trial_print("%s" % (result), newline=-1, level=2)
    return result

def get_dst_name(name):
    """Get PSI config file destination name for the given name"""
    trial_print("get_dst_name(%s): " % (name), newline=False, level=2)
    result = None
    value = None
    prefix = None
    if name is None:
        return result
    for regex in FILENAME_REGEX:
        match = regex.match(name)
        if match:
            value = match.group("name")
            prefix = match.group("prefix")
            break
    if value and prefix:
        result = "%s-%s%s.conf" % (prefix, value, PSI_CONF_SUFFIX)
    trial_print("%s" % (result), newline=-1, level=2)
    return result

def get_model_info(fileinfo, write_back=None):
    """Return dictionary of model info for given fileinfo"""
    if write_back is True:
        write_back = replace_path(hwutil.mfg_dir_path(), fileinfo.get("path"))
    return {
        "write_back" : write_back,
        "src" : fileinfo.get("path"),
        "dir" : fileinfo.get("dir"),
        "revision" : fileinfo.get("revision"),
        "file" : fileinfo.get("file"),
        "alt_name" : get_dst_name(fileinfo.get("file")),
    }

def get_psi_file_copy_info(mfg, local=None):
    """Return list of PSI config file model info for files found"""
    if TRIAL_RUN:
        trial_print("get_psi_file_copy_info():")
        trial_print("mfg = ", newline=False, level=2, pobj=mfg)
        if local:
            trial_print("local = ", newline=False, level=2, pobj=local)
    result = []
    # pylint: disable=W0141
    files = sorted(list(set(map(lambda x: x.get('file'), mfg + (local or [])))))
    for fname in files:
        l_files = filter_files_tool_version(local, fname)
        m_files = filter_files_tool_version(mfg, fname)
        if not m_files and not l_files:
            # If we use this func for backup() purposes, then we need to
            # (a) know this alternate use case, and (b) do not stop boot,
            # just return error indicator
            if local is None:
                return None
            stop_boot("No valid copy of PSI config files for %s" % fname)
        elif not m_files and l_files:
            # Only /etc has a correct version, copy to psi.conf.d
            # and write_back to mfg
            model_info = get_model_info(l_files[0], write_back=True)
        elif m_files and not l_files:
            # Only /mfg has a correct version, copy to psi.conf.d
            model_info = get_model_info(m_files[0])
        elif m_files and l_files:
            # If both copies have the correct version, check revision
            if (l_files[0].get("revision") > m_files[0].get("revision")):
                # If local revision is greater than what's on the mfg
                # partition use local as source to copy to
                # /etc/psi.conf.d/ and also overwrite copy in mfg
                model_info = get_model_info(l_files[0],
                                            write_back=m_files[0].get("path"))
            else:
                model_info = get_model_info(m_files[0])
        result.append(model_info)
    trial_print("get_psi_file_copy_info(): result:",
                pobj=result, indent=6, level=2)
    return result

def stop_boot(msg=""):
    """Stop boot (unless install flag is set), optionally displaying msg"""
    if hwutil.mfg_is_mounted():
        hwutil.unmount_mfg()
    msg_shell = [
        '',
        'Please contact EMC Isilon Technical Support for assistance.',
        'Entering zsh shell now.'
        ]
    if len(msg):
        print msg
    # If the OneFS install flag is passed, don't stop boot, but rather just
    # exit
    if ONEFS_INSTALL or TRIAL_RUN:
        sys.exit(0)
    for line in msg_shell:
        print line
    os.system('/usr/local/bin/zsh')

def validate_receipt_to_files(receipt, mfg, local=None):
    """Validate the given receipt to the mfg files"""
    if TRIAL_RUN:
        trial_print("validate_receipt_to_files():")
        trial_print("receipt = ", newline=False, level=2, pobj=receipt)
        trial_print("mfg = ", newline=False, level=2, pobj=mfg)
        if local:
            trial_print("local = ", newline=False, level=2, pobj=local)
    missing = {
        "keys" : [],
        "files": [],
    }
    # pylint: disable=W0141
    mfg_keys = set(map(lambda x: x.get("dir") if not x.get("sec_dir")
        else x.get("sec_dir"), mfg))
    local_keys = set(map(lambda x: x.get("dir") if not x.get("sec_dir")
        else x.get("sec_dir") , local or []))
    model_additional_keys = filter(lambda x: x in receipt["MODEL_BOM"],
        RECEIPT_MODEL_OPTIONAL_KEYS)
    additional_keys = model_additional_keys + filter(lambda x: x in receipt,
        RECEIPT_OPTIONAL_KEYS)
    check_keys = RECEIPT_REQUIRED_KEYS + additional_keys
    for key in check_keys:
        if not key in mfg_keys and not key in local_keys:
            if receipt.get(key):
                name = receipt.get(key)
            else:
                name = receipt["MODEL_BOM"].get(key, "")
            if not len(name) and key in RECEIPT_OPTIONAL_KEYS:
                # If the filename is empty and it's an optional key,
                # move to the next
                pass
            elif not len(name) and key not in RECEIPT_OPTIONAL_KEYS:
                # If the filename is empty and it's not an optional key,
                # append key
                missing["keys"].append(key)
            else:
                # If the filename is non-empty, convert and append
                missing["files"].append(convert_to_real_filename(name))
    return missing

def copy_config_files(src_path, dst_path, receipt_path):
    """Copy PSI config files to the desired location"""
    trial_print("copy_config_files(%s, %s, %s)" %
                (src_path, dst_path, receipt_path))
    result = 0
    # pylint: disable=W0703
    try:
        receipt = get_receipt_info(receipt_path)
    except Exception:
        hwutil.unmount_mfg()
        stop_boot("Chassis is missing a PSI receipt.\nA receipt must be "
               "provided before the boot will be allowed to continue.")
    if receipt is not None:
        mfg_files = get_psi_config_filepaths(MFG_PSI_DIR_PATH, receipt)
        local_files = get_psi_config_filepaths(src_path, receipt)
        if not mfg_files['valid'] and not local_files['valid']:
            stop_boot("Unable to find PSI conf files to configure node")
        missing = validate_receipt_to_files(receipt, mfg_files['valid'],
                                            local_files['valid'])
        if missing.get("keys") or missing.get("files"):
            if missing.get("keys"):
                print ("The following required key(s) are missing from the"
                    " PSI receipt: %s" % ', '.join(missing.get("keys")))
            if missing.get("files"):
                print ("The following PSI config file(s) do not exist "
                    "locally or in mfg: %s" % ', '.join(missing.get("files")))
                print_invalid_noexist(mfg_files, missing.get("files"))
                print_invalid_noexist(local_files, missing.get("files"))
            stop_boot()
        files = get_psi_file_copy_info(mfg_files['valid'], local_files['valid'])
        if files is None:
            print ("PSI conf files contain incorrect version. "
                       "Unable to properly configure node.")
            result = 1
        if not result:
            result = move_psi_files(files, dst_path)
    else:
        stop_boot("Chassis is missing a PSI receipt.\nA receipt must be "
               "provided before the boot will be allowed to continue.")
    return result

def cleanup_psi_conf_d_dir(conf_path=PSI_CONF_D_DIR_PATH):
    """Delete PSI config files found in /etc/psi.conf.d directory"""
    trial_print("cleanup_psi_conf_d_dir()")
    # All 00X files are for config, so we want to remove them from
    # psi.conf.d prior to copying over the new files
    result = 0
    file_regex = re.compile("^00[0-9]-.*")
    if not os.path.exists(conf_path):
        return 1
    try:
        files = os.listdir(conf_path)
    except OSError:
        result = 1
    if not result:
        for fname in files:
            if file_regex.match(fname):
                path = os.path.join(conf_path, fname)
                if TRIAL_RUN:
                    trial_print("cleanup_psi_conf_d_dir: delete(%s)" % path,
                                level=2)
                else:
                    os.unlink(path)
    return result

def move_psi_files(files, destination):
    """Move (copy) the given files to the deisred location"""
    trial_print("move_psi_files(destination=%s): files:" % (destination),
                pobj=files)
    result = 0
    if cleanup_psi_conf_d_dir():
        print "Unable to cleanup PSI conf files"
    for finfo in files:
        if not isinstance(finfo.get("alt_name"), basestring):
            # e.g. earlier releases did not have CHASSIS files
            continue
        dst_path = os.path.join(destination, finfo.get("alt_name", ''))
        src_path = finfo.get("src")
        if os.path.exists(src_path):
            fname = finfo.get("file")
            frevn = finfo.get("revision")
            if copy_config_file(src_path, dst_path, fname, frevn):
                result = 1
            wrb_path = finfo.get("write_back")
            if wrb_path:
                if copy_config_file(src_path, wrb_path, fname, frevn):
                    result = 1
        else:
            print "File path does not exist: %s" % src_path
            result = 1
    return result

def get_psi_files(receipt_path):
    """Returns list of filenames matching given PSI receipt file"""
    trial_print("get_psi_files(%s)" % (receipt_path))
    result = 0
    filenames = []
    if not valid_receipt_path(receipt_path):
        return 1, None
    receipt = get_receipt_info(receipt_path)
    if not receipt:
        print("%s PSI receipt found in mfg partition, "
              "skipping receipt backup" %
              (receipt is None and "No" or "Empty"))
        return 1, None
    filenames.append(receipt_path)
    # All of this code is based on original copy_config_files, but here
    # we allow looser get_psi_config_filepaths(), not restricting search
    # to MFG_PSI_DIR_PATH -- instead determine base_dir from receipt_path.
    base_dir = os.path.dirname(receipt_path)
    mfg_files = get_psi_config_filepaths(base_dir, receipt, skip_utility=True)
    if not mfg_files['valid']:
        print("No PSI conf files found in mfg partition, "
              "skipping conf file backup")
    missing = validate_receipt_to_files(receipt, mfg_files['valid'])
    if missing.get("keys") or missing.get("files"):
        # NB Missing isn't a "stop" condition like copy_config_files,
        # just treat it as another "warning, skipping missing files" case
        # similar to the messaging above
        if missing.get("keys"):
            print("Note: The following required key(s) are missing from "
                  "the PSI receipt: %s" % ', '.join(missing.get("keys")))
        if missing.get("files"):
            print("Note: The following PSI config file(s) do not exist "
                  "in mfg, skipping config file backup: %s" %
                  ', '.join(missing.get("files")))
            print_invalid_noexist(mfg_files, missing.get("files"))
            result = 1
    files = get_psi_file_copy_info(mfg_files['valid'])
    if not files: # Either None or empty [], ref code
        print("No valid PSI conf files matching OneFS PSI version %s "
              "found in mfg partition, skipping conf file backup" %
              get_tool_version_string())
        result = 1
    for finfo in files:
        src_path = finfo.get("src")
        if os.path.exists(src_path):
            filenames.append(src_path)
        else:
            print("PSI conf file path does not exist, skipping "
                  "conf file backup: %s" % src_path)
            result = 1
    return result, filenames

def backup_psi_files(receipt_path, peer=False):
    """Backup PSI config files to non-volatile location; 0 on success"""
    trial_print("backup_psi_files(%s, peer=%s)" % (receipt_path, peer))
    if not valid_receipt_path(receipt_path):
        print("Failed to backup PSI files for %s: Invalid path" %
              (receipt_path))
        return 1
    result, filenames = get_psi_files(receipt_path)
    if result and not filenames:
        return result
    if not filenames:
        print("Failed to backup PSI files for %s: No valid filenames" %
              (receipt_path))
        return 1
    if bmc_nvram.backup(filenames, peer, diff_check=False):
        print("Failed to backup PSI files for %s: BMC NVRAM failure" %
              (receipt_path))
        return 1
    # NB result can be non-0 even though bmc_nvram.backup() succeeded,
    # for the case that we only had partial filenames info
    return result

def push_psi_files(receipt_path, peer=False):
    """Push PSI config files to non-volatile location; 0 on success"""
    # Intelligent Backup: Backup ALWAYS updates destination BMC NVRAM,
    # using source info from Mfg partition, whereas push ONLY updates
    # BMC NVRAM if necessary, meaning one or more files have changed.
    trial_print("push_psi_files(%s, peer=%s)" % (receipt_path, peer))
    if not valid_receipt_path(receipt_path):
        print("Failed to push PSI files for %s: Invalid path" %
              (receipt_path))
        return 1
    result, filenames = get_psi_files(receipt_path)
    if result and not filenames:
        return result
    if not filenames:
        print("Failed to push PSI files for %s: No valid filenames" %
              (receipt_path))
        return 1
    if bmc_nvram.backup(filenames, peer, diff_check=True):
        print("Failed to push PSI files for %s: BMC NVRAM failure" %
              (receipt_path))
        return 1
    return 0

def restore_psi_files(receipt_path, peer=False):
    """Restore PSI config files from non-volatile location; 0 on success"""
    trial_print("restore_psi_files(%s, peer=%s)" % (receipt_path, peer))
    if not valid_receipt_path(receipt_path):
        print("Failed to restore PSI files for %s: Invalid path" %
              (receipt_path))
        return 1
    if bmc_nvram.restore(peer, diff_check=False):
        print("Failed to restore PSI files for %s: BMC NVRAM failure" %
              (receipt_path))
        return 1
    return 0

def pull_psi_files(receipt_path, peer=False):
    """Pull PSI config files from non-volatile location; 0 on success"""
    # Intelligent Restore: Restore ALWAYS updates destination (/mfg)
    # using source info from BMC NVRAM, whereas pull ONLY updates Mfg
    # if necessary, meaning per-file comparison and change detection.
    trial_print("pull_psi_files(%s, peer=%s)" % (receipt_path, peer))
    if not valid_receipt_path(receipt_path):
        print("Failed to pull PSI files for %s: Invalid path" %
              (receipt_path))
        return 1
    if bmc_nvram.restore(peer, diff_check=True):
        print("Failed to pull PSI files for %s: BMC NVRAM failure" %
              (receipt_path))
        return 1
    return 0

def display_psi_files(receipt_path, peer=False):
    """Display PSI config files from non-volatile location; 0 on success"""
    trial_print("display_psi_files(%s, peer=%s)" % (receipt_path, peer))
    # NB: We really don't care about receipt_path_on_mfg_partition for display,
    # since we're going straight to BMC NVRAM and not restoring.
    result = bmc_nvram.display(peer)
    return result

def list_psi_files(receipt_path, peer=False):
    """List PSI config files from non-volatile location; 0 on success"""
    trial_print("list_psi_files(%s, peer=%s)" % (receipt_path, peer))
    # NB: We really don't care about receipt_path_on_mfg_partition for list,
    # since we're going straight to BMC NVRAM and not restoring.
    result = bmc_nvram.display(peer, list_only=True)
    return result

def dump_receipt(receipt_path):
    """Display PSI Receipt file contents; 0 on success"""
    # pylint: disable=W0703
    result = 1
    try:
        receipt = get_receipt_info(receipt_path)
        if receipt:
            print json.dumps(receipt, indent=4)
            result = 0
    except Exception, exc:
        hwutil.unmount_mfg()
        print "Unable to get receipt info: %s" % exc
    return result

def getargs(args):
    """Parse the given arguments"""
    argparser = argparse.ArgumentParser()

    argparser.add_argument(
        '-p', '--path',
        default=ONEFS_CONF_DIR_PATH,
        help="Path to top level directory of the config files.")
    argparser.add_argument(
        '-d', '--destination',
        default=PSI_CONF_D_DIR_PATH,
        help="Destination for copying config files")
    argparser.add_argument(
        '-r', '--receipt',
        default=MFG_PSI_RECEIPT_PATH,
        help=("Path to PSI receipt. If no path is specified, "
              "attempt to get the receipt from the mfg partition"))

    group = argparser.add_mutually_exclusive_group(required=True)
    group.add_argument(
        '-v', '--view',
        action="store_true",
        help=("View PSI receipt. If no path is specified, read "
              "from the mfg partition"))
    group.add_argument(
        '-c', '--copy',
        action="store_true",
        help=("Copy PSI conf files to the desired location. PSI conf files to "
              "be copied are determined based on the PSI receipt"))
    group.add_argument(
        '--backup',
        action="store_true",
        help=("Backup PSI conf files to non-volatile location. PSI conf files "
              "to be copied are determined based on the PSI receipt"))
    group.add_argument(
        '--restore',
        action="store_true",
        help=("Restore PSI conf files from non-volatile location."))
    group.add_argument(
        '--push',
        action="store_true",
        help=("Push PSI conf files to non-volatile location. "
              "This is an intelligent and safe BACKUP, with AI."))
    group.add_argument(
        '--pull',
        action="store_true",
        help=("Pull PSI conf files from non-volatile location. "
              "This is an intelligent and safe RESTORE, with AI."))
    group.add_argument(
        '--display',
        action="store_true",
        help=("Display PSI conf files from non-volatile location."))
    group.add_argument(
        '--list',
        action="store_true",
        help=("List PSI conf files stored in non-volatile location."))

    argparser.add_argument(
        '--peer',
        action="store_true",
        help=("Backup/restore files from peer node rather than local."))
    argparser.add_argument(
        '-t', '--trial',
        action="count",
        help=("Trial run: Do not actually perform actions, only report to "
              "console what actions would normally be done."))
    argparser.add_argument(
        '-i', '--install',
        action="store_true",
        help=("Pass -i flag if calling isi_psi_tool during a OneFS Install."))

    opts = argparser.parse_args(args)
    return opts

def main(args):
    """main()"""
    # pylint: disable=R0912
    if args is None:
        print "No argument passed in"
        return 1

    if not bootstrap.isInfinityMotherboard():
        print "This node does not support PSI Receipts"
        return 0

    global ONEFS_INSTALL
    ONEFS_INSTALL = args.install

    set_trial_run(args.trial)

    # Mount mfg before anything is done
    if hwutil.mount_mfg():
        sys.exit(1)

    if args.copy:
        status = copy_config_files(args.path, args.destination, args.receipt)
    elif args.view:
        status = dump_receipt(args.receipt)
    elif args.backup:
        status = backup_psi_files(args.receipt, args.peer)
    elif args.restore:
        status = restore_psi_files(args.receipt, args.peer)
    elif args.push:
        status = push_psi_files(args.receipt, args.peer)
    elif args.pull:
        status = pull_psi_files(args.receipt, args.peer)
    elif args.display:
        status = display_psi_files(args.receipt, args.peer)
    elif args.list:
        status = list_psi_files(args.receipt, args.peer)
    else:
        print "No valid action specified"
        status = 1

    # Unmount before exiting, if we're still mounted
    if hwutil.mfg_is_mounted():
        if hwutil.unmount_mfg():
            status = 1

    sys.exit(status)

if __name__ == "__main__":
    if len(sys.argv) <= 1:
        print "No argument passed in"
        sys.exit(1)
    sys.exit(main(getargs(sys.argv[1:])))
