Skip to content

Commit

Permalink
Minor fixes (#10)
Browse files Browse the repository at this point in the history
* error on invalid subcommand

* catch timeout exception on service test failure

* fix missing config.json error

* spelling errors

* removed service logs testing for now
  • Loading branch information
omercnet authored May 14, 2018
1 parent af9e6d7 commit 02b266e
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 105 deletions.
2 changes: 1 addition & 1 deletion honeycomb/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Honeycomb main entry point.
This file allows runnign honeycomb as a module directly without calling a method
This file allows running honeycomb as a module directly without calling a method
.. code-block:: bash
$ python -m honeycomb --help
"""
Expand Down
5 changes: 4 additions & 1 deletion honeycomb/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def list_commands(self, ctx):
def get_command(self, ctx, name):
"""Fetch command from folder."""
plugin = os.path.basename(self.folder)
command = importlib.import_module("honeycomb.commands.{}.{}".format(plugin, name))
try:
command = importlib.import_module("honeycomb.commands.{}.{}".format(plugin, name))
except ImportError:
raise click.UsageError("No such command {} {}\n\n{}".format(plugin, name, self.get_help(ctx)))
return getattr(command, name)


Expand Down
2 changes: 1 addition & 1 deletion honeycomb/commands/integration/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@click.argument("integration")
@click.argument("args", nargs=-1)
@click.option("-e", "--editable", is_flag=True, default=False,
help="Load integration directly from spefified path without installing (mainly for dev)")
help="Load integration directly from unspecified path without installing (mainly for dev)")
@click.option("-a", "--show_args", is_flag=True, default=False, help="Show available integration arguments")
def configure(ctx, integration, args, show_args, editable):
"""Configure an integration with default parameters.
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/commands/integration/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@click.pass_context
@click.argument("integrations", nargs=-1)
@click.option("-e", "--editable", is_flag=True, default=False,
help="Run integration directly from spefified path (main for dev)")
help="Run integration directly from specified path (main for dev)")
def test(ctx, integrations, editable):
"""Execute the integration's internal test method to verify it's working as intended."""
logger.debug("running command %s (%s)", ctx.command.name, ctx.params,
Expand Down
4 changes: 2 additions & 2 deletions honeycomb/commands/service/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from honeycomb.defs import SERVICES
from honeycomb.utils.tailer import Tailer
from honeycomb.servicemanager.defs import STDERRLOG, LOGS_DIR
from honeycomb.servicemanager.defs import STDOUTLOG, LOGS_DIR

logger = logging.getLogger(__name__)

Expand All @@ -29,7 +29,7 @@ def logs(ctx, services, num, follow):

tail_threads = []
for service in services:
logpath = os.path.join(services_path, service, LOGS_DIR, STDERRLOG)
logpath = os.path.join(services_path, service, LOGS_DIR, STDOUTLOG)
if os.path.exists(logpath):
logger.debug("tailing %s", logpath)
# TODO: Print log lines from multiple services sorted by timestamp
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/commands/service/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@click.argument("args", nargs=-1)
@click.option("-d", "--daemon", is_flag=True, default=False, help="Run service in daemon mode")
@click.option("-e", "--editable", is_flag=True, default=False,
help="Load service directly from spefified path without installing (mainly for dev)")
help="Load service directly from specified path without installing (mainly for dev)")
@click.option("-a", "--show-args", is_flag=True, default=False, help="Show available service arguments")
@click.option("-i", "--integration", multiple=True, help="Enable an integration")
def run(ctx, service, args, show_args, daemon, editable, integration):
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/commands/service/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@click.command(short_help="Stop a running service daemon")
@click.argument("service")
@click.option("-e", "--editable", is_flag=True, default=False,
help="Load service directly from spefified path without installing (mainly for dev)")
help="Load service directly from specified path without installing (mainly for dev)")
@click.pass_context
def stop(ctx, service, editable):
"""Stop a running service daemon."""
Expand Down
16 changes: 9 additions & 7 deletions honeycomb/commands/service/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from honeycomb.defs import DEBUG_LOG_FILE, SERVICES, ARGS_JSON
from honeycomb.utils import plugin_utils
from honeycomb.utils.wait import wait_until, search_json_log
from honeycomb.utils.wait import wait_until, search_json_log, TimeoutException
from honeycomb.servicemanager.defs import EVENT_TYPE
from honeycomb.servicemanager.registration import register_service, get_service_module

