1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 osconnector implements all the methods to interact with openstack using the python-client.
27 __author__
="Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research"
28 __date__
="$22-jun-2014 11:19:29$"
39 from novaclient
import client
as nClient
, exceptions
as nvExceptions
40 from keystoneauth1
.identity
import v2
, v3
41 from keystoneauth1
import session
42 import keystoneclient
.exceptions
as ksExceptions
43 import keystoneclient
.v3
.client
as ksClient_v3
44 import keystoneclient
.v2_0
.client
as ksClient_v2
45 from glanceclient
import client
as glClient
46 import glanceclient
.client
as gl1Client
47 import glanceclient
.exc
as gl1Exceptions
48 from cinderclient
import client
as cClient
49 from httplib
import HTTPException
50 from neutronclient
.neutron
import client
as neClient
51 from neutronclient
.common
import exceptions
as neExceptions
52 from requests
.exceptions
import ConnectionError
54 '''contain the openstack virtual machine status to openmano status'''
55 vmStatus2manoFormat
={'ACTIVE':'ACTIVE',
57 'SUSPENDED': 'SUSPENDED',
60 'ERROR':'ERROR','DELETED':'DELETED'
62 netStatus2manoFormat
={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
65 #global var to have a timeout creating and deleting volumes
69 class vimconnector(vimconn
.vimconnector
):
70 def __init__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
=None, user
=None, passwd
=None,
71 log_level
=None, config
={}, persistent_info
={}):
72 '''using common constructor parameters. In this case
73 'url' is the keystone authorization url,
74 'url_admin' is not use
76 api_version
= config
.get('APIversion')
77 if api_version
and api_version
not in ('v3.3', 'v2.0', '2', '3'):
78 raise vimconn
.vimconnException("Invalid value '{}' for config:APIversion. "
79 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version
))
80 vimconn
.vimconnector
.__init
__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
, user
, passwd
, log_level
,
83 self
.insecure
= self
.config
.get("insecure", False)
85 raise TypeError, 'url param can not be NoneType'
86 self
.persistent_info
= persistent_info
87 self
.session
= persistent_info
.get('session', {'reload_client': True})
88 self
.nova
= self
.session
.get('nova')
89 self
.neutron
= self
.session
.get('neutron')
90 self
.cinder
= self
.session
.get('cinder')
91 self
.glance
= self
.session
.get('glance')
92 self
.glancev1
= self
.session
.get('glancev1')
93 self
.keystone
= self
.session
.get('keystone')
94 self
.api_version3
= self
.session
.get('api_version3')
96 self
.logger
= logging
.getLogger('openmano.vim.openstack')
98 self
.logger
.setLevel( getattr(logging
, log_level
) )
100 def __getitem__(self
, index
):
101 """Get individuals parameters.
103 if index
== 'project_domain_id':
104 return self
.config
.get("project_domain_id")
105 elif index
== 'user_domain_id':
106 return self
.config
.get("user_domain_id")
108 return vimconn
.vimconnector
.__getitem
__(self
, index
)
110 def __setitem__(self
, index
, value
):
111 """Set individuals parameters and it is marked as dirty so to force connection reload.
113 if index
== 'project_domain_id':
114 self
.config
["project_domain_id"] = value
115 elif index
== 'user_domain_id':
116 self
.config
["user_domain_id"] = value
118 vimconn
.vimconnector
.__setitem
__(self
, index
, value
)
119 self
.session
['reload_client'] = True
121 def _reload_connection(self
):
122 '''Called before any operation, it check if credentials has changed
123 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
125 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
126 if self
.session
['reload_client']:
127 if self
.config
.get('APIversion'):
128 self
.api_version3
= self
.config
['APIversion'] == 'v3.3' or self
.config
['APIversion'] == '3'
129 else: # get from ending auth_url that end with v3 or with v2.0
130 self
.api_version3
= self
.url
.split("/")[-1] == "v3"
131 self
.session
['api_version3'] = self
.api_version3
132 if self
.api_version3
:
133 auth
= v3
.Password(auth_url
=self
.url
,
135 password
=self
.passwd
,
136 project_name
=self
.tenant_name
,
137 project_id
=self
.tenant_id
,
138 project_domain_id
=self
.config
.get('project_domain_id', 'default'),
139 user_domain_id
=self
.config
.get('user_domain_id', 'default'))
141 auth
= v2
.Password(auth_url
=self
.url
,
143 password
=self
.passwd
,
144 tenant_name
=self
.tenant_name
,
145 tenant_id
=self
.tenant_id
)
146 sess
= session
.Session(auth
=auth
, verify
=not self
.insecure
)
147 if self
.api_version3
:
148 self
.keystone
= ksClient_v3
.Client(session
=sess
)
150 self
.keystone
= ksClient_v2
.Client(session
=sess
)
151 self
.session
['keystone'] = self
.keystone
152 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
153 # This implementation approach is due to the warning message in
154 # https://developer.openstack.org/api-guide/compute/microversions.html
155 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
156 # always require an specific microversion.
157 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
158 version
= self
.config
.get("microversion")
161 self
.nova
= self
.session
['nova'] = nClient
.Client(str(version
), session
=sess
)
162 self
.neutron
= self
.session
['neutron'] = neClient
.Client('2.0', session
=sess
)
163 self
.cinder
= self
.session
['cinder'] = cClient
.Client(2, session
=sess
)
164 self
.glance
= self
.session
['glance'] = glClient
.Client(2, session
=sess
)
165 self
.glancev1
= self
.session
['glancev1'] = glClient
.Client('1', session
=sess
)
166 self
.session
['reload_client'] = False
167 self
.persistent_info
['session'] = self
.session
169 def __net_os2mano(self
, net_list_dict
):
170 '''Transform the net openstack format to mano format
171 net_list_dict can be a list of dict or a single dict'''
172 if type(net_list_dict
) is dict:
173 net_list_
=(net_list_dict
,)
174 elif type(net_list_dict
) is list:
175 net_list_
=net_list_dict
177 raise TypeError("param net_list_dict must be a list or a dictionary")
178 for net
in net_list_
:
179 if net
.get('provider:network_type') == "vlan":
184 def _format_exception(self
, exception
):
185 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
186 if isinstance(exception
, (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
,
187 ConnectionError
, ksExceptions
.ConnectionError
, neExceptions
.ConnectionFailed
189 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
190 elif isinstance(exception
, (nvExceptions
.ClientException
, ksExceptions
.ClientException
,
191 neExceptions
.NeutronException
, nvExceptions
.BadRequest
)):
192 raise vimconn
.vimconnUnexpectedResponse(type(exception
).__name
__ + ": " + str(exception
))
193 elif isinstance(exception
, (neExceptions
.NetworkNotFoundClient
, nvExceptions
.NotFound
)):
194 raise vimconn
.vimconnNotFoundException(type(exception
).__name
__ + ": " + str(exception
))
195 elif isinstance(exception
, nvExceptions
.Conflict
):
196 raise vimconn
.vimconnConflictException(type(exception
).__name
__ + ": " + str(exception
))
197 elif isinstance(exception
, vimconn
.vimconnException
):
200 self
.logger
.error("General Exception " + str(exception
), exc_info
=True)
201 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
203 def get_tenant_list(self
, filter_dict
={}):
204 '''Obtain tenants of VIM
205 filter_dict can contain the following keys:
206 name: filter by tenant name
207 id: filter by tenant uuid/id
209 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
211 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
213 self
._reload
_connection
()
214 if self
.api_version3
:
215 project_class_list
= self
.keystone
.projects
.list(name
=filter_dict
.get("name"))
217 project_class_list
= self
.keystone
.tenants
.findall(**filter_dict
)
219 for project
in project_class_list
:
220 if filter_dict
.get('id') and filter_dict
["id"] != project
.id:
222 project_list
.append(project
.to_dict())
224 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
225 self
._format
_exception
(e
)
227 def new_tenant(self
, tenant_name
, tenant_description
):
228 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
229 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
231 self
._reload
_connection
()
232 if self
.api_version3
:
233 project
= self
.keystone
.projects
.create(tenant_name
, self
.config
.get("project_domain_id", "default"),
234 description
=tenant_description
, is_domain
=False)
236 project
= self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
238 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
239 self
._format
_exception
(e
)
241 def delete_tenant(self
, tenant_id
):
242 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
243 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
245 self
._reload
_connection
()
246 if self
.api_version3
:
247 self
.keystone
.projects
.delete(tenant_id
)
249 self
.keystone
.tenants
.delete(tenant_id
)
251 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
252 self
._format
_exception
(e
)
254 def new_network(self
,net_name
, net_type
, ip_profile
=None, shared
=False, vlan
=None):
255 '''Adds a tenant network to VIM. Returns the network identifier'''
256 self
.logger
.debug("Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
)
257 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
260 self
._reload
_connection
()
261 network_dict
= {'name': net_name
, 'admin_state_up': True}
262 if net_type
=="data" or net_type
=="ptp":
263 if self
.config
.get('dataplane_physical_net') == None:
264 raise vimconn
.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
265 network_dict
["provider:physical_network"] = self
.config
['dataplane_physical_net'] #"physnet_sriov" #TODO physical
266 network_dict
["provider:network_type"] = "vlan"
268 network_dict
["provider:network_type"] = vlan
269 network_dict
["shared"]=shared
270 new_net
=self
.neutron
.create_network({'network':network_dict
})
272 #create subnetwork, even if there is no profile
275 if 'subnet_address' not in ip_profile
:
276 #Fake subnet is required
277 subnet_rand
= random
.randint(0, 255)
278 ip_profile
['subnet_address'] = "192.168.{}.0/24".format(subnet_rand
)
279 if 'ip_version' not in ip_profile
:
280 ip_profile
['ip_version'] = "IPv4"
281 subnet
= {"name":net_name
+"-subnet",
282 "network_id": new_net
["network"]["id"],
283 "ip_version": 4 if ip_profile
['ip_version']=="IPv4" else 6,
284 "cidr": ip_profile
['subnet_address']
286 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
287 subnet
['gateway_ip'] = ip_profile
.get('gateway_address')
288 if ip_profile
.get('dns_address'):
289 subnet
['dns_nameservers'] = ip_profile
['dns_address'].split(";")
290 if 'dhcp_enabled' in ip_profile
:
291 subnet
['enable_dhcp'] = False if ip_profile
['dhcp_enabled']=="false" else True
292 if 'dhcp_start_address' in ip_profile
:
293 subnet
['allocation_pools'] = []
294 subnet
['allocation_pools'].append(dict())
295 subnet
['allocation_pools'][0]['start'] = ip_profile
['dhcp_start_address']
296 if 'dhcp_count' in ip_profile
:
297 #parts = ip_profile['dhcp_start_address'].split('.')
298 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
299 ip_int
= int(netaddr
.IPAddress(ip_profile
['dhcp_start_address']))
300 ip_int
+= ip_profile
['dhcp_count'] - 1
301 ip_str
= str(netaddr
.IPAddress(ip_int
))
302 subnet
['allocation_pools'][0]['end'] = ip_str
303 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
304 self
.neutron
.create_subnet({"subnet": subnet
} )
305 return new_net
["network"]["id"]
306 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
308 self
.neutron
.delete_network(new_net
['network']['id'])
309 self
._format
_exception
(e
)
311 def get_network_list(self
, filter_dict
={}):
312 '''Obtain tenant networks of VIM
318 admin_state_up: boolean
320 Returns the network list of dictionaries
322 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
324 self
._reload
_connection
()
325 if self
.api_version3
and "tenant_id" in filter_dict
:
326 filter_dict
['project_id'] = filter_dict
.pop('tenant_id') #TODO check
327 net_dict
=self
.neutron
.list_networks(**filter_dict
)
328 net_list
=net_dict
["networks"]
329 self
.__net
_os
2mano
(net_list
)
331 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
332 self
._format
_exception
(e
)
334 def get_network(self
, net_id
):
335 '''Obtain details of network from VIM
336 Returns the network information from a network id'''
337 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
338 filter_dict
={"id": net_id
}
339 net_list
= self
.get_network_list(filter_dict
)
341 raise vimconn
.vimconnNotFoundException("Network '{}' not found".format(net_id
))
342 elif len(net_list
)>1:
343 raise vimconn
.vimconnConflictException("Found more than one network with this criteria")
346 for subnet_id
in net
.get("subnets", () ):
348 subnet
= self
.neutron
.show_subnet(subnet_id
)
349 except Exception as e
:
350 self
.logger
.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id
, str(e
)))
351 subnet
= {"id": subnet_id
, "fault": str(e
)}
352 subnets
.append(subnet
)
353 net
["subnets"] = subnets
354 net
["encapsulation"] = net
.get('provider:network_type')
355 net
["segmentation_id"] = net
.get('provider:segmentation_id')
358 def delete_network(self
, net_id
):
359 '''Deletes a tenant network from VIM. Returns the old network identifier'''
360 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
362 self
._reload
_connection
()
363 #delete VM ports attached to this networks before the network
364 ports
= self
.neutron
.list_ports(network_id
=net_id
)
365 for p
in ports
['ports']:
367 self
.neutron
.delete_port(p
["id"])
368 except Exception as e
:
369 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
370 self
.neutron
.delete_network(net_id
)
372 except (neExceptions
.ConnectionFailed
, neExceptions
.NetworkNotFoundClient
, neExceptions
.NeutronException
,
373 ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
374 self
._format
_exception
(e
)
376 def refresh_nets_status(self
, net_list
):
377 '''Get the status of the networks
378 Params: the list of network identifiers
379 Returns a dictionary with:
380 net_id: #VIM id of this network
381 status: #Mandatory. Text with one of:
382 # DELETED (not found at vim)
383 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
384 # OTHER (Vim reported other status not understood)
385 # ERROR (VIM indicates an ERROR status)
386 # ACTIVE, INACTIVE, DOWN (admin down),
387 # BUILD (on building process)
389 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
390 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
394 for net_id
in net_list
:
397 net_vim
= self
.get_network(net_id
)
398 if net_vim
['status'] in netStatus2manoFormat
:
399 net
["status"] = netStatus2manoFormat
[ net_vim
['status'] ]
401 net
["status"] = "OTHER"
402 net
["error_msg"] = "VIM status reported " + net_vim
['status']
404 if net
['status'] == "ACTIVE" and not net_vim
['admin_state_up']:
405 net
['status'] = 'DOWN'
407 net
['vim_info'] = yaml
.safe_dump(net_vim
, default_flow_style
=True, width
=256)
408 except yaml
.representer
.RepresenterError
:
409 net
['vim_info'] = str(net_vim
)
410 if net_vim
.get('fault'): #TODO
411 net
['error_msg'] = str(net_vim
['fault'])
412 except vimconn
.vimconnNotFoundException
as e
:
413 self
.logger
.error("Exception getting net status: %s", str(e
))
414 net
['status'] = "DELETED"
415 net
['error_msg'] = str(e
)
416 except vimconn
.vimconnException
as e
:
417 self
.logger
.error("Exception getting net status: %s", str(e
))
418 net
['status'] = "VIM_ERROR"
419 net
['error_msg'] = str(e
)
420 net_dict
[net_id
] = net
423 def get_flavor(self
, flavor_id
):
424 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
425 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
427 self
._reload
_connection
()
428 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
429 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
430 return flavor
.to_dict()
431 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
432 self
._format
_exception
(e
)
434 def get_flavor_id_from_data(self
, flavor_dict
):
435 """Obtain flavor id that match the flavor description
436 Returns the flavor_id or raises a vimconnNotFoundException
437 flavor_dict: contains the required ram, vcpus, disk
438 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
439 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
440 vimconnNotFoundException is raised
442 exact_match
= False if self
.config
.get('use_existing_flavors') else True
444 self
._reload
_connection
()
445 flavor_candidate_id
= None
446 flavor_candidate_data
= (10000, 10000, 10000)
447 flavor_target
= (flavor_dict
["ram"], flavor_dict
["vcpus"], flavor_dict
["disk"])
449 numas
= flavor_dict
.get("extended", {}).get("numas")
452 raise vimconn
.vimconnNotFoundException("Flavor with EPA still not implemted")
454 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
456 # numas = extended.get("numas")
457 for flavor
in self
.nova
.flavors
.list():
458 epa
= flavor
.get_keys()
462 flavor_data
= (flavor
.ram
, flavor
.vcpus
, flavor
.disk
)
463 if flavor_data
== flavor_target
:
465 elif not exact_match
and flavor_target
< flavor_data
< flavor_candidate_data
:
466 flavor_candidate_id
= flavor
.id
467 flavor_candidate_data
= flavor_data
468 if not exact_match
and flavor_candidate_id
:
469 return flavor_candidate_id
470 raise vimconn
.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict
)))
471 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
472 self
._format
_exception
(e
)
475 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
476 '''Adds a tenant flavor to openstack VIM
477 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
478 Returns the flavor identifier
480 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
484 name
=flavor_data
['name']
485 while retry
<max_retries
:
488 self
._reload
_connection
()
489 if change_name_if_used
:
492 fl
=self
.nova
.flavors
.list()
494 fl_names
.append(f
.name
)
495 while name
in fl_names
:
497 name
= flavor_data
['name']+"-" + str(name_suffix
)
499 ram
= flavor_data
.get('ram',64)
500 vcpus
= flavor_data
.get('vcpus',1)
503 extended
= flavor_data
.get("extended")
505 numas
=extended
.get("numas")
507 numa_nodes
= len(numas
)
509 return -1, "Can not add flavor with more than one numa"
510 numa_properties
= {"hw:numa_nodes":str(numa_nodes
)}
511 numa_properties
["hw:mem_page_size"] = "large"
512 numa_properties
["hw:cpu_policy"] = "dedicated"
513 numa_properties
["hw:numa_mempolicy"] = "strict"
515 #overwrite ram and vcpus
516 ram
= numa
['memory']*1024
517 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
518 if 'paired-threads' in numa
:
519 vcpus
= numa
['paired-threads']*2
520 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
521 numa_properties
["hw:cpu_thread_policy"] = "require"
522 numa_properties
["hw:cpu_policy"] = "dedicated"
523 elif 'cores' in numa
:
524 vcpus
= numa
['cores']
525 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
526 numa_properties
["hw:cpu_thread_policy"] = "isolate"
527 numa_properties
["hw:cpu_policy"] = "dedicated"
528 elif 'threads' in numa
:
529 vcpus
= numa
['threads']
530 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
531 numa_properties
["hw:cpu_thread_policy"] = "prefer"
532 numa_properties
["hw:cpu_policy"] = "dedicated"
533 # for interface in numa.get("interfaces",() ):
534 # if interface["dedicated"]=="yes":
535 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
536 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
539 new_flavor
=self
.nova
.flavors
.create(name
,
542 flavor_data
.get('disk',1),
543 is_public
=flavor_data
.get('is_public', True)
547 new_flavor
.set_keys(numa_properties
)
549 except nvExceptions
.Conflict
as e
:
550 if change_name_if_used
and retry
< max_retries
:
552 self
._format
_exception
(e
)
553 #except nvExceptions.BadRequest as e:
554 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
555 self
._format
_exception
(e
)
557 def delete_flavor(self
,flavor_id
):
558 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
561 self
._reload
_connection
()
562 self
.nova
.flavors
.delete(flavor_id
)
564 #except nvExceptions.BadRequest as e:
565 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
566 self
._format
_exception
(e
)
568 def new_image(self
,image_dict
):
570 Adds a tenant image to VIM. imge_dict is a dictionary with:
572 disk_format: qcow2, vhd, vmdk, raw (by default), ...
573 location: path or URI
574 public: "yes" or "no"
575 metadata: metadata of the image
580 while retry
<max_retries
:
583 self
._reload
_connection
()
584 #determine format http://docs.openstack.org/developer/glance/formats.html
585 if "disk_format" in image_dict
:
586 disk_format
=image_dict
["disk_format"]
587 else: #autodiscover based on extension
588 if image_dict
['location'][-6:]==".qcow2":
590 elif image_dict
['location'][-4:]==".vhd":
592 elif image_dict
['location'][-5:]==".vmdk":
594 elif image_dict
['location'][-4:]==".vdi":
596 elif image_dict
['location'][-4:]==".iso":
598 elif image_dict
['location'][-4:]==".aki":
600 elif image_dict
['location'][-4:]==".ari":
602 elif image_dict
['location'][-4:]==".ami":
606 self
.logger
.debug("new_image: '%s' loading from '%s'", image_dict
['name'], image_dict
['location'])
607 if image_dict
['location'][0:4]=="http":
608 new_image
= self
.glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
609 container_format
="bare", location
=image_dict
['location'], disk_format
=disk_format
)
611 with
open(image_dict
['location']) as fimage
:
612 new_image
= self
.glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
613 container_format
="bare", data
=fimage
, disk_format
=disk_format
)
614 #insert metadata. We cannot use 'new_image.properties.setdefault'
615 #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
616 new_image_nova
=self
.nova
.images
.find(id=new_image
.id)
617 new_image_nova
.metadata
.setdefault('location',image_dict
['location'])
618 metadata_to_load
= image_dict
.get('metadata')
620 for k
,v
in yaml
.load(metadata_to_load
).iteritems():
621 new_image_nova
.metadata
.setdefault(k
,v
)
623 except (nvExceptions
.Conflict
, ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
624 self
._format
_exception
(e
)
625 except (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
626 if retry
==max_retries
:
628 self
._format
_exception
(e
)
629 except IOError as e
: #can not open the file
630 raise vimconn
.vimconnConnectionException(type(e
).__name
__ + ": " + str(e
)+ " for " + image_dict
['location'],
631 http_code
=vimconn
.HTTP_Bad_Request
)
633 def delete_image(self
, image_id
):
634 '''Deletes a tenant image from openstack VIM. Returns the old id
637 self
._reload
_connection
()
638 self
.nova
.images
.delete(image_id
)
640 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
: #TODO remove
641 self
._format
_exception
(e
)
643 def get_image_id_from_path(self
, path
):
644 '''Get the image id from image path in the VIM database. Returns the image_id'''
646 self
._reload
_connection
()
647 images
= self
.nova
.images
.list()
649 if image
.metadata
.get("location")==path
:
651 raise vimconn
.vimconnNotFoundException("image with location '{}' not found".format( path
))
652 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
653 self
._format
_exception
(e
)
655 def get_image_list(self
, filter_dict
={}):
656 '''Obtain tenant images from VIM
660 checksum: image checksum
661 Returns the image list of dictionaries:
662 [{<the fields at Filter_dict plus some VIM specific>}, ...]
665 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
667 self
._reload
_connection
()
668 filter_dict_os
=filter_dict
.copy()
669 #First we filter by the available filter fields: name, id. The others are removed.
670 filter_dict_os
.pop('checksum',None)
671 image_list
=self
.nova
.images
.findall(**filter_dict_os
)
672 if len(image_list
)==0:
674 #Then we filter by the rest of filter fields: checksum
676 for image
in image_list
:
677 image_class
=self
.glance
.images
.get(image
.id)
678 if 'checksum' not in filter_dict
or image_class
['checksum']==filter_dict
.get('checksum'):
679 filtered_list
.append(image_class
.copy())
681 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
682 self
._format
_exception
(e
)
684 def __wait_for_vm(self
, vm_id
, status
):
685 """wait until vm is in the desired status and return True.
686 If the VM gets in ERROR status, return false.
687 If the timeout is reached generate an exception"""
689 while elapsed_time
< server_timeout
:
690 vm_status
= self
.nova
.servers
.get(vm_id
).status
691 if vm_status
== status
:
693 if vm_status
== 'ERROR':
698 # if we exceeded the timeout rollback
699 if elapsed_time
>= server_timeout
:
700 raise vimconn
.vimconnException('Timeout waiting for instance ' + vm_id
+ ' to get ' + status
,
701 http_code
=vimconn
.HTTP_Request_Timeout
)
703 def new_vminstance(self
,name
,description
,start
,image_id
,flavor_id
,net_list
,cloud_config
=None,disk_list
=None):
704 '''Adds a VM instance to VIM
706 start: indicates if VM must start or boot in pause mode. Ignored
707 image_id,flavor_id: iamge and flavor uuid
708 net_list: list of interfaces, each one is a dictionary with:
710 net_id: network uuid to connect
711 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
712 model: interface model, ignored #TODO
713 mac_address: used for SR-IOV ifaces #TODO for other types
714 use: 'data', 'bridge', 'mgmt'
715 type: 'virtual', 'PF', 'VF', 'VFnotShared'
716 vim_id: filled/added by this function
717 floating_ip: True/False (or it can be None)
718 #TODO ip, security groups
719 Returns the instance identifier
721 self
.logger
.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id
, flavor_id
,str(net_list
))
726 external_network
=[] # list of external networks to be connected to instance, later on used to create floating_ip
727 no_secured_ports
= [] # List of port-is with port-security disabled
728 self
._reload
_connection
()
729 metadata_vpci
={} # For a specific neutron plugin
730 block_device_mapping
= None
732 if not net
.get("net_id"): #skip non connected iface
736 "network_id": net
["net_id"],
737 "name": net
.get("name"),
738 "admin_state_up": True
740 if net
["type"]=="virtual":
742 metadata_vpci
[ net
["net_id"] ] = [[ net
["vpci"], "" ]]
743 elif net
["type"]=="VF": # for VF
745 if "VF" not in metadata_vpci
:
746 metadata_vpci
["VF"]=[]
747 metadata_vpci
["VF"].append([ net
["vpci"], "" ])
748 port_dict
["binding:vnic_type"]="direct"
751 if "PF" not in metadata_vpci
:
752 metadata_vpci
["PF"]=[]
753 metadata_vpci
["PF"].append([ net
["vpci"], "" ])
754 port_dict
["binding:vnic_type"]="direct-physical"
755 if not port_dict
["name"]:
756 port_dict
["name"]=name
757 if net
.get("mac_address"):
758 port_dict
["mac_address"]=net
["mac_address"]
759 new_port
= self
.neutron
.create_port({"port": port_dict
})
760 net
["mac_adress"] = new_port
["port"]["mac_address"]
761 net
["vim_id"] = new_port
["port"]["id"]
762 # if try to use a network without subnetwork, it will return a emtpy list
763 fixed_ips
= new_port
["port"].get("fixed_ips")
765 net
["ip"] = fixed_ips
[0].get("ip_address")
769 port
= {"port-id": new_port
["port"]["id"]}
770 if float(self
.nova
.api_version
.get_string()) >= 2.32:
771 port
["tag"] = new_port
["port"]["name"]
772 net_list_vim
.append(port
)
774 if net
.get('floating_ip', False):
775 net
['exit_on_floating_ip_error'] = True
776 external_network
.append(net
)
777 elif net
['use'] == 'mgmt' and self
.config
.get('use_floating_ip'):
778 net
['exit_on_floating_ip_error'] = False
779 external_network
.append(net
)
781 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
782 # As a workaround we wait until the VM is active and then disable the port-security
783 if net
.get("port_security") == False:
784 no_secured_ports
.append(new_port
["port"]["id"])
787 metadata
= {"pci_assignement": json
.dumps(metadata_vpci
)}
788 if len(metadata
["pci_assignement"]) >255:
789 #limit the metadata size
790 #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
791 self
.logger
.warn("Metadata deleted since it exceeds the expected length (255) ")
794 self
.logger
.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
795 name
, image_id
, flavor_id
, str(net_list_vim
), description
, str(metadata
))
797 security_groups
= self
.config
.get('security_groups')
798 if type(security_groups
) is str:
799 security_groups
= ( security_groups
, )
803 if isinstance(cloud_config
, dict):
804 if cloud_config
.get("user-data"):
805 userdata
=cloud_config
["user-data"]
806 if cloud_config
.get("boot-data-drive") != None:
807 config_drive
= cloud_config
["boot-data-drive"]
808 if cloud_config
.get("config-files") or cloud_config
.get("users") or cloud_config
.get("key-pairs"):
810 raise vimconn
.vimconnConflictException("Cloud-config cannot contain both 'userdata' and 'config-files'/'users'/'key-pairs'")
813 if cloud_config
.get("key-pairs"):
814 userdata_dict
["ssh-authorized-keys"] = cloud_config
["key-pairs"]
815 userdata_dict
["users"] = [{"default": None, "ssh-authorized-keys": cloud_config
["key-pairs"] }]
816 if cloud_config
.get("users"):
817 if "users" not in userdata_dict
:
818 userdata_dict
["users"] = [ "default" ]
819 for user
in cloud_config
["users"]:
821 "name" : user
["name"],
822 "sudo": "ALL = (ALL)NOPASSWD:ALL"
824 if "user-info" in user
:
825 user_info
["gecos"] = user
["user-info"]
826 if user
.get("key-pairs"):
827 user_info
["ssh-authorized-keys"] = user
["key-pairs"]
828 userdata_dict
["users"].append(user_info
)
830 if cloud_config
.get("config-files"):
831 userdata_dict
["write_files"] = []
832 for file in cloud_config
["config-files"]:
834 "path" : file["dest"],
835 "content": file["content"]
837 if file.get("encoding"):
838 file_info
["encoding"] = file["encoding"]
839 if file.get("permissions"):
840 file_info
["permissions"] = file["permissions"]
841 if file.get("owner"):
842 file_info
["owner"] = file["owner"]
843 userdata_dict
["write_files"].append(file_info
)
844 userdata
= "#cloud-config\n"
845 userdata
+= yaml
.safe_dump(userdata_dict
, indent
=4, default_flow_style
=False)
846 self
.logger
.debug("userdata: %s", userdata
)
847 elif isinstance(cloud_config
, str):
848 userdata
= cloud_config
850 #Create additional volumes in case these are present in disk_list
851 base_disk_index
= ord('b')
852 if disk_list
!= None:
853 block_device_mapping
= {}
854 for disk
in disk_list
:
855 if 'image_id' in disk
:
856 volume
= self
.cinder
.volumes
.create(size
= disk
['size'],name
= name
+ '_vd' +
857 chr(base_disk_index
), imageRef
= disk
['image_id'])
859 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
860 chr(base_disk_index
))
861 block_device_mapping
['_vd' + chr(base_disk_index
)] = volume
.id
864 #wait until volumes are with status available
867 while keep_waiting
and elapsed_time
< volume_timeout
:
869 for volume_id
in block_device_mapping
.itervalues():
870 if self
.cinder
.volumes
.get(volume_id
).status
!= 'available':
876 #if we exceeded the timeout rollback
877 if elapsed_time
>= volume_timeout
:
878 #delete the volumes we just created
879 for volume_id
in block_device_mapping
.itervalues():
880 self
.cinder
.volumes
.delete(volume_id
)
882 #delete ports we just created
883 for net_item
in net_list_vim
:
884 if 'port-id' in net_item
:
885 self
.neutron
.delete_port(net_item
['port-id'])
887 raise vimconn
.vimconnException('Timeout creating volumes for instance ' + name
,
888 http_code
=vimconn
.HTTP_Request_Timeout
)
890 self
.logger
.debug("nova.servers.create({}, {}, {}, nics={}, meta={}, security_groups={}," \
891 "availability_zone={}, key_name={}, userdata={}, config_drive={}, " \
892 "block_device_mapping={})".format(name
, image_id
, flavor_id
, net_list_vim
,
893 metadata
, security_groups
, self
.config
.get('availability_zone'),
894 self
.config
.get('keypair'), userdata
, config_drive
, block_device_mapping
))
895 server
= self
.nova
.servers
.create(name
, image_id
, flavor_id
, nics
=net_list_vim
, meta
=metadata
,
896 security_groups
=security_groups
,
897 availability_zone
=self
.config
.get('availability_zone'),
898 key_name
=self
.config
.get('keypair'),
900 config_drive
=config_drive
,
901 block_device_mapping
=block_device_mapping
902 ) # , description=description)
904 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
906 self
.__wait
_for
_vm
(server
.id, 'ACTIVE')
908 for port_id
in no_secured_ports
:
910 self
.neutron
.update_port(port_id
, {"port": {"port_security_enabled": False, "security_groups": None} })
912 except Exception as e
:
913 self
.logger
.error("It was not possible to disable port security for port {}".format(port_id
))
914 self
.delete_vminstance(server
.id)
917 #print "DONE :-)", server
919 floating_ips
= self
.neutron
.list_floatingips().get("floatingips", ())
922 self
.__wait
_for
_vm
(server
.id, 'ACTIVE')
924 for floating_network
in external_network
:
927 while(assigned
== False):
929 ip
= floating_ips
.pop(0)
930 if not ip
.get("port_id", False) and ip
.get('tenant_id') == server
.tenant_id
:
931 free_floating_ip
= ip
.get("floating_ip_address")
933 fix_ip
= floating_network
.get('ip')
934 server
.add_floating_ip(free_floating_ip
, fix_ip
)
936 except Exception as e
:
937 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot create floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
939 #Find the external network
940 external_nets
= list()
941 for net
in self
.neutron
.list_networks()['networks']:
942 if net
['router:external']:
943 external_nets
.append(net
)
945 if len(external_nets
) == 0:
946 raise vimconn
.vimconnException("Cannot create floating_ip automatically since no external "
947 "network is present",
948 http_code
=vimconn
.HTTP_Conflict
)
949 if len(external_nets
) > 1:
950 raise vimconn
.vimconnException("Cannot create floating_ip automatically since multiple "
951 "external networks are present",
952 http_code
=vimconn
.HTTP_Conflict
)
954 pool_id
= external_nets
[0].get('id')
955 param
= {'floatingip': {'floating_network_id': pool_id
, 'tenant_id': server
.tenant_id
}}
957 #self.logger.debug("Creating floating IP")
958 new_floating_ip
= self
.neutron
.create_floatingip(param
)
959 free_floating_ip
= new_floating_ip
['floatingip']['floating_ip_address']
960 fix_ip
= floating_network
.get('ip')
961 server
.add_floating_ip(free_floating_ip
, fix_ip
)
963 except Exception as e
:
964 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot assign floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
965 except Exception as e
:
966 if not floating_network
['exit_on_floating_ip_error']:
967 self
.logger
.warn("Cannot create floating_ip. %s", str(e
))
972 # except nvExceptions.NotFound as e:
973 # error_value=-vimconn.HTTP_Not_Found
974 # error_text= "vm instance %s not found" % vm_id
975 # except TypeError as e:
976 # raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
978 except Exception as e
:
979 # delete the volumes we just created
980 if block_device_mapping
:
981 for volume_id
in block_device_mapping
.itervalues():
982 self
.cinder
.volumes
.delete(volume_id
)
986 self
.delete_vminstance(server
.id)
988 # delete ports we just created
989 for net_item
in net_list_vim
:
990 if 'port-id' in net_item
:
991 self
.neutron
.delete_port(net_item
['port-id'])
993 self
._format
_exception
(e
)
995 def get_vminstance(self
,vm_id
):
996 '''Returns the VM instance information from VIM'''
997 #self.logger.debug("Getting VM from VIM")
999 self
._reload
_connection
()
1000 server
= self
.nova
.servers
.find(id=vm_id
)
1001 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
1002 return server
.to_dict()
1003 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1004 self
._format
_exception
(e
)
1006 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
1008 Get a console for the virtual machine
1010 vm_id: uuid of the VM
1011 console_type, can be:
1012 "novnc" (by default), "xvpvnc" for VNC types,
1013 "rdp-html5" for RDP types, "spice-html5" for SPICE types
1014 Returns dict with the console parameters:
1015 protocol: ssh, ftp, http, https, ...
1016 server: usually ip address
1017 port: the http, ssh, ... port
1018 suffix: extra text, e.g. the http path and query string
1020 self
.logger
.debug("Getting VM CONSOLE from VIM")
1022 self
._reload
_connection
()
1023 server
= self
.nova
.servers
.find(id=vm_id
)
1024 if console_type
== None or console_type
== "novnc":
1025 console_dict
= server
.get_vnc_console("novnc")
1026 elif console_type
== "xvpvnc":
1027 console_dict
= server
.get_vnc_console(console_type
)
1028 elif console_type
== "rdp-html5":
1029 console_dict
= server
.get_rdp_console(console_type
)
1030 elif console_type
== "spice-html5":
1031 console_dict
= server
.get_spice_console(console_type
)
1033 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
), http_code
=vimconn
.HTTP_Bad_Request
)
1035 console_dict1
= console_dict
.get("console")
1037 console_url
= console_dict1
.get("url")
1040 protocol_index
= console_url
.find("//")
1041 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1042 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1043 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1044 return -vimconn
.HTTP_Internal_Server_Error
, "Unexpected response from VIM"
1045 console_dict
={"protocol": console_url
[0:protocol_index
],
1046 "server": console_url
[protocol_index
+2:port_index
],
1047 "port": console_url
[port_index
:suffix_index
],
1048 "suffix": console_url
[suffix_index
+1:]
1052 raise vimconn
.vimconnUnexpectedResponse("Unexpected response from VIM")
1054 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.BadRequest
, ConnectionError
) as e
:
1055 self
._format
_exception
(e
)
1057 def delete_vminstance(self
, vm_id
):
1058 '''Removes a VM instance from VIM. Returns the old identifier
1060 #print "osconnector: Getting VM from VIM"
1062 self
._reload
_connection
()
1063 #delete VM ports attached to this networks before the virtual machine
1064 ports
= self
.neutron
.list_ports(device_id
=vm_id
)
1065 for p
in ports
['ports']:
1067 self
.neutron
.delete_port(p
["id"])
1068 except Exception as e
:
1069 self
.logger
.error("Error deleting port: " + type(e
).__name
__ + ": "+ str(e
))
1071 #commented because detaching the volumes makes the servers.delete not work properly ?!?
1072 #dettach volumes attached
1073 server
= self
.nova
.servers
.get(vm_id
)
1074 volumes_attached_dict
= server
._info
['os-extended-volumes:volumes_attached']
1075 #for volume in volumes_attached_dict:
1076 # self.cinder.volumes.detach(volume['id'])
1078 self
.nova
.servers
.delete(vm_id
)
1081 #Although having detached them should have them in active status
1082 #we ensure in this loop
1085 while keep_waiting
and elapsed_time
< volume_timeout
:
1086 keep_waiting
= False
1087 for volume
in volumes_attached_dict
:
1088 if self
.cinder
.volumes
.get(volume
['id']).status
!= 'available':
1091 self
.cinder
.volumes
.delete(volume
['id'])
1097 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
1098 self
._format
_exception
(e
)
1099 #TODO insert exception vimconn.HTTP_Unauthorized
1100 #if reaching here is because an exception
1102 def refresh_vms_status(self
, vm_list
):
1103 '''Get the status of the virtual machines and their interfaces/ports
1104 Params: the list of VM identifiers
1105 Returns a dictionary with:
1106 vm_id: #VIM id of this Virtual Machine
1107 status: #Mandatory. Text with one of:
1108 # DELETED (not found at vim)
1109 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1110 # OTHER (Vim reported other status not understood)
1111 # ERROR (VIM indicates an ERROR status)
1112 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1113 # CREATING (on building process), ERROR
1114 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1116 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1117 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1119 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1120 mac_address: #Text format XX:XX:XX:XX:XX:XX
1121 vim_net_id: #network id where this interface is connected
1122 vim_interface_id: #interface/port VIM id
1123 ip_address: #null, or text with IPv4, IPv6 address
1124 compute_node: #identification of compute node where PF,VF interface is allocated
1125 pci: #PCI address of the NIC that hosts the PF,VF
1126 vlan: #physical VLAN used for VF
1129 self
.logger
.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1130 for vm_id
in vm_list
:
1133 vm_vim
= self
.get_vminstance(vm_id
)
1134 if vm_vim
['status'] in vmStatus2manoFormat
:
1135 vm
['status'] = vmStatus2manoFormat
[ vm_vim
['status'] ]
1137 vm
['status'] = "OTHER"
1138 vm
['error_msg'] = "VIM status reported " + vm_vim
['status']
1140 vm
['vim_info'] = yaml
.safe_dump(vm_vim
, default_flow_style
=True, width
=256)
1141 except yaml
.representer
.RepresenterError
:
1142 vm
['vim_info'] = str(vm_vim
)
1143 vm
["interfaces"] = []
1144 if vm_vim
.get('fault'):
1145 vm
['error_msg'] = str(vm_vim
['fault'])
1148 self
._reload
_connection
()
1149 port_dict
=self
.neutron
.list_ports(device_id
=vm_id
)
1150 for port
in port_dict
["ports"]:
1153 interface
['vim_info'] = yaml
.safe_dump(port
, default_flow_style
=True, width
=256)
1154 except yaml
.representer
.RepresenterError
:
1155 interface
['vim_info'] = str(port
)
1156 interface
["mac_address"] = port
.get("mac_address")
1157 interface
["vim_net_id"] = port
["network_id"]
1158 interface
["vim_interface_id"] = port
["id"]
1159 # check if OS-EXT-SRV-ATTR:host is there,
1160 # in case of non-admin credentials, it will be missing
1161 if vm_vim
.get('OS-EXT-SRV-ATTR:host'):
1162 interface
["compute_node"] = vm_vim
['OS-EXT-SRV-ATTR:host']
1163 interface
["pci"] = None
1165 # check if binding:profile is there,
1166 # in case of non-admin credentials, it will be missing
1167 if port
.get('binding:profile'):
1168 if port
['binding:profile'].get('pci_slot'):
1169 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1170 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1171 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1172 pci
= port
['binding:profile']['pci_slot']
1173 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1174 interface
["pci"] = pci
1175 interface
["vlan"] = None
1176 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
1177 network
= self
.neutron
.show_network(port
["network_id"])
1178 if network
['network'].get('provider:network_type') == 'vlan' and \
1179 port
.get("binding:vnic_type") == "direct":
1180 interface
["vlan"] = network
['network'].get('provider:segmentation_id')
1182 #look for floating ip address
1183 floating_ip_dict
= self
.neutron
.list_floatingips(port_id
=port
["id"])
1184 if floating_ip_dict
.get("floatingips"):
1185 ips
.append(floating_ip_dict
["floatingips"][0].get("floating_ip_address") )
1187 for subnet
in port
["fixed_ips"]:
1188 ips
.append(subnet
["ip_address"])
1189 interface
["ip_address"] = ";".join(ips
)
1190 vm
["interfaces"].append(interface
)
1191 except Exception as e
:
1192 self
.logger
.error("Error getting vm interface information " + type(e
).__name
__ + ": "+ str(e
))
1193 except vimconn
.vimconnNotFoundException
as e
:
1194 self
.logger
.error("Exception getting vm status: %s", str(e
))
1195 vm
['status'] = "DELETED"
1196 vm
['error_msg'] = str(e
)
1197 except vimconn
.vimconnException
as e
:
1198 self
.logger
.error("Exception getting vm status: %s", str(e
))
1199 vm
['status'] = "VIM_ERROR"
1200 vm
['error_msg'] = str(e
)
1204 def action_vminstance(self
, vm_id
, action_dict
):
1205 '''Send and action over a VM instance from VIM
1206 Returns the vm_id if the action was successfully sent to the VIM'''
1207 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
1209 self
._reload
_connection
()
1210 server
= self
.nova
.servers
.find(id=vm_id
)
1211 if "start" in action_dict
:
1212 if action_dict
["start"]=="rebuild":
1215 if server
.status
=="PAUSED":
1217 elif server
.status
=="SUSPENDED":
1219 elif server
.status
=="SHUTOFF":
1221 elif "pause" in action_dict
:
1223 elif "resume" in action_dict
:
1225 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
1227 elif "forceOff" in action_dict
:
1229 elif "terminate" in action_dict
:
1231 elif "createImage" in action_dict
:
1232 server
.create_image()
1233 #"path":path_schema,
1234 #"description":description_schema,
1235 #"name":name_schema,
1236 #"metadata":metadata_schema,
1237 #"imageRef": id_schema,
1238 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1239 elif "rebuild" in action_dict
:
1240 server
.rebuild(server
.image
['id'])
1241 elif "reboot" in action_dict
:
1242 server
.reboot() #reboot_type='SOFT'
1243 elif "console" in action_dict
:
1244 console_type
= action_dict
["console"]
1245 if console_type
== None or console_type
== "novnc":
1246 console_dict
= server
.get_vnc_console("novnc")
1247 elif console_type
== "xvpvnc":
1248 console_dict
= server
.get_vnc_console(console_type
)
1249 elif console_type
== "rdp-html5":
1250 console_dict
= server
.get_rdp_console(console_type
)
1251 elif console_type
== "spice-html5":
1252 console_dict
= server
.get_spice_console(console_type
)
1254 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
),
1255 http_code
=vimconn
.HTTP_Bad_Request
)
1257 console_url
= console_dict
["console"]["url"]
1259 protocol_index
= console_url
.find("//")
1260 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1261 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1262 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1263 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1264 console_dict2
={"protocol": console_url
[0:protocol_index
],
1265 "server": console_url
[protocol_index
+2 : port_index
],
1266 "port": int(console_url
[port_index
+1 : suffix_index
]),
1267 "suffix": console_url
[suffix_index
+1:]
1269 return console_dict2
1270 except Exception as e
:
1271 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1274 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1275 self
._format
_exception
(e
)
1276 #TODO insert exception vimconn.HTTP_Unauthorized
1280 def new_external_port(self
, port_data
):
1281 #TODO openstack if needed
1282 '''Adds a external port to VIM'''
1283 '''Returns the port identifier'''
1284 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.new_external_port() not implemented"
1286 def connect_port_network(self
, port_id
, network_id
, admin
=False):
1287 #TODO openstack if needed
1288 '''Connects a external port to a network'''
1289 '''Returns status code of the VIM response'''
1290 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.connect_port_network() not implemented"
1292 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
1293 '''Adds a new user to openstack VIM'''
1294 '''Returns the user identifier'''
1295 self
.logger
.debug("osconnector: Adding a new user to VIM")
1297 self
._reload
_connection
()
1298 user
=self
.keystone
.users
.create(user_name
, user_passwd
, tenant_id
=tenant_id
)
1299 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1301 except ksExceptions
.ConnectionError
as e
:
1302 error_value
=-vimconn
.HTTP_Bad_Request
1303 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1304 except ksExceptions
.ClientException
as e
: #TODO remove
1305 error_value
=-vimconn
.HTTP_Bad_Request
1306 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1307 #TODO insert exception vimconn.HTTP_Unauthorized
1308 #if reaching here is because an exception
1310 self
.logger
.debug("new_user " + error_text
)
1311 return error_value
, error_text
1313 def delete_user(self
, user_id
):
1314 '''Delete a user from openstack VIM'''
1315 '''Returns the user identifier'''
1317 print "osconnector: Deleting a user from VIM"
1319 self
._reload
_connection
()
1320 self
.keystone
.users
.delete(user_id
)
1322 except ksExceptions
.ConnectionError
as e
:
1323 error_value
=-vimconn
.HTTP_Bad_Request
1324 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1325 except ksExceptions
.NotFound
as e
:
1326 error_value
=-vimconn
.HTTP_Not_Found
1327 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1328 except ksExceptions
.ClientException
as e
: #TODO remove
1329 error_value
=-vimconn
.HTTP_Bad_Request
1330 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1331 #TODO insert exception vimconn.HTTP_Unauthorized
1332 #if reaching here is because an exception
1334 print "delete_tenant " + error_text
1335 return error_value
, error_text
1337 def get_hosts_info(self
):
1338 '''Get the information of deployed hosts
1339 Returns the hosts content'''
1341 print "osconnector: Getting Host info from VIM"
1344 self
._reload
_connection
()
1345 hypervisors
= self
.nova
.hypervisors
.list()
1346 for hype
in hypervisors
:
1347 h_list
.append( hype
.to_dict() )
1348 return 1, {"hosts":h_list
}
1349 except nvExceptions
.NotFound
as e
:
1350 error_value
=-vimconn
.HTTP_Not_Found
1351 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1352 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1353 error_value
=-vimconn
.HTTP_Bad_Request
1354 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1355 #TODO insert exception vimconn.HTTP_Unauthorized
1356 #if reaching here is because an exception
1358 print "get_hosts_info " + error_text
1359 return error_value
, error_text
1361 def get_hosts(self
, vim_tenant
):
1362 '''Get the hosts and deployed instances
1363 Returns the hosts content'''
1364 r
, hype_dict
= self
.get_hosts_info()
1367 hypervisors
= hype_dict
["hosts"]
1369 servers
= self
.nova
.servers
.list()
1370 for hype
in hypervisors
:
1371 for server
in servers
:
1372 if server
.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype
['hypervisor_hostname']:
1374 hype
['vm'].append(server
.id)
1376 hype
['vm'] = [server
.id]
1378 except nvExceptions
.NotFound
as e
:
1379 error_value
=-vimconn
.HTTP_Not_Found
1380 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1381 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1382 error_value
=-vimconn
.HTTP_Bad_Request
1383 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1384 #TODO insert exception vimconn.HTTP_Unauthorized
1385 #if reaching here is because an exception
1387 print "get_hosts " + error_text
1388 return error_value
, error_text