Beispiel #1
0
def main():
    try:
        tornado.options.parse_command_line()
        application = tornado.web.Application([
            (r'/', OverviewHandler),
            (r'/query', SingleQueryHandler),
            (r'/batch', BatchQueryHandler),
            (r'/chain', QueryChainHandler),
            (r'/multi_query', MultiQueryHandler),
            (r'/callback_and_wait', CallbackWaitHandler),
        ],
                                              debug=True)

        application.db = momoko.AsyncClient({
            'host': "localhost",
            'port': 5432,
            'database': "stock",
            'user': "******",
            'password': "******",
            'min_conn': 1,
            'max_conn': 10,
            'cleanup_timeout': 10
        })

        http_server = tornado.httpserver.HTTPServer(application)
        http_server.listen(8888)
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        print('Exit')
Beispiel #2
0
def main():
    try:
        tornado.options.parse_command_line()
        application = tornado.web.Application([
            (r'/', OverviewHandler),
            (r'/query', SingleQueryHandler),
            (r'/batch', BatchQueryHandler),
            (r'/chain', QueryChainHandler),
        ], debug=True)

        application.db = momoko.AsyncClient({
            'host': settings.host,
            'port': settings.port,
            'database': settings.database,
            'user': settings.user,
            'password': settings.password,
            'min_conn': settings.min_conn,
            'max_conn': settings.max_conn,
            'cleanup_timeout': settings.cleanup_timeout
        })

        http_server = tornado.httpserver.HTTPServer(application)
        http_server.listen(8888)
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        print('Exit')
Beispiel #3
0
    def __init__(self):
        self.db = momoko.AsyncClient({
            'host':
            options.database_host,
            'port':
            options.database_port,
            'database':
            options.database_name,
            'user':
            options.database_username,
            'password':
            options.database_password,
            'min_conn':
            options.database_min_conn,
            'max_conn':
            options.database_max_conn,
            'cleanup_timeout':
            options.database_cleanup_timeout
        })

        # Start a connection to the redis to the database ID that stores
        # cached versions of database read queries. Delete all of them.
        self.r = redis.StrictRedis(
            host=options.redis_hostname,
            port=options.redis_port,
            db=options.redis_database_id_for_database_results)
        self.r.flushdb()
Beispiel #4
0
 def _get_database(self):
     return momoko.AsyncClient({
         'host': self.options['hostname'],
         'port': self.options['port'],
         'user': self.options['username'],
         'password': self.options['password'],
         'ioloop': self.configuration.io_loop
     })
Beispiel #5
0
def pg_async():
    return momoko.AsyncClient({
            'host': pg_host,
            'database': pg_db,
            'port': pg_port,
            'user': pg_user,
            'password': pg_pass,
            'min_conn': 1,
            'max_conn': 20,
            'cleanup_timeout': 5
        })
Beispiel #6
0
 def setUp(self):
     super(AsyncClientTest, self).setUp()
     self.db = momoko.AsyncClient({
         'host': settings.host,
         'port': settings.port,
         'database': settings.database,
         'user': settings.user,
         'password': settings.password,
         'min_conn': settings.min_conn,
         'max_conn': settings.max_conn,
         'cleanup_timeout': settings.cleanup_timeout,
         'ioloop': self.io_loop
     })
Beispiel #7
0
 def db(self):
     # Create a database connection when a request handler is called
     # and store the connection in the application object.
     if not hasattr(self.application, 'db'):
         self.application.db = momoko.AsyncClient({
             'host': 'queue1',
             'database': 'sgedb',
             'user': '******',
             'password': '',
             'min_conn': 1,
             'max_conn': 20,
             'cleanup_timeout': 10
         })
     return self.application.db
Beispiel #8
0
 def db(self):
     # Create a database connection when a request handler is called
     # and store the connection in the application object.
     if not hasattr(self.application, 'db'):
         self.application.db = momoko.AsyncClient({
             'host': settings.host,
             'port': settings.port,
             'database': settings.database,
             'user': settings.user,
             'password': settings.password,
             'min_conn': settings.min_conn,
             'max_conn': settings.max_conn,
             'cleanup_timeout': settings.cleanup_timeout
         })
     return self.application.db
