# Path: firefly/data_server.py
# def initialize_data_server(config_global, secret_key=None):
#     config = config_global["data_server"]
# 
#     # connect to the database to store annotation in
#     # I kind of hate having the schema for this DB here, but I'm going to leave it for to retain parity with ui_server.py
#     db_conn = sqlite3.connect(config['db_file'], isolation_level=None)
#     db_conn.execute("""
#         create table if not exists annotations (
#             id integer primary key autoincrement,
#             type integer not null,
#             description text not null,
#             time float not null
#         )""")
#     db_conn.execute("create index if not exists time on annotations(time)")
# 
#     config['db'] = db_conn
#     config["secret_key"] = secret_key
# 
#     # init the application instance
#     application = tornado.web.Application([
#         (r"/data", DataHandler),
#         (r"/legend", GraphLegendHandler),
#         (r"/title", GraphTitleHandler),
#         (r"/ping", PingHandler),
#         (r"/annotations", AnnotationsHandler),
#         (r"/add_annotation", AddAnnotationHandler),
#         (r"/sources", SourcesHandler)], **config)
# 
#     # start the main server
#     http_server = tornado.httpserver.HTTPServer(application)
#     http_server.bind(config["port"])
#     http_server.start(0)
# 
#     # setup logging
#     util.setup_logging(config_global)
# 
#     log.info('Firefly data server started on port %d' % config["port"])
# 
# Path: firefly/ui_server.py
# def initialize_ui_server(config_global, secret_key=None):
#     config = config_global["ui_server"]
# 
#     # connect to the database
#     conn = sqlite3.connect(config['db_file'], isolation_level=None)
#     conn.execute("create table if not exists states (id integer primary key autoincrement, state text not null, state_hash blob not null)")
#     conn.execute("create index if not exists hash_idx on states(state_hash)")
#     conn.execute("create table if not exists names (id integer primary key autoincrement, name text not null, stateid integer not null)")
#     conn.execute("create unique index if not exists name_idx on names (name)")
# 
#     config["static_path"] = os.path.join(os.path.join(*os.path.split(__file__)[:-1]), 'static')
#     config["db_connection"] = conn
#     config["static_url_prefix"] = os.path.join(config["url_path_prefix"], "static") + "/"
#     config["secret_key"] = secret_key
#     config['autoescape'] = None
# 
#     # init the application instance
#     application = tornado.web.Application([
#         (r"/", IndexHandler),
#         (r"/token", TokenHandler),
#         (r"/shorten", ShortenHandler),
#         (r"/expand/(.*)", ExpandHandler),
#         (r"/redirect/(.*)", RedirectHandler),
#         (r"/named/(.*)", NameHandler),
#         (r"/dashboards", DashboardListHandler),
#         (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": config['static_path']}),
#     ], **config)
# 
#     # start the main server
#     http_server = tornado.httpserver.HTTPServer(application)
#     http_server.listen(config["port"])
# 
#     # setup logging
#     util.setup_logging(config_global)
# 
#     log.info('Firefly UI server started on port %d' % config["port"])


from collections import defaultdict
from optparse import OptionGroup, OptionParser
from firefly.data_server import initialize_data_server
from firefly.ui_server import initialize_ui_server
import logging
import os
import socket
import sys
import util
import tornado.ioloop
import yaml
                'name': 'http://%s:%s' % (socket.getfqdn(), config["data_server"]["port"]),
                'desc': socket.getfqdn()
            })

    data_sources = []

    # mix in the configured data sources to the data server configuration
    def get_ds_instance(ds):
        ds_class = util.import_module_class(ds)
        ds_kwargs = config['data_server']['data_source_config'].get(ds, {})
        ds_instance = ds_class(**ds_kwargs)  # args only used by StatMonsterRRD atm
        key = util.generate_ds_key(ds)
        ds_instance._FF_KEY = key
        config['data_server']['data_sources_by_key'][key] = ds_instance
        return ds_instance

    for ds in config['data_server']['data_sources']:
        ds_instance = get_ds_instance(ds)
        data_sources.append(ds_instance)
        log.debug('Using datasource %s' % type(ds_instance).__name__)

    config["data_server"]["data_sources"] = data_sources

    if "secret_key" not in config.keys():
        log.error("No Secret Key Provided: Exiting")
        sys.exit(1)

    if not options.omit_data_server:
        # Allow the data server to initialize itself and attach itself to the IOLoop
        try:

