#!/usr/bin/env python
'''Jaws->Riptide and Moby->Riptide NFS exports and settings conversion'''
# TODO:
# Batch commits for large config to avoid killing tardis?
# Tardis error checking on ns.update()?

import errno
import os
import sys
import struct
import syslog
import logging
import subprocess
import isi.error
import isi.fs.enc as enc
import isi.config as iconf
import isi.gconfig as gconfig
import isi.config.nfs as nfs
import isi.config.zone as izone
import isi.config.tardis as tardis
from isi.app.lib import safelockfile
from isi.app.lib.confParser import ConfParser
from isi.app.lib.cluster import cluster_get_sysctl
from ctypes import *

NFS_SCHEDULER_CONFIG_LIB = '/usr/likewise/lib/liblwnfs_sched_config.so'
nfs_sched_config_lib = CDLL(NFS_SCHEDULER_CONFIG_LIB, mode=(RTLD_GLOBAL))

NFS_DEFAULT_PATH = "/ifs/.ifsvar/modules/nfs"
LOG_FILE = os.path.join(
    NFS_DEFAULT_PATH, "riptide-nfs-exports-upgrade.log")
NFS_EXPORTS_UPGRADE_LOCKFILE = os.path.join(
    NFS_DEFAULT_PATH, "riptide-nfs-exports-upgrading")
NFS_EXPORTS_UPGRADED_FILE = os.path.join(
    NFS_DEFAULT_PATH, "riptide-nfs-exports-upgrade-complete")
MOBY_NFS_UPGRADED_FILE = os.path.join(
    NFS_DEFAULT_PATH, "moby-nfs-upgrade-complete") # XXX replace with uapi?

WRITE_TYPES = {'datasync' : nfs.NFS_WRITE_DATASYNC,
               'filesync' : nfs.NFS_WRITE_FILESYNC,
               'unstable' : nfs.NFS_WRITE_UNSTABLE}

EXPORT_TYPES = {'export'    : nfs.NFS_EXPORT_TYPE_EXPORT,
                'prototype' : nfs.NFS_EXPORT_TYPE_PROTOTYPE,
                'template'  : nfs.NFS_EXPORT_TYPE_TEMPLATE}

LWREG_PREFIX = 'registry.Services.lwio.Parameters.Drivers.nfs.'
SYSCTL_PREFIX = 'sysctls.entries.'
SYSCTL_CONF = "/etc/mcp/override/sysctl.conf"
SHOWMOUNT_NG_SETTING_PATH = ':one.mountd_config.md_ngs_in_showmount_e'
MAX_COMMIT_RETRIES = 100

