#!/usr/local/bin/python

# Copyright C 2001-2005 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

### Isilon configure services
#
# This script configures any services that are platform dependent.
# In the future, this functionality should be automatically handled
# by MCP but for now we do it this way.

# PROVIDE: isi_configure_services
# REQUIRE: isi_first_update isi_configure_dons

import os
import os.path
import sys

import isi.config.mcp as mcp
import isi.hw.hal as hal

ETHMIXER_IB = "/etc/mcp/sys/ethmixer_ib.xml"
ETHMIXER_ETH = "/etc/mcp/sys/ethmixer_eth.xml"
ETHMIXER_SYS = "/etc/mcp/sys/ethmixer.xml"
OSMMON_OVERRIDE = "/etc/mcp/override/isi_opensm_monitor"

print "Configuring hardware-specific MCP services..."

mc = mcp.mcpconfig()
mc.showall()

#
# InfiniBand platforms require OpenSM
#
if hal.hasInfiniband():
    print "\tInfiniBand hardware detected."
    try:
        if not os.path.exists(ETHMIXER_SYS):
            print "\tInfiniBand: configuring Ethmixer."
            os.link(ETHMIXER_IB, ETHMIXER_SYS)
    except:
        print "\tError configuring Ethmixer for InfiniBand back-end."
        raise

    print "\tInfiniBand: enabling OpenSM services."
    try:
        mc.enable('opensm_ib0')
        mc.enable('opensm_ib1')
        print "\tOpenSM services enabled."
    except mcp.error, strerror:
        print "\tMCP Error enabling OpenSM service: %s." % strerror
        raise
    except:
        print ("\tUnexpected error enabling OpenSM service: %s; %s." %
               (sys.exc_info()[:2]))
        raise

    # Only enable the service if the override does not exist
    if not os.path.exists(OSMMON_OVERRIDE):
        try:
            mc.enable('isi_opensm_monitor')
            print "\tisi_opensm_monitor service enabled."
        except mcp.error, strerror:
            print ("\tMCP Error enabling isi_opensm_monitor service: %s." %
                    strerror)
            raise
        except:
            print ("\tUnexpected error enabling isi_opensm_monitor service: "
                    "%s; %s." % (sys.exc_info()[:2]))
            raise

else:
    print "\tNo InfiniBand hardware detected."
    try:
        if not os.path.exists(ETHMIXER_SYS):
            print "\tEthernet: configuring Ethmixer."
            os.link(ETHMIXER_ETH, ETHMIXER_SYS)
    except:
        print "\tError configuring Ethmixer for Ethernet back-end."
        raise

#
# SAS PHY Monitoring
#
if hal.hasSasPhyMonitoring():
    print "\tSAS PHY Monitoring hardware detected; " \
          "enabling SAS PHY Monitor services"
    try:
        mc.enable('isi_sasphymon')
        print "\tSAS PHY Monitor services enabled."
    except mcp.error, strerror:
        print "\tMCP Error enabling SAS PHY Monitor service: %s" % strerror
        raise
    except:
        print ("\tUnexpected error enabling SAS PHY Monitor " \
               " service: %s; %s" % (sys.exc_info()[:2]))
        raise
else:
    print "\tNo SAS PHY Monitoring hardware detected."

#
# LCD (Front Panel Display and Keyboard) Service
#
if hal.hasFrontPanelDisplay() and hal.hasFrontPanelKeyboard():
    try:
        mc.enable('isi_lcd')
        print "\tLCD services enabled."
    except mcp.error, strerror:
        print "\tMCP Error enabling LCD service: %s" % strerror
        raise
    except:
        print "\tUnexpected error enabling LCD service: %s; %s" % \
              (sys.exc_info()[:2])
        raise
else:
    print "\tNo LCD hardware detected."
#
# BMC Sensor Monitoring
#
if hal.hasBmcSensorMonitoring():
    print "\tBMC Sensor Monitoring hardware detected; " \
          "enabling HW Monitor services"
    try:
        mc.enable('isi_hwmon')
        print "\tHW Monitor services enabled."
    except mcp.error, strerror:
        print "\tMCP Error enabling HW Monitor service: %s" % strerror
        raise
    except:
        print ("\tUnexpected error enabling HW Monitor " \
               " service: %s; %s" % (sys.exc_info()[:2]))
        raise
else:
    print "\tNo BMC Sensor Monitoring hardware detected."
