[haizea-commit] r473 - in trunk: bin src/haizea/cli

haizea-commit at mailman.cs.uchicago.edu haizea-commit at mailman.cs.uchicago.edu
Sat Aug 30 14:38:52 CDT 2008


Author: borja
Date: 2008-08-30 14:38:52 -0500 (Sat, 30 Aug 2008)
New Revision: 473

Modified:
   trunk/bin/haizea
   trunk/bin/haizea-cancel-lease
   trunk/bin/haizea-convert-data
   trunk/bin/haizea-generate-configs
   trunk/bin/haizea-generate-scripts
   trunk/bin/haizea-list-leases
   trunk/bin/haizea-request-lease
   trunk/src/haizea/cli/__init__.py
   trunk/src/haizea/cli/commands.py
   trunk/src/haizea/cli/rpc_commands.py
Log:
- Switched CLI implementation from functions to classes, mostly so they can be self-documenting
- Improved console pretty-printing
- Added haizea-show-queue and haizea-list-hosts

Modified: trunk/bin/haizea
===================================================================
--- trunk/bin/haizea	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/bin/haizea	2008-08-30 19:38:52 UTC (rev 473)
@@ -8,4 +8,5 @@
 except:
 	pass
 	
-commands.haizea(sys.argv)
\ No newline at end of file
+c = commands.haizea(sys.argv)
+c.run()
\ No newline at end of file

Modified: trunk/bin/haizea-cancel-lease
===================================================================
--- trunk/bin/haizea-cancel-lease	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/bin/haizea-cancel-lease	2008-08-30 19:38:52 UTC (rev 473)
@@ -3,4 +3,5 @@
 from haizea.cli import rpc_commands
 import sys
 
-rpc_commands.haizea_cancel_lease(sys.argv)
\ No newline at end of file
+c = rpc_commands.haizea_cancel_lease(sys.argv)
+c.run()
\ No newline at end of file

Modified: trunk/bin/haizea-convert-data
===================================================================
--- trunk/bin/haizea-convert-data	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/bin/haizea-convert-data	2008-08-30 19:38:52 UTC (rev 473)
@@ -3,4 +3,5 @@
 from haizea.cli import commands
 import sys
 	
-commands.haizea_convert_data(sys.argv)
\ No newline at end of file
+c = commands.haizea_convert_data(sys.argv)
+c.run()
\ No newline at end of file

Modified: trunk/bin/haizea-generate-configs
===================================================================
--- trunk/bin/haizea-generate-configs	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/bin/haizea-generate-configs	2008-08-30 19:38:52 UTC (rev 473)
@@ -3,4 +3,5 @@
 from haizea.cli import commands
 import sys
 
-commands.haizea_generate_configs(sys.argv)
\ No newline at end of file
+c = commands.haizea_generate_configs(sys.argv)
+c.run()
\ No newline at end of file

Modified: trunk/bin/haizea-generate-scripts
===================================================================
--- trunk/bin/haizea-generate-scripts	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/bin/haizea-generate-scripts	2008-08-30 19:38:52 UTC (rev 473)
@@ -3,4 +3,5 @@
 from haizea.cli import commands
 import sys
 
-commands.haizea_generate_scripts(sys.argv)
\ No newline at end of file
+c = commands.haizea_generate_scripts(sys.argv)
+c.run()
\ No newline at end of file

Modified: trunk/bin/haizea-list-leases
===================================================================
--- trunk/bin/haizea-list-leases	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/bin/haizea-list-leases	2008-08-30 19:38:52 UTC (rev 473)
@@ -3,4 +3,5 @@
 from haizea.cli import rpc_commands
 import sys
 
-rpc_commands.haizea_list_leases(sys.argv)
\ No newline at end of file
+c = rpc_commands.haizea_list_leases(sys.argv)
+c.run()
\ No newline at end of file

Modified: trunk/bin/haizea-request-lease
===================================================================
--- trunk/bin/haizea-request-lease	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/bin/haizea-request-lease	2008-08-30 19:38:52 UTC (rev 473)
@@ -3,4 +3,5 @@
 from haizea.cli import rpc_commands
 import sys
 
