forked from openstack/nova
-
Notifications
You must be signed in to change notification settings - Fork 7
/
contrailVC.py
307 lines (254 loc) · 12.2 KB
/
contrailVC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
# Copyright (c) 2012 VMware, Inc.
# Copyright (c) 2011 Citrix Systems, Inc.
# Copyright 2011 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Contrail wrapper around VCenter
"""
import re
import time
import socket
import sys
import uuid
from oslo_config import cfg
from oslo_vmware import vim_util as oslo_vmware_vim_util
from oslo_vmware import exceptions as vexc
from oslo_log import log as logging
from oslo_utils import uuidutils
from nova import exception
#from nova.openstack.common import loopingcall
from nova.virt import driver
from nova.virt.vmwareapi import error_util
from nova.virt.vmwareapi import host
from nova.virt.vmwareapi import vim_util
from nova.virt.vmwareapi import vm_util
from nova.virt.vmwareapi import vmops
from nova.virt.vmwareapi import volumeops
from nova.virt.vmwareapi import network_util
from nova.virt.vmwareapi.driver import VMwareVCDriver
from bitstring import BitArray
#from thrift.transport import TTransport, TSocket
#from thrift.transport.TTransport import TTransportException
#from thrift.protocol import TBinaryProtocol, TProtocol
#from nova_contrail_vif.gen_py.instance_service import InstanceService
#from nova_contrail_vif.gen_py.instance_service import ttypes
LOG = logging.getLogger(__name__)
vmwareapi_contrail_opts = [
cfg.StrOpt('vcenter_dvswitch',
help='Vswitch name to use to instantiate VMs incase of Contaril VCenter solution'),
]
CONF = cfg.CONF
CONF.register_opts(vmwareapi_contrail_opts, 'vmware')
INVALID_VLAN_ID = 4096
MAX_VLAN_ID = 4095
class VCPVlans(object):
"""The private-vlan object"""
def __init__(self, vc_session):
self._vlan_id_bits = BitArray(MAX_VLAN_ID + 1)
self._init_pvlans(vc_session)
#end __init__()
def _init_pvlans(self, session):
self._vlan_id_bits.set(1)
dvs_mor = vm_util.get_dvs_ref_from_name(session,
CONF.vmware.vcenter_dvswitch);
config_ret = session._call_method(oslo_vmware_vim_util,
"get_object_property",
dvs_mor,
"config")
if not config_ret:
return
for pvlan_map_entry in config_ret.pvlanConfig:
if pvlan_map_entry.pvlanType == "isolated":
self._vlan_id_bits[pvlan_map_entry.secondaryVlanId] = 0
port_grps_mor_ret = session._call_method(oslo_vmware_vim_util,
"get_object_property",
dvs_mor,
"portgroup")
if not port_grps_mor_ret:
return
port_grps_mor = port_grps_mor_ret.ManagedObjectReference
for port_grp_mor in port_grps_mor:
cfg_ret = session._call_method(oslo_vmware_vim_util,
"get_object_property",
port_grp_mor,
"config")
if not cfg_ret:
continue;
if not cfg_ret.defaultPortConfig:
continue;
if not cfg_ret.defaultPortConfig.vlan:
continue;
if hasattr(cfg_ret.defaultPortConfig.vlan, "pvlanId"):
self._vlan_id_bits[cfg_ret.defaultPortConfig.vlan.pvlanId] = 1
#end _init_vlans()
def alloc_pvlan(self):
vid = self._vlan_id_bits.find('0b0')
if vid:
self._vlan_id_bits[vid[0]] = 1
return vid[0]
return INVALID_VLAN_ID
#end alloc_vlan()
def free_pvlan(self, vlan_id):
if vlan_id < 0 or vlan_id > MAX_VLAN_ID:
return
self._vlan_id_bits[vlan_id] = 0
#end free_vlan()
#end VCPVlans
class ContrailVCDriver(VMwareVCDriver):
"""Sub class of VC"""
def __init__(self, virtapi, read_only=False, scheme="https"):
super(ContrailVCDriver, self).__init__(virtapi)
if CONF.vmware.vcenter_dvswitch is None:
raise Exception(_("Must specify vcenter_dvswitch, to use "
"compute_driver=vmwareapi.contrailVCDriver"))
self.Vlan = VCPVlans(self._session)
def spawn(self, context, instance, image_meta, injected_files,
admin_password, network_info=None, block_device_info=None):
session = self._session
if network_info:
for vif in network_info:
network_uuid = vif['network']['id']
network_ref=None
try:
network_ref = network_util.get_network_with_the_name(session,
network_uuid, self._vmops._cluster)
except vexc.VMwareDriverException:
LOG.debug("Network %s not found on cluster!", network_uuid)
if not network_ref:
vif['network']['bridge'] = vif['network']['id']
pvlan_id = self.Vlan.alloc_pvlan()
if pvlan_id == INVALID_VLAN_ID:
raise exception.NovaException("Vlan id space is full")
try:
network_util.create_dvport_group(session,
network_uuid,
CONF.vmware.vcenter_dvswitch,
pvlan_id, self._vmops._cluster)
except vexc.DuplicateName:
self.Vlan.free_pvlan(pvlan_id)
else:
vif['network']['bridge'] = vif['network']['id']
#LOG.debug(_("Network %s found on host!") % network_uuid)
args = {'should_create_vlan':False, 'vlan':'0'}
vif['network']._set_meta(args)
super(ContrailVCDriver, self).spawn(context, instance, image_meta,
injected_files, admin_password,
network_info, block_device_info)
def destroy(self, context, instance, network_info, block_device_info=None,
destroy_disks=True, migrate_data=None):
session = self._session
super(ContrailVCDriver, self).destroy(context, instance, network_info,
block_device_info, destroy_disks,
migrate_data)
if not network_info:
return
for vif in network_info:
network_uuid = vif['network']['id']
network_ref=None
try:
network_ref = network_util.get_network_with_the_name(session,
network_uuid, self._vmops._cluster);
except vexc.VMwareDriverException:
LOG.debug("Network %s not found on cluster!", network_uuid)
if not network_ref:
continue;
dvpg_mor = oslo_vmware_vim_util.get_moref(network_ref['dvpg'], network_ref['type']);
vms_ret = session._call_method(oslo_vmware_vim_util,
"get_object_property",
dvpg_mor,
"vm")
if not (vms_ret) or not (vms_ret.ManagedObjectReference):
cfg_ret = session._call_method(oslo_vmware_vim_util,
"get_object_property",
dvpg_mor,
"config")
if (cfg_ret) and (cfg_ret.defaultPortConfig) and (cfg_ret.defaultPortConfig.vlan) :
if hasattr(cfg_ret.defaultPortConfig.vlan, "pvlanId"):
pvlan_id = cfg_ret.defaultPortConfig.vlan.pvlanId
# No VM exists on the dvPortGroup. Delete the network.
# Only if delete network is successful, free pvlan.
# This is to avoid it getting reused when old
# network is hanging around due to failed n/w delete operation
try:
network_util.delete_dvport_group(session, dvpg_mor)
if pvlan_id:
self.Vlan.free_pvlan(pvlan_id)
except Exception as excep:
LOG.warn(("In contrailVC:dvportgroup:delete, "
"got this exception while destroying "
"the dvportGroup: %s") % str(excep))
def plug_vifs(self, instance, network_info):
"""Plug VIFs into networks."""
# vif plug is done by vcenter plugin. Not done here.
return
def attach_interface(self, instance, image_meta, vif):
"""Attach an interface to the instance."""
session = self._session
network_uuid = vif['network']['id']
network_mor=None
try:
network_mor = network_util.get_network_with_the_name(session,
network_uuid, self._vmops._cluster);
except vexc.VMwareDriverException:
LOG.debug("Network %s not found on cluster!", network_uuid)
if not network_mor:
vif['network']['bridge'] = vif['network']['id']
pvlan_id = self.Vlan.alloc_pvlan()
#LOG.debug(_("Allocated pvlan for network %s") % network_uuid)
if pvlan_id == INVALID_VLAN_ID:
raise exception.NovaException("Vlan id space is full")
#LOG.debug(_("creating %s port-group on cluster!") % network_uuid)
network_util.create_dvport_group(session,
network_uuid,
CONF.vmware.vcenter_dvswitch,
pvlan_id, _vmops._cluster)
else:
vif['network']['bridge'] = vif['network']['id']
#LOG.debug(_("Network %s found on host!") % network_uuid)
args = {'should_create_vlan':False, 'vlan':'0'}
vif['network']._set_meta(args)
super(ContrailVCDriver, self).attach_interface(instance, image_meta, vif)
def detach_interface(self, instance, vif):
"""Detach an interface from the instance."""
super(ContrailVCDriver, self).detach_interface(instance, vif)
session = self._session
network_uuid = vif['network']['id']
dvpg_mor=None
try:
dvpg_mor = network_util.get_network_with_the_name(session,
network_uuid, self._vmops._cluster);
except vexc.VMwareDriverException:
LOG.debug("Network %s not found on cluster!", network_uuid)
if not dvpg_mor:
return;
vms_ret = session._call_method(oslo_vmware_vim_util,
"get_object_property",
dvpg_mor,
"vm")
if not (vms_ret) or (vms_ret.ManagedObjectReference):
cfg_ret = session._call_method(oslo_vmware_vim_util,
"get_object_property",
dvpg_mor,
"config")
if not cfg_ret:
return;
if not cfg_ret.defaultPortConfig:
return;
if not cfg_ret.defaultPortConfig.vlan:
return;
if hasattr(cfg_ret.defaultPortConfig.vlan, "pvlanId"):
self.Vlan.free_pvlan(cfg_ret.defaultPortConfig.vlan.pvlanId)
self.remove_dvport_group(dvpg_mor)