NFS_LWREG_CONVERSION_DICT = {
    "vfs.nfs.downdelayinitial":"DownDelayInitial",
    "vfs.nfs.downdelayinterval":"DownDelayInterval",
    "vfs.nfs.export_config_backup_copies":"ExportConfigBackupCopies",
    "vfs.nfs.keytab_enctype":"KeytabEncType",
    "vfs.nfs.skip_wcc_data_onerr":"SkipWCCDataOnErr",
    "vfs.nfs.nfs3_jukebox_delay":"NFS3JukeboxDelay",
    "vfs.nfs.reconnects":"Reconnects",
    "vfs.nfs.bufpackets":"BufPackets",
    "vfs.nfs.callback_addr":"CallbackAddr",
    "vfs.nfs.realign_count":"RealignCount",
    "vfs.nfs.realign_test":"RealignTest",
    "vfs.nfs.defect":"Defect",
    "vfs.nfs.iodmax":"IodMax",
    "vfs.nfs.iodmin":"IodMin",
    "vfs.nfs.iodmaxidle":"IodMaxIdle",
    "vfs.nfs.diskless_rootaddr":"DisklessRootAddr",
    "vfs.nfs.diskless_rootpath":"DisklessRootpath",
    "vfs.nfs.diskless_valid":"DisklessValid",
    "vfs.nfs.nfs_ip_paranoia":"NfsIpParanoia",
    "vfs.nfs.nfs_directio_allow_mmap":"NfsDirectioAllowMmap",
    "vfs.nfs.nfs_directio_enable":"NfsDirectioEnable",
    "vfs.nfs.clean_pages_on_close":"CleanPagesOnClose",
    "vfs.nfs.commit_on_close":"CommitOnClose",
    "vfs.nfs.prime_access_cache":"PrimeAccessCache",
    "vfs.nfs.access_cache_timeout":"AccessCacheTimeout",
    "vfs.nfs.debug":"Debug",
    "vfs.nfs.cache_floodlevel":"CacheFloodLevel",
    "vfs.nfsrv.nfsv4.callback_timeout":"NFSV4CallbackTimeout",
    "vfs.nfsrv.nfsv4.upcall_timeout":"NFSV4UpcallTimeout",
    "vfs.nfsrv.nfsv4.lease_time":"NFSV4LeaseTime",
    "vfs.nfsrv.nfsv4.acls_return_synthetic":"NFSV4AclsReturnSynthetic",
    "vfs.nfsrv.nfsv4.groupat_identifier":"NFSV4GroupAtIdentifier",
    "vfs.nfsrv.nfsv4.use_acls":"NFSV4UseAcls",
    "vfs.nfsrv.nfsv4.defaultgid":"NFSV4DefaultGID",
    "vfs.nfsrv.nfsv4.defaultuid":"NFSV4DefaultUID",
    "vfs.nfsrv.nfsv4.idmap_alert_threshold_duration":"AlertFailedIDQueryThrottleDurationSec",
    "vfs.nfsrv.nfsv4.idmap_alert_threshold_count":"AlertFailedIDQueryThrottleThreshold",
    "vfs.nfsrv.nfsv4.nfs4_returnoldstateid":"NFSV4Nfs4ReturnOldStateID",
    "vfs.nfsrv.nfsv4.max_badseqid":"NFSV4MaxBadSeqID",
    "vfs.nfsrv.nfsv4.lease_delta":"NFSV4LeaseDelta",
    "vfs.nfsrv.nfsv4.servertimer_upperbound_exceeded":"NFSV4ServerTimerUpperboundExceeded",
    "vfs.nfsrv.nfsv4.servertimer_upperbound":"NFSV4ServerTimerUpperbound",
    "vfs.nfsrv.nfsv4.openowner_noopen_expiry":"NFSV4OpenOwnerNoopenExpiry",
    "vfs.nfsrv.nfsv4.lockowner_nolock_expiry":"NFSV4LockOwnerNolockExpiry",
    "vfs.nfsrv.nfsv4.expire_on_shutdown":"NFSV4ExpireOnShutdown",
    "vfs.nfsrv.nfsv4.expire_on_disconnect":"NFSV4ExpireOnDisconnect",
    "vfs.nfsrv.nfsv4.access_write_allows_read":"NFSV4AccessWriteAllowsRead",
    "vfs.nfsrv.nfsv4.specialreads_bypass_locks":"NFSV4SpecialReadsBypassLocks",
    "vfs.nfsrv.nfsv4.honor_deny_delete":"NFSV4HonorDenyDelete",
    "vfs.nfsrv.nfsv4.check_utf8":"NFSV4CheckUTF8",
    "vfs.nfsrv.nfsv4.zero_offset_verifier_allowed":"NFSV4ZeroOffsetVerifierAllowed",
    "vfs.nfsrv.nfsv4.allow_owner_override":"NFSV4AllowOwnerOverride",
    "vfs.nfsrv.nfsv4.enable_locallocks":"NFSV4EnableLocallocks",
    "vfs.nfsrv.fake_dir_delete_access":"FakeDirDeleteAccess",
    "vfs.nfsrv.use_local_nfsver":"UseLocalNFSVer",
    "vfs.nfsrv.cache_udp_threshold":"CacheUDPThreshold",
    "vfs.nfsrv.cache_tcp_threshold":"CacheTCPThreshold",
    "vfs.nfsrv.cache_reconnect_timeout":"CacheReconnectTimeout",
    "vfs.nfsrv.cache_check_interval":"CacheCheckInterval",
    "vfs.nfsrv.cache_tcptimeout":"CacheTCPTimeout",
    "vfs.nfsrv.cache_udphighwater":"CacheUDPHighwater",
    "vfs.nfsrv.cache_udptimeout":"CacheUDPTimeout",
    "vfs.nfsrv.cache_enabled":"CacheEnabled",
    "vfs.nfsrv.server_v4enabled":"ServerV4Enabled",
    "vfs.nfsrv.server_v3enabled":"ServerV3Enabled",
    "vfs.nfsrv.nfs_privport":"NFSPrivPort",
    "vfs.nfsrv.errors_clear":"ErrorsClear",
    "vfs.nfsrv.errors_v4":"ErrorsV4",
    "vfs.nfsrv.errors_v3":"ErrorsV3",
    "vfs.nfsrv.log_errmap_defaults":"LogErrmapDefaults",
    "vfs.nfsrv.request_space_low":"RequestSpaceLow",
    "vfs.nfsrv.request_space_high":"RequestSpaceHigh",
    "vfs.nfsrv.max_wsize":"MaxWSize",
    "vfs.nfsrv.max_rsize":"MaxRSize",
    "vfs.nfsrv.create_attributes_ids_enabled":"CreateAttributesIdsEnabled",
    "vfs.nfsrv.create_attributes_enabled":"CreateAttributesEnabled",
    "vfs.nfsrv.readdir_requires_read":"ReaddirRequiresRead",
    "vfs.nfsrv.dopanic":"DoPanic",
    "vfs.nfsrv.allow_kerberos_squashing":"AllowKerberosSquashing",
    "vfs.nfsrv.commit_miss":"CommitMiss",
    "vfs.nfsrv.commit_blks":"CommitBlks",
    "vfs.nfsrv.mirrormnt":"MirrorMnt",
    "vfs.nfsrv.fha.bin_shift":"FhaBinShift",
    "vfs.nfsrv.fha.multiwriter_enabled":"FhaMultiwriterEnabled",
    "vfs.nfsrv.fha.max_nfsds_per_fh":"FhaMaxNfsdsPerFh",
    "vfs.nfsrv.fha.shared_max_per_fh":"FhaSharedMaxPerFh",
    "vfs.nfsrv.fha.exclusive_max_per_fh":"FhaExclusiveMaxPerFh",
    "vfs.nfsrv.fha.activity":"FhaActivity",
    "vfs.nfsrv.fha.stats":"FhaStats",
    "vfs.nfsrv.fha.stats_clear":"FhaStatsClear",
    "vfs.nfsrv.rpc.threads_min":"RpcThreadsMin",
    "vfs.nfsrv.rpc.threads_max":"RpcThreadsMax",
    "vfs.nfsrv.rpc.threads_alloc_current":"RpcThreadsAllocCurrent",
    "vfs.nfsrv.rpc.threads_alloc_highwater":"RpcThreadsAllocHighwater",
    "vfs.nfsrv.rpc.request_space_used":"RpcRequestSpaceUsed",
    "vfs.nfsrv.rpc.request_space_used_highest":"RpcRequestSpaceUsedHighest",
    "vfs.nfsrv.rpc.request_space_high":"RpcRequestSpaceHigh",
    "vfs.nfsrv.rpc.request_space_overcommit":"RpcRequestSpaceOvercommit",
    "vfs.nfsrv.rpc.request_space_low":"RpcRequestSpaceLow",
    "vfs.nfsrv.rpc.request_space_throttled":"RpcRequestSpaceThrottled",
    "vfs.nfsrv.rpc.request_space_throttle_count":"RpcRequestSpaceThrottleCount",
    "vfs.nfsrv.rpc.overcommit_active":"RpcOvercommitActive",
    "vfs.nfsrv.rpc.overcommit_events":"RpcOvercommitEvents",
    "vfs.nfsrv.rpc.overcommit_off":"RpcOvercommitOff",
    "vfs.nfsrv.rpc.overcommit_on":"RpcOvercommitOn",
    "vfs.nfsrv.rpc.idletimeout":"RpcIdletimeout",
    "vfs.nfsrv.rpc.shutdown_state":"RpcShutdownState",
    "vfs.nfsrv.rpc.stats":"RpcStats",
    "vfs.nfsrv.rpc.disc_ifdown":"RpcDiscIfdown",
    "vfs.nfsrv.rpc.disc_addrgone":"RpcDiscAddrgone"
}

