summaryrefslogtreecommitdiffstats
path: root/src/software/openlmi/software/core/SoftwarePackage.py
blob: de652ac84a2dac2d91528127b5ad70b8bbd2b1e1 (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
# -*- encoding: utf-8 -*-
# Software Management Providers
#
# Copyright (C) 2012-2013 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Michal Minar <miminar@redhat.com>
#

"""
Just a common functionality related to LMI_SoftwarePackage provider.
"""

import pywbem

from openlmi.common import cmpi_logging
from openlmi.software import util
from openlmi.software.yumdb import PackageInfo, YumDB

@cmpi_logging.trace_function
def object_path2pkg(objpath, kind='installed'):
    """
    @param objpath must contain precise information of package,
    otherwise a CIM_ERR_NOT_FOUND error is raised
    @param kind one of {'installed', 'all', 'available'}
    says, where to look for given package
    """
    if not isinstance(objpath, pywbem.CIMInstanceName):
        raise TypeError("objpath must be an instance of CIMInstanceName")
    if not isinstance(kind, basestring):
        raise TypeError("kind must be a string")
    if not kind in ('installed', 'all', 'available'):
        raise ValueError('unsupported package list "%s"' % kind)

    if (  not objpath['Name'] or not objpath['SoftwareElementID']
       or not objpath['SoftwareElementID'].startswith(objpath['Name'])
       or objpath['SoftwareElementID'].find(objpath['Version']) == -1):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND, "Wrong keys.")
    if not util.check_target_operating_system(objpath['TargetOperatingSystem']):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "Wrong target operating system.")
    if not objpath['Name'] or not objpath['Version']:
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                'Both "Name" and "Version" must be given')
    match = util.RE_NEVRA_OPT_EPOCH.match(objpath['SoftwareElementID'])
    if not match:
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "Wrong SotwareElementID. Expected valid nevra"
                " (name-[epoch:]version-release.arch).")
    if objpath['Version'] != match.group('ver'):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
            "Version does not match version part in SoftwareElementID.")
    pkglist = YumDB.getInstance().filter_packages(kind,
            allow_duplicates=kind != 'installed',
            **util.nevra2filter(match))
    if len(pkglist) > 0:
        return pkglist[0]
    raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
        "No matching package found.")

@cmpi_logging.trace_function
def object_path2pkg_search(objpath):
    """
    similar to object_path2pkg, but tries to find best suitable
    package matching keys
    
    If any matching package is already installed, it is returned.
    Otherwise available package with highest version is returned.

    @param objpath may be object of CIMInstance or CIMInstanceName and
    must contain at least \"Name\" or \"SoftwareElementID\"
    @return instance PackageInfo
    """
    if isinstance(objpath, pywbem.CIMInstance):
        def _get_key(k):
            """@return value of instance's key"""
            value = objpath.properties.get(k, None)
            if isinstance(value, pywbem.CIMProperty):
                return value.value
            if value is not None:
                return value
            cmpi_logging.logger.error(
                    'missing key "%s" in inst.props', k)
            return objpath.path[k] if k in objpath.path else None
    elif isinstance(objpath, pywbem.CIMInstanceName):
        _get_key = lambda k: objpath[k] if k in objpath else None
    else:
        raise TypeError("objpath must be either CIMInstance"
                        "or CIMInstanceName")

    # parse and check arguments
    filters = {}
    if _get_key('SoftwareElementID'):
        match = util.RE_NEVRA_OPT_EPOCH.match(_get_key('SoftwareElementID'))
        if not match:
            raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                    "SoftwareElementID could not be parsed.")
        filters = util.nevra2filter(match)
    else:
        for k in ('name', 'epoch', 'version', 'release', 'arch'):
            ikey = k if k != 'arch' else "architecture"
            if _get_key(ikey):
                filters[k] = _get_key(ikey)

    if not filters:
        raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                "Too few key values given (give at least a Name).")
    if not 'name' in filters:
        raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                "Missing either Name or SoftwareElementID property.")

    pkglist = YumDB.getInstance().filter_packages('all',
            allow_duplicates=True, sort=True, **filters)
    if len(pkglist) == 0:
        cmpi_logging.logger.error(
            'could not find any matching package in list: %s',
            [p.nevra for p in pkglist])
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
            "No matching package found.")
    for pkg in pkglist: # check, whether package is already installed
        if pkg.installed:
            return pkg
    cmpi_logging.logger.info(
        ( 'found multiple matching packages'
        if len(pkglist) > 1 else 'exact match found'))
    return pkglist[-1] # select highest version

