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