logger = logging.getLogger("riptide_nfs_export_upgrade")


def ignore_attribute(export, attribute, value):
    logger.info('Ignoring attribute "%s" with value "%s"' % (attribute,
                                                             str(value)))


def set_attribute(export, attribute, value):
    logger.info('Setting export attribute %s to %s' % (attribute, str(value)))
    try:
        setattr(export, attribute, value)
    except AttributeError:
        logger.error('Failed to set export attribute %s!' % attribute)


def set_unrecognized_attribute(export, attribute, value):
    logger.warning('Unrecognized export attribute "%s"! Trying to set '
                   'anyhow.' % attribute)
    set_attribute(export, attribute, value)


def set_paths(export, attribute, value):
    """value is list of dict of paths, e.g. [{'path': '/ifs'}]"""
    paths = []
    for pathdict in value:
        for path in pathdict.values():
            paths.append(path)
    set_attribute(export, attribute, paths)


def set_encoding(export, attribute, value):
    try:
        encoding = enc.from_name(value)
    except SystemError:
        logger.warn('Failed to determine encoding for %s, using '
                    'default' % value)
        encoding = enc.DEFAULT
    set_attribute(export, attribute, encoding)


def set_security_flavors(export, attribute, value):
    sec_flavors = value.split(':') if value else []
    set_attribute(export, attribute, sec_flavors)


