summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/Packages/YumHelper.py
blob: f26d6ba1877a0b20e92b93859c0bec7a68f2b33d (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
""" Libraries for bcfg2-yum-helper plugin, used if yum library support
is enabled.  The yum libs have horrific memory leaks, so apparently
the right way to get around that in long-running processes it to have
a short-lived helper.  No, seriously -- check out the yum-updatesd
code.  It's pure madness. """

import os
import sys
import yum
import logging
import Bcfg2.Options
import Bcfg2.Logger
from Bcfg2.Compat import wraps
from lockfile import FileLock, LockTimeout
try:
    import json
except ImportError:
    import simplejson as json


def pkg_to_tuple(package):
    """ json doesn't distinguish between tuples and lists, but yum
    does, so we convert a package in list format to one in tuple
    format """
    if isinstance(package, list):
        return tuple(package)
    else:
        return package


def pkgtup_to_string(package):
    """ given a package tuple, return a human-readable string
    describing the package """
    if package[3] in ['auto', 'any']:
        return package[0]

    rv = [package[0], "-"]
    if package[2]:
        rv.extend([package[2], ':'])
    rv.extend([package[3], '-', package[4]])
    if package[1]:
        rv.extend(['.', package[1]])
    return ''.join(str(e) for e in rv)


class YumHelper(object):
    """ Yum helper base object """

    def __init__(self, cfgfile, verbose=1):
        self.cfgfile = cfgfile
        self.yumbase = yum.YumBase()
        # pylint: disable=E1121,W0212
        try:
            self.yumbase.preconf.debuglevel = verbose
            self.yumbase.preconf.fn = cfgfile
            self.yumbase._getConfig()
        except AttributeError:
            self.yumbase._getConfig(cfgfile, debuglevel=verbose)
        # pylint: enable=E1121,W0212
        self.logger = logging.getLogger(self.__class__.__name__)


class DepSolver(YumHelper):
    """ Yum dependency solver.  This is used for operations that only
    read from the yum cache, and thus operates in cacheonly mode. """

    def __init__(self, cfgfile, verbose=1):
        YumHelper.__init__(self, cfgfile, verbose=verbose)
        # internally, yum uses an integer, not a boolean, for conf.cache
        self.yumbase.conf.cache = 1
        self._groups = None

    def get_groups(self):
        """ getter for the groups property """
        if self._groups is not None:
            return self._groups
        else:
            return ["noarch"]

    def set_groups(self, groups):
        """ setter for the groups property """
        self._groups = set(groups).union(["noarch"])

    groups = property(get_groups, set_groups)

    def get_package_object(self, pkgtup, silent=False):
        """ given a package tuple, get a yum package object """
        try:
            matches = yum.packageSack.packagesNewestByName(
                self.yumbase.pkgSack.searchPkgTuple(pkgtup))
        except yum.Errors.PackageSackError:
            if not silent:
                self.logger.warning("Package '%s' not found" %
                                    self.get_package_name(pkgtup))
            matches = []
        except yum.Errors.RepoError:
            err = sys.exc_info()[1]
            self.logger.error("Temporary failure loading metadata for %s: %s" %
                              (self.get_package_name(pkgtup), err))
            matches = []

        pkgs = self._filter_arch(matches)
        if pkgs:
            return pkgs[0]
        else:
            return None

    def get_group(self, group, ptype="default"):
        """ Resolve a package group name into a list of packages """
        if group.startswith("@"):
            group = group[1:]

        try:
            if self.yumbase.comps.has_group(group):
                group = self.yumbase.comps.return_group(group)
            else:
                self.logger.error("%s is not a valid group" % group)
                return []
        except yum.Errors.GroupsError:
            err = sys.exc_info()[1]
            self.logger.warning(err)
            return []

        if ptype == "default":
            return [p
                    for p, d in list(group.default_packages.items())
                    if d]
        elif ptype == "mandatory":
            return [p
                    for p, m in list(group.mandatory_packages.items())
                    if m]
        elif ptype == "optional" or ptype == "all":
            return group.packages
        else:
            self.logger.warning("Unknown group package type '%s'" % ptype)
            return []

    def _filter_arch(self, packages):
        """ filter packages in the given list that do not have an
        architecture in the list of groups for this client """
        matching = []
        for pkg in packages:
            if pkg.arch in self.groups:
                matching.append(pkg)
            else:
                self.logger.debug("%s has non-matching architecture (%s)" %
                                  (pkg, pkg.arch))
        if matching:
            return matching
        else:
            # no packages match architecture; we'll assume that the
            # user knows what s/he is doing and this is a multiarch
            # box.
            return packages

    def get_package_name(self, package):
        """ get the name of a package or virtual package from the
        internal representation used by this Collection class """
        if isinstance(package, tuple):
            if len(package) == 3:
                return yum.misc.prco_tuple_to_string(package)
            else:
                return pkgtup_to_string(package)
        else:
            return str(package)

    def complete(self, packagelist):
        """ resolve dependencies and generate a complete package list
        from the given list of initial packages """
        packages = set()
        unknown = set()
        for pkg in packagelist:
            if isinstance(pkg, tuple):
                pkgtup = pkg
            else:
                pkgtup = (pkg, None, None, None, None)
            pkgobj = self.get_package_object(pkgtup)
            if not pkgobj:
                self.logger.debug("Unknown package %s" %
                                  self.get_package_name(pkg))
                unknown.add(pkg)
            else:
                if self.yumbase.tsInfo.exists(pkgtup=pkgobj.pkgtup):
                    self.logger.debug("%s added to transaction multiple times"
                                      % pkgobj)
                else:
                    self.logger.debug("Adding %s to transaction" % pkgobj)
                    self.yumbase.tsInfo.addInstall(pkgobj)
        self.yumbase.resolveDeps()

        for txmbr in self.yumbase.tsInfo:
            packages.add(txmbr.pkgtup)
        return list(packages), list(unknown)


def acquire_lock(func):
    """ decorator for CacheManager methods that gets and release a
    lock while the method runs """
    @wraps(func)
    def inner(self, *args, **kwargs):
        """ Get and release a lock while running the function this
        wraps. """
        self.logger.debug("Acquiring lock at %s" % self.lockfile)
        while not self.lock.i_am_locking():
            try:
                self.lock.acquire(timeout=60)  # wait up to 60 seconds
            except LockTimeout:
                self.lock.break_lock()
                self.lock.acquire()
        try:
            func(self, *args, **kwargs)
        finally:
            self.lock.release()
            self.logger.debug("Released lock at %s" % self.lockfile)

    return inner


class CacheManager(YumHelper):
    """ Yum cache manager.  Unlike :class:`DepSolver`, this can write
    to the yum cache, and so is used for operations that muck with the
    cache.  (Technically, :func:`CacheManager.clean_cache` could be in
    either DepSolver or CacheManager, but for consistency I've put it
    here.) """

    def __init__(self, cfgfile, verbose=1):
        YumHelper.__init__(self, cfgfile, verbose=verbose)
        self.lockfile = \
            os.path.join(os.path.dirname(self.yumbase.conf.config_file_path),
                         "lock")
        self.lock = FileLock(self.lockfile)

    @acquire_lock
    def clean_cache(self):
        """ clean the yum cache """
        for mdtype in ["Headers", "Packages", "Sqlite", "Metadata",
                       "ExpireCache"]:
            # for reasons that are entirely obvious, all of the yum
            # API clean* methods return a tuple of 0 (zero, always
            # zero) and a list containing a single message about how
            # many files were deleted.  so useful.  thanks, yum.
            msg = getattr(self.yumbase, "clean%s" % mdtype)()[1][0]
            if not msg.startswith("0 "):
                self.logger.info(msg)

    @acquire_lock
    def populate_cache(self):
        """ populate the yum cache """
        for repo in self.yumbase.repos.findRepos('*'):
            repo.metadata_expire = 0
            repo.mdpolicy = "group:all"
        self.yumbase.doRepoSetup()
        self.yumbase.repos.doSetup()
        for repo in self.yumbase.repos.listEnabled():
            # this populates the cache as a side effect
            repo.repoXML  # pylint: disable=W0104
            try:
                repo.getGroups()
            except yum.Errors.RepoMDError:
                pass  # this repo has no groups
        self.yumbase.repos.populateSack(mdtype='metadata', cacheonly=1)
        self.yumbase.repos.populateSack(mdtype='filelists', cacheonly=1)
        self.yumbase.repos.populateSack(mdtype='otherdata', cacheonly=1)
        # this does something with the groups cache as a side effect
        self.yumbase.comps  # pylint: disable=W0104


class HelperSubcommand(Bcfg2.Options.Subcommand):
    """ Base class for all yum helper subcommands """

    # the value to JSON encode and print out if the command fails
    fallback = None

    # whether or not this command accepts input on stdin
    accept_input = True

    def __init__(self):
        Bcfg2.Options.Subcommand.__init__(self)
        self.verbosity = 0
        if Bcfg2.Options.setup.debug:
            self.verbosity = 5
        elif Bcfg2.Options.setup.verbose:
            self.verbosity = 1

    def run(self, setup):
        try:
            data = json.loads(sys.stdin.read())
        except:  # pylint: disable=W0702
            self.logger.error("Unexpected error decoding JSON input: %s" %
                              sys.exc_info()[1])
            print(json.dumps(self.fallback))
            return 2

        try:
            print(json.dumps(self._run(setup, data)))
        except:  # pylint: disable=W0702
            self.logger.error("Unexpected error running %s: %s" %
                              self.__class__.__name__.lower(),
                              sys.exc_info()[1], exc_info=1)
            print(json.dumps(self.fallback))
            return 2
        return 0

    def _run(self, setup, data):
        """ Actually run the command """
        raise NotImplementedError


class DepSolverSubcommand(HelperSubcommand):  # pylint: disable=W0223
    """ Base class for helper commands that use the depsolver (i.e.,
    only resolve dependencies, don't modify the cache) """

    def __init__(self):
        HelperSubcommand.__init__(self)
        self.depsolver = DepSolver(Bcfg2.Options.setup.yum_config,
                                   self.verbosity)


class CacheManagerSubcommand(HelperSubcommand):  # pylint: disable=W0223
    """ Base class for helper commands that use the cachemanager
    (i.e., modify the cache) """
    fallback = False
    accept_input = False

    def __init__(self):
        HelperSubcommand.__init__(self)
        self.cachemgr = CacheManager(Bcfg2.Options.setup.yum_config,
                                     self.verbosity)


class Clean(CacheManagerSubcommand):
    """ Clean the cache """
    def _run(self, setup, data):  # pylint: disable=W0613
        self.cachemgr.clean_cache()
        return True


class MakeCache(CacheManagerSubcommand):
    """ Update the on-disk cache """
    def _run(self, setup, data):  # pylint: disable=W0613
        self.cachemgr.populate_cache()
        return True


class Complete(DepSolverSubcommand):
    """ Given an initial set of packages, get a complete set of
    packages with all dependencies resolved """
    fallback = dict(packages=[], unknown=[])

    def _run(self, _, data):
        self.depsolver.groups = data['groups']
        self.fallback['unknown'] = data['packages']
        (packages, unknown) = self.depsolver.complete(
            [pkg_to_tuple(p) for p in data['packages']])
        return dict(packages=list(packages), unknown=list(unknown))


class GetGroups(DepSolverSubcommand):
    """ Resolve the given package groups """
    def _run(self, _, data):
        rv = dict()
        for gdata in data:
            if "type" in gdata:
                packages = self.depsolver.get_group(gdata['group'],
                                                    ptype=gdata['type'])
            else:
                packages = self.depsolver.get_group(gdata['group'])
            rv[gdata['group']] = list(packages)
        return rv


Get_Groups = GetGroups  # pylint: disable=C0103


class CLI(Bcfg2.Options.CommandRegistry):
    """ The bcfg2-yum-helper CLI """
    options = [
        Bcfg2.Options.PathOption(
            "-c", "--yum-config", help="Yum config file"),
        Bcfg2.Options.PositionalArgument(
            "command", help="Yum helper command",
            choices=['clean', 'complete', 'get_groups'])]

    def __init__(self):
        Bcfg2.Options.CommandRegistry.__init__(self)
        self.register_commands(globals().values(), parent=HelperSubcommand)
        parser = Bcfg2.Options.get_parser("Bcfg2 yum helper",
                                          components=[self])
        parser.add_options(self.subcommand_options)
        parser.parse()
        self.logger = logging.getLogger(parser.prog)

    def run(self):
        """ Run bcfg2-yum-helper """
        if not os.path.exists(Bcfg2.Options.setup.yum_config):
            self.logger.error("Config file %s not found" %
                              Bcfg2.Options.setup.yum_config)
            return 1
        return self.runcommand()