-rpc_commands.haizea_request_lease(sys.argv)
\ No newline at end of file
+c = rpc_commands.haizea_request_lease(sys.argv)
+c.run()
\ No newline at end of file

Modified: trunk/src/haizea/cli/__init__.py
===================================================================
--- trunk/src/haizea/cli/__init__.py	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/src/haizea/cli/__init__.py	2008-08-30 19:38:52 UTC (rev 473)
@@ -0,0 +1,12 @@
+from haizea.cli.optionparser import OptionParser
+
+class Command(object):
+    
+    def __init__(self, argv):
+        self.argv = argv
+        self.optparser = OptionParser()
+        
+    def parse_options(self):
+        opt, args = self.optparser.parse_args(self.argv)
+        self.opt = opt
+        
\ No newline at end of file

Modified: trunk/src/haizea/cli/commands.py
===================================================================
--- trunk/src/haizea/cli/commands.py	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/src/haizea/cli/commands.py	2008-08-30 19:38:52 UTC (rev 473)
@@ -21,188 +21,196 @@
 from haizea.resourcemanager.configfile import HaizeaConfig, HaizeaMultiConfig
 from haizea.common.config import ConfigException
 from haizea.cli.optionparser import OptionParser, Option
+from haizea.cli import Command
+import haizea.common.defaults as defaults
 import sys
 import os
-import os.path
 import errno
 import signal
 import time
 
-DEFAULT_CONFIG_LOCATIONS = ["/etc/haizea.conf",
-                            os.path.expanduser("~/.haizea/haizea.conf")]
-DEFAULT_PIDFILE = "/var/tmp/haizea.pid"
 
-
-def haizea(argv):
-    p = OptionParser()
-    p.add_option(Option("-c", "--conf", action="store", type="string", dest="conf"))
-    p.add_option(Option("-f", "--fg", action="store_true",  dest="foreground"))
-    p.add_option(Option("--stop", action="store_true",  dest="stop"))
-
-    opt, args = p.parse_args(argv)
+class haizea(Command):
+    def __init__(self, argv):
+        Command.__init__(self, argv)
         
-    pidfile = DEFAULT_PIDFILE # TODO: Make configurable
+        self.optparser.add_option(Option("-c", "--conf", action="store", type="string", dest="conf"))
+        self.optparser.add_option(Option("-f", "--fg", action="store_true",  dest="foreground"))
+        self.optparser.add_option(Option("--stop", action="store_true",  dest="stop"))
+        
+        self.parse_options()
+        
+    def run(self):
+        pidfile = defaults.DAEMON_PIDFILE # TODO: Make configurable
 
-    if opt.stop == None:
-        # Start Haizea
-         
-        # Check if a daemon is already running
-        if os.path.exists(pidfile):
-            pf  = file(pidfile,'r')
-            pid = int(pf.read().strip())
-            pf.close()
- 
-            try:
-                os.kill(pid, signal.SIG_DFL)
-            except OSError, (err, msg):
-                if err == errno.ESRCH:
-                    # Pidfile is stale. Remove it.
-                    os.remove(pidfile)
+        if self.opt.stop == None:
+            # Start Haizea
+             
+            # Check if a daemon is already running
+            if os.path.exists(pidfile):
+                pf  = file(pidfile,'r')
+                pid = int(pf.read().strip())
+                pf.close()
+     
+                try:
+                    os.kill(pid, signal.SIG_DFL)
+                except OSError, (err, msg):
+                    if err == errno.ESRCH:
+                        # Pidfile is stale. Remove it.
+                        os.remove(pidfile)
+                    else:
+                        msg = "Unexpected error when checking pid file '%s'.\n%s\n" %(pidfile, msg)
+                        sys.stderr.write(msg)
+                        sys.exit(1)
                 else:
-                    msg = "Unexpected error when checking pid file '%s'.\n%s\n" %(pidfile, msg)
+                    msg = "Haizea seems to be already running (pid %i)\n" % pid
                     sys.stderr.write(msg)
                     sys.exit(1)