def set_write_type(export, attribute, value):
    try:
        write_type = WRITE_TYPES[value]
    except KeyError:
        logger.warn('Failed to translate write type %s, using '
                    'default' % value)
        return

    set_attribute(export, attribute, write_type)


def set_mapping(export, attribute, value):
    """value is a dictionary of the form
    {'primary_group': 'nobody', 'enabled': True, 'user': 'root',
     'secondary_groups': 'wheel:users'}"""
    for sub_option, sub_value in value.items():
        full_option = '_'.join([attribute, sub_option])
        if sub_option == 'secondary_groups':
            sub_value = sub_value.split(':') if sub_value else []

        set_attribute(export, full_option, sub_value)


OPTION_CONVERTERS = {
    'all_dirs'              : set_attribute,
    'bsize'                 : set_attribute,
    'cansettime'            : set_attribute,
    'case_insensitive'      : set_attribute,
    'case_preserving'       : set_attribute,
    'chown_restricted'      : set_attribute,
    'commit_asynchronous'   : set_attribute,
    'dtpref'                : set_attribute,
    'encoding'              : set_encoding,
    'link_max'              : set_attribute,
    'map_failure'           : set_mapping,
    'map_lookup_uid'        : set_attribute,
    'map_non_root'          : set_mapping,
    'map_retry'             : set_attribute,
    'map_root'              : set_mapping,
    'map_user_full'         : set_attribute,
    'maxfilesize'           : set_attribute,
    'name_max'              : set_attribute,
    'no_trunc'              : set_attribute,
    'options_mask1'         : set_attribute,
    'options_mask2'         : set_attribute,
    'read_only'             : set_attribute,
    'readdirplus_enable'    : set_attribute,
    'readdirplus_prefetch'  : set_attribute,
    'return_32bit_file_ids' : set_attribute,
    'rtmax'                 : set_attribute,
    'rtmult'                : set_attribute,
    'rtpref'                : set_attribute,
    'security_flavors'      : set_security_flavors,
    'setattr_asynchronous'  : set_attribute,
    'snapshot'              : set_attribute,
    'supported_mask1'       : set_attribute,
    'supported_mask2'       : set_attribute,
    'symlink_supported'     : set_attribute,
    'time_delta_nsec'       : set_attribute,
    'time_delta_sec'        : set_attribute,
    'wtmax'                 : set_attribute,
    'wtmult'                : set_attribute,
    'wtpref'                : set_attribute,
    'write_filesync_action' : set_write_type,
    'write_filesync_reply'  : set_write_type,
    'write_unstable_action' : set_write_type,
    'write_unstable_reply'  : set_write_type,
    'write_datasync_action' : set_write_type,
    'write_datasync_reply'  : set_write_type,
    }


def set_options(export, attribute, options):
    for option in options:
        conversion_func = OPTION_CONVERTERS.get(option,
                                                set_unrecognized_attribute)
        conversion_func(export, option, options[option])


def convert_client(client):
    '''Client can be composed of either 1) address 2) address+mask 3) name'''
    if not isinstance(client, dict):
        # Empty client
        new_client = nfs.Client('')
    elif client['name'] is not None:
        new_client = nfs.Client(client['name'])
    elif client['mask'] is not None:
        new_client = nfs.Client(client['address'] + '/' + client['mask'])
    elif client['address'] is not None:
        new_client = nfs.Client(client['address'])
    else:
        logger.warn('Failed to convert client %s' % client)
        new_client = nfs.Client('')

    return new_client


