Ejemplo n.º 1
0
def save_config():
    logging.info("Saving current config to: %s" % options.config)
    with open(options.config, 'w') as fp:
        fp.write("##########################")
        fp.write(" Root the Box Config File ")
        fp.write("##########################\n")
        fp.write("# Last updated: %s\n" % datetime.now())
        for group in options.groups():
            # Shitty work around for Tornado 4.1
            if 'rootthebox.py' in group.lower() or group == '':
                continue
            fp.write("\n# [ %s ]\n" % group.title())
            try:
                # python2
                opt = options.group_dict(group).iteritems()
            except AttributeError:
                # python3
                opt = options.group_dict(group).items()
            for key, value in opt:
                try:
                    # python2
                    value_type = basestring
                except NameError:
                    # python 3
                    value_type = str
                if isinstance(value, value_type):
                    # Str/Unicode needs to have quotes
                    fp.write(u'%s = "%s"\n' % (key, value))
                else:
                    # Int/Bool/List use __str__
                    fp.write('%s = %s\n' % (key, value))
Ejemplo n.º 2
0
def main():
    if os.path.exists(options.config_file):
        parse_config_file(options.config_file)
    parse_command_line()

    app = Application(
        [
            url(r'/token/?', auth.TokenHandler),
            url(r'/tasks/?', tasks.TaskHandler),
            url(r'/tasks/({uuid})/?'.format(uuid=UUID_PATTERN),
                tasks.TaskHandler),
            url(r'/domains/?', vms.DomainHandler),
            url(r'/domains/({uuid})/?'.format(uuid=UUID_PATTERN),
                vms.DomainHandler),
            url(r'/nodes/?', vms.NodeHandler),
            url(r'/nodes/([\d+])/?', vms.NodeHandler)
        ],
        template_path=os.path.join(BASE_DIR, 'templates'),
        static_path=os.path.join(BASE_DIR, 'static'),
        **options.group_dict('application')
    )
    app.listen(options.port)

    logging.info('Listening on http://localhost:%d' % options.port)

    IOLoop.current().add_callback(start_queue, app)
    IOLoop.current().start()
Ejemplo n.º 3
0
def main():
    """Construct and serve the tornado application."""
    api_root = '/api/v1'
    app = Application(
        handlers=[
            (r'/', HomeView),
            (r'/favicon.ico', HomeView),
            (r'/error_500', ErrorView),
            (api_root, InfoView),
            (api_root + r'/login', LoginView),
            (api_root + r'/accounts', RegistrationView),
            (api_root + r'/accounts/([\w]+)', ProfileView),
            (api_root + r'/accounts/([\w]+)/tasks', TaskListView),
            (api_root + r'/accounts/([\w]+)/tasks/([\d]+)', TaskView),
            (api_root + r'/accounts/([\w]+)/logout', LogoutView),
        ],
        db=SQLAlchemy(
            os.environ.get(
                'DATABASE_URL',
                'postgres://*****:*****@localhost:5432/task_manager')),
        cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
        **options.group_dict('application'),
        login_url="/api/v1/login",
        xsrf_cookies=True,
        debug=True,
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        template_path=os.path.join(os.path.dirname(__file__), "templates"))
    http_server = HTTPServer(app)
    http_server.listen(options.port)
    print('Listening on http://localhost:%d' % options.port)
    logging.info('Listening on http://localhost:%d' % options.port)
    IOLoop.current().start()
Ejemplo n.º 4
0
 def setup_options(self):
     name = self.name
     opts = options.group_dict(f'{name} database')
     uri = opts[option_name(name, 'uri')]
     r = urlparse(uri)
     q = query_as_dict(r.query)
     if r.scheme.lower() != 'mysql':
         raise MysqlConnectorError(
             f'{uri} is not a mysql connection scheme')
     self._host = r.hostname or 'localhost'
     self._port = r.port or 3306
     self._user = r.username
     self._password = r.password
     self._db = r.path.lstrip('/') or r.username
     self._charset = q.get('charset') or 'utf8mb4'
     fmt = ('host={host} port={port} dbname={db} '
            'user={user} password={pw} charset={charset}')
     dsn = fmt.format(host=self._host,
                      port=self._port,
                      user=self._user,
                      pw=self._password,
                      db=self._db,
                      charset=self._charset)
     self._connection_string = dsn
     self._num_connections = opts[option_name(name, 'num-connections')]
     self._pool_recycle = opts[option_name(name, 'pool-recycle')]
