~0x44/nova/extdoc

« back to all changes in this revision

Viewing changes to nova/virt/vmwareapi/vmops.py

  • Committer: Dan Prince
  • Date: 2011-09-14 15:34:08 UTC
  • mfrom: (1568 nova)
  • mto: This revision was merged to the branch mainline in revision 1570.
  • Revision ID: dan.prince@rackspace.com-20110914153408-y8iqlnz2uphraexp
Merge w/ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import uuid
28
28
 
29
29
from nova import context as nova_context
30
 
from nova import db
31
30
from nova import exception
32
31
from nova import flags
33
32
from nova import log as logging
111
110
        client_factory = self._session._get_vim().client.factory
112
111
        service_content = self._session._get_vim().get_service_content()
113
112
 
114
 
        network = db.network_get_by_instance(nova_context.get_admin_context(),
115
 
                                            instance['id'])
116
 
 
117
 
        net_name = network['bridge']
118
 
 
119
 
        def _check_if_network_bridge_exists():
120
 
            network_ref = \
121
 
                network_utils.get_network_with_the_name(self._session,
122
 
                                                        net_name)
123
 
            if network_ref is None:
124
 
                raise exception.NetworkNotFoundForBridge(bridge=net_name)
125
 
            return network_ref
126
 
 
127
 
        self.plug_vifs(instance, network_info)
128
 
        network_obj = _check_if_network_bridge_exists()
129
 
 
130
113
        def _get_datastore_ref():
131
114
            """Get the datastore list and choose the first local storage."""
132
115
            data_stores = self._session._call_method(vim_util, "get_objects",
182
165
 
183
166
        vm_folder_mor, res_pool_mor = _get_vmfolder_and_res_pool_mors()
184
167
 
 
168
        def _check_if_network_bridge_exists(network_name):
 
169
            network_ref = \
 
170
                network_utils.get_network_with_the_name(self._session,
 
171
                                                        network_name)
 
172
            if network_ref is None:
 
173
                raise exception.NetworkNotFoundForBridge(bridge=network_name)
 
174
            return network_ref
 
175
 
 
176
        def _get_vif_infos():
 
177
            vif_infos = []
 
178
            for (network, mapping) in network_info:
 
179
                mac_address = mapping['mac']
 
180
                network_name = network['bridge']
 
181
                if mapping.get('should_create_vlan'):
 
182
                    network_ref = self._vif_driver.ensure_vlan_bridge(
 
183
                                                        self._session, network)
 
184
                else:
 
185
                    network_ref = _check_if_network_bridge_exists(network_name)
 
186
                vif_infos.append({'network_name': network_name,
 
187
                                  'mac_address': mac_address,
 
188
                                  'network_ref': network_ref,
 
189
                                 })
 
190
            return vif_infos
 
191
 
 
192
        vif_infos = _get_vif_infos()
 
193
 
185
194
        # Get the create vm config spec
186
195
        config_spec = vm_util.get_vm_create_spec(
187
196
                            client_factory, instance,
188
 
                            data_store_name, net_name, os_type,
189
 
                            network_obj)
 
197
                            data_store_name, vif_infos, os_type)
190
198
 
191
199
        def _execute_create_vm():
192
200
            """Create VM on ESX host."""
204
212
 
205
213
        _execute_create_vm()
206
214
 
207
 
        # Set the machine id for the VM for setting the IP
208
 
        self._set_machine_id(client_factory, instance)
 
215
        # Set the machine.id parameter of the instance to inject
 
216
        # the NIC configuration inside the VM
 
217
        if FLAGS.flat_injected:
 
218
            self._set_machine_id(client_factory, instance, network_info)
209
219
 
210
220
        # Naming the VM files in correspondence with the VM instance name
211
221
        # The flat vmdk file name
718
728
        """Return link to instance's ajax console."""
719
729
        return 'http://fakeajaxconsole/fake_url'
720
730
 
721
 
    def _set_machine_id(self, client_factory, instance):
722
 
        """
723
 
        Set the machine id of the VM for guest tools to pick up and change
724
 
        the IP.
725
 
        """
726
 
        admin_context = nova_context.get_admin_context()
 
731
    def _set_machine_id(self, client_factory, instance, network_info):
 
732
        """
 
733
        Set the machine id of the VM for guest tools to pick up and reconfigure
 
734
        the network interfaces.
 
735
        """
727
736
        vm_ref = self._get_vm_ref_from_the_name(instance.name)
728
737
        if vm_ref is None:
729
738
            raise exception.InstanceNotFound(instance_id=instance.id)
730
 
        network = db.network_get_by_instance(nova_context.get_admin_context(),
731
 
                                            instance['id'])
732
 
        mac_address = None
733
 
        if instance['mac_addresses']:
734
 
            mac_address = instance['mac_addresses'][0]['address']
735
 
 
736
 
        net_mask = network["netmask"]
737
 
        gateway = network["gateway"]
738
 
        broadcast = network["broadcast"]
739
 
        # TODO(vish): add support for dns2
740
 
        dns = network["dns1"]
741
 
 
742
 
        addresses = db.instance_get_fixed_addresses(admin_context,
743
 
                                                    instance['id'])
744
 
        ip_addr = addresses[0] if addresses else None
 
739
 
 
740
        machine_id_str = ''
 
741
        for (network, info) in network_info:
 
742
            # TODO(vish): add support for dns2
 
743
            # TODO(sateesh): add support for injection of ipv6 configuration
 
744
            ip_v4 = ip_v6 = None
 
745
            if 'ips' in info and len(info['ips']) > 0:
 
746
                ip_v4 = info['ips'][0]
 
747
            if 'ip6s' in info and len(info['ip6s']) > 0:
 
748
                ip_v6 = info['ip6s'][0]
 
749
            if len(info['dns']) > 0:
 
750
                dns = info['dns'][0]
 
751
            else:
 
752
                dns = ''
 
753
 
 
754
            interface_str = "%s;%s;%s;%s;%s;%s" % \
 
755
                                            (info['mac'],
 
756
                                             ip_v4 and ip_v4['ip'] or '',
 
757
                                             ip_v4 and ip_v4['netmask'] or '',
 
758
                                             info['gateway'],
 
759
                                             info['broadcast'],
 
760
                                             dns)
 
761
            machine_id_str = machine_id_str + interface_str + '#'
745
762
 
746
763
        machine_id_change_spec = \
747
 
            vm_util.get_machine_id_change_spec(client_factory, mac_address,
748
 
                                               ip_addr, net_mask, gateway,
749
 
                                               broadcast, dns)
 
764
            vm_util.get_machine_id_change_spec(client_factory, machine_id_str)
 
765
 
750
766
        LOG.debug(_("Reconfiguring VM instance %(name)s to set the machine id "
751
767
                  "with ip - %(ip_addr)s") %
752
768
                  ({'name': instance.name,
753
 
                   'ip_addr': ip_addr}))
 
769
                   'ip_addr': ip_v4['ip']}))
754
770
        reconfig_task = self._session._call_method(self._session._get_vim(),
755
771
                           "ReconfigVM_Task", vm_ref,
756
772
                           spec=machine_id_change_spec)
758
774
        LOG.debug(_("Reconfigured VM instance %(name)s to set the machine id "
759
775
                  "with ip - %(ip_addr)s") %
760
776
                  ({'name': instance.name,
761
 
                   'ip_addr': ip_addr}))
 
777
                   'ip_addr': ip_v4['ip']}))
762
778
 
763
779
    def _get_datacenter_name_and_ref(self):
764
780
        """Get the datacenter name and the reference."""