Expand All @@ -22,7 +22,7 @@
@click.argument("services", nargs=-1)
@click.option("-f", "--force", is_flag=True, default=False, help="Do not check if service is running before testing")
@click.option("-e", "--editable", is_flag=True, default=False,
help="Run service directly from spefified path (main for dev)")
help="Run service directly from specified path (main for dev)")
def test(ctx, services, force, editable):
"""Execute the service's internal test method to verify it's working as intended.
Expand Down Expand Up @@ -67,15 +67,17 @@ def test(ctx, services, force, editable):
logger.debug("loaded service {}".format(service_obj))

if hasattr(service_obj, "test"):
click.secho("[+] Executing internal test method for service")
click.secho("[+] Executing internal test method for service..")
logger.debug("executing internal test method for service")
event_types = service_obj.test()
for event_type in event_types:
assert wait_until(search_json_log, filepath=os.path.join(home, DEBUG_LOG_FILE),
total_timeout=10, key=EVENT_TYPE, value=event_type), ""
"failed to test alert: {}".format(event_type)
try:
wait_until(search_json_log, filepath=os.path.join(home, DEBUG_LOG_FILE),
total_timeout=10, key=EVENT_TYPE, value=event_type)
except TimeoutException:
raise click.ClickException("failed to test alert: {}".format(event_type))

click.secho("{} alert tested succesfully".format(event_type))
click.secho("{} alert tested successfully".format(event_type))

elif hasattr(service, "ports") and len(service.ports) > 0:
click.secho("[+] No internal test method found, only testing ports are open")
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/decoymanager/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Hooneycomb defs and constants."""
"""Honeycomb defs and constants."""

from __future__ import unicode_literals, absolute_import

Expand Down
2 changes: 1 addition & 1 deletion honeycomb/integrationmanager/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Honetcomb integration models."""
"""Honeycomb integration models."""
from __future__ import unicode_literals, absolute_import


Expand Down
4 changes: 2 additions & 2 deletions honeycomb/integrationmanager/registration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Honeycomb serivce manager."""
"""Honeycomb service manager."""

from __future__ import unicode_literals, absolute_import

Expand Down Expand Up @@ -59,7 +59,7 @@ def register_integration(package_folder):

json_config_path = os.path.join(package_folder, CONFIG_FILE_NAME)
if not os.path.exists(json_config_path):
raise ConfigFileNotFound()
raise ConfigFileNotFound(json_config_path)

with open(json_config_path, "r") as f:
config_json = json.load(f)
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/integrationmanager/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def send_alert_to_configured_integration(integration_alert):

integration_alert.send_time = get_current_datetime_utc()
integration_alert.output_data = json.dumps(output_data)
# TODO: do something with succesfully handled alerts? They are all writted to debug log file
# TODO: do something with successfully handled alerts? They are all written to debug log file