Ejemplo n.º 5
0
def main():
    define("debug", default=False, help="Run in debug mode")
    options.parse_command_line()
    settings = {}
    settings.update(options.group_dict(None))
    settings.update(tornado_jsonapi.handlers.not_found_handling_settings())

    conn = sqlite3.connect(":memory:")

    r = tornado_jsonapi.resource.DBAPI2Resource(schema, sqlite3, conn)
    r._create_table()

    cur = conn.cursor()
    for i in range(1, 16):
        cur.execute("INSERT INTO posts(text,author) VALUES(?,?)",
                    ("Text" + str(i), str(i)))

    application = tornado.web.Application([
        (
            r"/api/posts/([^/]*)",
            tornado_jsonapi.handlers.APIHandler,
            dict(resource=r),
        ),
    ], **settings)
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()
Ejemplo n.º 6
0
def start(prefix, settings, modules, routes, known_exceptions, **kwargs):
    """starts the tornado application.
    :param prefix: the url prefix
    :param settings: the user defined settings
    :param modules: the modules to load
    :param handlers: the list of url routes (url, handler)
    :param known_exceptions: the mapping of known exceptions to HTTP codes
    :param kwargs: the tornado application arguments
    """
    from tornado.options import options

    options.define(
        "config",
        type=str,
        help="path to config file",
        callback=lambda p: options.parse_config_file(p, final=False))
    options.define("port", default=8000, help="listening port", type=int)
    options.define("address", default='127.0.0.1', help="listening address")

    options.add_parse_callback(log.patch_logger)

    loop = _get_event_loop()
    modules_registry = ModulesRegistry(loop.asyncio_loop, log.gen_log)

    for module in modules:
        modules_registry.lazy_load(module, options)

    for opt in settings:
        options.define(**opt)

    options.parse_command_line(final=True)

    if not prefix.endswith('/'):
        prefix += '/'

    kwargs.update(options.group_dict('application'))
    kwargs.setdefault('default_handler_class', handler.DefaultHandler)
    # prevent override this option
    kwargs['known_exceptions'] = known_exceptions
    kwargs['modules'] = modules_registry

    handlers = []
    for uri, methods in routes:
        log.app_log.info("add resource: %s", uri)
        handlers.append((_concat_url(prefix, uri), compile_handler(methods)))

    app = web.Application(handlers, **kwargs)
    app.listen(options.port, options.address, xheaders=True)

    signal.signal(signal.SIGTERM, lambda *x: loop.stop())
    log.app_log.info("start listening on %s:%d", options.address, options.port
                     or 80)

    try:
        loop.start()
    except (KeyboardInterrupt, SystemExit):
        pass

    loop.close()
    log.app_log.info("gracefully shutdown.")
Ejemplo n.º 7
0
async def infinite_update_loop():
    while True:
        await asyncio.sleep(
            UPDATE_PERIOD
        )  # switch to other code and continue execution in 5 seconds

        db = options.group_dict('db')
        dbpass = db['password']
        dbuser = db['username']
        dbhost = db['host']
        dbname = db['name']
        dbport = db['port']

        session = queries.TornadoSession(
            f'postgresql://{dbuser}:{dbpass}@{dbhost}:{dbport}/{dbname}')

        ## DETERMINE IF THERE WAS AN UPDATE OR NOT BY CHECKING IN THE PASSED XX SECONDS (which should coincide with
        ## HOW EVER LONG WE WAIT BAHHHH!HHH!HH!H!!HHHH
        if update:
            logger.debug(f"sending update, next update in {UPDATE_PERIOD}")
            for connection in connections:
                await to_asyncio_future(
                    connection.write_message(msg)
                )  # send message to each connected client
        else:
            logger.debug(f"next update in {UPDATE_PERIOD}")