def set_client_list(export, attribute, value):
    """
    value = list of dicts of mask/name/address, e.g.:
    [{'mask': None, 'name': 'maps.west.isilon.com', 'address': None},
     {'mask': None, 'name': None, 'address': '8.8.8.8'}]
    """
    # Python object attrs do not have the underscores present in old gconfig
    translated_attribute = attribute.replace('_', '')
    clients = [convert_client(client) for client in value]
    set_attribute(export, translated_attribute, clients)


def set_type(export, attribute, value):
    try:
        export_type = EXPORT_TYPES[value]
    except KeyError:
        logger.warn('Failed to determine export type (%s), using '
                     '"export"' % value)
        export_type = nfs.NFS_EXPORT_TYPE_EXPORT
    set_attribute(export, attribute, export_type)


ATTRIBUTE_CONVERTERS = {
    'clients'            : set_client_list,
    'comment'            : set_attribute,
    'id'                 : set_attribute,
    'options'            : set_options,
    'paths'              : set_paths,
    'ro_clients'         : set_client_list,
    'root_clients'       : set_client_list,
    'rw_clients'         : set_client_list,
    'type'               : set_type,
    'unresolved_clients' : ignore_attribute,
    'zid'                : ignore_attribute,
    }


def convert_gconfig_export(export):
    logger.info('Converting export %d' % export['id'])
    new_export = nfs.Export(export['zid'])

    for attribute in export:
        conversion_func = ATTRIBUTE_CONVERTERS.get(attribute,
                                                   set_unrecognized_attribute)
        conversion_func(new_export, attribute, export[attribute])

    return new_export


def add_export(zid, exports, new_export):
    if new_export.type == nfs.NFS_EXPORT_TYPE_TEMPLATE:
        logger.info('Discarding export with id %d' % new_export.id);
        return
    elif new_export.type == nfs.NFS_EXPORT_TYPE_PROTOTYPE:
        flags = nfs.NFS_FLAG_FORCE
    else:
        flags = nfs.NFS_FLAG_CREATE_WITH_ID | nfs.NFS_FLAG_FORCE

    logger.info('Adding export with id %d.' % new_export.id)
    try:
        exports.export_set(new_export.id, new_export, zid, flags)
    except isi.error.IsiError as err:
        logger.error('Failed to create export %d!\n'
                     'Error was: %s' % (new_export.id, str(err)))


def convert_all_exports_for_zone(zone_exports):
    zid = zone_exports['zid']
    logger.info('Converting exports for zone %d' % zid)

    # Remove any preexisting exports
    exports = nfs.Exports(zid, nfs.NFS_EXPORTS_LOAD_CREATE_CONFIG)
    for export_id in exports.exports_list():
        if export_id > 0: # Can't delete prototype/template
            exports.export_set(export_id, None, zid, nfs.NFS_FLAG_FORCE)
    exports.commit()

    exports = nfs.Exports(zid)

    # Convert the prototype
    new_prototype = convert_gconfig_export(
        zone_exports['exports']['prototype'])
    add_export(zid, exports, new_prototype)

    # Convert the rest of the exports
    for export in zone_exports['exports']['exports']:
        new_export = convert_gconfig_export(export)
        add_export(zid, exports, new_export)

    # XXX error check?
    exports.commit(zid)


def set_next_export_id_for_zone(namespace, last_export_id):
    next_export_id = int(last_export_id) + 1
    new_kv_set = tardis.KVSet()
    namespace.sync(new_kv_set)
    new_kv_set['next_export_id'] = struct.pack('i', next_export_id)
    namespace.update(new_kv_set)
    return


def convert_legacy_export(legacy_export):
    '''
    struct nfs_legacy_export {
        int id;
        char *path;
        uint64_t fid_lin;
        uint64_t fid_snapid;
        uint64_t fid_portal_depth;
        uint64_t fid_rootlin;
    };
    '''
    logger.info('Converting legacy export with id %d' % legacy_export['id'])

    pathlen = len(legacy_export['path']) + 1

    if pathlen == 1:
        logger.error(
            'Found malformed legacy export with id %d!' % legacy_export['id'])
        return None

    fmtstr = 'iQQQQQ%ds' % pathlen
    packed_export = struct.pack(fmtstr,
                                legacy_export['id'],
                                1L,    # Path char*
                                legacy_export['fid_lin'],
                                legacy_export['fid_snapid'],
                                legacy_export['fid_portal_depth'],
                                legacy_export['fid_rootlin'],
                                legacy_export['path'])

    return packed_export


