Skip to content

Commit

Permalink
Merge pull request #30 from diggyk/master
Browse files Browse the repository at this point in the history
Added support for event hooks
  • Loading branch information
gmjosack committed Aug 5, 2015
2 parents c3e891a + 19e34bc commit 1b4cbeb
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 10 deletions.
8 changes: 8 additions & 0 deletions bin/hermes-server
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import hermes
from hermes import version
from hermes.app import Application
from hermes.settings import settings
from hermes.plugin import get_hooks
from hermes import models


from sqlalchemy.exc import OperationalError
Expand Down Expand Up @@ -69,6 +71,12 @@ def main():
"cookie_secret": settings.secret_key,
}

# load and register any hooks we have
hooks = get_hooks([settings.plugin_dir])
for hook in hooks:
logging.debug("registering hook {}".format(hook))
models.register_hook(hook)

my_settings = {
"db_uri": settings.database,
"db_engine": None,
Expand Down
5 changes: 4 additions & 1 deletion config/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ email_notifications: false
auth_token_expiry: 600

# Sentry DSN if using Sentry to log exceptions.
# sentry_dsn:
# sentry_dsn:

# Additional plugin directory (full path)
# plugin_dir:
26 changes: 18 additions & 8 deletions hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

log = logging.getLogger(__name__)

_HOOKS = []

def register_hook(hook):
_HOOKS.append(hook)


class Session(_Session):
""" Custom session meant to utilize add on the model.
Expand Down Expand Up @@ -827,6 +832,8 @@ def create(
Returns:
a newly created Event
"""
log.debug("Event.create()")

if host is None:
raise exc.ValidationError(
"Host cannot be null for an event"
Expand All @@ -851,12 +858,11 @@ def create(
session.rollback()
raise

# slack_message("*Event:* {} = {} {}".format(
# event.host.hostname,
# event.event_type.category,
# event.event_type.state
# ))
# if we have any hooks defined, call the on_event method on each
for hook in _HOOKS:
hook.on_event(event)

# refer to fates to see if this event should close or open any labors
Fate.question_the_fates(session, [event], quest=quest)

return event
Expand All @@ -872,19 +878,23 @@ def create_many(cls, session, events, tx, quest=None):
quest: optional if events tied to quests
flush: indicate if we should flush after we are done
"""
log.debug("Event.create_many()")

session.execute(
Event.__table__.insert(), events
)

events = session.query(Event).filter(Event.tx == tx).all()
log.info("Created {} events".format(len(events)))

for event in events:
# if we have any hooks defined, call the on_event method on each
for hook in _HOOKS:
hook.on_event(event)

# slack_message("*Events:* created {} events".format(len(events)))

# refer to fates to see if these events should close or open any labors
Fate.question_the_fates(session, events, quest=quest)


def href(self, base_uri):
"""Create an HREF value for this object
Expand Down
40 changes: 40 additions & 0 deletions hermes/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Module for declaring plugin base classes and helpers.
"""

import annex
import os
import logging

BUILTIN_PLUGIN_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "plugins")

log = logging.getLogger(__name__)

class BaseHermesHook(object):
""" Base class for adding hooks into Hermes.
This class must be overridden to add actions to perform before and
after particular state transitions in Hermes.
"""

def on_event(self, event):
"""Called when an event is created.
Args:
event: the event that was created
"""


def get_hooks(additional_dirs=None):
""" Helper function to find and load all hooks. """
log.debug("get_hooks()")
if additional_dirs is None:
additional_dirs = []
hooks = annex.Annex(BaseHermesHook, [
os.path.join(BUILTIN_PLUGIN_DIR, "hooks"),
"/etc/hermes/plugins/hooks",
[os.path.expanduser(os.path.join(plugin_dir, "hooks"))
for plugin_dir in additional_dirs]
], instantiate=True)

return hooks
1 change: 1 addition & 0 deletions hermes/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'digant'
1 change: 1 addition & 0 deletions hermes/plugins/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'digant'
1 change: 1 addition & 0 deletions hermes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ def __getattr__(self, name):
"secret_key": "SECRET_KEY",
"auth_token_expiry": 600, # 10 minutes
"sentry_dsn": None,
"plugin_dir": "plugins ",
})
2 changes: 1 addition & 1 deletion hermes/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.4"
__version__ = "0.2.6"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tornado==4.0.2
MySQL-python==1.2.5
requests[security]==2.7.0
python-dateutil==2.4.2
annex==0.3.1

0 comments on commit 1b4cbeb

Please sign in to comment.