~ubuntu-branches/ubuntu/utopic/ceilometer/utopic-proposed

« back to all changes in this revision

Viewing changes to ceilometer/compute/virt/libvirt/inspector.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-10-16 14:07:11 UTC
  • mfrom: (1.2.1) (28.1.5 utopic-proposed)
  • Revision ID: package-import@ubuntu.com-20141016140711-95mki6bdkivvfr2x
Tags: 2014.2-0ubuntu1
New upstream release. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
CONF.register_opts(libvirt_opts)
44
44
 
45
45
 
 
46
def retry_on_disconnect(function):
 
47
    def decorator(self, *args, **kwargs):
 
48
        try:
 
49
            return function(self, *args, **kwargs)
 
50
        except libvirt.libvirtError as e:
 
51
            if (e.get_error_code() == libvirt.VIR_ERR_SYSTEM_ERROR and
 
52
                e.get_error_domain() in (libvirt.VIR_FROM_REMOTE,
 
53
                                         libvirt.VIR_FROM_RPC)):
 
54
                LOG.debug('Connection to libvirt broken')
 
55
                self.connection = None
 
56
                return function(self, *args, **kwargs)
 
57
            else:
 
58
                raise
 
59
    return decorator
 
60
 
 
61
 
46
62
class LibvirtInspector(virt_inspector.Inspector):
47
63
 
48
64
    per_type_uris = dict(uml='uml:///system', xen='xen:///', lxc='lxc:///')
56
72
                                                          'qemu:///system')
57
73
 
58
74
    def _get_connection(self):
59
 
        if not self.connection or not self._test_connection():
 
75
        if not self.connection:
60
76
            global libvirt
61
77
            if libvirt is None:
62
78
                libvirt = __import__('libvirt')
63
 
 
64
 
            LOG.debug(_('Connecting to libvirt: %s'), self.uri)
 
79
            LOG.debug('Connecting to libvirt: %s', self.uri)
65
80
            self.connection = libvirt.openReadOnly(self.uri)
66
81
 
67
82
        return self.connection
68
83
 
69
 
    def _test_connection(self):
70
 
        try:
71
 
            self.connection.getCapabilities()
72
 
            return True
73
 
        except libvirt.libvirtError as e:
74
 
            if (e.get_error_code() == libvirt.VIR_ERR_SYSTEM_ERROR and
75
 
                e.get_error_domain() in (libvirt.VIR_FROM_REMOTE,
76
 
                                         libvirt.VIR_FROM_RPC)):
77
 
                LOG.debug(_('Connection to libvirt broke'))
78
 
                return False
79
 
            raise
80
 
 
 
84
    @retry_on_disconnect
81
85
    def _lookup_by_name(self, instance_name):
82
86
        try:
83
87
            return self._get_connection().lookupByName(instance_name)
85
89
            if not libvirt or not isinstance(ex, libvirt.libvirtError):
86
90
                raise virt_inspector.InspectorException(six.text_type(ex))
87
91
            error_code = ex.get_error_code()
 
92
            if (error_code == libvirt.VIR_ERR_SYSTEM_ERROR and
 
93
                ex.get_error_domain() in (libvirt.VIR_FROM_REMOTE,
 
94
                                          libvirt.VIR_FROM_RPC)):
 
95
                raise
88
96
            msg = ("Error from libvirt while looking up %(instance_name)s: "
89
97
                   "[Error Code %(error_code)s] "
90
98
                   "%(ex)s" % {'instance_name': instance_name,
92
100
                               'ex': ex})
93
101
            raise virt_inspector.InstanceNotFoundException(msg)
94
102
 
 
103
    @retry_on_disconnect
 
104
    def inspect_instance(self, domain_id):
 
105
        domain = self._get_connection().lookupByID(domain_id)
 
106
        return virt_inspector.Instance(name=domain.name(),
 
107
                                       UUID=domain.UUIDString())
 
108
 
 
109
    @retry_on_disconnect
95
110
    def inspect_instances(self):
96
111
        if self._get_connection().numOfDomains() > 0:
97
112
            for domain_id in self._get_connection().listDomainsID():
98
 
                try:
99
 
                    # We skip domains with ID 0 (hypervisors).
100
 
                    if domain_id != 0:
101
 
                        domain = self._get_connection().lookupByID(domain_id)
102
 
                        yield virt_inspector.Instance(name=domain.name(),
103
 
                                                      UUID=domain.UUIDString())
104
 
                except libvirt.libvirtError:
105
 
                    # Instance was deleted while listing... ignore it
106
 
                    pass
 
113
                if domain_id != 0:
 
114
                    try:
 
115
                        yield self.inspect_instance(domain_id)
 
116
                    except libvirt.libvirtError:
 
117
                        # Instance was deleted while listing... ignore it
 
118
                        pass
107
119
 
108
120
    def inspect_cpus(self, instance_name):
109
121
        domain = self._lookup_by_name(instance_name)