minor change to increase log information
[osm/RO.git] / osm_ro / vimconn_openstack.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 '''
25 osconnector implements all the methods to interact with openstack using the python-client.
26 '''
27 __author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research"
28 __date__ ="$22-jun-2014 11:19:29$"
29
30 import vimconn
31 import json
32 import yaml
33 import logging
34 import netaddr
35 import time
36 import yaml
37 import random
38
39 from novaclient import client as nClient_v2, exceptions as nvExceptions
40 from novaclient import api_versions
41 import keystoneclient.v2_0.client as ksClient_v2
42 from novaclient.v2.client import Client as nClient
43 import keystoneclient.v3.client as ksClient
44 import keystoneclient.exceptions as ksExceptions
45 import glanceclient.v2.client as glClient
46 import glanceclient.client as gl1Client
47 import glanceclient.exc as gl1Exceptions
48 import cinderclient.v2.client as cClient_v2
49 from httplib import HTTPException
50 from neutronclient.neutron import client as neClient_v2
51 from neutronclient.v2_0 import client as neClient
52 from neutronclient.common import exceptions as neExceptions
53 from requests.exceptions import ConnectionError
54
55 '''contain the openstack virtual machine status to openmano status'''
56 vmStatus2manoFormat={'ACTIVE':'ACTIVE',
57 'PAUSED':'PAUSED',
58 'SUSPENDED': 'SUSPENDED',
59 'SHUTOFF':'INACTIVE',
60 'BUILD':'BUILD',
61 'ERROR':'ERROR','DELETED':'DELETED'
62 }
63 netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
64 }
65
66 #global var to have a timeout creating and deleting volumes
67 volume_timeout = 60
68 server_timeout = 60
69
70 class vimconnector(vimconn.vimconnector):
71 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
72 log_level=None, config={}, persistent_info={}):
73 '''using common constructor parameters. In this case
74 'url' is the keystone authorization url,
75 'url_admin' is not use
76 '''
77 self.osc_api_version = 'v2.0'
78 if config.get('APIversion') == 'v3.3':
79 self.osc_api_version = 'v3.3'
80 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config)
81
82 self.persistent_info = persistent_info
83 self.k_creds={}
84 self.n_creds={}
85 if self.config.get("insecure"):
86 self.k_creds["insecure"] = True
87 self.n_creds["insecure"] = True
88 if not url:
89 raise TypeError, 'url param can not be NoneType'
90 self.k_creds['auth_url'] = url
91 self.n_creds['auth_url'] = url
92 if tenant_name:
93 self.k_creds['tenant_name'] = tenant_name
94 self.n_creds['project_id'] = tenant_name
95 if tenant_id:
96 self.k_creds['tenant_id'] = tenant_id
97 self.n_creds['tenant_id'] = tenant_id
98 if user:
99 self.k_creds['username'] = user
100 self.n_creds['username'] = user
101 if passwd:
102 self.k_creds['password'] = passwd
103 self.n_creds['api_key'] = passwd
104 if self.osc_api_version == 'v3.3':
105 self.k_creds['project_name'] = tenant_name
106 self.k_creds['project_id'] = tenant_id
107 if config.get('region_name'):
108 self.k_creds['region_name'] = config.get('region_name')
109 self.n_creds['region_name'] = config.get('region_name')
110
111 self.reload_client = True
112 self.logger = logging.getLogger('openmano.vim.openstack')
113 if log_level:
114 self.logger.setLevel( getattr(logging, log_level) )
115
116 def __setitem__(self,index, value):
117 '''Set individuals parameters
118 Throw TypeError, KeyError
119 '''
120 if index=='tenant_id':
121 self.reload_client=True
122 self.tenant_id = value
123 if self.osc_api_version == 'v3.3':
124 if value:
125 self.k_creds['project_id'] = value
126 self.n_creds['project_id'] = value
127 else:
128 del self.k_creds['project_id']
129 del self.n_creds['project_id']
130 else:
131 if value:
132 self.k_creds['tenant_id'] = value
133 self.n_creds['tenant_id'] = value
134 else:
135 del self.k_creds['tenant_id']
136 del self.n_creds['tenant_id']
137 elif index=='tenant_name':
138 self.reload_client=True
139 self.tenant_name = value
140 if self.osc_api_version == 'v3.3':
141 if value:
142 self.k_creds['project_name'] = value
143 self.n_creds['project_name'] = value
144 else:
145 del self.k_creds['project_name']
146 del self.n_creds['project_name']
147 else:
148 if value:
149 self.k_creds['tenant_name'] = value
150 self.n_creds['project_id'] = value
151 else:
152 del self.k_creds['tenant_name']
153 del self.n_creds['project_id']
154 elif index=='user':
155 self.reload_client=True
156 self.user = value
157 if value:
158 self.k_creds['username'] = value
159 self.n_creds['username'] = value
160 else:
161 del self.k_creds['username']
162 del self.n_creds['username']
163 elif index=='passwd':
164 self.reload_client=True
165 self.passwd = value
166 if value:
167 self.k_creds['password'] = value
168 self.n_creds['api_key'] = value
169 else:
170 del self.k_creds['password']
171 del self.n_creds['api_key']
172 elif index=='url':
173 self.reload_client=True
174 self.url = value
175 if value:
176 self.k_creds['auth_url'] = value
177 self.n_creds['auth_url'] = value
178 else:
179 raise TypeError, 'url param can not be NoneType'
180 else:
181 vimconn.vimconnector.__setitem__(self,index, value)
182
183 def _reload_connection(self):
184 '''Called before any operation, it check if credentials has changed
185 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
186 '''
187 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
188 if self.reload_client:
189 #test valid params
190 if len(self.n_creds) <4:
191 raise ksExceptions.ClientException("Not enough parameters to connect to openstack")
192 if self.osc_api_version == 'v3.3':
193 self.nova = nClient(api_version=api_versions.APIVersion(version_str='2.0'), **self.n_creds)
194 #TODO To be updated for v3
195 #self.cinder = cClient.Client(**self.n_creds)
196 self.keystone = ksClient.Client(**self.k_creds)
197 self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
198 self.neutron = neClient.Client(api_version=api_versions.APIVersion(version_str='2.0'), endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds)
199 else:
200 self.nova = nClient_v2.Client(version='2', **self.n_creds)
201 self.cinder = cClient_v2.Client(**self.n_creds)
202 self.keystone = ksClient_v2.Client(**self.k_creds)
203 self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
204 self.neutron = neClient_v2.Client('2.0', endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds)
205 self.glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
206 self.glance = glClient.Client(self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds
207 self.reload_client = False
208
209 def __net_os2mano(self, net_list_dict):
210 '''Transform the net openstack format to mano format
211 net_list_dict can be a list of dict or a single dict'''
212 if type(net_list_dict) is dict:
213 net_list_=(net_list_dict,)
214 elif type(net_list_dict) is list:
215 net_list_=net_list_dict
216 else:
217 raise TypeError("param net_list_dict must be a list or a dictionary")
218 for net in net_list_:
219 if net.get('provider:network_type') == "vlan":
220 net['type']='data'
221 else:
222 net['type']='bridge'
223
224
225
226 def _format_exception(self, exception):
227 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
228 if isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
229 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed
230 )):
231 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
232 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
233 neExceptions.NeutronException, nvExceptions.BadRequest)):
234 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception))
235 elif isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound)):
236 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + str(exception))
237 elif isinstance(exception, nvExceptions.Conflict):
238 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + str(exception))
239 else: # ()
240 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
241
242 def get_tenant_list(self, filter_dict={}):
243 '''Obtain tenants of VIM
244 filter_dict can contain the following keys:
245 name: filter by tenant name
246 id: filter by tenant uuid/id
247 <other VIM specific>
248 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
249 '''
250 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
251 try:
252 self._reload_connection()
253 if self.osc_api_version == 'v3.3':
254 project_class_list=self.keystone.projects.findall(**filter_dict)
255 else:
256 project_class_list=self.keystone.tenants.findall(**filter_dict)
257 project_list=[]
258 for project in project_class_list:
259 project_list.append(project.to_dict())
260 return project_list
261 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
262 self._format_exception(e)
263
264 def new_tenant(self, tenant_name, tenant_description):
265 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
266 self.logger.debug("Adding a new tenant name: %s", tenant_name)
267 try:
268 self._reload_connection()
269 if self.osc_api_version == 'v3.3':
270 project=self.keystone.projects.create(tenant_name, tenant_description)
271 else:
272 project=self.keystone.tenants.create(tenant_name, tenant_description)
273 return project.id
274 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
275 self._format_exception(e)
276
277 def delete_tenant(self, tenant_id):
278 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
279 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
280 try:
281 self._reload_connection()
282 if self.osc_api_version == 'v3.3':
283 self.keystone.projects.delete(tenant_id)
284 else:
285 self.keystone.tenants.delete(tenant_id)
286 return tenant_id
287 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
288 self._format_exception(e)
289
290 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None):
291 '''Adds a tenant network to VIM. Returns the network identifier'''
292 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
293 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
294 try:
295 new_net = None
296 self._reload_connection()
297 network_dict = {'name': net_name, 'admin_state_up': True}
298 if net_type=="data" or net_type=="ptp":
299 if self.config.get('dataplane_physical_net') == None:
300 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
301 network_dict["provider:physical_network"] = self.config['dataplane_physical_net'] #"physnet_sriov" #TODO physical
302 network_dict["provider:network_type"] = "vlan"
303 if vlan!=None:
304 network_dict["provider:network_type"] = vlan
305 network_dict["shared"]=shared
306 new_net=self.neutron.create_network({'network':network_dict})
307 #print new_net
308 #create subnetwork, even if there is no profile
309 if not ip_profile:
310 ip_profile = {}
311 if 'subnet_address' not in ip_profile:
312 #Fake subnet is required
313 subnet_rand = random.randint(0, 255)
314 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
315 if 'ip_version' not in ip_profile:
316 ip_profile['ip_version'] = "IPv4"
317 subnet={"name":net_name+"-subnet",
318 "network_id": new_net["network"]["id"],
319 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
320 "cidr": ip_profile['subnet_address']
321 }
322 if 'gateway_address' in ip_profile:
323 subnet['gateway_ip'] = ip_profile['gateway_address']
324 if ip_profile.get('dns_address'):
325 subnet['dns_nameservers'] = ip_profile['dns_address'].split(";")
326 if 'dhcp_enabled' in ip_profile:
327 subnet['enable_dhcp'] = False if ip_profile['dhcp_enabled']=="false" else True
328 if 'dhcp_start_address' in ip_profile:
329 subnet['allocation_pools']=[]
330 subnet['allocation_pools'].append(dict())
331 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
332 if 'dhcp_count' in ip_profile:
333 #parts = ip_profile['dhcp_start_address'].split('.')
334 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
335 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
336 ip_int += ip_profile['dhcp_count'] - 1
337 ip_str = str(netaddr.IPAddress(ip_int))
338 subnet['allocation_pools'][0]['end'] = ip_str
339 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
340 self.neutron.create_subnet({"subnet": subnet} )
341 return new_net["network"]["id"]
342 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
343 if new_net:
344 self.neutron.delete_network(new_net['network']['id'])
345 self._format_exception(e)
346
347 def get_network_list(self, filter_dict={}):
348 '''Obtain tenant networks of VIM
349 Filter_dict can be:
350 name: network name
351 id: network uuid
352 shared: boolean
353 tenant_id: tenant
354 admin_state_up: boolean
355 status: 'ACTIVE'
356 Returns the network list of dictionaries
357 '''
358 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
359 try:
360 self._reload_connection()
361 if self.osc_api_version == 'v3.3' and "tenant_id" in filter_dict:
362 filter_dict['project_id'] = filter_dict.pop('tenant_id')
363 net_dict=self.neutron.list_networks(**filter_dict)
364 net_list=net_dict["networks"]
365 self.__net_os2mano(net_list)
366 return net_list
367 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
368 self._format_exception(e)
369
370 def get_network(self, net_id):
371 '''Obtain details of network from VIM
372 Returns the network information from a network id'''
373 self.logger.debug(" Getting tenant network %s from VIM", net_id)
374 filter_dict={"id": net_id}
375 net_list = self.get_network_list(filter_dict)
376 if len(net_list)==0:
377 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
378 elif len(net_list)>1:
379 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
380 net = net_list[0]
381 subnets=[]
382 for subnet_id in net.get("subnets", () ):
383 try:
384 subnet = self.neutron.show_subnet(subnet_id)
385 except Exception as e:
386 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
387 subnet = {"id": subnet_id, "fault": str(e)}
388 subnets.append(subnet)
389 net["subnets"] = subnets
390 net["encapsulation"] = net.get('provider:network_type')
391 net["segmentation_id"] = net.get('provider:segmentation_id')
392 return net
393
394 def delete_network(self, net_id):
395 '''Deletes a tenant network from VIM. Returns the old network identifier'''
396 self.logger.debug("Deleting network '%s' from VIM", net_id)
397 try:
398 self._reload_connection()
399 #delete VM ports attached to this networks before the network
400 ports = self.neutron.list_ports(network_id=net_id)
401 for p in ports['ports']:
402 try:
403 self.neutron.delete_port(p["id"])
404 except Exception as e:
405 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
406 self.neutron.delete_network(net_id)
407 return net_id
408 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
409 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
410 self._format_exception(e)
411
412 def refresh_nets_status(self, net_list):
413 '''Get the status of the networks
414 Params: the list of network identifiers
415 Returns a dictionary with:
416 net_id: #VIM id of this network
417 status: #Mandatory. Text with one of:
418 # DELETED (not found at vim)
419 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
420 # OTHER (Vim reported other status not understood)
421 # ERROR (VIM indicates an ERROR status)
422 # ACTIVE, INACTIVE, DOWN (admin down),
423 # BUILD (on building process)
424 #
425 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
426 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
427
428 '''
429 net_dict={}
430 for net_id in net_list:
431 net = {}
432 try:
433 net_vim = self.get_network(net_id)
434 if net_vim['status'] in netStatus2manoFormat:
435 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
436 else:
437 net["status"] = "OTHER"
438 net["error_msg"] = "VIM status reported " + net_vim['status']
439
440 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
441 net['status'] = 'DOWN'
442 try:
443 net['vim_info'] = yaml.safe_dump(net_vim, default_flow_style=True, width=256)
444 except yaml.representer.RepresenterError:
445 net['vim_info'] = str(net_vim)
446 if net_vim.get('fault'): #TODO
447 net['error_msg'] = str(net_vim['fault'])
448 except vimconn.vimconnNotFoundException as e:
449 self.logger.error("Exception getting net status: %s", str(e))
450 net['status'] = "DELETED"
451 net['error_msg'] = str(e)
452 except vimconn.vimconnException as e:
453 self.logger.error("Exception getting net status: %s", str(e))
454 net['status'] = "VIM_ERROR"
455 net['error_msg'] = str(e)
456 net_dict[net_id] = net
457 return net_dict
458
459 def get_flavor(self, flavor_id):
460 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
461 self.logger.debug("Getting flavor '%s'", flavor_id)
462 try:
463 self._reload_connection()
464 flavor = self.nova.flavors.find(id=flavor_id)
465 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
466 return flavor.to_dict()
467 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
468 self._format_exception(e)
469
470 def get_flavor_id_from_data(self, flavor_dict):
471 """Obtain flavor id that match the flavor description
472 Returns the flavor_id or raises a vimconnNotFoundException
473 flavor_dict: contains the required ram, vcpus, disk
474 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
475 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
476 vimconnNotFoundException is raised
477 """
478 exact_match = False if self.config.get('use_existing_flavors') else True
479 try:
480 self._reload_connection()
481 flavor_candidate_id = None
482 flavor_candidate_data = (10000, 10000, 10000)
483 flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"])
484 # numa=None
485 numas = flavor_dict.get("extended", {}).get("numas")
486 if numas:
487 #TODO
488 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemted")
489 # if len(numas) > 1:
490 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
491 # numa=numas[0]
492 # numas = extended.get("numas")
493 for flavor in self.nova.flavors.list():
494 epa = flavor.get_keys()
495 if epa:
496 continue
497 # TODO
498 flavor_data = (flavor.ram, flavor.vcpus, flavor.disk)
499 if flavor_data == flavor_target:
500 return flavor.id
501 elif not exact_match and flavor_target < flavor_data < flavor_candidate_data:
502 flavor_candidate_id = flavor.id
503 flavor_candidate_data = flavor_data
504 if not exact_match and flavor_candidate_id:
505 return flavor_candidate_id
506 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
507 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
508 self._format_exception(e)
509
510
511 def new_flavor(self, flavor_data, change_name_if_used=True):
512 '''Adds a tenant flavor to openstack VIM
513 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
514 Returns the flavor identifier
515 '''
516 self.logger.debug("Adding flavor '%s'", str(flavor_data))
517 retry=0
518 max_retries=3
519 name_suffix = 0
520 name=flavor_data['name']
521 while retry<max_retries:
522 retry+=1
523 try:
524 self._reload_connection()
525 if change_name_if_used:
526 #get used names
527 fl_names=[]
528 fl=self.nova.flavors.list()
529 for f in fl:
530 fl_names.append(f.name)
531 while name in fl_names:
532 name_suffix += 1
533 name = flavor_data['name']+"-" + str(name_suffix)
534
535 ram = flavor_data.get('ram',64)
536 vcpus = flavor_data.get('vcpus',1)
537 numa_properties=None
538
539 extended = flavor_data.get("extended")
540 if extended:
541 numas=extended.get("numas")
542 if numas:
543 numa_nodes = len(numas)
544 if numa_nodes > 1:
545 return -1, "Can not add flavor with more than one numa"
546 numa_properties = {"hw:numa_nodes":str(numa_nodes)}
547 numa_properties["hw:mem_page_size"] = "large"
548 numa_properties["hw:cpu_policy"] = "dedicated"
549 numa_properties["hw:numa_mempolicy"] = "strict"
550 for numa in numas:
551 #overwrite ram and vcpus
552 ram = numa['memory']*1024
553 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
554 if 'paired-threads' in numa:
555 vcpus = numa['paired-threads']*2
556 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
557 numa_properties["hw:cpu_thread_policy"] = "require"
558 numa_properties["hw:cpu_policy"] = "dedicated"
559 elif 'cores' in numa:
560 vcpus = numa['cores']
561 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
562 numa_properties["hw:cpu_thread_policy"] = "isolate"
563 numa_properties["hw:cpu_policy"] = "dedicated"
564 elif 'threads' in numa:
565 vcpus = numa['threads']
566 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
567 numa_properties["hw:cpu_thread_policy"] = "prefer"
568 numa_properties["hw:cpu_policy"] = "dedicated"
569 # for interface in numa.get("interfaces",() ):
570 # if interface["dedicated"]=="yes":
571 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
572 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
573
574 #create flavor
575 new_flavor=self.nova.flavors.create(name,
576 ram,
577 vcpus,
578 flavor_data.get('disk',1),
579 is_public=flavor_data.get('is_public', True)
580 )
581 #add metadata
582 if numa_properties:
583 new_flavor.set_keys(numa_properties)
584 return new_flavor.id
585 except nvExceptions.Conflict as e:
586 if change_name_if_used and retry < max_retries:
587 continue
588 self._format_exception(e)
589 #except nvExceptions.BadRequest as e:
590 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
591 self._format_exception(e)
592
593 def delete_flavor(self,flavor_id):
594 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
595 '''
596 try:
597 self._reload_connection()
598 self.nova.flavors.delete(flavor_id)
599 return flavor_id
600 #except nvExceptions.BadRequest as e:
601 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
602 self._format_exception(e)
603
604 def new_image(self,image_dict):
605 '''
606 Adds a tenant image to VIM. imge_dict is a dictionary with:
607 name: name
608 disk_format: qcow2, vhd, vmdk, raw (by default), ...
609 location: path or URI
610 public: "yes" or "no"
611 metadata: metadata of the image
612 Returns the image_id
613 '''
614 #using version 1 of glance client
615 glancev1 = gl1Client.Client('1',self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds
616 retry=0
617 max_retries=3
618 while retry<max_retries:
619 retry+=1
620 try:
621 self._reload_connection()
622 #determine format http://docs.openstack.org/developer/glance/formats.html
623 if "disk_format" in image_dict:
624 disk_format=image_dict["disk_format"]
625 else: #autodiscover based on extension
626 if image_dict['location'][-6:]==".qcow2":
627 disk_format="qcow2"
628 elif image_dict['location'][-4:]==".vhd":
629 disk_format="vhd"
630 elif image_dict['location'][-5:]==".vmdk":
631 disk_format="vmdk"
632 elif image_dict['location'][-4:]==".vdi":
633 disk_format="vdi"
634 elif image_dict['location'][-4:]==".iso":
635 disk_format="iso"
636 elif image_dict['location'][-4:]==".aki":
637 disk_format="aki"
638 elif image_dict['location'][-4:]==".ari":
639 disk_format="ari"
640 elif image_dict['location'][-4:]==".ami":
641 disk_format="ami"
642 else:
643 disk_format="raw"
644 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
645 if image_dict['location'][0:4]=="http":
646 new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
647 container_format="bare", location=image_dict['location'], disk_format=disk_format)
648 else: #local path
649 with open(image_dict['location']) as fimage:
650 new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
651 container_format="bare", data=fimage, disk_format=disk_format)
652 #insert metadata. We cannot use 'new_image.properties.setdefault'
653 #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
654 new_image_nova=self.nova.images.find(id=new_image.id)
655 new_image_nova.metadata.setdefault('location',image_dict['location'])
656 metadata_to_load = image_dict.get('metadata')
657 if metadata_to_load:
658 for k,v in yaml.load(metadata_to_load).iteritems():
659 new_image_nova.metadata.setdefault(k,v)
660 return new_image.id
661 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
662 self._format_exception(e)
663 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
664 if retry==max_retries:
665 continue
666 self._format_exception(e)
667 except IOError as e: #can not open the file
668 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
669 http_code=vimconn.HTTP_Bad_Request)
670
671 def delete_image(self, image_id):
672 '''Deletes a tenant image from openstack VIM. Returns the old id
673 '''
674 try:
675 self._reload_connection()
676 self.nova.images.delete(image_id)
677 return image_id
678 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: #TODO remove
679 self._format_exception(e)
680
681 def get_image_id_from_path(self, path):
682 '''Get the image id from image path in the VIM database. Returns the image_id'''
683 try:
684 self._reload_connection()
685 images = self.nova.images.list()
686 for image in images:
687 if image.metadata.get("location")==path:
688 return image.id
689 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
690 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
691 self._format_exception(e)
692
693 def get_image_list(self, filter_dict={}):
694 '''Obtain tenant images from VIM
695 Filter_dict can be:
696 id: image id
697 name: image name
698 checksum: image checksum
699 Returns the image list of dictionaries:
700 [{<the fields at Filter_dict plus some VIM specific>}, ...]
701 List can be empty
702 '''
703 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
704 try:
705 self._reload_connection()
706 filter_dict_os=filter_dict.copy()
707 #First we filter by the available filter fields: name, id. The others are removed.
708 filter_dict_os.pop('checksum',None)
709 image_list=self.nova.images.findall(**filter_dict_os)
710 if len(image_list)==0:
711 return []
712 #Then we filter by the rest of filter fields: checksum
713 filtered_list = []
714 for image in image_list:
715 image_class=self.glance.images.get(image.id)
716 if 'checksum' not in filter_dict or image_class['checksum']==filter_dict.get('checksum'):
717 filtered_list.append(image_class.copy())
718 return filtered_list
719 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
720 self._format_exception(e)
721
722 def new_vminstance(self,name,description,start,image_id,flavor_id,net_list,cloud_config=None,disk_list=None):
723 '''Adds a VM instance to VIM
724 Params:
725 start: indicates if VM must start or boot in pause mode. Ignored
726 image_id,flavor_id: iamge and flavor uuid
727 net_list: list of interfaces, each one is a dictionary with:
728 name:
729 net_id: network uuid to connect
730 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
731 model: interface model, ignored #TODO
732 mac_address: used for SR-IOV ifaces #TODO for other types
733 use: 'data', 'bridge', 'mgmt'
734 type: 'virtual', 'PF', 'VF', 'VFnotShared'
735 vim_id: filled/added by this function
736 floating_ip: True/False (or it can be None)
737 #TODO ip, security groups
738 Returns the instance identifier
739 '''
740 self.logger.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id, flavor_id,str(net_list))
741 try:
742 metadata={}
743 net_list_vim=[]
744 external_network=[] #list of external networks to be connected to instance, later on used to create floating_ip
745 self._reload_connection()
746 metadata_vpci={} #For a specific neutron plugin
747 for net in net_list:
748 if not net.get("net_id"): #skip non connected iface
749 continue
750
751 port_dict={
752 "network_id": net["net_id"],
753 "name": net.get("name"),
754 "admin_state_up": True
755 }
756 if net["type"]=="virtual":
757 if "vpci" in net:
758 metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
759 elif net["type"]=="VF": # for VF
760 if "vpci" in net:
761 if "VF" not in metadata_vpci:
762 metadata_vpci["VF"]=[]
763 metadata_vpci["VF"].append([ net["vpci"], "" ])
764 port_dict["binding:vnic_type"]="direct"
765 else: #For PT
766 if "vpci" in net:
767 if "PF" not in metadata_vpci:
768 metadata_vpci["PF"]=[]
769 metadata_vpci["PF"].append([ net["vpci"], "" ])
770 port_dict["binding:vnic_type"]="direct-physical"
771 if not port_dict["name"]:
772 port_dict["name"]=name
773 if net.get("mac_address"):
774 port_dict["mac_address"]=net["mac_address"]
775 if net.get("port_security") == False:
776 port_dict["port_security_enabled"]=net["port_security"]
777 new_port = self.neutron.create_port({"port": port_dict })
778 net["mac_adress"] = new_port["port"]["mac_address"]
779 net["vim_id"] = new_port["port"]["id"]
780 net["ip"] = new_port["port"].get("fixed_ips", [{}])[0].get("ip_address")
781 net_list_vim.append({"port-id": new_port["port"]["id"]})
782
783 if net.get('floating_ip', False):
784 net['exit_on_floating_ip_error'] = True
785 external_network.append(net)
786 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
787 net['exit_on_floating_ip_error'] = False
788 external_network.append(net)
789
790 if metadata_vpci:
791 metadata = {"pci_assignement": json.dumps(metadata_vpci)}
792 if len(metadata["pci_assignement"]) >255:
793 #limit the metadata size
794 #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
795 self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
796 metadata = {}
797
798 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
799 name, image_id, flavor_id, str(net_list_vim), description, str(metadata))
800
801 security_groups = self.config.get('security_groups')
802 if type(security_groups) is str:
803 security_groups = ( security_groups, )
804 #cloud config
805 userdata=None
806 config_drive = None
807 if isinstance(cloud_config, dict):
808 if cloud_config.get("user-data"):
809 userdata=cloud_config["user-data"]
810 if cloud_config.get("boot-data-drive") != None:
811 config_drive = cloud_config["boot-data-drive"]
812 if cloud_config.get("config-files") or cloud_config.get("users") or cloud_config.get("key-pairs"):
813 if userdata:
814 raise vimconn.vimconnConflictException("Cloud-config cannot contain both 'userdata' and 'config-files'/'users'/'key-pairs'")
815 userdata_dict={}
816 #default user
817 if cloud_config.get("key-pairs"):
818 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
819 userdata_dict["users"] = [{"default": None, "ssh-authorized-keys": cloud_config["key-pairs"] }]
820 if cloud_config.get("users"):
821 if "users" not in userdata_dict:
822 userdata_dict["users"] = [ "default" ]
823 for user in cloud_config["users"]:
824 user_info = {
825 "name" : user["name"],
826 "sudo": "ALL = (ALL)NOPASSWD:ALL"
827 }
828 if "user-info" in user:
829 user_info["gecos"] = user["user-info"]
830 if user.get("key-pairs"):
831 user_info["ssh-authorized-keys"] = user["key-pairs"]
832 userdata_dict["users"].append(user_info)
833
834 if cloud_config.get("config-files"):
835 userdata_dict["write_files"] = []
836 for file in cloud_config["config-files"]:
837 file_info = {
838 "path" : file["dest"],
839 "content": file["content"]
840 }
841 if file.get("encoding"):
842 file_info["encoding"] = file["encoding"]
843 if file.get("permissions"):
844 file_info["permissions"] = file["permissions"]
845 if file.get("owner"):
846 file_info["owner"] = file["owner"]
847 userdata_dict["write_files"].append(file_info)
848 userdata = "#cloud-config\n"
849 userdata += yaml.safe_dump(userdata_dict, indent=4, default_flow_style=False)
850 self.logger.debug("userdata: %s", userdata)
851 elif isinstance(cloud_config, str):
852 userdata = cloud_config
853
854 #Create additional volumes in case these are present in disk_list
855 block_device_mapping = None
856 base_disk_index = ord('b')
857 if disk_list != None:
858 block_device_mapping = dict()
859 for disk in disk_list:
860 if 'image_id' in disk:
861 volume = self.cinder.volumes.create(size = disk['size'],name = name + '_vd' +
862 chr(base_disk_index), imageRef = disk['image_id'])
863 else:
864 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
865 chr(base_disk_index))
866 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
867 base_disk_index += 1
868
869 #wait until volumes are with status available
870 keep_waiting = True
871 elapsed_time = 0
872 while keep_waiting and elapsed_time < volume_timeout:
873 keep_waiting = False
874 for volume_id in block_device_mapping.itervalues():
875 if self.cinder.volumes.get(volume_id).status != 'available':
876 keep_waiting = True
877 if keep_waiting:
878 time.sleep(1)
879 elapsed_time += 1
880
881 #if we exceeded the timeout rollback
882 if elapsed_time >= volume_timeout:
883 #delete the volumes we just created
884 for volume_id in block_device_mapping.itervalues():
885 self.cinder.volumes.delete(volume_id)
886
887 #delete ports we just created
888 for net_item in net_list_vim:
889 if 'port-id' in net_item:
890 self.neutron.delete_port(net_item['port-id'])
891
892 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
893 http_code=vimconn.HTTP_Request_Timeout)
894
895 self.logger.debug("nova.servers.create({}, {}, {}, nics={}, meta={}, security_groups={}," \
896 "availability_zone={}, key_name={}, userdata={}, config_drive={}, " \
897 "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim,
898 metadata, security_groups, self.config.get('availability_zone'),
899 self.config.get('keypair'), userdata, config_drive, block_device_mapping))
900 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim, meta=metadata,
901 security_groups=security_groups,
902 availability_zone=self.config.get('availability_zone'),
903 key_name=self.config.get('keypair'),
904 userdata=userdata,
905 config_drive = config_drive,
906 block_device_mapping = block_device_mapping
907 ) # , description=description)
908 #print "DONE :-)", server
909 pool_id = None
910 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
911 for floating_network in external_network:
912 try:
913 # wait until vm is active
914 elapsed_time = 0
915 while elapsed_time < server_timeout:
916 status = self.nova.servers.get(server.id).status
917 if status == 'ACTIVE':
918 break
919 time.sleep(1)
920 elapsed_time += 1
921
922 #if we exceeded the timeout rollback
923 if elapsed_time >= server_timeout:
924 raise vimconn.vimconnException('Timeout creating instance ' + name,
925 http_code=vimconn.HTTP_Request_Timeout)
926
927 assigned = False
928 while(assigned == False):
929 if floating_ips:
930 ip = floating_ips.pop(0)
931 if not ip.get("port_id", False) and ip.get('tenant_id') == server.tenant_id:
932 free_floating_ip = ip.get("floating_ip_address")
933 try:
934 fix_ip = floating_network.get('ip')
935 server.add_floating_ip(free_floating_ip, fix_ip)
936 assigned = True
937 except Exception as e:
938 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create floating_ip "+ str(e), http_code=vimconn.HTTP_Conflict)
939 else:
940 #Find the external network
941 external_nets = list()
942 for net in self.neutron.list_networks()['networks']:
943 if net['router:external']:
944 external_nets.append(net)
945
946 if len(external_nets) == 0:
947 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
948 "network is present",
949 http_code=vimconn.HTTP_Conflict)
950 if len(external_nets) > 1:
951 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
952 "external networks are present",
953 http_code=vimconn.HTTP_Conflict)
954
955 pool_id = external_nets[0].get('id')
956 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
957 try:
958 #self.logger.debug("Creating floating IP")
959 new_floating_ip = self.neutron.create_floatingip(param)
960 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
961 fix_ip = floating_network.get('ip')
962 server.add_floating_ip(free_floating_ip, fix_ip)
963 assigned=True
964 except Exception as e:
965 raise vimconn.vimconnException(type(e).__name__ + ": Cannot assign floating_ip "+ str(e), http_code=vimconn.HTTP_Conflict)
966 except Exception as e:
967 if not floating_network['exit_on_floating_ip_error']:
968 self.logger.warn("Cannot create floating_ip. %s", str(e))
969 continue
970 self.delete_vminstance(server.id)
971 raise
972
973 return server.id
974 # except nvExceptions.NotFound as e:
975 # error_value=-vimconn.HTTP_Not_Found
976 # error_text= "vm instance %s not found" % vm_id
977 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
978 # delete the volumes we just created
979 if block_device_mapping != None:
980 for volume_id in block_device_mapping.itervalues():
981 self.cinder.volumes.delete(volume_id)
982
983 # delete ports we just created
984 for net_item in net_list_vim:
985 if 'port-id' in net_item:
986 self.neutron.delete_port(net_item['port-id'])
987 self._format_exception(e)
988 except TypeError as e:
989 raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
990
991 def get_vminstance(self,vm_id):
992 '''Returns the VM instance information from VIM'''
993 #self.logger.debug("Getting VM from VIM")
994 try:
995 self._reload_connection()
996 server = self.nova.servers.find(id=vm_id)
997 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
998 return server.to_dict()
999 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
1000 self._format_exception(e)
1001
1002 def get_vminstance_console(self,vm_id, console_type="vnc"):
1003 '''
1004 Get a console for the virtual machine
1005 Params:
1006 vm_id: uuid of the VM
1007 console_type, can be:
1008 "novnc" (by default), "xvpvnc" for VNC types,
1009 "rdp-html5" for RDP types, "spice-html5" for SPICE types
1010 Returns dict with the console parameters:
1011 protocol: ssh, ftp, http, https, ...
1012 server: usually ip address
1013 port: the http, ssh, ... port
1014 suffix: extra text, e.g. the http path and query string
1015 '''
1016 self.logger.debug("Getting VM CONSOLE from VIM")
1017 try:
1018 self._reload_connection()
1019 server = self.nova.servers.find(id=vm_id)
1020 if console_type == None or console_type == "novnc":
1021 console_dict = server.get_vnc_console("novnc")
1022 elif console_type == "xvpvnc":
1023 console_dict = server.get_vnc_console(console_type)
1024 elif console_type == "rdp-html5":
1025 console_dict = server.get_rdp_console(console_type)
1026 elif console_type == "spice-html5":
1027 console_dict = server.get_spice_console(console_type)
1028 else:
1029 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
1030
1031 console_dict1 = console_dict.get("console")
1032 if console_dict1:
1033 console_url = console_dict1.get("url")
1034 if console_url:
1035 #parse console_url
1036 protocol_index = console_url.find("//")
1037 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1038 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1039 if protocol_index < 0 or port_index<0 or suffix_index<0:
1040 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1041 console_dict={"protocol": console_url[0:protocol_index],
1042 "server": console_url[protocol_index+2:port_index],
1043 "port": console_url[port_index:suffix_index],
1044 "suffix": console_url[suffix_index+1:]
1045 }
1046 protocol_index += 2
1047 return console_dict
1048 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
1049
1050 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
1051 self._format_exception(e)
1052
1053 def delete_vminstance(self, vm_id):
1054 '''Removes a VM instance from VIM. Returns the old identifier
1055 '''
1056 #print "osconnector: Getting VM from VIM"
1057 try:
1058 self._reload_connection()
1059 #delete VM ports attached to this networks before the virtual machine
1060 ports = self.neutron.list_ports(device_id=vm_id)
1061 for p in ports['ports']:
1062 try:
1063 self.neutron.delete_port(p["id"])
1064 except Exception as e:
1065 self.logger.error("Error deleting port: " + type(e).__name__ + ": "+ str(e))
1066
1067 #commented because detaching the volumes makes the servers.delete not work properly ?!?
1068 #dettach volumes attached
1069 server = self.nova.servers.get(vm_id)
1070 volumes_attached_dict = server._info['os-extended-volumes:volumes_attached']
1071 #for volume in volumes_attached_dict:
1072 # self.cinder.volumes.detach(volume['id'])
1073
1074 self.nova.servers.delete(vm_id)
1075
1076 #delete volumes.
1077 #Although having detached them should have them in active status
1078 #we ensure in this loop
1079 keep_waiting = True
1080 elapsed_time = 0
1081 while keep_waiting and elapsed_time < volume_timeout:
1082 keep_waiting = False
1083 for volume in volumes_attached_dict:
1084 if self.cinder.volumes.get(volume['id']).status != 'available':
1085 keep_waiting = True
1086 else:
1087 self.cinder.volumes.delete(volume['id'])
1088 if keep_waiting:
1089 time.sleep(1)
1090 elapsed_time += 1
1091
1092 return vm_id
1093 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
1094 self._format_exception(e)
1095 #TODO insert exception vimconn.HTTP_Unauthorized
1096 #if reaching here is because an exception
1097
1098 def refresh_vms_status(self, vm_list):
1099 '''Get the status of the virtual machines and their interfaces/ports
1100 Params: the list of VM identifiers
1101 Returns a dictionary with:
1102 vm_id: #VIM id of this Virtual Machine
1103 status: #Mandatory. Text with one of:
1104 # DELETED (not found at vim)
1105 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1106 # OTHER (Vim reported other status not understood)
1107 # ERROR (VIM indicates an ERROR status)
1108 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1109 # CREATING (on building process), ERROR
1110 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1111 #
1112 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1113 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1114 interfaces:
1115 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1116 mac_address: #Text format XX:XX:XX:XX:XX:XX
1117 vim_net_id: #network id where this interface is connected
1118 vim_interface_id: #interface/port VIM id
1119 ip_address: #null, or text with IPv4, IPv6 address
1120 compute_node: #identification of compute node where PF,VF interface is allocated
1121 pci: #PCI address of the NIC that hosts the PF,VF
1122 vlan: #physical VLAN used for VF
1123 '''
1124 vm_dict={}
1125 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1126 for vm_id in vm_list:
1127 vm={}
1128 try:
1129 vm_vim = self.get_vminstance(vm_id)
1130 if vm_vim['status'] in vmStatus2manoFormat:
1131 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
1132 else:
1133 vm['status'] = "OTHER"
1134 vm['error_msg'] = "VIM status reported " + vm_vim['status']
1135 try:
1136 vm['vim_info'] = yaml.safe_dump(vm_vim, default_flow_style=True, width=256)
1137 except yaml.representer.RepresenterError:
1138 vm['vim_info'] = str(vm_vim)
1139 vm["interfaces"] = []
1140 if vm_vim.get('fault'):
1141 vm['error_msg'] = str(vm_vim['fault'])
1142 #get interfaces
1143 try:
1144 self._reload_connection()
1145 port_dict=self.neutron.list_ports(device_id=vm_id)
1146 for port in port_dict["ports"]:
1147 interface={}
1148 try:
1149 interface['vim_info'] = yaml.safe_dump(port, default_flow_style=True, width=256)
1150 except yaml.representer.RepresenterError:
1151 interface['vim_info'] = str(port)
1152 interface["mac_address"] = port.get("mac_address")
1153 interface["vim_net_id"] = port["network_id"]
1154 interface["vim_interface_id"] = port["id"]
1155 # check if OS-EXT-SRV-ATTR:host is there,
1156 # in case of non-admin credentials, it will be missing
1157 if vm_vim.get('OS-EXT-SRV-ATTR:host'):
1158 interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
1159 interface["pci"] = None
1160
1161 # check if binding:profile is there,
1162 # in case of non-admin credentials, it will be missing
1163 if port.get('binding:profile'):
1164 if port['binding:profile'].get('pci_slot'):
1165 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1166 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1167 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1168 pci = port['binding:profile']['pci_slot']
1169 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1170 interface["pci"] = pci
1171 interface["vlan"] = None
1172 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
1173 network = self.neutron.show_network(port["network_id"])
1174 if network['network'].get('provider:network_type') == 'vlan' and \
1175 port.get("binding:vnic_type") == "direct":
1176 interface["vlan"] = network['network'].get('provider:segmentation_id')
1177 ips=[]
1178 #look for floating ip address
1179 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1180 if floating_ip_dict.get("floatingips"):
1181 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
1182
1183 for subnet in port["fixed_ips"]:
1184 ips.append(subnet["ip_address"])
1185 interface["ip_address"] = ";".join(ips)
1186 vm["interfaces"].append(interface)
1187 except Exception as e:
1188 self.logger.error("Error getting vm interface information " + type(e).__name__ + ": "+ str(e))
1189 except vimconn.vimconnNotFoundException as e:
1190 self.logger.error("Exception getting vm status: %s", str(e))
1191 vm['status'] = "DELETED"
1192 vm['error_msg'] = str(e)
1193 except vimconn.vimconnException as e:
1194 self.logger.error("Exception getting vm status: %s", str(e))
1195 vm['status'] = "VIM_ERROR"
1196 vm['error_msg'] = str(e)
1197 vm_dict[vm_id] = vm
1198 return vm_dict
1199
1200 def action_vminstance(self, vm_id, action_dict):
1201 '''Send and action over a VM instance from VIM
1202 Returns the vm_id if the action was successfully sent to the VIM'''
1203 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
1204 try:
1205 self._reload_connection()
1206 server = self.nova.servers.find(id=vm_id)
1207 if "start" in action_dict:
1208 if action_dict["start"]=="rebuild":
1209 server.rebuild()
1210 else:
1211 if server.status=="PAUSED":
1212 server.unpause()
1213 elif server.status=="SUSPENDED":
1214 server.resume()
1215 elif server.status=="SHUTOFF":
1216 server.start()
1217 elif "pause" in action_dict:
1218 server.pause()
1219 elif "resume" in action_dict:
1220 server.resume()
1221 elif "shutoff" in action_dict or "shutdown" in action_dict:
1222 server.stop()
1223 elif "forceOff" in action_dict:
1224 server.stop() #TODO
1225 elif "terminate" in action_dict:
1226 server.delete()
1227 elif "createImage" in action_dict:
1228 server.create_image()
1229 #"path":path_schema,
1230 #"description":description_schema,
1231 #"name":name_schema,
1232 #"metadata":metadata_schema,
1233 #"imageRef": id_schema,
1234 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1235 elif "rebuild" in action_dict:
1236 server.rebuild(server.image['id'])
1237 elif "reboot" in action_dict:
1238 server.reboot() #reboot_type='SOFT'
1239 elif "console" in action_dict:
1240 console_type = action_dict["console"]
1241 if console_type == None or console_type == "novnc":
1242 console_dict = server.get_vnc_console("novnc")
1243 elif console_type == "xvpvnc":
1244 console_dict = server.get_vnc_console(console_type)
1245 elif console_type == "rdp-html5":
1246 console_dict = server.get_rdp_console(console_type)
1247 elif console_type == "spice-html5":
1248 console_dict = server.get_spice_console(console_type)
1249 else:
1250 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
1251 http_code=vimconn.HTTP_Bad_Request)
1252 try:
1253 console_url = console_dict["console"]["url"]
1254 #parse console_url
1255 protocol_index = console_url.find("//")
1256 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1257 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1258 if protocol_index < 0 or port_index<0 or suffix_index<0:
1259 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
1260 console_dict2={"protocol": console_url[0:protocol_index],
1261 "server": console_url[protocol_index+2 : port_index],
1262 "port": int(console_url[port_index+1 : suffix_index]),
1263 "suffix": console_url[suffix_index+1:]
1264 }
1265 return console_dict2
1266 except Exception as e:
1267 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
1268
1269 return vm_id
1270 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
1271 self._format_exception(e)
1272 #TODO insert exception vimconn.HTTP_Unauthorized
1273
1274 #NOT USED FUNCTIONS
1275
1276 def new_external_port(self, port_data):
1277 #TODO openstack if needed
1278 '''Adds a external port to VIM'''
1279 '''Returns the port identifier'''
1280 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1281
1282 def connect_port_network(self, port_id, network_id, admin=False):
1283 #TODO openstack if needed
1284 '''Connects a external port to a network'''
1285 '''Returns status code of the VIM response'''
1286 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1287
1288 def new_user(self, user_name, user_passwd, tenant_id=None):
1289 '''Adds a new user to openstack VIM'''
1290 '''Returns the user identifier'''
1291 self.logger.debug("osconnector: Adding a new user to VIM")
1292 try:
1293 self._reload_connection()
1294 user=self.keystone.users.create(user_name, user_passwd, tenant_id=tenant_id)
1295 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1296 return user.id
1297 except ksExceptions.ConnectionError as e:
1298 error_value=-vimconn.HTTP_Bad_Request
1299 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1300 except ksExceptions.ClientException as e: #TODO remove
1301 error_value=-vimconn.HTTP_Bad_Request
1302 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1303 #TODO insert exception vimconn.HTTP_Unauthorized
1304 #if reaching here is because an exception
1305 if self.debug:
1306 self.logger.debug("new_user " + error_text)
1307 return error_value, error_text
1308
1309 def delete_user(self, user_id):
1310 '''Delete a user from openstack VIM'''
1311 '''Returns the user identifier'''
1312 if self.debug:
1313 print "osconnector: Deleting a user from VIM"
1314 try:
1315 self._reload_connection()
1316 self.keystone.users.delete(user_id)
1317 return 1, user_id
1318 except ksExceptions.ConnectionError as e:
1319 error_value=-vimconn.HTTP_Bad_Request
1320 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1321 except ksExceptions.NotFound as e:
1322 error_value=-vimconn.HTTP_Not_Found
1323 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1324 except ksExceptions.ClientException as e: #TODO remove
1325 error_value=-vimconn.HTTP_Bad_Request
1326 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1327 #TODO insert exception vimconn.HTTP_Unauthorized
1328 #if reaching here is because an exception
1329 if self.debug:
1330 print "delete_tenant " + error_text
1331 return error_value, error_text
1332
1333 def get_hosts_info(self):
1334 '''Get the information of deployed hosts
1335 Returns the hosts content'''
1336 if self.debug:
1337 print "osconnector: Getting Host info from VIM"
1338 try:
1339 h_list=[]
1340 self._reload_connection()
1341 hypervisors = self.nova.hypervisors.list()
1342 for hype in hypervisors:
1343 h_list.append( hype.to_dict() )
1344 return 1, {"hosts":h_list}
1345 except nvExceptions.NotFound as e:
1346 error_value=-vimconn.HTTP_Not_Found
1347 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1348 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1349 error_value=-vimconn.HTTP_Bad_Request
1350 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1351 #TODO insert exception vimconn.HTTP_Unauthorized
1352 #if reaching here is because an exception
1353 if self.debug:
1354 print "get_hosts_info " + error_text
1355 return error_value, error_text
1356
1357 def get_hosts(self, vim_tenant):
1358 '''Get the hosts and deployed instances
1359 Returns the hosts content'''
1360 r, hype_dict = self.get_hosts_info()
1361 if r<0:
1362 return r, hype_dict
1363 hypervisors = hype_dict["hosts"]
1364 try:
1365 servers = self.nova.servers.list()
1366 for hype in hypervisors:
1367 for server in servers:
1368 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1369 if 'vm' in hype:
1370 hype['vm'].append(server.id)
1371 else:
1372 hype['vm'] = [server.id]
1373 return 1, hype_dict
1374 except nvExceptions.NotFound as e:
1375 error_value=-vimconn.HTTP_Not_Found
1376 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1377 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1378 error_value=-vimconn.HTTP_Bad_Request
1379 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1380 #TODO insert exception vimconn.HTTP_Unauthorized
1381 #if reaching here is because an exception
1382 if self.debug:
1383 print "get_hosts " + error_text
1384 return error_value, error_text
1385
1386