Ejemplo n.º 8
0
def main():
    define("debug", default=False, help="Run in debug mode")
    options.parse_command_line()
    settings = {}
    settings.update(options.group_dict(None))
    settings.update(tornado_jsonapi.handlers.not_found_handling_settings())

    io_loop = tornado.ioloop.IOLoop.current()

    # connect to postgre using momoko
    #  see http://momoko.61924.nl/en/latest/tutorial.html#trival-example
    conn = momoko.Pool(
        'dbname=postgres '
        'user=postgres '
        'host=localhost '
        'port=5432',
        ioloop=io_loop)
    future = conn.connect()
    io_loop.add_future(future, lambda x: io_loop.stop())
    io_loop.start()
    connection = future.result()  # raises exception on connection error

    r = tornado_jsonapi.resource.DBAPI2Resource(schema, momoko, connection)
    io_loop.add_future(r._create_table(), lambda x: io_loop.stop())
    io_loop.start()
    application = tornado.web.Application([
        (r"/api/posts/([^/]*)", tornado_jsonapi.handlers.APIHandler,
         dict(resource=r)),
    ], **settings)
    application.listen(8888)
    io_loop.start()
Ejemplo n.º 9
0
def save_config():
    logging.info("Saving current config to: %s" % options.config)
    with open(options.config, "w") as fp:
        fp.write("##########################")
        fp.write(" Root the Box Config File ")
        fp.write("##########################\n")
        fp.write(
            "# Documentation: %s\n" %
            "https://github.com/moloch--/RootTheBox/wiki/Configuration-File-Details"
        )
        fp.write("# Last updated: %s\n" % datetime.now())
        for group in options.groups():
            # Shitty work around for Tornado 4.1
            if "rootthebox.py" in group.lower() or group == "":
                continue
            fp.write("\n# [ %s ]\n" % group.title())
            opt = list(options.group_dict(group).items())
            for key, value in opt:
                try:
                    # python2
                    value_type = basestring
                except NameError:
                    # python 3
                    value_type = str
                if isinstance(value, value_type):
                    # Str/Unicode needs to have quotes
                    fp.write('%s = "%s"\n' % (key, value))
                else:
                    # Int/Bool/List use __str__
                    fp.write("%s = %s\n" % (key, value))
Ejemplo n.º 10
0
 def export_game_config(self, root):
     """
     Exports the game configuration options to an XML doc.
     """
     game_elem = ET.SubElement(root, "configuration")
     game_list = options.group_dict("game")
     images = ["ctf_logo", "story_character", "scoreboard_right_image"]
     for key in game_list:
         value = game_list[key]
         if key in images and len(value) > 0:
             if os.path.isfile(value):
                 path = value
             elif os.path.isfile(os.path.join("files", value)):
                 path = os.path.join("files", value)
             elif value[0] == "/" and os.path.isfile(value[1:]):
                 path = value[1:]
             else:
                 continue
             with open(path, mode="rb") as logo:
                 data = logo.read()
                 ET.SubElement(game_elem, key).text = encode(data, "base64")
         elif isinstance(value, list):
             child = ET.Element(key)
             game_elem.append(child)
             for item in value:
                 ET.SubElement(child, "line").text = str(item)
         elif len(str(value)) > 0:
             ET.SubElement(game_elem, key).text = str(value)
Ejemplo n.º 11
0
    async def connect(self, load_balancing_policy=None):
        name = self.name
        opts = options.group_dict(f'{name} cassandra')
        uri = opts[option_name(name, 'uri')]
        r = urlparse(uri)
        query_as_dict(r.query)
        if r.scheme.lower() != 'cassandra':
            raise CassandraConnectorError(
                f'{uri} is not a cassandra connection scheme')
        self._hosts = r.hostname.split(',') if r.hostname else ['127.0.0.1']
        self._port = r.port or 9042
        self._keyspace = None
        self._user = r.username
        self._password = r.password
        keyspace = r.path.lstrip('/')
        if keyspace:
            self._keyspace = keyspace
        executor_threads = opts[option_name(name, 'executor-threads')]
        _load_balancing_policy = load_balancing_policy or RoundRobinPolicy()
        LOG.info(f'connecting cassandra [{self.name}] {uri}')
        auth_provider = PlainTextAuthProvider(
            username=self._user, password=self._password)

        # TODO 选取合适的loadbalancingpolicy
        # Cluster.__init__ called with contact_points specified
        # should specify a load-balancing policy
        # http://datastax.github.io/python-driver/_modules/cassandra/cluster.html#Cluster # NOQA
        # RoundRobinPolicy:
        # http://datastax.github.io/python-driver/_modules/cassandra/policies.html#RoundRobinPolicy # NOQA
        cluster = Cluster(contact_points=self._hosts,
                          port=self._port, executor_threads=executor_threads,
                          load_balancing_policy=_load_balancing_policy,
                          auth_provider=auth_provider)
        self._session = cluster.connect(keyspace=self._keyspace)
        aiosession(self._session)
Ejemplo n.º 12
0
 async def connect(self, event_loop=None):
     if event_loop is None:
         event_loop = asyncio.get_event_loop()
     name = self.name
     opts = options.group_dict(f'{name} queue')
     uri = opts[option_name(name, 'uri')]
     LOG.info(f'connecting amqp [{self.name} {uri}')
     self._connection = await connect_robust(uri, loop=event_loop)
Ejemplo n.º 13
0
def run_server():
    bootstrap()
    client = connect_to_mongo()
    database = client['tinyurld']
    app = make_app(database, options.group_dict('application'))
    tornado.log.app_log.info('Start application at {}:{} port'.format(options.host, options.port))
    app.listen(options.port, address=options.host)
    IOLoop.current().start()
Ejemplo n.º 14
0
 async def connect(self, event_loop=None):
     if event_loop is None:
         event_loop = asyncio.get_event_loop()
     name = self.name
     opts = options.group_dict('%s queue' % name)
     uri = opts[option_name(name, "uri")]
     LOG.info('connecting amqp [%s] %s' % (self.name, uri))
     self._connection = await connect_robust(uri, loop=event_loop)
Ejemplo n.º 15
0
def run_server():
    bootstrap()
    client = connect_to_mongo()
    database = client['tinyurld']
    app = make_app(database, options.group_dict('application'))
    tornado.log.app_log.info('Start application at {}:{} port'.format(
        options.host, options.port))
    app.listen(options.port, address=options.host)
    IOLoop.current().start()
Ejemplo n.º 16
0
def start(prefix, settings, modules, routes, known_exceptions, **kwargs):
    """starts the tornado application.
    :param prefix: the url prefix
    :param settings: the user defined settings
    :param modules: the modules to load
    :param handlers: the list of url routes (url, handler)
    :param known_exceptions: the mapping of known exceptions to HTTP codes
    :param kwargs: the tornado application arguments
    """
    from tornado.options import options

    options.define("config", type=str, help="path to config file",
                   callback=lambda p: options.parse_config_file(p, final=False))
    options.define("port", default=8000, help="listening port", type=int)
    options.define("address", default='127.0.0.1', help="listening address")

    options.add_parse_callback(log.patch_logger)

    loop = _get_event_loop()
    modules_registry = ModulesRegistry(loop.asyncio_loop, log.gen_log)

    for module in modules:
        modules_registry.lazy_load(module, options)

    for opt in settings:
        options.define(**opt)

    options.parse_command_line(final=True)

    if not prefix.endswith('/'):
        prefix += '/'

    kwargs.update(options.group_dict('application'))
    kwargs.setdefault('default_handler_class', handler.DefaultHandler)
    # prevent override this option
    kwargs['known_exceptions'] = known_exceptions
    kwargs['modules'] = modules_registry

    handlers = []
    for uri, methods in routes:
        log.app_log.info("add resource: %s", uri)
        handlers.append((_concat_url(prefix, uri), compile_handler(methods)))

    app = web.Application(handlers, **kwargs)
    app.listen(options.port, options.address, xheaders=True)

    signal.signal(signal.SIGTERM, lambda *x: loop.stop())
    log.app_log.info("start listening on %s:%d", options.address, options.port or 80)

    try:
        loop.start()
    except (KeyboardInterrupt, SystemExit):
        pass

    loop.close()
    log.app_log.info("gracefully shutdown.")
