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_v2
, exceptions
as nvExceptions
40 from novaclient
import api_versions
41 import keystoneclient
.v2_0
.client
as ksClient_v2
42 from novaclient
.v2
.client
import Client
as nClient
43 import keystoneclient
.v3
.client
as ksClient
44 import keystoneclient
.exceptions
as ksExceptions
45 import glanceclient
.v2
.client
as glClient
46 import glanceclient
.client
as gl1Client
47 import glanceclient
.exc
as gl1Exceptions
48 import cinderclient
.v2
.client
as cClient_v2
49 from httplib
import HTTPException
50 from neutronclient
.neutron
import client
as neClient_v2
51 from neutronclient
.v2_0
import client
as neClient
52 from neutronclient
.common
import exceptions
as neExceptions
53 from requests
.exceptions
import ConnectionError
55 '''contain the openstack virtual machine status to openmano status'''
56 vmStatus2manoFormat
={'ACTIVE':'ACTIVE',
58 'SUSPENDED': 'SUSPENDED',
61 'ERROR':'ERROR','DELETED':'DELETED'
63 netStatus2manoFormat
={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
66 #global var to have a timeout creating and deleting volumes
70 class vimconnector(vimconn
.vimconnector
):
71 def __init__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
=None, user
=None, passwd
=None, log_level
=None, config
={}):
72 '''using common constructor parameters. In this case
73 'url' is the keystone authorization url,
74 'url_admin' is not use
76 self
.osc_api_version
= 'v2.0'
77 if config
.get('APIversion') == 'v3.3':
78 self
.osc_api_version
= 'v3.3'
79 vimconn
.vimconnector
.__init
__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
, user
, passwd
, log_level
, config
)
83 if self
.config
.get("insecure"):
84 self
.k_creds
["insecure"] = True
85 self
.n_creds
["insecure"] = True
87 raise TypeError, 'url param can not be NoneType'
88 self
.k_creds
['auth_url'] = url
89 self
.n_creds
['auth_url'] = url
91 self
.k_creds
['tenant_name'] = tenant_name
92 self
.n_creds
['project_id'] = tenant_name
94 self
.k_creds
['tenant_id'] = tenant_id
95 self
.n_creds
['tenant_id'] = tenant_id
97 self
.k_creds
['username'] = user
98 self
.n_creds
['username'] = user
100 self
.k_creds
['password'] = passwd
101 self
.n_creds
['api_key'] = passwd
102 if self
.osc_api_version
== 'v3.3':
103 self
.k_creds
['project_name'] = tenant_name
104 self
.k_creds
['project_id'] = tenant_id
105 if config
.get('region_name'):
106 self
.k_creds
['region_name'] = config
.get('region_name')
107 self
.n_creds
['region_name'] = config
.get('region_name')
109 self
.reload_client
= True
110 self
.logger
= logging
.getLogger('openmano.vim.openstack')
112 self
.logger
.setLevel( getattr(logging
, log_level
) )
114 def __setitem__(self
,index
, value
):
115 '''Set individuals parameters
116 Throw TypeError, KeyError
118 if index
=='tenant_id':
119 self
.reload_client
=True
120 self
.tenant_id
= value
121 if self
.osc_api_version
== 'v3.3':
123 self
.k_creds
['project_id'] = value
124 self
.n_creds
['project_id'] = value
126 del self
.k_creds
['project_id']
127 del self
.n_creds
['project_id']
130 self
.k_creds
['tenant_id'] = value
131 self
.n_creds
['tenant_id'] = value
133 del self
.k_creds
['tenant_id']
134 del self
.n_creds
['tenant_id']
135 elif index
=='tenant_name':
136 self
.reload_client
=True
137 self
.tenant_name
= value
138 if self
.osc_api_version
== 'v3.3':
140 self
.k_creds
['project_name'] = value
141 self
.n_creds
['project_name'] = value
143 del self
.k_creds
['project_name']
144 del self
.n_creds
['project_name']
147 self
.k_creds
['tenant_name'] = value
148 self
.n_creds
['project_id'] = value
150 del self
.k_creds
['tenant_name']
151 del self
.n_creds
['project_id']
153 self
.reload_client
=True
156 self
.k_creds
['username'] = value
157 self
.n_creds
['username'] = value
159 del self
.k_creds
['username']
160 del self
.n_creds
['username']
161 elif index
=='passwd':
162 self
.reload_client
=True
165 self
.k_creds
['password'] = value
166 self
.n_creds
['api_key'] = value
168 del self
.k_creds
['password']
169 del self
.n_creds
['api_key']
171 self
.reload_client
=True
174 self
.k_creds
['auth_url'] = value
175 self
.n_creds
['auth_url'] = value
177 raise TypeError, 'url param can not be NoneType'
179 vimconn
.vimconnector
.__setitem
__(self
,index
, value
)
181 def _reload_connection(self
):
182 '''Called before any operation, it check if credentials has changed
183 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
185 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
186 if self
.reload_client
:
188 if len(self
.n_creds
) <4:
189 raise ksExceptions
.ClientException("Not enough parameters to connect to openstack")
190 if self
.osc_api_version
== 'v3.3':
191 self
.nova
= nClient(api_version
=api_versions
.APIVersion(version_str
='2.0'), **self
.n_creds
)
192 #TODO To be updated for v3
193 #self.cinder = cClient.Client(**self.n_creds)
194 self
.keystone
= ksClient
.Client(**self
.k_creds
)
195 self
.ne_endpoint
=self
.keystone
.service_catalog
.url_for(service_type
='network', endpoint_type
='publicURL')
196 self
.neutron
= neClient
.Client(api_version
=api_versions
.APIVersion(version_str
='2.0'), endpoint_url
=self
.ne_endpoint
, token
=self
.keystone
.auth_token
, **self
.k_creds
)
198 self
.nova
= nClient_v2
.Client(version
='2', **self
.n_creds
)
199 self
.cinder
= cClient_v2
.Client(**self
.n_creds
)
200 self
.keystone
= ksClient_v2
.Client(**self
.k_creds
)
201 self
.ne_endpoint
=self
.keystone
.service_catalog
.url_for(service_type
='network', endpoint_type
='publicURL')
202 self
.neutron
= neClient_v2
.Client('2.0', endpoint_url
=self
.ne_endpoint
, token
=self
.keystone
.auth_token
, **self
.k_creds
)
203 self
.glance_endpoint
= self
.keystone
.service_catalog
.url_for(service_type
='image', endpoint_type
='publicURL')
204 self
.glance
= glClient
.Client(self
.glance_endpoint
, token
=self
.keystone
.auth_token
, **self
.k_creds
) #TODO check k_creds vs n_creds
205 self
.reload_client
= False
207 def __net_os2mano(self
, net_list_dict
):
208 '''Transform the net openstack format to mano format
209 net_list_dict can be a list of dict or a single dict'''
210 if type(net_list_dict
) is dict:
211 net_list_
=(net_list_dict
,)
212 elif type(net_list_dict
) is list:
213 net_list_
=net_list_dict
215 raise TypeError("param net_list_dict must be a list or a dictionary")
216 for net
in net_list_
:
217 if net
.get('provider:network_type') == "vlan":
224 def _format_exception(self
, exception
):
225 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
226 if isinstance(exception
, (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
,
227 ConnectionError
, ksExceptions
.ConnectionError
, neExceptions
.ConnectionFailed
229 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
230 elif isinstance(exception
, (nvExceptions
.ClientException
, ksExceptions
.ClientException
,
231 neExceptions
.NeutronException
, nvExceptions
.BadRequest
)):
232 raise vimconn
.vimconnUnexpectedResponse(type(exception
).__name
__ + ": " + str(exception
))
233 elif isinstance(exception
, (neExceptions
.NetworkNotFoundClient
, nvExceptions
.NotFound
)):
234 raise vimconn
.vimconnNotFoundException(type(exception
).__name
__ + ": " + str(exception
))
235 elif isinstance(exception
, nvExceptions
.Conflict
):
236 raise vimconn
.vimconnConflictException(type(exception
).__name
__ + ": " + str(exception
))
238 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
240 def get_tenant_list(self
, filter_dict
={}):
241 '''Obtain tenants of VIM
242 filter_dict can contain the following keys:
243 name: filter by tenant name
244 id: filter by tenant uuid/id
246 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
248 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
250 self
._reload
_connection
()
251 if self
.osc_api_version
== 'v3.3':
252 project_class_list
=self
.keystone
.projects
.findall(**filter_dict
)
254 project_class_list
=self
.keystone
.tenants
.findall(**filter_dict
)
256 for project
in project_class_list
:
257 project_list
.append(project
.to_dict())
259 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
260 self
._format
_exception
(e
)
262 def new_tenant(self
, tenant_name
, tenant_description
):
263 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
264 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
266 self
._reload
_connection
()
267 if self
.osc_api_version
== 'v3.3':
268 project
=self
.keystone
.projects
.create(tenant_name
, tenant_description
)
270 project
=self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
272 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
273 self
._format
_exception
(e
)
275 def delete_tenant(self
, tenant_id
):
276 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
277 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
279 self
._reload
_connection
()
280 if self
.osc_api_version
== 'v3.3':
281 self
.keystone
.projects
.delete(tenant_id
)
283 self
.keystone
.tenants
.delete(tenant_id
)
285 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
286 self
._format
_exception
(e
)
288 def new_network(self
,net_name
, net_type
, ip_profile
=None, shared
=False, vlan
=None):
289 '''Adds a tenant network to VIM. Returns the network identifier'''
290 self
.logger
.debug("Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
)
291 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
294 self
._reload
_connection
()
295 network_dict
= {'name': net_name
, 'admin_state_up': True}
296 if net_type
=="data" or net_type
=="ptp":
297 if self
.config
.get('dataplane_physical_net') == None:
298 raise vimconn
.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
299 network_dict
["provider:physical_network"] = self
.config
['dataplane_physical_net'] #"physnet_sriov" #TODO physical
300 network_dict
["provider:network_type"] = "vlan"
302 network_dict
["provider:network_type"] = vlan
303 network_dict
["shared"]=shared
304 new_net
=self
.neutron
.create_network({'network':network_dict
})
306 #create subnetwork, even if there is no profile
309 if 'subnet_address' not in ip_profile
:
310 #Fake subnet is required
311 subnet_rand
= random
.randint(0, 255)
312 ip_profile
['subnet_address'] = "192.168.{}.0/24".format(subnet_rand
)
313 if 'ip_version' not in ip_profile
:
314 ip_profile
['ip_version'] = "IPv4"
315 subnet
={"name":net_name
+"-subnet",
316 "network_id": new_net
["network"]["id"],
317 "ip_version": 4 if ip_profile
['ip_version']=="IPv4" else 6,
318 "cidr": ip_profile
['subnet_address']
320 if 'gateway_address' in ip_profile
:
321 subnet
['gateway_ip'] = ip_profile
['gateway_address']
322 if ip_profile
.get('dns_address'):
323 #TODO: manage dns_address as a list of addresses separated by commas
324 subnet
['dns_nameservers'] = []
325 subnet
['dns_nameservers'].append(ip_profile
['dns_address'])
326 if 'dhcp_enabled' in ip_profile
:
327 subnet
['enable_dhcp'] = False if ip_profile
['dhcp_enabled']=="false" else True
328 if 'dhcp_start_address' in ip_profile
:
329 subnet
['allocation_pools']=[]
330 subnet
['allocation_pools'].append(dict())
331 subnet
['allocation_pools'][0]['start'] = ip_profile
['dhcp_start_address']
332 if 'dhcp_count' in ip_profile
:
333 #parts = ip_profile['dhcp_start_address'].split('.')
334 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
335 ip_int
= int(netaddr
.IPAddress(ip_profile
['dhcp_start_address']))
336 ip_int
+= ip_profile
['dhcp_count'] - 1
337 ip_str
= str(netaddr
.IPAddress(ip_int
))
338 subnet
['allocation_pools'][0]['end'] = ip_str
339 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
340 self
.neutron
.create_subnet({"subnet": subnet
} )
341 return new_net
["network"]["id"]
342 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
344 self
.neutron
.delete_network(new_net
['network']['id'])
345 self
._format
_exception
(e
)
347 def get_network_list(self
, filter_dict
={}):
348 '''Obtain tenant networks of VIM
354 admin_state_up: boolean
356 Returns the network list of dictionaries
358 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
360 self
._reload
_connection
()
361 if self
.osc_api_version
== 'v3.3' and "tenant_id" in filter_dict
:
362 filter_dict
['project_id'] = filter_dict
.pop('tenant_id')
363 net_dict
=self
.neutron
.list_networks(**filter_dict
)
364 net_list
=net_dict
["networks"]
365 self
.__net
_os
2mano
(net_list
)
367 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
368 self
._format
_exception
(e
)
370 def get_network(self
, net_id
):
371 '''Obtain details of network from VIM
372 Returns the network information from a network id'''
373 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
374 filter_dict
={"id": net_id
}
375 net_list
= self
.get_network_list(filter_dict
)
377 raise vimconn
.vimconnNotFoundException("Network '{}' not found".format(net_id
))
378 elif len(net_list
)>1:
379 raise vimconn
.vimconnConflictException("Found more than one network with this criteria")
382 for subnet_id
in net
.get("subnets", () ):
384 subnet
= self
.neutron
.show_subnet(subnet_id
)
385 except Exception as e
:
386 self
.logger
.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id
, str(e
)))
387 subnet
= {"id": subnet_id
, "fault": str(e
)}
388 subnets
.append(subnet
)
389 net
["subnets"] = subnets
392 def delete_network(self
, net_id
):
393 '''Deletes a tenant network from VIM. Returns the old network identifier'''
394 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
396 self
._reload
_connection
()
397 #delete VM ports attached to this networks before the network
398 ports
= self
.neutron
.list_ports(network_id
=net_id
)
399 for p
in ports
['ports']:
401 self
.neutron
.delete_port(p
["id"])
402 except Exception as e
:
403 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
404 self
.neutron
.delete_network(net_id
)
406 except (neExceptions
.ConnectionFailed
, neExceptions
.NetworkNotFoundClient
, neExceptions
.NeutronException
,
407 ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
408 self
._format
_exception
(e
)
410 def refresh_nets_status(self
, net_list
):
411 '''Get the status of the networks
412 Params: the list of network identifiers
413 Returns a dictionary with:
414 net_id: #VIM id of this network
415 status: #Mandatory. Text with one of:
416 # DELETED (not found at vim)
417 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
418 # OTHER (Vim reported other status not understood)
419 # ERROR (VIM indicates an ERROR status)
420 # ACTIVE, INACTIVE, DOWN (admin down),
421 # BUILD (on building process)
423 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
424 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
428 for net_id
in net_list
:
431 net_vim
= self
.get_network(net_id
)
432 if net_vim
['status'] in netStatus2manoFormat
:
433 net
["status"] = netStatus2manoFormat
[ net_vim
['status'] ]
435 net
["status"] = "OTHER"
436 net
["error_msg"] = "VIM status reported " + net_vim
['status']
438 if net
['status'] == "ACTIVE" and not net_vim
['admin_state_up']:
439 net
['status'] = 'DOWN'
441 net
['vim_info'] = yaml
.safe_dump(net_vim
, default_flow_style
=True, width
=256)
442 except yaml
.representer
.RepresenterError
:
443 net
['vim_info'] = str(net_vim
)
444 if net_vim
.get('fault'): #TODO
445 net
['error_msg'] = str(net_vim
['fault'])
446 except vimconn
.vimconnNotFoundException
as e
:
447 self
.logger
.error("Exception getting net status: %s", str(e
))
448 net
['status'] = "DELETED"
449 net
['error_msg'] = str(e
)
450 except vimconn
.vimconnException
as e
:
451 self
.logger
.error("Exception getting net status: %s", str(e
))
452 net
['status'] = "VIM_ERROR"
453 net
['error_msg'] = str(e
)
454 net_dict
[net_id
] = net
457 def get_flavor(self
, flavor_id
):
458 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
459 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
461 self
._reload
_connection
()
462 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
463 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
464 return flavor
.to_dict()
465 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
466 self
._format
_exception
(e
)
468 def get_flavor_id_from_data(self
, flavor_dict
):
469 """Obtain flavor id that match the flavor description
470 Returns the flavor_id or raises a vimconnNotFoundException
473 self
._reload
_connection
()
475 numas
= flavor_dict
.get("extended",{}).get("numas")
478 raise vimconn
.vimconnNotFoundException("Flavor with EPA still not implemted")
480 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
482 # numas = extended.get("numas")
483 for flavor
in self
.nova
.flavors
.list():
484 epa
= flavor
.get_keys()
488 if flavor
.ram
!= flavor_dict
["ram"]:
490 if flavor
.vcpus
!= flavor_dict
["vcpus"]:
492 if flavor
.disk
!= flavor_dict
["disk"]:
495 raise vimconn
.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict
)))
496 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
497 self
._format
_exception
(e
)
500 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
501 '''Adds a tenant flavor to openstack VIM
502 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
503 Returns the flavor identifier
505 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
509 name
=flavor_data
['name']
510 while retry
<max_retries
:
513 self
._reload
_connection
()
514 if change_name_if_used
:
517 fl
=self
.nova
.flavors
.list()
519 fl_names
.append(f
.name
)
520 while name
in fl_names
:
522 name
= flavor_data
['name']+"-" + str(name_suffix
)
524 ram
= flavor_data
.get('ram',64)
525 vcpus
= flavor_data
.get('vcpus',1)
528 extended
= flavor_data
.get("extended")
530 numas
=extended
.get("numas")
532 numa_nodes
= len(numas
)
534 return -1, "Can not add flavor with more than one numa"
535 numa_properties
= {"hw:numa_nodes":str(numa_nodes
)}
536 numa_properties
["hw:mem_page_size"] = "large"
537 numa_properties
["hw:cpu_policy"] = "dedicated"
538 numa_properties
["hw:numa_mempolicy"] = "strict"
540 #overwrite ram and vcpus
541 ram
= numa
['memory']*1024
542 if 'paired-threads' in numa
:
543 vcpus
= numa
['paired-threads']*2
544 numa_properties
["hw:cpu_threads_policy"] = "prefer"
545 elif 'cores' in numa
:
546 vcpus
= numa
['cores']
547 #numa_properties["hw:cpu_threads_policy"] = "prefer"
548 elif 'threads' in numa
:
549 vcpus
= numa
['threads']
550 numa_properties
["hw:cpu_policy"] = "isolated"
551 for interface
in numa
.get("interfaces",() ):
552 if interface
["dedicated"]=="yes":
553 raise vimconn
.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code
=vimconn
.HTTP_Service_Unavailable
)
554 #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
557 new_flavor
=self
.nova
.flavors
.create(name
,
560 flavor_data
.get('disk',1),
561 is_public
=flavor_data
.get('is_public', True)
565 new_flavor
.set_keys(numa_properties
)
567 except nvExceptions
.Conflict
as e
:
568 if change_name_if_used
and retry
< max_retries
:
570 self
._format
_exception
(e
)
571 #except nvExceptions.BadRequest as e:
572 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
573 self
._format
_exception
(e
)
575 def delete_flavor(self
,flavor_id
):
576 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
579 self
._reload
_connection
()
580 self
.nova
.flavors
.delete(flavor_id
)
582 #except nvExceptions.BadRequest as e:
583 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
584 self
._format
_exception
(e
)
586 def new_image(self
,image_dict
):
588 Adds a tenant image to VIM. imge_dict is a dictionary with:
590 disk_format: qcow2, vhd, vmdk, raw (by default), ...
591 location: path or URI
592 public: "yes" or "no"
593 metadata: metadata of the image
596 #using version 1 of glance client
597 glancev1
= gl1Client
.Client('1',self
.glance_endpoint
, token
=self
.keystone
.auth_token
, **self
.k_creds
) #TODO check k_creds vs n_creds
600 while retry
<max_retries
:
603 self
._reload
_connection
()
604 #determine format http://docs.openstack.org/developer/glance/formats.html
605 if "disk_format" in image_dict
:
606 disk_format
=image_dict
["disk_format"]
607 else: #autodiscover based on extension
608 if image_dict
['location'][-6:]==".qcow2":
610 elif image_dict
['location'][-4:]==".vhd":
612 elif image_dict
['location'][-5:]==".vmdk":
614 elif image_dict
['location'][-4:]==".vdi":
616 elif image_dict
['location'][-4:]==".iso":
618 elif image_dict
['location'][-4:]==".aki":
620 elif image_dict
['location'][-4:]==".ari":
622 elif image_dict
['location'][-4:]==".ami":
626 self
.logger
.debug("new_image: '%s' loading from '%s'", image_dict
['name'], image_dict
['location'])
627 if image_dict
['location'][0:4]=="http":
628 new_image
= glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
629 container_format
="bare", location
=image_dict
['location'], disk_format
=disk_format
)
631 with
open(image_dict
['location']) as fimage
:
632 new_image
= glancev1
.images
.create(name
=image_dict
['name'], is_public
=image_dict
.get('public',"yes")=="yes",
633 container_format
="bare", data
=fimage
, disk_format
=disk_format
)
634 #insert metadata. We cannot use 'new_image.properties.setdefault'
635 #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
636 new_image_nova
=self
.nova
.images
.find(id=new_image
.id)
637 new_image_nova
.metadata
.setdefault('location',image_dict
['location'])
638 metadata_to_load
= image_dict
.get('metadata')
640 for k
,v
in yaml
.load(metadata_to_load
).iteritems():
641 new_image_nova
.metadata
.setdefault(k
,v
)
643 except (nvExceptions
.Conflict
, ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
644 self
._format
_exception
(e
)
645 except (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
646 if retry
==max_retries
:
648 self
._format
_exception
(e
)
649 except IOError as e
: #can not open the file
650 raise vimconn
.vimconnConnectionException(type(e
).__name
__ + ": " + str(e
)+ " for " + image_dict
['location'],
651 http_code
=vimconn
.HTTP_Bad_Request
)
653 def delete_image(self
, image_id
):
654 '''Deletes a tenant image from openstack VIM. Returns the old id
657 self
._reload
_connection
()
658 self
.nova
.images
.delete(image_id
)
660 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
: #TODO remove
661 self
._format
_exception
(e
)
663 def get_image_id_from_path(self
, path
):
664 '''Get the image id from image path in the VIM database. Returns the image_id'''
666 self
._reload
_connection
()
667 images
= self
.nova
.images
.list()
669 if image
.metadata
.get("location")==path
:
671 raise vimconn
.vimconnNotFoundException("image with location '{}' not found".format( path
))
672 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
673 self
._format
_exception
(e
)
675 def get_image_list(self
, filter_dict
={}):
676 '''Obtain tenant images from VIM
680 checksum: image checksum
681 Returns the image list of dictionaries:
682 [{<the fields at Filter_dict plus some VIM specific>}, ...]
685 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
687 self
._reload
_connection
()
688 filter_dict_os
=filter_dict
.copy()
689 #First we filter by the available filter fields: name, id. The others are removed.
690 filter_dict_os
.pop('checksum',None)
691 image_list
=self
.nova
.images
.findall(**filter_dict_os
)
692 if len(image_list
)==0:
694 #Then we filter by the rest of filter fields: checksum
696 for image
in image_list
:
697 image_class
=self
.glance
.images
.get(image
.id)
698 if 'checksum' not in filter_dict
or image_class
['checksum']==filter_dict
.get('checksum'):
699 filtered_list
.append(image_class
.copy())
701 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
702 self
._format
_exception
(e
)
704 def new_vminstance(self
,name
,description
,start
,image_id
,flavor_id
,net_list
,cloud_config
=None,disk_list
=None):
705 '''Adds a VM instance to VIM
707 start: indicates if VM must start or boot in pause mode. Ignored
708 image_id,flavor_id: iamge and flavor uuid
709 net_list: list of interfaces, each one is a dictionary with:
711 net_id: network uuid to connect
712 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
713 model: interface model, ignored #TODO
714 mac_address: used for SR-IOV ifaces #TODO for other types
715 use: 'data', 'bridge', 'mgmt'
716 type: 'virtual', 'PF', 'VF', 'VFnotShared'
717 vim_id: filled/added by this function
718 floating_ip: True/False (or it can be None)
719 #TODO ip, security groups
720 Returns the instance identifier
722 self
.logger
.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id
, flavor_id
,str(net_list
))
726 external_network
=[] #list of external networks to be connected to instance, later on used to create floating_ip
727 self
._reload
_connection
()
728 metadata_vpci
={} #For a specific neutron plugin
730 if not net
.get("net_id"): #skip non connected iface
732 if net
["type"]=="virtual" or net
["type"]=="VF":
734 "network_id": net
["net_id"],
735 "name": net
.get("name"),
736 "admin_state_up": True
738 if net
["type"]=="virtual":
740 metadata_vpci
[ net
["net_id"] ] = [[ net
["vpci"], "" ]]
743 if "VF" not in metadata_vpci
:
744 metadata_vpci
["VF"]=[]
745 metadata_vpci
["VF"].append([ net
["vpci"], "" ])
746 port_dict
["binding:vnic_type"]="direct"
747 if not port_dict
["name"]:
748 port_dict
["name"]=name
749 if net
.get("mac_address"):
750 port_dict
["mac_address"]=net
["mac_address"]
751 if net
.get("port_security") == False:
752 port_dict
["port_security_enabled"]=net
["port_security"]
753 new_port
= self
.neutron
.create_port({"port": port_dict
})
754 net
["mac_adress"] = new_port
["port"]["mac_address"]
755 net
["vim_id"] = new_port
["port"]["id"]
756 net
["ip"] = new_port
["port"].get("fixed_ips", [{}])[0].get("ip_address")
757 net_list_vim
.append({"port-id": new_port
["port"]["id"]})
759 self
.logger
.warn("new_vminstance: Warning, can not connect a passthrough interface ")
760 #TODO insert this when openstack consider passthrough ports as openstack neutron ports
761 if net
.get('floating_ip', False):
762 net
['exit_on_floating_ip_error'] = True
763 external_network
.append(net
)
764 elif net
['use'] == 'mgmt' and self
.config
.get('use_floating_ip'):
765 net
['exit_on_floating_ip_error'] = False
766 external_network
.append(net
)
769 metadata
= {"pci_assignement": json
.dumps(metadata_vpci
)}
770 if len(metadata
["pci_assignement"]) >255:
771 #limit the metadata size
772 #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
773 self
.logger
.warn("Metadata deleted since it exceeds the expected length (255) ")
776 self
.logger
.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
777 name
, image_id
, flavor_id
, str(net_list_vim
), description
, str(metadata
))
779 security_groups
= self
.config
.get('security_groups')
780 if type(security_groups
) is str:
781 security_groups
= ( security_groups
, )
785 if isinstance(cloud_config
, dict):
786 if cloud_config
.get("user-data"):
787 userdata
=cloud_config
["user-data"]
788 if cloud_config
.get("boot-data-drive") != None:
789 config_drive
= cloud_config
["boot-data-drive"]
790 if cloud_config
.get("config-files") or cloud_config
.get("users") or cloud_config
.get("key-pairs"):
792 raise vimconn
.vimconnConflictException("Cloud-config cannot contain both 'userdata' and 'config-files'/'users'/'key-pairs'")
795 if cloud_config
.get("key-pairs"):
796 userdata_dict
["ssh-authorized-keys"] = cloud_config
["key-pairs"]
797 userdata_dict
["users"] = [{"default": None, "ssh-authorized-keys": cloud_config
["key-pairs"] }]
798 if cloud_config
.get("users"):
799 if "users" not in userdata_dict
:
800 userdata_dict
["users"] = [ "default" ]
801 for user
in cloud_config
["users"]:
803 "name" : user
["name"],
804 "sudo": "ALL = (ALL)NOPASSWD:ALL"
806 if "user-info" in user
:
807 user_info
["gecos"] = user
["user-info"]
808 if user
.get("key-pairs"):
809 user_info
["ssh-authorized-keys"] = user
["key-pairs"]
810 userdata_dict
["users"].append(user_info
)
812 if cloud_config
.get("config-files"):
813 userdata_dict
["write_files"] = []
814 for file in cloud_config
["config-files"]:
816 "path" : file["dest"],
817 "content": file["content"]
819 if file.get("encoding"):
820 file_info
["encoding"] = file["encoding"]
821 if file.get("permissions"):
822 file_info
["permissions"] = file["permissions"]
823 if file.get("owner"):
824 file_info
["owner"] = file["owner"]
825 userdata_dict
["write_files"].append(file_info
)
826 userdata
= "#cloud-config\n"
827 userdata
+= yaml
.safe_dump(userdata_dict
, indent
=4, default_flow_style
=False)
828 self
.logger
.debug("userdata: %s", userdata
)
829 elif isinstance(cloud_config
, str):
830 userdata
= cloud_config
832 #Create additional volumes in case these are present in disk_list
833 block_device_mapping
= None
834 base_disk_index
= ord('b')
835 if disk_list
!= None:
836 block_device_mapping
= dict()
837 for disk
in disk_list
:
838 if 'image_id' in disk
:
839 volume
= self
.cinder
.volumes
.create(size
= disk
['size'],name
= name
+ '_vd' +
840 chr(base_disk_index
), imageRef
= disk
['image_id'])
842 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
843 chr(base_disk_index
))
844 block_device_mapping
['_vd' + chr(base_disk_index
)] = volume
.id
847 #wait until volumes are with status available
850 while keep_waiting
and elapsed_time
< volume_timeout
:
852 for volume_id
in block_device_mapping
.itervalues():
853 if self
.cinder
.volumes
.get(volume_id
).status
!= 'available':
859 #if we exceeded the timeout rollback
860 if elapsed_time
>= volume_timeout
:
861 #delete the volumes we just created
862 for volume_id
in block_device_mapping
.itervalues():
863 self
.cinder
.volumes
.delete(volume_id
)
865 #delete ports we just created
866 for net_item
in net_list_vim
:
867 if 'port-id' in net_item
:
868 self
.neutron
.delete_port(net_item
['port-id'])
870 raise vimconn
.vimconnException('Timeout creating volumes for instance ' + name
,
871 http_code
=vimconn
.HTTP_Request_Timeout
)
873 server
= self
.nova
.servers
.create(name
, image_id
, flavor_id
, nics
=net_list_vim
, meta
=metadata
,
874 security_groups
=security_groups
,
875 availability_zone
=self
.config
.get('availability_zone'),
876 key_name
=self
.config
.get('keypair'),
878 config_drive
= config_drive
,
879 block_device_mapping
= block_device_mapping
880 ) # , description=description)
881 #print "DONE :-)", server
883 floating_ips
= self
.neutron
.list_floatingips().get("floatingips", ())
884 for floating_network
in external_network
:
886 # wait until vm is active
888 while elapsed_time
< server_timeout
:
889 status
= self
.nova
.servers
.get(server
.id).status
890 if status
== 'ACTIVE':
895 #if we exceeded the timeout rollback
896 if elapsed_time
>= server_timeout
:
897 raise vimconn
.vimconnException('Timeout creating instance ' + name
,
898 http_code
=vimconn
.HTTP_Request_Timeout
)
901 while(assigned
== False):
903 ip
= floating_ips
.pop(0)
904 if not ip
.get("port_id", False) and ip
.get('tenant_id') == server
.tenant_id
:
905 free_floating_ip
= ip
.get("floating_ip_address")
907 fix_ip
= floating_network
.get('ip')
908 server
.add_floating_ip(free_floating_ip
, fix_ip
)
910 except Exception as e
:
911 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot create floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
913 #Find the external network
914 external_nets
= list()
915 for net
in self
.neutron
.list_networks()['networks']:
916 if net
['router:external']:
917 external_nets
.append(net
)
919 if len(external_nets
) == 0:
920 raise vimconn
.vimconnException("Cannot create floating_ip automatically since no external "
921 "network is present",
922 http_code
=vimconn
.HTTP_Conflict
)
923 if len(external_nets
) > 1:
924 raise vimconn
.vimconnException("Cannot create floating_ip automatically since multiple "
925 "external networks are present",
926 http_code
=vimconn
.HTTP_Conflict
)
928 pool_id
= external_nets
[0].get('id')
929 param
= {'floatingip': {'floating_network_id': pool_id
, 'tenant_id': server
.tenant_id
}}
931 #self.logger.debug("Creating floating IP")
932 new_floating_ip
= self
.neutron
.create_floatingip(param
)
933 free_floating_ip
= new_floating_ip
['floatingip']['floating_ip_address']
934 fix_ip
= floating_network
.get('ip')
935 server
.add_floating_ip(free_floating_ip
, fix_ip
)
937 except Exception as e
:
938 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot assign floating_ip "+ str(e
), http_code
=vimconn
.HTTP_Conflict
)
939 except Exception as e
:
940 if not floating_network
['exit_on_floating_ip_error']:
941 self
.logger
.warn("Cannot create floating_ip. %s", str(e
))
943 self
.delete_vminstance(server
.id)
947 # except nvExceptions.NotFound as e:
948 # error_value=-vimconn.HTTP_Not_Found
949 # error_text= "vm instance %s not found" % vm_id
950 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
951 # delete the volumes we just created
952 if block_device_mapping
!= None:
953 for volume_id
in block_device_mapping
.itervalues():
954 self
.cinder
.volumes
.delete(volume_id
)
956 # delete ports we just created
957 for net_item
in net_list_vim
:
958 if 'port-id' in net_item
:
959 self
.neutron
.delete_port(net_item
['port-id'])
960 self
._format
_exception
(e
)
961 except TypeError as e
:
962 raise vimconn
.vimconnException(type(e
).__name
__ + ": "+ str(e
), http_code
=vimconn
.HTTP_Bad_Request
)
964 def get_vminstance(self
,vm_id
):
965 '''Returns the VM instance information from VIM'''
966 #self.logger.debug("Getting VM from VIM")
968 self
._reload
_connection
()
969 server
= self
.nova
.servers
.find(id=vm_id
)
970 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
971 return server
.to_dict()
972 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
973 self
._format
_exception
(e
)
975 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
977 Get a console for the virtual machine
979 vm_id: uuid of the VM
980 console_type, can be:
981 "novnc" (by default), "xvpvnc" for VNC types,
982 "rdp-html5" for RDP types, "spice-html5" for SPICE types
983 Returns dict with the console parameters:
984 protocol: ssh, ftp, http, https, ...
985 server: usually ip address
986 port: the http, ssh, ... port
987 suffix: extra text, e.g. the http path and query string
989 self
.logger
.debug("Getting VM CONSOLE from VIM")
991 self
._reload
_connection
()
992 server
= self
.nova
.servers
.find(id=vm_id
)
993 if console_type
== None or console_type
== "novnc":
994 console_dict
= server
.get_vnc_console("novnc")
995 elif console_type
== "xvpvnc":
996 console_dict
= server
.get_vnc_console(console_type
)
997 elif console_type
== "rdp-html5":
998 console_dict
= server
.get_rdp_console(console_type
)
999 elif console_type
== "spice-html5":
1000 console_dict
= server
.get_spice_console(console_type
)
1002 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
), http_code
=vimconn
.HTTP_Bad_Request
)
1004 console_dict1
= console_dict
.get("console")
1006 console_url
= console_dict1
.get("url")
1009 protocol_index
= console_url
.find("//")
1010 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1011 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1012 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1013 return -vimconn
.HTTP_Internal_Server_Error
, "Unexpected response from VIM"
1014 console_dict
={"protocol": console_url
[0:protocol_index
],
1015 "server": console_url
[protocol_index
+2:port_index
],
1016 "port": console_url
[port_index
:suffix_index
],
1017 "suffix": console_url
[suffix_index
+1:]
1021 raise vimconn
.vimconnUnexpectedResponse("Unexpected response from VIM")
1023 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.BadRequest
, ConnectionError
) as e
:
1024 self
._format
_exception
(e
)
1026 def delete_vminstance(self
, vm_id
):
1027 '''Removes a VM instance from VIM. Returns the old identifier
1029 #print "osconnector: Getting VM from VIM"
1031 self
._reload
_connection
()
1032 #delete VM ports attached to this networks before the virtual machine
1033 ports
= self
.neutron
.list_ports(device_id
=vm_id
)
1034 for p
in ports
['ports']:
1036 self
.neutron
.delete_port(p
["id"])
1037 except Exception as e
:
1038 self
.logger
.error("Error deleting port: " + type(e
).__name
__ + ": "+ str(e
))
1040 #commented because detaching the volumes makes the servers.delete not work properly ?!?
1041 #dettach volumes attached
1042 server
= self
.nova
.servers
.get(vm_id
)
1043 volumes_attached_dict
= server
._info
['os-extended-volumes:volumes_attached']
1044 #for volume in volumes_attached_dict:
1045 # self.cinder.volumes.detach(volume['id'])
1047 self
.nova
.servers
.delete(vm_id
)
1050 #Although having detached them should have them in active status
1051 #we ensure in this loop
1054 while keep_waiting
and elapsed_time
< volume_timeout
:
1055 keep_waiting
= False
1056 for volume
in volumes_attached_dict
:
1057 if self
.cinder
.volumes
.get(volume
['id']).status
!= 'available':
1060 self
.cinder
.volumes
.delete(volume
['id'])
1066 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
1067 self
._format
_exception
(e
)
1068 #TODO insert exception vimconn.HTTP_Unauthorized
1069 #if reaching here is because an exception
1071 def refresh_vms_status(self
, vm_list
):
1072 '''Get the status of the virtual machines and their interfaces/ports
1073 Params: the list of VM identifiers
1074 Returns a dictionary with:
1075 vm_id: #VIM id of this Virtual Machine
1076 status: #Mandatory. Text with one of:
1077 # DELETED (not found at vim)
1078 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1079 # OTHER (Vim reported other status not understood)
1080 # ERROR (VIM indicates an ERROR status)
1081 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1082 # CREATING (on building process), ERROR
1083 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1085 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1086 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1088 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1089 mac_address: #Text format XX:XX:XX:XX:XX:XX
1090 vim_net_id: #network id where this interface is connected
1091 vim_interface_id: #interface/port VIM id
1092 ip_address: #null, or text with IPv4, IPv6 address
1095 self
.logger
.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1096 for vm_id
in vm_list
:
1099 vm_vim
= self
.get_vminstance(vm_id
)
1100 if vm_vim
['status'] in vmStatus2manoFormat
:
1101 vm
['status'] = vmStatus2manoFormat
[ vm_vim
['status'] ]
1103 vm
['status'] = "OTHER"
1104 vm
['error_msg'] = "VIM status reported " + vm_vim
['status']
1106 vm
['vim_info'] = yaml
.safe_dump(vm_vim
, default_flow_style
=True, width
=256)
1107 except yaml
.representer
.RepresenterError
:
1108 vm
['vim_info'] = str(vm_vim
)
1109 vm
["interfaces"] = []
1110 if vm_vim
.get('fault'):
1111 vm
['error_msg'] = str(vm_vim
['fault'])
1114 self
._reload
_connection
()
1115 port_dict
=self
.neutron
.list_ports(device_id
=vm_id
)
1116 for port
in port_dict
["ports"]:
1119 interface
['vim_info'] = yaml
.safe_dump(port
, default_flow_style
=True, width
=256)
1120 except yaml
.representer
.RepresenterError
:
1121 interface
['vim_info'] = str(port
)
1122 interface
["mac_address"] = port
.get("mac_address")
1123 interface
["vim_net_id"] = port
["network_id"]
1124 interface
["vim_interface_id"] = port
["id"]
1126 #look for floating ip address
1127 floating_ip_dict
= self
.neutron
.list_floatingips(port_id
=port
["id"])
1128 if floating_ip_dict
.get("floatingips"):
1129 ips
.append(floating_ip_dict
["floatingips"][0].get("floating_ip_address") )
1131 for subnet
in port
["fixed_ips"]:
1132 ips
.append(subnet
["ip_address"])
1133 interface
["ip_address"] = ";".join(ips
)
1134 vm
["interfaces"].append(interface
)
1135 except Exception as e
:
1136 self
.logger
.error("Error getting vm interface information " + type(e
).__name
__ + ": "+ str(e
))
1137 except vimconn
.vimconnNotFoundException
as e
:
1138 self
.logger
.error("Exception getting vm status: %s", str(e
))
1139 vm
['status'] = "DELETED"
1140 vm
['error_msg'] = str(e
)
1141 except vimconn
.vimconnException
as e
:
1142 self
.logger
.error("Exception getting vm status: %s", str(e
))
1143 vm
['status'] = "VIM_ERROR"
1144 vm
['error_msg'] = str(e
)
1148 def action_vminstance(self
, vm_id
, action_dict
):
1149 '''Send and action over a VM instance from VIM
1150 Returns the vm_id if the action was successfully sent to the VIM'''
1151 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
1153 self
._reload
_connection
()
1154 server
= self
.nova
.servers
.find(id=vm_id
)
1155 if "start" in action_dict
:
1156 if action_dict
["start"]=="rebuild":
1159 if server
.status
=="PAUSED":
1161 elif server
.status
=="SUSPENDED":
1163 elif server
.status
=="SHUTOFF":
1165 elif "pause" in action_dict
:
1167 elif "resume" in action_dict
:
1169 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
1171 elif "forceOff" in action_dict
:
1173 elif "terminate" in action_dict
:
1175 elif "createImage" in action_dict
:
1176 server
.create_image()
1177 #"path":path_schema,
1178 #"description":description_schema,
1179 #"name":name_schema,
1180 #"metadata":metadata_schema,
1181 #"imageRef": id_schema,
1182 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1183 elif "rebuild" in action_dict
:
1184 server
.rebuild(server
.image
['id'])
1185 elif "reboot" in action_dict
:
1186 server
.reboot() #reboot_type='SOFT'
1187 elif "console" in action_dict
:
1188 console_type
= action_dict
["console"]
1189 if console_type
== None or console_type
== "novnc":
1190 console_dict
= server
.get_vnc_console("novnc")
1191 elif console_type
== "xvpvnc":
1192 console_dict
= server
.get_vnc_console(console_type
)
1193 elif console_type
== "rdp-html5":
1194 console_dict
= server
.get_rdp_console(console_type
)
1195 elif console_type
== "spice-html5":
1196 console_dict
= server
.get_spice_console(console_type
)
1198 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
),
1199 http_code
=vimconn
.HTTP_Bad_Request
)
1201 console_url
= console_dict
["console"]["url"]
1203 protocol_index
= console_url
.find("//")
1204 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1205 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1206 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1207 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1208 console_dict2
={"protocol": console_url
[0:protocol_index
],
1209 "server": console_url
[protocol_index
+2 : port_index
],
1210 "port": int(console_url
[port_index
+1 : suffix_index
]),
1211 "suffix": console_url
[suffix_index
+1:]
1213 return console_dict2
1214 except Exception as e
:
1215 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1218 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1219 self
._format
_exception
(e
)
1220 #TODO insert exception vimconn.HTTP_Unauthorized
1224 def new_external_port(self
, port_data
):
1225 #TODO openstack if needed
1226 '''Adds a external port to VIM'''
1227 '''Returns the port identifier'''
1228 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.new_external_port() not implemented"
1230 def connect_port_network(self
, port_id
, network_id
, admin
=False):
1231 #TODO openstack if needed
1232 '''Connects a external port to a network'''
1233 '''Returns status code of the VIM response'''
1234 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.connect_port_network() not implemented"
1236 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
1237 '''Adds a new user to openstack VIM'''
1238 '''Returns the user identifier'''
1239 self
.logger
.debug("osconnector: Adding a new user to VIM")
1241 self
._reload
_connection
()
1242 user
=self
.keystone
.users
.create(user_name
, user_passwd
, tenant_id
=tenant_id
)
1243 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1245 except ksExceptions
.ConnectionError
as e
:
1246 error_value
=-vimconn
.HTTP_Bad_Request
1247 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1248 except ksExceptions
.ClientException
as e
: #TODO remove
1249 error_value
=-vimconn
.HTTP_Bad_Request
1250 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1251 #TODO insert exception vimconn.HTTP_Unauthorized
1252 #if reaching here is because an exception
1254 self
.logger
.debug("new_user " + error_text
)
1255 return error_value
, error_text
1257 def delete_user(self
, user_id
):
1258 '''Delete a user from openstack VIM'''
1259 '''Returns the user identifier'''
1261 print "osconnector: Deleting a user from VIM"
1263 self
._reload
_connection
()
1264 self
.keystone
.users
.delete(user_id
)
1266 except ksExceptions
.ConnectionError
as e
:
1267 error_value
=-vimconn
.HTTP_Bad_Request
1268 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1269 except ksExceptions
.NotFound
as e
:
1270 error_value
=-vimconn
.HTTP_Not_Found
1271 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1272 except ksExceptions
.ClientException
as e
: #TODO remove
1273 error_value
=-vimconn
.HTTP_Bad_Request
1274 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1275 #TODO insert exception vimconn.HTTP_Unauthorized
1276 #if reaching here is because an exception
1278 print "delete_tenant " + error_text
1279 return error_value
, error_text
1281 def get_hosts_info(self
):
1282 '''Get the information of deployed hosts
1283 Returns the hosts content'''
1285 print "osconnector: Getting Host info from VIM"
1288 self
._reload
_connection
()
1289 hypervisors
= self
.nova
.hypervisors
.list()
1290 for hype
in hypervisors
:
1291 h_list
.append( hype
.to_dict() )
1292 return 1, {"hosts":h_list
}
1293 except nvExceptions
.NotFound
as e
:
1294 error_value
=-vimconn
.HTTP_Not_Found
1295 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1296 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1297 error_value
=-vimconn
.HTTP_Bad_Request
1298 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1299 #TODO insert exception vimconn.HTTP_Unauthorized
1300 #if reaching here is because an exception
1302 print "get_hosts_info " + error_text
1303 return error_value
, error_text
1305 def get_hosts(self
, vim_tenant
):
1306 '''Get the hosts and deployed instances
1307 Returns the hosts content'''
1308 r
, hype_dict
= self
.get_hosts_info()
1311 hypervisors
= hype_dict
["hosts"]
1313 servers
= self
.nova
.servers
.list()
1314 for hype
in hypervisors
:
1315 for server
in servers
:
1316 if server
.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype
['hypervisor_hostname']:
1318 hype
['vm'].append(server
.id)
1320 hype
['vm'] = [server
.id]
1322 except nvExceptions
.NotFound
as e
:
1323 error_value
=-vimconn
.HTTP_Not_Found
1324 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1325 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1326 error_value
=-vimconn
.HTTP_Bad_Request
1327 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1328 #TODO insert exception vimconn.HTTP_Unauthorized
1329 #if reaching here is because an exception
1331 print "get_hosts " + error_text
1332 return error_value
, error_text