ia64/xen-unstable

changeset 6790:c5045197dcb7

Removed {add,get}_{config,device}_handler in favour of using a simple dictionary directly. Fix a misnamed variable blconfig that was supposed to refer to blcfg. This showed up under pylint as a undefined variable, but would have manifested itself in the bootloader configuration being ignored.
author ewan@linford.intra
date Tue Sep 13 14:48:22 2005 +0100 (2005-09-13)
parents 723f81936cf7
children 5aa6a2eff69f
files tools/python/xen/xend/XendDomainInfo.py
line diff
     1.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Tue Sep 13 14:43:18 2005 +0100
     1.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Tue Sep 13 14:48:22 2005 +0100
     1.3 @@ -102,34 +102,6 @@ def shutdown_reason(code):
     1.4      """
     1.5      return shutdown_reasons.get(code, "?")
     1.6  
     1.7 -config_handlers = {}
     1.8 -
     1.9 -def add_config_handler(name, h):
    1.10 -    """Add a handler for a config field.
    1.11 -
    1.12 -    @param name:     field name
    1.13 -    @param h:        handler: fn(vm, config, field, index)
    1.14 -    """
    1.15 -    config_handlers[name] = h
    1.16 -
    1.17 -def get_config_handler(name):
    1.18 -    """Get a handler for a config field.
    1.19 -
    1.20 -    returns handler or None
    1.21 -    """
    1.22 -    return config_handlers.get(name)
    1.23 -
    1.24 -"""Table of handlers for devices.
    1.25 -Indexed by device type.
    1.26 -"""
    1.27 -device_handlers = {}
    1.28 -
    1.29 -def add_device_handler(name, type):
    1.30 -    device_handlers[name] = type
    1.31 -
    1.32 -def get_device_handler(name):
    1.33 -    return device_handlers[name]
    1.34 -
    1.35  def dom_get(dom):
    1.36      """Get info from xen for an existing domain.
    1.37  
    1.38 @@ -369,12 +341,6 @@ class XendDomainInfo:
    1.39  
    1.40      __repr__ = __str__
    1.41  
    1.42 -    def getDeviceTypes(self):
    1.43 -        return self.controllers.keys()
    1.44 -
    1.45 -    def getDeviceControllers(self):
    1.46 -        return self.controllers.values()
    1.47 -
    1.48      def getDeviceController(self, type, error=True):
    1.49          ctrl = self.controllers.get(type)
    1.50          if not ctrl and error:
    1.51 @@ -595,7 +561,7 @@ class XendDomainInfo:
    1.52  
    1.53      def sxpr_devices(self):
    1.54          sxpr = []
    1.55 -        for ty in self.getDeviceTypes():
    1.56 +        for ty in self.controllers.keys():
    1.57              devs = self.getDeviceSxprs(ty)
    1.58              sxpr += devs
    1.59          if sxpr:
    1.60 @@ -796,7 +762,7 @@ class XendDomainInfo:
    1.61          """Release all vm devices.
    1.62          """
    1.63          reboot = self.restart_pending()
    1.64 -        for ctrl in self.getDeviceControllers():
    1.65 +        for ctrl in self.controllers.values():
    1.66              if ctrl.isDestroyed(): continue
    1.67              ctrl.destroyController(reboot=reboot)
    1.68          t = xstransact("%s/device" % self.path)
    1.69 @@ -874,6 +840,7 @@ class XendDomainInfo:
    1.70          self.store_channel = self.eventChannelOld("store_channel")
    1.71          self.console_channel = self.eventChannel("console", "port")
    1.72  
    1.73 +
    1.74      def create_configured_devices(self):
    1.75          devices = sxp.children(self.config, 'device')
    1.76          for d in devices:
    1.77 @@ -881,18 +848,20 @@ class XendDomainInfo:
    1.78              if dev_config is None:
    1.79                  raise VmError('invalid device')
    1.80              dev_type = sxp.name(dev_config)
    1.81 -            ctrl_type = get_device_handler(dev_type)
    1.82 -            if ctrl_type is None:
    1.83 +
    1.84 +            if not controller.isDevControllerClass(dev_type):
    1.85                  raise VmError('unknown device type: ' + dev_type)
    1.86 -            self.createDevice(ctrl_type, dev_config)
    1.87 -        
    1.88 +            
    1.89 +            self.createDevice(dev_type, dev_config)
    1.90 +
    1.91 +
    1.92      def create_devices(self):
    1.93          """Create the devices for a vm.
    1.94  
    1.95          @raise: VmError for invalid devices
    1.96          """
    1.97          if self.rebooting():
    1.98 -            for ctrl in self.getDeviceControllers():
    1.99 +            for ctrl in self.controllers.values():
   1.100                  ctrl.initController(reboot=True)
   1.101          else:
   1.102              self.create_configured_devices()
   1.103 @@ -1043,7 +1012,7 @@ class XendDomainInfo:
   1.104              msg = "Had a bootloader specified, but can't find disk"
   1.105              log.error(msg)
   1.106              raise VmError(msg)
   1.107 -        config = sxp.merge(['vm', blconfig ], self.config)
   1.108 +        config = sxp.merge(['vm', blcfg ], self.config)
   1.109          return config
   1.110  
   1.111      def configure_backends(self):
   1.112 @@ -1092,7 +1061,7 @@ class XendDomainInfo:
   1.113          for field in sxp.children(self.config):
   1.114              field_name = sxp.name(field)
   1.115              field_index = index.get(field_name, 0)
   1.116 -            field_handler = get_config_handler(field_name)
   1.117 +            field_handler = config_handlers.get(field_name)
   1.118              # Ignore unknown fields. Warn?
   1.119              if field_handler:
   1.120                  v = field_handler(self, self.config, field, field_index)
   1.121 @@ -1161,23 +1130,17 @@ class XendDomainInfo:
   1.122          # get run-time value of vcpus and update store
   1.123          self.exportVCPUSToDB(dom_get(self.id)['vcpus'])
   1.124  
   1.125 -def vm_field_ignore(vm, config, val, index):
   1.126 +
   1.127 +def vm_field_ignore(_, _1, _2, _3):
   1.128      """Dummy config field handler used for fields with built-in handling.
   1.129 -
   1.130 -    @param vm:        virtual machine
   1.131 -    @param config:    vm config
   1.132 -    @param val:       config field
   1.133 -    @param index:     field index
   1.134 +    Matches the signature required by config_handlers.
   1.135      """
   1.136      pass
   1.137  
   1.138 -def vm_field_maxmem(vm, config, val, index):
   1.139 -    """Configure vm memory limit.
   1.140  
   1.141 -    @param vm:        virtual machine
   1.142 -    @param config:    vm config
   1.143 -    @param val:       config field
   1.144 -    @param index:     field index
   1.145 +def vm_field_maxmem(vm, _1, val, _2):
   1.146 +    """Config field handler to configure vm memory limit.  Matches the
   1.147 +    signature required by config_handlers.
   1.148      """
   1.149      maxmem = sxp.child0(val)
   1.150      if maxmem is None:
   1.151 @@ -1188,8 +1151,10 @@ def vm_field_maxmem(vm, config, val, ind
   1.152          raise VmError("invalid maxmem: " + str(maxmem))
   1.153      xc.domain_setmaxmem(vm.id, maxmem_kb = maxmem * 1024)
   1.154  
   1.155 +
   1.156  #============================================================================
   1.157  # Register image handlers.
   1.158 +
   1.159  from image import          \
   1.160       addImageHandlerClass, \
   1.161       ImageHandler,         \
   1.162 @@ -1199,43 +1164,37 @@ from image import          \
   1.163  addImageHandlerClass(LinuxImageHandler)
   1.164  addImageHandlerClass(VmxImageHandler)
   1.165  
   1.166 -# Ignore the fields we already handle.
   1.167 -add_config_handler('name',       vm_field_ignore)
   1.168 -add_config_handler('memory',     vm_field_ignore)
   1.169 -add_config_handler('ssidref',    vm_field_ignore)
   1.170 -add_config_handler('cpu',        vm_field_ignore)
   1.171 -add_config_handler('cpu_weight', vm_field_ignore)
   1.172 -add_config_handler('restart',    vm_field_ignore)
   1.173 -add_config_handler('image',      vm_field_ignore)
   1.174 -add_config_handler('device',     vm_field_ignore)
   1.175 -add_config_handler('backend',    vm_field_ignore)
   1.176 -add_config_handler('vcpus',      vm_field_ignore)
   1.177 -add_config_handler('bootloader', vm_field_ignore)
   1.178 +
   1.179 +"""Table of handlers for field configuration.
   1.180  
   1.181 -# Register other config handlers.
   1.182 -add_config_handler('maxmem',     vm_field_maxmem)
   1.183 +field_name[String]: fn(vm, config, field, index) -> value(ignored)
   1.184 +"""
   1.185 +config_handlers = {
   1.186 +    
   1.187 +    # Ignore the fields we already handle.
   1.188 +    
   1.189 +    'name':       vm_field_ignore,
   1.190 +    'memory':     vm_field_ignore,
   1.191 +    'ssidref':    vm_field_ignore,
   1.192 +    'cpu':        vm_field_ignore,
   1.193 +    'cpu_weight': vm_field_ignore,
   1.194 +    'restart':    vm_field_ignore,
   1.195 +    'image':      vm_field_ignore,
   1.196 +    'device':     vm_field_ignore,
   1.197 +    'backend':    vm_field_ignore,
   1.198 +    'vcpus':      vm_field_ignore,
   1.199 +    'bootloader': vm_field_ignore,
   1.200 +    
   1.201 +    # Register other config handlers.
   1.202 +    'maxmem':     vm_field_maxmem
   1.203 +    }
   1.204 +
   1.205  
   1.206  #============================================================================
   1.207  # Register device controllers and their device config types.
   1.208  
   1.209 -from server import blkif
   1.210 -controller.addDevControllerClass("vbd", blkif.BlkifController)
   1.211 -add_device_handler("vbd", "vbd")
   1.212 -
   1.213 -from server import netif
   1.214 -controller.addDevControllerClass("vif", netif.NetifController)
   1.215 -add_device_handler("vif", "vif")
   1.216 -
   1.217 -from server import tpmif
   1.218 -controller.addDevControllerClass("vtpm", tpmif.TPMifController)
   1.219 -add_device_handler("vtpm", "vtpm")
   1.220 -
   1.221 -from server import pciif
   1.222 -controller.addDevControllerClass("pci", pciif.PciController)
   1.223 -add_device_handler("pci", "pci")
   1.224 -
   1.225 -from xen.xend.server import usbif
   1.226 -controller.addDevControllerClass("usb", usbif.UsbifController)
   1.227 -add_device_handler("usb", "usb")
   1.228 -
   1.229 -#============================================================================
   1.230 +controller.addDevControllerClass("vbd",  server.blkif.BlkifController)
   1.231 +controller.addDevControllerClass("vif",  server.netif.NetifController)
   1.232 +controller.addDevControllerClass("vtpm", server.tpmif.TPMifController)
   1.233 +controller.addDevControllerClass("pci",  server.pciif.PciController)
   1.234 +controller.addDevControllerClass("usb",  server.usbif.UsbifController)