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 from glanceclient
import client
as glClient
44 import glanceclient
.client
as gl1Client
45 import glanceclient
.exc
as gl1Exceptions
46 from cinderclient
import client
as cClient
47 from httplib
import HTTPException
48 from neutronclient
.neutron
import client
as neClient
49 from neutronclient
.common
import exceptions
as neExceptions
50 from requests
.exceptions
import ConnectionError
52 '''contain the openstack virtual machine status to openmano status'''
53 vmStatus2manoFormat
={'ACTIVE':'ACTIVE',
55 'SUSPENDED': 'SUSPENDED',
58 'ERROR':'ERROR','DELETED':'DELETED'
60 netStatus2manoFormat
={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
63 #global var to have a timeout creating and deleting volumes
67 class vimconnector(vimconn
.vimconnector
):
68 def __init__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
=None, user
=None, passwd
=None,
69 log_level
=None, config
={}, persistent_info
={}):
70 '''using common constructor parameters. In this case
71 'url' is the keystone authorization url,
72 'url_admin' is not use
74 self
.osc_api_version
= config
.get('APIversion')
75 if self
.osc_api_version
!= 'v3.3' and self
.osc_api_version
!= 'v2.0' and self
.osc_api_version
:
76 raise vimconn
.vimconnException("Invalid value '{}' for config:APIversion. "
77 "Allowed values are 'v3.3' or 'v2.0'".format(self
.osc_api_version
))
78 vimconn
.vimconnector
.__init
__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
, user
, passwd
, log_level
,
81 self
.insecure
= self
.config
.get("insecure", False)
83 raise TypeError, 'url param can not be NoneType'
85 self
.tenant_name
= tenant_name
86 self
.tenant_id
= tenant_id
89 self
.persistent_info
= persistent_info
90 self
.session
= persistent_info
.get('session', {'reload_client': True})
91 self
.nova
= self
.session
.get('nova')
92 self
.neutron
= self
.session
.get('neutron')
93 self
.cinder
= self
.session
.get('cinder')
94 self
.glance
= self
.session
.get('glance')
96 self
.logger
= logging
.getLogger('openmano.vim.openstack')
98 self
.logger
.setLevel( getattr(logging
, log_level
) )
100 def __setitem__(self
,index
, value
):
101 '''Set individuals parameters
102 Throw TypeError, KeyError
104 self
.session
['reload_client'] = True
105 vimconn
.vimconnector
.__setitem
__(self
,index
, value
)
107 def _reload_connection(self
):
108 '''Called before any operation, it check if credentials has changed
109 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
111 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
112 if self
.session
['reload_client']:
113 if self
.osc_api_version
== 'v3.3' or self
.osc_api_version
== '3' or \
114 (not self
.osc_api_version
and self
.auth_url
.split("/")[-1] == "v3"):
115 auth
= v3
.Password(auth_url
=self
.auth_url
,
117 password
=self
.passwd
,
118 project_name
=self
.tenant_name
,
119 project_id
=self
.tenant_id
,
120 project_domain_id
=self
.config
.get('project_domain_id', 'default'),
121 user_domain_id
=self
.config
.get('user_domain_id', 'default'))
123 auth
= v2
.Password(auth_url
=self
.auth_url
,
125 password
=self
.passwd
,
126 tenant_name
=self
.tenant_name
,
127 tenant_id
=self
.tenant_id
)
128 sess
= session
.Session(auth
=auth
, verify
=not self
.insecure
)
129 self
.nova
= self
.session
['nova'] = nClient
.Client("2.1", session
=sess
)
130 self
.neutron
= self
.session
['neutron'] = neClient
.Client('2.0', session
=sess
)
131 self
.cinder
= self
.session
['cinder'] = cClient
.Client(2, session
=sess
)
132 self
.glance
= self
.session
['glance'] = glClient
.Client(2, session
=sess
)
133 self
.session
['reload_client'] = False
134 self
.persistent_info
['session'] = self
.session
136 def __net_os2mano(self
, net_list_dict
):
137 '''Transform the net openstack format to mano format
138 net_list_dict can be a list of dict or a single dict'''
139 if type(net_list_dict
) is dict:
140 net_list_
=(net_list_dict
,)
141 elif type(net_list_dict
) is list:
142 net_list_
=net_list_dict
144 raise TypeError("param net_list_dict must be a list or a dictionary")
145 for net
in net_list_
:
146 if net
.get('provider:network_type') == "vlan":
153 def _format_exception(self
, exception
):
154 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
155 if isinstance(exception
, (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
,
156 ConnectionError
, ksExceptions
.ConnectionError
, neExceptions
.ConnectionFailed
158 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
159 elif isinstance(exception
, (nvExceptions
.ClientException
, ksExceptions
.ClientException
,
160 neExceptions
.NeutronException
, nvExceptions
.BadRequest
)):
161 raise vimconn
.vimconnUnexpectedResponse(type(exception
).__name
__ + ": " + str(exception
))
162 elif isinstance(exception
, (neExceptions
.NetworkNotFoundClient
, nvExceptions
.NotFound
)):
163 raise vimconn
.vimconnNotFoundException(type(exception
).__name
__ + ": " + str(exception
))
164 elif isinstance(exception
, nvExceptions
.Conflict
):
165 raise vimconn
.vimconnConflictException(type(exception
).__name
__ + ": " + str(exception
))
167 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
169 def get_tenant_list(self
, filter_dict
={}):
170 '''Obtain tenants of VIM
171 filter_dict can contain the following keys:
172 name: filter by tenant name
173 id: filter by tenant uuid/id
175 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
177 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
179 self
._reload
_connection
()
180 if self
.osc_api_version
== 'v3.3':
181 project_class_list
=self
.keystone
.projects
.findall(**filter_dict
)
183 project_class_list
=self
.keystone
.tenants
.findall(**filter_dict
)
185 for project
in project_class_list
:
186 project_list
.append(project
.to_dict())
188 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
189 self
._format
_exception
(e
)
191 def new_tenant(self
, tenant_name
, tenant_description
):
192 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
193 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
195 self
._reload
_connection
()
196 if self
.osc_api_version
== 'v3.3':
197 project
=self
.keystone
.projects
.create(tenant_name
, tenant_description
)
199 project
=self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
201 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
202 self
._format
_exception
(e
)
204 def delete_tenant(self
, tenant_id
):
205 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
206 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
208 self
._reload
_connection
()
209 if self
.osc_api_version
== 'v3.3':
210 self
.keystone
.projects
.delete(tenant_id
)
212 self
.keystone
.tenants
.delete(tenant_id
)
214 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
215 self
._format
_exception
(e
)
217 def new_network(self
,net_name
, net_type
, ip_profile
=None, shared
=False, vlan
=None):
218 '''Adds a tenant network to VIM. Returns the network identifier'''
219 self
.logger
.debug("Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
)
220 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
223 self
._reload
_connection
()
224 network_dict
= {'name': net_name
, 'admin_state_up': True}
225 if net_type
=="data" or net_type
=="ptp":
226 if self
.config
.get('dataplane_physical_net') == None:
227 raise vimconn
.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
228 network_dict
["provider:physical_network"] = self
.config
['dataplane_physical_net'] #"physnet_sriov" #TODO physical
229 network_dict
["provider:network_type"] = "vlan"
231 network_dict
["provider:network_type"] = vlan
232 network_dict
["shared"]=shared
233 new_net
=self
.neutron
.create_network({'network':network_dict
})
235 #create subnetwork, even if there is no profile
238 if 'subnet_address' not in ip_profile
:
239 #Fake subnet is required
240 subnet_rand
= random
.randint(0, 255)
241 ip_profile
['subnet_address'] = "192.168.{}.0/24".format(subnet_rand
)
242 if 'ip_version' not in ip_profile
:
243 ip_profile
['ip_version'] = "IPv4"
244 subnet
={"name":net_name
+"-subnet",
245 "network_id": new_net
["network"]["id"],
246 "ip_version": 4 if ip_profile
['ip_version']=="IPv4" else 6,
247 "cidr": ip_profile
['subnet_address']
249 if 'gateway_address' in ip_profile
:
250 subnet
['gateway_ip'] = ip_profile
['gateway_address']
251 if ip_profile
.get('dns_address'):
252 subnet
['dns_nameservers'] = ip_profile
['dns_address'].split(";")
253 if 'dhcp_enabled' in ip_profile
:
254 subnet
['enable_dhcp'] = False if ip_profile
['dhcp_enabled']=="false" else True
255 if 'dhcp_start_address' in ip_profile
:
256 subnet
['allocation_pools']=[]
257 subnet
['allocation_pools'].append(dict())
258 subnet
['allocation_pools'][0]['start'] = ip_profile
['dhcp_start_address']
259 if 'dhcp_count' in ip_profile
:
260 #parts = ip_profile['dhcp_start_address'].split('.')
261 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
262 ip_int
= int(netaddr
.IPAddress(ip_profile
['dhcp_start_address']))
263 ip_int
+= ip_profile
['dhcp_count'] - 1
264 ip_str
= str(netaddr
.IPAddress(ip_int
))
265 subnet
['allocation_pools'][0]['end'] = ip_str
266 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
267 self
.neutron
.create_subnet({"subnet": subnet
} )
268 return new_net
["network"]["id"]
269 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
271 self
.neutron
.delete_network(new_net
['network']['id'])
272 self
._format
_exception
(e
)
274 def get_network_list(self
, filter_dict
={}):
275 '''Obtain tenant networks of VIM
281 admin_state_up: boolean
283 Returns the network list of dictionaries
285 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
287 self
._reload
_connection
()
288 if self
.osc_api_version
== 'v3.3' and "tenant_id" in filter_dict
:
289 filter_dict
['project_id'] = filter_dict
.pop('tenant_id')
290 net_dict
=self
.neutron
.list_networks(**filter_dict
)
291 net_list
=net_dict
["networks"]
292 self
.__net
_os
2mano
(net_list
)
294 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
295 self
._format
_exception
(e
)
297 def get_network(self
, net_id
):
298 '''Obtain details of network from VIM
299 Returns the network information from a network id'''
300 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
301 filter_dict
={"id": net_id
}
302 net_list
= self
.get_network_list(filter_dict
)
304 raise vimconn
.vimconnNotFoundException("Network '{}' not found".format(net_id
))
305 elif len(net_list
)>1:
306 raise vimconn
.vimconnConflictException("Found more than one network with this criteria")
309 for subnet_id
in net
.get("subnets", () ):
311 subnet
= self
.neutron
.show_subnet(subnet_id
)
312 except Exception as e
:
313 self
.logger
.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id
, str(e
)))
314 subnet
= {"id": subnet_id
, "fault": str(e
)}
315 subnets
.append(subnet
)
316 net
["subnets"] = subnets
317 net
["encapsulation"] = net
.get('provider:network_type')
318 net
["segmentation_id"] = net
.get('provider:segmentation_id')
321 def delete_network(self
, net_id
):
322 '''Deletes a tenant network from VIM. Returns the old network identifier'''
323 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
325 self
._reload
_connection
()
326 #delete VM ports attached to this networks before the network
327 ports
= self
.neutron
.list_ports(network_id
=net_id
)
328 for p
in ports
['ports']:
330 self
.neutron
.delete_port(p
["id"])
331 except Exception as e
:
332 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
333 self
.neutron
.delete_network(net_id
)
335 except (neExceptions
.ConnectionFailed
, neExceptions
.NetworkNotFoundClient
, neExceptions
.NeutronException
,
336 ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
337 self
._format
_exception
(e
)
339 def refresh_nets_status(self
, net_list
):
340 '''Get the status of the networks
341 Params: the list of network identifiers
342 Returns a dictionary with:
343 net_id: #VIM id of this network
344 status: #Mandatory. Text with one of:
345 # DELETED (not found at vim)
346 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
347 # OTHER (Vim reported other status not understood)
348 # ERROR (VIM indicates an ERROR status)
349 # ACTIVE, INACTIVE, DOWN (admin down),
350 # BUILD (on building process)
352 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
353 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
357 for net_id
in net_list
:
360 net_vim
= self
.get_network(net_id
)
361 if net_vim
['status'] in netStatus2manoFormat
:
362 net
["status"] = netStatus2manoFormat
[ net_vim
['status'] ]
364 net
["status"] = "OTHER"
365 net
["error_msg"] = "VIM status reported " + net_vim
['status']
367 if net
['status'] == "ACTIVE" and not net_vim
['admin_state_up']:
368 net
['status'] = 'DOWN'
370 net
['vim_info'] = yaml
.safe_dump(net_vim
, default_flow_style
=True, width
=256)
371 except yaml
.representer
.RepresenterError
:
372 net
['vim_info'] = str(net_vim
)
373 if net_vim
.get('fault'): #TODO
374 net
['error_msg'] = str(net_vim
['fault'])
375 except vimconn
.vimconnNotFoundException
as e
:
376 self
.logger
.error("Exception getting net status: %s", str(e
))
377 net
['status'] = "DELETED"
378 net
['error_msg'] = str(e
)
379 except vimconn
.vimconnException
as e
:
380 self
.logger
.error("Exception getting net status: %s", str(e
))
381 net
['status'] = "VIM_ERROR"
382 net
['error_msg'] = str(e
)
383 net_dict
[net_id
] = net
386 def get_flavor(self
, flavor_id
):
387 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
388 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
390 self
._reload
_connection
()
391 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
392 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
393 return flavor
.to_dict()
394 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
395 self
._format
_exception
(e
)
397 def get_flavor_id_from_data(self
, flavor_dict
):
398 """Obtain flavor id that match the flavor description
399 Returns the flavor_id or raises a vimconnNotFoundException
400 flavor_dict: contains the required ram, vcpus, disk
401 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
402 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
403 vimconnNotFoundException is raised
405 exact_match
= False if self
.config
.get('use_existing_flavors') else True
407 self
._reload
_connection
()
408 flavor_candidate_id
= None
409 flavor_candidate_data
= (10000, 10000, 10000)
410 flavor_target
= (flavor_dict
["ram"], flavor_dict
["vcpus"], flavor_dict
["disk"])
412 numas
= flavor_dict
.get("extended", {}).get("numas")
415 raise vimconn
.vimconnNotFoundException("Flavor with EPA still not implemted")
417 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
419 # numas = extended.get("numas")
420 for flavor
in self
.nova
.flavors
.list():
421 epa
= flavor
.get_keys()
425 flavor_data
= (flavor
.ram
, flavor
.vcpus
, flavor
.disk
)
426 if flavor_data
== flavor_target
:
428 elif not exact_match
and flavor_target
< flavor_data
< flavor_candidate_data
:
429 flavor_candidate_id
= flavor
.id
430 flavor_candidate_data
= flavor_data
431 if not exact_match
and flavor_candidate_id
:
432 return flavor_candidate_id
433 raise vimconn
.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict
)))
434 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
435 self
._format
_exception
(e
)
438 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
439 '''Adds a tenant flavor to openstack VIM
440 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
441 Returns the flavor identifier
443 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
447 name
=flavor_data
['name']
448 while retry
<max_retries
:
451 self
._reload
_connection
()
452 if change_name_if_used
:
455 fl
=self
.nova
.flavors
.list()
457 fl_names
.append(f
.name
)
458 while name
in fl_names
:
460 name
= flavor_data
['name']+"-" + str(name_suffix
)
462 ram
= flavor_data
.get('ram',64)
463 vcpus
= flavor_data
.get('vcpus',1)
466 extended
= flavor_data
.get("extended")
468 numas
=extended
.get("numas")
470 numa_nodes
= len(numas
)
472 return -1, "Can not add flavor with more than one numa"
473 numa_properties
= {"hw:numa_nodes":str(numa_nodes
)}
474 numa_properties
["hw:mem_page_size"] = "large"
475 numa_properties
["hw:cpu_policy"] = "dedicated"
476 numa_properties
["hw:numa_mempolicy"] = "strict"
478 #overwrite ram and vcpus
479 ram
= numa
['memory']*1024
480 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
481 if 'paired-threads' in numa
:
482 vcpus
= numa
['paired-threads']*2
483 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
484 numa_properties
["hw:cpu_thread_policy"] = "require"
485 numa_properties
["hw:cpu_policy"] = "dedicated"
486 elif 'cores' in numa
:
487 vcpus
= numa
['cores']
488 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
489 numa_properties
["hw:cpu_thread_policy"] = "isolate"
490 numa_properties
["hw:cpu_policy"] = "dedicated"
491 elif 'threads' in numa
:
492 vcpus
= numa
['threads']
493 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
494 numa_properties
["hw:cpu_thread_policy"] = "prefer"
495 numa_properties
["hw:cpu_policy"] = "dedicated"
496 # for interface in numa.get("interfaces",() ):
497 # if interface["dedicated"]=="yes":
498 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
499 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
502 new_flavor
=self
.nova
.flavors
.create(name
,
505 flavor_data
.get('disk',1),
506 is_public
=flavor_data
.get('is_public', True)
510 new_flavor
.set_keys(numa_properties
)
512 except nvExceptions
.Conflict
as e
:
513 if change_name_if_used
and retry
< max_retries
:
515 self
._format
_exception
(e
)
516 #except nvExceptions.BadRequest as e:
517 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
518 self
._format
_exception
(e
)
520 def delete_flavor(self
,flavor_id
):
521 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
524 self
._reload
_connection
()
525 self
.nova
.flavors
.delete(flavor_id
)
527 #except nvExceptions.BadRequest as e:
528 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
529 self
._format
_exception
(e
)
531 def new_image(self
,image_dict
):
533 Adds a tenant image to VIM. imge_dict is a dictionary with:
535 disk_format: qcow2, vhd, vmdk, raw (by default), ...
536 location: path or URI
537 public: "yes" or "no"
538 metadata: metadata of the image
541 # ALF TODO: revise and change for the new method or session
542 #using version 1 of glance client
543 glancev1
= gl1Client
.Client('1',self
.glance_endpoint
, token
=self
.keystone
.auth_token
, **self
.k_creds
) #TODO check k_creds vs n_creds
546 while retry
<max_retries
:
549 self
._reload
_connection
()
550 #determine format http://docs.openstack.org/developer/glance/formats.html
551 if "disk_format" in image_dict
:
552 disk_format
=image_dict
["disk_format"]
553 else: #autodiscover based on extension
554 if image_dict
['location'][-6:]==".qcow2":
556 elif image_dict
['location'][-4:]==".vhd":
558 elif image_dict
['location'][-5:]==".vmdk":
560 elif image_dict
['location'][-4:]==".vdi":
562 elif image_dict
['location'][-4:]==".iso":
564 elif image_dict
['location'][-4:]==".aki":
566 elif image_dict
['location'][-4:]==".ari":
568 elif image_dict
['location'][-4:]==".ami":
572 self
.logger
.debug("new_image: '%s' loading from '%s'", image_dict
['name'], image_dict
['location'])
573 if image_dict
['location'][0:4]=="http":
574 new_image
= glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
575 container_format
="bare", location
=image_dict
['location'], disk_format
=disk_format
)
577 with
open(image_dict
['location']) as fimage
:
578 new_image
= glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
579 container_format
="bare", data
=fimage
, disk_format
=disk_format
)
580 #insert metadata. We cannot use 'new_image.properties.setdefault'
581 #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
582 new_image_nova
=self
.nova
.images
.find(id=new_image
.id)
583 new_image_nova
.metadata
.setdefault('location',image_dict
['location'])
584 metadata_to_load
= image_dict
.get('metadata')
586 for k
,v
in yaml
.load(metadata_to_load
).iteritems():
587 new_image_nova
.metadata
.setdefault(k
,v
)
589 except (nvExceptions
.Conflict
, ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
590 self
._format
_exception
(e
)
591 except (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
592 if retry
==max_retries
:
594 self
._format
_exception
(e
)
595 except IOError as e
: #can not open the file
596 raise vimconn
.vimconnConnectionException(type(e
).__name
__ + ": " + str(e
)+ " for " + image_dict
['location'],
597 http_code
=vimconn
.HTTP_Bad_Request
)
599 def delete_image(self
, image_id
):
600 '''Deletes a tenant image from openstack VIM. Returns the old id
603 self
._reload
_connection
()
604 self
.nova
.images
.delete(image_id
)
606 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
: #TODO remove
607 self
._format
_exception
(e
)
609 def get_image_id_from_path(self
, path
):
610 '''Get the image id from image path in the VIM database. Returns the image_id'''
612 self
._reload
_connection
()
613 images
= self
.nova
.images
.list()
615 if image
.metadata
.get("location")==path
:
617 raise vimconn
.vimconnNotFoundException("image with location '{}' not found".format( path
))
618 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
619 self
._format
_exception
(e
)
621 def get_image_list(self
, filter_dict
={}):
622 '''Obtain tenant images from VIM
626 checksum: image checksum
627 Returns the image list of dictionaries:
628 [{<the fields at Filter_dict plus some VIM specific>}, ...]
631 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
633 self
._reload
_connection
()
634 filter_dict_os
=filter_dict
.copy()
635 #First we filter by the available filter fields: name, id. The others are removed.
636 filter_dict_os
.pop('checksum',None)
637 image_list
=self
.nova
.images
.findall(**filter_dict_os
)
638 if len(image_list
)==0:
640 #Then we filter by the rest of filter fields: checksum
642 for image
in image_list
:
643 image_class
=self
.glance
.images
.get(image
.id)
644 if 'checksum' not in filter_dict
or image_class
['checksum']==filter_dict
.get('checksum'):
645 filtered_list
.append(image_class
.copy())
647 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
648 self
._format
_exception
(e
)
650 def new_vminstance(self
,name
,description
,start
,image_id
,flavor_id
,net_list
,cloud_config
=None,disk_list
=None):
651 '''Adds a VM instance to VIM
653 start: indicates if VM must start or boot in pause mode. Ignored
654 image_id,flavor_id: iamge and flavor uuid
655 net_list: list of interfaces, each one is a dictionary with:
657 net_id: network uuid to connect
658 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
659 model: interface model, ignored #TODO
660 mac_address: used for SR-IOV ifaces #TODO for other types
661 use: 'data', 'bridge', 'mgmt'
662 type: 'virtual', 'PF', 'VF', 'VFnotShared'
663 vim_id: filled/added by this function
664 floating_ip: True/False (or it can be None)
665 #TODO ip, security groups
666 Returns the instance identifier
668 self
.logger
.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id
, flavor_id
,str(net_list
))
672 external_network
=[] #list of external networks to be connected to instance, later on used to create floating_ip
673 self
._reload
_connection
()
674 metadata_vpci
={} #For a specific neutron plugin
676 if not net
.get("net_id"): #skip non connected iface
680 "network_id": net
["net_id"],
681 "name": net
.get("name"),
682 "admin_state_up": True
684 if net
["type"]=="virtual":
686 metadata_vpci
[ net
["net_id"] ] = [[ net
["vpci"], "" ]]
687 elif net
["type"]=="VF": # for VF
689 if "VF" not in metadata_vpci
:
690 metadata_vpci
["VF"]=[]
691 metadata_vpci
["VF"].append([ net
["vpci"], "" ])
692 port_dict
["binding:vnic_type"]="direct"
695 if "PF" not in metadata_vpci
:
696 metadata_vpci
["PF"]=[]
697 metadata_vpci
["PF"].append([ net
["vpci"], "" ])
698 port_dict
["binding:vnic_type"]="direct-physical"
699 if not port_dict
["name"]:
700 port_dict
["name"]=name
701 if net
.get("mac_address"):
702 port_dict
["mac_address"]=net
["mac_address"]
703 if net
.get("port_security") == False:
704 port_dict
["port_security_enabled"]=net
["port_security"]
705 new_port
= self
.neutron
.create_port({"port": port_dict
})
706 net
["mac_adress"] = new_port
["port"]["mac_address"]
707 net
["vim_id"] = new_port
["port"]["id"]
708 net
["ip"] = new_port
["port"].get("fixed_ips", [{}])[0].get("ip_address")
709 net_list_vim
.append({"port-id": new_port
["port"]["id"]})
711 if net
.get('floating_ip', False):
712 net
['exit_on_floating_ip_error'] = True
713 external_network
.append(net
)
714 elif net
['use'] == 'mgmt' and self
.config
.get('use_floating_ip'):
715 net
['exit_on_floating_ip_error'] = False
716 external_network
.append(net
)
719 metadata
= {"pci_assignement": json
.dumps(metadata_vpci
)}
720 if len(metadata
["pci_assignement"]) >255:
721 #limit the metadata size
722 #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
723 self
.logger
.warn("Metadata deleted since it exceeds the expected length (255) ")
726 self
.logger
.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
727 name
, image_id
, flavor_id
, str(net_list_vim
), description
, str(metadata
))
729 security_groups
= self
.config
.get('security_groups')
730 if type(security_groups
) is str:
731 security_groups
= ( security_groups
, )
735 if isinstance(cloud_config
, dict):
736 if cloud_config
.get("user-data"):
737 userdata
=cloud_config
["user-data"]
738 if cloud_config
.get("boot-data-drive") != None:
739 config_drive
= cloud_config
["boot-data-drive"]
740 if cloud_config
.get("config-files") or cloud_config
.get("users") or cloud_config
.get("key-pairs"):
742 raise vimconn
.vimconnConflictException("Cloud-config cannot contain both 'userdata' and 'config-files'/'users'/'key-pairs'")
745 if cloud_config
.get("key-pairs"):
746 userdata_dict
["ssh-authorized-keys"] = cloud_config
["key-pairs"]
747 userdata_dict
["users"] = [{"default": None, "ssh-authorized-keys": cloud_config
["key-pairs"] }]
748 if cloud_config
.get("users"):
749 if "users" not in userdata_dict
:
750 userdata_dict
["users"] = [ "default" ]
751 for user
in cloud_config
["users"]:
753 "name" : user
["name"],
754 "sudo": "ALL = (ALL)NOPASSWD:ALL"
756 if "user-info" in user
:
757 user_info
["gecos"] = user
["user-info"]
758 if user
.get("key-pairs"):
759 user_info
["ssh-authorized-keys"] = user
["key-pairs"]
760 userdata_dict
["users"].append(user_info
)
762 if cloud_config
.get("config-files"):
763 userdata_dict
["write_files"] = []
764 for file in cloud_config
["config-files"]:
766 "path" : file["dest"],
767 "content": file["content"]
769 if file.get("encoding"):
770 file_info
["encoding"] = file["encoding"]
771 if file.get("permissions"):
772 file_info
["permissions"] = file["permissions"]
773 if file.get("owner"):
774 file_info
["owner"] = file["owner"]
775 userdata_dict
["write_files"].append(file_info
)
776 userdata
= "#cloud-config\n"
777 userdata
+= yaml
.safe_dump(userdata_dict
, indent
=4, default_flow_style
=False)
778 self
.logger
.debug("userdata: %s", userdata
)
779 elif isinstance(cloud_config
, str):
780 userdata
= cloud_config
782 #Create additional volumes in case these are present in disk_list
783 block_device_mapping
= None
784 base_disk_index
= ord('b')
785 if disk_list
!= None:
786 block_device_mapping
= dict()
787 for disk
in disk_list
:
788 if 'image_id' in disk
:
789 volume
= self
.cinder
.volumes
.create(size
= disk
['size'],name
= name
+ '_vd' +
790 chr(base_disk_index
), imageRef
= disk
['image_id'])
792 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
793 chr(base_disk_index
))
794 block_device_mapping
['_vd' + chr(base_disk_index
)] = volume
.id
797 #wait until volumes are with status available
800 while keep_waiting
and elapsed_time
< volume_timeout
:
802 for volume_id
in block_device_mapping
.itervalues():
803 if self
.cinder
.volumes
.get(volume_id
).status
!= 'available':
809 #if we exceeded the timeout rollback
810 if elapsed_time
>= volume_timeout
:
811 #delete the volumes we just created
812 for volume_id
in block_device_mapping
.itervalues():
813 self
.cinder
.volumes
.delete(volume_id
)
815 #delete ports we just created
816 for net_item
in net_list_vim
:
817 if 'port-id' in net_item
:
818 self
.neutron
.delete_port(net_item
['port-id'])
820 raise vimconn
.vimconnException('Timeout creating volumes for instance ' + name
,
821 http_code
=vimconn
.HTTP_Request_Timeout
)
823 server
= self
.nova
.servers
.create(name
, image_id
, flavor_id
, nics
=net_list_vim
, meta
=metadata
,
824 security_groups
=security_groups
,
825 availability_zone
=self
.config
.get('availability_zone'),
826 key_name
=self
.config
.get('keypair'),
828 config_drive
= config_drive
,
829 block_device_mapping
= block_device_mapping
830 ) # , description=description)
831 #print "DONE :-)", server
833 floating_ips
= self
.neutron
.list_floatingips().get("floatingips", ())
834 for floating_network
in external_network
:
836 # wait until vm is active
838 while elapsed_time
< server_timeout
:
839 status
= self
.nova
.servers
.get(server
.id).status
840 if status
== 'ACTIVE':
845 #if we exceeded the timeout rollback
846 if elapsed_time
>= server_timeout
:
847 raise vimconn
.vimconnException('Timeout creating instance ' + name
,
848 http_code
=vimconn
.HTTP_Request_Timeout
)
851 while(assigned
== False):
853 ip
= floating_ips
.pop(0)
854 if not ip
.get("port_id", False) and ip
.get('tenant_id') == server
.tenant_id
:
855 free_floating_ip
= ip
.get("floating_ip_address")
857 fix_ip
= floating_network
.get('ip')
858 server
.add_floating_ip(free_floating_ip
, fix_ip
)
860 except Exception as e
:
861 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot create floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
863 #Find the external network
864 external_nets
= list()
865 for net
in self
.neutron
.list_networks()['networks']:
866 if net
['router:external']:
867 external_nets
.append(net
)
869 if len(external_nets
) == 0:
870 raise vimconn
.vimconnException("Cannot create floating_ip automatically since no external "
871 "network is present",
872 http_code
=vimconn
.HTTP_Conflict
)
873 if len(external_nets
) > 1:
874 raise vimconn
.vimconnException("Cannot create floating_ip automatically since multiple "
875 "external networks are present",
876 http_code
=vimconn
.HTTP_Conflict
)
878 pool_id
= external_nets
[0].get('id')
879 param
= {'floatingip': {'floating_network_id': pool_id
, 'tenant_id': server
.tenant_id
}}
881 #self.logger.debug("Creating floating IP")
882 new_floating_ip
= self
.neutron
.create_floatingip(param
)
883 free_floating_ip
= new_floating_ip
['floatingip']['floating_ip_address']
884 fix_ip
= floating_network
.get('ip')
885 server
.add_floating_ip(free_floating_ip
, fix_ip
)
887 except Exception as e
:
888 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot assign floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
889 except Exception as e
:
890 if not floating_network
['exit_on_floating_ip_error']:
891 self
.logger
.warn("Cannot create floating_ip. %s", str(e
))
893 self
.delete_vminstance(server
.id)
897 # except nvExceptions.NotFound as e:
898 # error_value=-vimconn.HTTP_Not_Found
899 # error_text= "vm instance %s not found" % vm_id
900 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
901 # delete the volumes we just created
902 if block_device_mapping
!= None:
903 for volume_id
in block_device_mapping
.itervalues():
904 self
.cinder
.volumes
.delete(volume_id
)
906 # delete ports we just created
907 for net_item
in net_list_vim
:
908 if 'port-id' in net_item
:
909 self
.neutron
.delete_port(net_item
['port-id'])
910 self
._format
_exception
(e
)
911 except TypeError as e
:
912 raise vimconn
.vimconnException(type(e
).__name
__ + ": "+ str(e
), http_code
=vimconn
.HTTP_Bad_Request
)
914 def get_vminstance(self
,vm_id
):
915 '''Returns the VM instance information from VIM'''
916 #self.logger.debug("Getting VM from VIM")
918 self
._reload
_connection
()
919 server
= self
.nova
.servers
.find(id=vm_id
)
920 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
921 return server
.to_dict()
922 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
923 self
._format
_exception
(e
)
925 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
927 Get a console for the virtual machine
929 vm_id: uuid of the VM
930 console_type, can be:
931 "novnc" (by default), "xvpvnc" for VNC types,
932 "rdp-html5" for RDP types, "spice-html5" for SPICE types
933 Returns dict with the console parameters:
934 protocol: ssh, ftp, http, https, ...
935 server: usually ip address
936 port: the http, ssh, ... port
937 suffix: extra text, e.g. the http path and query string
939 self
.logger
.debug("Getting VM CONSOLE from VIM")
941 self
._reload
_connection
()
942 server
= self
.nova
.servers
.find(id=vm_id
)
943 if console_type
== None or console_type
== "novnc":
944 console_dict
= server
.get_vnc_console("novnc")
945 elif console_type
== "xvpvnc":
946 console_dict
= server
.get_vnc_console(console_type
)
947 elif console_type
== "rdp-html5":
948 console_dict
= server
.get_rdp_console(console_type
)
949 elif console_type
== "spice-html5":
950 console_dict
= server
.get_spice_console(console_type
)
952 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
), http_code
=vimconn
.HTTP_Bad_Request
)
954 console_dict1
= console_dict
.get("console")
956 console_url
= console_dict1
.get("url")
959 protocol_index
= console_url
.find("//")
960 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
961 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
962 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
963 return -vimconn
.HTTP_Internal_Server_Error
, "Unexpected response from VIM"
964 console_dict
={"protocol": console_url
[0:protocol_index
],
965 "server": console_url
[protocol_index
+2:port_index
],
966 "port": console_url
[port_index
:suffix_index
],
967 "suffix": console_url
[suffix_index
+1:]
971 raise vimconn
.vimconnUnexpectedResponse("Unexpected response from VIM")
973 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.BadRequest
, ConnectionError
) as e
:
974 self
._format
_exception
(e
)
976 def delete_vminstance(self
, vm_id
):
977 '''Removes a VM instance from VIM. Returns the old identifier
979 #print "osconnector: Getting VM from VIM"
981 self
._reload
_connection
()
982 #delete VM ports attached to this networks before the virtual machine
983 ports
= self
.neutron
.list_ports(device_id
=vm_id
)
984 for p
in ports
['ports']:
986 self
.neutron
.delete_port(p
["id"])
987 except Exception as e
:
988 self
.logger
.error("Error deleting port: " + type(e
).__name
__ + ": "+ str(e
))
990 #commented because detaching the volumes makes the servers.delete not work properly ?!?
991 #dettach volumes attached
992 server
= self
.nova
.servers
.get(vm_id
)
993 volumes_attached_dict
= server
._info
['os-extended-volumes:volumes_attached']
994 #for volume in volumes_attached_dict:
995 # self.cinder.volumes.detach(volume['id'])
997 self
.nova
.servers
.delete(vm_id
)
1000 #Although having detached them should have them in active status
1001 #we ensure in this loop
1004 while keep_waiting
and elapsed_time
< volume_timeout
:
1005 keep_waiting
= False
1006 for volume
in volumes_attached_dict
:
1007 if self
.cinder
.volumes
.get(volume
['id']).status
!= 'available':
1010 self
.cinder
.volumes
.delete(volume
['id'])
1016 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
1017 self
._format
_exception
(e
)
1018 #TODO insert exception vimconn.HTTP_Unauthorized
1019 #if reaching here is because an exception
1021 def refresh_vms_status(self
, vm_list
):
1022 '''Get the status of the virtual machines and their interfaces/ports
1023 Params: the list of VM identifiers
1024 Returns a dictionary with:
1025 vm_id: #VIM id of this Virtual Machine
1026 status: #Mandatory. Text with one of:
1027 # DELETED (not found at vim)
1028 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1029 # OTHER (Vim reported other status not understood)
1030 # ERROR (VIM indicates an ERROR status)
1031 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1032 # CREATING (on building process), ERROR
1033 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1035 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1036 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1038 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1039 mac_address: #Text format XX:XX:XX:XX:XX:XX
1040 vim_net_id: #network id where this interface is connected
1041 vim_interface_id: #interface/port VIM id
1042 ip_address: #null, or text with IPv4, IPv6 address
1043 compute_node: #identification of compute node where PF,VF interface is allocated
1044 pci: #PCI address of the NIC that hosts the PF,VF
1045 vlan: #physical VLAN used for VF
1048 self
.logger
.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1049 for vm_id
in vm_list
:
1052 vm_vim
= self
.get_vminstance(vm_id
)
1053 if vm_vim
['status'] in vmStatus2manoFormat
:
1054 vm
['status'] = vmStatus2manoFormat
[ vm_vim
['status'] ]
1056 vm
['status'] = "OTHER"
1057 vm
['error_msg'] = "VIM status reported " + vm_vim
['status']
1059 vm
['vim_info'] = yaml
.safe_dump(vm_vim
, default_flow_style
=True, width
=256)
1060 except yaml
.representer
.RepresenterError
:
1061 vm
['vim_info'] = str(vm_vim
)
1062 vm
["interfaces"] = []
1063 if vm_vim
.get('fault'):
1064 vm
['error_msg'] = str(vm_vim
['fault'])
1067 self
._reload
_connection
()
1068 port_dict
=self
.neutron
.list_ports(device_id
=vm_id
)
1069 for port
in port_dict
["ports"]:
1072 interface
['vim_info'] = yaml
.safe_dump(port
, default_flow_style
=True, width
=256)
1073 except yaml
.representer
.RepresenterError
:
1074 interface
['vim_info'] = str(port
)
1075 interface
["mac_address"] = port
.get("mac_address")
1076 interface
["vim_net_id"] = port
["network_id"]
1077 interface
["vim_interface_id"] = port
["id"]
1078 # check if OS-EXT-SRV-ATTR:host is there,
1079 # in case of non-admin credentials, it will be missing
1080 if vm_vim
.get('OS-EXT-SRV-ATTR:host'):
1081 interface
["compute_node"] = vm_vim
['OS-EXT-SRV-ATTR:host']
1082 interface
["pci"] = None
1084 # check if binding:profile is there,
1085 # in case of non-admin credentials, it will be missing
1086 if port
.get('binding:profile'):
1087 if port
['binding:profile'].get('pci_slot'):
1088 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1089 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1090 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1091 pci
= port
['binding:profile']['pci_slot']
1092 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1093 interface
["pci"] = pci
1094 interface
["vlan"] = None
1095 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
1096 network
= self
.neutron
.show_network(port
["network_id"])
1097 if network
['network'].get('provider:network_type') == 'vlan' and \
1098 port
.get("binding:vnic_type") == "direct":
1099 interface
["vlan"] = network
['network'].get('provider:segmentation_id')
1101 #look for floating ip address
1102 floating_ip_dict
= self
.neutron
.list_floatingips(port_id
=port
["id"])
1103 if floating_ip_dict
.get("floatingips"):
1104 ips
.append(floating_ip_dict
["floatingips"][0].get("floating_ip_address") )
1106 for subnet
in port
["fixed_ips"]:
1107 ips
.append(subnet
["ip_address"])
1108 interface
["ip_address"] = ";".join(ips
)
1109 vm
["interfaces"].append(interface
)
1110 except Exception as e
:
1111 self
.logger
.error("Error getting vm interface information " + type(e
).__name
__ + ": "+ str(e
))
1112 except vimconn
.vimconnNotFoundException
as e
:
1113 self
.logger
.error("Exception getting vm status: %s", str(e
))
1114 vm
['status'] = "DELETED"
1115 vm
['error_msg'] = str(e
)
1116 except vimconn
.vimconnException
as e
:
1117 self
.logger
.error("Exception getting vm status: %s", str(e
))
1118 vm
['status'] = "VIM_ERROR"
1119 vm
['error_msg'] = str(e
)
1123 def action_vminstance(self
, vm_id
, action_dict
):
1124 '''Send and action over a VM instance from VIM
1125 Returns the vm_id if the action was successfully sent to the VIM'''
1126 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
1128 self
._reload
_connection
()
1129 server
= self
.nova
.servers
.find(id=vm_id
)
1130 if "start" in action_dict
:
1131 if action_dict
["start"]=="rebuild":
1134 if server
.status
=="PAUSED":
1136 elif server
.status
=="SUSPENDED":
1138 elif server
.status
=="SHUTOFF":
1140 elif "pause" in action_dict
:
1142 elif "resume" in action_dict
:
1144 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
1146 elif "forceOff" in action_dict
:
1148 elif "terminate" in action_dict
:
1150 elif "createImage" in action_dict
:
1151 server
.create_image()
1152 #"path":path_schema,
1153 #"description":description_schema,
1154 #"name":name_schema,
1155 #"metadata":metadata_schema,
1156 #"imageRef": id_schema,
1157 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1158 elif "rebuild" in action_dict
:
1159 server
.rebuild(server
.image
['id'])
1160 elif "reboot" in action_dict
:
1161 server
.reboot() #reboot_type='SOFT'
1162 elif "console" in action_dict
:
1163 console_type
= action_dict
["console"]
1164 if console_type
== None or console_type
== "novnc":
1165 console_dict
= server
.get_vnc_console("novnc")
1166 elif console_type
== "xvpvnc":
1167 console_dict
= server
.get_vnc_console(console_type
)
1168 elif console_type
== "rdp-html5":
1169 console_dict
= server
.get_rdp_console(console_type
)
1170 elif console_type
== "spice-html5":
1171 console_dict
= server
.get_spice_console(console_type
)
1173 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
),
1174 http_code
=vimconn
.HTTP_Bad_Request
)
1176 console_url
= console_dict
["console"]["url"]
1178 protocol_index
= console_url
.find("//")
1179 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1180 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1181 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1182 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1183 console_dict2
={"protocol": console_url
[0:protocol_index
],
1184 "server": console_url
[protocol_index
+2 : port_index
],
1185 "port": int(console_url
[port_index
+1 : suffix_index
]),
1186 "suffix": console_url
[suffix_index
+1:]
1188 return console_dict2
1189 except Exception as e
:
1190 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1193 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1194 self
._format
_exception
(e
)
1195 #TODO insert exception vimconn.HTTP_Unauthorized
1199 def new_external_port(self
, port_data
):
1200 #TODO openstack if needed
1201 '''Adds a external port to VIM'''
1202 '''Returns the port identifier'''
1203 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.new_external_port() not implemented"
1205 def connect_port_network(self
, port_id
, network_id
, admin
=False):
1206 #TODO openstack if needed
1207 '''Connects a external port to a network'''
1208 '''Returns status code of the VIM response'''
1209 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.connect_port_network() not implemented"
1211 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
1212 '''Adds a new user to openstack VIM'''
1213 '''Returns the user identifier'''
1214 self
.logger
.debug("osconnector: Adding a new user to VIM")
1216 self
._reload
_connection
()
1217 user
=self
.keystone
.users
.create(user_name
, user_passwd
, tenant_id
=tenant_id
)
1218 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1220 except ksExceptions
.ConnectionError
as e
:
1221 error_value
=-vimconn
.HTTP_Bad_Request
1222 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1223 except ksExceptions
.ClientException
as e
: #TODO remove
1224 error_value
=-vimconn
.HTTP_Bad_Request
1225 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1226 #TODO insert exception vimconn.HTTP_Unauthorized
1227 #if reaching here is because an exception
1229 self
.logger
.debug("new_user " + error_text
)
1230 return error_value
, error_text
1232 def delete_user(self
, user_id
):
1233 '''Delete a user from openstack VIM'''
1234 '''Returns the user identifier'''
1236 print "osconnector: Deleting a user from VIM"
1238 self
._reload
_connection
()
1239 self
.keystone
.users
.delete(user_id
)
1241 except ksExceptions
.ConnectionError
as e
:
1242 error_value
=-vimconn
.HTTP_Bad_Request
1243 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1244 except ksExceptions
.NotFound
as e
:
1245 error_value
=-vimconn
.HTTP_Not_Found
1246 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1247 except ksExceptions
.ClientException
as e
: #TODO remove
1248 error_value
=-vimconn
.HTTP_Bad_Request
1249 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1250 #TODO insert exception vimconn.HTTP_Unauthorized
1251 #if reaching here is because an exception
1253 print "delete_tenant " + error_text
1254 return error_value
, error_text
1256 def get_hosts_info(self
):
1257 '''Get the information of deployed hosts
1258 Returns the hosts content'''
1260 print "osconnector: Getting Host info from VIM"
1263 self
._reload
_connection
()
1264 hypervisors
= self
.nova
.hypervisors
.list()
1265 for hype
in hypervisors
:
1266 h_list
.append( hype
.to_dict() )
1267 return 1, {"hosts":h_list
}
1268 except nvExceptions
.NotFound
as e
:
1269 error_value
=-vimconn
.HTTP_Not_Found
1270 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1271 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1272 error_value
=-vimconn
.HTTP_Bad_Request
1273 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1274 #TODO insert exception vimconn.HTTP_Unauthorized
1275 #if reaching here is because an exception
1277 print "get_hosts_info " + error_text
1278 return error_value
, error_text
1280 def get_hosts(self
, vim_tenant
):
1281 '''Get the hosts and deployed instances
1282 Returns the hosts content'''
1283 r
, hype_dict
= self
.get_hosts_info()
1286 hypervisors
= hype_dict
["hosts"]
1288 servers
= self
.nova
.servers
.list()
1289 for hype
in hypervisors
:
1290 for server
in servers
:
1291 if server
.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype
['hypervisor_hostname']:
1293 hype
['vm'].append(server
.id)
1295 hype
['vm'] = [server
.id]
1297 except nvExceptions
.NotFound
as e
:
1298 error_value
=-vimconn
.HTTP_Not_Found
1299 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1300 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1301 error_value
=-vimconn
.HTTP_Bad_Request
1302 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1303 #TODO insert exception vimconn.HTTP_Unauthorized
1304 #if reaching here is because an exception
1306 print "get_hosts " + error_text
1307 return error_value
, error_text