Make the plnet module a script that uses plcsh to get info for a node.
authorDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Thu, 4 Dec 2008 19:41:50 +0000 (19:41 +0000)
committerDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Thu, 4 Dec 2008 19:41:50 +0000 (19:41 +0000)
plnet.py [changed mode: 0644->0755]
pyplnet.spec

old mode 100644 (file)
new mode 100755 (executable)
index dfb7fb6..272fa00
--- a/plnet.py
+++ b/plnet.py
@@ -1,3 +1,4 @@
+#!/usr/bin/python /usr/bin/plcsh
 # $Id$
 
 import os
@@ -166,8 +167,9 @@ def InitInterfaces(logger, plc, data, root="", files_only=False):
         for ifcfg in ifcfgs:
             dev = ifcfg[len('ifcfg-'):]
             path = "%s/ifcfg-%s" % (sysconfig,dev)
-            logger.verbose("net:InitInterfaces removing %s %s"%(dev,path))
-            os.system("/sbin/ifdown %s" % dev)
+            if not files_only:
+                logger.verbose("net:InitInterfaces removing %s %s"%(dev,path))
+                os.system("/sbin/ifdown %s" % dev)
             deletedSomething=True
             os.unlink(path)
 
@@ -230,11 +232,12 @@ def InitInterfaces(logger, plc, data, root="", files_only=False):
             
         elif not comparefiles(tmpnam,path):
             logger.verbose('net:InitInterfaces Configuration change for %s' % dev)
-            logger.verbose('net:InitInterfaces ifdown %s' % dev)
-            # invoke ifdown for the old configuration
-            os.system("/sbin/ifdown %s" % dev)
-            # wait a few secs for ifdown to complete
-            time.sleep(2)
+            if not files_only:
+                logger.verbose('net:InitInterfaces ifdown %s' % dev)
+                # invoke ifdown for the old configuration
+                os.system("/sbin/ifdown %s" % dev)
+                # wait a few secs for ifdown to complete
+                time.sleep(2)
 
             logger.log('replacing configuration for %s' % dev)
             # replace ifcfg-$dev configuration file
@@ -275,6 +278,37 @@ def InitInterfaces(logger, plc, data, root="", files_only=False):
         # handle those correctly
         if getvar("SLAVE") == 'yes': continue
 
-        logger.verbose('net:InitInterfaces bringing up %s' % dev)
-        os.system("/sbin/ifup %s" % dev)
-
+        if not files_only:
+            logger.verbose('net:InitInterfaces bringing up %s' % dev)
+            os.system("/sbin/ifup %s" % dev)
+
+if __name__ == "__main__":
+    import optparse
+    import sys
+
+    parser = optparse.OptionParser()
+    parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
+    parser.add_option("-r", "--root", action="store", type="string",
+                      dest="root", default=None)
+    parser.add_option("-f", "--files-only", action="store_true",
+                      dest="files_only")
+    (options, args) = parser.parse_args()
+    if len(args) != 1 or options.root is None:
+        print >>sys.stderr, \
+            "Usage: %s [-v] [-f] -r <root> node_id" % sys.argv[0]
+        sys.exit(1)
+
+    node = shell.GetNodes({'node_id': [int(args[0])]})
+    networks = shell.GetInterfaces({'interface_id': node[0]['interface_ids']})
+
+    data = {'hostname': node[0]['hostname'], 'networks': networks}
+    class logger:
+        def __init__(self, verbose):
+            self.verbosity = verbose
+        def log(self, msg, loglevel=2):
+            if self.verbosity:
+                print msg
+        def verbose(self, msg):
+            self.log(msg, 1)
+    l = logger(options.verbose)
+    InitInterfaces(l, shell, data, options.root, options.files_only)
index bf443ee..7d3ed30 100644 (file)
@@ -41,12 +41,15 @@ python setup.py build
 %install
 rm -rf $RPM_BUILD_ROOT
 python setup.py install --skip-build --root "$RPM_BUILD_ROOT"
+chmod +x %{python_sitearch}/plnet.py
+ln -s %{python_sitearch}/plnet.py %{_bindir}/plnet
 
 %clean
 rm -rf $RPM_BUILD_ROOT
 
 %files
 %defattr(-,root,root,-)
+%{_bindir}/plnet
 %{python_sitearch}/*
 
 %changelog