def convert_legacy_exports(namespace, nfscfg):
    logger.info('Converting legacy exports')
    legacy_exports = nfscfg['legacy_exports']['exports']
    max_legacy_id = nfscfg['legacy_exports']['maximum_legacy_id']

    # If there is content in legacy exports, the id should be nonzero
    if len(legacy_exports) == 0 ^ max_legacy_id == 0:
        logger.warn('Legacy exports configuration failed sanity check!')
        return

    if len(legacy_exports) == 0 and max_legacy_id == 0:
        logger.info('No legacy exports found')
        return

    current_kv_set = tardis.KVSet()
    namespace.sync(current_kv_set)
    current_kv_set['export_max_legacy_id'] = struct.pack('i', max_legacy_id)
    namespace.update(current_kv_set)

    new_kv_set = tardis.KVSet()
    for legacy_export in legacy_exports:
        packed_legacy_export = convert_legacy_export(legacy_export)
        if packed_legacy_export is not None:
            new_kv_set['legacy_exports_%d' %
                       legacy_export['id']] = packed_legacy_export
    namespace.create(new_kv_set)

    logger.info('Converted legacy exports')

    return


def convert_zone_settings(namespace, zonecfg):
    '''
    struct nfs_zone_settings {
        char* zone_root_path;
        char* domain;
        uint32_t replace_domain;
        uint32_t no_domain;
        uint32_t no_domain_uids;
        uint32_t no_names;
        uint32_t allow_numeric_ids;
    };
    '''
    zid = zonecfg['zid']
    settings = zonecfg['settings']
    global_zone_settings = izone.get_zone_byid(zid)
    pathlen = len(global_zone_settings['path']) + 1
    domainlen = len(settings['domain']) + 1

    if pathlen == 1 or domainlen == 1:
        logger.error('Found malformed zone settings for zid %d!' % zid)
        return

    fmtstr = 'QQIIIIII%ds%ds' % (pathlen, domainlen)
    packed_zone_settings = struct.pack(fmtstr,
                                       1L,  # Zone root path char*
                                       1L,  # Domain char*
                                       int(settings['replace_domain']),
                                       int(settings['no_domain']),
                                       int(settings['no_domain_uids']),
                                       int(settings['no_names']),
                                       int(settings['allow_numeric_ids']),
                                       0L,  # 4-byte padding; hack to
                                            # accomodate non-packed c struct
                                            # (Bug 160102)
                                       global_zone_settings['path'],
                                       settings['domain'])
    new_kv_set = tardis.KVSet()
    namespace.sync(new_kv_set)
    new_kv_set['settings'] = packed_zone_settings
    namespace.update(new_kv_set)


def convert_alias(alias):
    '''
    struct nfs_alias {
        nfs_alias_id_t id;
        const char *path;
        enum nfs_alias_health_t health;
        struct isi_hash_entry name_hent;
        zid_t zone_id;
        char name[0];
    };
    '''
    logger.info('Converting alias %d' % alias['alias_id'])
    pathlen = len(alias['alias_path']) + 1
    namelen = len(alias['alias_name']) + 1

    if pathlen == 1 or namelen == 1:
        logger.error('Found malformed alias with id %d!' % alias['alias_id'])
        return None

    fmtstr = 'iQcQQ%ds%ds' % (namelen, pathlen)
    packed_alias = struct.pack(fmtstr,
                               alias['alias_id'],
                               1L,      # Path char*
                               '0',     # Health enum
                               0L,      # Hash entry
                               alias['zone_id'],
                               alias['alias_name'],
                               alias['alias_path'])
    return packed_alias


def convert_aliases(namespace, zonecfg):
    zid = zonecfg['zid']
    logger.info("Converting aliases for zone %d" % zid)
    new_kv_set = tardis.KVSet()
    namespace.sync(new_kv_set)
    for alias in zonecfg['aliases']['aliases']:
        packed_alias = convert_alias(alias)
        if packed_alias is not None:
            new_kv_set['aliases_%s' % alias['alias_name'].lstrip('/')] = packed_alias
    namespace.create(new_kv_set)
    logger.info("Converted aliases for zone %d" % zid)


