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
.keystone
= self
.session
.get('keystone')
93 self
.api_version3
= self
.session
.get('api_version3')
95 self
.logger
= logging
.getLogger('openmano.vim.openstack')
97 self
.logger
.setLevel( getattr(logging
, log_level
) )
99 def __getitem__(self
, index
):
100 """Get individuals parameters.
102 if index
== 'project_domain_id':
103 return self
.config
.get("project_domain_id")
104 elif index
== 'user_domain_id':
105 return self
.config
.get("user_domain_id")
107 return vimconn
.vimconnector
.__getitem
__(self
, index
)
109 def __setitem__(self
, index
, value
):
110 """Set individuals parameters and it is marked as dirty so to force connection reload.
112 if index
== 'project_domain_id':
113 self
.config
["project_domain_id"] = value
114 elif index
== 'user_domain_id':
115 self
.config
["user_domain_id"] = value
117 vimconn
.vimconnector
.__setitem
__(self
, index
, value
)
118 self
.session
['reload_client'] = True
120 def _reload_connection(self
):
121 '''Called before any operation, it check if credentials has changed
122 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
124 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
125 if self
.session
['reload_client']:
126 if self
.config
.get('APIversion'):
127 self
.api_version3
= self
.config
['APIversion'] == 'v3.3' or self
.config
['APIversion'] == '3'
128 else: # get from ending auth_url that end with v3 or with v2.0
129 self
.api_version3
= self
.url
.split("/")[-1] == "v3"
130 self
.session
['api_version3'] = self
.api_version3
131 if self
.api_version3
:
132 auth
= v3
.Password(auth_url
=self
.url
,
134 password
=self
.passwd
,
135 project_name
=self
.tenant_name
,
136 project_id
=self
.tenant_id
,
137 project_domain_id
=self
.config
.get('project_domain_id', 'default'),
138 user_domain_id
=self
.config
.get('user_domain_id', 'default'))
140 auth
= v2
.Password(auth_url
=self
.url
,
142 password
=self
.passwd
,
143 tenant_name
=self
.tenant_name
,
144 tenant_id
=self
.tenant_id
)
145 sess
= session
.Session(auth
=auth
, verify
=not self
.insecure
)
146 if self
.api_version3
:
147 self
.keystone
= ksClient_v3
.Client(session
=sess
)
149 self
.keystone
= ksClient_v2
.Client(session
=sess
)
150 self
.session
['keystone'] = self
.keystone
151 self
.nova
= self
.session
['nova'] = nClient
.Client("2.1", session
=sess
)
152 self
.neutron
= self
.session
['neutron'] = neClient
.Client('2.0', session
=sess
)
153 self
.cinder
= self
.session
['cinder'] = cClient
.Client(2, session
=sess
)
154 self
.glance
= self
.session
['glance'] = glClient
.Client(2, session
=sess
)
155 self
.session
['reload_client'] = False
156 self
.persistent_info
['session'] = self
.session
158 def __net_os2mano(self
, net_list_dict
):
159 '''Transform the net openstack format to mano format
160 net_list_dict can be a list of dict or a single dict'''
161 if type(net_list_dict
) is dict:
162 net_list_
=(net_list_dict
,)
163 elif type(net_list_dict
) is list:
164 net_list_
=net_list_dict
166 raise TypeError("param net_list_dict must be a list or a dictionary")
167 for net
in net_list_
:
168 if net
.get('provider:network_type') == "vlan":
173 def _format_exception(self
, exception
):
174 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
175 if isinstance(exception
, (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
,
176 ConnectionError
, ksExceptions
.ConnectionError
, neExceptions
.ConnectionFailed
178 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
179 elif isinstance(exception
, (nvExceptions
.ClientException
, ksExceptions
.ClientException
,
180 neExceptions
.NeutronException
, nvExceptions
.BadRequest
)):
181 raise vimconn
.vimconnUnexpectedResponse(type(exception
).__name
__ + ": " + str(exception
))
182 elif isinstance(exception
, (neExceptions
.NetworkNotFoundClient
, nvExceptions
.NotFound
)):
183 raise vimconn
.vimconnNotFoundException(type(exception
).__name
__ + ": " + str(exception
))
184 elif isinstance(exception
, nvExceptions
.Conflict
):
185 raise vimconn
.vimconnConflictException(type(exception
).__name
__ + ": " + str(exception
))
186 elif isinstance(exception
, vimconn
.vimconnException
):
189 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
191 def get_tenant_list(self
, filter_dict
={}):
192 '''Obtain tenants of VIM
193 filter_dict can contain the following keys:
194 name: filter by tenant name
195 id: filter by tenant uuid/id
197 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
199 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
201 self
._reload
_connection
()
202 if self
.api_version3
:
203 project_class_list
= self
.keystone
.projects
.list(name
=filter_dict
.get("name"))
205 project_class_list
= self
.keystone
.tenants
.findall(**filter_dict
)
207 for project
in project_class_list
:
208 if filter_dict
.get('id') and filter_dict
["id"] != project
.id:
210 project_list
.append(project
.to_dict())
212 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
213 self
._format
_exception
(e
)
215 def new_tenant(self
, tenant_name
, tenant_description
):
216 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
217 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
219 self
._reload
_connection
()
220 if self
.api_version3
:
221 project
= self
.keystone
.projects
.create(tenant_name
, self
.config
.get("project_domain_id", "default"),
222 description
=tenant_description
, is_domain
=False)
224 project
= self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
226 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
227 self
._format
_exception
(e
)
229 def delete_tenant(self
, tenant_id
):
230 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
231 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
233 self
._reload
_connection
()
234 if self
.api_version3
:
235 self
.keystone
.projects
.delete(tenant_id
)
237 self
.keystone
.tenants
.delete(tenant_id
)
239 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
240 self
._format
_exception
(e
)
242 def new_network(self
,net_name
, net_type
, ip_profile
=None, shared
=False, vlan
=None):
243 '''Adds a tenant network to VIM. Returns the network identifier'''
244 self
.logger
.debug("Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
)
245 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
248 self
._reload
_connection
()
249 network_dict
= {'name': net_name
, 'admin_state_up': True}
250 if net_type
=="data" or net_type
=="ptp":
251 if self
.config
.get('dataplane_physical_net') == None:
252 raise vimconn
.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
253 network_dict
["provider:physical_network"] = self
.config
['dataplane_physical_net'] #"physnet_sriov" #TODO physical
254 network_dict
["provider:network_type"] = "vlan"
256 network_dict
["provider:network_type"] = vlan
257 network_dict
["shared"]=shared
258 new_net
=self
.neutron
.create_network({'network':network_dict
})
260 #create subnetwork, even if there is no profile
263 if 'subnet_address' not in ip_profile
:
264 #Fake subnet is required
265 subnet_rand
= random
.randint(0, 255)
266 ip_profile
['subnet_address'] = "192.168.{}.0/24".format(subnet_rand
)
267 if 'ip_version' not in ip_profile
:
268 ip_profile
['ip_version'] = "IPv4"
269 subnet
= {"name":net_name
+"-subnet",
270 "network_id": new_net
["network"]["id"],
271 "ip_version": 4 if ip_profile
['ip_version']=="IPv4" else 6,
272 "cidr": ip_profile
['subnet_address']
274 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
275 subnet
['gateway_ip'] = ip_profile
.get('gateway_address')
276 if ip_profile
.get('dns_address'):
277 subnet
['dns_nameservers'] = ip_profile
['dns_address'].split(";")
278 if 'dhcp_enabled' in ip_profile
:
279 subnet
['enable_dhcp'] = False if ip_profile
['dhcp_enabled']=="false" else True
280 if 'dhcp_start_address' in ip_profile
:
281 subnet
['allocation_pools'] = []
282 subnet
['allocation_pools'].append(dict())
283 subnet
['allocation_pools'][0]['start'] = ip_profile
['dhcp_start_address']
284 if 'dhcp_count' in ip_profile
:
285 #parts = ip_profile['dhcp_start_address'].split('.')
286 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
287 ip_int
= int(netaddr
.IPAddress(ip_profile
['dhcp_start_address']))
288 ip_int
+= ip_profile
['dhcp_count'] - 1
289 ip_str
= str(netaddr
.IPAddress(ip_int
))
290 subnet
['allocation_pools'][0]['end'] = ip_str
291 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
292 self
.neutron
.create_subnet({"subnet": subnet
} )
293 return new_net
["network"]["id"]
294 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
296 self
.neutron
.delete_network(new_net
['network']['id'])
297 self
._format
_exception
(e
)
299 def get_network_list(self
, filter_dict
={}):
300 '''Obtain tenant networks of VIM
306 admin_state_up: boolean
308 Returns the network list of dictionaries
310 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
312 self
._reload
_connection
()
313 if self
.api_version3
and "tenant_id" in filter_dict
:
314 filter_dict
['project_id'] = filter_dict
.pop('tenant_id') #TODO check
315 net_dict
=self
.neutron
.list_networks(**filter_dict
)
316 net_list
=net_dict
["networks"]
317 self
.__net
_os
2mano
(net_list
)
319 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
320 self
._format
_exception
(e
)
322 def get_network(self
, net_id
):
323 '''Obtain details of network from VIM
324 Returns the network information from a network id'''
325 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
326 filter_dict
={"id": net_id
}
327 net_list
= self
.get_network_list(filter_dict
)
329 raise vimconn
.vimconnNotFoundException("Network '{}' not found".format(net_id
))
330 elif len(net_list
)>1:
331 raise vimconn
.vimconnConflictException("Found more than one network with this criteria")
334 for subnet_id
in net
.get("subnets", () ):
336 subnet
= self
.neutron
.show_subnet(subnet_id
)
337 except Exception as e
:
338 self
.logger
.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id
, str(e
)))
339 subnet
= {"id": subnet_id
, "fault": str(e
)}
340 subnets
.append(subnet
)
341 net
["subnets"] = subnets
342 net
["encapsulation"] = net
.get('provider:network_type')
343 net
["segmentation_id"] = net
.get('provider:segmentation_id')
346 def delete_network(self
, net_id
):
347 '''Deletes a tenant network from VIM. Returns the old network identifier'''
348 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
350 self
._reload
_connection
()
351 #delete VM ports attached to this networks before the network
352 ports
= self
.neutron
.list_ports(network_id
=net_id
)
353 for p
in ports
['ports']:
355 self
.neutron
.delete_port(p
["id"])
356 except Exception as e
:
357 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
358 self
.neutron
.delete_network(net_id
)
360 except (neExceptions
.ConnectionFailed
, neExceptions
.NetworkNotFoundClient
, neExceptions
.NeutronException
,
361 ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
362 self
._format
_exception
(e
)
364 def refresh_nets_status(self
, net_list
):
365 '''Get the status of the networks
366 Params: the list of network identifiers
367 Returns a dictionary with:
368 net_id: #VIM id of this network
369 status: #Mandatory. Text with one of:
370 # DELETED (not found at vim)
371 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
372 # OTHER (Vim reported other status not understood)
373 # ERROR (VIM indicates an ERROR status)
374 # ACTIVE, INACTIVE, DOWN (admin down),
375 # BUILD (on building process)
377 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
378 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
382 for net_id
in net_list
:
385 net_vim
= self
.get_network(net_id
)
386 if net_vim
['status'] in netStatus2manoFormat
:
387 net
["status"] = netStatus2manoFormat
[ net_vim
['status'] ]
389 net
["status"] = "OTHER"
390 net
["error_msg"] = "VIM status reported " + net_vim
['status']
392 if net
['status'] == "ACTIVE" and not net_vim
['admin_state_up']:
393 net
['status'] = 'DOWN'
395 net
['vim_info'] = yaml
.safe_dump(net_vim
, default_flow_style
=True, width
=256)
396 except yaml
.representer
.RepresenterError
:
397 net
['vim_info'] = str(net_vim
)
398 if net_vim
.get('fault'): #TODO
399 net
['error_msg'] = str(net_vim
['fault'])
400 except vimconn
.vimconnNotFoundException
as e
:
401 self
.logger
.error("Exception getting net status: %s", str(e
))
402 net
['status'] = "DELETED"
403 net
['error_msg'] = str(e
)
404 except vimconn
.vimconnException
as e
:
405 self
.logger
.error("Exception getting net status: %s", str(e
))
406 net
['status'] = "VIM_ERROR"
407 net
['error_msg'] = str(e
)
408 net_dict
[net_id
] = net
411 def get_flavor(self
, flavor_id
):
412 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
413 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
415 self
._reload
_connection
()
416 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
417 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
418 return flavor
.to_dict()
419 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
420 self
._format
_exception
(e
)
422 def get_flavor_id_from_data(self
, flavor_dict
):
423 """Obtain flavor id that match the flavor description
424 Returns the flavor_id or raises a vimconnNotFoundException
425 flavor_dict: contains the required ram, vcpus, disk
426 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
427 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
428 vimconnNotFoundException is raised
430 exact_match
= False if self
.config
.get('use_existing_flavors') else True
432 self
._reload
_connection
()
433 flavor_candidate_id
= None
434 flavor_candidate_data
= (10000, 10000, 10000)
435 flavor_target
= (flavor_dict
["ram"], flavor_dict
["vcpus"], flavor_dict
["disk"])
437 numas
= flavor_dict
.get("extended", {}).get("numas")
440 raise vimconn
.vimconnNotFoundException("Flavor with EPA still not implemted")
442 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
444 # numas = extended.get("numas")
445 for flavor
in self
.nova
.flavors
.list():
446 epa
= flavor
.get_keys()
450 flavor_data
= (flavor
.ram
, flavor
.vcpus
, flavor
.disk
)
451 if flavor_data
== flavor_target
:
453 elif not exact_match
and flavor_target
< flavor_data
< flavor_candidate_data
:
454 flavor_candidate_id
= flavor
.id
455 flavor_candidate_data
= flavor_data
456 if not exact_match
and flavor_candidate_id
:
457 return flavor_candidate_id
458 raise vimconn
.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict
)))
459 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
460 self
._format
_exception
(e
)
463 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
464 '''Adds a tenant flavor to openstack VIM
465 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
466 Returns the flavor identifier
468 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
472 name
=flavor_data
['name']
473 while retry
<max_retries
:
476 self
._reload
_connection
()
477 if change_name_if_used
:
480 fl
=self
.nova
.flavors
.list()
482 fl_names
.append(f
.name
)
483 while name
in fl_names
:
485 name
= flavor_data
['name']+"-" + str(name_suffix
)
487 ram
= flavor_data
.get('ram',64)
488 vcpus
= flavor_data
.get('vcpus',1)
491 extended
= flavor_data
.get("extended")
493 numas
=extended
.get("numas")
495 numa_nodes
= len(numas
)
497 return -1, "Can not add flavor with more than one numa"
498 numa_properties
= {"hw:numa_nodes":str(numa_nodes
)}
499 numa_properties
["hw:mem_page_size"] = "large"
500 numa_properties
["hw:cpu_policy"] = "dedicated"
501 numa_properties
["hw:numa_mempolicy"] = "strict"
503 #overwrite ram and vcpus
504 ram
= numa
['memory']*1024
505 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
506 if 'paired-threads' in numa
:
507 vcpus
= numa
['paired-threads']*2
508 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
509 numa_properties
["hw:cpu_thread_policy"] = "require"
510 numa_properties
["hw:cpu_policy"] = "dedicated"
511 elif 'cores' in numa
:
512 vcpus
= numa
['cores']
513 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
514 numa_properties
["hw:cpu_thread_policy"] = "isolate"
515 numa_properties
["hw:cpu_policy"] = "dedicated"
516 elif 'threads' in numa
:
517 vcpus
= numa
['threads']
518 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
519 numa_properties
["hw:cpu_thread_policy"] = "prefer"
520 numa_properties
["hw:cpu_policy"] = "dedicated"
521 # for interface in numa.get("interfaces",() ):
522 # if interface["dedicated"]=="yes":
523 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
524 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
527 new_flavor
=self
.nova
.flavors
.create(name
,
530 flavor_data
.get('disk',1),
531 is_public
=flavor_data
.get('is_public', True)
535 new_flavor
.set_keys(numa_properties
)
537 except nvExceptions
.Conflict
as e
:
538 if change_name_if_used
and retry
< max_retries
:
540 self
._format
_exception
(e
)
541 #except nvExceptions.BadRequest as e:
542 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
543 self
._format
_exception
(e
)
545 def delete_flavor(self
,flavor_id
):
546 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
549 self
._reload
_connection
()
550 self
.nova
.flavors
.delete(flavor_id
)
552 #except nvExceptions.BadRequest as e:
553 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
554 self
._format
_exception
(e
)
556 def new_image(self
,image_dict
):
558 Adds a tenant image to VIM. imge_dict is a dictionary with:
560 disk_format: qcow2, vhd, vmdk, raw (by default), ...
561 location: path or URI
562 public: "yes" or "no"
563 metadata: metadata of the image
566 # ALF TODO: revise and change for the new method or session
567 #using version 1 of glance client
568 glancev1
= gl1Client
.Client('1',self
.glance_endpoint
, token
=self
.keystone
.auth_token
, **self
.k_creds
) #TODO check k_creds vs n_creds
571 while retry
<max_retries
:
574 self
._reload
_connection
()
575 #determine format http://docs.openstack.org/developer/glance/formats.html
576 if "disk_format" in image_dict
:
577 disk_format
=image_dict
["disk_format"]
578 else: #autodiscover based on extension
579 if image_dict
['location'][-6:]==".qcow2":
581 elif image_dict
['location'][-4:]==".vhd":
583 elif image_dict
['location'][-5:]==".vmdk":
585 elif image_dict
['location'][-4:]==".vdi":
587 elif image_dict
['location'][-4:]==".iso":
589 elif image_dict
['location'][-4:]==".aki":
591 elif image_dict
['location'][-4:]==".ari":
593 elif image_dict
['location'][-4:]==".ami":
597 self
.logger
.debug("new_image: '%s' loading from '%s'", image_dict
['name'], image_dict
['location'])
598 if image_dict
['location'][0:4]=="http":
599 new_image
= glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
600 container_format
="bare", location
=image_dict
['location'], disk_format
=disk_format
)
602 with
open(image_dict
['location']) as fimage
:
603 new_image
= glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
604 container_format
="bare", data
=fimage
, disk_format
=disk_format
)
605 #insert metadata. We cannot use 'new_image.properties.setdefault'
606 #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
607 new_image_nova
=self
.nova
.images
.find(id=new_image
.id)
608 new_image_nova
.metadata
.setdefault('location',image_dict
['location'])
609 metadata_to_load
= image_dict
.get('metadata')
611 for k
,v
in yaml
.load(metadata_to_load
).iteritems():
612 new_image_nova
.metadata
.setdefault(k
,v
)
614 except (nvExceptions
.Conflict
, ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
615 self
._format
_exception
(e
)
616 except (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
617 if retry
==max_retries
:
619 self
._format
_exception
(e
)
620 except IOError as e
: #can not open the file
621 raise vimconn
.vimconnConnectionException(type(e
).__name
__ + ": " + str(e
)+ " for " + image_dict
['location'],
622 http_code
=vimconn
.HTTP_Bad_Request
)
624 def delete_image(self
, image_id
):
625 '''Deletes a tenant image from openstack VIM. Returns the old id
628 self
._reload
_connection
()
629 self
.nova
.images
.delete(image_id
)
631 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
: #TODO remove
632 self
._format
_exception
(e
)
634 def get_image_id_from_path(self
, path
):
635 '''Get the image id from image path in the VIM database. Returns the image_id'''
637 self
._reload
_connection
()
638 images
= self
.nova
.images
.list()
640 if image
.metadata
.get("location")==path
:
642 raise vimconn
.vimconnNotFoundException("image with location '{}' not found".format( path
))
643 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
644 self
._format
_exception
(e
)
646 def get_image_list(self
, filter_dict
={}):
647 '''Obtain tenant images from VIM
651 checksum: image checksum
652 Returns the image list of dictionaries:
653 [{<the fields at Filter_dict plus some VIM specific>}, ...]
656 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
658 self
._reload
_connection
()
659 filter_dict_os
=filter_dict
.copy()
660 #First we filter by the available filter fields: name, id. The others are removed.
661 filter_dict_os
.pop('checksum',None)
662 image_list
=self
.nova
.images
.findall(**filter_dict_os
)
663 if len(image_list
)==0:
665 #Then we filter by the rest of filter fields: checksum
667 for image
in image_list
:
668 image_class
=self
.glance
.images
.get(image
.id)
669 if 'checksum' not in filter_dict
or image_class
['checksum']==filter_dict
.get('checksum'):
670 filtered_list
.append(image_class
.copy())
672 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
673 self
._format
_exception
(e
)
675 def __wait_for_vm(self
, vm_id
, status
):
676 """wait until vm is in the desired status and return True.
677 If the VM gets in ERROR status, return false.
678 If the timeout is reached generate an exception"""
680 while elapsed_time
< server_timeout
:
681 vm_status
= self
.nova
.servers
.get(vm_id
).status
682 if vm_status
== status
:
684 if vm_status
== 'ERROR':
689 # if we exceeded the timeout rollback
690 if elapsed_time
>= server_timeout
:
691 raise vimconn
.vimconnException('Timeout waiting for instance ' + vm_id
+ ' to get ' + status
,
692 http_code
=vimconn
.HTTP_Request_Timeout
)
694 def new_vminstance(self
,name
,description
,start
,image_id
,flavor_id
,net_list
,cloud_config
=None,disk_list
=None):
695 '''Adds a VM instance to VIM
697 start: indicates if VM must start or boot in pause mode. Ignored
698 image_id,flavor_id: iamge and flavor uuid
699 net_list: list of interfaces, each one is a dictionary with:
701 net_id: network uuid to connect
702 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
703 model: interface model, ignored #TODO
704 mac_address: used for SR-IOV ifaces #TODO for other types
705 use: 'data', 'bridge', 'mgmt'
706 type: 'virtual', 'PF', 'VF', 'VFnotShared'
707 vim_id: filled/added by this function
708 floating_ip: True/False (or it can be None)
709 #TODO ip, security groups
710 Returns the instance identifier
712 self
.logger
.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id
, flavor_id
,str(net_list
))
717 external_network
=[] # list of external networks to be connected to instance, later on used to create floating_ip
718 no_secured_ports
= [] # List of port-is with port-security disabled
719 self
._reload
_connection
()
720 metadata_vpci
={} # For a specific neutron plugin
722 if not net
.get("net_id"): #skip non connected iface
726 "network_id": net
["net_id"],
727 "name": net
.get("name"),
728 "admin_state_up": True
730 if net
["type"]=="virtual":
732 metadata_vpci
[ net
["net_id"] ] = [[ net
["vpci"], "" ]]
733 elif net
["type"]=="VF": # for VF
735 if "VF" not in metadata_vpci
:
736 metadata_vpci
["VF"]=[]
737 metadata_vpci
["VF"].append([ net
["vpci"], "" ])
738 port_dict
["binding:vnic_type"]="direct"
741 if "PF" not in metadata_vpci
:
742 metadata_vpci
["PF"]=[]
743 metadata_vpci
["PF"].append([ net
["vpci"], "" ])
744 port_dict
["binding:vnic_type"]="direct-physical"
745 if not port_dict
["name"]:
746 port_dict
["name"]=name
747 if net
.get("mac_address"):
748 port_dict
["mac_address"]=net
["mac_address"]
749 new_port
= self
.neutron
.create_port({"port": port_dict
})
750 net
["mac_adress"] = new_port
["port"]["mac_address"]
751 net
["vim_id"] = new_port
["port"]["id"]
752 net
["ip"] = new_port
["port"].get("fixed_ips", [{}])[0].get("ip_address")
753 net_list_vim
.append({"port-id": new_port
["port"]["id"]})
755 if net
.get('floating_ip', False):
756 net
['exit_on_floating_ip_error'] = True
757 external_network
.append(net
)
758 elif net
['use'] == 'mgmt' and self
.config
.get('use_floating_ip'):
759 net
['exit_on_floating_ip_error'] = False
760 external_network
.append(net
)
762 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
763 # As a workaround we wait until the VM is active and then disable the port-security
764 if net
.get("port_security") == False:
765 no_secured_ports
.append(new_port
["port"]["id"])
768 metadata
= {"pci_assignement": json
.dumps(metadata_vpci
)}
769 if len(metadata
["pci_assignement"]) >255:
770 #limit the metadata size
771 #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
772 self
.logger
.warn("Metadata deleted since it exceeds the expected length (255) ")
775 self
.logger
.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
776 name
, image_id
, flavor_id
, str(net_list_vim
), description
, str(metadata
))
778 security_groups
= self
.config
.get('security_groups')
779 if type(security_groups
) is str:
780 security_groups
= ( security_groups
, )
784 if isinstance(cloud_config
, dict):
785 if cloud_config
.get("user-data"):
786 userdata
=cloud_config
["user-data"]
787 if cloud_config
.get("boot-data-drive") != None:
788 config_drive
= cloud_config
["boot-data-drive"]
789 if cloud_config
.get("config-files") or cloud_config
.get("users") or cloud_config
.get("key-pairs"):
791 raise vimconn
.vimconnConflictException("Cloud-config cannot contain both 'userdata' and 'config-files'/'users'/'key-pairs'")
794 if cloud_config
.get("key-pairs"):
795 userdata_dict
["ssh-authorized-keys"] = cloud_config
["key-pairs"]
796 userdata_dict
["users"] = [{"default": None, "ssh-authorized-keys": cloud_config
["key-pairs"] }]
797 if cloud_config
.get("users"):
798 if "users" not in userdata_dict
:
799 userdata_dict
["users"] = [ "default" ]
800 for user
in cloud_config
["users"]:
802 "name" : user
["name"],
803 "sudo": "ALL = (ALL)NOPASSWD:ALL"
805 if "user-info" in user
:
806 user_info
["gecos"] = user
["user-info"]
807 if user
.get("key-pairs"):
808 user_info
["ssh-authorized-keys"] = user
["key-pairs"]
809 userdata_dict
["users"].append(user_info
)
811 if cloud_config
.get("config-files"):
812 userdata_dict
["write_files"] = []
813 for file in cloud_config
["config-files"]:
815 "path" : file["dest"],
816 "content": file["content"]
818 if file.get("encoding"):
819 file_info
["encoding"] = file["encoding"]
820 if file.get("permissions"):
821 file_info
["permissions"] = file["permissions"]
822 if file.get("owner"):
823 file_info
["owner"] = file["owner"]
824 userdata_dict
["write_files"].append(file_info
)
825 userdata
= "#cloud-config\n"
826 userdata
+= yaml
.safe_dump(userdata_dict
, indent
=4, default_flow_style
=False)
827 self
.logger
.debug("userdata: %s", userdata
)
828 elif isinstance(cloud_config
, str):
829 userdata
= cloud_config
831 #Create additional volumes in case these are present in disk_list
832 block_device_mapping
= None
833 base_disk_index
= ord('b')
834 if disk_list
!= None:
835 block_device_mapping
= dict()
836 for disk
in disk_list
:
837 if 'image_id' in disk
:
838 volume
= self
.cinder
.volumes
.create(size
= disk
['size'],name
= name
+ '_vd' +
839 chr(base_disk_index
), imageRef
= disk
['image_id'])
841 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
842 chr(base_disk_index
))
843 block_device_mapping
['_vd' + chr(base_disk_index
)] = volume
.id
846 #wait until volumes are with status available
849 while keep_waiting
and elapsed_time
< volume_timeout
:
851 for volume_id
in block_device_mapping
.itervalues():
852 if self
.cinder
.volumes
.get(volume_id
).status
!= 'available':
858 #if we exceeded the timeout rollback
859 if elapsed_time
>= volume_timeout
:
860 #delete the volumes we just created
861 for volume_id
in block_device_mapping
.itervalues():
862 self
.cinder
.volumes
.delete(volume_id
)
864 #delete ports we just created
865 for net_item
in net_list_vim
:
866 if 'port-id' in net_item
:
867 self
.neutron
.delete_port(net_item
['port-id'])
869 raise vimconn
.vimconnException('Timeout creating volumes for instance ' + name
,
870 http_code
=vimconn
.HTTP_Request_Timeout
)
872 self
.logger
.debug("nova.servers.create({}, {}, {}, nics={}, meta={}, security_groups={}," \
873 "availability_zone={}, key_name={}, userdata={}, config_drive={}, " \
874 "block_device_mapping={})".format(name
, image_id
, flavor_id
, net_list_vim
,
875 metadata
, security_groups
, self
.config
.get('availability_zone'),
876 self
.config
.get('keypair'), userdata
, config_drive
, block_device_mapping
))
877 server
= self
.nova
.servers
.create(name
, image_id
, flavor_id
, nics
=net_list_vim
, meta
=metadata
,
878 security_groups
=security_groups
,
879 availability_zone
=self
.config
.get('availability_zone'),
880 key_name
=self
.config
.get('keypair'),
882 config_drive
= config_drive
,
883 block_device_mapping
= block_device_mapping
884 ) # , description=description)
886 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
888 self
.__wait
_for
_vm
(server
.id, 'ACTIVE')
890 for port_id
in no_secured_ports
:
892 self
.neutron
.update_port(port_id
, {"port": {"port_security_enabled": False, "security_groups": None} })
894 except Exception as e
:
895 self
.logger
.error("It was not possible to disable port security for port {}".format(port_id
))
896 self
.delete_vminstance(server
.id)
899 #print "DONE :-)", server
901 floating_ips
= self
.neutron
.list_floatingips().get("floatingips", ())
904 self
.__wait
_for
_vm
(server
.id, 'ACTIVE')
906 for floating_network
in external_network
:
909 while(assigned
== False):
911 ip
= floating_ips
.pop(0)
912 if not ip
.get("port_id", False) and ip
.get('tenant_id') == server
.tenant_id
:
913 free_floating_ip
= ip
.get("floating_ip_address")
915 fix_ip
= floating_network
.get('ip')
916 server
.add_floating_ip(free_floating_ip
, fix_ip
)
918 except Exception as e
:
919 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot create floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
921 #Find the external network
922 external_nets
= list()
923 for net
in self
.neutron
.list_networks()['networks']:
924 if net
['router:external']:
925 external_nets
.append(net
)
927 if len(external_nets
) == 0:
928 raise vimconn
.vimconnException("Cannot create floating_ip automatically since no external "
929 "network is present",
930 http_code
=vimconn
.HTTP_Conflict
)
931 if len(external_nets
) > 1:
932 raise vimconn
.vimconnException("Cannot create floating_ip automatically since multiple "
933 "external networks are present",
934 http_code
=vimconn
.HTTP_Conflict
)
936 pool_id
= external_nets
[0].get('id')
937 param
= {'floatingip': {'floating_network_id': pool_id
, 'tenant_id': server
.tenant_id
}}
939 #self.logger.debug("Creating floating IP")
940 new_floating_ip
= self
.neutron
.create_floatingip(param
)
941 free_floating_ip
= new_floating_ip
['floatingip']['floating_ip_address']
942 fix_ip
= floating_network
.get('ip')
943 server
.add_floating_ip(free_floating_ip
, fix_ip
)
945 except Exception as e
:
946 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot assign floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
947 except Exception as e
:
948 if not floating_network
['exit_on_floating_ip_error']:
949 self
.logger
.warn("Cannot create floating_ip. %s", str(e
))
954 # except nvExceptions.NotFound as e:
955 # error_value=-vimconn.HTTP_Not_Found
956 # error_text= "vm instance %s not found" % vm_id
957 # except TypeError as e:
958 # raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
960 except Exception as e
:
961 # delete the volumes we just created
962 if block_device_mapping
!= None:
963 for volume_id
in block_device_mapping
.itervalues():
964 self
.cinder
.volumes
.delete(volume_id
)
968 self
.delete_vminstance(server
.id)
970 # delete ports we just created
971 for net_item
in net_list_vim
:
972 if 'port-id' in net_item
:
973 self
.neutron
.delete_port(net_item
['port-id'])
975 self
._format
_exception
(e
)
977 def get_vminstance(self
,vm_id
):
978 '''Returns the VM instance information from VIM'''
979 #self.logger.debug("Getting VM from VIM")
981 self
._reload
_connection
()
982 server
= self
.nova
.servers
.find(id=vm_id
)
983 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
984 return server
.to_dict()
985 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
986 self
._format
_exception
(e
)
988 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
990 Get a console for the virtual machine
992 vm_id: uuid of the VM
993 console_type, can be:
994 "novnc" (by default), "xvpvnc" for VNC types,
995 "rdp-html5" for RDP types, "spice-html5" for SPICE types
996 Returns dict with the console parameters:
997 protocol: ssh, ftp, http, https, ...
998 server: usually ip address
999 port: the http, ssh, ... port
1000 suffix: extra text, e.g. the http path and query string
1002 self
.logger
.debug("Getting VM CONSOLE from VIM")
1004 self
._reload
_connection
()
1005 server
= self
.nova
.servers
.find(id=vm_id
)
1006 if console_type
== None or console_type
== "novnc":
1007 console_dict
= server
.get_vnc_console("novnc")
1008 elif console_type
== "xvpvnc":
1009 console_dict
= server
.get_vnc_console(console_type
)
1010 elif console_type
== "rdp-html5":
1011 console_dict
= server
.get_rdp_console(console_type
)
1012 elif console_type
== "spice-html5":
1013 console_dict
= server
.get_spice_console(console_type
)
1015 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
), http_code
=vimconn
.HTTP_Bad_Request
)
1017 console_dict1
= console_dict
.get("console")
1019 console_url
= console_dict1
.get("url")
1022 protocol_index
= console_url
.find("//")
1023 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1024 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1025 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1026 return -vimconn
.HTTP_Internal_Server_Error
, "Unexpected response from VIM"
1027 console_dict
={"protocol": console_url
[0:protocol_index
],
1028 "server": console_url
[protocol_index
+2:port_index
],
1029 "port": console_url
[port_index
:suffix_index
],
1030 "suffix": console_url
[suffix_index
+1:]
1034 raise vimconn
.vimconnUnexpectedResponse("Unexpected response from VIM")
1036 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.BadRequest
, ConnectionError
) as e
:
1037 self
._format
_exception
(e
)
1039 def delete_vminstance(self
, vm_id
):
1040 '''Removes a VM instance from VIM. Returns the old identifier
1042 #print "osconnector: Getting VM from VIM"
1044 self
._reload
_connection
()
1045 #delete VM ports attached to this networks before the virtual machine
1046 ports
= self
.neutron
.list_ports(device_id
=vm_id
)
1047 for p
in ports
['ports']:
1049 self
.neutron
.delete_port(p
["id"])
1050 except Exception as e
:
1051 self
.logger
.error("Error deleting port: " + type(e
).__name
__ + ": "+ str(e
))
1053 #commented because detaching the volumes makes the servers.delete not work properly ?!?
1054 #dettach volumes attached
1055 server
= self
.nova
.servers
.get(vm_id
)
1056 volumes_attached_dict
= server
._info
['os-extended-volumes:volumes_attached']
1057 #for volume in volumes_attached_dict:
1058 # self.cinder.volumes.detach(volume['id'])
1060 self
.nova
.servers
.delete(vm_id
)
1063 #Although having detached them should have them in active status
1064 #we ensure in this loop
1067 while keep_waiting
and elapsed_time
< volume_timeout
:
1068 keep_waiting
= False
1069 for volume
in volumes_attached_dict
:
1070 if self
.cinder
.volumes
.get(volume
['id']).status
!= 'available':
1073 self
.cinder
.volumes
.delete(volume
['id'])
1079 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
1080 self
._format
_exception
(e
)
1081 #TODO insert exception vimconn.HTTP_Unauthorized
1082 #if reaching here is because an exception
1084 def refresh_vms_status(self
, vm_list
):
1085 '''Get the status of the virtual machines and their interfaces/ports
1086 Params: the list of VM identifiers
1087 Returns a dictionary with:
1088 vm_id: #VIM id of this Virtual Machine
1089 status: #Mandatory. Text with one of:
1090 # DELETED (not found at vim)
1091 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1092 # OTHER (Vim reported other status not understood)
1093 # ERROR (VIM indicates an ERROR status)
1094 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1095 # CREATING (on building process), ERROR
1096 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1098 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1099 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1101 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1102 mac_address: #Text format XX:XX:XX:XX:XX:XX
1103 vim_net_id: #network id where this interface is connected
1104 vim_interface_id: #interface/port VIM id
1105 ip_address: #null, or text with IPv4, IPv6 address
1106 compute_node: #identification of compute node where PF,VF interface is allocated
1107 pci: #PCI address of the NIC that hosts the PF,VF
1108 vlan: #physical VLAN used for VF
1111 self
.logger
.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1112 for vm_id
in vm_list
:
1115 vm_vim
= self
.get_vminstance(vm_id
)
1116 if vm_vim
['status'] in vmStatus2manoFormat
:
1117 vm
['status'] = vmStatus2manoFormat
[ vm_vim
['status'] ]
1119 vm
['status'] = "OTHER"
1120 vm
['error_msg'] = "VIM status reported " + vm_vim
['status']
1122 vm
['vim_info'] = yaml
.safe_dump(vm_vim
, default_flow_style
=True, width
=256)
1123 except yaml
.representer
.RepresenterError
:
1124 vm
['vim_info'] = str(vm_vim
)
1125 vm
["interfaces"] = []
1126 if vm_vim
.get('fault'):
1127 vm
['error_msg'] = str(vm_vim
['fault'])
1130 self
._reload
_connection
()
1131 port_dict
=self
.neutron
.list_ports(device_id
=vm_id
)
1132 for port
in port_dict
["ports"]:
1135 interface
['vim_info'] = yaml
.safe_dump(port
, default_flow_style
=True, width
=256)
1136 except yaml
.representer
.RepresenterError
:
1137 interface
['vim_info'] = str(port
)
1138 interface
["mac_address"] = port
.get("mac_address")
1139 interface
["vim_net_id"] = port
["network_id"]
1140 interface
["vim_interface_id"] = port
["id"]
1141 # check if OS-EXT-SRV-ATTR:host is there,
1142 # in case of non-admin credentials, it will be missing
1143 if vm_vim
.get('OS-EXT-SRV-ATTR:host'):
1144 interface
["compute_node"] = vm_vim
['OS-EXT-SRV-ATTR:host']
1145 interface
["pci"] = None
1147 # check if binding:profile is there,
1148 # in case of non-admin credentials, it will be missing
1149 if port
.get('binding:profile'):
1150 if port
['binding:profile'].get('pci_slot'):
1151 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1152 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1153 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1154 pci
= port
['binding:profile']['pci_slot']
1155 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1156 interface
["pci"] = pci
1157 interface
["vlan"] = None
1158 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
1159 network
= self
.neutron
.show_network(port
["network_id"])
1160 if network
['network'].get('provider:network_type') == 'vlan' and \
1161 port
.get("binding:vnic_type") == "direct":
1162 interface
["vlan"] = network
['network'].get('provider:segmentation_id')
1164 #look for floating ip address
1165 floating_ip_dict
= self
.neutron
.list_floatingips(port_id
=port
["id"])
1166 if floating_ip_dict
.get("floatingips"):
1167 ips
.append(floating_ip_dict
["floatingips"][0].get("floating_ip_address") )
1169 for subnet
in port
["fixed_ips"]:
1170 ips
.append(subnet
["ip_address"])
1171 interface
["ip_address"] = ";".join(ips
)
1172 vm
["interfaces"].append(interface
)
1173 except Exception as e
:
1174 self
.logger
.error("Error getting vm interface information " + type(e
).__name
__ + ": "+ str(e
))
1175 except vimconn
.vimconnNotFoundException
as e
:
1176 self
.logger
.error("Exception getting vm status: %s", str(e
))
1177 vm
['status'] = "DELETED"
1178 vm
['error_msg'] = str(e
)
1179 except vimconn
.vimconnException
as e
:
1180 self
.logger
.error("Exception getting vm status: %s", str(e
))
1181 vm
['status'] = "VIM_ERROR"
1182 vm
['error_msg'] = str(e
)
1186 def action_vminstance(self
, vm_id
, action_dict
):
1187 '''Send and action over a VM instance from VIM
1188 Returns the vm_id if the action was successfully sent to the VIM'''
1189 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
1191 self
._reload
_connection
()
1192 server
= self
.nova
.servers
.find(id=vm_id
)
1193 if "start" in action_dict
:
1194 if action_dict
["start"]=="rebuild":
1197 if server
.status
=="PAUSED":
1199 elif server
.status
=="SUSPENDED":
1201 elif server
.status
=="SHUTOFF":
1203 elif "pause" in action_dict
:
1205 elif "resume" in action_dict
:
1207 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
1209 elif "forceOff" in action_dict
:
1211 elif "terminate" in action_dict
:
1213 elif "createImage" in action_dict
:
1214 server
.create_image()
1215 #"path":path_schema,
1216 #"description":description_schema,
1217 #"name":name_schema,
1218 #"metadata":metadata_schema,
1219 #"imageRef": id_schema,
1220 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1221 elif "rebuild" in action_dict
:
1222 server
.rebuild(server
.image
['id'])
1223 elif "reboot" in action_dict
:
1224 server
.reboot() #reboot_type='SOFT'
1225 elif "console" in action_dict
:
1226 console_type
= action_dict
["console"]
1227 if console_type
== None or console_type
== "novnc":
1228 console_dict
= server
.get_vnc_console("novnc")
1229 elif console_type
== "xvpvnc":
1230 console_dict
= server
.get_vnc_console(console_type
)
1231 elif console_type
== "rdp-html5":
1232 console_dict
= server
.get_rdp_console(console_type
)
1233 elif console_type
== "spice-html5":
1234 console_dict
= server
.get_spice_console(console_type
)
1236 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
),
1237 http_code
=vimconn
.HTTP_Bad_Request
)
1239 console_url
= console_dict
["console"]["url"]
1241 protocol_index
= console_url
.find("//")
1242 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1243 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1244 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1245 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1246 console_dict2
={"protocol": console_url
[0:protocol_index
],
1247 "server": console_url
[protocol_index
+2 : port_index
],
1248 "port": int(console_url
[port_index
+1 : suffix_index
]),
1249 "suffix": console_url
[suffix_index
+1:]
1251 return console_dict2
1252 except Exception as e
:
1253 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1256 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1257 self
._format
_exception
(e
)
1258 #TODO insert exception vimconn.HTTP_Unauthorized
1262 def new_external_port(self
, port_data
):
1263 #TODO openstack if needed
1264 '''Adds a external port to VIM'''
1265 '''Returns the port identifier'''
1266 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.new_external_port() not implemented"
1268 def connect_port_network(self
, port_id
, network_id
, admin
=False):
1269 #TODO openstack if needed
1270 '''Connects a external port to a network'''
1271 '''Returns status code of the VIM response'''
1272 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.connect_port_network() not implemented"
1274 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
1275 '''Adds a new user to openstack VIM'''
1276 '''Returns the user identifier'''
1277 self
.logger
.debug("osconnector: Adding a new user to VIM")
1279 self
._reload
_connection
()
1280 user
=self
.keystone
.users
.create(user_name
, user_passwd
, tenant_id
=tenant_id
)
1281 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1283 except ksExceptions
.ConnectionError
as e
:
1284 error_value
=-vimconn
.HTTP_Bad_Request
1285 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1286 except ksExceptions
.ClientException
as e
: #TODO remove
1287 error_value
=-vimconn
.HTTP_Bad_Request
1288 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1289 #TODO insert exception vimconn.HTTP_Unauthorized
1290 #if reaching here is because an exception
1292 self
.logger
.debug("new_user " + error_text
)
1293 return error_value
, error_text
1295 def delete_user(self
, user_id
):
1296 '''Delete a user from openstack VIM'''
1297 '''Returns the user identifier'''
1299 print "osconnector: Deleting a user from VIM"
1301 self
._reload
_connection
()
1302 self
.keystone
.users
.delete(user_id
)
1304 except ksExceptions
.ConnectionError
as e
:
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 except ksExceptions
.NotFound
as e
:
1308 error_value
=-vimconn
.HTTP_Not_Found
1309 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1310 except ksExceptions
.ClientException
as e
: #TODO remove
1311 error_value
=-vimconn
.HTTP_Bad_Request
1312 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1313 #TODO insert exception vimconn.HTTP_Unauthorized
1314 #if reaching here is because an exception
1316 print "delete_tenant " + error_text
1317 return error_value
, error_text
1319 def get_hosts_info(self
):
1320 '''Get the information of deployed hosts
1321 Returns the hosts content'''
1323 print "osconnector: Getting Host info from VIM"
1326 self
._reload
_connection
()
1327 hypervisors
= self
.nova
.hypervisors
.list()
1328 for hype
in hypervisors
:
1329 h_list
.append( hype
.to_dict() )
1330 return 1, {"hosts":h_list
}
1331 except nvExceptions
.NotFound
as e
:
1332 error_value
=-vimconn
.HTTP_Not_Found
1333 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1334 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1335 error_value
=-vimconn
.HTTP_Bad_Request
1336 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1337 #TODO insert exception vimconn.HTTP_Unauthorized
1338 #if reaching here is because an exception
1340 print "get_hosts_info " + error_text
1341 return error_value
, error_text
1343 def get_hosts(self
, vim_tenant
):
1344 '''Get the hosts and deployed instances
1345 Returns the hosts content'''
1346 r
, hype_dict
= self
.get_hosts_info()
1349 hypervisors
= hype_dict
["hosts"]
1351 servers
= self
.nova
.servers
.list()
1352 for hype
in hypervisors
:
1353 for server
in servers
:
1354 if server
.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype
['hypervisor_hostname']:
1356 hype
['vm'].append(server
.id)
1358 hype
['vm'] = [server
.id]
1360 except nvExceptions
.NotFound
as e
:
1361 error_value
=-vimconn
.HTTP_Not_Found
1362 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1363 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1364 error_value
=-vimconn
.HTTP_Bad_Request
1365 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1366 #TODO insert exception vimconn.HTTP_Unauthorized
1367 #if reaching here is because an exception
1369 print "get_hosts " + error_text
1370 return error_value
, error_text