# Path: chaosmonkey/engine/app.py
# def configure_engine(database_uri, attacks_folder, planners_folder, cme_timezone):
#     """
#     Create a Flask App and all the configuration needed to run the CMEEngine
# 
#     * Init and configure the SQLAlchemy store (create db and tables if don't exists)
#     * Init ModuleStores (attacks and planners)
#     * Configure the timezone and jobstore for the scheduler
#     * Configure the CMEManager
# 
#     TODO:
#         The scheduler start is not made until the first request is made. This is due to
#         the way the SQLAlchemy store is created, because it needs the app.context to work
#         properly
# 
#     :param database_uri:    SQLAlchemy SQLALCHEMY_DATABASE_URI
#     :param attacks_folder:  folder to load the attacks modules
#     :param planners_folder: folder to load the planners modules
#     :param cme_timezone:    timezone to set in the scheduler
#     """
# 
#     # configure and init FlaskSQLAlchemy
#     if database_uri != ":memory:":
#         # check for memory database for tests if not, make sure the path exists and create it
#         make_sure_path_exists(os.path.dirname(database_uri))
# 
#     flask_app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///%s" % database_uri
#     flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
#     db.init_app(flask_app)
#     with flask_app.app_context():
#         db.create_all()
#         db.app = flask_app
# 
#     # init stores
#     sql_store = CMESQLAlchemyStore()
#     planners_store = ModulesStore(Planner)
#     attacks_store = ModulesStore(Attack)
# 
#     # configure the scheduler
#     tz = timezone(cme_timezone)
#     jobstores = {
#         "default": sql_store
#     }
#     scheduler.configure(jobstores=jobstores, timezone=tz)
# 
#     # configure module stores
#     attacks_store.load(attacks_folder)
#     planners_store.load(planners_folder)
# 
#     # configure CMEManager
#     manager.configure(scheduler, sql_store, planners_store, attacks_store)
# 
# def shutdown_engine():
#     """
#     Shutdown the scheduler
#     """
#     if scheduler.running:
#         scheduler.shutdown()
# 
# Path: chaosmonkey/api/app.py
# def handle_invalid_usage(error):
# 
# Path: chaosmonkey/cm/profiling.py
# PROFILER_FILE_PATH = '/tmp/profile.prof'
# 
# @contextmanager
# def profile_ctx(profiling):
#     """
#     Wrapper around to isolate responbability of profiling in a contextmanager
#     """
#     if profiling:
#         pr = cProfile.Profile()
#         pr.enable()
#     yield
#     if profiling:
#         pr.disable()
#         pr.dump_stats(PROFILER_FILE_PATH)


import logging
import signal
import click
from gevent.wsgi import WSGIServer
from chaosmonkey.engine.app import configure_engine, shutdown_engine
from chaosmonkey.api.app import flask_app
from .profiling import PROFILER_FILE_PATH, profile_ctx




@click.command(name='chaos-monkey-engine')
@click.option("--port", "-p", default=5000, help="Port used to expose the CM API. Default 5000")
@click.option("--timezone", "-t", default="Europe/Madrid", help="Timezone to configure the scheduler. "
                                                                "Default 'Europe/Madrid'")

# Path: chaosmonkey/engine/app.py
# def configure_engine(database_uri, attacks_folder, planners_folder, cme_timezone):
#     """
#     Create a Flask App and all the configuration needed to run the CMEEngine
# 
#     * Init and configure the SQLAlchemy store (create db and tables if don't exists)
#     * Init ModuleStores (attacks and planners)
#     * Configure the timezone and jobstore for the scheduler
#     * Configure the CMEManager
# 
#     TODO:
#         The scheduler start is not made until the first request is made. This is due to
#         the way the SQLAlchemy store is created, because it needs the app.context to work
#         properly
# 
#     :param database_uri:    SQLAlchemy SQLALCHEMY_DATABASE_URI
#     :param attacks_folder:  folder to load the attacks modules
#     :param planners_folder: folder to load the planners modules
#     :param cme_timezone:    timezone to set in the scheduler
#     """
# 
#     # configure and init FlaskSQLAlchemy
#     if database_uri != ":memory:":
#         # check for memory database for tests if not, make sure the path exists and create it
#         make_sure_path_exists(os.path.dirname(database_uri))
# 
#     flask_app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///%s" % database_uri
#     flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
#     db.init_app(flask_app)
#     with flask_app.app_context():
#         db.create_all()
#         db.app = flask_app
# 
#     # init stores
#     sql_store = CMESQLAlchemyStore()
#     planners_store = ModulesStore(Planner)
#     attacks_store = ModulesStore(Attack)
# 
#     # configure the scheduler
#     tz = timezone(cme_timezone)
#     jobstores = {
#         "default": sql_store
#     }
#     scheduler.configure(jobstores=jobstores, timezone=tz)
# 
#     # configure module stores
#     attacks_store.load(attacks_folder)
#     planners_store.load(planners_folder)
# 
#     # configure CMEManager
#     manager.configure(scheduler, sql_store, planners_store, attacks_store)
# 
# def shutdown_engine():
#     """
#     Shutdown the scheduler
#     """
#     if scheduler.running:
#         scheduler.shutdown()
# 
# Path: chaosmonkey/api/app.py
# def handle_invalid_usage(error):
# 
# Path: chaosmonkey/cm/profiling.py
# PROFILER_FILE_PATH = '/tmp/profile.prof'
# 
# @contextmanager
# def profile_ctx(profiling):
#     """
#     Wrapper around to isolate responbability of profiling in a contextmanager
#     """
#     if profiling:
#         pr = cProfile.Profile()
#         pr.enable()
#     yield
#     if profiling:
#         pr.disable()
#         pr.dump_stats(PROFILER_FILE_PATH)


# Path: chaosmonkey/cm/cm.py
import logging
import signal
import click
from gevent.wsgi import WSGIServer
from chaosmonkey.engine.app import configure_engine, shutdown_engine
from chaosmonkey.api.app import flask_app
from .profiling import PROFILER_FILE_PATH, profile_ctx





@click.command(name='chaos-monkey-engine')
@click.option("--port", "-p", default=5000, help="Port used to expose the CM API. Default 5000")
@click.option("--timezone", "-t", default="Europe/Madrid", help="Timezone to configure the scheduler. "
                                                                "Default 'Europe/Madrid'")

@click.option("--profiling", "-x", default=False, help="Enable cProfile module, dumping data in /tmp/profile.prof")