#!/usr/bin/python

# This tool provides a way to update the PSI receipt on a node.
# It also provides the configuration file, if missing.
# It is intended to be used as an under bar utility for NIC upgrades.

import argparse
import base64
import json
import os
import shutil
import re
import sys
import time

import isi.hw.psi as psi

from isi.app.lib.procs import get_cmd_output

ROOT_DIR = os.path.sep
ETC_DIR = os.path.join(ROOT_DIR, 'etc')
MFG_MIRROR = os.path.join(ROOT_DIR, 'dev', 'mirror', 'mfg')
MNT_POINT = os.path.join(ROOT_DIR, 'mfg')
RECEIPT = os.path.join(MNT_POINT, 'psi', 'receipt.json')

ISI_PSI_TOOL = '/usr/bin/isi_hwtools/isi_psi_tool %s'

# It's a bit unfortunate to hardcode this, but do it so we can do
# some amount of input validation to help the user modify the
# receipt properly. If we have new models or name changes this needs
# to be updated accordingly.
psi_receipt_opts = {
    # This upgrade script is intended to only handle front-end NIC devices
    # The back-end is prescriptive to align with positioning, perf
    # expectations and pricing.
    #'back-end': (
    ## psi key, (UX name, psi receipt model)
    #'BE', [('10G', '001-iBEIO-10GBE-psi'),
    #       ('40G', '001-iBEIO-40GBE-psi'),
    #       ('40G-IB', '001-iBEIO-IB-40GBE-psi'),
    #       ('10G-IB', '001-iBEIO-IBQDR-psi'),
    #      ],
    #),
    'front-end' : (
    'FE', [#('10G-FC',
           # [
           #     {
           #         'regex' : '.*F800.*',
           #         'value' : '001-iFEIO-10GBE-FC-F-psi',
           #         'psi_key' : 'hw.attributes.model',
           #     },
           #     {
           #         'regex' : '.*',
           #         'value' : '001-iFEIO-10GBE-FC-psi',
           #         'psi_key' : 'hw.attributes.model',
           #     },
           # ]),
           # The Sheba 10G-FC card is not slated until Pipeline. Don't expose
           # it as an option to avoid confusion.
           ('10G', [
                {
                    'regex' : '.*F800.*',
                    'value' : '001-iFEIO-10GBE-F-psi',
                    'psi_key' : 'hw.attributes.model',
                    'type' : str,
                },
                {
                    'regex' : '.*',
                    'value' : '001-iFEIO-10GBE-psi',
                    'psi_key' : 'hw.attributes.model',
                    'type' : str,
                },
            ]),
           ('40G', [
                {
                    'regex' : '.*F800.*',
                    'value' : '001-iFEIO-40GBE-F-psi',
                    'psi_key' : 'hw.attributes.model',
                    'type' : str,
                },
                {
                    'regex' : '.*',
                    'value' : '001-iFEIO-40GbE-psi',
                    'psi_key' : 'hw.attributes.model',
                    'type' : str,
                },
            ]),
           ('10G-B', '002-iFEIO-10GBE-B-psi'), # (pelican)
         ],
    ),
}

MAKE_TMP_DIR_CMD = '/usr/bin/mktemp -d'
EXTRACT_TREE_CMD = '/usr/bin/tar xvfz %s --directory %s'
REMOVE_FILES_CMD = '/bin/rm -rf %s'
CP_F_SRC_TGT_CMD = '/bin/cp -f %s %s'

# Only include files for which we modify receipt entries and
# which were added to psi-conf after GA.
PSI_CONF_FILES = {
        '001-iFEIO-10GBE-F-psi' : 'psi/FE/001-1.0-iFEIO-10GBE-F-psi.conf',
        '001-iFEIO-40GBE-F-psi' : 'psi/FE/001-1.0-iFEIO-40GBE-F-psi.conf',
        }

PSI_TARBALL_GZIPPED_NAME = 'psi-select.tar.gz'
PSI_TARBALL_GZIPPED_DATA = base64.b64decode('''
        H4sIAOAmgloAA+3TTU+DMBwGcK7wKcg8s7VbN4yLiTEO48mT56UDNquOkrZsUbPv
        bumYxsSoyTJ0yfML0DfCn0CfUoteMukRQiPaJZFIJje3ESXXl5MoiUotuqks5t5+
        iDVizHMNjeuWxkPi5t0aHXl00O/HLB4MGPUIHTG7HJI96/5KpQ1XYehlqjIv39x3
        JZdcFOGdzpVu471achIu+YNU4XlIA9sXhesT21f5SmghC7d0v7bNa+CnRm47PjdG
        iVllct1M+EVu1lI97oY+JQuxyN+HPs8y9zSjqny8nUplVZi6QDOeK1mYvMg+3bUJ
        mkt9przkM/EkjGi38CbYBPq5KVlnQiyaAi4yUxeZabIrurK7pPl27lkZN/ULdfp2
        50dkGNHTCxqfEWKPzvijQvv/v/wq/+wf5H+A/LfhgPlnf5X/QxT+Kf/sSPMPAAAA
        AAAAAAAAAAAAAAAAx+8N1yvWFAAoAAA=
''')

