| 1 | '''This module manages the crontab file for bcfg2''' |
|---|
| 2 | __revision__ = '$Revision: 1887 $' |
|---|
| 3 | |
|---|
| 4 | import binascii, os, socket, Bcfg2.Server.Plugin, random |
|---|
| 5 | |
|---|
| 6 | class Crontab(Bcfg2.Server.Plugin.Plugin): |
|---|
| 7 | '''This Generates a random set of times for the cron.daily entries to run. |
|---|
| 8 | The goal is to ensure that our Configuration Server/network does get crushed |
|---|
| 9 | all in a 5-10 minute period. |
|---|
| 10 | ''' |
|---|
| 11 | __name__ = 'Crontab' |
|---|
| 12 | __version__ = '$Id: Crontab 1887 2006-06-18 02:35:54Z desai $' |
|---|
| 13 | __author__ = 'bcfg-dev@mcs.anl.gov' |
|---|
| 14 | |
|---|
| 15 | def __init__(self, core, datastore): |
|---|
| 16 | Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore) |
|---|
| 17 | try: |
|---|
| 18 | self.repository = Bcfg2.Server.Plugin.DirectoryBacked(self.data, self.core.fam) |
|---|
| 19 | except OSError, ioerr: |
|---|
| 20 | self.logger.error("Failed to load Crontab repository from %s" % (self.data)) |
|---|
| 21 | self.logger.error(ioerr) |
|---|
| 22 | raise Bcfg2.Server.Plugin.PluginInitError |
|---|
| 23 | try: |
|---|
| 24 | prefix = open("%s/prefix" % (self.data)).read().strip() |
|---|
| 25 | except IOError: |
|---|
| 26 | prefix = '' |
|---|
| 27 | self.Entries = {'ConfigFile': |
|---|
| 28 | {prefix + '/etc/crontab':self.build_crontab}} |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | def build_crontab(self, entry, metadata): |
|---|
| 32 | '''This function builds builds a crontab file with a random time for cron.daily''' |
|---|
| 33 | random.seed(metadata.hostname) |
|---|
| 34 | hour = random.randrange(0,6) |
|---|
| 35 | minute = random.randrange(0,59) |
|---|
| 36 | entry.text = self.repository.entries['crontab.template'].data% (minute, hour) |
|---|
| 37 | permdata = {'owner':'root', 'group':'root', 'perms':'0644'} |
|---|
| 38 | [entry.attrib.__setitem__(key, permdata[key]) for key in permdata] |
|---|