summaryrefslogtreecommitdiff
path: root/virtinst/pollhelpers.py
blob: 20edc522d9e7faf84dc213d1fd434bfb5c9c804e (plain)
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
#
# Copyright (C) 2013 Red Hat, Inc.
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
#

from .logger import log


# Debugging helper to force old style polling
# Can be enabled with virt-manager --test-old-poll
FORCE_OLD_POLL = False


def _new_poll_helper(origmap, typename, listfunc, buildfunc):
    """
    Helper for new style listAll* APIs
    """
    current = {}
    new = {}
    objs = []

    try:
        objs = listfunc()
    except Exception as e:
        log.debug("Unable to list all %ss: %s", typename, e)

    for obj in objs:
        connkey = obj.name()

        if connkey not in origmap:
            # Object is brand new this period
            current[connkey] = buildfunc(obj, connkey)
            new[connkey] = current[connkey]
        else:
            # Previously known object
            current[connkey] = origmap[connkey]
            del(origmap[connkey])

    return (list(origmap.values()), list(new.values()), list(current.values()))


def _old_poll_helper(origmap, typename,
                     active_list, inactive_list,
                     lookup_func, build_func):
    """
    Helper routine for old style split API libvirt polling.
    @origmap: Pre-existing mapping of objects, with connkey->obj mapping.
        objects must have an is_active and set_active API
    @typename: string describing type of objects we are polling for use
        in debug messages.
    @active_list: Function that returns the list of active objects
    @inactive_list: Function that returns the list of inactive objects
    @lookup_func: Function to get an object handle for the passed name
    @build_func: Function that builds a new object class. It is passed
        args of (raw libvirt object, connkey)
    """
    current = {}
    new = {}
    newActiveNames = []
    newInactiveNames = []

    try:
        newActiveNames = active_list()
    except Exception as e:
        log.debug("Unable to list active %ss: %s", typename, e)
    try:
        newInactiveNames = inactive_list()
    except Exception as e:
        log.debug("Unable to list inactive %ss: %s", typename, e)

    def check_obj(name):
        obj = None
        connkey = name

        if connkey not in origmap:
            try:
                obj = lookup_func(name)
            except Exception as e:
                log.debug("Could not fetch %s '%s': %s",
                              typename, connkey, e)
                return

            # Object is brand new this period
            current[connkey] = build_func(obj, connkey)
            new[connkey] = current[connkey]
        else:
            # Previously known object
            current[connkey] = origmap[connkey]
            del(origmap[connkey])

    for name in newActiveNames + newInactiveNames:
        try:
            check_obj(name)
        except Exception:
            log.exception("Couldn't fetch %s '%s'", typename, name)

    return (list(origmap.values()), list(new.values()), list(current.values()))


def fetch_nets(backend, origmap, build_func):
    name = "network"

    if backend.support.conn_listallnetworks() and not FORCE_OLD_POLL:
        return _new_poll_helper(origmap, name,
                                backend.listAllNetworks, build_func)
    else:
        active_list = backend.listNetworks
        inactive_list = backend.listDefinedNetworks
        lookup_func = backend.networkLookupByName

        return _old_poll_helper(origmap, name,
                                active_list, inactive_list,
                                lookup_func, build_func)


def fetch_pools(backend, origmap, build_func):
    name = "pool"

    if backend.support.conn_listallstoragepools() and not FORCE_OLD_POLL:
        return _new_poll_helper(origmap, name,
                                backend.listAllStoragePools, build_func)
    else:
        active_list = backend.listStoragePools
        inactive_list = backend.listDefinedStoragePools
        lookup_func = backend.storagePoolLookupByName

        return _old_poll_helper(origmap, name,
                                active_list, inactive_list,
                                lookup_func, build_func)


def fetch_volumes(backend, pool, origmap, build_func):
    name = "volume"

    if backend.support.pool_listallvolumes(pool) and not FORCE_OLD_POLL:
        return _new_poll_helper(origmap, name,
                                pool.listAllVolumes, build_func)
    else:
        active_list = pool.listVolumes
        def inactive_list():
            return []
        lookup_func = pool.storageVolLookupByName
        return _old_poll_helper(origmap, name,
                                active_list, inactive_list,
                                lookup_func, build_func)


def fetch_interfaces(backend, origmap, build_func):
    name = "interface"

    if backend.support.conn_listallinterfaces() and not FORCE_OLD_POLL:
        return _new_poll_helper(origmap, name,
                                backend.listAllInterfaces, build_func)
    else:
        active_list = backend.listInterfaces
        inactive_list = backend.listDefinedInterfaces
        lookup_func = backend.interfaceLookupByName

        return _old_poll_helper(origmap, name,
                                active_list, inactive_list,
                                lookup_func, build_func)


def fetch_nodedevs(backend, origmap, build_func):
    name = "nodedev"
    if backend.support.conn_listalldevices() and not FORCE_OLD_POLL:
        return _new_poll_helper(origmap, name,
                                backend.listAllDevices, build_func)
    else:
        def active_list():
            return backend.listDevices(None, 0)
        def inactive_list():
            return []
        lookup_func = backend.nodeDeviceLookupByName
        return _old_poll_helper(origmap, name,
                                active_list, inactive_list,
                                lookup_func, build_func)


def _old_fetch_vms(backend, origmap, build_func):
    # We can't easily use _old_poll_helper here because the domain API
    # doesn't always return names like other objects, it returns
    # IDs for active VMs

    newActiveIDs = []
    newInactiveNames = []
    oldActiveIDs = {}
    oldInactiveNames = {}
    current = {}
    new = {}

    # Build list of previous vms with proper id/name mappings
    for vm in list(origmap.values()):
        if vm.is_active():
            oldActiveIDs[vm.get_id()] = vm
        else:
            oldInactiveNames[vm.get_name()] = vm

    try:
        newActiveIDs = backend.listDomainsID()
    except Exception as e:
        log.debug("Unable to list active domains: %s", e)

    try:
        newInactiveNames = backend.listDefinedDomains()
    except Exception as e:
        log.exception("Unable to list inactive domains: %s", e)

    def add_vm(vm):
        connkey = vm.get_name()

        current[connkey] = vm
        del(origmap[connkey])

    def check_new(rawvm, connkey):
        if connkey in origmap:
            vm = origmap[connkey]
            del(origmap[connkey])
        else:
            vm = build_func(rawvm, connkey)
            new[connkey] = vm

        current[connkey] = vm

    for _id in newActiveIDs:
        if _id in oldActiveIDs:
            # No change, copy across existing VM object
            vm = oldActiveIDs[_id]
            add_vm(vm)
        else:
            # Check if domain is brand new, or old one that changed state
            try:
                vm = backend.lookupByID(_id)
                connkey = vm.name()

                check_new(vm, connkey)
            except Exception:
                log.exception("Couldn't fetch domain id '%s'", _id)


    for name in newInactiveNames:
        if name in oldInactiveNames:
            # No change, copy across existing VM object
            vm = oldInactiveNames[name]
            add_vm(vm)
        else:
            # Check if domain is brand new, or old one that changed state
            try:
                vm = backend.lookupByName(name)
                connkey = name

                check_new(vm, connkey)
            except Exception:
                log.exception("Couldn't fetch domain '%s'", name)

    return (list(origmap.values()), list(new.values()), list(current.values()))


def fetch_vms(backend, origmap, build_func):
    name = "domain"
    if backend.support.conn_listalldomains():
        return _new_poll_helper(origmap, name,
                                backend.listAllDomains, build_func)
    else:
        return _old_fetch_vms(backend, origmap, build_func)