Ejemplo n.º 17
0
 def decorator(self, *args, **kwargs):
     user = HandlerMethods.get_current_user(self)
     if not user:
         self.set_status(401)
         self.write({'status': 'error', 'message': 'not authorized'})
     else:
         self.user_authenticator = pickle.loads(user)
         self.user_authenticator.xen = XenAdapter({**opts.group_dict('xenadapter'), **opts.group_dict('rethinkdb')})
         self.xen = self.user_authenticator.xen
         return method(self)
Ejemplo n.º 18
0
    def initialize(self):
        logger.debug("setting up db session")
        db = options.group_dict('db')
        dbpass = db['password']
        dbuser = db['username']
        dbhost = db['host']
        dbname = db['name']
        dbport = db['port']

        self.session = queries.TornadoSession(
            f'postgresql://{dbuser}:{dbpass}@{dbhost}:{dbport}/{dbname}')
Ejemplo n.º 19
0
 def start(name, io_loop=None):
     opt_name = '' if name == 'master' else '-%s' % name
     postgres_settings = tornado_options.group_dict('%s database' % name)
     instance = PostgreSQLConnector.instance(name)
     uri = postgres_settings['postgres%s-uri' % opt_name]
     if not io_loop:
         io_loop = tornado.ioloop.IOLoop.instance()
     def _start():
         return instance.connect(uri, **postgres_settings)
     #io_loop.add_future(f, _done)
     io_loop.run_sync(_start)
Ejemplo n.º 20
0
def get_engine(db_path=None):
    """Create and return engine. If db_path is not given, settings in
    app.conf are used"""
    global engine
    if not engine:
        if not db_path:
            db_path = options.db_path % options.group_dict('db')
        engine = create_engine(db_path,
                               convert_unicode=True,
                               echo=options.db_debug,
                               pool_size=POOL_SIZE)
    return engine
Ejemplo n.º 21
0
def main():
    parse_command_line(final=False)
    parse_config_file(options.config_file)

    app = Application([("/", MainHandler), ("/login", LoginHandler),
                       ("/logout", LogoutHandler)],
                      login_url="/login",
                      **options.group_dict("application"))
    app.listen(options.port)

    logging.info("Listening on http://localhost:%d" % options.port)
    IOLoop.current().start()
Ejemplo n.º 22
0
def main():
    parse_command_line(final=False)
    parse_config_file(options.config_file)

    app = Application(
        [("/", MainHandler), ("/login", LoginHandler), ("/logout", LogoutHandler)],
        login_url="/login",
        **options.group_dict("application")
    )
    app.listen(options.port)

    logging.info("Listening on http://localhost:%d" % options.port)
    IOLoop.current().start()
Ejemplo n.º 23
0
def main():
    parse_command_line(final=False)
    parse_config_file(options.config_file)

    app = Application([
        ('/', MainHandler),
        ('/login', LoginHandler),
        ('/logout', LogoutHandler),
    ],
                      login_url='/login',
                      **options.group_dict('application'))
    app.listen(options.port)

    logging.info('Listening on http://localhost:%d' % options.port)
    IOLoop.instance().start()
Ejemplo n.º 24
0
def main():
    if os.path.exists(options.config_file):
        parse_config_file(options.config_file)
    parse_command_line()

    app = Application([
        url(r'/directions/?', directions.DirectionsHandler),
        url(r'/routes/?', routes.RoutesHandler),
        url(r'/routes/({uuid})/?'.format(uuid=UUID4_PATTERN),
            routes.RoutesHandler),
    ], **options.group_dict('application'))
    app.listen(port=options.port, address=options.host)
    logging.info('Listening on http://%s:%d' % (options.host, options.port))

    IOLoop.current().start()
