1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
# Copyright 2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import ast
import contextlib
try:
import cPickle as pickle
except ImportError:
import pickle
import errno
import socket
import time
from eventlet import queue
from eventlet import timeout
from oslo_log import log as logging
from oslo_utils import versionutils
from six.moves import http_client
from six.moves import urllib
try:
import xmlrpclib
except ImportError:
import six.moves.xmlrpc_client as xmlrpclib
from os_xenapi.client import exception
from os_xenapi.client.i18n import _
from os_xenapi.client.i18n import _LW
from os_xenapi.client import objects as cli_objects
from os_xenapi.client import XenAPI
LOG = logging.getLogger(__name__)
def apply_session_helpers(session):
session.console = cli_objects.Console(session)
session.GPU_group = cli_objects.GPU_group(session)
session.host = cli_objects.Host(session)
session.host_metrics = cli_objects.Host_metrics(session)
session.network = cli_objects.Network(session)
session.PBD = cli_objects.PBD(session)
session.PCI = cli_objects.PCI(session)
session.PGPU = cli_objects.PGPU(session)
session.PIF = cli_objects.PIF(session)
session.PIF_metrics = cli_objects.PIF_metrics(session)
session.pool = cli_objects.Pool(session)
session.pool_update = cli_objects.Pool_update(session)
session.role = cli_objects.Role(session)
session.SDN_controller = cli_objects.SDN_controller(session)
session.secret = cli_objects.Secret(session)
session.SM = cli_objects.SM(session)
session.SR = cli_objects.SR(session)
session.task = cli_objects.Task(session)
session.VBD = cli_objects.VBD(session)
session.VDI = cli_objects.VDI(session)
session.VGPU = cli_objects.VGPU(session)
session.VGPU_type = cli_objects.VGPU_type(session)
session.VIF = cli_objects.VIF(session)
session.VLAN = cli_objects.VLAN(session)
session.VM = cli_objects.VM(session)
class XenAPISession(object):
"""The session to invoke XenAPI SDK calls."""
# This is not a config option as it should only ever be
# changed in development environments.
# MAJOR VERSION: Incompatible changes with the plugins
# MINOR VERSION: Compatible changes, new plguins, etc
PLUGIN_REQUIRED_VERSION = '2.1'
def __init__(self, url, user, pw, originator="os-xenapi", timeout=10,
concurrent=5):
"""Initialize session for connection with XenServer/Xen Cloud Platform
:param url: URL for connection to XenServer/Xen Cloud Platform
:param user: Username for connection to XenServer/Xen Cloud Platform
:param pw: Password for connection to XenServer/Xen Cloud Platform
:param originator: Specify the caller for this API
:param timeout: Timeout in seconds for XenAPI login
:param concurrent: Maximum concurrent XenAPI connections
"""
self.XenAPI = XenAPI
self.originator = originator
self.timeout = timeout
self.concurrent = concurrent
self._sessions = queue.Queue()
self.host_checked = False
self.is_slave = False
self.ip = self._get_ip_from_url(url)
self.url = url
self.master_url = self._create_first_session(url, user, pw)
self._populate_session_pool(self.master_url, user, pw)
self.host_ref = self._get_host_ref(self.ip)
self.host_uuid = self._get_host_uuid()
self.product_version, self.product_brand = \
self._get_product_version_and_brand()
self._verify_plugin_version()
self.platform_version = self._get_platform_version()
self._cached_xsm_sr_relaxed = None
apply_session_helpers(self)
def _login_with_password(self, user, pw, session):
login_exception = XenAPI.Failure(_("Unable to log in to XenAPI "
"(is the Dom0 disk full?)"))
with timeout.Timeout(self.timeout, login_exception):
session.login_with_password(user, pw, self.PLUGIN_REQUIRED_VERSION,
self.originator)
def _verify_plugin_version(self):
requested_version = self.PLUGIN_REQUIRED_VERSION
current_version = self.call_plugin_serialized(
'dom0_plugin_version.py', 'get_version')
if not versionutils.is_compatible(requested_version, current_version):
raise XenAPI.Failure(
_("Plugin version mismatch (Expected %(exp)s, got %(got)s)") %
{'exp': requested_version, 'got': current_version})
def _create_first_session(self, url, user, pw):
try:
session = self._create_session_and_login(url, user, pw)
except XenAPI.Failure as e:
# if user and pw of the master are different, we're doomed!
if e.details[0] == 'HOST_IS_SLAVE':
master = e.details[1]
url = self.swap_xapi_host(url, master)
session = self._create_session_and_login(url, user, pw)
self.is_slave = True
else:
raise
self._sessions.put(session)
return url
def _get_ip_from_url(self, url):
url_parts = urllib.parse.urlparse(url)
return socket.gethostbyname(url_parts.netloc)
def swap_xapi_host(self, url, host_addr):
"""Replace the XenServer address present in 'url' with 'host_addr'."""
temp_url = urllib.parse.urlparse(url)
return url.replace(temp_url.hostname, '%s' % host_addr)
def _populate_session_pool(self, url, user, pw):
for i in range(self.concurrent - 1):
session = self._create_session_and_login(url, user, pw)
self._sessions.put(session)
def _get_host_uuid(self):
with self._get_session() as session:
return session.xenapi.host.get_uuid(self.host_ref)
def _get_product_version_and_brand(self):
"""Return tuple of (major, minor, rev)
This tuple is for host version and product brand.
"""
software_version = self._get_software_version()
product_version_str = software_version.get('product_version')
# Product version is only set in some cases (e.g. XCP, XenServer) and
# not in others (e.g. xenserver-core, XAPI-XCP).
# In these cases, the platform version is the best number to use.
if product_version_str is None:
product_version_str = software_version.get('platform_version',
'0.0.0')
product_brand = software_version.get('product_brand')
product_version =\
versionutils.convert_version_to_tuple(product_version_str)
return product_version, product_brand
def _get_platform_version(self):
"""Return a tuple of (major, minor, rev) for the host version"""
software_version = self._get_software_version()
platform_version_str = software_version.get('platform_version',
'0.0.0')
platform_version = versionutils.convert_version_to_tuple(
platform_version_str)
return platform_version
def _get_software_version(self):
return self.call_xenapi('host.get_software_version', self.host_ref)
def get_session_id(self):
"""Return a string session_id. Used for vnc consoles."""
with self._get_session() as session:
return str(session._session)
@contextlib.contextmanager
def _get_session(self):
"""Return exclusive session for scope of with statement."""
session = self._sessions.get()
try:
yield session
finally:
self._sessions.put(session)
def _get_host_ref(self, host_ip):
with self._get_session() as session:
if self.is_slave:
rec_dict = session.xenapi.PIF.get_all_records_where(
'field "IP"="%s"' % host_ip)
if not rec_dict:
raise XenAPI.Failure(
("ERROR, couldn't find host ref with ip \
%(slave_ip)s ") % {'slave_ip': host_ip})
if len(rec_dict) > 1:
raise XenAPI.Failure(
("ERROR, find more than one host ref with ip \
%(slave_ip)s ") % {'slave_ip': host_ip})
value = list(rec_dict.values())[0]
return value['host']
else:
return session.xenapi.session.get_this_host(session.handle)
def call_xenapi(self, method, *args):
"""Call the specified XenAPI method on a background thread."""
with self._get_session() as session:
return session.xenapi_request(method, args)
def call_plugin(self, plugin, fn, args):
"""Call host.call_plugin on a background thread."""
# NOTE(armando): pass the host uuid along with the args so that
# the plugin gets executed on the right host when using XS pools
args['host_uuid'] = self.host_uuid
if not plugin.endswith('.py'):
plugin = '%s.py' % plugin
with self._get_session() as session:
return self._unwrap_plugin_exceptions(
session.xenapi.host.call_plugin,
self.host_ref, plugin, fn, args)
def call_plugin_serialized(self, plugin, fn, *args, **kwargs):
params = {'params': pickle.dumps(dict(args=args, kwargs=kwargs))}
rv = self.call_plugin(plugin, fn, params)
return pickle.loads(rv)
def call_plugin_serialized_with_retry(self, plugin, fn, num_retries,
callback, retry_cb=None, *args,
**kwargs):
"""Allows a plugin to raise RetryableError so we can try again."""
attempts = num_retries + 1
sleep_time = 0.5
for attempt in range(1, attempts + 1):
try:
if attempt > 1:
time.sleep(sleep_time)
sleep_time = min(2 * sleep_time, 15)
callback_result = None
if callback:
callback_result = callback(kwargs)
msg = ('%(plugin)s.%(fn)s attempt %(attempt)d/%(attempts)d, '
'callback_result: %(callback_result)s')
LOG.debug(msg,
{'plugin': plugin, 'fn': fn, 'attempt': attempt,
'attempts': attempts,
'callback_result': callback_result})
return self.call_plugin_serialized(plugin, fn, *args, **kwargs)
except XenAPI.Failure as exc:
if self._is_retryable_exception(exc, fn):
LOG.warning(_LW('%(plugin)s.%(fn)s failed. '
'Retrying call.'),
{'plugin': plugin, 'fn': fn})
if retry_cb:
retry_cb(exc=exc)
else:
raise
except socket.error as exc:
if exc.errno == errno.ECONNRESET:
LOG.warning(_LW('Lost connection to XenAPI during call to '
'%(plugin)s.%(fn)s. Retrying call.'),
{'plugin': plugin, 'fn': fn})
if retry_cb:
retry_cb(exc=exc)
else:
raise
raise exception.PluginRetriesExceeded(num_retries=num_retries)
def _is_retryable_exception(self, exc, fn):
_type, method, error = exc.details[:3]
if error == 'RetryableError':
LOG.debug("RetryableError, so retrying %(fn)s", {'fn': fn},
exc_info=True)
return True
if "signal" in method:
LOG.debug("Error due to a signal, retrying %(fn)s", {'fn': fn},
exc_info=True)
return True
else:
return False
def _create_session(self, url):
"""Stubout point. This can be replaced with a mock session."""
self.is_local_connection = url == "unix://local"
if self.is_local_connection:
return XenAPI.xapi_local()
return XenAPI.Session(url)
def _create_session_and_login(self, url, user, pw):
session = self._create_session(url)
self._login_with_password(user, pw, session)
return session
def _unwrap_plugin_exceptions(self, func, *args, **kwargs):
"""Parse exception details."""
try:
return func(*args, **kwargs)
except XenAPI.Failure as exc:
LOG.debug("Got exception: %s", exc)
if (len(exc.details) == 4 and
exc.details[0] == 'XENAPI_PLUGIN_EXCEPTION' and
exc.details[2] == 'Failure'):
params = None
try:
params = ast.literal_eval(exc.details[3])
except Exception:
raise exc
raise XenAPI.Failure(params)
else:
raise
except xmlrpclib.ProtocolError as exc:
LOG.debug("Got exception: %s", exc)
raise
def get_rec(self, record_type, ref):
try:
return self.call_xenapi('%s.get_record' % record_type, ref)
except XenAPI.Failure as e:
if e.details[0] != 'HANDLE_INVALID':
raise
return None
def get_all_refs_and_recs(self, record_type):
"""Retrieve all refs and recs for a Xen record type.
Handles race-conditions where the record may be deleted between
the `get_all` call and the `get_record` call.
"""
return self.call_xenapi('%s.get_all_records' % record_type).items()
@contextlib.contextmanager
def custom_task(self, label, desc=''):
"""Return exclusive session for scope of with statement."""
name = '%s-%s' % (self.originator, label)
task_ref = self.call_xenapi("task.create", name, desc)
try:
LOG.debug('Created task %s with ref %s', name, task_ref)
yield task_ref
finally:
self.call_xenapi("task.destroy", task_ref)
LOG.debug('Destroyed task ref %s', task_ref)
@contextlib.contextmanager
def http_connection(self):
conn = None
xs_url = urllib.parse.urlparse(self.url)
LOG.debug("Creating http(s) connection to %s", self.url)
if xs_url.scheme == 'http':
conn = http_client.HTTPConnection(xs_url.netloc)
elif xs_url.scheme == 'https':
conn = http_client.HTTPSConnection(xs_url.netloc)
conn.connect()
try:
yield conn
finally:
conn.close()
def is_xsm_sr_check_relaxed(self):
if self._cached_xsm_sr_relaxed is None:
config_value = self.call_plugin('config_file', 'get_val',
dict(key='relax-xsm-sr-check'))
if not config_value:
version_str = '.'.join(str(v) for v in self.platform_version)
if versionutils.is_compatible('2.1.0', version_str,
same_major=False):
self._cached_xsm_sr_relaxed = True
else:
self._cached_xsm_sr_relaxed = False
else:
self._cached_xsm_sr_relaxed = config_value.lower() == 'true'
return self._cached_xsm_sr_relaxed
|