Skip to content

Commit

Permalink
Added create event cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Digant C Kasundra committed May 15, 2015
1 parent e7abeb8 commit 1dd5078
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
69 changes: 63 additions & 6 deletions bin/hermes
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,55 @@ def list_hosts(args):
def add_host(args):
logging.debug("add_host(%s)", args.hostname)
session = make_session()
Host.create(session, args.hostname)
print "Created host {}".format(args.hostname)
session.commit()
host = Host.get_host(session, args.hostname)
if host is None:
Host.create(session, args.hostname)
print "Created host {}".format(args.hostname)
session.commit()
else:
print "Host {} already exists".format(args.hostname)
session.close()


def main():
def create_event(args):
logging.debug("create_event()")
logging.debug(
"host: %s category: %s state: %s note: %s",
args.hostname, args.category, args.state, args.note
)

session = make_session()

event_type = EventType.get_event_type(session, args.category, args.state)

if event_type is None:
logging.error(
"No matching EventState found for %s %s", args.category, args.state
)
session.close()
return

host = Host.get_host(session, args.hostname)

if host is None:
host = Host.create(session, args.hostname)

user = getpass.getuser()

description_msg = "EMS CLI"
event = Event.create(session, host, user, event_type, args.note)

if event:
print "Event created."
session.commit()
else:
print "Event creation failed."
session.rollback()

session.close()


def parse_cli_args():
description_msg = "Hermes CLI"
parser = argparse.ArgumentParser(description=description_msg)

parser.add_argument("-c", "--config", default="/etc/hermes.yaml",
Expand Down Expand Up @@ -103,8 +143,25 @@ def main():
host_add_parser.add_argument("hostname")
host_add_parser.set_defaults(func=add_host)

# events command line parser
event_parser = subparsers.add_parser(
"event", help="Create and list events"
)
event_subparser = event_parser.add_subparsers()
event_create_parser = event_subparser.add_parser("create")
event_create_parser.add_argument("hostname")
event_create_parser.add_argument("category")
event_create_parser.add_argument("state")
event_create_parser.add_argument(
"-n", "--note", type=str, help="Note to attach to the event"
)
event_create_parser.set_defaults(func=create_event)

return parser.parse_args()

args = parser.parse_args()

def main():
args = parse_cli_args()
settings.update_from_config(args.config)

if args.verbose:
Expand Down
32 changes: 29 additions & 3 deletions hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,25 @@ def create(cls, session, category, state, description=None):

return obj

@classmethod
def get_event_type(cls, session, category, state):
"""Look up an EventType with the given category and state
Args:
session: a live database session
category: the category to match
state: the state to match
Returns:
the matching EventType, or None
"""
return session.query(EventType).filter(
and_(
EventType.category == category,
EventType.state == state
)
).first()


class Host(Model):
"""A basic declaration of a host, which should map to unique server (real or virtual)
Expand Down Expand Up @@ -279,9 +298,6 @@ def create(cls, session, hostname):
if hostname is None:
raise exc.ValidationError("Hostname is required")

if session.query(Host).filter(Host.hostname == hostname).all():
raise exc.Conflict("Hostname already exists")

try:
obj = cls(hostname=hostname)
obj.add(session)
Expand All @@ -293,6 +309,16 @@ def create(cls, session, hostname):

return obj

@classmethod
def get_host(cls, session, hostname):
"""Find a host with a given hostname
Args:
session: a database session
hostname: the name to look for
"""
return session.query(Host).filter(Host.hostname == hostname).first()


class Fate(Model):
"""A Fate is a mapping of EventTypes to inform the system what kind of Events
Expand Down

0 comments on commit 1dd5078

Please sign in to comment.