ia64/xen-unstable

changeset 1870:f8df05dd9dbf

bitkeeper revision 1.1108.1.12 (40ffee27o1VPDwZVRgzbC8H1a0rrLQ)

Small fix in error handling.
author mjw@wray-m-3.hpl.hp.com
date Thu Jul 22 16:41:11 2004 +0000 (2004-07-22)
parents a0d49fb31f04
children 495ae087d703 a2d2b4ac2439
files tools/python/xen/xend/XendVnet.py tools/python/xen/xend/server/SrvBase.py
line diff
     1.1 --- a/tools/python/xen/xend/XendVnet.py	Thu Jul 22 16:39:49 2004 +0000
     1.2 +++ b/tools/python/xen/xend/XendVnet.py	Thu Jul 22 16:41:11 2004 +0000
     1.3 @@ -3,8 +3,58 @@
     1.4  """Handler for vnet operations.
     1.5  """
     1.6  
     1.7 +from xen.util import Brctl
     1.8 +
     1.9  import sxp
    1.10  import XendDB
    1.11 +from XendError import XendError
    1.12 +from XendLogging import log
    1.13 +
    1.14 +def vnet_cmd(cmd):
    1.15 +    out = None
    1.16 +    try:
    1.17 +        try:
    1.18 +            out = file("/proc/vnet/policy", "wb")
    1.19 +            sxp.show(cmd, out)
    1.20 +        except IOError, ex:
    1.21 +            raise XendError(str(ex))
    1.22 +    finally:
    1.23 +        if out: out.close()
    1.24 +
    1.25 +class XendVnetInfo:
    1.26 +    
    1.27 +    vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
    1.28 +    
    1.29 +    def __init__(self, config):
    1.30 +        self.config = config
    1.31 +        self.id = sxp.child_value(config, 'id')
    1.32 +        self.id = str(self.id)
    1.33 +        self.bridge = sxp.child_value(config, 'bridge')
    1.34 +        if not self.bridge:
    1.35 +            self.bridge = "vnet%s" % self.id
    1.36 +        self.vnetif = sxp.child_value(config, 'vnetif')
    1.37 +        if not self.vnetif:
    1.38 +            self.vnetif = "vnetif%s" % self.id
    1.39 +
    1.40 +    def sxpr(self):
    1.41 +        return self.config
    1.42 +
    1.43 +    def configure(self):
    1.44 +        log.info("Configuring vnet %s", self.id)
    1.45 +        val = vnet_cmd(['vnet.add'] + sxp.children(self.config))
    1.46 +        Brctl.bridge_create(self.bridge)
    1.47 +        Brctl.vif_bridge_add({'bridge': self.bridge, 'vif': self.vnetif})
    1.48 +        return val
    1.49 +        
    1.50 +    def delete(self):
    1.51 +        log.info("Deleting vnet %s", self.id)
    1.52 +        Brctl.vif_bridge_rem({'bridge': self.bridge, 'vif': self.vnetif})
    1.53 +        Brctl.bridge_del(self.bridge)
    1.54 +        return vnet_cmd(['vnet.del', self.id])
    1.55 +
    1.56 +    def vifctl(self, op, vif, vmac):
    1.57 +        fn = self.vifctl_ops[op]
    1.58 +        return vnet_cmd([fn, ['vif', vif], ['vmac', vmac]])
    1.59  
    1.60  class XendVnet:
    1.61      """Index of all vnets. Singleton.
    1.62 @@ -16,49 +66,65 @@ class XendVnet:
    1.63          # Table of vnet info indexed by vnet id.
    1.64          self.vnet = {}
    1.65          self.db = XendDB.XendDB(self.dbpath)
    1.66 -        self.vnet = self.db.fetchall("")
    1.67 +        vnets = self.db.fetchall("")
    1.68 +        for config in vnets.values():
    1.69 +            info = XendVnetInfo(config)
    1.70 +            self.vnet[info.id] = info
    1.71 +            try:
    1.72 +                info.configure()
    1.73 +            except:
    1.74 +                log.exception("Error configuring vnet")
    1.75 +
    1.76 +    def vnet_of_bridge(self, bridge):
    1.77 +        """Get the vnet for a bridge (if any).
    1.78 +
    1.79 +        @param bridge: bridge name
    1.80 +        @return vnet or None
    1.81 +        """
    1.82 +        for v in self.vnet.values():
    1.83 +            if v.bridge == bridge:
    1.84 +                return v
    1.85 +        else:
    1.86 +            return None
    1.87  
    1.88      def vnet_ls(self):
    1.89 -        """List all vnets.
    1.90 +        """List all vnet ids.
    1.91          """
    1.92          return self.vnet.keys()
    1.93  
    1.94      def vnets(self):
    1.95 +        """List all vnets.
    1.96 +        """
    1.97          return self.vnet.values()
    1.98  
    1.99      def vnet_get(self, id):
   1.100          """Get a vnet.
   1.101  
   1.102 -        id	vnet id
   1.103 +        @param id: vnet id
   1.104          """
   1.105 +        id = str(id)
   1.106          return self.vnet.get(id)
   1.107  
   1.108 -    def vnet_create(self, info):
   1.109 +    def vnet_create(self, config):
   1.110          """Create a vnet.
   1.111  
   1.112 -        info	config
   1.113 +        @param config: config
   1.114          """
   1.115 -        self.vnet_configure(info)
   1.116 -
   1.117 -    def vnet_configure(self, info):
   1.118 -        """Configure a vnet.
   1.119 -        id	vnet id
   1.120 -        info	config
   1.121 -        """
   1.122 -        # Need to configure for real.
   1.123 -        # Only sync if succeeded - otherwise need to back out.
   1.124 +        info = XendVnetInfo(config)
   1.125          self.vnet[info.id] = info
   1.126 -        self.db.save(info.id, info)
   1.127 +        self.db.save(info.id, info.sxpr())
   1.128 +        info.configure()
   1.129  
   1.130      def vnet_delete(self, id):
   1.131          """Delete a vnet.
   1.132  
   1.133 -        id	vnet id
   1.134 +        @param id: vnet id
   1.135          """
   1.136 -        # Need to delete for real. What if fails?
   1.137 -        if id in self.vnet:
   1.138 +        info = self.vnet_get(id)
   1.139 +        if info:
   1.140              del self.vnet[id]
   1.141              self.db.delete(id)
   1.142 +            info.delete()
   1.143  
   1.144  def instance():
   1.145      global inst
     2.1 --- a/tools/python/xen/xend/server/SrvBase.py	Thu Jul 22 16:39:49 2004 +0000
     2.2 +++ b/tools/python/xen/xend/server/SrvBase.py	Thu Jul 22 16:41:11 2004 +0000
     2.3 @@ -161,8 +161,7 @@ class SrvBase(resource.Resource):
     2.4              sxp.show(['xend.err', str(err)], out=req)
     2.5          else:
     2.6              req.setHeader("Content-Type", "text/plain")
     2.7 -            req.write('Error in ')
     2.8 -            req.write(op)
     2.9 +            req.write('Error ')
    2.10              req.write(': ')
    2.11              req.write(str(err))
    2.12          if dfr: