<reponame>opendxl-community/opendxl-cuckoo-service<filename>dxlcuckooservice/__main__.py
# Shout out to <NAME>, whose epo-service/epo-client libraries created an
# excellent framework for Sofware Wrappers.
# By <NAME>

import logging
from logging.config import fileConfig

import sys
import os
import signal
import threading

from dxlcuckooservice.service import CuckooService

# The location of the logging configuration file (optional)
LOGGING_CONFIG_FILE = "logging.config"

# Whether the service is running
running = False

# Condition used to notify that the service should exit
run_condition = threading.Condition()


def signal_handler(signum, frame):
    """
    Signal handler invoked when registered signals are triggered

    :param signum: The signal number
    :param frame: The frame
    """
    del signum, frame
    global running, run_condition
    with run_condition:
        if running:
            # Stop the service
            running = False
            run_condition.notify()
        else:
            exit(1)

# Signals to register for
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)

# Validate command line
if len(sys.argv) != 2:
    print "Usage: dxlcuckooservice <configuration files directory>"
    sys.exit(1)

#
# Configure Logging
#

config_dir = sys.argv[1]
logging_config_path = os.path.join(config_dir, LOGGING_CONFIG_FILE)
if os.access(logging_config_path, os.R_OK):
    # Log configuration via configuration file
    fileConfig(logging_config_path, disable_existing_loggers=False)
    logger = logging.getLogger()
else:
    # Default log configuration (no configuration file)
    log_formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')

    console_handler = logging.StreamHandler()
    console_handler.setFormatter(log_formatter)

    logger = logging.getLogger()
    logger.addHandler(console_handler)
    logger.setLevel(logging.INFO)


# Create the cuckoo service
with CuckooService(sys.argv[1]) as cuckoo_service:
    try:
        # Run the cuckoo service
        cuckoo_service.run()
        running = True

        logger.info("Waiting for requests ...")
        with run_condition:
            # Wait until notified to exit
            while running:
                run_condition.wait(60)

    except KeyboardInterrupt:
        pass
    except:
        logger.exception("Error occurred, exiting")

# Shout out to <NAME>, whose epo-service/epo-client libraries created an
# excellent framework for Sofware Wrappers.
# By <NAME>

import logging
from logging.config import fileConfig

import sys
import os
import signal
import threading

from dxlcuckooservice.service import CuckooService

# The location of the logging configuration file (optional)
LOGGING_CONFIG_FILE = "logging.config"

# Whether the service is running
running = False

# Condition used to notify that the service should exit
run_condition = threading.Condition()


def signal_handler(signum, frame):
    """
    Signal handler invoked when registered signals are triggered

    :param signum: The signal number
    :param frame: The frame
    """
    del signum, frame
    global running, run_condition
    with run_condition:
        if running:
            # Stop the service
            running = False
            run_condition.notify()
        else:
            exit(1)

# Signals to register for
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)

# Validate command line
if len(sys.argv) != 2:
    print "Usage: dxlcuckooservice <configuration files directory>"
    sys.exit(1)

#
# Configure Logging
#

config_dir = sys.argv[1]
logging_config_path = os.path.join(config_dir, LOGGING_CONFIG_FILE)
if os.access(logging_config_path, os.R_OK):
    # Log configuration via configuration file
    fileConfig(logging_config_path, disable_existing_loggers=False)
    logger = logging.getLogger()
else:
    # Default log configuration (no configuration file)
    log_formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')

    console_handler = logging.StreamHandler()
    console_handler.setFormatter(log_formatter)

    logger = logging.getLogger()
    logger.addHandler(console_handler)
    logger.setLevel(logging.INFO)


# Create the cuckoo service
with CuckooService(sys.argv[1]) as cuckoo_service:
    try:
        # Run the cuckoo service
        cuckoo_service.run()
        running = True

        logger.info("Waiting for requests ...")
        with run_condition:
            # Wait until notified to exit
            while running:
                run_condition.wait(60)

    except KeyboardInterrupt:
        pass
    except:
        logger.exception("Error occurred, exiting")
# Shout out to <NAME>, whose epo-service/epo-client libraries created an # excellent framework for Sofware Wrappers. # By <NAME> # The location of the logging configuration file (optional) # Whether the service is running # Condition used to notify that the service should exit Signal handler invoked when registered signals are triggered      :param signum: The signal number     :param frame: The frame # Stop the service # Signals to register for # Validate command line # # Configure Logging # # Log configuration via configuration file # Default log configuration (no configuration file) # Create the cuckoo service # Run the cuckoo service # Wait until notified to exit