Exemplo n.º 1
0
def run(args):
    """
    Run a set of simple canned queries against the DB
    """
    commands = [
        ("use otter;", False, ""),
        ("SELECT COUNT(*) FROM scaling_config WHERE deleted=false;", True,
         "Number of scaling groups: {0}"),
        ("SELECT COUNT(*) FROM launch_config WHERE deleted=false;", True,
         "Number of launch configs (should be same as above): {0}"),
        ("SELECT COUNT(*) FROM group_state WHERE deleted=false;", True,
         "Number of group states (should be same as above): {0}"),
        ("SELECT COUNT(*) FROM scaling_policies WHERE deleted=false;", True,
         "Number of scaling policies: {0}"),
        ("SELECT COUNT(*) FROM policy_webhooks WHERE deleted=false;", True,
         "Number of webhooks: {0}"),
    ]

    # connect
    if args.verbose > 0:
        print "Attempting to connect to {0}:{1}".format(args.host, args.port)

    connection = connect(args.host, args.port, cql_version='3.0.4')
    cursor = connection.cursor()

    for command, displayResults, label in commands:
        try:
            cursor.execute(command, {})
            if displayResults:
                print label.format(cursor.fetchone()[0])
        except ProgrammingError as pe:
            # if somewhat verbose, then print out all errors.
            # if less verbose, print out only non-already-existing errors
            message = pe.message.lower()
            significant_error = ("already exist" not in message
                                 and "existing keyspace" not in message)

            if args.verbose > 1 or significant_error:
                print '\n----\n'
                print command
                print "{0}".format(pe.message.strip())

            if significant_error:
                sys.exit(1)

        else:
            # extremely verbose - notify that command executed correctly.
            if args.verbose > 2:
                print '\n----\n'
                print command
                print "Ok."

    if args.verbose > 0:
        print '\n----\n'
        print "Done.  Disconnecting."

    cursor.close()
    connection.close()
Exemplo n.º 2
0
def run(args):
    """
    Run a set of simple canned queries against the DB
    """
    commands = [("use otter;", False, ""),
                ("SELECT COUNT(*) FROM scaling_config WHERE deleted=false;", True,
                 "Number of scaling groups: {0}"),
                ("SELECT COUNT(*) FROM launch_config WHERE deleted=false;", True,
                 "Number of launch configs (should be same as above): {0}"),
                ("SELECT COUNT(*) FROM group_state WHERE deleted=false;", True,
                 "Number of group states (should be same as above): {0}"),
                ("SELECT COUNT(*) FROM scaling_policies WHERE deleted=false;", True,
                 "Number of scaling policies: {0}"),
                ("SELECT COUNT(*) FROM policy_webhooks WHERE deleted=false;", True,
                 "Number of webhooks: {0}"),
                ]

    # connect
    if args.verbose > 0:
        print "Attempting to connect to {0}:{1}".format(args.host, args.port)

    connection = connect(args.host, args.port, cql_version='3.0.4')
    cursor = connection.cursor()

    for command, displayResults, label in commands:
        try:
            cursor.execute(command, {})
            if displayResults:
                print label.format(cursor.fetchone()[0])
        except ProgrammingError as pe:
            # if somewhat verbose, then print out all errors.
            # if less verbose, print out only non-already-existing errors
            message = pe.message.lower()
            significant_error = (
                "already exist" not in message and
                "existing keyspace" not in message)

            if args.verbose > 1 or significant_error:
                print '\n----\n'
                print command
                print "{0}".format(pe.message.strip())

            if significant_error:
                sys.exit(1)

        else:
            # extremely verbose - notify that command executed correctly.
            if args.verbose > 2:
                print '\n----\n'
                print command
                print "Ok."

    if args.verbose > 0:
        print '\n----\n'
        print "Done.  Disconnecting."

    cursor.close()
    connection.close()
Exemplo n.º 3
0
 def _get_cursor(self):
     try:
         cursor = self._connection.cursor()
     except ProgrammingError:
         self._connection = connect(self.host, self.port,
                                    cql_version=self.cql_version)
         # if this doesn't take, blow up
         cursor = self._connection.cursor()
     return cursor
Exemplo n.º 4
0
 def _get_cursor(self):
     try:
         cursor = self._connection.cursor()
     except ProgrammingError:
         self._connection = connect(self.host,
                                    self.port,
                                    cql_version=self.cql_version)
         # if this doesn't take, blow up
         cursor = self._connection.cursor()
     return cursor