how_the_above_got_created = '''
drutz@drutz-wkstn:/1/psi-conf$ rm psi-select.tar.gz
drutz@drutz-wkstn:/1/psi-conf$ git checkout tags/8.1.1.2-79ecf6b
Note: checking out 'tags/8.1.1.2-79ecf6b'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 79ecf6b... Merge pull request #48 from drutz/master
drutz@drutz-wkstn:/1/psi-conf$ tar -zcvf psi-select.tar.gz \
>         psi/FE/001-1.0-iFEIO-10GBE-F-psi.conf  \
>         psi/FE/001-1.0-iFEIO-40GBE-F-psi.conf
psi/FE/001-1.0-iFEIO-10GBE-F-psi.conf
psi/FE/001-1.0-iFEIO-40GBE-F-psi.conf
drutz@drutz-wkstn:/1/psi-conf$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> with open('psi-select.tar.gz') as fd:
...     data = base64.b64encode(fd.read())
...
>>> for (start, end) in [(start, start + 64) for start in range(0, len(data), 64)]:
...     print ' ' * 8 + data[start:end]
...
        H4sIAOAmgloAA+3TTU+DMBwGcK7wKcg8s7VbN4yLiTEO48mT56UDNquOkrZsUbPv
        bumYxsSoyTJ0yfML0DfCn0CfUoteMukRQiPaJZFIJje3ESXXl5MoiUotuqks5t5+
        iDVizHMNjeuWxkPi5t0aHXl00O/HLB4MGPUIHTG7HJI96/5KpQ1XYehlqjIv39x3
        JZdcFOGdzpVu471achIu+YNU4XlIA9sXhesT21f5SmghC7d0v7bNa+CnRm47PjdG
        iVllct1M+EVu1lI97oY+JQuxyN+HPs8y9zSjqny8nUplVZi6QDOeK1mYvMg+3bUJ
        mkt9przkM/EkjGi38CbYBPq5KVlnQiyaAi4yUxeZabIrurK7pPl27lkZN/ULdfp2
        50dkGNHTCxqfEWKPzvijQvv/v/wq/+wf5H+A/LfhgPlnf5X/QxT+Kf/sSPMPAAAA
        AAAAAAAAAAAAAAAAx+8N1yvWFAAoAAA=
>>> quit()
drutz@drutz-wkstn:/1/psi-conf$
'''

a_couple_of_more_things_to_restore = '''
drutz@drutz-wkstn:/1/psi-conf$ rm psi-select.tar.gz
drutz@drutz-wkstn:/1/psi-conf$ git checkout master
Previous HEAD position was 79ecf6b... Merge pull request #48 from drutz/master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
drutz@drutz-wkstn:/1/psi-conf$

'''

def get_output(command):
    error, output = get_cmd_output(command)
    if error:
        raise Exception(
                'Error: %s; command: %s; output: %s' % (error, command, output))
    return output

def opts2str():
    msg = ""
    for key, val in psi_receipt_opts.items():
        msg += "%s:\n%s\n\n" % (key.title(), ', '.join(i[0] for i in val[1]))
    return msg

USAGE_EPILOG = """
Use this tool to update the node's receipt to use a
new configuration
and to provide the configuration file, if missing

*The available types and values are:

%s

To make an update, specify the type of config key and what it
should be set to.

E.g:
%s Front-End 40G
""" % (opts2str(), os.path.basename(sys.argv[0]))

def errx(msg, out=None, rc=1):
    if out:
        for line in out:
            msg += '\n%s' % line
    sys.stderr.write("%s\n" % msg)
    sys.exit(rc)

def mount_mfg():
    if os.path.ismount(MNT_POINT):
        return 0
    cmd = '/sbin/mount %s %s' % (MFG_MIRROR, MNT_POINT)
    err, out = get_cmd_output(cmd)
    if err:
        errx('Unable to mount "%s" on "%s"' %
            (MFG_MIRROR, MNT_POINT), out)
        return 1
    return 0

def unmount_mfg():
    if not os.path.ismount(MNT_POINT):
        return 0
    err, out = get_cmd_output("/sbin/umount %s" % MNT_POINT)
    if err:
        errx('Unable to umount "%s"' % MNT_POINT, out)
        return 1
    return 0