def upgrade_from_moby():
    '''In Moby, the exports and zone settings existed in gconfig
    (nfs-config)'''
    logger.info("Converting Moby NFS settings and exports")
    ctx = gconfig.open(gconfig.gcfg_nfs_config).ctx_new()
    nfscfg = ctx.read('')

    # Sort the zones list so that we create the system zone first
    # (creating the system zone exports creates the generic nfs_config also)
    nfscfg['zone_list']['zones'].sort(key=lambda zone: zone['zid'])
    for zone in nfscfg['zone_list']['zones']:
        # Bug 168402 - We may encounter exports from deleted zones
        try:
            izone.get_zone_byid(zone['zid'])
        except isi.error.IsiSystemError as err:
            if err.error == errno.ENOENT:
                logger.warning("Skipping unrecognized zone %d" % zone['zid'])
                continue
            else:
                raise err

        convert_all_exports_for_zone(zone)
        namespace = tardis.Namespace('nfs_config_zone_%d' % zone['zid'])
        set_next_export_id_for_zone(namespace, nfscfg['last_export_id'])
        convert_zone_settings(namespace, zone)
        convert_aliases(namespace, zone)

        # Legacy (pre-Moby) exports exist only in the system zone
        if zone['zid'] == 1:
            convert_legacy_exports(namespace, nfscfg)

# Moby upgrades above this line
# Pre-moby upgrades below this line

def add_config(main_cfg, name, value):
    '''Translate the setting name to the lwreg equivalent and write it'''
    if len(name) and name in NFS_LWREG_CONVERSION_DICT.keys():
        try:
            value = long(value)
        except ValueError:
            pass
        try:
            main_cfg.write(LWREG_PREFIX+NFS_LWREG_CONVERSION_DICT[name], value)
        except TypeError:
            logger.error("Failed to migrate setting %s." % name)
        else:
            logger.info("Migrated setting %s." % name)


def sysctl_upgrade_conf(main_cfg):
    '''sysctl conf copy -> lwreg'''
    conf = ConfParser(SYSCTL_CONF)

    for sysctl in conf.values.iteritems():
        name = sysctl[0]
        value = sysctl[1]

        add_config(main_cfg, name, value)

    logger.info("Completed settings migration (1/2)")


def sysctl_upgrade_gc(main_cfg):
    '''sysctl gconfig tree copy -> lwreg'''
    old_sysctls = main_cfg.read(SYSCTL_PREFIX)

    for sysctl in old_sysctls:
        name = sysctl.get('name', '')
        value = sysctl.get('value', '')

        add_config(main_cfg, name, value)

    logger.info("Completed settings migration (2/2)")

def upgrade_mountd_ng_setting(main_cfg, value):
    '''Special case the showmount e setting, the only one which carried over'''
    name = 'md_ngs_in_showmount_e'
    try:
        value = long(value)
    except ValueError:
        pass
    try:
        main_cfg.write(LWREG_PREFIX+'MountdNgsInShowmountE', value)
    except TypeError:
        logger.error("Failed to migrate setting %s." % name)
    else:
        logger.info("Migrated setting %s." % name)


def get_gcfg_ctx(tree):
    '''Shorthand gconfig open + context create'''
    return gconfig.open(tree).ctx_new()

def jaws_sysctl_upgrade():
    '''Copy old sysctl settings to the lw registry (main config)'''
    for commit in xrange(MAX_COMMIT_RETRIES):
        try:
            nfs_cfg = get_gcfg_ctx(gconfig.gcfg_nfs_config)
            old_mount_ng_setting = nfs_cfg.read_create(path=SHOWMOUNT_NG_SETTING_PATH)
            main_cfg = get_gcfg_ctx(gconfig.gcfg_main)
            sysctl_upgrade_conf(main_cfg)
            sysctl_upgrade_gc(main_cfg)
            upgrade_mountd_ng_setting(main_cfg, old_mount_ng_setting)
            main_cfg.commit()
            break
        except gconfig.RecoverableCommitError:
            logger.info("Retrying sysctl upgrade commit (%d)." % commit)
    # Ran out of retries
    else:
        raise RuntimeError("NFS upgrade could not commit NFS gconfig")


def jaws_exports_upgrade():
    logger.info("Export conversion started. "
                "See /var/log/nfs_convert.log for more details.")
    process = subprocess.Popen('isi_nfs_convert > /dev/null 2>&1', shell=True)
    process.wait()
    if process.returncode == 0:
        logger.info("Successfully converted NFS exports")
    else:
        raise RuntimeError("Failed to convert NFS exports")

def safe_sysctlbyname(sysctl, default):
    '''A pessimistic sysctlbyname that falls back to the provided default'''
    val = None
    for store in ('conf', 'gcfg'):
        try:
            val = cluster_get_sysctl(sysctl, store)
            break
        except isi.misc.error: # 'unknown oid'
            continue
    if val is None:
        val = default
    return val