Ejemplo n.º 25
0
def main():
    define("debug", default=False, help="Run in debug mode")
    options.parse_command_line()
    settings = {}
    settings.update(options.group_dict(None))
    settings.update(tornado_jsonapi.handlers.not_found_handling_settings())
    application = tornado.web.Application([
        (
            r"/api/posts/([^/]*)",
            tornado_jsonapi.handlers.APIHandler,
            dict(resource=Posts(json.loads(open("data.json").read()))),
        ),
    ], **settings)
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()
Ejemplo n.º 26
0
def make_app():
    wsgi_app = get_wsgi_application()
    container = tornado.wsgi.WSGIContainer(wsgi_app)
    urls_to_tornado = [
        (r'/static/?(.*)', tornado.web.StaticFileHandler, {
            'path': STATIC_ROOT,
            'default_filename': 'index.html'
        }),
        (r'/api/(.*)', tornado.web.FallbackHandler, dict(fallback=container)),
        (r'/?(.*)', tornado.web.StaticFileHandler, {
            'path': ANGULARJS_APP,
            'default_filename': 'index.html'
        }),
    ]
    return tornado.web.Application(urls_to_tornado,
                                   **options.group_dict('application'))
Ejemplo n.º 27
0
def main():

    parse_command_line(final=False)
    parse_config_file(options.config)
    handlers = [
        URLSpec("/", MainHandler, name="root"),
        URLSpec("/login", LoginHandler, name="login"),
        URLSpec("/logout", LogoutHandler, name="logout")
    ]
    app = Application(handlers,
                      login_url="/login",
                      **options.group_dict("application"))
    app.listen(options.port)

    logging.info("Listening on http://localhost:%d" % options.port)
    IOLoop.current().start()
Ejemplo n.º 28
0
def main():
    parse_command_line(final=False)
    parse_config_file(options.config_file)

    app = Application(
        [
            ('/', MainHandler),
            ('/login', LoginHandler),
            ('/logout', LogoutHandler),
        ],
        login_url='/login',
        **options.group_dict('application'))
    app.listen(options.port)

    logging.info('Listening on http://localhost:%d' % options.port)
    IOLoop.instance().start()
Ejemplo n.º 29
0
def main():
    define("debug", default=False, help="Run in debug mode")
    options.parse_command_line()
    settings = {}
    settings.update(options.group_dict(None))
    settings.update(tornado_jsonapi.handlers.not_found_handling_settings())

    r = tornado_jsonapi.resource.DBAPI2Resource(schema, sqlite3,
                                                sqlite3.connect(':memory:'))
    r._create_table()
    application = tornado.web.Application([
        (r"/api/posts/([^/]*)", tornado_jsonapi.handlers.APIHandler,
         dict(resource=r)),
    ], **settings)
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()
Ejemplo n.º 30
0
    def prepare(self):
        super().prepare()

        #copy some members to context, we'll use then in a resolvers
        self.request.async_run = self.async_run

        self.request.log = self.log
        self.request.actions_log = self.actions_log
        user = self.get_current_user()
        self.request.executor = self.executor
        if user:
            self.request.user_authenticator = pickle.loads(user)
            self.request.user_authenticator.xen = XenAdapter({**opts.group_dict('xenadapter'), **opts.group_dict('rethinkdb')})

            self.request.set_task_status = lambda **operation: self.op.upsert_task(self.request.user_authenticator, operation)
            self.request.get_task_status = lambda id: self.op.get_task(self.request.user_authenticator, id)
        self.request.conn = self.conn
Ejemplo n.º 31
0
def main():
    tornado.options.parse_command_line()

    if options.config:
        config_to_read = options.config
    else:
        config_to_read = './server.conf'

    tornado.options.parse_config_file(config_to_read)

    if options.debug:
        logging.getLogger("palnot.server").setLevel(logging.DEBUG)

    http_server = tornado.httpserver.HTTPServer(setup_application(options.group_dict('application')))
    http_server.listen(options.port)

    tornado.ioloop.IOLoop.instance().start()
Ejemplo n.º 32
0
def make_app():
    """
    Create a Tornado app for BackendWebServer.
    """
    return tornado.web.Application(
        [("/", MainHandler), (r"/archive", ArchiveHandler),
         (r"/ranking", RankingHandler), (r"/problem/.+", ProblemHandler),
         (r"/contestlist", ContestListHandler),
         (r"/submissions", SubmissionsHandler),
         (r"/submission", SubmissionDetailsHandler),
         (r"/register", RegisterHandler), (r"/contest/.+", ContestHandler),
         (r"/contest/.+/problem/.+", ProblemHandler),
         (r"/user/.+", UserHandler), (r"/login", LoginHandler),
         (r"/logout", LogoutHandler),
         (r"/confirm/.+", UserConfirmationHandler),
         (r"/recovery/.+", RecoveryHandler)],
        **options.group_dict('application'))