def get_psi(key, typ):
    if typ in [int, 'int']:
        val = psi.getInt(key)
    elif typ in ['uint64']:
        val = psi.getUint64(key)
    elif typ in [str, 'str', 'string']:
        val = psi.getString(key)
    elif typ in [bool, 'bool', 'boolean']:
        val = psi.getBool(key)
    else:
        raise Exception("Unknown psi key type '%s' for key '%s'" % (typ, key))
    return val

def create_backup(fname, maxfiles=10, ext='bak'):
    try:
        dname = os.path.dirname(fname)
        def is_receipt(dname, name, ext):
            bname = os.path.splitext(os.path.basename(name))[0]
            return (os.path.isfile(os.path.join(dname, name)) and
                    name.startswith(bname) and name.endswith(ext))
        files = [name for name in os.listdir(dname)
            if is_receipt(dname, name, ext)]
        bak_file = "%s.%s.%s" % (fname, time.strftime("%Y%m%d-%H%M%S"), ext)
        if len(files) >= maxfiles:
            # remove oldest
            oldest = min(files, key=lambda f:
                os.path.getctime(os.path.join(dname, f)))
            os.remove(os.path.join(dname, oldest))
        shutil.copy(fname, bak_file)
    except (OSError, IOError) as e:
        errx("Failed to create backup receipt: %s" % e)

def update_receipt():
    if args.type.lower() not in [i.lower() for i in psi_receipt_opts.keys()]:
        errx("Provided type '%s' is not recognized" % args.type)
    key, options = psi_receipt_opts[args.type.lower()]
    options = dict(options)
    if args.value.lower() not in [o.lower() for o in options]:
        errx("Provided value '%s' for '%s' is not supported" %
        (args.value, args.type))
    # XXX This sets the paramtesrs used for the isi_json_update
    # code. If we install this to isi_hwtools or elsewhere, we should use the
    # tool directly instead of wrapping it here.
    value = [val for k, val in options.items()
                    if k.lower() == args.value.lower()][0]
    if isinstance(value, list):
        for opt in value:
            psi_val = get_psi(opt['psi_key'], opt['type'])
            if psi_val is None:
                raise Exception("Failed PSI get on '%s'" % opt['psi_key'])
            if re.match(opt['regex'], psi_val):
                value = opt['value']
                break
    if value is None:
        raise Exception("Unable to determine PSI value for '%s'" % key)
    args.value = value
    args.output = args.file = RECEIPT
    args.key = key
    if args.debug:
        print("%s -> %s" % (args.key, args.value))
    result = 1
    if mount_mfg():
        return 1
    try:
        # create a backup
        create_backup(RECEIPT)
        print("Updating receipt")
        result = json_update()
    finally:
        unmount_mfg()
    if result == 0:
        print("Success")
    return result

# XXX The following json functions are lifted from
# /usr/bin/isi_hwtools/isi_json_update
# This script is not installed, but handles modifying JSON
# If we install this to isi_hwtools or elsewhere, we should use the
# tool directly instead of wrapping it here.
def read_json(filepath):
    obj = {}
    with open(filepath, "r") as fp:
        obj = json.load(fp)
    return obj

def write_json(filepath, obj, sort_keys=True, indent=4, separators=(',', ': ')):
    with open(filepath, "w") as fp:
        json.dump(obj, fp,
                  sort_keys=sort_keys, indent=indent, separators=separators)

def get_split_key(key):
    if isinstance(key, list):
        pass
    elif isinstance(key, basestring):
        key = key.split(",")
    else:
        raise Exception("Invalid key '%s', type '%s'" % (key, type(key)))
    return key

def get_key_value(obj, key):
    skey = get_split_key(key)
    keysofar = ""
    o = obj
    for k in skey:
        if not isinstance(o, dict):
            raise Exception("Cannot index into JSON data farther than "
                            "key=%s: object is type %s" % (keysofar, type(o)))
        keysofar = ",".join([keysofar, k]).strip(",")
        if not k in o:
            raise Exception("Cannot find '%s' in JSON data" % keysofar)
        o = o[k]
    return o

def replace_key_value(obj, key, newval):
    oldval = get_key_value(obj, key)
    mismatch = type(oldval) != type(newval)
    if isinstance(oldval, basestring) and isinstance(newval, basestring):
        mismatch = False
    if mismatch:
        raise Exception("Mismatched types: old val type %s != new val type %s" %
                        (type(oldval), type(newval)))
    skey = get_split_key(key)
    o = obj
    for k in skey[:-1]:
        o = o[k]
    k = skey[-1]
    if o[k] == newval:
        print("NOTE: new value == old value; no update needed")
        return None
    o[k] = newval
    return obj