def jaws_zone_settings_upgrade():
    namespace = tardis.Namespace('nfs_config_zone_1')
    domainname = safe_sysctlbyname("vfs.nfsrv.nfsv4.domainname", "localhost")
    path = "/ifs"
    pathlen = len(path) + 1
    domainlen = len(domainname) + 1

    if pathlen == 1 or domainlen == 1:
        logger.error('Found malformed zone settings for system zone!')
        return

    fmtstr = 'QQIIIIII%ds%ds' % (pathlen, domainlen)
    packed_zone_settings = struct.pack(
        fmtstr,
        1L,  # Zone root path char*
        1L,  # Domain char*
        int(safe_sysctlbyname("vfs.nfsrv.nfsv4.idmap_replacedomain", 1)),
        int(safe_sysctlbyname("vfs.nfsrv.nfsv4.idmap_nodomain", 0)),
        int(safe_sysctlbyname("vfs.nfsrv.nfsv4.idmap_nodomain_uids", 1)),
        int(safe_sysctlbyname("vfs.nfsrv.nfsv4.idmap_nonames", 0)),
        1,   # Allow numeric IDs
        0L,  # 4-byte pad; hack to accomodate non-packed c struct (Bug 160102)
        path,
        domainname)

    new_kv_set = tardis.KVSet()
    namespace.sync(new_kv_set)
    new_kv_set['settings'] = packed_zone_settings
    namespace.update(new_kv_set)


def upgrade_from_pre_moby():
    '''Pre-moby, the exports existed in XML format and the server settings
    were stored as sysctls in either mcp sysctl.conf or gconfig sysctl tree'''
    logger.info("Converting pre-Moby NFS settings and exports")
    jaws_sysctl_upgrade()
    jaws_exports_upgrade()
    jaws_zone_settings_upgrade()

def build_scheduler_config():
    build_function = nfs_sched_config_lib.NfsSchedulerConfigBuild
    build_function.argtypes = []
    build_function.restype = c_int

    error = build_function()

    if error != 0:
        logger.info("Failed to build scheduler config with status: %d" % error)


def main():
    sentry = None
    from_moby = os.path.exists(MOBY_NFS_UPGRADED_FILE) # JDR cleaner way to check?

    try:
        lnn = "Node %d -" % iconf.getNode()['node id']
        lfmt = lnn + "%(asctime)s - %(levelname)s - %(message)s"
        logger.setLevel(logging.INFO)
        logfh = logging.FileHandler(LOG_FILE, 'a')
        logfh.setLevel(logging.INFO)
        logfh.setFormatter(logging.Formatter(lfmt))
        logger.addHandler(logfh)

    except EnvironmentError as err:
        syslog.syslog(syslog.LOG_NOTICE,
                      "Error running nfs upgrade script: %s" % err)
        return 1

    try:
        os.chmod(LOG_FILE, 0640)
    except OSError as err:
        syslog.syslog(syslog.LOG_NOTICE,
                      "Error chmod'ing %s (errno: %d)" % (LOG_FILE, err.errno))

    logger.info("NFS exports upgrade started.")

    if os.path.exists(NFS_EXPORTS_UPGRADED_FILE):
        logger.info("NFS exports upgrade has already occurred successfully.")
        return 0

    # Block on the worker lock
    try:
        sentry = safelockfile(NFS_EXPORTS_UPGRADE_LOCKFILE, 'w')
    except EnvironmentError as err:
        syslog.syslog(syslog.LOG_NOTICE,
                      "Error running NFS exports upgrade script: %s" % err)
        if sentry:
            sentry.close()
        return 1

    # Finished while we were waiting?
    if os.path.exists(NFS_EXPORTS_UPGRADED_FILE):
        logger.info("NFS exports upgrade has already occurred successfully.")
        if sentry:
            sentry.close()
        return 0

    build_scheduler_config()

    if from_moby:
        upgrade_from_moby()
    else:
        upgrade_from_pre_moby()

    logger.info('Completed exports upgrade.')

    print >> file(NFS_EXPORTS_UPGRADED_FILE, 'w'), "Upgrade was successful"
    os.chmod(NFS_EXPORTS_UPGRADED_FILE, 0640)

    if sentry:
        sentry.close()
    return 0

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