[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] Added xm list --state option, to select VMs with a particular state. Make



# HG changeset patch
# User Ewan Mellor <ewan@xxxxxxxxxxxxx>
# Node ID bbcaa0cad3d2b7cf90e935bf1dd61aec129b3252
# Parent  723dbe1fc75199e9b6dfad64c7a8790b1d75cfd1
Added xm list --state option, to select VMs with a particular state.  Make
this handled by a new message (xend.domains_with_state) and make xend.domains
equivalent to xend.domains_with_state(running).  This restores the Xen 3.0.3
semantics of xend.domains.

Signed-off-by: Ewan Mellor <ewan@xxxxxxxxxxxxx>
---
 tools/python/xen/xend/XendDomain.py          |   50 +++++++++++++++++++++------
 tools/python/xen/xend/server/XMLRPCServer.py |   16 +++++---
 tools/python/xen/xm/main.py                  |   26 +++++++++-----
 3 files changed, 68 insertions(+), 24 deletions(-)

diff -r 723dbe1fc751 -r bbcaa0cad3d2 tools/python/xen/xend/XendDomain.py
--- a/tools/python/xen/xend/XendDomain.py       Tue Nov 28 13:32:07 2006 +0000
+++ b/tools/python/xen/xend/XendDomain.py       Tue Nov 28 13:34:15 2006 +0000
@@ -35,8 +35,11 @@ from xen.xend.XendConfig import XendConf
 from xen.xend.XendConfig import XendConfig
 from xen.xend.XendError import XendError, XendInvalidDomain, VmError
 from xen.xend.XendLogging import log
+from xen.xend.XendAPIConstants import XEN_API_VM_POWER_STATE
 from xen.xend.XendConstants import XS_VMROOT
-from xen.xend.XendConstants import DOM_STATE_HALTED, DOM_STATE_RUNNING
+from xen.xend.XendConstants import DOM_STATE_HALTED, DOM_STATE_PAUSED
+from xen.xend.XendConstants import DOM_STATE_RUNNING, DOM_STATE_SUSPENDED
+from xen.xend.XendConstants import DOM_STATE_SHUTDOWN, DOM_STATE_UNKNOWN
 from xen.xend.XendDevices import XendDevices
 
 from xen.xend.xenstore.xstransact import xstransact
@@ -54,6 +57,16 @@ DOM0_UUID = "00000000-0000-0000-0000-000
 DOM0_UUID = "00000000-0000-0000-0000-000000000000"
 DOM0_NAME = "Domain-0"
 DOM0_ID   = 0
+
+POWER_STATE_NAMES = dict([(x, XEN_API_VM_POWER_STATE[x])
+                          for x in [DOM_STATE_HALTED,
+                                    DOM_STATE_PAUSED,
+                                    DOM_STATE_RUNNING,
+                                    DOM_STATE_SUSPENDED,
+                                    DOM_STATE_SHUTDOWN,
+                                    DOM_STATE_UNKNOWN]])
+POWER_STATE_ALL = 'all'
+
 
 class XendDomain:
     """Index of all domains. Singleton.
@@ -687,12 +700,18 @@ class XendDomain:
     # ------------------------------------------------------------
     # Xen Legacy API     
 
-    def list(self):
+    def list(self, state = DOM_STATE_RUNNING):
         """Get list of domain objects.
 
+        @param: the state in which the VMs should be -- one of the
+        DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
         @return: domains
         @rtype: list of XendDomainInfo
         """
+        if type(state) == int:
+            state = POWER_STATE_NAMES[state]
+        state = state.lower()
+        
         self.domains_lock.acquire()
         try:
             self._refresh()
@@ -707,28 +726,37 @@ class XendDomain:
                 if dom_uuid not in active_uuids:
                     inactive_domains.append(dom)
 
-            return active_domains + inactive_domains
-        finally:
-            self.domains_lock.release()
-
-
-    def list_sorted(self):
+            if state == POWER_STATE_ALL:
+                return active_domains + inactive_domains
+            else:
+                return filter(lambda x:
+                                  POWER_STATE_NAMES[x.state].lower() == state,
+                              active_domains + inactive_domains)
+        finally:
+            self.domains_lock.release()
+
+
+    def list_sorted(self, state = DOM_STATE_RUNNING):
         """Get list of domain objects, sorted by name.
 
+        @param: the state in which the VMs should be -- one of the
+        DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
         @return: domain objects
         @rtype: list of XendDomainInfo
         """
-        doms = self.list()
+        doms = self.list(state)
         doms.sort(lambda x, y: cmp(x.getName(), y.getName()))
         return doms
 
-    def list_names(self):
+    def list_names(self, state = DOM_STATE_RUNNING):
         """Get list of domain names.
 
+        @param: the state in which the VMs should be -- one of the
+        DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
         @return: domain names
         @rtype: list of strings.
         """
-        return [d.getName() for d in self.list_sorted()]
+        return [d.getName() for d in self.list_sorted(state)]
 
     def domain_suspend(self, domname):
         """Suspends a domain that is persistently managed by Xend