Exemplo n.º 5
0
    def __init__(self, host="localhost", port=9160, cql_version="3.0.4",
                 setup_cql=None, teardown_cql=None, overwrite_keyspaces=True):
        self.host = host
        self.port = port
        self.cql_version = cql_version
        self.setup_cql = setup_cql or simple_create_keyspace
        self.teardown_cql = teardown_cql or simple_drop_keyspace
        self.overwrite_keyspaces = overwrite_keyspaces

        self._connection = connect(self.host, self.port,
                                   cql_version=self.cql_version)
Exemplo n.º 6
0
 def __create_connection(self):
     return connect(self.hostname,
                       port=self.port,
                       keyspace=self.keyspace,
                       user=self.username,
                       password=self.password,
                       cql_version=self.cql_version,
                       native=self.native,
                       compression=self.compression,
                       consistency_level=self.consistency_level,
                       transport=self.transport,
                       )
Exemplo n.º 7
0
 def __create_connection(self):
     return connect(
         self.hostname,
         port=self.port,
         keyspace=self.keyspace,
         user=self.username,
         password=self.password,
         cql_version=self.cql_version,
         native=self.native,
         compression=self.compression,
         consistency_level=self.consistency_level,
         transport=self.transport,
     )
Exemplo n.º 8
0
    def __init__(self,
                 host="localhost",
                 port=9160,
                 cql_version="3.0.4",
                 setup_cql=None,
                 teardown_cql=None,
                 overwrite_keyspaces=True):
        self.host = host
        self.port = port
        self.cql_version = cql_version
        self.setup_cql = setup_cql or simple_create_keyspace
        self.teardown_cql = teardown_cql or simple_drop_keyspace
        self.overwrite_keyspaces = overwrite_keyspaces

        self._connection = connect(self.host,
                                   self.port,
                                   cql_version=self.cql_version)
Exemplo n.º 9
0
def generate(args):
    """
    Generate CQL and/or load it into a cassandra instance/cluster.
    """
    try:
        generator = CQLGenerator(args.cql_dir, safe_only=args.ban_unsafe)
    except Exception as e:
        print(e.message)
        sys.exit(1)

    cql = generator.generate_cql(keyspace_name=args.keyspace,
                                 replication_factor=args.replication,
                                 outfile=args.outfile)

    if args.dry_run:
        return

    # filter out comments, to make debugging easier
    cql = "\n".join([
        line for line in cql.split('\n')
        if line.strip() and not line.strip().startswith('--')
    ])

    # no blank lines or pointless whitespace
    commands = [x.strip() for x in cql.split(';') if x.strip()]

    # connect
    if args.verbose > 0:
        print("Attempting to connect to {0}:{1}".format(args.host, args.port))
    try:
        connection = connect(args.host, args.port, cql_version='3.0.4')
    except Exception as e:
        print("CONNECTION ERROR: {0}".format(e.message))
        sys.exit(1)
    cursor = connection.cursor()

    # execute commands
    execute_commands(cursor, commands, args.verbose)

    if args.verbose > 0:
        print('\n----\n')
        print("Done.  Disconnecting.")

    cursor.close()
    connection.close()
Exemplo n.º 10
0
def generate(args):
    """
    Generate CQL and/or load it into a cassandra instance/cluster.
    """
    try:
        generator = CQLGenerator(args.cql_dir, safe_only=args.ban_unsafe)
    except Exception as e:
        print(e.message)
        sys.exit(1)

    cql = generator.generate_cql(
        keyspace_name=args.keyspace,
        replication_factor=args.replication,
        outfile=args.outfile)

    if args.dry_run:
        return

    # filter out comments, to make debugging easier
    cql = "\n".join(
        [line for line in cql.split('\n')
         if line.strip() and not line.strip().startswith('--')])

    # no blank lines or pointless whitespace
    commands = [x.strip() for x in cql.split(';') if x.strip()]

    # connect
    if args.verbose > 0:
        print("Attempting to connect to {0}:{1}".format(args.host, args.port))
    try:
        connection = connect(args.host, args.port, cql_version='3.0.4')
    except Exception as e:
        print("CONNECTION ERROR: {0}".format(e.message))
        sys.exit(1)
    cursor = connection.cursor()

    # execute commands
    execute_commands(cursor, commands, args.verbose)

    if args.verbose > 0:
        print('\n----\n')
        print("Done.  Disconnecting.")

    cursor.close()
    connection.close()
