summaryrefslogtreecommitdiffstats
path: root/src/lib/Client/Tools/RcUpdate.py
blob: 58beb56a432d46c3138943f9631eb6d10bc2444a (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
'''This is rc-update support'''
__revision__ = '$Revision$'

import os
import Bcfg2.Client.Tools
import Bcfg2.Client.XML

class RcUpdate(Bcfg2.Client.Tools.SvcTool):
    '''RcUpdate support for Bcfg2'''
    name = 'RcUpdate'
    __execs__ = ['/sbin/rc-update', '/bin/rc-status']
    __handles__ = [('Service', 'rc-update')]
    __req__ = {'Service': ['name', 'status']}

    def VerifyService(self, entry, _):
        '''
        Verify Service status for entry.
        Assumes we run in the "default" runlevel.
        '''
        rc, output = self.cmd.run('/bin/rc-status -s | grep %s | grep started' % \
                                  entry.attrib['name'])
        status = (rc == 0)

        if not status:
            # service is off
            if entry.get('status') == 'on':
                # we want it on, it's not
                entry.set('current_status', 'off')
            else:
                # we want it off, check if it is
                rc, output = self.cmd.run('/bin/rc-status -u | grep %s | grep stopped' % \
                                        entry.get('name'))
                status = (rc == 0)
                if not status:
                    # it's not off and should be
                    entry.set('current_status', 'on')
        else:
            if entry.get('status') == 'off':
                # we want it off, it's not
                entry.set('current_status', 'on')
                return False;
        return status

    def InstallService(self, entry):
        '''Install Service entry'''
        self.logger.info("Installing Service %s" % (entry.get('name')))
        try:
            os.stat('/etc/init.d/%s' % entry.get('name'))
        except OSError:
            self.logger.debug("Init script for service %s does not exist" %
                              entry.get('name'))
            return False

        if entry.get('status') == 'off':
            self.cmd.run("/etc/init.d/%s stop" % (entry.get('name')))
            cmdrc = self.cmd.run("/sbin/rc-update del %s default" %
                                (entry.get('name')))
        else:
            cmdrc = self.cmd.run("/sbin/rc-update add %s default" %
                                 entry.get('name'))[0]
        return cmdrc == 0

    def FindExtra(self):
        '''Locate extra rc-update Services'''
        allsrv = [line.split()[0] for line in \
                  self.cmd.run("/bin/rc-status -s | grep started")[1]]
        self.logger.debug('Found active services:')
        self.logger.debug(allsrv)
        specified = [srv.get('name') for srv in self.getSupportedEntries()]
        return [Bcfg2.Client.XML.Element('Service', type='rc-update', name=name) \
                for name in allsrv if name not in specified]