-            else:
-                msg = "Haizea seems to be already running (pid %i)\n" % pid
-                sys.stderr.write(msg)
+     
+            try:
+                configfile=self.opt.conf
+                if configfile == None:
+                    # Look for config file in default locations
+                    for loc in defaults.CONFIG_LOCATIONS:
+                        if os.path.exists(loc):
+                            config = HaizeaConfig.from_file(loc)
+                            break
+                    else:
+                        print >> sys.stdout, "No configuration file specified, and none found at default locations."
+                        print >> sys.stdout, "Make sure a config file exists at:\n  -> %s" % "\n  -> ".join(DEFAULT_CONFIG_LOCATIONS)
+                        print >> sys.stdout, "Or specify a configuration file with the --conf option."
+                        exit(1)
+                else:
+                    config = HaizeaConfig.from_file(configfile)
+            except ConfigException, msg:
+                print >> sys.stderr, "Error in configuration file:"
+                print >> sys.stderr, msg
+                exit(1)
+                
+            daemon = not self.opt.foreground
+        
+            rm = ResourceManager(config, daemon, pidfile)
+        
+            rm.start()
+        elif self.opt.stop: # Stop Haizea
+            # Based on code in:  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
+            try:
+                pf  = file(pidfile,'r')
+                pid = int(pf.read().strip())
+                pf.close()
+            except IOError:
+                msg = "Could not stop, pid file '%s' missing.\n"
+                sys.stderr.write(msg % pidfile)
                 sys.exit(1)
- 
+            try:
+               while 1:
+                   os.kill(pid, signal.SIGTERM)
+                   time.sleep(1)
+            except OSError, err:
+               err = str(err)
+               if err.find("No such process") > 0:
+                   os.remove(pidfile)
+               else:
+                   print str(err)
+                   sys.exit(1)
+
+class haizea_generate_configs(Command):
+    def __init__(self, argv):
+        Command.__init__(self, argv)
+        
+        self.optparser.add_option(Option("-c", "--conf", action="store", type="string", dest="conf", required=True))
+        self.optparser.add_option(Option("-d", "--dir", action="store", type="string", dest="dir", required=True))
+        
+        self.parse_options()
+        
+    def run(self):    
+        configfile=self.opt.conf
+        multiconfig = HaizeaMultiConfig.from_file(configfile)
+        
+        dir = self.opt.dir
+        
+        configs = multiconfig.get_configs()
+        
+        etcdir = os.path.abspath(dir)    
+        if not os.path.exists(etcdir):
+            os.makedirs(etcdir)
+            
+        for c in configs:
+            profile = c.get_attr("profile")
+            tracefile = c.get("tracefile")
+            injfile = c.get("injectionfile")
+            configname = generate_config_name(profile, tracefile, injfile)
+            configfile = etcdir + "/%s.conf" % configname
+            fc = open(configfile, "w")
+            c.config.write(fc)
+            fc.close()
+
+class haizea_generate_scripts(Command):
+    def __init__(self, argv):
+        Command.__init__(self, argv)
+        
+        self.optparser.add_option(Option("-c", "--conf", action="store", type="string", dest="conf", required=True))
+        self.optparser.add_option(Option("-t", "--template", action="store", type="string", dest="template", required=True))
+        self.optparser.add_option(Option("-d", "--confdir", action="store", type="string", dest="confdir", required=True))
+        self.optparser.add_option(Option("-m", "--only-missing", action="store_true",  dest="onlymissing"))
+        
+        self.parse_options()
+        
+    def run(self):        
+        configfile=self.opt.conf
+        multiconfig = HaizeaMultiConfig.from_file(configfile)
+                
         try:
