111
110
client_factory = self._session._get_vim().client.factory
112
111
service_content = self._session._get_vim().get_service_content()
114
network = db.network_get_by_instance(nova_context.get_admin_context(),
117
net_name = network['bridge']
119
def _check_if_network_bridge_exists():
121
network_utils.get_network_with_the_name(self._session,
123
if network_ref is None:
124
raise exception.NetworkNotFoundForBridge(bridge=net_name)
127
self.plug_vifs(instance, network_info)
128
network_obj = _check_if_network_bridge_exists()
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",
183
166
vm_folder_mor, res_pool_mor = _get_vmfolder_and_res_pool_mors()
168
def _check_if_network_bridge_exists(network_name):
170
network_utils.get_network_with_the_name(self._session,
172
if network_ref is None:
173
raise exception.NetworkNotFoundForBridge(bridge=network_name)
176
def _get_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)
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,
192
vif_infos = _get_vif_infos()
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,
197
data_store_name, vif_infos, os_type)
191
199
def _execute_create_vm():
192
200
"""Create VM on ESX host."""
205
213
_execute_create_vm()
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)
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'
721
def _set_machine_id(self, client_factory, instance):
723
Set the machine id of the VM for guest tools to pick up and change
726
admin_context = nova_context.get_admin_context()
731
def _set_machine_id(self, client_factory, instance, network_info):
733
Set the machine id of the VM for guest tools to pick up and reconfigure
734
the network interfaces.
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(),
733
if instance['mac_addresses']:
734
mac_address = instance['mac_addresses'][0]['address']
736
net_mask = network["netmask"]
737
gateway = network["gateway"]
738
broadcast = network["broadcast"]
739
# TODO(vish): add support for dns2
740
dns = network["dns1"]
742
addresses = db.instance_get_fixed_addresses(admin_context,
744
ip_addr = addresses[0] if addresses else None
741
for (network, info) in network_info:
742
# TODO(vish): add support for dns2
743
# TODO(sateesh): add support for injection of ipv6 configuration
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:
754
interface_str = "%s;%s;%s;%s;%s;%s" % \
756
ip_v4 and ip_v4['ip'] or '',
757
ip_v4 and ip_v4['netmask'] or '',
761
machine_id_str = machine_id_str + interface_str + '#'
746
763
machine_id_change_spec = \
747
vm_util.get_machine_id_change_spec(client_factory, mac_address,
748
ip_addr, net_mask, gateway,
764
vm_util.get_machine_id_change_spec(client_factory, machine_id_str)
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)