Ejemplo n.º 33
0
def save_config():
    logging.info("Saving current config to: %s" % options.config)
    with open(options.config, 'w') as fp:
        fp.write("##########################")
        fp.write(" Sonar Server  Config File ")
        fp.write("##########################\n")
        fp.write("# Last updated: %s\n" % datetime.now())
        for group in options.groups():
            if group.lower() in SKIP_GROUPS or group == '':
                continue
            fp.write("\n# [ %s ]\n" % group.title())
            for key, value in options.group_dict(group).iteritems():
                if isinstance(value, basestring):
                    # Str/Unicode needs to have quotes
                    fp.write(u'%s = "%s"\n' % (key, value))
                else:
                    # Int/Bool/List use __str__
                    fp.write('%s = %s\n' % (key, value))
Ejemplo n.º 34
0
 def setup_options(self):
     name = self.name
     opts = options.group_dict(f'{name} database')
     uri = opts[option_name(name, 'uri')]
     r = urlparse(uri)
     if r.scheme.lower() != 'postgres':
         raise PostgresConnectorError(
             f'{uri} is not a postgres connection scheme')
     fmt = ('host={host} port={port} dbname={db} user={user} password={pw}')
     dsn = fmt.format(host=r.hostname or 'localhost',
                      port=r.port or 5432,
                      user=r.username,
                      pw=r.password,
                      db=r.path.lstrip('/') or r.username)
     self._connection_string = dsn
     self._reconnect_interval = opts[option_name(name,
                                                 'reconnect-interval')]
     self._num_connections = opts[option_name(name, 'num-connections')]
Ejemplo n.º 35
0
def main():
    define("debug", default=False, help="Run in debug mode")
    options.parse_command_line()
    settings = {}
    settings.update(options.group_dict(None))
    settings.update(tornado_jsonapi.handlers.not_found_handling_settings())

    engine = create_engine('sqlite:///:memory:', echo=settings['debug'])
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    application = tornado.web.Application([
        (r"/api/posts/([^/]*)", tornado_jsonapi.handlers.APIHandler,
         dict(resource=tornado_jsonapi.resource.SQLAlchemyResource(
             Post, Session))),
    ], **settings)
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()
Ejemplo n.º 36
0
def main():
    """Construct and serve the application."""
    api_root = '/api/v1'
    app = Application([
        (api_root, InfoView),
        (api_root + r'/accounts', RegistrationView),
        (api_root + r'/accounts/([\w]+)', ProfileView),
        (api_root + r'/accounts/([\w]+)/tasks', TaskListView),
        (api_root + r'/accounts/([\w]+)/tasks/([\d]+)', TaskView),
    ],
        session_factory=factory,
        cookie_secret=os.environ.get('SESSION_SECRET', 'beefy'),
        **options.group_dict('application'),
    )
    http_server = HTTPServer(app)
    http_server.listen(options.port)
    logging.info('Listening on http://localhost:%d' % options.port)
    IOLoop.current().start()
Ejemplo n.º 37
0
def main():
    if os.path.exists(options.config_file):
        parse_config_file(options.config_file)
    parse_command_line()

    app = Application(
        [
            url(r'/directions/?', directions.DirectionsHandler),
            url(r'/routes/?', routes.RoutesHandler),
            url(r'/routes/({uuid})/?'.format(uuid=UUID4_PATTERN),
                routes.RoutesHandler),
        ],
        **options.group_dict('application')
    )
    app.listen(port=options.port, address=options.host)
    logging.info('Listening on http://%s:%d' % (options.host, options.port))

    IOLoop.current().start()
Ejemplo n.º 38
0
def main():

    parse_command_line(final=False)
    parse_config_file(options.config)
    handlers = [
        URLSpec("/", MainHandler, name="root"),
        URLSpec("/login", LoginHandler, name="login"),
        URLSpec("/logout", LogoutHandler, name="logout")
    ]
    app = Application(
        handlers,
        login_url="/login",
        **options.group_dict("application")
    )
    app.listen(options.port)

    logging.info("Listening on http://localhost:%d" % options.port)
    IOLoop.current().start()
