Bug 2217 fixed: modified the cloud-init merge configs and defined the default SSH...
[osm/RO.git] / RO-plugin / osm_ro_plugin / vimconn.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
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
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
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
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 """
25 vimconn implement an Abstract class for the vim connector plugins
26 with the definition of the method to be implemented.
27 """
28
29 from email.mime.multipart import MIMEMultipart
30 from email.mime.text import MIMEText
31 from http import HTTPStatus
32 from io import StringIO
33 import logging
34 import socket
35 import sys
36 import traceback
37 import warnings
38
39 import paramiko
40 import yaml
41
42 __author__ = "Alfonso Tierno, Igor D.C."
43 __date__ = "$14-aug-2017 23:59:59$"
44
45
46 def deprecated(message):
47 def deprecated_decorator(func):
48 def deprecated_func(*args, **kwargs):
49 warnings.warn(
50 "{} is a deprecated function. {}".format(func.__name__, message),
51 category=DeprecationWarning,
52 stacklevel=2,
53 )
54 warnings.simplefilter("default", DeprecationWarning)
55
56 return func(*args, **kwargs)
57
58 return deprecated_func
59
60 return deprecated_decorator
61
62
63 # Error variables
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
73
74
75 class VimConnException(Exception):
76 """Common and base class Exception for all VimConnector exceptions"""
77
78 def __init__(self, message, http_code=HTTP_Bad_Request):
79 Exception.__init__(self, message)
80 self.http_code = http_code
81
82
83 class VimConnConnectionException(VimConnException):
84 """Connectivity error with the VIM"""
85
86 def __init__(self, message, http_code=HTTP_Service_Unavailable):
87 VimConnException.__init__(self, message, http_code)
88
89
90 class VimConnUnexpectedResponse(VimConnException):
91 """Get an wrong response from VIM"""
92
93 def __init__(self, message, http_code=HTTP_Service_Unavailable):
94 VimConnException.__init__(self, message, http_code)
95
96
97 class VimConnAuthException(VimConnException):
98 """Invalid credentials or authorization to perform this action over the VIM"""
99
100 def __init__(self, message, http_code=HTTP_Unauthorized):
101 VimConnException.__init__(self, message, http_code)
102
103
104 class VimConnNotFoundException(VimConnException):
105 """The item is not found at VIM"""
106
107 def __init__(self, message, http_code=HTTP_Not_Found):
108 VimConnException.__init__(self, message, http_code)
109
110
111 class VimConnConflictException(VimConnException):
112 """There is a conflict, e.g. more item found than one"""
113
114 def __init__(self, message, http_code=HTTP_Conflict):
115 VimConnException.__init__(self, message, http_code)
116
117
118 class VimConnNotSupportedException(VimConnException):
119 """The request is not supported by connector"""
120
121 def __init__(self, message, http_code=HTTP_Service_Unavailable):
122 VimConnException.__init__(self, message, http_code)
123
124
125 class VimConnNotImplemented(VimConnException):
126 """The method is not implemented by the connected"""
127
128 def __init__(self, message, http_code=HTTP_Not_Implemented):
129 VimConnException.__init__(self, message, http_code)
130
131
132 class VimConnector:
133 """Abstract base class for all the VIM connector plugins
134 These plugins must implement a VimConnector class derived from this
135 and all these privated methods
136 """
137
138 def __init__(
139 self,
140 uuid,
141 name,
142 tenant_id,
143 tenant_name,
144 url,
145 url_admin=None,
146 user=None,
147 passwd=None,
148 log_level=None,
149 config={},
150 persistent_info={},
151 ):
152 """
153 Constructor of VIM. Raise an exception is some needed parameter is missing, but it must not do any connectivity
154 checking against the VIM
155 :param uuid: internal id of this VIM
156 :param name: name assigned to this VIM, can be used for logging
157 :param tenant_id: 'tenant_id': (only one of them is mandatory) VIM tenant to be used
158 :param tenant_name: 'tenant_name': (only one of them is mandatory) VIM tenant to be used
159 :param url: url used for normal operations
160 :param url_admin: (optional), url used for administrative tasks
161 :param user: user to access
162 :param passwd: password
163 :param log_level: provided if it should use a different log_level than the general one
164 :param config: dictionary with extra VIM information. This contains a consolidate version of VIM config
165 at VIM_ACCOUNT (attach)
166 :param persitent_info: dict where the class can store information that will be available among class
167 destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an
168 empty dict. Useful to store login/tokens information for speed up communication
169
170 """
171 self.id = uuid
172 self.name = name
173 self.url = url
174 self.url_admin = url_admin
175 self.tenant_id = tenant_id
176 self.tenant_name = tenant_name
177 self.user = user
178 self.passwd = passwd
179 self.config = config or {}
180 self.availability_zone = None
181 self.logger = logging.getLogger("ro.vim")
182
183 if log_level:
184 self.logger.setLevel(getattr(logging, log_level))
185
186 if not self.url_admin: # try to use normal url
187 self.url_admin = self.url
188
189 def __getitem__(self, index):
190 if index == "tenant_id":
191 return self.tenant_id
192
193 if index == "tenant_name":
194 return self.tenant_name
195 elif index == "id":
196 return self.id
197 elif index == "name":
198 return self.name
199 elif index == "user":
200 return self.user
201 elif index == "passwd":
202 return self.passwd
203 elif index == "url":
204 return self.url
205 elif index == "url_admin":
206 return self.url_admin
207 elif index == "config":
208 return self.config
209 else:
210 raise KeyError("Invalid key '{}'".format(index))
211
212 def __setitem__(self, index, value):
213 if index == "tenant_id":
214 self.tenant_id = value
215
216 if index == "tenant_name":
217 self.tenant_name = value
218 elif index == "id":
219 self.id = value
220 elif index == "name":
221 self.name = value
222 elif index == "user":
223 self.user = value
224 elif index == "passwd":
225 self.passwd = value
226 elif index == "url":
227 self.url = value
228 elif index == "url_admin":
229 self.url_admin = value
230 else:
231 raise KeyError("Invalid key '{}'".format(index))
232
233 @staticmethod
234 def _create_mimemultipart(content_list):
235 """Creates a MIMEmultipart text combining the content_list
236 :param content_list: list of text scripts to be combined
237 :return: str of the created MIMEmultipart. If the list is empty returns None, if the list contains only one
238 element MIMEmultipart is not created and this content is returned
239 """
240 if not content_list:
241 return None
242 elif len(content_list) == 1:
243 return content_list[0]
244
245 combined_message = MIMEMultipart()
246
247 for content in content_list:
248 if content.startswith("#include"):
249 mime_format = "text/x-include-url"
250 elif content.startswith("#include-once"):
251 mime_format = "text/x-include-once-url"
252 elif content.startswith("#!"):
253 mime_format = "text/x-shellscript"
254 elif content.startswith("#cloud-config"):
255 mime_format = "text/cloud-config"
256 elif content.startswith("#cloud-config-archive"):
257 mime_format = "text/cloud-config-archive"
258 elif content.startswith("#upstart-job"):
259 mime_format = "text/upstart-job"
260 elif content.startswith("#part-handler"):
261 mime_format = "text/part-handler"
262 elif content.startswith("#cloud-boothook"):
263 mime_format = "text/cloud-boothook"
264 else: # by default
265 mime_format = "text/x-shellscript"
266
267 sub_message = MIMEText(content, mime_format, sys.getdefaultencoding())
268 combined_message.attach(sub_message)
269
270 return combined_message.as_string()
271
272 def _create_user_data(self, cloud_config):
273 """
274 Creates a script user database on cloud_config info
275 :param cloud_config: dictionary with
276 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
277 'users': (optional) list of users to be inserted, each item is a dict with:
278 'name': (mandatory) user name,
279 'key-pairs': (optional) list of strings with the public key to be inserted to the user
280 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
281 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
282 'config-files': (optional). List of files to be transferred. Each item is a dict with:
283 'dest': (mandatory) string with the destination absolute path
284 'encoding': (optional, by default text). Can be one of:
285 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
286 'content' (mandatory): string with the content of the file
287 'permissions': (optional) string with file permissions, typically octal notation '0644'
288 'owner': (optional) file owner, string with the format 'owner:group'
289 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
290 :return: config_drive, userdata. The first is a boolean or None, the second a string or None
291 """
292 config_drive = None
293 userdata = None
294 userdata_list = []
295
296 # For more information, check https://cloudinit.readthedocs.io/en/latest/reference/merging.html
297 # Basically, with this, we don't override the provider's cloud config
298 merge_how = yaml.safe_dump(
299 {
300 "merge_how": [
301 {
302 "name": "list",
303 "settings": ["append", "recurse_dict", "recurse_list"],
304 },
305 {
306 "name": "dict",
307 "settings": ["no_replace", "recurse_list", "recurse_dict"],
308 },
309 ]
310 },
311 indent=4,
312 default_flow_style=False,
313 )
314
315 if isinstance(cloud_config, dict):
316 if cloud_config.get("user-data"):
317 if isinstance(cloud_config["user-data"], str):
318 userdata_list.append(cloud_config["user-data"] + f"\n{merge_how}")
319 else:
320 for u in cloud_config["user-data"]:
321 userdata_list.append(u + f"\n{merge_how}")
322
323 if cloud_config.get("boot-data-drive") is not None:
324 config_drive = cloud_config["boot-data-drive"]
325
326 if (
327 cloud_config.get("config-files")
328 or cloud_config.get("users")
329 or cloud_config.get("key-pairs")
330 ):
331 userdata_dict = {}
332
333 # default user
334 if cloud_config.get("key-pairs"):
335 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
336 userdata_dict["system_info"] = {
337 "default_user": {
338 "ssh_authorized_keys": cloud_config["key-pairs"],
339 }
340 }
341 userdata_dict["users"] = ["default"]
342
343 if cloud_config.get("users"):
344 if "users" not in userdata_dict:
345 userdata_dict["users"] = ["default"]
346
347 for user in cloud_config["users"]:
348 user_info = {
349 "name": user["name"],
350 "sudo": "ALL = (ALL)NOPASSWD:ALL",
351 }
352
353 if "user-info" in user:
354 user_info["gecos"] = user["user-info"]
355
356 if user.get("key-pairs"):
357 user_info["ssh-authorized-keys"] = user["key-pairs"]
358
359 userdata_dict["users"].append(user_info)
360
361 if cloud_config.get("config-files"):
362 userdata_dict["write_files"] = []
363 for file in cloud_config["config-files"]:
364 file_info = {"path": file["dest"], "content": file["content"]}
365
366 if file.get("encoding"):
367 file_info["encoding"] = file["encoding"]
368
369 if file.get("permissions"):
370 file_info["permissions"] = file["permissions"]
371
372 if file.get("owner"):
373 file_info["owner"] = file["owner"]
374
375 userdata_dict["write_files"].append(file_info)
376
377 userdata_list.append(
378 "#cloud-config\n"
379 + yaml.safe_dump(userdata_dict, indent=4, default_flow_style=False)
380 + f"\n{merge_how}"
381 )
382 userdata = self._create_mimemultipart(userdata_list)
383 self.logger.debug("userdata: %s", userdata)
384 elif isinstance(cloud_config, str):
385 userdata = cloud_config
386
387 return config_drive, userdata
388
389 def check_vim_connectivity(self):
390 """Checks VIM can be reached and user credentials are ok.
391 Returns None if success or raises VimConnConnectionException, VimConnAuthException, ...
392 """
393 # by default no checking until each connector implements it
394 return None
395
396 def get_tenant_list(self, filter_dict={}):
397 """Obtain tenants of VIM
398 filter_dict dictionary that can contain the following keys:
399 name: filter by tenant name
400 id: filter by tenant uuid/id
401 <other VIM specific>
402 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
403 [{'name':'<name>, 'id':'<id>, ...}, ...]
404 """
405 raise VimConnNotImplemented("Should have implemented this")
406
407 def new_network(
408 self,
409 net_name,
410 net_type,
411 ip_profile=None,
412 shared=False,
413 provider_network_profile=None,
414 ):
415 """Adds a tenant network to VIM
416 Params:
417 'net_name': name of the network
418 'net_type': one of:
419 'bridge': overlay isolated network
420 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
421 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
422 'ip_profile': is a dict containing the IP parameters of the network
423 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
424 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
425 'gateway_address': (Optional) ip_schema, that is X.X.X.X
426 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
427 'dhcp_enabled': True or False
428 'dhcp_start_address': ip_schema, first IP to grant
429 'dhcp_count': number of IPs to grant.
430 'shared': if this network can be seen/use by other tenants/organization
431 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
432 Returns a tuple with the network identifier and created_items, or raises an exception on error
433 created_items can be None or a dictionary where this method can include key-values that will be passed to
434 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
435 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
436 as not present.
437 """
438 raise VimConnNotImplemented("Should have implemented this")
439
440 def get_network_list(self, filter_dict={}):
441 """Obtain tenant networks of VIM
442 Params:
443 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
444 name: string => returns only networks with this name
445 id: string => returns networks with this VIM id, this imply returns one network at most
446 shared: boolean >= returns only networks that are (or are not) shared
447 tenant_id: sting => returns only networks that belong to this tenant/project
448 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state
449 active
450 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
451 Returns the network list of dictionaries. each dictionary contains:
452 'id': (mandatory) VIM network id
453 'name': (mandatory) VIM network name
454 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
455 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
456 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
457 'error_msg': (optional) text that explains the ERROR status
458 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
459 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
460 authorization, or some other unspecific error
461 """
462 raise VimConnNotImplemented("Should have implemented this")
463
464 def get_network(self, net_id):
465 """Obtain network details from the 'net_id' VIM network
466 Return a dict that contains:
467 'id': (mandatory) VIM network id, that is, net_id
468 'name': (mandatory) VIM network name
469 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
470 'error_msg': (optional) text that explains the ERROR status
471 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
472 Raises an exception upon error or when network is not found
473 """
474 raise VimConnNotImplemented("Should have implemented this")
475
476 def delete_network(self, net_id, created_items=None):
477 """
478 Removes a tenant network from VIM and its associated elements
479 :param net_id: VIM identifier of the network, provided by method new_network
480 :param created_items: dictionary with extra items to be deleted. provided by method new_network
481 Returns the network identifier or raises an exception upon error or when network is not found
482 """
483 raise VimConnNotImplemented("Should have implemented this")
484
485 def refresh_nets_status(self, net_list):
486 """Get the status of the networks
487 Params:
488 'net_list': a list with the VIM network id to be get the status
489 Returns a dictionary with:
490 'net_id': #VIM id of this network
491 status: #Mandatory. Text with one of:
492 # DELETED (not found at vim)
493 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
494 # OTHER (Vim reported other status not understood)
495 # ERROR (VIM indicates an ERROR status)
496 # ACTIVE, INACTIVE, DOWN (admin down),
497 # BUILD (on building process)
498 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
499 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
500 'net_id2': ...
501 """
502 raise VimConnNotImplemented("Should have implemented this")
503
504 def get_flavor(self, flavor_id):
505 """Obtain flavor details from the VIM
506 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
507 Raises an exception upon error or if not found
508 """
509 raise VimConnNotImplemented("Should have implemented this")
510
511 def get_flavor_id_from_data(self, flavor_dict):
512 """Obtain flavor id that match the flavor description
513 Params:
514 'flavor_dict': dictionary that contains:
515 'disk': main hard disk in GB
516 'ram': meomry in MB
517 'vcpus': number of virtual cpus
518 #TODO: complete parameters for EPA
519 Returns the flavor_id or raises a VimConnNotFoundException
520 """
521 raise VimConnNotImplemented("Should have implemented this")
522
523 def new_flavor(self, flavor_data):
524 """Adds a tenant flavor to VIM
525 flavor_data contains a dictionary with information, keys:
526 name: flavor name
527 ram: memory (cloud type) in MBytes
528 vpcus: cpus (cloud type)
529 extended: EPA parameters
530 - numas: #items requested in same NUMA
531 memory: number of 1G huge pages memory
532 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual
533 threads
534 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
535 - name: interface name
536 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
537 bandwidth: X Gbps; requested guarantee bandwidth
538 vpci: requested virtual PCI address
539 disk: disk size
540 is_public:
541 #TODO to concrete
542 Returns the flavor identifier
543 """
544 raise VimConnNotImplemented("Should have implemented this")
545
546 def delete_flavor(self, flavor_id):
547 """Deletes a tenant flavor from VIM identify by its id
548 Returns the used id or raise an exception
549 """
550 raise VimConnNotImplemented("Should have implemented this")
551
552 def get_affinity_group(self, affinity_group_id):
553 """Obtain affinity or anti affinity group details from the VIM
554 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
555 Raises an exception upon error or if not found
556 """
557 raise VimConnNotImplemented("Should have implemented this")
558
559 def new_affinity_group(self, affinity_group_data):
560 """Adds an affinity or anti affinity group to VIM
561 affinity_group_data contains a dictionary with information, keys:
562 name: name in VIM for the affinity or anti-affinity group
563 type: affinity or anti-affinity
564 scope: Only nfvi-node allowed
565 Returns the affinity or anti affinity group identifier
566 """
567 raise VimConnNotImplemented("Should have implemented this")
568
569 def delete_affinity_group(self, affinity_group_id):
570 """Deletes an affinity or anti affinity group from the VIM identified by its id
571 Returns the used id or raise an exception
572 """
573 raise VimConnNotImplemented("Should have implemented this")
574
575 def new_image(self, image_dict):
576 """Adds a tenant image to VIM
577 Returns the image id or raises an exception if failed
578 """
579 raise VimConnNotImplemented("Should have implemented this")
580
581 def delete_image(self, image_id):
582 """Deletes a tenant image from VIM
583 Returns the image_id if image is deleted or raises an exception on error
584 """
585 raise VimConnNotImplemented("Should have implemented this")
586
587 def get_image_id_from_path(self, path):
588 """Get the image id from image path in the VIM database.
589 Returns the image_id or raises a VimConnNotFoundException
590 """
591 raise VimConnNotImplemented("Should have implemented this")
592
593 def get_image_list(self, filter_dict={}):
594 """Obtain tenant images from VIM
595 Filter_dict can be:
596 name: image name
597 id: image uuid
598 checksum: image checksum
599 location: image path
600 Returns the image list of dictionaries:
601 [{<the fields at Filter_dict plus some VIM specific>}, ...]
602 List can be empty
603 """
604 raise VimConnNotImplemented("Should have implemented this")
605
606 def new_vminstance(
607 self,
608 name,
609 description,
610 start,
611 image_id,
612 flavor_id,
613 affinity_group_list,
614 net_list,
615 cloud_config=None,
616 disk_list=None,
617 availability_zone_index=None,
618 availability_zone_list=None,
619 ):
620 """Adds a VM instance to VIM
621 Params:
622 'start': (boolean) indicates if VM must start or created in pause mode.
623 'image_id','flavor_id': image and flavor VIM id to use for the VM
624 affinity_group_list: list of affinity groups, each one is a dictionary.
625 Ignore if empty.
626 'net_list': list of interfaces, each one is a dictionary with:
627 'name': (optional) name for the interface.
628 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
629 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
630 capabilities
631 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
632 'mac_address': (optional) mac address to assign to this interface
633 'ip_address': (optional) IP address to assign to this interface
634 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not
635 provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is used
636 for tagging VF
637 'type': (mandatory) can be one of:
638 'virtual', in this case always connected to a network of type 'net_type=bridge'
639 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a
640 data/ptp network ot it
641 can created unconnected
642 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
643 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
644 are allocated on the same physical NIC
645 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
646 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
647 or True, it must apply the default VIM behaviour
648 After execution the method will add the key:
649 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
650 interface. 'net_list' is modified
651 'cloud_config': (optional) dictionary with:
652 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
653 'users': (optional) list of users to be inserted, each item is a dict with:
654 'name': (mandatory) user name,
655 'key-pairs': (optional) list of strings with the public key to be inserted to the user
656 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
657 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
658 'config-files': (optional). List of files to be transferred. Each item is a dict with:
659 'dest': (mandatory) string with the destination absolute path
660 'encoding': (optional, by default text). Can be one of:
661 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
662 'content' (mandatory): string with the content of the file
663 'permissions': (optional) string with file permissions, typically octal notation '0644'
664 'owner': (optional) file owner, string with the format 'owner:group'
665 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
666 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
667 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
668 'size': (mandatory) string with the size of the disk in GB
669 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
670 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
671 availability_zone_index is None
672 Returns a tuple with the instance identifier and created_items or raises an exception on error
673 created_items can be None or a dictionary where this method can include key-values that will be passed to
674 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
675 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
676 as not present.
677 """
678 raise VimConnNotImplemented("Should have implemented this")
679
680 def get_vminstance(self, vm_id):
681 """Returns the VM instance information from VIM"""
682 raise VimConnNotImplemented("Should have implemented this")
683
684 def delete_vminstance(self, vm_id, created_items=None, volumes_to_hold=None):
685 """
686 Removes a VM instance from VIM and its associated elements
687 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
688 :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method
689 action_vminstance
690 :return: None or the same vm_id. Raises an exception on fail
691 """
692 raise VimConnNotImplemented("Should have implemented this")
693
694 def refresh_vms_status(self, vm_list):
695 """Get the status of the virtual machines and their interfaces/ports
696 Params: the list of VM identifiers
697 Returns a dictionary with:
698 vm_id: #VIM id of this Virtual Machine
699 status: #Mandatory. Text with one of:
700 # DELETED (not found at vim)
701 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
702 # OTHER (Vim reported other status not understood)
703 # ERROR (VIM indicates an ERROR status)
704 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
705 # BUILD (on building process), ERROR
706 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
707 #
708 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
709 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
710 interfaces: list with interface info. Each item a dictionary with:
711 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
712 mac_address: #Text format XX:XX:XX:XX:XX:XX
713 vim_net_id: #network id where this interface is connected, if provided at creation
714 vim_interface_id: #interface/port VIM id
715 ip_address: #null, or text with IPv4, IPv6 address
716 compute_node: #identification of compute node where PF,VF interface is allocated
717 pci: #PCI address of the NIC that hosts the PF,VF
718 vlan: #physical VLAN used for VF
719 """
720 raise VimConnNotImplemented("Should have implemented this")
721
722 def action_vminstance(self, vm_id, action_dict, created_items={}):
723 """
724 Send and action over a VM instance. Returns created_items if the action was successfully sent to the VIM.
725 created_items is a dictionary with items that
726 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
727 :param action_dict: dictionary with the action to perform
728 :param created_items: provided by method new_vminstance is a dictionary with key-values that will be passed to
729 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is VimConnector
730 dependent, but do not use nested dictionaries and a value of None should be the same as not present. This
731 method can modify this value
732 :return: None, or a console dict
733 """
734 raise VimConnNotImplemented("Should have implemented this")
735
736 def get_vminstance_console(self, vm_id, console_type="vnc"):
737 """
738 Get a console for the virtual machine
739 Params:
740 vm_id: uuid of the VM
741 console_type, can be:
742 "novnc" (by default), "xvpvnc" for VNC types,
743 "rdp-html5" for RDP types, "spice-html5" for SPICE types
744 Returns dict with the console parameters:
745 protocol: ssh, ftp, http, https, ...
746 server: usually ip address
747 port: the http, ssh, ... port
748 suffix: extra text, e.g. the http path and query string
749 """
750 raise VimConnNotImplemented("Should have implemented this")
751
752 def inject_user_key(
753 self, ip_addr=None, user=None, key=None, ro_key=None, password=None
754 ):
755 """
756 Inject a ssh public key in a VM
757 Params:
758 ip_addr: ip address of the VM
759 user: username (default-user) to enter in the VM
760 key: public key to be injected in the VM
761 ro_key: private key of the RO, used to enter in the VM if the password is not provided
762 password: password of the user to enter in the VM
763 The function doesn't return a value:
764 """
765 if not ip_addr or not user:
766 raise VimConnNotSupportedException(
767 "All parameters should be different from 'None'"
768 )
769 elif not ro_key and not password:
770 raise VimConnNotSupportedException(
771 "All parameters should be different from 'None'"
772 )
773 else:
774 commands = {
775 "mkdir -p ~/.ssh/",
776 'echo "{}" >> ~/.ssh/authorized_keys'.format(key),
777 "chmod 644 ~/.ssh/authorized_keys",
778 "chmod 700 ~/.ssh/",
779 }
780
781 logging.basicConfig(
782 format="%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
783 )
784 logging.getLogger("paramiko").setLevel(logging.DEBUG)
785 client = paramiko.SSHClient()
786
787 try:
788 if ro_key:
789 pkey = paramiko.RSAKey.from_private_key(StringIO(ro_key))
790 else:
791 pkey = None
792
793 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
794
795 client.connect(
796 ip_addr,
797 username=user,
798 password=password,
799 pkey=pkey,
800 timeout=30,
801 auth_timeout=60,
802 )
803
804 for command in commands:
805 (i, o, e) = client.exec_command(command, timeout=30)
806 returncode = o.channel.recv_exit_status()
807 outerror = e.read()
808
809 if returncode != 0:
810 text = "run_command='{}' Error='{}'".format(command, outerror)
811 self.logger.debug(traceback.format_tb(e.__traceback__))
812 raise VimConnUnexpectedResponse(
813 "Cannot inject ssh key in VM: '{}'".format(text)
814 )
815 return
816 except (
817 socket.error,
818 paramiko.AuthenticationException,
819 paramiko.SSHException,
820 ) as message:
821 self.logger.debug(traceback.format_exc())
822 raise VimConnUnexpectedResponse(
823 "Cannot inject ssh key in VM: '{}' - {}".format(
824 ip_addr, str(message)
825 )
826 )
827 return
828
829 # Optional methods
830 def new_tenant(self, tenant_name, tenant_description):
831 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
832 "tenant_name": string max lenght 64
833 "tenant_description": string max length 256
834 returns the tenant identifier or raise exception
835 """
836 raise VimConnNotImplemented("Should have implemented this")
837
838 def delete_tenant(self, tenant_id):
839 """Delete a tenant from VIM
840 tenant_id: returned VIM tenant_id on "new_tenant"
841 Returns None on success. Raises and exception of failure. If tenant is not found raises VimConnNotFoundException
842 """
843 raise VimConnNotImplemented("Should have implemented this")
844
845 def migrate_instance(self, vm_id, compute_host=None):
846 """Migrate a vdu
847 Params:
848 vm_id: ID of an instance
849 compute_host: Host to migrate the vdu to
850 Returns the vm state or raises an exception upon error
851 """
852 raise VimConnNotImplemented("Should have implemented this")
853
854 def resize_instance(self, vm_id, flavor_id=None):
855 """
856 resize a vdu
857 param:
858 vm_id: ID of an instance
859 flavor_id: flavor_id to resize the vdu to
860 """
861 raise VimConnNotImplemented("Should have implemented this")