@cmpi_logging.trace_function
def pkg2model(pkg, keys_only=True, model=None):
    """
    @param model if None, will be filled with data, otherwise
    a new instance of CIMInstance or CIMObjectPath is created
    """
    if not isinstance(pkg, PackageInfo):
        raise TypeError("pkg must be an instance of PackageInfo")
    if model is None:
        model = pywbem.CIMInstanceName('LMI_SoftwarePackage',
                namespace='root/cimv2')
        if not keys_only:
            model = pywbem.CIMInstance('LMI_SoftwarePackage', path=model)
    if isinstance(model, pywbem.CIMInstance):
        def _set_key(k, value):
            """Sets the value of key property of cim instance"""
            model[k] = value
            model.path[k] = value #pylint: disable=E1103
    else:
        _set_key = model.__setitem__
    _set_key('Name', pkg.name)
    _set_key('SoftwareElementID', pkg.nevra)
    _set_key('SoftwareElementState', 
                 Values.SoftwareElementState.Executable
            if   pkg.installed
            else Values.SoftwareElementState.Installable)
    _set_key('TargetOperatingSystem', 
            pywbem.Uint16(util.get_target_operating_system()[0]))
    _set_key('Version', pkg.version)
    if not keys_only:
        model['Caption']      = pkg.summary
        model['Description']  = pkg.description
        if pkg.installed:
            model['InstallDate'] = pywbem.CIMDateTime(pkg.install_time)
        if pkg.vendor:
            model['Manufacturer'] = pkg.vendor
        model['Release']      = pkg.release
        model['Epoch']        = pywbem.Uint16(pkg.epoch)
        model["Architecture"] = pkg.arch
        model['License']      = pkg.license
        model['Group']        = pkg.group
        model['Size']         = pywbem.Uint64(pkg.size)
    return model

