#!/usr/bin/env python

#
# This script allows the adminstrator to toggle SmartConnect
# ignoring nodes with down services in their response. This allows
# other protocols to continue to function if a single service
# cannot start, potentially causing DU.
#
import os
from optparse import OptionParser
import isi.net.flexnet as flx

def get_ignore_proto_down():
    config = flx.config_try_load()
    retval = config.ignore_protocol_down
    config.close()
    return retval

def set_ignore_proto_down(value):
    config = flx.config_open_exclusive()
    config.ignore_protocol_down = value
    config.write(False)
    config.close()

if __name__ == "__main__":

    try:
        usage = "usage: %prog []"
        parser = OptionParser(usage)
        parser.add_option("-e", "--enable", dest="ignore",
                          help="Ignore services (default action)",
                          action="store_true", default=True)
        parser.add_option("-d", "--disable", dest='ignore',
                          help="Revert setting ignore state",
                          action="store_false")
        parser.add_option("-p", "--print", dest="display",
                          help="Print the current ignore state",
                          action="store_true", default=False)
        (options, args) = parser.parse_args()

        if options.display:
            print "%r" % get_ignore_proto_down()
        else:
            set_ignore_proto_down(options.ignore)
    except Exception as e:
        print "%s failed: %s" % (os.path.basename(__file__), str(e))
