ia64/xen-unstable

changeset 12615:bbcaa0cad3d2

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@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Tue Nov 28 13:34:15 2006 +0000 (2006-11-28)
parents 723dbe1fc751
children b08e7ed94991
files tools/python/xen/xend/XendDomain.py tools/python/xen/xend/server/XMLRPCServer.py tools/python/xen/xm/main.py
line diff
     1.1 --- a/tools/python/xen/xend/XendDomain.py	Tue Nov 28 13:32:07 2006 +0000
     1.2 +++ b/tools/python/xen/xend/XendDomain.py	Tue Nov 28 13:34:15 2006 +0000
     1.3 @@ -35,8 +35,11 @@ from xen.xend.PrettyPrint import prettyp
     1.4  from xen.xend.XendConfig import XendConfig
     1.5  from xen.xend.XendError import XendError, XendInvalidDomain, VmError
     1.6  from xen.xend.XendLogging import log
     1.7 +from xen.xend.XendAPIConstants import XEN_API_VM_POWER_STATE
     1.8  from xen.xend.XendConstants import XS_VMROOT
     1.9 -from xen.xend.XendConstants import DOM_STATE_HALTED, DOM_STATE_RUNNING
    1.10 +from xen.xend.XendConstants import DOM_STATE_HALTED, DOM_STATE_PAUSED
    1.11 +from xen.xend.XendConstants import DOM_STATE_RUNNING, DOM_STATE_SUSPENDED
    1.12 +from xen.xend.XendConstants import DOM_STATE_SHUTDOWN, DOM_STATE_UNKNOWN
    1.13  from xen.xend.XendDevices import XendDevices
    1.14  
    1.15  from xen.xend.xenstore.xstransact import xstransact
    1.16 @@ -55,6 +58,16 @@ DOM0_UUID = "00000000-0000-0000-0000-000
    1.17  DOM0_NAME = "Domain-0"
    1.18  DOM0_ID   = 0
    1.19  
    1.20 +POWER_STATE_NAMES = dict([(x, XEN_API_VM_POWER_STATE[x])
    1.21 +                          for x in [DOM_STATE_HALTED,
    1.22 +                                    DOM_STATE_PAUSED,
    1.23 +                                    DOM_STATE_RUNNING,
    1.24 +                                    DOM_STATE_SUSPENDED,
    1.25 +                                    DOM_STATE_SHUTDOWN,
    1.26 +                                    DOM_STATE_UNKNOWN]])
    1.27 +POWER_STATE_ALL = 'all'
    1.28 +
    1.29 +
    1.30  class XendDomain:
    1.31      """Index of all domains. Singleton.
    1.32  
    1.33 @@ -687,12 +700,18 @@ class XendDomain:
    1.34      # ------------------------------------------------------------
    1.35      # Xen Legacy API     
    1.36  
    1.37 -    def list(self):
    1.38 +    def list(self, state = DOM_STATE_RUNNING):
    1.39          """Get list of domain objects.
    1.40  
    1.41 +        @param: the state in which the VMs should be -- one of the
    1.42 +        DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
    1.43          @return: domains
    1.44          @rtype: list of XendDomainInfo
    1.45          """
    1.46 +        if type(state) == int:
    1.47 +            state = POWER_STATE_NAMES[state]
    1.48 +        state = state.lower()
    1.49 +        
    1.50          self.domains_lock.acquire()
    1.51          try:
    1.52              self._refresh()
    1.53 @@ -707,28 +726,37 @@ class XendDomain:
    1.54                  if dom_uuid not in active_uuids:
    1.55                      inactive_domains.append(dom)
    1.56  
    1.57 -            return active_domains + inactive_domains
    1.58 +            if state == POWER_STATE_ALL:
    1.59 +                return active_domains + inactive_domains
    1.60 +            else:
    1.61 +                return filter(lambda x:
    1.62 +                                  POWER_STATE_NAMES[x.state].lower() == state,
    1.63 +                              active_domains + inactive_domains)
    1.64          finally:
    1.65              self.domains_lock.release()
    1.66  
    1.67  
    1.68 -    def list_sorted(self):
    1.69 +    def list_sorted(self, state = DOM_STATE_RUNNING):
    1.70          """Get list of domain objects, sorted by name.
    1.71  
    1.72 +        @param: the state in which the VMs should be -- one of the
    1.73 +        DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
    1.74          @return: domain objects
    1.75          @rtype: list of XendDomainInfo
    1.76          """
    1.77 -        doms = self.list()
    1.78 +        doms = self.list(state)
    1.79          doms.sort(lambda x, y: cmp(x.getName(), y.getName()))
    1.80          return doms
    1.81  
    1.82 -    def list_names(self):
    1.83 +    def list_names(self, state = DOM_STATE_RUNNING):
    1.84          """Get list of domain names.
    1.85  
    1.86 +        @param: the state in which the VMs should be -- one of the
    1.87 +        DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
    1.88          @return: domain names
    1.89          @rtype: list of strings.
    1.90          """
    1.91 -        return [d.getName() for d in self.list_sorted()]
    1.92 +        return [d.getName() for d in self.list_sorted(state)]
    1.93  
    1.94      def domain_suspend(self, domname):
    1.95          """Suspends a domain that is persistently managed by Xend
     2.1 --- a/tools/python/xen/xend/server/XMLRPCServer.py	Tue Nov 28 13:32:07 2006 +0000
     2.2 +++ b/tools/python/xen/xend/server/XMLRPCServer.py	Tue Nov 28 13:34:15 2006 +0000
     2.3 @@ -23,6 +23,7 @@ from xen.util.xmlrpclib2 import UnixXMLR
     2.4  from xen.xend import XendAPI, XendDomain, XendDomainInfo, XendNode
     2.5  from xen.xend import XendLogging, XendDmesg
     2.6  from xen.xend.XendClient import XML_RPC_SOCKET
     2.7 +from xen.xend.XendConstants import DOM_STATE_RUNNING
     2.8  from xen.xend.XendLogging import log
     2.9  from xen.xend.XendError import XendInvalidDomain
    2.10  
    2.11 @@ -52,12 +53,15 @@ def domain(domid, full = 0):
    2.12      info = lookup(domid)
    2.13      return fixup_sxpr(info.sxpr(not full))
    2.14  
    2.15 -def domains(detail=1, full = 0):
    2.16 -    if detail < 1:
    2.17 -        return XendDomain.instance().list_names()
    2.18 +def domains(detail = True, full = False):
    2.19 +    return domains_with_state(detail, DOM_STATE_RUNNING, full)
    2.20 +
    2.21 +def domains_with_state(detail, state, full):
    2.22 +    if detail:
    2.23 +        domains = XendDomain.instance().list_sorted(state)
    2.24 +        return map(lambda dom: fixup_sxpr(dom.sxpr(not full)), domains)
    2.25      else:
    2.26 -        domains = XendDomain.instance().list_sorted()
    2.27 -        return map(lambda dom: fixup_sxpr(dom.sxpr(not full)), domains)
    2.28 +        return XendDomain.instance().list_names(state)
    2.29  
    2.30  def domain_create(config):
    2.31      info = XendDomain.instance().domain_create(config)
    2.32 @@ -153,6 +157,8 @@ class XMLRPCServer:
    2.33          # A few special cases
    2.34          self.server.register_function(domain, 'xend.domain')
    2.35          self.server.register_function(domains, 'xend.domains')
    2.36 +        self.server.register_function(domains_with_state,
    2.37 +                                      'xend.domains_with_state')
    2.38          self.server.register_function(get_log, 'xend.node.log')
    2.39          self.server.register_function(domain_create, 'xend.domain.create')
    2.40          self.server.register_function(domain_restore, 'xend.domain.restore')
     3.1 --- a/tools/python/xen/xm/main.py	Tue Nov 28 13:32:07 2006 +0000
     3.2 +++ b/tools/python/xen/xm/main.py	Tue Nov 28 13:34:15 2006 +0000
     3.3 @@ -187,8 +187,9 @@ SUBCOMMAND_OPTIONS = {
     3.4         ('-c CAP',    '--cap=CAP',       'Cap (int)'),
     3.5      ),
     3.6      'list': (
     3.7 -       ('-l', '--long', 'Output all VM details in SXP'),
     3.8 -       ('', '--label',  'Include security labels'),
     3.9 +       ('-l', '--long',         'Output all VM details in SXP'),
    3.10 +       ('', '--label',          'Include security labels'),
    3.11 +       ('', '--state=<state>',  'Select only VMs with the specified state'),
    3.12      ),
    3.13      'console': (
    3.14         ('-q', '--quiet', 'Do not print an error message if the domain does not exist'),
    3.15 @@ -513,20 +514,22 @@ def xm_restore(args):
    3.16      server.xend.domain.restore(savefile, paused)
    3.17  
    3.18  
    3.19 -def getDomains(domain_names, full = 0):
    3.20 +def getDomains(domain_names, state, full = 0):
    3.21      if domain_names:
    3.22          return [server.xend.domain(dom, full) for dom in domain_names]
    3.23      else:
    3.24 -        return server.xend.domains(1, full)
    3.25 +        return server.xend.domains_with_state(True, state, full)
    3.26  
    3.27  
    3.28  def xm_list(args):
    3.29      use_long = 0
    3.30      show_vcpus = 0
    3.31      show_labels = 0
    3.32 +    state = 'all'
    3.33      try:
    3.34          (options, params) = getopt.gnu_getopt(args, 'lv',
    3.35 -                                              ['long','vcpus','label'])
    3.36 +                                              ['long','vcpus','label',
    3.37 +                                               'state='])
    3.38      except getopt.GetoptError, opterr:
    3.39          err(opterr)
    3.40          usage('list')
    3.41 @@ -539,6 +542,12 @@ def xm_list(args):
    3.42              show_vcpus = 1
    3.43          if k in ['--label']:
    3.44              show_labels = 1
    3.45 +        if k in ['--state']:
    3.46 +            state = v
    3.47 +
    3.48 +    if state != 'all' and len(params) > 0:
    3.49 +        raise OptionError(
    3.50 +            "You may specify either a state or a particular VM, but not both")
    3.51  
    3.52      if show_vcpus:
    3.53          print >>sys.stderr, (
    3.54 @@ -546,7 +555,7 @@ def xm_list(args):
    3.55          xm_vcpu_list(params)
    3.56          return
    3.57  
    3.58 -    doms = getDomains(params, use_long)
    3.59 +    doms = getDomains(params, state, use_long)
    3.60  
    3.61      if use_long:
    3.62          map(PrettyPrint.prettyprint, doms)
    3.63 @@ -941,7 +950,8 @@ def xm_sched_sedf(args):
    3.64              opts['weight'] = v
    3.65  
    3.66      doms = filter(lambda x : domid_match(domid, x),
    3.67 -                        [parse_doms_info(dom) for dom in getDomains("")])
    3.68 +                        [parse_doms_info(dom)
    3.69 +                         for dom in getDomains(None, 'running')])
    3.70  
    3.71      # print header if we aren't setting any parameters
    3.72      if len(opts.keys()) == 0:
    3.73 @@ -1077,7 +1087,7 @@ def xm_uptime(args):
    3.74          if k in ['-s', '--short']:
    3.75              short_mode = 1
    3.76  
    3.77 -    doms = getDomains(params)
    3.78 +    doms = getDomains(params, 'running')
    3.79  
    3.80      if short_mode == 0:
    3.81          print 'Name                              ID Uptime'