summaryrefslogtreecommitdiffstats
path: root/src/lib/Client/Redhat.py
blob: ad417aa8277a4aff22bd8b4c7cc9d5e1692b12fa (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
# This is the bcfg2 support for redhat
# $Id: $

'''This is redhat client support'''
__revision__ = '$Revision$'

from os import popen, system
from popen2 import Popen4

from Bcfg2.Client.Toolset import Toolset

class Redhat(Toolset):
    '''This class implelements support for rpm packages and standard chkconfig services'''
    pkgtool = ("rpm --oldpackage --replacepkgs --quiet -U %s", ("%s", ["url"]))

    def __init__(self, cfg, setup):
        Toolset.__init__(self, cfg, setup)
        self.pkgwork = {'add':[], 'update':[], 'remove':[]}
        self.Refresh()

    def Refresh(self):
        '''Refresh memory hashes of packages'''
        self.installed = {}

        # Build list of packages
        instp = popen("rpm -qa --qf '%{NAME} %{VERSION}\n'")
        for line in instp:
            [name, version] = line.split(' ')
            self.installed[name] = version

    def VerifyService(self, entry):
        '''Verify Service status for entry'''
        try:
            srvdata = popen("/sbin/chkconfig --list %s"%entry.attrib['name']).readlines()[0].split()
        except IndexError:
            # Ocurrs when no lines are returned (service not installed)
            return False
        if entry.attrib['type'] == 'xinetd':
            return entry.attrib['status'] == srvdata[1]

        onlevels = [level.split(':')[0] for level in srvdata[1:] if level.split(':')[1] == 'on']

        # chkconfig/init.d service
        if entry.get('status') == 'on':
            return len(onlevels) > 0
        else:
            return len(onlevels) == 0
    
    def InstallService(self, entry):
        '''Install Service entry'''
        system("/sbin/chkconfig --add %s"%(entry.attrib['name']))
        self.CondPrint('verbose', "Installing Service %s" % (entry.get('name')))
        if entry.attrib['status'] == 'off':
            if self.setup['dryrun']:
                print "Disabling server %s" % (entry.get('name'))
            else:
                cmdrc = system("/sbin/chkconfig --level 0123456 %s %s" % (entry.attrib['name'], entry.attrib['status']))
        else:
            if self.setup['dryrun']:
                print "Enabling server %s" % (entry.get('name'))
            else:
                cmdrc = system("/sbin/chkconfig %s %s" %
                            (entry.attrib['name'], entry.attrib['status']))
        if cmdrc == 0:
            return True
        else:
            return False

    def VerifyPackage(self, entry, modlist):
        '''Verify Package status for entry'''
        if not entry.get('version'):
            print "Can't install package %s, not enough data." % (entry.get('name'))
            return False
        instp = Popen4("rpm -qi %s-%s" % (entry.get('name'), entry.get('version')))
        istat = instp.poll()
        while istat == -1:
            instp.fromchild.read()
            istat = instp.poll()
        if istat != 0:
            self.CondPrint('debug', "Package %s version incorrect" % entry.get('name'))
        else:
            if entry.attrib.get('verify', 'true') == 'true':
                if self.setup['quick']:
                    return True
                verp = Popen4("rpm --verify -q %s-%s" %
                              (entry.get('name'),entry.get('version')), bufsize=16384)
                odata = verp.fromchild.read()
                vstat = verp.poll()
                while vstat == -1:
                    odata += verp.fromchild.read()
                    vstat = verp.poll()
                output = [line for line in odata.split("\n") if line]
                if vstat == 0:
                    return True
                else:
                    if len([name for name in output if name.split()[-1] not in modlist]):
                        return True
                    else:
                        self.CondPrint('debug',
                                       "Package %s content verification failed" % entry.get('name'))
        return False