# Path: firefly/data_server.py
# def initialize_data_server(config_global, secret_key=None):
#     config = config_global["data_server"]
# 
#     # connect to the database to store annotation in
#     # I kind of hate having the schema for this DB here, but I'm going to leave it for to retain parity with ui_server.py
#     db_conn = sqlite3.connect(config['db_file'], isolation_level=None)
#     db_conn.execute("""
#         create table if not exists annotations (
#             id integer primary key autoincrement,
#             type integer not null,
#             description text not null,
#             time float not null
#         )""")
#     db_conn.execute("create index if not exists time on annotations(time)")
# 
#     config['db'] = db_conn
#     config["secret_key"] = secret_key
# 
#     # init the application instance
#     application = tornado.web.Application([
#         (r"/data", DataHandler),
#         (r"/legend", GraphLegendHandler),
#         (r"/title", GraphTitleHandler),
#         (r"/ping", PingHandler),
#         (r"/annotations", AnnotationsHandler),
#         (r"/add_annotation", AddAnnotationHandler),
#         (r"/sources", SourcesHandler)], **config)
# 
#     # start the main server
#     http_server = tornado.httpserver.HTTPServer(application)
#     http_server.bind(config["port"])
#     http_server.start(0)
# 
#     # setup logging
#     util.setup_logging(config_global)
# 
#     log.info('Firefly data server started on port %d' % config["port"])
# 
# Path: firefly/ui_server.py
# def initialize_ui_server(config_global, secret_key=None):
#     config = config_global["ui_server"]
# 
#     # connect to the database
#     conn = sqlite3.connect(config['db_file'], isolation_level=None)
#     conn.execute("create table if not exists states (id integer primary key autoincrement, state text not null, state_hash blob not null)")
#     conn.execute("create index if not exists hash_idx on states(state_hash)")
#     conn.execute("create table if not exists names (id integer primary key autoincrement, name text not null, stateid integer not null)")
#     conn.execute("create unique index if not exists name_idx on names (name)")
# 
#     config["static_path"] = os.path.join(os.path.join(*os.path.split(__file__)[:-1]), 'static')
#     config["db_connection"] = conn
#     config["static_url_prefix"] = os.path.join(config["url_path_prefix"], "static") + "/"
#     config["secret_key"] = secret_key
#     config['autoescape'] = None
# 
#     # init the application instance
#     application = tornado.web.Application([
#         (r"/", IndexHandler),
#         (r"/token", TokenHandler),
#         (r"/shorten", ShortenHandler),
#         (r"/expand/(.*)", ExpandHandler),
#         (r"/redirect/(.*)", RedirectHandler),
#         (r"/named/(.*)", NameHandler),
#         (r"/dashboards", DashboardListHandler),
#         (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": config['static_path']}),
#     ], **config)
# 
#     # start the main server
#     http_server = tornado.httpserver.HTTPServer(application)
#     http_server.listen(config["port"])
# 
#     # setup logging
#     util.setup_logging(config_global)
# 
#     log.info('Firefly UI server started on port %d' % config["port"])


# Path: firefly/main.py
from collections import defaultdict
from optparse import OptionGroup, OptionParser
from firefly.data_server import initialize_data_server
from firefly.ui_server import initialize_ui_server
import logging
import os
import socket
import sys
import util
import tornado.ioloop
import yaml

                'name': 'http://%s:%s' % (socket.getfqdn(), config["data_server"]["port"]),
                'desc': socket.getfqdn()
            })

    data_sources = []

    # mix in the configured data sources to the data server configuration
    def get_ds_instance(ds):
        ds_class = util.import_module_class(ds)
        ds_kwargs = config['data_server']['data_source_config'].get(ds, {})
        ds_instance = ds_class(**ds_kwargs)  # args only used by StatMonsterRRD atm
        key = util.generate_ds_key(ds)
        ds_instance._FF_KEY = key
        config['data_server']['data_sources_by_key'][key] = ds_instance
        return ds_instance

    for ds in config['data_server']['data_sources']:
        ds_instance = get_ds_instance(ds)
        data_sources.append(ds_instance)
        log.debug('Using datasource %s' % type(ds_instance).__name__)

    config["data_server"]["data_sources"] = data_sources

    if "secret_key" not in config.keys():
        log.error("No Secret Key Provided: Exiting")
        sys.exit(1)

    if not options.omit_data_server:
        # Allow the data server to initialize itself and attach itself to the IOLoop
        try:

            initialize_data_server(config, secret_key=config["secret_key"])