XenAPI support: List the instances

1) Added XenAPI inspector.
2) Implemented inspect_instances() to list the instances on the XenServer host.

Change-Id: I1f6e33696770ec2f4e5cbb2e54e29991978aed32
Implements: blueprint xenapi-support
This commit is contained in:
Qiaowei Ren 2014-08-06 17:04:03 +08:00
parent 9af6af5e52
commit 2fc9301e15
5 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,105 @@
# Copyright 2014 Intel
#
# Author: Ren Qiaowei <qiaowei.ren@intel.com>
#
# 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.
"""Implementation of Inspector abstraction for XenAPI."""
from eventlet import timeout
from oslo.config import cfg
try:
import XenAPI as api
except ImportError:
api = None
from ceilometer.compute.virt import inspector as virt_inspector
from ceilometer.openstack.common.gettextutils import _
opt_group = cfg.OptGroup(name='xenapi',
title='Options for XenAPI')
xenapi_opts = [
cfg.StrOpt('connection_url',
help='URL for connection to XenServer/Xen Cloud Platform'),
cfg.StrOpt('connection_username',
default='root',
help='Username for connection to XenServer/Xen Cloud Platform'),
cfg.StrOpt('connection_password',
help='Password for connection to XenServer/Xen Cloud Platform',
secret=True),
cfg.IntOpt('login_timeout',
default=10,
help='Timeout in seconds for XenAPI login.'),
]
CONF = cfg.CONF
CONF.register_group(opt_group)
CONF.register_opts(xenapi_opts, group=opt_group)
class XenapiException(virt_inspector.InspectorException):
pass
def get_api_session():
if not api:
raise ImportError(_('XenAPI not installed'))
url = CONF.xenapi.connection_url
username = CONF.xenapi.connection_username
password = CONF.xenapi.connection_password
if not url or password is None:
raise XenapiException(_('Must specify connection_url, and '
'connection_password to use'))
exception = api.Failure(_("Unable to log in to XenAPI "
"(is the Dom0 disk full?)"))
try:
session = api.Session(url)
with timeout.Timeout(CONF.xenapi.login_timeout, exception):
session.login_with_password(username, password)
except api.Failure as e:
msg = _("Could not connect to XenAPI: %s") % e.details[0]
raise XenapiException(msg)
return session
class XenapiInspector(virt_inspector.Inspector):
def __init__(self):
super(XenapiInspector, self).__init__()
self.session = get_api_session()
def _get_host_ref(self):
"""Return the xenapi host on which nova-compute runs on."""
return self.session.xenapi.session.get_this_host(self.session.handle)
def _call_xenapi(self, method, *args):
return self.session.xenapi_request(method, args)
def _list_vms(self):
host_ref = self._get_host_ref()
vms = self._call_xenapi("VM.get_all_records_where",
'field "is_control_domain"="false" and '
'field "is_a_template"="false" and '
'field "resident_on"="%s"' % host_ref)
for vm_ref in vms.keys():
yield vm_ref, vms[vm_ref]
def inspect_instances(self):
for vm_ref, vm_rec in self._list_vms():
name = vm_rec['name_label']
other_config = vm_rec['other_config']
uuid = other_config.get('nova_uuid')
if uuid:
yield virt_inspector.Instance(name, uuid)

View File

@ -0,0 +1,48 @@
# Copyright 2014 Intel
#
# Author: Ren Qiaowei <qiaowei.ren@intel.com>
#
# 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.
"""Tests for xenapi inspector.
"""
import mock
from oslotest import base
from ceilometer.compute.virt.xenapi import inspector as xenapi_inspector
class TestXenapiInspection(base.BaseTestCase):
def setUp(self):
api_session = mock.Mock()
xenapi_inspector.get_api_session = mock.Mock(return_value=api_session)
self.inspector = xenapi_inspector.XenapiInspector()
super(TestXenapiInspection, self).setUp()
def test_inspect_instances(self):
vms = {
'ref': {
'name_label': 'fake_name',
'other_config': {'nova_uuid': 'fake_uuid', },
}
}
session = self.inspector.session
with mock.patch.object(session, 'xenapi_request',
return_value=vms):
inspected_instances = list(self.inspector.inspect_instances())
inspected_instance = inspected_instances[0]
self.assertEqual('fake_name', inspected_instance.name)
self.assertEqual('fake_uuid', inspected_instance.UUID)

View File

@ -199,6 +199,7 @@ ceilometer.compute.virt =
libvirt = ceilometer.compute.virt.libvirt.inspector:LibvirtInspector
hyperv = ceilometer.compute.virt.hyperv.inspector:HyperVInspector
vsphere = ceilometer.compute.virt.vmware.inspector:VsphereInspector
xenapi = ceilometer.compute.virt.xenapi.inspector:XenapiInspector
ceilometer.hardware.inspectors =
snmp = ceilometer.hardware.inspector.snmp:SNMPInspector