diff -r 723dbe1fc751 -r bbcaa0cad3d2 
tools/python/xen/xend/server/XMLRPCServer.py
--- a/tools/python/xen/xend/server/XMLRPCServer.py      Tue Nov 28 13:32:07 
2006 +0000
+++ b/tools/python/xen/xend/server/XMLRPCServer.py      Tue Nov 28 13:34:15 
2006 +0000
@@ -23,6 +23,7 @@ from xen.xend import XendAPI, XendDomain
 from xen.xend import XendAPI, XendDomain, XendDomainInfo, XendNode
 from xen.xend import XendLogging, XendDmesg
 from xen.xend.XendClient import XML_RPC_SOCKET
+from xen.xend.XendConstants import DOM_STATE_RUNNING
 from xen.xend.XendLogging import log
 from xen.xend.XendError import XendInvalidDomain
 
@@ -52,12 +53,15 @@ def domain(domid, full = 0):
     info = lookup(domid)
     return fixup_sxpr(info.sxpr(not full))
 
-def domains(detail=1, full = 0):
-    if detail < 1:
-        return XendDomain.instance().list_names()
+def domains(detail = True, full = False):
+    return domains_with_state(detail, DOM_STATE_RUNNING, full)
+
+def domains_with_state(detail, state, full):
+    if detail:
+        domains = XendDomain.instance().list_sorted(state)
+        return map(lambda dom: fixup_sxpr(dom.sxpr(not full)), domains)
     else:
-        domains = XendDomain.instance().list_sorted()
-        return map(lambda dom: fixup_sxpr(dom.sxpr(not full)), domains)
+        return XendDomain.instance().list_names(state)
 
 def domain_create(config):
     info = XendDomain.instance().domain_create(config)
@@ -153,6 +157,8 @@ class XMLRPCServer:
         # A few special cases
         self.server.register_function(domain, 'xend.domain')
         self.server.register_function(domains, 'xend.domains')
+        self.server.register_function(domains_with_state,
+                                      'xend.domains_with_state')
         self.server.register_function(get_log, 'xend.node.log')
         self.server.register_function(domain_create, 'xend.domain.create')
         self.server.register_function(domain_restore, 'xend.domain.restore')
diff -r 723dbe1fc751 -r bbcaa0cad3d2 tools/python/xen/xm/main.py
--- a/tools/python/xen/xm/main.py       Tue Nov 28 13:32:07 2006 +0000
+++ b/tools/python/xen/xm/main.py       Tue Nov 28 13:34:15 2006 +0000
@@ -187,8 +187,9 @@ SUBCOMMAND_OPTIONS = {
        ('-c CAP',    '--cap=CAP',       'Cap (int)'),
     ),
     'list': (
-       ('-l', '--long', 'Output all VM details in SXP'),
-       ('', '--label',  'Include security labels'),
+       ('-l', '--long',         'Output all VM details in SXP'),
+       ('', '--label',          'Include security labels'),
+       ('', '--state=<state>',  'Select only VMs with the specified state'),
     ),
     'console': (
        ('-q', '--quiet', 'Do not print an error message if the domain does not 
exist'),
@@ -513,20 +514,22 @@ def xm_restore(args):
     server.xend.domain.restore(savefile, paused)
 
 
-def getDomains(domain_names, full = 0):
+def getDomains(domain_names, state, full = 0):
     if domain_names:
         return [server.xend.domain(dom, full) for dom in domain_names]
     else:
-        return server.xend.domains(1, full)
+        return server.xend.domains_with_state(True, state, full)
 
 
 def xm_list(args):
     use_long = 0
     show_vcpus = 0
     show_labels = 0
+    state = 'all'
     try:
         (options, params) = getopt.gnu_getopt(args, 'lv',
-                                              ['long','vcpus','label'])
+                                              ['long','vcpus','label',
+                                               'state='])
     except getopt.GetoptError, opterr:
         err(opterr)
         usage('list')
@@ -539,6 +542,12 @@ def xm_list(args):
             show_vcpus = 1
         if k in ['--label']:
             show_labels = 1
+        if k in ['--state']:
+            state = v
+
+    if state != 'all' and len(params) > 0:
+        raise OptionError(
+            "You may specify either a state or a particular VM, but not both")
 
     if show_vcpus:
         print >>sys.stderr, (
@@ -546,7 +555,7 @@ def xm_list(args):
         xm_vcpu_list(params)
         return
 
-    doms = getDomains(params, use_long)
+    doms = getDomains(params, state, use_long)
 
     if use_long:
         map(PrettyPrint.prettyprint, doms)
@@ -941,7 +950,8 @@ def xm_sched_sedf(args):
             opts['weight'] = v
 
     doms = filter(lambda x : domid_match(domid, x),
-                        [parse_doms_info(dom) for dom in getDomains("")])
+                        [parse_doms_info(dom)
+                         for dom in getDomains(None, 'running')])
 
     # print header if we aren't setting any parameters
     if len(opts.keys()) == 0:
@@ -1077,7 +1087,7 @@ def xm_uptime(args):
         if k in ['-s', '--short']:
             short_mode = 1
 
-    doms = getDomains(params)
+    doms = getDomains(params, 'running')
 
     if short_mode == 0:
         print 'Name                              ID Uptime'

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.