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