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

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

from os import popen, system
from popen2 import Popen4

from Bcfg2.Client.Toolset import Toolset

def Detect():
    # until the code works
    return False

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

    def __init__(self, cfg, setup):
        Toolset.__init__(self, cfg, setup)
        self.pkgtodo = []

    def VerifyService(self, entry):
        srvdata = popen("/sbin/chkconfig --list %s"%entry.attrib['name']).readlines()[0].split()
        if entry.attrib['type'] == 'xinetd':
            if entry.attrib['status'] == srvdata[1]:
                return True
            else:
                return False
        else:
            # chkconfig/init.d service
            if entry.attrib['status'] == 'off':
                for level in srvdata[1:]:
                    if level.split(':')[1] != 'off':
                        return False
                return True
            else:
                # services should be on for 2345
                for level in srvdata[1:]:
                    [num, status] = level.split(':')
                    if num in '2345':
                        if status == 'off':
                            return False
                    else:
                        if status == 'on':
                            return False
                return True
    
    def InstallService(self, entry):
        system("/sbin/chkconfig --add %s"%(entry.attrib['name']))
        if entry.attrib['status'] == 'off':
            rc = system("/sbin/chkconfig --level 0123456 %s %s" % (entry.attrib['name'], entry.attrib['status']))
        else:
            rc = system("/sbin/chkconfig %s %s" %
                        (entry.attrib['name'], entry.attrib['status']))
        if rc == 0:
            return True
        else:
            return False

    def VerifyPackage(self, entry, modlist = []):
        instp = Popen4("rpm -qi %s-%s" % (entry.attrib['name'], entry.attrib['version']))
        istat = instp.wait()/256
        if istat == 0:
            if entry.attrib.get('verify', 'true') == 'true':
                if self.setup['quick']:
                    return True
                verp = Popen4("rpm --verify --nomd5 -q %s-%s" %
                              (entry.attrib['name'],entry.attrib['version']), bufsize=16384)
                odata = ''
                vstat = verp.poll()
                while vstat == -1:
                    odata += verp.fromchild.read()
                    vstat = verp.poll() >> 8
                output = [x for x in odata.split("\n") if x]
                if vstat == 0:
                    return True
                else:
                    if len([x for x in output if x.split()[-1] not in modlist]) == 0:
                        return True
        return False

    def InstallPackage(self, entry):
        self.pkgtodo.append(entry)
        return False

    def Install(self):
        # try single install
        rc = system(self.rpmcmd % (" ".join([x.attrib['url'] for x in self.pkgtodo])))
        if rc == 0:
            # set state == True for all just-installed packages
            for pkg in self.pkgtodo:
                self.states[pkg] = True
            self.pkgtodo = []
        else:
            # fall back to single package installs
            oldlen = len(self.pkgtodo) + 1
            while oldlen > len(self.pkgtodo):
                oldlen = len(self.pkgtodo)
                for entry in self.pkgtodo:
                    rc = system(self.rpmcmd%(entry.attrib['url']))
                    if rc == 0:
                        self.states[entry] = True
                        self.pkgtodo.remove(entry)
                    else:
                        if self.setup['verbose']:
                            print "package %s-%s failed to install" % (entry.attrib['name'], entry.attrib['version'])