summaryrefslogtreecommitdiffstats
path: root/src/lib/Client/Tools/YUMng.py
blob: cdc43b00b5446647d56bb90d6a9a380106fb16fe (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
'''This provides bcfg2 support for yum'''
__revision__ = '0.1'

import Bcfg2.Client.Tools.RPMng, ConfigParser, sys

try:
    set
except NameError:
    from sets import Set as set

YAD = True
CP = ConfigParser.ConfigParser()
try:
    if '-C' in sys.argv:
        CP.read([sys.argv[sys.argv.index('-C') + 1]])
    else:
        CP.read(['/etc/bcfg2.conf'])
    if CP.get('YUMng', 'autodep') == 'false':
        YAD = False
except:
    pass

class YUMng(Bcfg2.Client.Tools.RPMng.RPMng):
    '''Support for Yum packages'''
    pkgtype = 'yum'

    __name__ = 'YUMng'
    __execs__ = ['/usr/bin/yum', '/var/lib/rpm']
    __handles__ = [('Package', 'yum'), ('Package', 'rpm')]
 
    __req__ = {'Package': ['name', 'version']}
    __ireq__ = {'Package': ['name', 'version']}
 
    __new_req__ = {'Package': ['name'], 'Instance': ['version', 'release', 'arch']}
    __new_ireq__ = {'Package': ['name', 'uri'], \
                    'Instance': ['simplefile', 'version', 'release', 'arch']}

    __gpg_req__ = {'Package': ['name', 'version']}
    __gpg_ireq__ = {'Package': ['name', 'version']}

    __new_gpg_req__ = {'Package': ['name'], 'Instance': ['version', 'release']}
    __new_gpg_ireq__ = {'Package': ['name'], 'Instance': ['version', 'release']}

    conflicts = ['RPMng']

    def Install(self, packages):
        '''
           Try and fix everything that RPMng.VerifyPackages() found wrong for
           each Package Entry.  This can result in individual RPMs being
           installed (for the first time), deleted, downgraded
           or upgraded. 

           NOTE: YUM can not reinstall a package that it thinks is already
                 installed.

           packages is a list of Package Elements that has
               self.states[<Package Element>] == False

           The following effects occur:
           - self.states{} is conditionally updated for each package.
           - self.installed{} is rebuilt, possibly multiple times.
           - self.instance_status{} is conditionally updated for each instance
             of a package.
           - Each package will be added to self.modified[] if its self.states{}
             entry is set to True.
        '''
        self.logger.info('Runing YUMng.Install()')

        install_pkgs = []
        gpg_keys = []
        upgrade_pkgs = []

        # Remove extra instances.
        # Can not reverify because we don't have a package entry.
        if len(self.extra_instances) > 0:
            self.RemovePackages(self.extra_instances)

        # Figure out which instances of the packages actually need something
        # doing to them and place in the appropriate work 'queue'.
        for pkg in packages:
            for inst in [inst for inst in pkg if inst.tag == 'Instance' or inst.tag == 'Package']:
                if self.instance_status[inst].get('installed', False) == False:
                    if pkg.get('name') == 'gpg-pubkey':
                        gpg_keys.append(inst)
                    else:
                        install_pkgs.append(inst)
                elif self.instance_status[inst].get('version_fail', False) == True:
                    upgrade_pkgs.append(inst)

        # Install GPG keys.
        # Alternatively specify the required keys using 'gpgkey' in the 
        # repository definition in yum.conf.  YUM will install the keys 
        # automatically.
        if len(gpg_keys) > 0:
            for inst in gpg_keys:
                self.logger.info("Installing GPG keys.")
                key_arg = os.path.join(self.instance_status[inst].get('pkg').get('uri'), \
                                                     inst.get('simplefile'))
                cmdrc, output = self.cmd.run("rpm --import %s" % key_arg)
                if cmdrc != 0:
                    self.logger.debug("Unable to install %s-%s" % \
                                              (self.instance_status[inst].get('pkg').get('name'), \
                                               self.str_evra(inst)))
                else:
                    self.logger.debug("Installed %s-%s-%s" % \
                                              (self.instance_status[inst].get('pkg').get('name'), \
                                               inst.get('version'), inst.get('release')))
            self.RefreshPackages()
            self.gpg_keyids = self.getinstalledgpg()
            pkg = self.instance_status[gpg_keys[0]].get('pkg')
            self.states[pkg] = self.VerifyPackage(pkg, [])

        # Install packages.
        if len(install_pkgs) > 0:
            self.logger.info("Attempting to install packages")

            if YAD:
                pkgtool = "/usr/bin/yum -d0 -y install %s"
            else:
                pkgtool = "/usr/bin/yum -d0 install %s"

            install_args = []
            for inst in install_pkgs:
                pkg_arg = self.instance_status[inst].get('pkg').get('name') + '-'
                if inst.get('epoch', False):
                    pkg_arg = pkg_arg + inst.get('epoch') + ':'
                pkg_arg = pkg_arg + inst.get('version') + '-' + inst.get('release')
                if inst.get('arch', False):
                    pkg_arg = pkg_arg + '.' + inst.get('arch')
                install_args.append(pkg_arg)
            
            cmdrc, output = self.cmd.run(pkgtool % " ".join(install_args))
            if cmdrc == 0:
                # The yum command succeeded.  All packages installed.
                self.logger.info("Single Pass for Install Succeded")
                self.RefreshPackages()

                # Reverify all the packages that we might have just changed.
                # There may be multiple instances per package, only do the
                # verification once.
                install_pkg_set = set([self.instance_status[inst].get('pkg') \
                                                      for inst in install_pkgs])
                self.logger.info("Reverifying Installed Packages")
                for inst in install_pkgs:
                    pkg_entry = self.instance_status[inst].get('pkg')
                    if pkg_entry in install_pkg_set:
                        self.logger.debug("Reverifying Installed %s" % \
                                                                      (pkg_entry.get('name')))
                        install_pkg_set.remove(pkg_entry)
                        self.states[pkg_entry] = self.VerifyPackage(pkg_entry, \
                                                         self.instance_status[inst].get('modlist'))
                    else:
                        # We already reverified this pacakge.
                        continue
            else:
                # The yum command failed.  No packages installed.
                # Try installing instances individually.
                self.logger.error("Single Pass Install of Packages Failed")
                installed_instances = []
                for inst in install_pkgs:
                    pkg_arg = self.instance_status[inst].get('pkg').get('name') + '-'
                    if inst.get('epoch', False):
                        pkg_arg = pkg_arg + inst.get('epoch') + ':'
                    pkg_arg = pkg_arg + inst.get('version') + '-' + inst.get('release')
                    if inst.get('arch', False):
                        pkg_arg = pkg_arg + '.' + inst.get('arch')
                    cmdrc, output = self.cmd.run(pkgtool % pkg_arg)
                    if cmdrc == 0:
                        installed_instances.append(inst)
                    else:
                        self.logger.debug("%s %s would not install." % \
                                              (self.instance_status[inst].get('pkg').get('name'), \
                                               self.str_evra(inst)))

                install_pkg_set = set([self.instance_status[inst].get('pkg') \
                                                      for inst in install_pkgs])
                self.RefreshPackages()
                for inst in installed_instances:
                    pkg = inst.get('pkg')
                    # Reverify all the packages that we might have just changed.
                    # There may be multiple instances per package, only do the
                    # verification once.
                    if pkg in install_pkg_set:
                        self.logger.debug("Reverifying Installed Package %s" % \
                                                                      (pkg_entry.get('name')))
                        install_pkg_set.remove(pkg)
                        self.states[pkg_entry] = self.VerifyPackage(pkg, \
                                                         self.instance_status[inst].get('modlist'))
                    else:
                        # We already reverified this pacakge.
                        continue

        # Fix upgradeable packages.
        if len(upgrade_pkgs) > 0:
            self.logger.info("Attempting to upgrade packages")

            if YAD:
                pkgtool = "/usr/bin/yum -d0 -y update %s"
            else:
                pkgtool = "/usr/bin/yum -d0 update %s"

            upgrade_args = []
            for inst in upgrade_pkgs:
                pkg_arg = self.instance_status[inst].get('pkg').get('name') + '-'
                if inst.haskey('epoch'):
                    pkg_arg = pkg_arg + inst.get('epoch') + ':'
                pkg_arg = pkg_arg + inst.get('version') + '-' + inst.get('release')
                if inst.haskey('arch'):
                    pkg_arg = pkg_arg + '.' + inst.get('arch')
                upgrade_args.append(pkg_arg)
            
            cmdrc, output = self.cmd.run(pkgtool % " ".join(upgrade_args))
            if cmdrc == 0:
                # The yum command succeeded.  All packages installed.
                self.logger.info("Single Pass for Install Succeded")
                self.RefreshPackages()

                # Reverify all the packages that we might have just changed.
                # There may be multiple instances per package, only do the
                # verification once.
                install_pkg_set = set([self.instance_status[inst].get('pkg') \
                                                      for inst in upgrade_pkgs])
                self.logger.info("Reverifying Installed Packages")
                for inst in upgrade_pkgs:
                    pkg_entry = self.instance_status[inst].get('pkg')
                    if pkg_entry in install_pkg_set:
                        self.logger.debug("Reverifying Installed %s" % \
                                                                      (pkg_entry.get('name')))
                        install_pkg_set.remove(pkg_entry)
                        self.states[pkg_entry] = self.VerifyPackage(pkg_entry, \
                                                         self.instance_status[inst].get('modlist'))
                    else:
                        # We already reverified this pacakge.
                        continue
            else:
                # The yum command failed.  No packages installed.
                # Try installing instances individually.
                self.logger.error("Single Pass Install of Packages Failed")
                installed_instances = []
                for inst in upgrade_pkgs:
                    pkg_arg = self.instance_status[inst].get('pkg').get('name') + '-'
                    if inst.haskey('epoch'):
                        pkg_arg = pkg_arg + inst.get('epoch') + ':'
                    pkg_arg = pkg_arg + inst.get('version') + '-' + inst.get('release')
                    if inst.haskey('arch'):
                        pkg_arg = pkg_arg + '.' + inst.get('arch')
                    cmdrc, output = self.cmd.run(pkgtool % pkg_arg)
                    if cmdrc == 0:
                        installed_instances.append(inst)
                    else:
                        self.logger.debug("%s %s would not install." % \
                                              (self.instance_status[inst].get('pkg').get('name'), \
                                               self.str_evra(inst)))

                install_pkg_set = set([self.instance_status[inst].get('pkg') \
                                                      for inst in upgrade_pkgs])
                self.RefreshPackages()
                for inst in installed_instances:
                    pkg = inst.get('pkg')
                    # Reverify all the packages that we might have just changed.
                    # There may be multiple instances per package, only do the
                    # verification once.
                    if pkg in install_pkg_set:
                        self.logger.debug("Reverifying Installed Package %s" % \
                                                                      (pkg_entry.get('name')))
                        install_pkg_set.remove(pkg)
                        self.states[pkg_entry] = self.VerifyPackage(pkg, \
                                                         self.instance_status[inst].get('modlist'))
                    else:
                        # We already reverified this pacakge.
                        continue


        for entry in [ent for ent in packages if self.states[ent]]:
            self.modified.append(entry)

    def RemovePackages(self, packages):
        '''
           Remove specified entries.

           packages is a list of Package Entries with Instances generated
           by FindExtraPackages().
        '''
        self.logger.debug('Running YUMng.RemovePackages()')

        if YAD:
            pkgtool = "/usr/bin/yum -d0 -y erase %s"
        else:
            pkgtool = "/usr/bin/yum -d0 erase %s"

        erase_args = []
        for pkg in packages:
            for inst in pkg:
                if pkg.get('name') != 'gpg-pubkey':
                    pkg_arg = pkg.get('name') + '-'
                    if inst.get('epoch', False):
                        pkg_arg = pkg_arg + inst.get('epoch') + ':'
                    pkg_arg = pkg_arg + inst.get('version') + '-' + inst.get('release')
                    if inst.get('arch', False):
                        pkg_arg = pkg_arg + '.' + inst.get('arch')
                    erase_args.append(pkg_arg)
                else:
                    self.logger.info("WARNING: gpg-pubkey package not in configuration %s %s"\
                                                 % (pkgspec.get('name'), self.str_evra(pkgspec)))
                    self.logger.info("         This package will be deleted in a future version of the RPMng driver.")

        cmdrc, output = self.cmd.run(pkgtool % " ".join(erase_args))
        if cmdrc == 0:
            self.modified += packages
            for pkg in erase_args:
                self.logger.info("Deleted %s" % (pkg))
        else:
            self.logger.info("Bulk erase failed with errors:")
            self.logger.debug("Erase results = %s" % output)
            self.logger.info("Attempting individual erase for each package.")
            for pkg in packages:
                pkg_modified = False
                for inst in pkg:
                    if pkg.get('name') != 'gpg-pubkey':
                        pkg_arg = pkg.get('name') + '-'
                        if inst.haskey('epoch'):
                            pkg_arg = pkg_arg + inst.get('epoch') + ':'
                        pkg_arg = pkg_arg + inst.get('version') + '-' + inst.get('release')
                        if inst.haskey('arch'):
                            pkg_arg = pkg_arg + '.' + inst.get('arch')
                    else:
                        self.logger.info("WARNING: gpg-pubkey package not in configuration %s %s"\
                                                 % (pkgspec.get('name'), self.str_evra(pkgspec)))
                        self.logger.info("         This package will be deleted in a future version of the RPMng driver.")

                    cmdrc, output = self.cmd.run(self.pkgtool % pkg_arg)
                    if cmdrc == 0:
                        pkg_modified = True
                        self.logger.info("Deleted %s" % pkg_arg)
                    else:
                        self.logger.error("unable to delete %s" % pkg_arg)
                        self.logger.debug("Failure = %s" % output)
                if pkg_modified == True:
                    self.modified.append(pkg)


        self.RefreshPackages()
        self.extra = self.FindExtraPackages()