Beispiel #9
0
class PostgresServiceTest(BaseServiceTest):
    def _postgres_binary_path(self):
        if platform.system() == 'Darwin':
            # Postgres binaries are in the path on OSX.
            return ""
        else:
            # TODO: This is Ubuntu specific.
            return "/usr/lib/postgresql/9.1/bin"

    def setUp(self):
        super(PostgresServiceTest, self).setUp()

        self.server = paasmaker.util.postgresdaemon.PostgresDaemon(
            self.configuration)
        self.server.configure(
            self.configuration.get_scratch_path_exists('postgres'),
            self._postgres_binary_path(),
            self.configuration.get_free_port(),
            '127.0.0.1',
            self.stop,
            self.stop,
            password="******")
        self.wait()
        self.server.start(self.stop, self.stop)
        result = self.wait()

        # Give the database a short time to settle down.
        self.short_wait_hack(length=0.5)
        #delay = raw_input("Press enter to continue.")

        self.registry.register(
            'paasmaker.service.postgres',
            'paasmaker.pacemaker.service.postgres.PostgresService', {
                'hostname': '127.0.0.1',
                'port': self.server.get_port(),
                'username': '******',
                'password': '******',
                'binary_path': self._postgres_binary_path()
            }, 'Postgres Service')

    def tearDown(self):
        if hasattr(self, 'server'):
            self.server.destroy(self.stop, self.stop)
            self.wait(timeout=10)

        super(PostgresServiceTest, self).tearDown()

    def test_simple(self):
        logger = self.configuration.get_job_logger('testservice')
        service = self.registry.instantiate(
            'paasmaker.service.postgres',
            paasmaker.util.plugin.MODE.SERVICE_CREATE, {},
            logger=logger)

        service.create('test', self.success_callback, self.failure_callback)

        try:
            self.wait()
        except psycopg2.OperationalError, ex:
            # Ignore this exception - it's an async handler
            # trying to reconnect to the now-stopped Postgres server.
            pass

        #print str(self.credentials)
        self.assertTrue(self.success, "Service creation was not successful.")
        self.assertEquals(len(self.credentials), 6,
                          "Service did not return expected number of keys.")

        #delay = raw_input("Press enter to proceed.")

        # Connect to the database and execute a query.
        db = momoko.AsyncClient({
            'host': self.credentials['hostname'],
            'port': self.credentials['port'],
            'user': self.credentials['username'],
            'password': self.credentials['password'],
            'ioloop': self.configuration.io_loop
        })

        db.execute("CREATE TABLE foo (id integer)", callback=self.stop)
        result = self.wait()
        db.execute("INSERT INTO foo VALUES (1)", callback=self.stop)
        result = self.wait()
        db.execute("SELECT id FROM foo", callback=self.stop)
        result = self.wait()

        for row in result:
            self.assertEquals(row[0], 1, "Result was not as expected.")

        db.close()

        credentials_copy = self.credentials

        # Export the database.
        service.export('testlongname', credentials_copy,
                       self.success_remove_callback, self.failure_callback,
                       self.sink_export)
        self.wait()

        self.assertTrue(self.success, "Did not succeed to export.")
        self.assertIn('CREATE TABLE foo', self.export_data,
                      "Export data missing table data.")

        # Now attempt to delete the service.
        service.remove('test', credentials_copy, self.success_remove_callback,
                       self.failure_callback)
        self.wait()

        self.assertTrue(self.success, "Did not succeed.")

        self.credentials = credentials_copy

        # Try to export the DB again.
        service.export('testlongname', credentials_copy,
                       self.success_remove_callback, self.failure_callback,
                       self.sink_export)
        self.wait()

        self.assertFalse(self.success, "Succeeded when it should not have.")
        self.assertIn("FATAL", self.message, "Wrong error message.")

        self.credentials = credentials_copy

        # Create a new database and import the backup into it.
        service.create('test2', self.success_callback, self.failure_callback)
        self.wait()

        import_credentials = self.credentials

        self.assertTrue(self.success, "Did not create new database.")

        tempimport = tempfile.mkstemp()[1]
        open(tempimport, 'w').write(self.export_data)

        service.import_file('test2', import_credentials, tempimport,
                            self.success_remove_callback,
                            self.failure_callback)
        self.wait()

        self.assertTrue(self.success, "Did not successfully import.")

        # Connect to the database and execute a query.
        db = momoko.AsyncClient({
            'host': import_credentials['hostname'],
            'port': import_credentials['port'],
            'user': import_credentials['username'],
            'password': import_credentials['password'],
            'ioloop': self.configuration.io_loop
        })

        db.execute("SELECT id FROM foo", callback=self.stop)
        result = self.wait()

        for row in result:
            self.assertEquals(row[0], 1, "Result was not as expected.")

        db.close()
Beispiel #10
0
	def test_simple(self):
		# Override the plugin it registered.
		self.registry.register(
			'paasmaker.service.postgres',
			'paasmaker.pacemaker.service.managedpostgres.ManagedPostgresService',
			{
				'port': self.configuration.get_free_port(),
				'root_password': '******',
				'shutdown': True
			},
			'Postgres Service'
		)

		# Try starting the service.
		service = self.registry.instantiate(
			'paasmaker.service.postgres',
			paasmaker.util.plugin.MODE.SERVICE_CREATE,
			{}
		)

		service.create('test', self.success_callback, self.failure_callback)
		self.wait()

		self.assertTrue(self.success, "Creating managed postgres service didn't succeed.")

		# Some basic operations copied out of the parent test.
		db = momoko.AsyncClient(
			{
				'host': self.credentials['hostname'],
				'port': self.credentials['port'],
				'user': self.credentials['username'],
				'password': self.credentials['password'],
				'ioloop': self.configuration.io_loop
			}
		)

		db.execute("CREATE TABLE foo (id integer)", callback=self.stop)
		result = self.wait()
		db.execute("INSERT INTO foo VALUES (1)", callback=self.stop)
		result = self.wait()
		db.execute("SELECT id FROM foo", callback=self.stop)
		result = self.wait()

		for row in result:
			self.assertEquals(row[0], 1, "Result was not as expected.")

		db.close()

		# Now, shut down the instances.
		service.shutdown_postnotify(self.stop, self.stop)
		self.wait()
		self.short_wait_hack(length=1.0)

		# The port should now be free.
		self.assertFalse(self.configuration.port_allocator.in_use(service.options['port']), "Port was not free.")

		# Now start them back up again.
		service.startup_async_prelisten(self.stop, self.stop)
		self.wait()
		self.short_wait_hack(length=1.0)

		# The port should now be used.
		self.assertTrue(self.configuration.port_allocator.in_use(service.options['port']), "Port was not in use.")

		# Finally, instantiate the plugin in delete mode and delete the service.
		service = self.registry.instantiate(
			'paasmaker.service.postgres',
			paasmaker.util.plugin.MODE.SERVICE_DELETE,
			{}
		)

		service.remove('test', self.credentials, self.success_remove_callback, self.failure_callback)
		try:
			self.wait()
		except psycopg2.OperationalError, ex:
			# Ignore this exception - it's an async handler
			# trying to reconnect to the now-stopped Postgres server.
			pass