-            configfile=opt.conf
-            if configfile == None:
-                # Look for config file in default locations
-                for loc in DEFAULT_CONFIG_LOCATIONS:
-                    if os.path.exists(loc):
-                        config = HaizeaConfig.from_file(loc)
-                        break
-                else:
-                    print >> sys.stdout, "No configuration file specified, and none found at default locations."
-                    print >> sys.stdout, "Make sure a config file exists at:\n  -> %s" % "\n  -> ".join(DEFAULT_CONFIG_LOCATIONS)
-                    print >> sys.stdout, "Or specify a configuration file with the --conf option."
-                    exit(1)
-            else:
-                config = HaizeaConfig.from_file(configfile)
-        except ConfigException, msg:
-            print >> sys.stderr, "Error in configuration file:"
-            print >> sys.stderr, msg
+            from mako.template import Template
+        except:
+            print "You need Mako Templates for Python to run this command."
+            print "You can download them at http://www.makotemplates.org/"
             exit(1)
+    
+        configs = multiconfig.get_configs()
+        
+        etcdir = os.path.abspath(self.opt.confdir)    
+        if not os.path.exists(etcdir):
+            os.makedirs(etcdir)
             
-        daemon = not opt.foreground
+        templatedata = []    
+        for c in configs:
+            profile = c.get_attr("profile")
+            tracefile = c.get("tracefile")
+            injfile = c.get("injectionfile")
+            datafile = c.get("datafile")
+            configname = generate_config_name(profile, tracefile, injfile)
+            if not self.opt.onlymissing or not os.path.exists(datafile):
+                configfile = etcdir + "/%s.conf" % configname
+                templatedata.append((configname, configfile))
     
-        rm = ResourceManager(config, daemon, pidfile)
-    
-        rm.start()
-    elif opt.stop: # Stop Haizea
-        # Based on code in:  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
-        try:
-            pf  = file(pidfile,'r')
-            pid = int(pf.read().strip())
-            pf.close()
-        except IOError:
-            msg = "Could not stop, pid file '%s' missing.\n"
-            sys.stderr.write(msg % pidfile)
-            sys.exit(1)
-        try:
-           while 1:
-               os.kill(pid, signal.SIGTERM)
-               time.sleep(1)
-        except OSError, err:
-           err = str(err)
-           if err.find("No such process") > 0:
-               os.remove(pidfile)
-           else:
-               print str(err)
-               sys.exit(1)
+        template = Template(filename=self.opt.template)
+        print template.render(configs=templatedata, etcdir=etcdir)
 
 
-def haizea_generate_configs(argv):
-    p = OptionParser()
-    p.add_option(Option("-c", "--conf", action="store", type="string", dest="conf", required=True))
-    p.add_option(Option("-d", "--dir", action="store", type="string", dest="dir", required=True))
-
-    opt, args = p.parse_args(argv)
-    
-    configfile=opt.conf
-    multiconfig = HaizeaMultiConfig.from_file(configfile)
-    
-    dir = opt.dir
-    
-    configs = multiconfig.get_configs()
-    
-    etcdir = os.path.abspath(dir)    
-    if not os.path.exists(etcdir):
-        os.makedirs(etcdir)
+class haizea_convert_data(Command):
+    def __init__(self, argv):
+        Command.__init__(self, argv)
         
-    for c in configs:
-        profile = c.get_attr("profile")
-        tracefile = c.get("tracefile")
-        injfile = c.get("injectionfile")
-        configname = generate_config_name(profile, tracefile, injfile)
-        configfile = etcdir + "/%s.conf" % configname
-        fc = open(configfile, "w")
-        c.config.write(fc)
-        fc.close()
-                        
-def haizea_generate_scripts(argv):
-    p = OptionParser()
-    p.add_option(Option("-c", "--conf", action="store", type="string", dest="conf", required=True))
-    p.add_option(Option("-t", "--template", action="store", type="string", dest="template", required=True))
-    p.add_option(Option("-d", "--confdir", action="store", type="string", dest="confdir", required=True))
-    p.add_option(Option("-m", "--only-missing", action="store_true",  dest="onlymissing"))
-
-    opt, args = p.parse_args(argv)
-    
-    configfile=opt.conf
-    multiconfig = HaizeaMultiConfig.from_file(configfile)
-            
-    try:
-        from mako.template import Template
-    except:
-        print "You need Mako Templates for Python to run this command."
-        print "You can download them at http://www.makotemplates.org/"
-        exit(1)
-
-    configs = multiconfig.get_configs()
-    
-    etcdir = os.path.abspath(opt.confdir)    
-    if not os.path.exists(etcdir):
-        os.makedirs(etcdir)
+        self.optparser.add_option(Option("-d", "--datafiles", action="store", type="string", dest="datafiles", required=True))
+        self.optparser.add_option(Option("-s", "--summary", action="store_true",  dest="summary"))
+        self.optparser.add_option(Option("-l", "--lease-stats", action="store", type="string", dest="lease"))
+        self.optparser.add_option(Option("-t", "--include-attributes", action="store_true", dest="attributes"))
+        self.optparser.add_option(Option("-f", "--format", action="store", type="string", dest="format"))
         
