Add an alert file publisher

Write the current alert to a file on the file system.  Later this
can be served by apache and included on other pages.

Change-Id: I71a88df993b16cecad7287c54361f8cd5ff78a05
This commit is contained in:
James E. Blair 2013-12-06 09:31:40 -08:00
parent d1de6ddc91
commit 42a2678379
1 changed files with 30 additions and 5 deletions

View File

@ -56,16 +56,16 @@ except:
class UpdateInterface(object):
def alert(self, msg=None):
raise NotImplementedError()
pass
def notice(self, msg=None):
raise NotImplementedError()
pass
def log(self, msg=None):
raise NotImplementedError()
pass
def ok(self, msg=None):
raise NotImplementedError()
pass
class StatusPage(UpdateInterface):
@ -149,6 +149,30 @@ class StatusPage(UpdateInterface):
self.current_alert = current_alert
class AlertFile(UpdateInterface):
def __init__(self, config):
if config.has_section('alertfile'):
self.dir = config.get('alertfile', 'dir')
self.path = os.path.join(self.dir, 'alert.json')
else:
self.path = None
self.ok()
def write(self, msg):
if not self.path:
return
f, path = tempfile.mkstemp(dir=self.dir)
os.write(f, json.dumps(dict(alert=msg)))
os.close(f)
os.rename(path, self.path)
def alert(self, msg=None):
self.write(msg)
def ok(self, msg=None):
self.write(None)
class StatusBot(irc.bot.SingleServerIRCBot):
log = logging.getLogger("statusbot.bot")
@ -319,7 +343,8 @@ def _main(configpath):
config.get('ircbot', 'channels').split(',')]
nicks = [name.strip() for name in
config.get('ircbot', 'nicks').split(',')]
publishers = [StatusPage(config)]
publishers = [StatusPage(config),
AlertFile(config)]
bot = StatusBot(channels, nicks, publishers,
config.get('ircbot', 'nick'),