except exceptions.IntegrationMissingRequiredFieldError as exc:
logger.exception("Send response formatting for integration alert %s failed. Missing required fields",
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/servicemanager/defs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Hooneycomb services definitions and constants."""
"""Honeycomb services definitions and constants."""

from __future__ import unicode_literals, absolute_import

Expand Down
2 changes: 1 addition & 1 deletion honeycomb/servicemanager/error_messages.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Hooneycomb services error messages."""
"""Honeycomb services error messages."""

from __future__ import unicode_literals, absolute_import

Expand Down
2 changes: 1 addition & 1 deletion honeycomb/servicemanager/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class ServiceManagerException(honeycomb.exceptions.PluginError):
"""Generic Seriver Manager Exception."""
"""Generic Service Manager Exception."""


class ServiceNotFound(ServiceManagerException):
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/servicemanager/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Hooneycomb service models."""
"""Honeycomb service models."""

from __future__ import unicode_literals, absolute_import

Expand Down
4 changes: 2 additions & 2 deletions honeycomb/servicemanager/registration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Honeycomb serivce manager."""
"""Honeycomb service manager."""

from __future__ import unicode_literals, absolute_import

Expand Down Expand Up @@ -62,7 +62,7 @@ def register_service(package_folder):

json_config_path = os.path.join(package_folder, CONFIG_FILE_NAME)
if not os.path.exists(json_config_path):
raise ConfigFileNotFound()
raise ConfigFileNotFound(json_config_path)

with open(json_config_path, "r") as f:
config_json = json.load(f)
Expand Down
10 changes: 5 additions & 5 deletions honeycomb/utils/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def is_valid_field_name(value):


def process_config(ctx, configfile):
"""Proccess a yaml config with instructions.
"""Process a yaml config with instructions.
This is a heavy method that loads lots of content, so we only run the imports if its called.
"""
Expand Down Expand Up @@ -145,23 +145,23 @@ def install_plugins(services, integrations):
try:
ctx.invoke(cmd, **kwargs)
except SystemExit:
# If a plugin is already installed honeycomb will exit abnormaly
# If a plugin is already installed honeycomb will exit abnormally
pass

def parametets_to_string(parameters_dict):
def parameters_to_string(parameters_dict):
return ["{}={}".format(k, v) for k, v in parameters_dict.items()]

def configure_integrations(integrations):
for integration in integrations:
args_list = parametets_to_string(config[INTEGRATIONS][integration].get(defs.PARAMETERS, dict()))
args_list = parameters_to_string(config[INTEGRATIONS][integration].get(defs.PARAMETERS, dict()))
ctx.invoke(integration_configure, integration=integration, args=args_list)

def run_services(services, integrations):
# TODO: Enable support with multiple services as daemon, and run service.logs afterwards
# tricky part is that services launched as daemon are exited with os._exit(0) so you
# can't catch it.
for service in services:
args_list = parametets_to_string(config[SERVICES][service].get(defs.PARAMETERS, dict()))
args_list = parameters_to_string(config[SERVICES][service].get(defs.PARAMETERS, dict()))
ctx.invoke(service_run, service=service, integration=integrations, args=args_list)

# TODO: Silence normal stdout and follow honeycomb.debug.json instead
Expand Down
8 changes: 4 additions & 4 deletions honeycomb/utils/plugin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def install_dir(pkgpath, install_path, register_func, delete_after_install=False

def install_from_zip(pkgpath, install_path, register_func, delete_after_install=False):
"""Install plugin from zipfile."""
logger.debug("%s is a file, atttempting to load zip", pkgpath)
logger.debug("%s is a file, attempting to load zip", pkgpath)
pkgtempdir = tempfile.mkdtemp(prefix="honeycomb_")
try:
with zipfile.ZipFile(pkgpath) as pkgzip:
Expand Down Expand Up @@ -254,7 +254,7 @@ def uninstall_plugin(pkgpath, force):
abort=True)
try:
shutil.rmtree(pkgpath)
logger.debug("succesfully uninstalled {}".format(pkgname))
logger.debug("successfully uninstalled {}".format(pkgname))
click.secho("[*] Uninstalled {}".format(pkgname))
except OSError as exc:
logger.exception(str(exc))
Expand Down Expand Up @@ -316,7 +316,7 @@ def parse_plugin_args(command_args, config_args):
# will raise if invalid
config_utils.validate_field_matches_type(value, parsed_args[value], value_type,
arg.get(defs.ITEMS), arg.get(defs.MIN), arg.get(defs.MAX))
elif defs.DEFAULT in arg: # Has a default feild
elif defs.DEFAULT in arg: # Has a default field
# return default values for unset parameters
parsed_args[value] = arg[defs.DEFAULT]
elif arg[defs.REQUIRED]: # requires field is true
Expand Down Expand Up @@ -350,7 +350,7 @@ def _parse_select_options(arg):


def print_plugin_args(plugin_path):
"""Pring plugin parameters table."""
"""Print plugin parameters table."""
args = config_utils.get_config_parameters(plugin_path)
args_format = "{:20} {:10} {:^15} {:^10} {:25}"
title = args_format.format(defs.NAME.upper(), defs.TYPE.upper(), defs.DEFAULT.upper(),
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/utils/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Hooneycomb generic validators."""
"""Honeycomb generic validators."""
from __future__ import unicode_literals, absolute_import

import socket
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/utils/wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def wait_until(func,
:param func: Function to call and wait for
:param bool check_return_value: Examine return value
:param int total_timeout: Wait timeout,
:param float interval: Sleep interval between retrys
:param float interval: Sleep interval between retries
:param list exc_list: Acceptable exception list
:param str error_message: Default error messages
:param args: args to pass to func
Expand Down
Loading

0 comments on commit 02b266e

Please sign in to comment.