-    templatedata = []    
-    for c in configs:
-        profile = c.get_attr("profile")
-        tracefile = c.get("tracefile")
-        injfile = c.get("injectionfile")
-        datafile = c.get("datafile")
-        configname = generate_config_name(profile, tracefile, injfile)
-        if not opt.onlymissing or not os.path.exists(datafile):
-            configfile = etcdir + "/%s.conf" % configname
-            templatedata.append((configname, configfile))
+        self.parse_options()
+        
+    def run(self):            
+        datafile=self.opt.datafiles
+        
+        stats = unpickle(datafile)
+        
+        # Barebones for now. Just prints out lease id, waiting time, and
+        # slowdown (only best-effort leases)
+        waitingtimes = stats.get_waiting_times()
+        slowdowns = stats.get_slowdowns()
+        print "lease_id waiting_time slowdown"
+        for lease_id in waitingtimes:
+            print lease_id, waitingtimes[lease_id].seconds, slowdowns[lease_id]
 
-    template = Template(filename=opt.template)
-    print template.render(configs=templatedata, etcdir=etcdir)
 
-
-def haizea_convert_data(argv):
-    p = OptionParser()
-    p.add_option(Option("-d", "--datafiles", action="store", type="string", dest="datafiles", required=True))
-    p.add_option(Option("-s", "--summary", action="store_true",  dest="summary"))
-    p.add_option(Option("-l", "--lease-stats", action="store", type="string", dest="lease"))
-    p.add_option(Option("-t", "--include-attributes", action="store_true", dest="attributes"))
-    p.add_option(Option("-f", "--format", action="store", type="string", dest="format"))
-
-    opt, args = p.parse_args(argv)
-    
-    datafile=opt.datafiles
-    
-    stats = unpickle(datafile)
-    
-    # Barebones for now. Just prints out lease id, waiting time, and
-    # slowdown (only best-effort leases)
-    waitingtimes = stats.get_waiting_times()
-    slowdowns = stats.get_slowdowns()
-    print "lease_id waiting_time slowdown"
-    for lease_id in waitingtimes:
-        print lease_id, waitingtimes[lease_id].seconds, slowdowns[lease_id]
-
-

Modified: trunk/src/haizea/cli/rpc_commands.py
===================================================================
--- trunk/src/haizea/cli/rpc_commands.py	2008-08-28 15:16:06 UTC (rev 472)
+++ trunk/src/haizea/cli/rpc_commands.py	2008-08-30 19:38:52 UTC (rev 473)
@@ -16,93 +16,155 @@
 # limitations under the License.                                             #
 # -------------------------------------------------------------------------- #
 from haizea.cli.optionparser import OptionParser, Option
+from haizea.common.constants import state_str
+from mx.DateTime import TimeDelta
+import haizea.common.defaults as defaults
+from haizea.cli import Command
 import xmlrpclib
 import sys
+from mx.DateTime import ISO
 
-def add_rpc_options(op):
-    op.add_option(Option("-s", "--server", action="store", type="string", dest="server", default="http://localhost:42493"))
+class RPCCommand(Command):
+    def __init__(self, argv):
+        Command.__init__(self, argv)
+        self.optparser.add_option(Option("-s", "--server", action="store", type="string", dest="server", default=defaults.RPC_URI))
 
-def create_rpc_proxy(server):
-    return xmlrpclib.ServerProxy(server, allow_none=True)
+    def create_rpc_proxy(self, server):
+        return xmlrpclib.ServerProxy(server, allow_none=True)
 
