blob: 66fe1909fc9fe4c310f9656bf986eff09191b7e4 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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
tiernoa7d34d02017-02-23 14:42:07 +010024"""
tierno7edb6752016-03-21 17:37:52 +010025vimconn implement an Abstract class for the vim connector plugins
26 with the definition of the method to be implemented.
tiernoa7d34d02017-02-23 14:42:07 +010027"""
tierno7edb6752016-03-21 17:37:52 +010028
tierno0a1437e2017-10-02 00:17:43 +020029from email.mime.multipart import MIMEMultipart
30from email.mime.text import MIMEText
tierno72774862020-05-04 11:44:15 +000031from http import HTTPStatus
sousaedu049cbb12022-01-05 11:39:35 +000032from io import StringIO
33import logging
34import socket
35import sys
aticige5d78422022-05-16 23:03:54 +030036import traceback
tierno72774862020-05-04 11:44:15 +000037import warnings
tierno9228f512019-07-04 16:23:00 +000038
sousaedu049cbb12022-01-05 11:39:35 +000039import paramiko
40import yaml
41
tierno9228f512019-07-04 16:23:00 +000042__author__ = "Alfonso Tierno, Igor D.C."
tierno72774862020-05-04 11:44:15 +000043__date__ = "$14-aug-2017 23:59:59$"
tierno7edb6752016-03-21 17:37:52 +010044
tierno9228f512019-07-04 16:23:00 +000045
tierno72774862020-05-04 11:44:15 +000046def deprecated(message):
47 def deprecated_decorator(func):
48 def deprecated_func(*args, **kwargs):
sousaedu80135b92021-02-17 15:05:18 +010049 warnings.warn(
50 "{} is a deprecated function. {}".format(func.__name__, message),
51 category=DeprecationWarning,
52 stacklevel=2,
53 )
54 warnings.simplefilter("default", DeprecationWarning)
55
tierno72774862020-05-04 11:44:15 +000056 return func(*args, **kwargs)
sousaedu80135b92021-02-17 15:05:18 +010057
tierno72774862020-05-04 11:44:15 +000058 return deprecated_func
sousaedu80135b92021-02-17 15:05:18 +010059
tierno72774862020-05-04 11:44:15 +000060 return deprecated_decorator
61
62
63# Error variables
64HTTP_Bad_Request = HTTPStatus.BAD_REQUEST.value
65HTTP_Unauthorized = HTTPStatus.UNAUTHORIZED.value
66HTTP_Not_Found = HTTPStatus.NOT_FOUND.value
67HTTP_Method_Not_Allowed = HTTPStatus.METHOD_NOT_ALLOWED.value
68HTTP_Request_Timeout = HTTPStatus.REQUEST_TIMEOUT.value
69HTTP_Conflict = HTTPStatus.CONFLICT.value
70HTTP_Not_Implemented = HTTPStatus.NOT_IMPLEMENTED.value
71HTTP_Service_Unavailable = HTTPStatus.SERVICE_UNAVAILABLE.value
72HTTP_Internal_Server_Error = HTTPStatus.INTERNAL_SERVER_ERROR.value
73
74
75class VimConnException(Exception):
76 """Common and base class Exception for all VimConnector exceptions"""
sousaedu80135b92021-02-17 15:05:18 +010077
tiernoae4a8d12016-07-08 12:30:39 +020078 def __init__(self, message, http_code=HTTP_Bad_Request):
79 Exception.__init__(self, message)
80 self.http_code = http_code
81
tierno9228f512019-07-04 16:23:00 +000082
tierno72774862020-05-04 11:44:15 +000083class VimConnConnectionException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010084 """Connectivity error with the VIM"""
sousaedu80135b92021-02-17 15:05:18 +010085
tiernoae4a8d12016-07-08 12:30:39 +020086 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000087 VimConnException.__init__(self, message, http_code)
tierno9228f512019-07-04 16:23:00 +000088
89
tierno72774862020-05-04 11:44:15 +000090class VimConnUnexpectedResponse(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010091 """Get an wrong response from VIM"""
sousaedu80135b92021-02-17 15:05:18 +010092
tiernoae4a8d12016-07-08 12:30:39 +020093 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000094 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +020095
tierno9228f512019-07-04 16:23:00 +000096
tierno72774862020-05-04 11:44:15 +000097class VimConnAuthException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010098 """Invalid credentials or authorization to perform this action over the VIM"""
sousaedu80135b92021-02-17 15:05:18 +010099
tiernoae4a8d12016-07-08 12:30:39 +0200100 def __init__(self, message, http_code=HTTP_Unauthorized):
tierno72774862020-05-04 11:44:15 +0000101 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200102
tierno9228f512019-07-04 16:23:00 +0000103
tierno72774862020-05-04 11:44:15 +0000104class VimConnNotFoundException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100105 """The item is not found at VIM"""
sousaedu80135b92021-02-17 15:05:18 +0100106
tiernoae4a8d12016-07-08 12:30:39 +0200107 def __init__(self, message, http_code=HTTP_Not_Found):
tierno72774862020-05-04 11:44:15 +0000108 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200109
tierno9228f512019-07-04 16:23:00 +0000110
tierno72774862020-05-04 11:44:15 +0000111class VimConnConflictException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100112 """There is a conflict, e.g. more item found than one"""
sousaedu80135b92021-02-17 15:05:18 +0100113
tiernoae4a8d12016-07-08 12:30:39 +0200114 def __init__(self, message, http_code=HTTP_Conflict):
tierno72774862020-05-04 11:44:15 +0000115 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200116
tierno9228f512019-07-04 16:23:00 +0000117
tierno72774862020-05-04 11:44:15 +0000118class VimConnNotSupportedException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100119 """The request is not supported by connector"""
sousaedu80135b92021-02-17 15:05:18 +0100120
tiernoa7d34d02017-02-23 14:42:07 +0100121 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +0000122 VimConnException.__init__(self, message, http_code)
tiernoa7d34d02017-02-23 14:42:07 +0100123
tierno9228f512019-07-04 16:23:00 +0000124
tierno72774862020-05-04 11:44:15 +0000125class VimConnNotImplemented(VimConnException):
Patricia Reinoso17852162023-06-15 07:33:04 +0000126 """The method is not implemented by the connector"""
sousaedu80135b92021-02-17 15:05:18 +0100127
tiernoae4a8d12016-07-08 12:30:39 +0200128 def __init__(self, message, http_code=HTTP_Not_Implemented):
tierno72774862020-05-04 11:44:15 +0000129 VimConnException.__init__(self, message, http_code)
tierno7edb6752016-03-21 17:37:52 +0100130
mirabal29356312017-07-27 12:21:22 +0200131
Patricia Reinoso17852162023-06-15 07:33:04 +0000132class 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
sousaedu80135b92021-02-17 15:05:18 +0100139class VimConnector:
tiernoa7d34d02017-02-23 14:42:07 +0100140 """Abstract base class for all the VIM connector plugins
tierno72774862020-05-04 11:44:15 +0000141 These plugins must implement a VimConnector class derived from this
tiernoa7d34d02017-02-23 14:42:07 +0100142 and all these privated methods
borsatti8a2dda32019-12-18 15:08:57 +0000143 """
sousaedu80135b92021-02-17 15:05:18 +0100144
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 ):
tierno9228f512019-07-04 16:23:00 +0000159 """
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
tiernoa7d34d02017-02-23 14:42:07 +0100174 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
tiernofb1987b2016-11-15 17:35:06 +0000177 """
tierno9228f512019-07-04 16:23:00 +0000178 self.id = uuid
179 self.name = name
180 self.url = url
tierno7edb6752016-03-21 17:37:52 +0100181 self.url_admin = url_admin
tierno392f2852016-05-13 12:28:55 +0200182 self.tenant_id = tenant_id
183 self.tenant_name = tenant_name
tierno9228f512019-07-04 16:23:00 +0000184 self.user = user
185 self.passwd = passwd
186 self.config = config or {}
mirabal29356312017-07-27 12:21:22 +0200187 self.availability_zone = None
sousaedu80135b92021-02-17 15:05:18 +0100188 self.logger = logging.getLogger("ro.vim")
189
tiernofe789902016-09-29 14:20:44 +0000190 if log_level:
tierno9228f512019-07-04 16:23:00 +0000191 self.logger.setLevel(getattr(logging, log_level))
sousaedu80135b92021-02-17 15:05:18 +0100192
193 if not self.url_admin: # try to use normal url
tiernoae4a8d12016-07-08 12:30:39 +0200194 self.url_admin = self.url
borsatti8a2dda32019-12-18 15:08:57 +0000195
tierno9228f512019-07-04 16:23:00 +0000196 def __getitem__(self, index):
sousaedu80135b92021-02-17 15:05:18 +0100197 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200198 return self.tenant_id
sousaedu80135b92021-02-17 15:05:18 +0100199
200 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200201 return self.tenant_name
sousaedu80135b92021-02-17 15:05:18 +0100202 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100203 return self.id
sousaedu80135b92021-02-17 15:05:18 +0100204 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100205 return self.name
sousaedu80135b92021-02-17 15:05:18 +0100206 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100207 return self.user
sousaedu80135b92021-02-17 15:05:18 +0100208 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100209 return self.passwd
sousaedu80135b92021-02-17 15:05:18 +0100210 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100211 return self.url
sousaedu80135b92021-02-17 15:05:18 +0100212 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100213 return self.url_admin
tierno9228f512019-07-04 16:23:00 +0000214 elif index == "config":
tierno7edb6752016-03-21 17:37:52 +0100215 return self.config
216 else:
tierno9228f512019-07-04 16:23:00 +0000217 raise KeyError("Invalid key '{}'".format(index))
borsatti8a2dda32019-12-18 15:08:57 +0000218
tierno9228f512019-07-04 16:23:00 +0000219 def __setitem__(self, index, value):
sousaedu80135b92021-02-17 15:05:18 +0100220 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200221 self.tenant_id = value
sousaedu80135b92021-02-17 15:05:18 +0100222
223 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200224 self.tenant_name = value
sousaedu80135b92021-02-17 15:05:18 +0100225 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100226 self.id = value
sousaedu80135b92021-02-17 15:05:18 +0100227 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100228 self.name = value
sousaedu80135b92021-02-17 15:05:18 +0100229 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100230 self.user = value
sousaedu80135b92021-02-17 15:05:18 +0100231 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100232 self.passwd = value
sousaedu80135b92021-02-17 15:05:18 +0100233 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100234 self.url = value
sousaedu80135b92021-02-17 15:05:18 +0100235 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100236 self.url_admin = value
237 else:
tierno9228f512019-07-04 16:23:00 +0000238 raise KeyError("Invalid key '{}'".format(index))
tierno0a1437e2017-10-02 00:17:43 +0200239
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]
sousaedu80135b92021-02-17 15:05:18 +0100251
tierno0a1437e2017-10-02 00:17:43 +0200252 combined_message = MIMEMultipart()
sousaedu80135b92021-02-17 15:05:18 +0100253
tierno0a1437e2017-10-02 00:17:43 +0200254 for content in content_list:
sousaedu80135b92021-02-17 15:05:18 +0100255 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"
tierno0a1437e2017-10-02 00:17:43 +0200271 else: # by default
sousaedu80135b92021-02-17 15:05:18 +0100272 mime_format = "text/x-shellscript"
273
tierno9228f512019-07-04 16:23:00 +0000274 sub_message = MIMEText(content, mime_format, sys.getdefaultencoding())
tierno0a1437e2017-10-02 00:17:43 +0200275 combined_message.attach(sub_message)
sousaedu80135b92021-02-17 15:05:18 +0100276
tierno0a1437e2017-10-02 00:17:43 +0200277 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 = []
sousaedu80135b92021-02-17 15:05:18 +0100302
Pedro Escaleira98740c02023-02-22 10:48:52 +0000303 # 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(
306 {
307 "merge_how": [
308 {
309 "name": "list",
310 "settings": ["append", "recurse_dict", "recurse_list"],
311 },
312 {
313 "name": "dict",
314 "settings": ["no_replace", "recurse_list", "recurse_dict"],
315 },
316 ]
317 },
318 indent=4,
319 default_flow_style=False,
320 )
321
tierno0a1437e2017-10-02 00:17:43 +0200322 if isinstance(cloud_config, dict):
323 if cloud_config.get("user-data"):
324 if isinstance(cloud_config["user-data"], str):
Pedro Escaleira98740c02023-02-22 10:48:52 +0000325 userdata_list.append(cloud_config["user-data"] + f"\n{merge_how}")
tierno0a1437e2017-10-02 00:17:43 +0200326 else:
327 for u in cloud_config["user-data"]:
Pedro Escaleira98740c02023-02-22 10:48:52 +0000328 userdata_list.append(u + f"\n{merge_how}")
sousaedu80135b92021-02-17 15:05:18 +0100329
tierno9228f512019-07-04 16:23:00 +0000330 if cloud_config.get("boot-data-drive") is not None:
tierno0a1437e2017-10-02 00:17:43 +0200331 config_drive = cloud_config["boot-data-drive"]
sousaedu80135b92021-02-17 15:05:18 +0100332
333 if (
334 cloud_config.get("config-files")
335 or cloud_config.get("users")
336 or cloud_config.get("key-pairs")
337 ):
tierno0a1437e2017-10-02 00:17:43 +0200338 userdata_dict = {}
sousaedu80135b92021-02-17 15:05:18 +0100339
tierno0a1437e2017-10-02 00:17:43 +0200340 # default user
341 if cloud_config.get("key-pairs"):
342 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
Pedro Escaleira98740c02023-02-22 10:48:52 +0000343 userdata_dict["system_info"] = {
344 "default_user": {
345 "ssh_authorized_keys": cloud_config["key-pairs"],
sousaedu80135b92021-02-17 15:05:18 +0100346 }
Pedro Escaleira98740c02023-02-22 10:48:52 +0000347 }
348 userdata_dict["users"] = ["default"]
sousaedu80135b92021-02-17 15:05:18 +0100349
tierno0a1437e2017-10-02 00:17:43 +0200350 if cloud_config.get("users"):
351 if "users" not in userdata_dict:
352 userdata_dict["users"] = ["default"]
sousaedu80135b92021-02-17 15:05:18 +0100353
tierno0a1437e2017-10-02 00:17:43 +0200354 for user in cloud_config["users"]:
355 user_info = {
356 "name": user["name"],
sousaedu80135b92021-02-17 15:05:18 +0100357 "sudo": "ALL = (ALL)NOPASSWD:ALL",
tierno0a1437e2017-10-02 00:17:43 +0200358 }
sousaedu80135b92021-02-17 15:05:18 +0100359
tierno0a1437e2017-10-02 00:17:43 +0200360 if "user-info" in user:
361 user_info["gecos"] = user["user-info"]
sousaedu80135b92021-02-17 15:05:18 +0100362
tierno0a1437e2017-10-02 00:17:43 +0200363 if user.get("key-pairs"):
364 user_info["ssh-authorized-keys"] = user["key-pairs"]
sousaedu80135b92021-02-17 15:05:18 +0100365
tierno0a1437e2017-10-02 00:17:43 +0200366 userdata_dict["users"].append(user_info)
367
368 if cloud_config.get("config-files"):
369 userdata_dict["write_files"] = []
370 for file in cloud_config["config-files"]:
sousaedu80135b92021-02-17 15:05:18 +0100371 file_info = {"path": file["dest"], "content": file["content"]}
372
tierno0a1437e2017-10-02 00:17:43 +0200373 if file.get("encoding"):
374 file_info["encoding"] = file["encoding"]
sousaedu80135b92021-02-17 15:05:18 +0100375
tierno0a1437e2017-10-02 00:17:43 +0200376 if file.get("permissions"):
377 file_info["permissions"] = file["permissions"]
sousaedu80135b92021-02-17 15:05:18 +0100378
tierno0a1437e2017-10-02 00:17:43 +0200379 if file.get("owner"):
380 file_info["owner"] = file["owner"]
sousaedu80135b92021-02-17 15:05:18 +0100381
tierno0a1437e2017-10-02 00:17:43 +0200382 userdata_dict["write_files"].append(file_info)
sousaedu80135b92021-02-17 15:05:18 +0100383
384 userdata_list.append(
385 "#cloud-config\n"
386 + yaml.safe_dump(userdata_dict, indent=4, default_flow_style=False)
Pedro Escaleira98740c02023-02-22 10:48:52 +0000387 + f"\n{merge_how}"
sousaedu80135b92021-02-17 15:05:18 +0100388 )
tierno0a1437e2017-10-02 00:17:43 +0200389 userdata = self._create_mimemultipart(userdata_list)
390 self.logger.debug("userdata: %s", userdata)
391 elif isinstance(cloud_config, str):
392 userdata = cloud_config
sousaedu80135b92021-02-17 15:05:18 +0100393
tierno0a1437e2017-10-02 00:17:43 +0200394 return config_drive, userdata
395
tiernoa7d34d02017-02-23 14:42:07 +0100396 def check_vim_connectivity(self):
397 """Checks VIM can be reached and user credentials are ok.
tierno72774862020-05-04 11:44:15 +0000398 Returns None if success or raises VimConnConnectionException, VimConnAuthException, ...
tiernoa7d34d02017-02-23 14:42:07 +0100399 """
tierno9228f512019-07-04 16:23:00 +0000400 # by default no checking until each connector implements it
401 return None
tiernoa7d34d02017-02-23 14:42:07 +0100402
tiernoae4a8d12016-07-08 12:30:39 +0200403 def get_tenant_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100404 """Obtain tenants of VIM
tiernofb1987b2016-11-15 17:35:06 +0000405 filter_dict dictionary that can contain the following keys:
tiernoae4a8d12016-07-08 12:30:39 +0200406 name: filter by tenant name
407 id: filter by tenant uuid/id
408 <other VIM specific>
tiernofb1987b2016-11-15 17:35:06 +0000409 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
tiernoae4a8d12016-07-08 12:30:39 +0200410 [{'name':'<name>, 'id':'<id>, ...}, ...]
tiernoa7d34d02017-02-23 14:42:07 +0100411 """
tierno72774862020-05-04 11:44:15 +0000412 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200413
sousaedu80135b92021-02-17 15:05:18 +0100414 def new_network(
415 self,
416 net_name,
417 net_type,
418 ip_profile=None,
419 shared=False,
420 provider_network_profile=None,
421 ):
tiernofb1987b2016-11-15 17:35:06 +0000422 """Adds a tenant network to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100423 Params:
424 'net_name': name of the network
425 'net_type': one of:
426 'bridge': overlay isolated network
427 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
428 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
tierno41a69812018-02-16 14:34:33 +0100429 'ip_profile': is a dict containing the IP parameters of the network
430 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
431 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
432 'gateway_address': (Optional) ip_schema, that is X.X.X.X
433 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
434 'dhcp_enabled': True or False
435 'dhcp_start_address': ip_schema, first IP to grant
436 'dhcp_count': number of IPs to grant.
tiernoa7d34d02017-02-23 14:42:07 +0100437 'shared': if this network can be seen/use by other tenants/organization
kbsuba85c54d2019-10-17 16:30:32 +0000438 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
garciadeblasebd66722019-01-31 16:01:31 +0000439 Returns a tuple with the network identifier and created_items, or raises an exception on error
440 created_items can be None or a dictionary where this method can include key-values that will be passed to
441 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
tierno72774862020-05-04 11:44:15 +0000442 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
garciadeblasebd66722019-01-31 16:01:31 +0000443 as not present.
tiernofb1987b2016-11-15 17:35:06 +0000444 """
tierno72774862020-05-04 11:44:15 +0000445 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100446
447 def get_network_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100448 """Obtain tenant networks of VIM
449 Params:
450 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
451 name: string => returns only networks with this name
452 id: string => returns networks with this VIM id, this imply returns one network at most
453 shared: boolean >= returns only networks that are (or are not) shared
454 tenant_id: sting => returns only networks that belong to this tenant/project
tierno72774862020-05-04 11:44:15 +0000455 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state
456 active
tiernoa7d34d02017-02-23 14:42:07 +0100457 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
458 Returns the network list of dictionaries. each dictionary contains:
459 'id': (mandatory) VIM network id
460 'name': (mandatory) VIM network name
461 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100462 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
463 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
tiernoa7d34d02017-02-23 14:42:07 +0100464 'error_msg': (optional) text that explains the ERROR status
465 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
466 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
467 authorization, or some other unspecific error
468 """
tierno72774862020-05-04 11:44:15 +0000469 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100470
tiernoae4a8d12016-07-08 12:30:39 +0200471 def get_network(self, net_id):
tiernoa7d34d02017-02-23 14:42:07 +0100472 """Obtain network details from the 'net_id' VIM network
473 Return a dict that contains:
474 'id': (mandatory) VIM network id, that is, net_id
475 'name': (mandatory) VIM network name
476 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
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 Raises an exception upon error or when network is not found
480 """
tierno72774862020-05-04 11:44:15 +0000481 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100482
garciadeblasebd66722019-01-31 16:01:31 +0000483 def delete_network(self, net_id, created_items=None):
484 """
485 Removes a tenant network from VIM and its associated elements
486 :param net_id: VIM identifier of the network, provided by method new_network
487 :param created_items: dictionary with extra items to be deleted. provided by method new_network
tiernoa7d34d02017-02-23 14:42:07 +0100488 Returns the network identifier or raises an exception upon error or when network is not found
489 """
tierno72774862020-05-04 11:44:15 +0000490 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100491
tiernoae4a8d12016-07-08 12:30:39 +0200492 def refresh_nets_status(self, net_list):
tiernoa7d34d02017-02-23 14:42:07 +0100493 """Get the status of the networks
494 Params:
495 'net_list': a list with the VIM network id to be get the status
496 Returns a dictionary with:
497 'net_id': #VIM id of this network
498 status: #Mandatory. Text with one of:
499 # DELETED (not found at vim)
500 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
501 # OTHER (Vim reported other status not understood)
502 # ERROR (VIM indicates an ERROR status)
503 # ACTIVE, INACTIVE, DOWN (admin down),
504 # BUILD (on building process)
505 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
506 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
507 'net_id2': ...
508 """
tierno72774862020-05-04 11:44:15 +0000509 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200510
511 def get_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100512 """Obtain flavor details from the VIM
513 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
514 Raises an exception upon error or if not found
515 """
tierno72774862020-05-04 11:44:15 +0000516 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100517
518 def get_flavor_id_from_data(self, flavor_dict):
519 """Obtain flavor id that match the flavor description
tiernoa7d34d02017-02-23 14:42:07 +0100520 Params:
521 'flavor_dict': dictionary that contains:
522 'disk': main hard disk in GB
523 'ram': meomry in MB
524 'vcpus': number of virtual cpus
525 #TODO: complete parameters for EPA
tierno72774862020-05-04 11:44:15 +0000526 Returns the flavor_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100527 """
tierno72774862020-05-04 11:44:15 +0000528 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100529
tiernoae4a8d12016-07-08 12:30:39 +0200530 def new_flavor(self, flavor_data):
tiernoa7d34d02017-02-23 14:42:07 +0100531 """Adds a tenant flavor to VIM
tiernoae4a8d12016-07-08 12:30:39 +0200532 flavor_data contains a dictionary with information, keys:
533 name: flavor name
534 ram: memory (cloud type) in MBytes
535 vpcus: cpus (cloud type)
536 extended: EPA parameters
537 - numas: #items requested in same NUMA
538 memory: number of 1G huge pages memory
tierno72774862020-05-04 11:44:15 +0000539 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual
540 threads
tiernoae4a8d12016-07-08 12:30:39 +0200541 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
542 - name: interface name
543 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
544 bandwidth: X Gbps; requested guarantee bandwidth
borsatti8a2dda32019-12-18 15:08:57 +0000545 vpci: requested virtual PCI address
tiernoae4a8d12016-07-08 12:30:39 +0200546 disk: disk size
547 is_public:
tiernoae4a8d12016-07-08 12:30:39 +0200548 #TODO to concrete
sousaedu80135b92021-02-17 15:05:18 +0100549 Returns the flavor identifier
550 """
tierno72774862020-05-04 11:44:15 +0000551 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100552
tiernoae4a8d12016-07-08 12:30:39 +0200553 def delete_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100554 """Deletes a tenant flavor from VIM identify by its id
sousaedu80135b92021-02-17 15:05:18 +0100555 Returns the used id or raise an exception
556 """
tierno72774862020-05-04 11:44:15 +0000557 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100558
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100559 def get_affinity_group(self, affinity_group_id):
560 """Obtain affinity or anti affinity group details from the VIM
561 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
562 Raises an exception upon error or if not found
563 """
564 raise VimConnNotImplemented("Should have implemented this")
565
566 def new_affinity_group(self, affinity_group_data):
567 """Adds an affinity or anti affinity group to VIM
568 affinity_group_data contains a dictionary with information, keys:
569 name: name in VIM for the affinity or anti-affinity group
570 type: affinity or anti-affinity
571 scope: Only nfvi-node allowed
572 Returns the affinity or anti affinity group identifier
573 """
574 raise VimConnNotImplemented("Should have implemented this")
575
576 def delete_affinity_group(self, affinity_group_id):
577 """Deletes an affinity or anti affinity group from the VIM identified by its id
578 Returns the used id or raise an exception
579 """
580 raise VimConnNotImplemented("Should have implemented this")
581
tiernoa7d34d02017-02-23 14:42:07 +0100582 def new_image(self, image_dict):
sousaedu80135b92021-02-17 15:05:18 +0100583 """Adds a tenant image to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100584 Returns the image id or raises an exception if failed
585 """
tierno72774862020-05-04 11:44:15 +0000586 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100587
tiernoae4a8d12016-07-08 12:30:39 +0200588 def delete_image(self, image_id):
tiernoa7d34d02017-02-23 14:42:07 +0100589 """Deletes a tenant image from VIM
sousaedu80135b92021-02-17 15:05:18 +0100590 Returns the image_id if image is deleted or raises an exception on error
591 """
tierno72774862020-05-04 11:44:15 +0000592 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100593
tiernoae4a8d12016-07-08 12:30:39 +0200594 def get_image_id_from_path(self, path):
tiernocf157a82017-01-30 14:07:06 +0100595 """Get the image id from image path in the VIM database.
sousaedu80135b92021-02-17 15:05:18 +0100596 Returns the image_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100597 """
tierno72774862020-05-04 11:44:15 +0000598 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000599
garciadeblasb69fa9f2016-09-28 12:04:10 +0200600 def get_image_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100601 """Obtain tenant images from VIM
garciadeblasb69fa9f2016-09-28 12:04:10 +0200602 Filter_dict can be:
603 name: image name
604 id: image uuid
605 checksum: image checksum
606 location: image path
607 Returns the image list of dictionaries:
608 [{<the fields at Filter_dict plus some VIM specific>}, ...]
609 List can be empty
tiernoa7d34d02017-02-23 14:42:07 +0100610 """
tierno72774862020-05-04 11:44:15 +0000611 raise VimConnNotImplemented("Should have implemented this")
garciadeblasb69fa9f2016-09-28 12:04:10 +0200612
sousaedu80135b92021-02-17 15:05:18 +0100613 def new_vminstance(
614 self,
615 name,
616 description,
617 start,
618 image_id,
619 flavor_id,
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100620 affinity_group_list,
sousaedu80135b92021-02-17 15:05:18 +0100621 net_list,
622 cloud_config=None,
623 disk_list=None,
624 availability_zone_index=None,
625 availability_zone_list=None,
626 ):
tiernoa7d34d02017-02-23 14:42:07 +0100627 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +0100628 Params:
tiernoa7d34d02017-02-23 14:42:07 +0100629 'start': (boolean) indicates if VM must start or created in pause mode.
630 'image_id','flavor_id': image and flavor VIM id to use for the VM
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100631 affinity_group_list: list of affinity groups, each one is a dictionary.
632 Ignore if empty.
tiernoa7d34d02017-02-23 14:42:07 +0100633 'net_list': list of interfaces, each one is a dictionary with:
634 'name': (optional) name for the interface.
635 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
sousaedu80135b92021-02-17 15:05:18 +0100636 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
tierno72774862020-05-04 11:44:15 +0000637 capabilities
garciadeblasc4f4d732018-10-25 18:17:24 +0200638 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
tiernoa7d34d02017-02-23 14:42:07 +0100639 'mac_address': (optional) mac address to assign to this interface
tierno41a69812018-02-16 14:34:33 +0100640 'ip_address': (optional) IP address to assign to this interface
tierno72774862020-05-04 11:44:15 +0000641 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not
sousaedu80135b92021-02-17 15:05:18 +0100642 provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is used
tierno72774862020-05-04 11:44:15 +0000643 for tagging VF
tiernoa7d34d02017-02-23 14:42:07 +0100644 'type': (mandatory) can be one of:
645 'virtual', in this case always connected to a network of type 'net_type=bridge'
tierno72774862020-05-04 11:44:15 +0000646 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a
647 data/ptp network ot it
tiernoa7d34d02017-02-23 14:42:07 +0100648 can created unconnected
tierno66eba6e2017-11-10 17:09:18 +0100649 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
tiernoa7d34d02017-02-23 14:42:07 +0100650 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
651 are allocated on the same physical NIC
652 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
tiernob3d36742017-03-03 23:51:05 +0100653 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
654 or True, it must apply the default VIM behaviour
tiernoa7d34d02017-02-23 14:42:07 +0100655 After execution the method will add the key:
656 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
657 interface. 'net_list' is modified
658 'cloud_config': (optional) dictionary with:
659 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
660 'users': (optional) list of users to be inserted, each item is a dict with:
661 'name': (mandatory) user name,
662 'key-pairs': (optional) list of strings with the public key to be inserted to the user
tierno40e1bce2017-08-09 09:12:04 +0200663 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
664 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
tiernoa7d34d02017-02-23 14:42:07 +0100665 'config-files': (optional). List of files to be transferred. Each item is a dict with:
666 'dest': (mandatory) string with the destination absolute path
667 'encoding': (optional, by default text). Can be one of:
668 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
669 'content' (mandatory): string with the content of the file
670 'permissions': (optional) string with file permissions, typically octal notation '0644'
671 'owner': (optional) file owner, string with the format 'owner:group'
672 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
673 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
674 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
675 'size': (mandatory) string with the size of the disk in GB
tierno5a3273c2017-08-29 11:43:46 +0200676 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
677 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
678 availability_zone_index is None
tierno98e909c2017-10-14 13:27:03 +0200679 Returns a tuple with the instance identifier and created_items or raises an exception on error
680 created_items can be None or a dictionary where this method can include key-values that will be passed to
681 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
tierno72774862020-05-04 11:44:15 +0000682 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
tierno98e909c2017-10-14 13:27:03 +0200683 as not present.
tiernoa7d34d02017-02-23 14:42:07 +0100684 """
tierno72774862020-05-04 11:44:15 +0000685 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000686
tierno72774862020-05-04 11:44:15 +0000687 def get_vminstance(self, vm_id):
tiernoa7d34d02017-02-23 14:42:07 +0100688 """Returns the VM instance information from VIM"""
tierno72774862020-05-04 11:44:15 +0000689 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000690
garciadeblas89598d42022-06-30 13:57:43 +0200691 def delete_vminstance(self, vm_id, created_items=None, volumes_to_hold=None):
tierno98e909c2017-10-14 13:27:03 +0200692 """
garciadeblasebd66722019-01-31 16:01:31 +0000693 Removes a VM instance from VIM and its associated elements
tierno98e909c2017-10-14 13:27:03 +0200694 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
695 :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method
696 action_vminstance
697 :return: None or the same vm_id. Raises an exception on fail
698 """
tierno72774862020-05-04 11:44:15 +0000699 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100700
tiernoae4a8d12016-07-08 12:30:39 +0200701 def refresh_vms_status(self, vm_list):
tiernoa7d34d02017-02-23 14:42:07 +0100702 """Get the status of the virtual machines and their interfaces/ports
sousaedu80135b92021-02-17 15:05:18 +0100703 Params: the list of VM identifiers
704 Returns a dictionary with:
705 vm_id: #VIM id of this Virtual Machine
706 status: #Mandatory. Text with one of:
707 # DELETED (not found at vim)
708 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
709 # OTHER (Vim reported other status not understood)
710 # ERROR (VIM indicates an ERROR status)
711 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
712 # BUILD (on building process), ERROR
713 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
714 #
715 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
716 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
717 interfaces: list with interface info. Each item a dictionary with:
718 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
719 mac_address: #Text format XX:XX:XX:XX:XX:XX
720 vim_net_id: #network id where this interface is connected, if provided at creation
721 vim_interface_id: #interface/port VIM id
722 ip_address: #null, or text with IPv4, IPv6 address
723 compute_node: #identification of compute node where PF,VF interface is allocated
724 pci: #PCI address of the NIC that hosts the PF,VF
725 vlan: #physical VLAN used for VF
tiernoa7d34d02017-02-23 14:42:07 +0100726 """
tierno72774862020-05-04 11:44:15 +0000727 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000728
tierno98e909c2017-10-14 13:27:03 +0200729 def action_vminstance(self, vm_id, action_dict, created_items={}):
730 """
731 Send and action over a VM instance. Returns created_items if the action was successfully sent to the VIM.
732 created_items is a dictionary with items that
733 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
734 :param action_dict: dictionary with the action to perform
735 :param created_items: provided by method new_vminstance is a dictionary with key-values that will be passed to
tierno72774862020-05-04 11:44:15 +0000736 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is VimConnector
tierno98e909c2017-10-14 13:27:03 +0200737 dependent, but do not use nested dictionaries and a value of None should be the same as not present. This
738 method can modify this value
739 :return: None, or a console dict
740 """
tierno72774862020-05-04 11:44:15 +0000741 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000742
tiernoa7d34d02017-02-23 14:42:07 +0100743 def get_vminstance_console(self, vm_id, console_type="vnc"):
744 """
tiernoae4a8d12016-07-08 12:30:39 +0200745 Get a console for the virtual machine
746 Params:
747 vm_id: uuid of the VM
748 console_type, can be:
borsatti8a2dda32019-12-18 15:08:57 +0000749 "novnc" (by default), "xvpvnc" for VNC types,
tiernoae4a8d12016-07-08 12:30:39 +0200750 "rdp-html5" for RDP types, "spice-html5" for SPICE types
751 Returns dict with the console parameters:
752 protocol: ssh, ftp, http, https, ...
borsatti8a2dda32019-12-18 15:08:57 +0000753 server: usually ip address
754 port: the http, ssh, ... port
755 suffix: extra text, e.g. the http path and query string
tiernoa7d34d02017-02-23 14:42:07 +0100756 """
tierno72774862020-05-04 11:44:15 +0000757 raise VimConnNotImplemented("Should have implemented this")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000758
sousaedu80135b92021-02-17 15:05:18 +0100759 def inject_user_key(
760 self, ip_addr=None, user=None, key=None, ro_key=None, password=None
761 ):
tierno9228f512019-07-04 16:23:00 +0000762 """
763 Inject a ssh public key in a VM
764 Params:
765 ip_addr: ip address of the VM
766 user: username (default-user) to enter in the VM
767 key: public key to be injected in the VM
768 ro_key: private key of the RO, used to enter in the VM if the password is not provided
769 password: password of the user to enter in the VM
770 The function doesn't return a value:
771 """
772 if not ip_addr or not user:
sousaedu80135b92021-02-17 15:05:18 +0100773 raise VimConnNotSupportedException(
774 "All parameters should be different from 'None'"
775 )
tierno9228f512019-07-04 16:23:00 +0000776 elif not ro_key and not password:
sousaedu80135b92021-02-17 15:05:18 +0100777 raise VimConnNotSupportedException(
778 "All parameters should be different from 'None'"
779 )
tierno9228f512019-07-04 16:23:00 +0000780 else:
sousaedu80135b92021-02-17 15:05:18 +0100781 commands = {
782 "mkdir -p ~/.ssh/",
783 'echo "{}" >> ~/.ssh/authorized_keys'.format(key),
784 "chmod 644 ~/.ssh/authorized_keys",
785 "chmod 700 ~/.ssh/",
786 }
aticig2e307cf2022-05-27 10:50:21 +0300787
788 logging.basicConfig(
789 format="%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
790 )
791 logging.getLogger("paramiko").setLevel(logging.DEBUG)
tierno9228f512019-07-04 16:23:00 +0000792 client = paramiko.SSHClient()
sousaedu80135b92021-02-17 15:05:18 +0100793
tierno9228f512019-07-04 16:23:00 +0000794 try:
795 if ro_key:
tierno7d782ef2019-10-04 12:56:31 +0000796 pkey = paramiko.RSAKey.from_private_key(StringIO(ro_key))
tierno9228f512019-07-04 16:23:00 +0000797 else:
798 pkey = None
sousaedu80135b92021-02-17 15:05:18 +0100799
tierno9228f512019-07-04 16:23:00 +0000800 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
aticig2e307cf2022-05-27 10:50:21 +0300801
sousaedu80135b92021-02-17 15:05:18 +0100802 client.connect(
aticig2e307cf2022-05-27 10:50:21 +0300803 ip_addr,
804 username=user,
805 password=password,
806 pkey=pkey,
807 timeout=30,
808 auth_timeout=60,
sousaedu80135b92021-02-17 15:05:18 +0100809 )
810
tierno9228f512019-07-04 16:23:00 +0000811 for command in commands:
aticige5d78422022-05-16 23:03:54 +0300812 (i, o, e) = client.exec_command(command, timeout=30)
tierno9228f512019-07-04 16:23:00 +0000813 returncode = o.channel.recv_exit_status()
tierno9228f512019-07-04 16:23:00 +0000814 outerror = e.read()
sousaedu80135b92021-02-17 15:05:18 +0100815
tierno9228f512019-07-04 16:23:00 +0000816 if returncode != 0:
817 text = "run_command='{}' Error='{}'".format(command, outerror)
aticige5d78422022-05-16 23:03:54 +0300818 self.logger.debug(traceback.format_tb(e.__traceback__))
sousaedu80135b92021-02-17 15:05:18 +0100819 raise VimConnUnexpectedResponse(
820 "Cannot inject ssh key in VM: '{}'".format(text)
821 )
tierno9228f512019-07-04 16:23:00 +0000822 return
sousaedu80135b92021-02-17 15:05:18 +0100823 except (
824 socket.error,
825 paramiko.AuthenticationException,
826 paramiko.SSHException,
827 ) as message:
aticige5d78422022-05-16 23:03:54 +0300828 self.logger.debug(traceback.format_exc())
tierno72774862020-05-04 11:44:15 +0000829 raise VimConnUnexpectedResponse(
sousaedu80135b92021-02-17 15:05:18 +0100830 "Cannot inject ssh key in VM: '{}' - {}".format(
831 ip_addr, str(message)
832 )
833 )
tierno9228f512019-07-04 16:23:00 +0000834 return
835
sousaedu80135b92021-02-17 15:05:18 +0100836 # Optional methods
tierno72774862020-05-04 11:44:15 +0000837 def new_tenant(self, tenant_name, tenant_description):
tierno9228f512019-07-04 16:23:00 +0000838 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
839 "tenant_name": string max lenght 64
840 "tenant_description": string max length 256
841 returns the tenant identifier or raise exception
842 """
tierno72774862020-05-04 11:44:15 +0000843 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000844
sousaedu80135b92021-02-17 15:05:18 +0100845 def delete_tenant(self, tenant_id):
tierno9228f512019-07-04 16:23:00 +0000846 """Delete a tenant from VIM
847 tenant_id: returned VIM tenant_id on "new_tenant"
tierno72774862020-05-04 11:44:15 +0000848 Returns None on success. Raises and exception of failure. If tenant is not found raises VimConnNotFoundException
tierno9228f512019-07-04 16:23:00 +0000849 """
tierno72774862020-05-04 11:44:15 +0000850 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000851
elumalai8658c2c2022-04-28 19:09:31 +0530852 def migrate_instance(self, vm_id, compute_host=None):
853 """Migrate a vdu
854 Params:
855 vm_id: ID of an instance
856 compute_host: Host to migrate the vdu to
857 Returns the vm state or raises an exception upon error
858 """
859 raise VimConnNotImplemented("Should have implemented this")
860
sritharan29a4c1a2022-05-05 12:15:04 +0000861 def resize_instance(self, vm_id, flavor_id=None):
862 """
863 resize a vdu
864 param:
865 vm_id: ID of an instance
866 flavor_id: flavor_id to resize the vdu to
867 """
868 raise VimConnNotImplemented("Should have implemented this")