#!/usr/bin/env python
""" Shutdown all back-end internal interfaces for this node. """

import sys
import subprocess

import isi.app.lib.cluster as clusterlib
import isi.net.flexnet as flx

def iface_down_cmd(iface):
    """ execv-style ifconfig-down command. """
    cmd = ["/sbin/ifconfig", iface, "down"]
    print ' '.join(cmd)
    subprocess.call(cmd)

def main():
    """ Loads the cluster configuration and downs all interfaces marked as internal. """
    print "Downing internal interfaces..."

    try:
        cluster = clusterlib.Cluster()
        local_devid = cluster.local()
        for iface in cluster.local().interfaces():
            if iface.flags & flx.IFFLAG_INTERNAL:
                iface_down_cmd(iface.nic.name)
    except Exception, e:
        print ("Error loading configuration: %s" % str(e))
        print ("Downing InfiniBand interfaces...")
        ibifaces = [ibiface for ibiface in
                subprocess.Popen(["/sbin/ifconfig", "-l"],
                    stdout=subprocess.PIPE).communicate()[0].strip().split()
                if ibiface.startswith("ib")]
        for iface in ibifaces:
            iface_down_cmd(iface)

if __name__ == '__main__':
    main()
