| 1 | # This is the bcfg2 support for chkconfig |
|---|
| 2 | # $Id$ |
|---|
| 3 | |
|---|
| 4 | '''This is chkconfig support''' |
|---|
| 5 | __revision__ = '$Revision$' |
|---|
| 6 | |
|---|
| 7 | import Bcfg2.Client.Tools, Bcfg2.Client.XML |
|---|
| 8 | |
|---|
| 9 | class Chkconfig(Bcfg2.Client.Tools.SvcTool): |
|---|
| 10 | '''Chkconfig support for Bcfg2''' |
|---|
| 11 | name = 'Chkconfig' |
|---|
| 12 | __execs__ = ['/sbin/chkconfig'] |
|---|
| 13 | __handles__ = [('Service', 'chkconfig')] |
|---|
| 14 | __req__ = {'Service': ['name', 'status']} |
|---|
| 15 | |
|---|
| 16 | def get_svc_command(self, service, action): |
|---|
| 17 | return "/sbin/service %s %s" % (service.get('name'), action) |
|---|
| 18 | |
|---|
| 19 | def VerifyService(self, entry, _): |
|---|
| 20 | '''Verify Service status for entry''' |
|---|
| 21 | try: |
|---|
| 22 | cmd = "/sbin/chkconfig --list %s " % (entry.get('name')) |
|---|
| 23 | raw = self.cmd.run(cmd)[1] |
|---|
| 24 | patterns = ["error reading information", "unknown service"] |
|---|
| 25 | srvdata = [line.split() for line in raw for pattern in patterns \ |
|---|
| 26 | if pattern not in line][0] |
|---|
| 27 | except IndexError: |
|---|
| 28 | # Ocurrs when no lines are returned (service not installed) |
|---|
| 29 | entry.set('current_status', 'off') |
|---|
| 30 | return False |
|---|
| 31 | if len(srvdata) == 2: |
|---|
| 32 | # This is an xinetd service |
|---|
| 33 | if entry.get('status') == srvdata[1]: |
|---|
| 34 | return True |
|---|
| 35 | else: |
|---|
| 36 | entry.set('current_status', srvdata[1]) |
|---|
| 37 | return False |
|---|
| 38 | |
|---|
| 39 | try: |
|---|
| 40 | onlevels = [level.split(':')[0] for level in srvdata[1:] \ |
|---|
| 41 | if level.split(':')[1] == 'on'] |
|---|
| 42 | except IndexError: |
|---|
| 43 | onlevels = [] |
|---|
| 44 | |
|---|
| 45 | if entry.get('status') == 'on': |
|---|
| 46 | status = (len(onlevels) > 0 ) |
|---|
| 47 | else: |
|---|
| 48 | status = (len(onlevels) == 0) |
|---|
| 49 | |
|---|
| 50 | if entry.get('mode', 'default') == 'supervised': |
|---|
| 51 | pstatus, pout = self.cmd.run('/sbin/service %s status' % \ |
|---|
| 52 | entry.get('name')) |
|---|
| 53 | if pstatus: |
|---|
| 54 | self.cmd.run('/sbin/service %s start' % (entry.get('name'))) |
|---|
| 55 | pstatus, pout = self.cmd.run('/sbin/service %s status' % \ |
|---|
| 56 | entry.get('name')) |
|---|
| 57 | # chkconfig/init.d service |
|---|
| 58 | if entry.get('status') == 'on': |
|---|
| 59 | status = status and not pstatus |
|---|
| 60 | |
|---|
| 61 | if not status: |
|---|
| 62 | if entry.get('status') == 'on': |
|---|
| 63 | entry.set('current_status', 'off') |
|---|
| 64 | else: |
|---|
| 65 | entry.set('current_status', 'on') |
|---|
| 66 | return status |
|---|
| 67 | |
|---|
| 68 | def InstallService(self, entry): |
|---|
| 69 | '''Install Service entry''' |
|---|
| 70 | rcmd = "/sbin/chkconfig %s %s" |
|---|
| 71 | self.cmd.run("/sbin/chkconfig --add %s"%(entry.attrib['name'])) |
|---|
| 72 | self.logger.info("Installing Service %s" % (entry.get('name'))) |
|---|
| 73 | pass1 = True |
|---|
| 74 | if entry.get('status') == 'off': |
|---|
| 75 | rc = self.cmd.run(rcmd % (entry.get('name'), entry.get('status')) + " --level 0123456")[0] |
|---|
| 76 | pass1 = rc == 0 |
|---|
| 77 | rc = self.cmd.run(rcmd % (entry.get('name'), entry.get('status')))[0] |
|---|
| 78 | return pass1 and rc == 0 |
|---|
| 79 | |
|---|
| 80 | def FindExtra(self): |
|---|
| 81 | '''Locate extra chkconfig Services''' |
|---|
| 82 | allsrv = [line.split()[0] for line in \ |
|---|
| 83 | self.cmd.run("/sbin/chkconfig --list|grep :on")[1]] |
|---|
| 84 | self.logger.debug('Found active services:') |
|---|
| 85 | self.logger.debug(allsrv) |
|---|
| 86 | specified = [srv.get('name') for srv in self.getSupportedEntries()] |
|---|
| 87 | return [Bcfg2.Client.XML.Element('Service', type='chkconfig', name=name) \ |
|---|
| 88 | for name in allsrv if name not in specified] |
|---|