summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Client/Tools/RcUpdate.py
blob: 2e58f2564f6d712e7f5e07f5c65b5af902875d6b (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
"""This is rc-update support."""

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.

        """
        if entry.get('status') == 'ignore':
            return True

        # check if service is enabled
        cmd = '/sbin/rc-update show default | grep %s'
        is_enabled = self.cmd.run(cmd % entry.get('name')).success

        # check if init script exists
        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

        # check if service is enabled
        cmd = '/etc/init.d/%s status | grep started'
        is_running = self.cmd.run(cmd % entry.attrib['name']).success

        if entry.get('status') == 'on' and not (is_enabled and is_running):
            entry.set('current_status', 'off')
            return False

        elif entry.get('status') == 'off' and (is_enabled or is_running):
            entry.set('current_status', 'on')
            return False

        return True

    def InstallService(self, entry):
        """
        Install Service entry

        """
        self.logger.info('Installing Service %s' % entry.get('name'))
        if entry.get('status') == 'on':
            if entry.get('current_status') == 'off':
                self.start_service(entry)
            # make sure it's enabled
            cmd = '/sbin/rc-update add %s default'
            return self.cmd.run(cmd % entry.get('name')).success
        elif entry.get('status') == 'off':
            if entry.get('current_status') == 'on':
                self.stop_service(entry)
            # make sure it's disabled
            cmd = '/sbin/rc-update del %s default'
            return self.cmd.run(cmd % entry.get('name')).success

        return False

    def FindExtra(self):
        """Locate extra rc-update services."""
        cmd = '/bin/rc-status -s'
        allsrv = [line.split()[0]
                  for line in self.cmd.run(cmd).stdout.splitlines()
                  if 'started' in line]
        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]