Ejemplo n.º 39
0
 async def connect(self, event_loop=None):
     name = self.name
     opts = options.group_dict('%s redis' % name)
     connection_string = opts[option_name(name, "uri")]
     r = urlparse(connection_string)
     if r.scheme.lower() != 'redis':
         raise RedisConnectorError('%s is not a redis connection scheme' %
                                   connection_string)
     num_connections = opts[option_name(name, "num-connections")]
     LOG.info('connecting redis [%s] %s' % (self.name, connection_string))
     if event_loop is None:
         event_loop = asyncio.get_event_loop()
     self._connections = await aioredis.create_pool(
         connection_string,
         encoding="UTF-8",
         minsize=int(num_connections[0]),
         maxsize=int(num_connections[-1]),
         loop=event_loop)
Ejemplo n.º 40
0
 async def connect(self, event_loop=None):
     name = self.name
     opts = options.group_dict(f'{name} redis')
     connection_string = opts[option_name(name, 'uri')]
     r = urlparse(connection_string)
     if r.scheme.lower() != 'redis':
         raise RedisConnectorError(
             f'{connection_string} is not a redis connection scheme')
     num_connections = opts[option_name(name, 'num-connections')]
     LOG.info(f'connecting redis [{self.name}] {connection_string}')
     if event_loop is None:
         event_loop = asyncio.get_event_loop()
     self._connections = await aioredis.create_pool(
         connection_string,
         encoding='UTF-8',
         minsize=int(num_connections[0]),
         maxsize=int(num_connections[-1]),
         loop=event_loop,
     )
Ejemplo n.º 41
0
def main():
    tornado.options.parse_command_line()

    if options.config:
        config_to_read = options.config
    else:
        config_to_read = './server.conf'

    tornado.options.parse_config_file(config_to_read)

    if options.debug:
        logging.getLogger("arduino.server").setLevel(logging.DEBUG)

    init(options.database_file)

    http_server = tornado.httpserver.HTTPServer(setup_application(options.group_dict('application')))
    http_server.listen(os.getenv('OIKOSENSE_PORT', options.port))

    if os.getenv('OIKOSENSE_ARDUINO_TTY', options.arduino_tty):
        hermes_server = SonersServer(hermes_urls)
        hermes_server.listen(os.getenv('OIKOSENSE_ARDUINO_TTY', options.arduino_tty))

    tornado.ioloop.IOLoop.instance().start()
Ejemplo n.º 42
0
def make_app():
    return Application([
        url(r"/", MainHandler),
        ], **options.group_dict("application"))
Ejemplo n.º 43
0
 def __init__(self, handlers, config='settings.py', **kwargs):
     # trigger parse_config_file
     options.config = config
     settings = options.group_dict('app')
     settings.update(kwargs)
     super(Application, self).__init__(handlers,**settings)
Ejemplo n.º 44
0
""" Observations """
from fieldDaySiteObservationsHandler import FieldDaySiteObservationsHandler

""" Volunteers """
from volunteerHandler import VolunteerListHandler

from baseHandler import BaseAuthenticatedHandler

import importlib

"""
    Determine which storage engine is specified in the user config and
    import the appropriate modules to support that
"""
try:
    persistenceOptions = options.group_dict("persistence")
except KeyError:
    raise
else:
    """ Mongo DB Persistence """

    if "mongo_instance" in persistenceOptions:
        from pymongo import MongoClient
        mongoClient = MongoClient(persistenceOptions["mongo_instance"])
        mongoDb = mongoClient[persistenceOptions["mongo_database"]]
        if "mongo_user" in persistenceOptions and persistenceOptions["mongo_user"]:
            mongoDb.authenticate(persistenceOptions["mongo_user"], persistenceOptions["mongo_password"])
    else:
        noMongoMessage = "Warning! No persistence engine was specified for Field Day List - See localSettings.py.sample for examples"
        print noMongoMessage
        print "Using Dummy persistence instead - That's ok for Demos"