class Values(object):
    class DetailedStatus(object):
        Not_Available = pywbem.Uint16(0)
        No_Additional_Information = pywbem.Uint16(1)
        Stressed = pywbem.Uint16(2)
        Predictive_Failure = pywbem.Uint16(3)
        Non_Recoverable_Error = pywbem.Uint16(4)
        Supporting_Entity_in_Error = pywbem.Uint16(5)
        # DMTF_Reserved = ..
        # Vendor_Reserved = 0x8000..

    class Status(object):
        OK = 'OK'
        Error = 'Error'
        Degraded = 'Degraded'
        Unknown = 'Unknown'
        Pred_Fail = 'Pred Fail'
        Starting = 'Starting'
        Stopping = 'Stopping'
        Service = 'Service'
        Stressed = 'Stressed'
        NonRecover = 'NonRecover'
        No_Contact = 'No Contact'
        Lost_Comm = 'Lost Comm'
        Stopped = 'Stopped'

    class HealthState(object):
        Unknown = pywbem.Uint16(0)
        OK = pywbem.Uint16(5)
        Degraded_Warning = pywbem.Uint16(10)
        Minor_failure = pywbem.Uint16(15)
        Major_failure = pywbem.Uint16(20)
        Critical_failure = pywbem.Uint16(25)
        Non_recoverable_error = pywbem.Uint16(30)
        # DMTF_Reserved = ..
        # Vendor_Specific = 32768..65535

    class TargetOperatingSystem(object):
        Unknown = pywbem.Uint16(0)
        Other = pywbem.Uint16(1)
        MACOS = pywbem.Uint16(2)
        ATTUNIX = pywbem.Uint16(3)
        DGUX = pywbem.Uint16(4)
        DECNT = pywbem.Uint16(5)
        Tru64_UNIX = pywbem.Uint16(6)
        OpenVMS = pywbem.Uint16(7)
        HPUX = pywbem.Uint16(8)
        AIX = pywbem.Uint16(9)
        MVS = pywbem.Uint16(10)
        OS400 = pywbem.Uint16(11)
        OS_2 = pywbem.Uint16(12)
        JavaVM = pywbem.Uint16(13)
        MSDOS = pywbem.Uint16(14)
        WIN3x = pywbem.Uint16(15)
        WIN95 = pywbem.Uint16(16)
        WIN98 = pywbem.Uint16(17)
        WINNT = pywbem.Uint16(18)
        WINCE = pywbem.Uint16(19)
        NCR3000 = pywbem.Uint16(20)
        NetWare = pywbem.Uint16(21)
        OSF = pywbem.Uint16(22)
        DC_OS = pywbem.Uint16(23)
        Reliant_UNIX = pywbem.Uint16(24)
        SCO_UnixWare = pywbem.Uint16(25)
        SCO_OpenServer = pywbem.Uint16(26)
        Sequent = pywbem.Uint16(27)
        IRIX = pywbem.Uint16(28)
        Solaris = pywbem.Uint16(29)
        SunOS = pywbem.Uint16(30)
        U6000 = pywbem.Uint16(31)
        ASERIES = pywbem.Uint16(32)
        HP_NonStop_OS = pywbem.Uint16(33)
        HP_NonStop_OSS = pywbem.Uint16(34)
        BS2000 = pywbem.Uint16(35)
        LINUX = pywbem.Uint16(36)
        Lynx = pywbem.Uint16(37)
        XENIX = pywbem.Uint16(38)
        VM = pywbem.Uint16(39)
        Interactive_UNIX = pywbem.Uint16(40)
        BSDUNIX = pywbem.Uint16(41)
        FreeBSD = pywbem.Uint16(42)
        NetBSD = pywbem.Uint16(43)
        GNU_Hurd = pywbem.Uint16(44)
        OS9 = pywbem.Uint16(45)
        MACH_Kernel = pywbem.Uint16(46)
        Inferno = pywbem.Uint16(47)
        QNX = pywbem.Uint16(48)
        EPOC = pywbem.Uint16(49)
        IxWorks = pywbem.Uint16(50)
        VxWorks = pywbem.Uint16(51)
        MiNT = pywbem.Uint16(52)
        BeOS = pywbem.Uint16(53)
        HP_MPE = pywbem.Uint16(54)
        NextStep = pywbem.Uint16(55)
        PalmPilot = pywbem.Uint16(56)
        Rhapsody = pywbem.Uint16(57)
        Windows_2000 = pywbem.Uint16(58)
        Dedicated = pywbem.Uint16(59)
        OS_390 = pywbem.Uint16(60)
        VSE = pywbem.Uint16(61)
        TPF = pywbem.Uint16(62)
        Windows__R__Me = pywbem.Uint16(63)
        Caldera_Open_UNIX = pywbem.Uint16(64)
        OpenBSD = pywbem.Uint16(65)
        Not_Applicable = pywbem.Uint16(66)
        Windows_XP = pywbem.Uint16(67)
        z_OS = pywbem.Uint16(68)
        Microsoft_Windows_Server_2003 = pywbem.Uint16(69)
        Microsoft_Windows_Server_2003_64_Bit = pywbem.Uint16(70)
        Windows_XP_64_Bit = pywbem.Uint16(71)
        Windows_XP_Embedded = pywbem.Uint16(72)
        Windows_Vista = pywbem.Uint16(73)
        Windows_Vista_64_Bit = pywbem.Uint16(74)
        Windows_Embedded_for_Point_of_Service = pywbem.Uint16(75)
        Microsoft_Windows_Server_2008 = pywbem.Uint16(76)
        Microsoft_Windows_Server_2008_64_Bit = pywbem.Uint16(77)
        FreeBSD_64_Bit = pywbem.Uint16(78)
        RedHat_Enterprise_Linux = pywbem.Uint16(79)
        RedHat_Enterprise_Linux_64_Bit = pywbem.Uint16(80)
        Solaris_64_Bit = pywbem.Uint16(81)
        SUSE = pywbem.Uint16(82)
        SUSE_64_Bit = pywbem.Uint16(83)
        SLES = pywbem.Uint16(84)
        SLES_64_Bit = pywbem.Uint16(85)
        Novell_OES = pywbem.Uint16(86)
        Novell_Linux_Desktop = pywbem.Uint16(87)
        Sun_Java_Desktop_System = pywbem.Uint16(88)
        Mandriva = pywbem.Uint16(89)
        Mandriva_64_Bit = pywbem.Uint16(90)
        TurboLinux = pywbem.Uint16(91)
        TurboLinux_64_Bit = pywbem.Uint16(92)
        Ubuntu = pywbem.Uint16(93)
        Ubuntu_64_Bit = pywbem.Uint16(94)
        Debian = pywbem.Uint16(95)
        Debian_64_Bit = pywbem.Uint16(96)
        Linux_2_4_x = pywbem.Uint16(97)
        Linux_2_4_x_64_Bit = pywbem.Uint16(98)
        Linux_2_6_x = pywbem.Uint16(99)
        Linux_2_6_x_64_Bit = pywbem.Uint16(100)
        Linux_64_Bit = pywbem.Uint16(101)
        Other_64_Bit = pywbem.Uint16(102)
        Microsoft_Windows_Server_2008_R2 = pywbem.Uint16(103)
        VMware_ESXi = pywbem.Uint16(104)
        Microsoft_Windows_7 = pywbem.Uint16(105)
        CentOS_32_bit = pywbem.Uint16(106)
        CentOS_64_bit = pywbem.Uint16(107)
        Oracle_Enterprise_Linux_32_bit = pywbem.Uint16(108)
        Oracle_Enterprise_Linux_64_bit = pywbem.Uint16(109)
        eComStation_32_bitx = pywbem.Uint16(110)

    class Remove(object):
        Not_installed = pywbem.Uint32(0)
        Successful_removal = pywbem.Uint32(1)
        Failed = pywbem.Uint32(2)

    class CommunicationStatus(object):
        Unknown = pywbem.Uint16(0)
        Not_Available = pywbem.Uint16(1)
        Communication_OK = pywbem.Uint16(2)
        Lost_Communication = pywbem.Uint16(3)
        No_Contact = pywbem.Uint16(4)
        # DMTF_Reserved = ..
        # Vendor_Reserved = 0x8000..

    class OperationalStatus(object):
        Unknown = pywbem.Uint16(0)
        Other = pywbem.Uint16(1)
        OK = pywbem.Uint16(2)
        Degraded = pywbem.Uint16(3)
        Stressed = pywbem.Uint16(4)
        Predictive_Failure = pywbem.Uint16(5)
        Error = pywbem.Uint16(6)
        Non_Recoverable_Error = pywbem.Uint16(7)
        Starting = pywbem.Uint16(8)
        Stopping = pywbem.Uint16(9)
        Stopped = pywbem.Uint16(10)
        In_Service = pywbem.Uint16(11)
        No_Contact = pywbem.Uint16(12)
        Lost_Communication = pywbem.Uint16(13)
        Aborted = pywbem.Uint16(14)
        Dormant = pywbem.Uint16(15)
        Supporting_Entity_in_Error = pywbem.Uint16(16)
        Completed = pywbem.Uint16(17)
        Power_Mode = pywbem.Uint16(18)
        Relocating = pywbem.Uint16(19)
        # DMTF_Reserved = ..
        # Vendor_Reserved = 0x8000..

    class OperatingStatus(object):
        Unknown = pywbem.Uint16(0)
        Not_Available = pywbem.Uint16(1)
        Servicing = pywbem.Uint16(2)
        Starting = pywbem.Uint16(3)
        Stopping = pywbem.Uint16(4)
        Stopped = pywbem.Uint16(5)
        Aborted = pywbem.Uint16(6)
        Dormant = pywbem.Uint16(7)
        Completed = pywbem.Uint16(8)
        Migrating = pywbem.Uint16(9)
        Emigrating = pywbem.Uint16(10)
        Immigrating = pywbem.Uint16(11)
        Snapshotting = pywbem.Uint16(12)
        Shutting_Down = pywbem.Uint16(13)
        In_Test = pywbem.Uint16(14)
        Transitioning = pywbem.Uint16(15)
        In_Service = pywbem.Uint16(16)
        # DMTF_Reserved = ..
        # Vendor_Reserved = 0x8000..

    class SoftwareElementState(object):
        Deployable = pywbem.Uint16(0)
        Installable = pywbem.Uint16(1)
        Executable = pywbem.Uint16(2)
        Running = pywbem.Uint16(3)

    class PrimaryStatus(object):
        Unknown = pywbem.Uint16(0)
        OK = pywbem.Uint16(1)
        Degraded = pywbem.Uint16(2)
        Error = pywbem.Uint16(3)
        # DMTF_Reserved = ..
        # Vendor_Reserved = 0x8000..

    class Install(object):
        Already_installed = pywbem.Uint32(0)
        Successful_installation = pywbem.Uint32(1)
        Failed = pywbem.Uint32(2)