summaryrefslogtreecommitdiffstats
path: root/src/software/providers/LMI_SoftwareInstalledPackage.py
blob: 008a47ba00011623cdd268cd7382d722613b3bdc (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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# -*- encoding: utf-8 -*-
# Software Management Providers
#
# Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Python Provider for LMI_SoftwareInstalledPackage

Instruments the CIM class LMI_SoftwareInstalledPackage

"""

import itertools
import pywbem
from pywbem.cim_provider2 import CIMProvider2
from LMI_SoftwarePackage import pkg2model, LMI_SoftwarePackage
from LMI_SoftwareFileCheck import filecheck2model
from util.common import *

class LMI_SoftwareInstalledPackage(CIMProvider2):
    """Instrument the CIM class LMI_SoftwareInstalledPackage

    The InstalledSoftwareElement association allows the identification of
    the ComputerSystem on which a particular SoftwareElement is installed.

    """

    def __init__ (self, env):
        logger = env.get_logger()
        logger.log_debug('Initializing provider %s from %s' \
                % (self.__class__.__name__, __file__))

    def get_instance(self, env, model):
        """Return an instance.

        Keyword arguments:
        env -- Provider Environment (pycimmb.ProviderEnvironment)
        model -- A template of the pywbem.CIMInstance to be returned.  The
            key properties are set on this instance to correspond to the
            instanceName that was requested.  The properties of the model
            are already filtered according to the PropertyList from the
            request.  Only properties present in the model need to be
            given values.  If you prefer, you can set all of the
            values, and the instance will be filtered for you.

        Possible Errors:
        CIM_ERR_ACCESS_DENIED
        CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
            or otherwise incorrect parameters)
        CIM_ERR_NOT_FOUND (the CIM Class does exist, but the requested CIM
            Instance does not exist in the specified namespace)
        CIM_ERR_FAILED (some other unspecified error occurred)

        """

        logger = env.get_logger()
        logger.log_debug('Entering %s.get_instance()' \
                % self.__class__.__name__)

        check_computer_system_op(env, model['System'])
        with YumDB.getInstance(env):
            pkg = SoftwarePackage.object_path2pkg(env, model['Software'])
            model['Software'] = pkg2model(env, pkg, True)
        return model

    def enum_instances(self, env, model, keys_only):
        """Enumerate instances.

        The WBEM operations EnumerateInstances and EnumerateInstanceNames
        are both mapped to this method.
        This method is a python generator

        Keyword arguments:
        env -- Provider Environment (pycimmb.ProviderEnvironment)
        model -- A template of the pywbem.CIMInstances to be generated.
            The properties of the model are already filtered according to
            the PropertyList from the request.  Only properties present in
            the model need to be given values.  If you prefer, you can
            always set all of the values, and the instance will be filtered
            for you.
        keys_only -- A boolean.  True if only the key properties should be
            set on the generated instances.

        Possible Errors:
        CIM_ERR_FAILED (some other unspecified error occurred)

        """

        logger = env.get_logger()
        logger.log_debug('Entering %s.enum_instances()' \
                % self.__class__.__name__)

        # Prime model.path with knowledge of the keys, so key values on
        # the CIMInstanceName (model.path) will automatically be set when
        # we set property values on the model.
        model.path.update({'System': None, 'Software': None})

        yum_package_path = pywbem.CIMInstanceName("LMI_SoftwarePackage",
                namespace=model.path.namespace,
                host=model.path.host)
        model['System'] = get_computer_system_op()
        with YumDB.getInstance(env) as yb:
            for rpm in yb.rpmdb:
                iname = pkg2model(env, rpm, True, yum_package_path)
                model['Software'] = iname
                if keys_only:
                    yield model
                else:
                    try:
                        yield self.get_instance(env, model)
                    except pywbem.CIMError, (num, msg):
                        if num not in (pywbem.CIM_ERR_NOT_FOUND,
                                       pywbem.CIM_ERR_ACCESS_DENIED):
                            raise

    def set_instance(self, env, instance, modify_existing):
        """Return a newly created or modified instance.

        Keyword arguments:
        env -- Provider Environment (pycimmb.ProviderEnvironment)
        instance -- The new pywbem.CIMInstance.  If modifying an existing
            instance, the properties on this instance have been filtered by
            the PropertyList from the request.
        modify_existing -- True if ModifyInstance, False if CreateInstance

        Return the new instance.  The keys must be set on the new instance.

        Possible Errors:
        CIM_ERR_ACCESS_DENIED
        CIM_ERR_NOT_SUPPORTED
        CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
            or otherwise incorrect parameters)
        CIM_ERR_ALREADY_EXISTS (the CIM Instance already exists -- only
            valid if modify_existing is False, indicating that the operation
            was CreateInstance)
        CIM_ERR_NOT_FOUND (the CIM Instance does not exist -- only valid
            if modify_existing is True, indicating that the operation
            was ModifyInstance)
        CIM_ERR_FAILED (some other unspecified error occurred)

        """

        logger = env.get_logger()
        logger.log_debug('Entering %s.set_instance()' \
                % self.__class__.__name__)

        # parse and check arguments
        if modify_existing is True:
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "MofifyInstance is not supported")

        check_computer_system_op(env, instance['System'])

        with YumDB.getInstance(env) as yb:
            pkg = SoftwarePackage.object_path2pkg_search(env, instance['Software'])
            if isinstance(pkg, yum.rpmsack.RPMInstalledPackage):
                raise pywbem.CIMError(pywbem.CIM_ERR_ALREADY_EXISTS,
                        "Package is already installed.")

            logger.log_info('installing package {}'.format(pkg.nevra))
            # install
            yb.install(pkg)
            yb.buildTransaction()
            yb.processTransaction()
            logger.log_info('package installed'.format(pkg.nevra))

            # return instance
            pkg_iname = pkg2model(env, pkg, True)
        pkg_iname["SoftwareElementState"] = \
                LMI_SoftwarePackage.Values.SoftwareElementState.Executable
        instance["Software"] = pkg_iname
        return LMI_SoftwareInstalledPackage(env).get_instance(env, instance)

    def delete_instance(self, env, instance_name):
        """Delete an instance.

        Keyword arguments:
        env -- Provider Environment (pycimmb.ProviderEnvironment)
        instance_name -- A pywbem.CIMInstanceName specifying the instance
            to delete.

        Possible Errors:
        CIM_ERR_ACCESS_DENIED
        CIM_ERR_NOT_SUPPORTED
        CIM_ERR_INVALID_NAMESPACE
        CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
            or otherwise incorrect parameters)
        CIM_ERR_INVALID_CLASS (the CIM Class does not exist in the specified
            namespace)
        CIM_ERR_NOT_FOUND (the CIM Class does exist, but the requested CIM
            Instance does not exist in the specified namespace)
        CIM_ERR_FAILED (some other unspecified error occurred)

        """

        logger = env.get_logger()
        logger.log_debug('Entering %s.delete_instance()' \
                % self.__class__.__name__)

        check_computer_system_op(env, instance_name['System'])
        with YumDB.getInstance(env) as yb:
            pkg = SoftwarePackage.object_path2pkg(env, instance_name["Software"])
            logger.log_info('removing package "%s"' % pkg.nevra)
            yb.remove(pkg)
            yb.buildTransaction()
            yb.processTransaction()
            logger.log_info('package "%s" removed' % pkg.nevra)

    def references(self, env, object_name, model, result_class_name, role,
                   result_role, keys_only):
        """Instrument Associations.

        All four association-related operations (Associators, AssociatorNames,
        References, ReferenceNames) are mapped to this method.
        This method is a python generator

        Keyword arguments:
        env -- Provider Environment (pycimmb.ProviderEnvironment)
        object_name -- A pywbem.CIMInstanceName that defines the source
            CIM Object whose associated Objects are to be returned.
        model -- A template pywbem.CIMInstance to serve as a model
            of the objects to be returned.  Only properties present on this
            model need to be set.
        result_class_name -- If not empty, this string acts as a filter on
            the returned set of Instances by mandating that each returned
            Instances MUST represent an association between object_name
            and an Instance of a Class whose name matches this parameter
            or a subclass.
        role -- If not empty, MUST be a valid Property name. It acts as a
            filter on the returned set of Instances by mandating that each
            returned Instance MUST refer to object_name via a Property
            whose name matches the value of this parameter.
        result_role -- If not empty, MUST be a valid Property name. It acts
            as a filter on the returned set of Instances by mandating that
            each returned Instance MUST represent associations of
            object_name to other Instances, where the other Instances play
            the specified result_role in the association (i.e. the
            name of the Property in the Association Class that refers to
            the Object related to object_name MUST match the value of this
            parameter).
        keys_only -- A boolean.  True if only the key properties should be
            set on the generated instances.

        The following diagram may be helpful in understanding the role,
        result_role, and result_class_name parameters.
        +------------------------+                    +-------------------+
        | object_name.classname  |                    | result_class_name |
        | ~~~~~~~~~~~~~~~~~~~~~  |                    | ~~~~~~~~~~~~~~~~~ |
        +------------------------+                    +-------------------+
           |              +-----------------------------------+      |
           |              |  [Association] model.classname    |      |
           | object_name  |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    |      |
           +--------------+ object_name.classname REF role    |      |
        (CIMInstanceName) | result_class_name REF result_role +------+
                          |                                   |(CIMInstanceName)
                          +-----------------------------------+

        Possible Errors:
        CIM_ERR_ACCESS_DENIED
        CIM_ERR_NOT_SUPPORTED
        CIM_ERR_INVALID_NAMESPACE
        CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
            or otherwise incorrect parameters)
        CIM_ERR_FAILED (some other unspecified error occurred)

        """

        logger = env.get_logger()
        logger.log_debug('Entering %s.references()' \
                % self.__class__.__name__)
        ch = env.get_cimom_handle()

        # If you want to get references for free, implemented in terms
        # of enum_instances, just leave the code below unaltered.
        if ch.is_subclass(object_name.namespace,
                          sub=object_name.classname,
                          super='CIM_ComputerSystem') or \
                ch.is_subclass(object_name.namespace,
                               sub=object_name.classname,
                               super='LMI_SoftwarePackage'):
            return self.simple_refs(env, object_name, model,
                          result_class_name, role, result_role, keys_only)

    def cim_method_checkintegrity(self, env, object_name):
        """Implements LMI_SoftwarePackage.CheckIntegrity()

        Verify existence and attributes of files installed from RPM
        package. If all files installed exist and their attributes
        matches, method returns "Pass". "Not passed" is returned when
        arbitrary file differs in its attributes. And "Error" if
        verification could not be done.

        Keyword arguments:
        env -- Provider Environment (pycimmb.ProviderEnvironment)
        object_name -- A pywbem.CIMInstanceName or pywbem.CIMCLassName
            specifying the object on which the method CheckIntegrity()
            should be invoked.

        Returns a two-tuple containing the return value (
            type pywbem.Uint32 self.Values.CheckIntegrity)
        and a list of CIMParameter objects representing the output parameters

        Output parameters:
        Failed -- (type REF (pywbem.CIMInstanceName(
                classname='LMI_SoftwareFileCheck', ...))
            Array of references to LMI_SoftwareFileCheck, that did not pass
            verification.

        Possible Errors:
        CIM_ERR_ACCESS_DENIED
        CIM_ERR_INVALID_PARAMETER (including missing, duplicate,
            unrecognized or otherwise incorrect parameters)
        CIM_ERR_NOT_FOUND (the target CIM Class or instance does not
            exist in the specified namespace)
        CIM_ERR_METHOD_NOT_AVAILABLE (the CIM Server is unable to honor
            the invocation request)
        CIM_ERR_FAILED (some other unspecified error occurred)

        """

        logger = env.get_logger()
        logger.log_debug('Entering %s.cim_method_checkintegrity()' \
                % self.__class__.__name__)

        failed = []
        with YumDB.getInstance(env) as yb:
            pkg = SoftwarePackage.object_path2pkg(env, object_name['Software'])
            csum = SoftwareFileCheck.pkg_checksum_type(pkg)
            vpkg = yum.packages._RPMVerifyPackage(
                    pkg, pkg.hdr.fiFromHeader(), csum, [], True)
            for vpf in vpkg:
                fc = SoftwareFileCheck.test_file(env, csum, vpf)
                if SoftwareFileCheck.filecheck_passed(fc): continue
                failed.append(filecheck2model(
                        vpkg, vpf.filename, env, keys_only=True, fc=fc))
        out_params = [ pywbem.CIMParameter('Failed', type='reference',
                value=failed) ]
        return ( getattr(self.Values.CheckIntegrity,
                    'Pass' if len(failed) == 0 else 'Not_passed')
               , out_params )

    def cim_method_update(self, env, object_name,
            param_epoch=None,
            param_release=None,
            param_version=None):
        """Implements LMI_SoftwareInstalledPackage.Update()

        Updates the package to latest version. When any of "version" or
        "release" argument is given, install only matching available
        package. Otherwise try to update to newest package available.

        Keyword arguments:
        env -- Provider Environment (pycimmb.ProviderEnvironment)
        object_name -- A pywbem.CIMInstanceName or pywbem.CIMCLassName
            specifying the object on which the method Update()
            should be invoked.
        param_release --  The input parameter release (type unicode)
            Specify particular release of package to update to. Update to
            newest, when empty
        param_version --  The input parameter version (type unicode)
            Specify particular version of package to update to. Update to
            newest, when empty

        Returns a two-tuple containing the return value (
            type pywbem.Uint16 self.Values.Update)
        and a list of CIMParameter objects representing the output parameters

        Output parameters:
        Installed -- (type REF (pywbem.CIMInstanceName(
                classname='LMI_SoftwareInstalledPackage', ...))
            The reference to newly installed package, if installation was
            successful. Otherwise reference to current package.

        Possible Errors:
        CIM_ERR_ACCESS_DENIED
        CIM_ERR_INVALID_PARAMETER (including missing, duplicate,
            unrecognized or otherwise incorrect parameters)
        CIM_ERR_NOT_FOUND (the target CIM Class or instance does not
            exist in the specified namespace)
        CIM_ERR_METHOD_NOT_AVAILABLE (the CIM Server is unable to honor
            the invocation request)
        CIM_ERR_FAILED (some other unspecified error occurred)

        """

        logger = env.get_logger()
        logger.log_debug('Entering %s.cim_method_update()' \
                % self.__class__.__name__)

        check_computer_system_op(env, object_name['System'])

        with YumDB.getInstance(env) as yb:
            orig = SoftwarePackage.object_path2pkg(env, object_name['Software'])

            evr_str = []
            for name, param in (
                    ('epoch', param_epoch),
                    ('version', param_version),
                    ('release', param_release)):
                evr_str.append("%s(%s)"%(name,param))
            if len(evr_str):
                evr_str = "specific "+'-'.join(evr_str)
            else:
                evr_str = "the newest version-release"

            logger.log_info('trying to update to %s of package \"%s\"' %
                    (evr_str, object_name["Software"]["Name"]))

            pl = yb.doPackageLists('all', showdups=True)
            exact,_,_ = yum.packages.parsePackages(
                    itertools.chain(pl.available, pl.installed),
                    [orig.name])
            # NOTE: available ∩ installed = ∅
            exact = sorted(exact, key=lambda a:a.evra)

            try:
                pkg = [ p for p in exact
                    if     (not param_epoch   or param_epoch == p.epoch)
                       and (not param_version or param_version == p.ver)
                       and (not param_release or param_release == p.rel) ] [-1]
            except IndexError:
                logger.log_error(
                        'could not find any matching available package')
                raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND)
            out_params = [pywbem.CIMParameter('Installed', type='reference',
                    value=pkg2model(env, pkg, True))]
            if orig.evra == pkg.evra:
                logger.log_info('already up to date')
                return (self.Values.Update.Already_newest, out_params)

            yb.update(update_to=True,
                    name=pkg.name,
                    epoch=pkg.epoch,
                    version=pkg.version,
                    release=pkg.release)
            yb.buildTransaction()
            yb.processTransaction()
            logger.log_info('package {} updated to: {}'.format(
                    orig.name, pkg.evra))

        out_params[0].value["SoftwareElementState"] = \
                LMI_SoftwarePackage.Values.SoftwareElementState.Executable

        return (self.Values.Update.Successful_installation, out_params)

    class Values(object):
        class Update(object):
            Already_newest = pywbem.Uint16(0)
            Successful_installation = pywbem.Uint16(1)
            Failed = pywbem.Uint16(2)

        class CheckIntegrity(object):
            Pass = pywbem.Uint32(0)
            Not_passed = pywbem.Uint32(1)
            Error = pywbem.Uint32(2)

## end of class LMI_SoftwareInstalledPackage

## get_providers() for associating CIM Class Name to python provider class name

def get_providers(env):
    lmi_softwareinstalledpackage_prov = LMI_SoftwareInstalledPackage(env)
    return {'LMI_SoftwareInstalledPackage': lmi_softwareinstalledpackage_prov}