| 1 | '''Debian Init Support for Bcfg2''' |
|---|
| 2 | __revision__ = '$Revision$' |
|---|
| 3 | |
|---|
| 4 | import glob, os, re |
|---|
| 5 | import Bcfg2.Client.Tools |
|---|
| 6 | |
|---|
| 7 | class DebInit(Bcfg2.Client.Tools.SvcTool): |
|---|
| 8 | '''Debian Service Support for Bcfg2''' |
|---|
| 9 | name = 'DebInit' |
|---|
| 10 | __execs__ = ['/usr/sbin/update-rc.d', '/usr/sbin/invoke-rc.d'] |
|---|
| 11 | __handles__ = [('Service', 'deb')] |
|---|
| 12 | __req__ = {'Service': ['name', 'status']} |
|---|
| 13 | svcre = re.compile("/etc/.*/(?P<action>[SK])(?P<sequence>\d+)(?P<name>\S+)") |
|---|
| 14 | |
|---|
| 15 | # implement entry (Verify|Install) ops |
|---|
| 16 | def VerifyService(self, entry, _): |
|---|
| 17 | '''Verify Service status for entry''' |
|---|
| 18 | rawfiles = glob.glob("/etc/rc*.d/[SK]*%s" % (entry.get('name'))) |
|---|
| 19 | files = [] |
|---|
| 20 | if entry.get('sequence'): |
|---|
| 21 | start_sequence = int(entry.get('sequence')) |
|---|
| 22 | kill_sequence = 100 - start_sequence |
|---|
| 23 | else: |
|---|
| 24 | start_sequence = None |
|---|
| 25 | |
|---|
| 26 | for filename in rawfiles: |
|---|
| 27 | match = self.svcre.match(filename) |
|---|
| 28 | if not match: |
|---|
| 29 | self.logger.error("Failed to match file: %s" % filename) |
|---|
| 30 | continue |
|---|
| 31 | if match.group('name') == entry.get('name'): |
|---|
| 32 | files.append(filename) |
|---|
| 33 | if entry.get('status') == 'off': |
|---|
| 34 | if files: |
|---|
| 35 | entry.set('current_status', 'on') |
|---|
| 36 | return False |
|---|
| 37 | else: |
|---|
| 38 | return True |
|---|
| 39 | else: |
|---|
| 40 | if files: |
|---|
| 41 | if start_sequence: |
|---|
| 42 | for filename in files: |
|---|
| 43 | match = self.svcre.match(filename) |
|---|
| 44 | file_sequence = int(match.group('sequence')) |
|---|
| 45 | if match.group('action') == 'S' and file_sequence != start_sequence: |
|---|
| 46 | return False |
|---|
| 47 | if match.group('action') == 'K' and file_sequence != kill_sequence: |
|---|
| 48 | return False |
|---|
| 49 | return True |
|---|
| 50 | else: |
|---|
| 51 | entry.set('current_status', 'off') |
|---|
| 52 | return False |
|---|
| 53 | |
|---|
| 54 | def InstallService(self, entry): |
|---|
| 55 | '''Install Service for entry''' |
|---|
| 56 | self.logger.info("Installing Service %s" % (entry.get('name'))) |
|---|
| 57 | try: |
|---|
| 58 | os.stat('/etc/init.d/%s' % entry.get('name')) |
|---|
| 59 | except OSError: |
|---|
| 60 | self.logger.debug("Init script for service %s does not exist" % entry.get('name')) |
|---|
| 61 | return False |
|---|
| 62 | |
|---|
| 63 | if entry.get('status') == 'off': |
|---|
| 64 | self.cmd.run("/usr/sbin/invoke-rc.d %s stop" % (entry.get('name'))) |
|---|
| 65 | cmdrc = self.cmd.run("/usr/sbin/update-rc.d -f %s remove" % entry.get('name'))[0] |
|---|
| 66 | else: |
|---|
| 67 | command = "/usr/sbin/update-rc.d %s defaults" % (entry.get('name')) |
|---|
| 68 | if entry.get('sequence'): |
|---|
| 69 | cmdrc = self.cmd.run("/usr/sbin/update-rc.d -f %s remove" % entry.get('name'))[0] |
|---|
| 70 | if cmdrc != 0: |
|---|
| 71 | return False |
|---|
| 72 | start_sequence = int(entry.get('sequence')) |
|---|
| 73 | kill_sequence = 100 - start_sequence |
|---|
| 74 | command = "%s %d %d" % (command, start_sequence, kill_sequence) |
|---|
| 75 | cmdrc = self.cmd.run(command)[0] |
|---|
| 76 | return cmdrc == 0 |
|---|
| 77 | |
|---|
| 78 | def FindExtra(self): |
|---|
| 79 | '''Find Extra Debian Service Entries''' |
|---|
| 80 | specified = [entry.get('name') for entry in self.getSupportedEntries()] |
|---|
| 81 | extra = [] |
|---|
| 82 | for name in [self.svcre.match(fname).group('name') for fname in |
|---|
| 83 | glob.glob("/etc/rc[12345].d/S*") \ |
|---|
| 84 | if self.svcre.match(fname).group('name') not in specified]: |
|---|
| 85 | if name not in extra: |
|---|
| 86 | extra.append(name) |
|---|
| 87 | return [Bcfg2.Client.XML.Element('Service', name=name, type='deb') for name \ |
|---|
| 88 | in extra] |
|---|
| 89 | |
|---|
| 90 | def Remove(self, _): |
|---|
| 91 | '''Remove extra service entries''' |
|---|
| 92 | # Extra service removal is nonsensical |
|---|
| 93 | # Extra services need to be reflected in the config |
|---|
| 94 | return |
|---|
| 95 | |
|---|
| 96 | def get_svc_command(self, service, action): |
|---|
| 97 | return '/usr/sbin/invoke-rc.d %s %s' % (service.get('name'), action) |
|---|