ia64/xen-unstable

changeset 14960:39383ac1aec8

Add support for creating networks to xm create XML file

signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
author Tom Wilkie <tom.wilkie@gmail.com>
date Thu Apr 26 17:30:56 2007 +0100 (2007-04-26)
parents 5754173c3d81
children f18e1ca69380
files tools/python/xen/xm/create.dtd tools/python/xen/xm/xenapi_create.py
line diff
     1.1 --- a/tools/python/xen/xm/create.dtd	Thu Apr 26 16:45:06 2007 +0100
     1.2 +++ b/tools/python/xen/xm/create.dtd	Thu Apr 26 17:30:56 2007 +0100
     1.3 @@ -27,7 +27,8 @@
     1.4                              | crashdump )">
     1.5  
     1.6  <!ELEMENT xm (vm*, 
     1.7 -              vdi*)> 
     1.8 +              vdi*,
     1.9 +              network*)> 
    1.10  
    1.11  <!ELEMENT version (#PCDATA)>
    1.12   
    1.13 @@ -98,6 +99,12 @@
    1.14                   sharable        CDATA #REQUIRED
    1.15                   read_only       CDATA #REQUIRED>
    1.16  
    1.17 +<!ELEMENT network (name,
    1.18 +                  other_config*)>
    1.19 +<!ATTLIST network %NAMEID;
    1.20 +                  default_gateway CDATA #REQUIRED
    1.21 +                  default_netmask CDATA #REQUIRED>
    1.22 +
    1.23  <!ELEMENT name   (label, 
    1.24                    description)> 
    1.25  
     2.1 --- a/tools/python/xen/xm/xenapi_create.py	Thu Apr 26 16:45:06 2007 +0100
     2.2 +++ b/tools/python/xen/xm/xenapi_create.py	Thu Apr 26 17:30:56 2007 +0100
     2.3 @@ -93,10 +93,13 @@ class xenapi_create:
     2.4  
     2.5          vdis = document.getElementsByTagName("vdi")
     2.6          vdi_refs_dict = self.create_vdis(vdis)
     2.7 +
     2.8 +        networks = document.getElementsByTagName("network")
     2.9 +        network_refs_dict = self.create_networks(networks)
    2.10          
    2.11          try:    
    2.12              vms = document.getElementsByTagName("vm")
    2.13 -            return self.create_vms(vms, vdi_refs_dict)
    2.14 +            return self.create_vms(vms, vdi_refs_dict, network_refs_dict)
    2.15          except Exception, exn:
    2.16              try_quietly(self.cleanup_vdis(vdi_refs_dict))
    2.17              raise exn
    2.18 @@ -223,11 +226,33 @@ class xenapi_create:
    2.19          
    2.20          return (key, value)
    2.21  
    2.22 -    def create_vms(self, vms, vdis):
    2.23 +    def create_networks(self, networks):
    2.24 +        log(DEBUG, "create_networks")
    2.25 +        return dict(map(self.create_network, networks))
    2.26 +
    2.27 +    def create_network(self, network):
    2.28 +        log(DEBUG, "create_network")
    2.29 +
    2.30 +        network_record = {
    2.31 +            "name_label":       get_name_label(network),
    2.32 +            "name_description": get_name_description(network),
    2.33 +            "other_config":
    2.34 +                get_child_nodes_as_dict(network, "other_config",
    2.35 +                                        "key", "value"),
    2.36 +            "default_netmask":  network.attributes["default_netmask"].value,
    2.37 +            "default_gateway":  network.attributes["default_gateway"].value
    2.38 +            }
    2.39 +
    2.40 +        key = network.attributes["name"].value
    2.41 +        value = server.xenapi.network.create(network_record)
    2.42 +
    2.43 +        return (key, value)
    2.44 +        
    2.45 +    def create_vms(self, vms, vdis, networks):
    2.46          log(DEBUG, "create_vms")
    2.47 -        return map(lambda vm: self.create_vm(vm, vdis), vms)
    2.48 +        return map(lambda vm: self.create_vm(vm, vdis, networks), vms)
    2.49  
    2.50 -    def create_vm(self, vm, vdis):
    2.51 +    def create_vm(self, vm, vdis, networks):
    2.52          log(DEBUG, "create_vm")
    2.53  
    2.54          vm_record = {
    2.55 @@ -321,7 +346,7 @@ class xenapi_create:
    2.56  
    2.57              vifs = vm.getElementsByTagName("vif")
    2.58  
    2.59 -            self.create_vifs(vm_ref, vifs)
    2.60 +            self.create_vifs(vm_ref, vifs, networks)
    2.61  
    2.62              # Now create consoles
    2.63  
    2.64 @@ -363,31 +388,35 @@ class xenapi_create:
    2.65  
    2.66          return server.xenapi.VBD.create(vbd_record)
    2.67  
    2.68 -    def create_vifs(self, vm_ref, vifs):
    2.69 +    def create_vifs(self, vm_ref, vifs, networks):
    2.70          log(DEBUG, "create_vifs")
    2.71 -        return map(lambda vif: self.create_vif(vm_ref, vif), vifs)
    2.72 +        return map(lambda vif: self.create_vif(vm_ref, vif, networks), vifs)
    2.73  
    2.74 -    def create_vif(self, vm_ref, vif):
    2.75 +    def create_vif(self, vm_ref, vif, networks):
    2.76          log(DEBUG, "create_vif")
    2.77  
    2.78 -        if "network" in vif.attributes.keys():
    2.79 -            networks = [network_ref
    2.80 -                for network_ref in server.xenapi.network.get_all()
    2.81 -                if server.xenapi.network.get_name_label(network_ref)
    2.82 -                       == vif.attributes["network"].value]
    2.83 -            if len(networks) > 0:
    2.84 -                network = networks[0]
    2.85 +        if 'network' in vif.attributes.keys():
    2.86 +            network_name = vif.attributes['network'].value
    2.87 +
    2.88 +            if network_name in networks.keys():
    2.89 +                network_uuid = networks[network_name]
    2.90              else:
    2.91 -                raise OptionError("Network %s doesn't exist"
    2.92 +                networks = dict([(record['name_label'], record['uuid'])
    2.93 +                                 for record in
    2.94 +                                 server.xenapi.network.get_all_record()])
    2.95 +                if network_name in networks.keys():
    2.96 +                    network_uuid = networks[network_name]
    2.97 +                else:
    2.98 +                    raise OptionError("Network %s doesn't exist"
    2.99                                    % vif.attributes["network"].value)
   2.100          else:
   2.101 -            network = self._get_network_ref()
   2.102 +            network_uuid = self._get_network_ref()
   2.103  
   2.104          vif_record = {
   2.105              "device":
   2.106                  vif.attributes["device"].value,
   2.107              "network":
   2.108 -                network,
   2.109 +                network_uuid,
   2.110              "VM":
   2.111                  vm_ref,
   2.112              "MAC":