-def haizea_request_lease(argv):
-    p = OptionParser()
-    add_rpc_options(p)
-    
-    p.add_option(Option("-t", "--start", action="store", type="string", dest="start"))
-    p.add_option(Option("-d", "--duration", action="store", type="string", dest="duration"))
-    p.add_option(Option("-n", "--numnodes", action="store", type="int", dest="numnodes"))
-    p.add_option(Option("--preemptible", action="store_true", dest="preemptible"))
-    p.add_option(Option("--non-preemptible", action="store_false", dest="preemptible"))
-    p.add_option(Option("-c", "--cpu", action="store", type="float", dest="cpu"))
-    p.add_option(Option("-m", "--mem", action="store", type="int", dest="mem"))
-    p.add_option(Option("-i", "--vmimage", action="store", type="string", dest="vmimage"))
-    p.add_option(Option("-z", "--vmimagesize", action="store", type="int", dest="vmimagesize"))
+class haizea_request_lease(RPCCommand):
+    def __init__(self, argv):
+        RPCCommand.__init__(self, argv)
+        
+        self.optparser.add_option(Option("-t", "--start", action="store", type="string", dest="start"))
+        self.optparser.add_option(Option("-d", "--duration", action="store", type="string", dest="duration"))
+        self.optparser.add_option(Option("-n", "--numnodes", action="store", type="int", dest="numnodes"))
+        self.optparser.add_option(Option("--preemptible", action="store_true", dest="preemptible"))
+        self.optparser.add_option(Option("--non-preemptible", action="store_false", dest="preemptible"))
+        self.optparser.add_option(Option("-c", "--cpu", action="store", type="float", dest="cpu"))
+        self.optparser.add_option(Option("-m", "--mem", action="store", type="int", dest="mem"))
+        self.optparser.add_option(Option("-i", "--vmimage", action="store", type="string", dest="vmimage"))
+        self.optparser.add_option(Option("-z", "--vmimagesize", action="store", type="int", dest="vmimagesize"))
+        
+        self.parse_options()
+        
+    def run(self):
+        if self.opt.preemptible == None:
+            preemptible = False
+        else:
+            preemptible = self.opt.preemptible
+            
+        server = self.create_rpc_proxy(self.opt.server)
+        
+        try:
+            lease_id = server.create_lease(self.opt.start, self.opt.duration, preemptible, self.opt.numnodes, 
+                                self.opt.cpu, self.opt.mem, self.opt.vmimage, self.opt.vmimagesize)
+            print "Lease submitted correctly."
+            print "Lease ID: %i" % lease_id
+        except Exception, msg:
+            print >> sys.stderr, "Error: %s" % msg
+        
+class haizea_cancel_lease(RPCCommand):
+    def __init__(self, argv):
+        RPCCommand.__init__(self, argv)
+        
+        self.optparser.add_option(Option("-l", "--lease", action="store", type="int", dest="lease"))
+        
+        self.parse_options()
+        
+    def run(self):
+        server = self.create_rpc_proxy(self.opt.server)
+        
+        try:
+            code = server.cancel_lease(self.opt.lease)
+        except Exception, msg:
+            print >> sys.stderr, "Error: %s" % msg
+        
+class haizea_list_leases(RPCCommand):
+    def __init__(self, argv):
+        RPCCommand.__init__(self, argv)
+                
+        self.parse_options()
+        
+    def run(self):
+        server = self.create_rpc_proxy(self.opt.server)
+        
+        fields = [("id","ID", 3),
+                  ("type","Type", 4),
+                  ("state","State", 9),
+                  ("start_req", "Starting time", 22),
+                  ("duration_req", "Duration", 12),
+                  ("numnodes", "Nodes", 3)]
+        
+        try:
+            leases = server.get_leases()
+            console_table_printer(fields, leases)
+        except Exception, msg:
+            print >> sys.stderr, "Error: %s" % msg
 
-    opt, args = p.parse_args(argv)
-
-    if opt.preemptible == None:
-        preemptible = False
-    else:
-        preemptible = opt.preemptible
+class haizea_list_hosts(RPCCommand):
+    def __init__(self, argv):
+        RPCCommand.__init__(self, argv)
+                
+        self.parse_options()
         
-    server = create_rpc_proxy(opt.server)
-    
-    try:
-        lease_id = server.create_lease(opt.start, opt.duration, preemptible, opt.numnodes, 
-                            opt.cpu, opt.mem, opt.vmimage, opt.vmimagesize)
-        print "Lease submitted correctly."
-        print "Lease ID: %i" % lease_id
-    except Exception, msg:
-        print >> sys.stderr, "Error: %s" % msg
+    def run(self):
+        server = self.create_rpc_proxy(self.opt.server)
         
+        fields = [("id","ID", 3),
+                  ("hostname","Hostname", 10),
+                  ("cpu","CPUs", 6),
+                  ("mem", "Mem", 6)]
+        
+        try:
+            hosts = server.get_hosts()
+            console_table_printer(fields, hosts)
+        except Exception, msg:
+            print >> sys.stderr, "Error: %s" % msg
 
-def haizea_cancel_lease(argv):
-    p = OptionParser()
-    add_rpc_options(p)
-    
-    p.add_option(Option("-l", "--lease", action="store", type="int", dest="lease"))
-
-    opt, args = p.parse_args(argv)
-
-    server = create_rpc_proxy(opt.server)
-    
-    try:
-        code = server.cancel_lease(opt.lease)
-    except Exception, msg:
-        print >> sys.stderr, "Error: %s" % msg
+class haizea_show_queue(RPCCommand):
+    def __init__(self, argv):
+        RPCCommand.__init__(self, argv)
+                
+        self.parse_options()
         
+    def run(self):
+        server = self.create_rpc_proxy(self.opt.server)
+        
+        fields = [("id","ID", 3),
+                  ("type","Type", 4),
+                  ("state","State", 9),
+                  ("start_sched", "Sched. Start time", 22),
+                  ("duration_req", "Duration", 12),
+                  ("numnodes", "Nodes", 3)]
+        
+        try:
+            leases = server.get_queue()
+            console_table_printer(fields, leases)
+        except Exception, msg:
+            print >> sys.stderr, "Error: %s" % msg
 
-def haizea_list_leases(argv):
-    p = OptionParser()
-    add_rpc_options(p)
+def console_table_printer(fields, values):
+    print "\33[1m\33[4m",
+    for (name,pname,width) in fields:
+        width = max(len(pname),width) + 1
+        centered = pname.ljust(width)
+        print centered,
+    print "\33[0m"
+    for v in values:
+        for (name,pname,width) in fields:
+            value = pretty_print_rpcvalue(name, v[name])
+            width = max(len(name),width)
+            print " %s" % str(value).ljust(width),
+        print
     
-    opt, args = p.parse_args(argv)
+def pretty_print_rpcvalue(name, value):
+    if name == "state":
+        value = state_str(value)
+    elif name == "duration_req":
+        value = TimeDelta(seconds=value)
+    elif name == "start_req":
+        if value != None:
+            value = ISO.ParseDateTime(value.value)
 
-    server = create_rpc_proxy(opt.server)
-    
-    # TODO: Make this configurable. Ideally, use some sort of
-    # "console table printer"
-    fields = [("id",3),
-              ("type",4),
-              ("state",7),
-              ("start_req",19),
-              ("duration_req",9),
-              ("numnodes",3)]
-    
-    try:
-        leases = server.get_leases()
-        print "\33[1m\33[4m",
-        for (name,width) in fields:
-            width = max(len(name),width) + 1
-            centered = name.ljust(width)
-            print centered,
-        print "\33[0m"
-        for l in leases:
-            for (name,width) in fields:
-                value = l[name]
-                width = max(len(name),width)
-                print " %s" % str(value).ljust(width),
-            print
-    except Exception, msg:
-        print >> sys.stderr, "Error: %s" % msg
\ No newline at end of file
+    return value



More information about the Haizea-commit mailing list