Exemplo n.º 11
0
def run(args):
    """
    Generate CQL and/or load it into a cassandra instance/cluster.
    """
    try:
        generator = CQLGenerator(args.cql_dir, safe_only=args.ban_unsafe)
    except Exception as e:
        print e.message
        sys.exit(1)

    cql = generator.generate_cql(
        keyspace_name=args.keyspace,
        replication_factor=args.replication,
        outfile=args.outfile)

    if args.dry_run:
        return

    # filter out comments, to make debugging easier
    cql = "\n".join(
        [line for line in cql.split('\n')
         if line.strip() and not line.strip().startswith('--')])

    # no blank lines or pointless whitespace
    commands = [x.strip() for x in cql.split(';') if x.strip()]

    # connect
    if args.verbose > 0:
        print "Attempting to connect to {0}:{1}".format(args.host, args.port)
    try:
        connection = connect(args.host, args.port, cql_version='3')
    except Exception as e:
        print "CONNECTION ERROR: {0}".format(e.message)
        sys.exit(1)

    cursor = connection.cursor()

    for command in commands:
        try:
            cursor.execute(command, {})
        except ProgrammingError as pe:
            # if somewhat verbose, then print out all errors.
            # if less verbose, print out only non-already-existing errors
            message = pe.message.lower()
            significant_error = (
                "already exist" not in message and
                "existing keyspace" not in message)

            if args.verbose > 1 or significant_error:
                print '\n----\n'
                print command
                print "{0}".format(pe.message.strip())

            if significant_error:
                sys.exit(1)

        else:
            # extremely verbose - notify that command executed correctly.
            if args.verbose > 2:
                print '\n----\n'
                print command
                print "Ok."

    if args.verbose > 0:
        print '\n----\n'
        print "Done.  Disconnecting."

    cursor.close()
    connection.close()
Exemplo n.º 12
0
def run(args):
    """
    Generate CQL and/or load it into a cassandra instance/cluster.
    """
    try:
        generator = CQLGenerator(args.cql_dir, safe_only=args.ban_unsafe)
    except Exception as e:
        print e.message
        sys.exit(1)

    cql = generator.generate_cql(keyspace_name=args.keyspace,
                                 replication_factor=args.replication,
                                 outfile=args.outfile)

    if args.dry_run:
        return

    # filter out comments, to make debugging easier
    cql = "\n".join([
        line for line in cql.split('\n')
        if line.strip() and not line.strip().startswith('--')
    ])

    # no blank lines or pointless whitespace
    commands = [x.strip() for x in cql.split(';') if x.strip()]

    # connect
    if args.verbose > 0:
        print "Attempting to connect to {0}:{1}".format(args.host, args.port)
    try:
        connection = connect(args.host, args.port, cql_version='3')
    except Exception as e:
        print "CONNECTION ERROR: {0}".format(e.message)
        sys.exit(1)

    cursor = connection.cursor()

    for command in commands:
        try:
            cursor.execute(command, {})
        except ProgrammingError as pe:
            # if somewhat verbose, then print out all errors.
            # if less verbose, print out only non-already-existing errors
            message = pe.message.lower()
            significant_error = ("already exist" not in message
                                 and "existing keyspace" not in message)

            if args.verbose > 1 or significant_error:
                print '\n----\n'
                print command
                print "{0}".format(pe.message.strip())

            if significant_error:
                sys.exit(1)

        else:
            # extremely verbose - notify that command executed correctly.
            if args.verbose > 2:
                print '\n----\n'
                print command
                print "Ok."

    if args.verbose > 0:
        print '\n----\n'
        print "Done.  Disconnecting."

    cursor.close()
    connection.close()
Exemplo n.º 13
0
import json
import sys
from cql import connection

config = json.loads("".join(open('config.json').readlines()))['cassandra']
conn = connection.connect(config['host'], int(config['port']), 'nlp')
cursor = conn.cursor()

"""
Add a function here to migrate
"""
roll_forward = [ \
    lambda cursor: map(lambda query: cursor.execute(query), \
          ["""
           CREATE TABLE service_responses (
             signature varchar PRIMARY KEY,
             doc_id varchar,
             service varchar,
             wiki_id int,
             response text,
             last_updated int
           );""",
           "CREATE INDEX ON service_responses (wiki_id);",
           "CREATE INDEX ON service_responses (service);",
           "CREATE INDEX ON service_responses (doc_id);",
           ]
                       )
]

"""
In the same order as roll_forward -- not used for now