def extract():
    tmp_dir = get_output(MAKE_TMP_DIR_CMD)[0] # E.g.: '/tmp/tmp.GTCzqtyK'
    full_path = os.path.join(tmp_dir, PSI_TARBALL_GZIPPED_NAME)

    f = open(full_path, 'w')
    f.write(PSI_TARBALL_GZIPPED_DATA)
    f.close()

    get_output(EXTRACT_TREE_CMD % (full_path, tmp_dir))
    get_output(REMOVE_FILES_CMD % full_path)

    return tmp_dir

def copy_force(source, target):
    get_output(CP_F_SRC_TGT_CMD % (source, target))

def remove_force(tmp_dir):
    get_output(REMOVE_FILES_CMD % (tmp_dir + '*'))

def provide_conf_file_to_etc_psi_if_missing():
    # This function ignores configuration files in /mfg/psi
    # and only concerns itself with those files in /etc/psi

    psi_conf_file = PSI_CONF_FILES.get(args.value)
    if psi_conf_file is None:
        # No psi_conf_file is defined for this particular args.value
        # so no attempt needs to be made to check if it is missing.
        return

    print
    target = os.path.join(ETC_DIR, psi_conf_file)
    if os.path.exists(target):
        print 'Configuration file already exists: %s' % target
    else:
        print 'Extracting temporary files'
        tmp_dir = extract()
        alignment_spaces = ' ' * (len(tmp_dir) - len(ETC_DIR))

        source = os.path.join(tmp_dir, psi_conf_file)
        print 'Providing missing configuration file:'
        print '                  target: %s%s' % (alignment_spaces, target)
        print '                  source: %s'   %                    source
        copy_force(source, target)

        print 'Removing temporary files: %s' % tmp_dir
        remove_force(tmp_dir)

        if not os.path.exists(target):
            raise Exception("Failed to provide missing configuration file")

    print

def json_update():
    if not os.path.exists(args.file):
        raise Exception("No such file: %s" % args.file)
    data = read_json(args.file)
    old_value = get_key_value(data, args.key)
    if args.debug:
        print("Old value %s: %s" % (args.key, old_value))
    new_data = replace_key_value(data, args.key, args.value)
    if new_data is None:
        if args.output:
            print("Creating new file %s, but no data changes were made" %
                  args.output)
            write_json(args.output, data)
            provide_conf_file_to_etc_psi_if_missing()
        else:
            print("No update to existing file %s: No data changes were made" %
                  args.file)
    else:
        if args.debug:
            print("New value %s: %s" % (args.key, args.value))
        if args.output:
            write_json(args.output, data)
        else:
            write_json(args.file, data)
        provide_conf_file_to_etc_psi_if_missing()
    return 0

def can_push_to_bmc_nvram():
    err, output = get_cmd_output(ISI_PSI_TOOL % '-h | grep -E "\-\-push"')
    return err == 0

def copy_psi_files():
    msg_option_pairs = [
            ('Copying PSI conf files to the desired location', '--copy'),
            ]
    if can_push_to_bmc_nvram():
        msg_option_pairs += [
                ('Backing up MFG PSI files locally', '--push'),
                ('Backing up MFG PSI files to peer', '--push --peer'),
                ]
    for msg, option in msg_option_pairs:
        # Add the --install option of isi_psi_tool to completely avoid the
        # possibility of launching a shell in isi_psi_tool.stop_boot().
        # This is better than modifying isi_psi_tool because some MRs
        # that have this issue have already shipped)
        cmd = ISI_PSI_TOOL % option + ' --install'
        print
        print '        %s' % msg
        print '   Cmd: %s' % cmd
        output = get_cmd_output(cmd)[1]
        for line in output[:1]: # The first line of output, if any
            print 'Output: %s' % line
        for line in output[1:]: # Succeeding lines of output, if any
            print '        %s' % line

    print

parser = argparse.ArgumentParser(
    description=('Update the PSI receipt configuraton'),
    formatter_class=argparse.RawDescriptionHelpFormatter,
    epilog=USAGE_EPILOG)
parser.add_argument('-d', '--debug', action='store_true',
                    help='Enable debug output')
parser.add_argument('type',
                    help='The type of configuration to change*')
parser.add_argument('value',
                    help='The new configuration value for the given type*')

def main():
    global args
    args = parser.parse_args()
    try:
        result = update_receipt()
        if result == 0:
            copy_psi_files()
    except Exception as e:
        sys.stderr.write("%s\n" % e)
        result = 1
    return result

if __name__ == '__main__':
    sys.exit(main())
