1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion 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 vimconn implement an Abstract class for the vim connector plugins
26 with the definition of the method to be implemented.
29 from email
.mime
.multipart
import MIMEMultipart
30 from email
.mime
.text
import MIMEText
31 from http
import HTTPStatus
32 from io
import StringIO
42 __author__
= "Alfonso Tierno, Igor D.C."
43 __date__
= "$14-aug-2017 23:59:59$"
46 def deprecated(message
):
47 def deprecated_decorator(func
):
48 def deprecated_func(*args
, **kwargs
):
50 "{} is a deprecated function. {}".format(func
.__name
__, message
),
51 category
=DeprecationWarning,
54 warnings
.simplefilter("default", DeprecationWarning)
56 return func(*args
, **kwargs
)
58 return deprecated_func
60 return deprecated_decorator
64 HTTP_Bad_Request
= HTTPStatus
.BAD_REQUEST
.value
65 HTTP_Unauthorized
= HTTPStatus
.UNAUTHORIZED
.value
66 HTTP_Not_Found
= HTTPStatus
.NOT_FOUND
.value
67 HTTP_Method_Not_Allowed
= HTTPStatus
.METHOD_NOT_ALLOWED
.value
68 HTTP_Request_Timeout
= HTTPStatus
.REQUEST_TIMEOUT
.value
69 HTTP_Conflict
= HTTPStatus
.CONFLICT
.value
70 HTTP_Not_Implemented
= HTTPStatus
.NOT_IMPLEMENTED
.value
71 HTTP_Service_Unavailable
= HTTPStatus
.SERVICE_UNAVAILABLE
.value
72 HTTP_Internal_Server_Error
= HTTPStatus
.INTERNAL_SERVER_ERROR
.value
75 class VimConnException(Exception):
76 """Common and base class Exception for all VimConnector exceptions"""
78 def __init__(self
, message
, http_code
=HTTP_Bad_Request
):
79 Exception.__init
__(self
, message
)
80 self
.http_code
= http_code
83 class VimConnConnectionException(VimConnException
):
84 """Connectivity error with the VIM"""
86 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
87 VimConnException
.__init
__(self
, message
, http_code
)
90 class VimConnUnexpectedResponse(VimConnException
):
91 """Get an wrong response from VIM"""
93 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
94 VimConnException
.__init
__(self
, message
, http_code
)
97 class VimConnAuthException(VimConnException
):
98 """Invalid credentials or authorization to perform this action over the VIM"""
100 def __init__(self
, message
, http_code
=HTTP_Unauthorized
):
101 VimConnException
.__init
__(self
, message
, http_code
)
104 class VimConnNotFoundException(VimConnException
):
105 """The item is not found at VIM"""
107 def __init__(self
, message
, http_code
=HTTP_Not_Found
):
108 VimConnException
.__init
__(self
, message
, http_code
)
111 class VimConnConflictException(VimConnException
):
112 """There is a conflict, e.g. more item found than one"""
114 def __init__(self
, message
, http_code
=HTTP_Conflict
):
115 VimConnException
.__init
__(self
, message
, http_code
)
118 class VimConnNotSupportedException(VimConnException
):
119 """The request is not supported by connector"""
121 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
122 VimConnException
.__init
__(self
, message
, http_code
)
125 class VimConnNotImplemented(VimConnException
):
126 """The method is not implemented by the connector"""
128 def __init__(self
, message
, http_code
=HTTP_Not_Implemented
):
129 VimConnException
.__init
__(self
, message
, http_code
)
132 class VimConnInsufficientCredentials(VimConnException
):
133 """The VIM account does not have efficient permissions to perform the requested operation."""
135 def __init__(self
, message
, http_code
=HTTP_Unauthorized
):
136 VimConnException
.__init
__(self
, message
, http_code
)
140 """Abstract base class for all the VIM connector plugins
141 These plugins must implement a VimConnector class derived from this
142 and all these privated methods
160 Constructor of VIM. Raise an exception is some needed parameter is missing, but it must not do any connectivity
161 checking against the VIM
162 :param uuid: internal id of this VIM
163 :param name: name assigned to this VIM, can be used for logging
164 :param tenant_id: 'tenant_id': (only one of them is mandatory) VIM tenant to be used
165 :param tenant_name: 'tenant_name': (only one of them is mandatory) VIM tenant to be used
166 :param url: url used for normal operations
167 :param url_admin: (optional), url used for administrative tasks
168 :param user: user to access
169 :param passwd: password
170 :param log_level: provided if it should use a different log_level than the general one
171 :param config: dictionary with extra VIM information. This contains a consolidate version of VIM config
172 at VIM_ACCOUNT (attach)
173 :param persitent_info: dict where the class can store information that will be available among class
174 destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an
175 empty dict. Useful to store login/tokens information for speed up communication
181 self
.url_admin
= url_admin
182 self
.tenant_id
= tenant_id
183 self
.tenant_name
= tenant_name
186 self
.config
= config
or {}
187 self
.availability_zone
= None
188 self
.logger
= logging
.getLogger("ro.vim")
191 self
.logger
.setLevel(getattr(logging
, log_level
))
193 if not self
.url_admin
: # try to use normal url
194 self
.url_admin
= self
.url
196 def __getitem__(self
, index
):
197 if index
== "tenant_id":
198 return self
.tenant_id
200 if index
== "tenant_name":
201 return self
.tenant_name
204 elif index
== "name":
206 elif index
== "user":
208 elif index
== "passwd":
212 elif index
== "url_admin":
213 return self
.url_admin
214 elif index
== "config":
217 raise KeyError("Invalid key '{}'".format(index
))
219 def __setitem__(self
, index
, value
):
220 if index
== "tenant_id":
221 self
.tenant_id
= value
223 if index
== "tenant_name":
224 self
.tenant_name
= value
227 elif index
== "name":
229 elif index
== "user":
231 elif index
== "passwd":
235 elif index
== "url_admin":
236 self
.url_admin
= value
238 raise KeyError("Invalid key '{}'".format(index
))
241 def _create_mimemultipart(content_list
):
242 """Creates a MIMEmultipart text combining the content_list
243 :param content_list: list of text scripts to be combined
244 :return: str of the created MIMEmultipart. If the list is empty returns None, if the list contains only one
245 element MIMEmultipart is not created and this content is returned
249 elif len(content_list
) == 1:
250 return content_list
[0]
252 combined_message
= MIMEMultipart()
254 for content
in content_list
:
255 if content
.startswith("#include"):
256 mime_format
= "text/x-include-url"
257 elif content
.startswith("#include-once"):
258 mime_format
= "text/x-include-once-url"
259 elif content
.startswith("#!"):
260 mime_format
= "text/x-shellscript"
261 elif content
.startswith("#cloud-config"):
262 mime_format
= "text/cloud-config"
263 elif content
.startswith("#cloud-config-archive"):
264 mime_format
= "text/cloud-config-archive"
265 elif content
.startswith("#upstart-job"):
266 mime_format
= "text/upstart-job"
267 elif content
.startswith("#part-handler"):
268 mime_format
= "text/part-handler"
269 elif content
.startswith("#cloud-boothook"):
270 mime_format
= "text/cloud-boothook"
272 mime_format
= "text/x-shellscript"
274 sub_message
= MIMEText(content
, mime_format
, sys
.getdefaultencoding())
275 combined_message
.attach(sub_message
)
277 return combined_message
.as_string()
279 def _create_user_data(self
, cloud_config
):
281 Creates a script user database on cloud_config info
282 :param cloud_config: dictionary with
283 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
284 'users': (optional) list of users to be inserted, each item is a dict with:
285 'name': (mandatory) user name,
286 'key-pairs': (optional) list of strings with the public key to be inserted to the user
287 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
288 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
289 'config-files': (optional). List of files to be transferred. Each item is a dict with:
290 'dest': (mandatory) string with the destination absolute path
291 'encoding': (optional, by default text). Can be one of:
292 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
293 'content' (mandatory): string with the content of the file
294 'permissions': (optional) string with file permissions, typically octal notation '0644'
295 'owner': (optional) file owner, string with the format 'owner:group'
296 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
297 :return: config_drive, userdata. The first is a boolean or None, the second a string or None
303 # For more information, check https://cloudinit.readthedocs.io/en/latest/reference/merging.html
304 # Basically, with this, we don't override the provider's cloud config
305 merge_how
= yaml
.safe_dump(
310 "settings": ["append", "recurse_dict", "recurse_list"],
314 "settings": ["no_replace", "recurse_list", "recurse_dict"],
319 default_flow_style
=False,
322 if isinstance(cloud_config
, dict):
323 if cloud_config
.get("boot-data-drive") is not None:
324 config_drive
= cloud_config
["boot-data-drive"]
325 # If a config drive is needed, userdata is passed directly
327 userdata
= cloud_config
.get("user-data")
328 # If a config drive is not necessary, then we process userdata and
329 # generate MIME multipart
331 if cloud_config
.get("user-data"):
332 if isinstance(cloud_config
["user-data"], str):
333 userdata_list
.append(
334 cloud_config
["user-data"] + f
"\n{merge_how}"
337 for u
in cloud_config
["user-data"]:
338 userdata_list
.append(u
+ f
"\n{merge_how}")
341 cloud_config
.get("config-files")
342 or cloud_config
.get("users")
343 or cloud_config
.get("key-pairs")
348 if cloud_config
.get("key-pairs"):
349 userdata_dict
["ssh-authorized-keys"] = cloud_config
["key-pairs"]
350 userdata_dict
["system_info"] = {
352 "ssh_authorized_keys": cloud_config
["key-pairs"],
355 userdata_dict
["users"] = ["default"]
357 if cloud_config
.get("users"):
358 if "users" not in userdata_dict
:
359 userdata_dict
["users"] = ["default"]
361 for user
in cloud_config
["users"]:
363 "name": user
["name"],
364 "sudo": "ALL = (ALL)NOPASSWD:ALL",
367 if "user-info" in user
:
368 user_info
["gecos"] = user
["user-info"]
370 if user
.get("key-pairs"):
371 user_info
["ssh-authorized-keys"] = user
["key-pairs"]
373 userdata_dict
["users"].append(user_info
)
375 if cloud_config
.get("config-files"):
376 userdata_dict
["write_files"] = []
377 for file in cloud_config
["config-files"]:
379 "path": file["dest"],
380 "content": file["content"],
383 if file.get("encoding"):
384 file_info
["encoding"] = file["encoding"]
386 if file.get("permissions"):
387 file_info
["permissions"] = file["permissions"]
389 if file.get("owner"):
390 file_info
["owner"] = file["owner"]
392 userdata_dict
["write_files"].append(file_info
)
394 userdata_list
.append(
397 userdata_dict
, indent
=4, default_flow_style
=False
401 userdata
= self
._create
_mimemultipart
(userdata_list
)
402 self
.logger
.debug("userdata: %s", userdata
)
403 # End if config_drive
404 elif isinstance(cloud_config
, str):
405 userdata
= cloud_config
407 return config_drive
, userdata
409 def check_vim_connectivity(self
):
410 """Checks VIM can be reached and user credentials are ok.
411 Returns None if success or raises VimConnConnectionException, VimConnAuthException, ...
413 # by default no checking until each connector implements it
416 def get_tenant_list(self
, filter_dict
={}):
417 """Obtain tenants of VIM
418 filter_dict dictionary that can contain the following keys:
419 name: filter by tenant name
420 id: filter by tenant uuid/id
422 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
423 [{'name':'<name>, 'id':'<id>, ...}, ...]
425 raise VimConnNotImplemented("Should have implemented this")
433 provider_network_profile
=None,
435 """Adds a tenant network to VIM
437 'net_name': name of the network
439 'bridge': overlay isolated network
440 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
441 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
442 'ip_profile': is a dict containing the IP parameters of the network
443 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
444 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
445 'gateway_address': (Optional) ip_schema, that is X.X.X.X
446 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
447 'dhcp_enabled': True or False
448 'dhcp_start_address': ip_schema, first IP to grant
449 'dhcp_count': number of IPs to grant.
450 'shared': if this network can be seen/use by other tenants/organization
451 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
452 Returns a tuple with the network identifier and created_items, or raises an exception on error
453 created_items can be None or a dictionary where this method can include key-values that will be passed to
454 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
455 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
458 raise VimConnNotImplemented("Should have implemented this")
460 def get_network_list(self
, filter_dict
={}):
461 """Obtain tenant networks of VIM
463 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
464 name: string => returns only networks with this name
465 id: string => returns networks with this VIM id, this imply returns one network at most
466 shared: boolean >= returns only networks that are (or are not) shared
467 tenant_id: sting => returns only networks that belong to this tenant/project
468 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state
470 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
471 Returns the network list of dictionaries. each dictionary contains:
472 'id': (mandatory) VIM network id
473 'name': (mandatory) VIM network name
474 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
475 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
476 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
477 'error_msg': (optional) text that explains the ERROR status
478 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
479 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
480 authorization, or some other unspecific error
482 raise VimConnNotImplemented("Should have implemented this")
484 def get_network(self
, net_id
):
485 """Obtain network details from the 'net_id' VIM network
486 Return a dict that contains:
487 'id': (mandatory) VIM network id, that is, net_id
488 'name': (mandatory) VIM network name
489 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
490 'error_msg': (optional) text that explains the ERROR status
491 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
492 Raises an exception upon error or when network is not found
494 raise VimConnNotImplemented("Should have implemented this")
496 def delete_network(self
, net_id
, created_items
=None):
498 Removes a tenant network from VIM and its associated elements
499 :param net_id: VIM identifier of the network, provided by method new_network
500 :param created_items: dictionary with extra items to be deleted. provided by method new_network
501 Returns the network identifier or raises an exception upon error or when network is not found
503 raise VimConnNotImplemented("Should have implemented this")
505 def refresh_nets_status(self
, net_list
):
506 """Get the status of the networks
508 'net_list': a list with the VIM network id to be get the status
509 Returns a dictionary with:
510 'net_id': #VIM id of this network
511 status: #Mandatory. Text with one of:
512 # DELETED (not found at vim)
513 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
514 # OTHER (Vim reported other status not understood)
515 # ERROR (VIM indicates an ERROR status)
516 # ACTIVE, INACTIVE, DOWN (admin down),
517 # BUILD (on building process)
518 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
519 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
522 raise VimConnNotImplemented("Should have implemented this")
524 def get_flavor(self
, flavor_id
):
525 """Obtain flavor details from the VIM
526 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
527 Raises an exception upon error or if not found
529 raise VimConnNotImplemented("Should have implemented this")
531 def get_flavor_id_from_data(self
, flavor_dict
):
532 """Obtain flavor id that match the flavor description
534 'flavor_dict': dictionary that contains:
535 'disk': main hard disk in GB
537 'vcpus': number of virtual cpus
538 #TODO: complete parameters for EPA
539 Returns the flavor_id or raises a VimConnNotFoundException
541 raise VimConnNotImplemented("Should have implemented this")
543 def new_flavor(self
, flavor_data
):
544 """Adds a tenant flavor to VIM
545 flavor_data contains a dictionary with information, keys:
547 ram: memory (cloud type) in MBytes
548 vpcus: cpus (cloud type)
549 extended: EPA parameters
550 - numas: #items requested in same NUMA
551 memory: number of 1G huge pages memory
552 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual
554 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
555 - name: interface name
556 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
557 bandwidth: X Gbps; requested guarantee bandwidth
558 vpci: requested virtual PCI address
562 Returns the flavor identifier
564 raise VimConnNotImplemented("Should have implemented this")
566 def delete_flavor(self
, flavor_id
):
567 """Deletes a tenant flavor from VIM identify by its id
568 Returns the used id or raise an exception
570 raise VimConnNotImplemented("Should have implemented this")
572 def get_affinity_group(self
, affinity_group_id
):
573 """Obtain affinity or anti affinity group details from the VIM
574 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
575 Raises an exception upon error or if not found
577 raise VimConnNotImplemented("Should have implemented this")
579 def new_affinity_group(self
, affinity_group_data
):
580 """Adds an affinity or anti affinity group to VIM
581 affinity_group_data contains a dictionary with information, keys:
582 name: name in VIM for the affinity or anti-affinity group
583 type: affinity or anti-affinity
584 scope: Only nfvi-node allowed
585 Returns the affinity or anti affinity group identifier
587 raise VimConnNotImplemented("Should have implemented this")
589 def delete_affinity_group(self
, affinity_group_id
):
590 """Deletes an affinity or anti affinity group from the VIM identified by its id
591 Returns the used id or raise an exception
593 raise VimConnNotImplemented("Should have implemented this")
595 def new_image(self
, image_dict
):
596 """Adds a tenant image to VIM
597 Returns the image id or raises an exception if failed
599 raise VimConnNotImplemented("Should have implemented this")
601 def delete_image(self
, image_id
):
602 """Deletes a tenant image from VIM
603 Returns the image_id if image is deleted or raises an exception on error
605 raise VimConnNotImplemented("Should have implemented this")
607 def get_image_id_from_path(self
, path
):
608 """Get the image id from image path in the VIM database.
609 Returns the image_id or raises a VimConnNotFoundException
611 raise VimConnNotImplemented("Should have implemented this")
613 def get_image_list(self
, filter_dict
={}):
614 """Obtain tenant images from VIM
618 checksum: image checksum
620 Returns the image list of dictionaries:
621 [{<the fields at Filter_dict plus some VIM specific>}, ...]
624 raise VimConnNotImplemented("Should have implemented this")
637 availability_zone_index
=None,
638 availability_zone_list
=None,
640 """Adds a VM instance to VIM
642 'start': (boolean) indicates if VM must start or created in pause mode.
643 'image_id','flavor_id': image and flavor VIM id to use for the VM
644 affinity_group_list: list of affinity groups, each one is a dictionary.
646 'net_list': list of interfaces, each one is a dictionary with:
647 'name': (optional) name for the interface.
648 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
649 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
651 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
652 'mac_address': (optional) mac address to assign to this interface
653 'ip_address': (optional) IP address to assign to this interface
654 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not
655 provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is used
657 'type': (mandatory) can be one of:
658 'virtual', in this case always connected to a network of type 'net_type=bridge'
659 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a
660 data/ptp network ot it
661 can created unconnected
662 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
663 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
664 are allocated on the same physical NIC
665 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
666 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
667 or True, it must apply the default VIM behaviour
668 After execution the method will add the key:
669 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
670 interface. 'net_list' is modified
671 'cloud_config': (optional) dictionary with:
672 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
673 'users': (optional) list of users to be inserted, each item is a dict with:
674 'name': (mandatory) user name,
675 'key-pairs': (optional) list of strings with the public key to be inserted to the user
676 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
677 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
678 'config-files': (optional). List of files to be transferred. Each item is a dict with:
679 'dest': (mandatory) string with the destination absolute path
680 'encoding': (optional, by default text). Can be one of:
681 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
682 'content' (mandatory): string with the content of the file
683 'permissions': (optional) string with file permissions, typically octal notation '0644'
684 'owner': (optional) file owner, string with the format 'owner:group'
685 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
686 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
687 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
688 'size': (mandatory) string with the size of the disk in GB
689 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
690 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
691 availability_zone_index is None
692 Returns a tuple with the instance identifier and created_items or raises an exception on error
693 created_items can be None or a dictionary where this method can include key-values that will be passed to
694 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
695 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
698 raise VimConnNotImplemented("Should have implemented this")
700 def get_vminstance(self
, vm_id
):
701 """Returns the VM instance information from VIM"""
702 raise VimConnNotImplemented("Should have implemented this")
704 def delete_vminstance(self
, vm_id
, created_items
=None, volumes_to_hold
=None):
706 Removes a VM instance from VIM and its associated elements
707 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
708 :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method
710 :return: None or the same vm_id. Raises an exception on fail
712 raise VimConnNotImplemented("Should have implemented this")
714 def refresh_vms_status(self
, vm_list
):
715 """Get the status of the virtual machines and their interfaces/ports
716 Params: the list of VM identifiers
717 Returns a dictionary with:
718 vm_id: #VIM id of this Virtual Machine
719 status: #Mandatory. Text with one of:
720 # DELETED (not found at vim)
721 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
722 # OTHER (Vim reported other status not understood)
723 # ERROR (VIM indicates an ERROR status)
724 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
725 # BUILD (on building process), ERROR
726 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
728 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
729 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
730 interfaces: list with interface info. Each item a dictionary with:
731 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
732 mac_address: #Text format XX:XX:XX:XX:XX:XX
733 vim_net_id: #network id where this interface is connected, if provided at creation
734 vim_interface_id: #interface/port VIM id
735 ip_address: #null, or text with IPv4, IPv6 address
736 compute_node: #identification of compute node where PF,VF interface is allocated
737 pci: #PCI address of the NIC that hosts the PF,VF
738 vlan: #physical VLAN used for VF
740 raise VimConnNotImplemented("Should have implemented this")
742 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
744 Send and action over a VM instance. Returns created_items if the action was successfully sent to the VIM.
745 created_items is a dictionary with items that
746 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
747 :param action_dict: dictionary with the action to perform
748 :param created_items: provided by method new_vminstance is a dictionary with key-values that will be passed to
749 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is VimConnector
750 dependent, but do not use nested dictionaries and a value of None should be the same as not present. This
751 method can modify this value
752 :return: None, or a console dict
754 raise VimConnNotImplemented("Should have implemented this")
756 def get_vminstance_console(self
, vm_id
, console_type
="vnc"):
758 Get a console for the virtual machine
760 vm_id: uuid of the VM
761 console_type, can be:
762 "novnc" (by default), "xvpvnc" for VNC types,
763 "rdp-html5" for RDP types, "spice-html5" for SPICE types
764 Returns dict with the console parameters:
765 protocol: ssh, ftp, http, https, ...
766 server: usually ip address
767 port: the http, ssh, ... port
768 suffix: extra text, e.g. the http path and query string
770 raise VimConnNotImplemented("Should have implemented this")
773 self
, ip_addr
=None, user
=None, key
=None, ro_key
=None, password
=None
776 Inject a ssh public key in a VM
778 ip_addr: ip address of the VM
779 user: username (default-user) to enter in the VM
780 key: public key to be injected in the VM
781 ro_key: private key of the RO, used to enter in the VM if the password is not provided
782 password: password of the user to enter in the VM
783 The function doesn't return a value:
785 if not ip_addr
or not user
:
786 raise VimConnNotSupportedException(
787 "All parameters should be different from 'None'"
789 elif not ro_key
and not password
:
790 raise VimConnNotSupportedException(
791 "All parameters should be different from 'None'"
796 'echo "{}" >> ~/.ssh/authorized_keys'.format(key
),
797 "chmod 644 ~/.ssh/authorized_keys",
802 format
="%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
804 logging
.getLogger("paramiko").setLevel(logging
.DEBUG
)
805 client
= paramiko
.SSHClient()
809 pkey
= paramiko
.RSAKey
.from_private_key(StringIO(ro_key
))
813 client
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
824 for command
in commands
:
825 (i
, o
, e
) = client
.exec_command(command
, timeout
=30)
826 returncode
= o
.channel
.recv_exit_status()
830 text
= "run_command='{}' Error='{}'".format(command
, outerror
)
831 self
.logger
.debug(traceback
.format_tb(e
.__traceback
__))
832 raise VimConnUnexpectedResponse(
833 "Cannot inject ssh key in VM: '{}'".format(text
)
838 paramiko
.AuthenticationException
,
839 paramiko
.SSHException
,
841 self
.logger
.debug(traceback
.format_exc())
842 raise VimConnUnexpectedResponse(
843 "Cannot inject ssh key in VM: '{}' - {}".format(
844 ip_addr
, str(message
)
850 def new_tenant(self
, tenant_name
, tenant_description
):
851 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
852 "tenant_name": string max lenght 64
853 "tenant_description": string max length 256
854 returns the tenant identifier or raise exception
856 raise VimConnNotImplemented("Should have implemented this")
858 def delete_tenant(self
, tenant_id
):
859 """Delete a tenant from VIM
860 tenant_id: returned VIM tenant_id on "new_tenant"
861 Returns None on success. Raises and exception of failure. If tenant is not found raises VimConnNotFoundException
863 raise VimConnNotImplemented("Should have implemented this")
865 def migrate_instance(self
, vm_id
, compute_host
=None):
868 vm_id: ID of an instance
869 compute_host: Host to migrate the vdu to
870 Returns the vm state or raises an exception upon error
872 raise VimConnNotImplemented("Should have implemented this")
874 def resize_instance(self
, vm_id
, flavor_id
=None):
878 vm_id: ID of an instance
879 flavor_id: flavor_id to resize the vdu to
881 raise VimConnNotImplemented("Should have implemented this")