- use invirt.config to get hostnames
authorYang Zhang <y_z@mit.edu>
Sun, 3 Aug 2008 00:28:18 +0000 (20:28 -0400)
committerYang Zhang <y_z@mit.edu>
Sun, 3 Aug 2008 00:28:18 +0000 (20:28 -0400)
- refactoring: extracted bcast() function into invirt.remote package
- fixed os.rename import bug
- using correct default paths in invirt-getconf

svn path=/trunk/packages/sipb-xen-base/; revision=816

files/usr/sbin/invirt-getconf
files/usr/share/python-support/sipb-xen-base/invirt/config.py
files/usr/share/python-support/sipb-xen-base/invirt/remote.py [new file with mode: 0644]

index 4721212..bb4fd80 100755 (executable)
@@ -16,7 +16,7 @@ Examples:
   invirt-getconf authn.0.type
 """
 
   invirt-getconf authn.0.type
 """
 
-from invirt.config import load
+from invirt.config import default_src_path, default_cache_path, load
 from sys import argv, exit, stderr, stdout
 from optparse import OptionParser
 
 from sys import argv, exit, stderr, stdout
 from optparse import OptionParser
 
@@ -27,10 +27,10 @@ def main(argv):
         parser = OptionParser(usage = '%prog [options] key',
                 description = __doc__.strip().split('\n\n')[0])
         parser.add_option('-s', '--src',
         parser = OptionParser(usage = '%prog [options] key',
                 description = __doc__.strip().split('\n\n')[0])
         parser.add_option('-s', '--src',
-                default = '/etc/invirt/master.yaml',
+                default = default_src_path,
                 help = 'the source YAML configuration file to read from')
         parser.add_option('-c', '--cache',
                 help = 'the source YAML configuration file to read from')
         parser.add_option('-c', '--cache',
-                default = '/var/lib/invirt/invirt.json',
+                default = default_cache_path,
                 help = 'path to the JSON cache')
         parser.add_option('-r', '--refresh',
                 action = 'store_true',
                 help = 'path to the JSON cache')
         parser.add_option('-r', '--refresh',
                 action = 'store_true',
@@ -80,8 +80,10 @@ def main(argv):
                 print conf
             else:
                 import yaml
                 print conf
             else:
                 import yaml
+                try:    dumper = yaml.CSafeDumper
+                except: dumper = yaml.SafeDumper
                 yaml.dump(conf, stdout,
                 yaml.dump(conf, stdout,
-                          Dumper=yaml.CSafeDumper, default_flow_style=False)
+                          Dumper = dumper, default_flow_style = False)
     except invirt_exception, ex:
         print >> stderr, ex
         return 1
     except invirt_exception, ex:
         print >> stderr, ex
         return 1
index cead926..8648c70 100644 (file)
@@ -1,5 +1,6 @@
 import json
 from invirt.common import *
 import json
 from invirt.common import *
+from os import rename
 from os.path import getmtime
 
 default_src_path   = '/etc/invirt/master.yaml'
 from os.path import getmtime
 
 default_src_path   = '/etc/invirt/master.yaml'
@@ -74,7 +75,7 @@ def load(src_path = default_src_path,
                 try: with_closing(file(cache_path + '.tmp', 'w')) (
                         lambda f: f.write(json.write(ns.cfg)))
                 except: pass # silent failure
                 try: with_closing(file(cache_path + '.tmp', 'w')) (
                         lambda f: f.write(json.write(ns.cfg)))
                 except: pass # silent failure
-                else: os.rename(cache_path + '.tmp', cache_path)
+                else: rename(cache_path + '.tmp', cache_path)
         except IOError:
             ns.cfg = with_closing(file(src_path)) (
                     lambda f: yaml.load(f, loader))
         except IOError:
             ns.cfg = with_closing(file(src_path)) (
                     lambda f: yaml.load(f, loader))
diff --git a/files/usr/share/python-support/sipb-xen-base/invirt/remote.py b/files/usr/share/python-support/sipb-xen-base/invirt/remote.py
new file mode 100644 (file)
index 0000000..0d7dba2
--- /dev/null
@@ -0,0 +1,19 @@
+from subprocess import PIPE, Popen
+from invirt.config import structs as config
+import yaml
+
+def bcast(cmd, hosts = [h.hostname for h in config.hosts]):
+    """
+    Given a command and a list of hostnames or IPs, issue the command to all
+    the nodes and return a list of (host, output) pairs (the order should be
+    the same as the order of the hosts).
+    """
+    pipes = [(host,
+              Popen(['remctl', host, 'remote', 'web', cmd], stdout=PIPE))
+             for host in hosts]
+    outputs = [(s, p.communicate()[0]) for (s, p) in pipes]
+    for (s, p) in pipes:
+        if p.returncode != 0:
+            raise RuntimeError("remctl to host %s returned non-zero exit status %d"
+                               % (s, p.returncode))
+    return [(s, yaml.load(o, yaml.CSafeLoader)) for (s, o) in outputs]