Beispiel #1
0
def get_cluster(cluster_ips_file: str = 'ips.txt') -> Cluster:
    auth = PlainTextAuthProvider(username=os.environ['DSE_USER'],
                                 password=os.environ['DSE_PASS'])
    with open(cluster_ips_file) as ips_file:
        ips = [ip.strip() for ip in ips_file.read().split(",")]
    logger.info(f'Read cluster nodes IPs from {cluster_ips_file}: {ips}')
    return Cluster(contact_points=ips, auth_provider=auth)
 def __init__(self):
     self.secure_connect_bundle = '/home/ubuntu/workspace/creds.zip'
     self.path_to_creds = ''
     self.cluster = Cluster(
         cloud={'secure_connect_bundle': self.secure_connect_bundle},
         auth_provider=PlainTextAuthProvider('KVUser', 'KVPassword'))
     self.session = self.cluster.connect()
 def get_authentication_provider(self, username, password):
     """
     Return correct authentication provider
     :param username: authentication username
     :param password: authentication password
     :return: authentication object suitable for Cluster.connect()
     """
     return PlainTextAuthProvider(username=username, password=password)
def setup_cluster():
    if args.host is None:
        nodes = ['localhost']
    else:
        nodes = [args.host]

    if args.port is None:
        port = 9042
    else:
        port = int(args.port)

    if args.connect_timeout is None:
        connect_timeout = 5
    else:
        connect_timeout = int(args.connect_timeout)

    if args.ssl is not None and args.certfile is not None:
        ssl_opts = {
            'ca_certs': args.certfile,
            'ssl_version': PROTOCOL_TLSv1,
            'keyfile': args.userkey,
            'certfile': args.usercert
        }
    else:
        ssl_opts = {}

    cluster = None

    if args.protocol_version is not None:
        auth = None

        if args.username is not None and args.password is not None:
            if args.protocol_version == 1:
                auth = get_credentials
            elif args.protocol_version > 1:
                auth = PlainTextAuthProvider(username=args.username,
                                             password=args.password)

        cluster = Cluster(control_connection_timeout=connect_timeout,
                          connect_timeout=connect_timeout,
                          contact_points=nodes,
                          port=port,
                          protocol_version=args.protocol_version,
                          auth_provider=auth,
                          ssl_options=ssl_opts)
    else:
        cluster = Cluster(control_connection_timeout=connect_timeout,
                          connect_timeout=connect_timeout,
                          contact_points=nodes,
                          port=port,
                          ssl_options=ssl_opts)

    session = cluster.connect()

    session.default_timeout = TIMEOUT
    session.default_fetch_size = FETCH_SIZE
    session.row_factory = dse.query.ordered_dict_factory
    return session
Beispiel #5
0
def connect_to_db():
    global session
    global get_flight_prepared

    if session is None:
        contact_points = get_ips('ips.txt')
        auth_provider = PlainTextAuthProvider(username='******',
                                              password='******')
        cluster = Cluster(auth_provider=auth_provider,
                          contact_points=contact_points)
        try:
            session = cluster.connect('competition')
            print('Connected to Cassandra cluster.')
            get_flight_prepared = session.prepare(
                f'SELECT flight_id, station_id, name, group, org_college, major, ts, latest_ts, valid '
                f'FROM positional WHERE flight_id = ? ORDER BY ts LIMIT 1;')
        except:
            print('Cannot connect to Cassandra cluster. Exiting ...')
            exit(1)
Beispiel #6
0
def main():
    module = AnsibleModule(argument_spec={
        'keyspace_name': {
            'required': True,
            'type': 'str'
        },
        'replication_class': {
            'required': True,
            'type': 'str'
        },
        'replication_dc': {
            'required': True,
            'type': 'dict'
        },
        'is_ssl': {
            'required': True,
            'type': 'bool'
        },
        'cert_path': {
            'required': False,
            'type': 'str'
        },
        'login_user': {
            'required': True,
            'type': 'str'
        },
        'login_password': {
            'required': True,
            'no_log': True,
            'type': 'str'
        },
        'login_hosts': {
            'required': True,
            'type': 'list'
        },
        'login_port': {
            'default': 9042,
            'type': 'int'
        }
    },
                           supports_check_mode=True)

    keyspace_name = module.params["keyspace_name"]
    replication_class = module.params["replication_class"]
    replication_dc = module.params["replication_dc"]
    is_ssl = module.params["is_ssl"]
    cert_path = module.params["cert_path"]
    login_user = module.params["login_user"]
    login_password = module.params["login_password"]
    login_hosts = module.params["login_hosts"]
    login_port = module.params["login_port"]

    if not cassandra_dep_found:
        module.fail_json(msg="the python cassandra-driver module is required")

    session = None
    changed = True
    ssl_options = dict(certfile=cert_path, ssl_version=ssl.PROTOCOL_TLSv1)

    try:
        if not login_user:
            cluster = Cluster(login_hosts, port=login_port)

        else:
            auth_provider = PlainTextAuthProvider(username=login_user,
                                                  password=login_password)

            if is_ssl:
                cluster = Cluster(login_hosts,
                                  auth_provider=auth_provider,
                                  protocol_version=3.3,
                                  port=login_port,
                                  ssl_options=ssl_options)
            else:
                cluster = Cluster(login_hosts,
                                  auth_provider=auth_provider,
                                  protocol_version=3.3,
                                  port=login_port)

            session = cluster.connect()
            session.row_factory = dict_factory
    except Exception, e:
        module.fail_json(
            msg=
            "unable to connect to cassandra, check login_user and login_password are correct. Exception message: %s"
            % e)
Beispiel #7
0
def main():
    module = AnsibleModule(argument_spec={
        'login_user': {
            'required': True,
            'type': 'str'
        },
        'login_password': {
            'required': True,
            'no_log': True,
            'type': 'str'
        },
        'login_hosts': {
            'required': True,
            'type': 'list'
        },
        'login_port': {
            'default': 9042,
            'type': 'int'
        },
        'is_ssl': {
            'required': True,
            'type': 'bool'
        },
        'cert_path': {
            'required': False,
            'type': 'str'
        },
        'name': {
            'required': True,
            'aliases': ['role']
        },
        'password': {
            'default': None,
            'no_log': True
        },
        'enable_login': {
            'default': False,
            'type': 'bool'
        },
        'superuser': {
            'default': False,
            'type': 'bool'
        },
        'state': {
            'default': "present",
            'choices': ["absent", "present"]
        }
    },
                           supports_check_mode=True)

    is_ssl = module.params["is_ssl"]
    cert_path = module.params["cert_path"]
    login_user = module.params["login_user"]
    login_password = module.params["login_password"]
    login_hosts = module.params["login_hosts"]
    login_port = module.params["login_port"]
    enable_login = module.params["enable_login"]
    name = module.params["name"]
    password = module.params["password"]
    superuser = module.params["superuser"]
    state = module.params["state"]

    if not cassandra_dep_found:
        module.fail_json(msg="the python cassandra-driver module is required")

    session = None
    changed = False
    ssl_options = dict(certfile=cert_path, ssl_version=ssl.PROTOCOL_TLSv1)

    try:
        if not login_user:
            cluster = Cluster(login_hosts, port=login_port)

        else:
            auth_provider = PlainTextAuthProvider(username=login_user,
                                                  password=login_password)

            if is_ssl:
                cluster = Cluster(login_hosts,
                                  auth_provider=auth_provider,
                                  protocol_version=3.3,
                                  port=login_port,
                                  ssl_options=ssl_options)
            else:
                cluster = Cluster(login_hosts,
                                  auth_provider=auth_provider,
                                  protocol_version=3.3,
                                  port=login_port)

            session = cluster.connect()
            session.row_factory = dict_factory
    except Exception, e:
        module.fail_json(
            msg=
            "unable to connect to cassandra, check login_user and login_password are correct. Exception message: %s"
            % e)
Beispiel #8
0
from dse.cluster import Cluster
from dse.auth import PlainTextAuthProvider

auth_provider = PlainTextAuthProvider(username='******',
                                      password='******')
cluster = Cluster(auth_provider=auth_provider, protocol_version=3)

print("Port: ", cluster.port)
print("Username: "******"Password: "******"Connection succeeded!")
except:
    print("Connection failed!")
Beispiel #9
0
import sys
from random import randint
import time

from dse.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT
from dse.auth import PlainTextAuthProvider
from dse.policies import DCAwareRoundRobinPolicy, TokenAwarePolicy, ConstantSpeculativeExecutionPolicy
from dse import ConsistencyLevel
from ssl import PROTOCOL_TLSv1, CERT_REQUIRED, CERT_OPTIONAL

#Configuration
contactpoints = ['13.68.201.227', '52.91.211.124']
localDC = "multicloud-aws"
CL = ConsistencyLevel.ONE
#CL = ConsistencyLevel.ALL
auth_provider = PlainTextAuthProvider(username='******', password='******')
profile1 = ExecutionProfile(
    load_balancing_policy=DCAwareRoundRobinPolicy(local_dc=localDC,
                                                  used_hosts_per_remote_dc=3),
    speculative_execution_policy=ConstantSpeculativeExecutionPolicy(.05, 20),
    consistency_level=CL)

ssl_opts = None
#ssl_opts = {
#    'ca_certs': '/path/to/my/ca.certs',
#    'ssl_version': PROTOCOL_TLSv1,
#    'cert_reqs':  CERT_OPTIONAL
#}

print "Connecting to cluster"
Beispiel #10
0
def serve():

    dse_username = os.getenv('KILLRVIDEO_DSE_USERNAME')
    dse_password = os.getenv('KILLRVIDEO_DSE_PASSWORD')
    dse_contact_points = os.getenv('KILLRVIDEO_DSE_CONTACT_POINTS', 'dse').split(',')
    service_port = os.getenv('KILLRVIDEO_SERVICE_PORT', '50101')

    file = open('config.json', 'r')
    config = json.load(file)

    default_consistency_level = config['DEFAULT_CONSISTENCY_LEVEL']

    # Initialize Cassandra Driver and Mapper
    load_balancing_policy = TokenAwarePolicy(DCAwareRoundRobinPolicy())
    profile = ExecutionProfile(consistency_level=ConsistencyLevel.name_to_value[default_consistency_level],
                               load_balancing_policy=load_balancing_policy)
    graph_profile = DseGraph.create_execution_profile('killrvideo_video_recommendations')

    auth_provider = None
    if dse_username:
        auth_provider = PlainTextAuthProvider(username=dse_username, password=dse_password)

    # Wait for Cassandra (DSE) to be up
    session = None
    while not session:
        try:
            session = Cluster(contact_points=dse_contact_points,
                              execution_profiles={EXEC_PROFILE_DEFAULT: profile, EXEC_PROFILE_GRAPH_DEFAULT: graph_profile},
                              auth_provider = auth_provider).connect("killrvideo")
        except (NoHostAvailable):
            logging.info('Waiting for Cassandra (DSE) to be available')
            time.sleep(10)

    # Additional retry loop to check if dummy keyspace exists
    while True:
        logging.info('Checking for schema to be created...')
        result = session.execute('SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name=\'kv_init_done\'')
        if result.one(): # any result indicates keyspace has been created
            break
        time.sleep(10)

    dse.cqlengine.connection.set_session(session)

    # Initialize GRPC Server
    grpc_server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    # Initialize Services (GRPC servicers with reference to GRPC Server and appropriate service reference
    CommentsServiceServicer(grpc_server, CommentsService(session=session))
    RatingsServiceServicer(grpc_server, RatingsService())
    SearchServiceServicer(grpc_server, SearchService(session=session))
    StatisticsServiceServicer(grpc_server, StatisticsService())
    SuggestedVideosServiceServicer(grpc_server, SuggestedVideosService(session=session))
    #UploadsServiceServicer(grpc_server, UploadsService())
    UserManagementServiceServicer(grpc_server, UserManagementService())
    VideoCatalogServiceServicer(grpc_server, VideoCatalogService(session=session))

    # Start GRPC Server
    grpc_server.add_insecure_port('[::]:' + service_port)
    grpc_server.start()

    # Keep application alive
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        grpc_server.stop(0)
Beispiel #11
0
config = ConfigParser()
config.read('demo.ini')

#Configuration

contactpoints = config.get('CONFIG', 'contactpoints').split(',')
localDC = config.get('KHAOS', 'localDC')
lcm = config.get('KHAOS', 'lcm')
lcmport = config.get('KHAOS', 'lcmport')
clustername = config.get('CONFIG', 'clustername').replace(' ', '%20')
username = config.get('KHAOS', 'sshusername')
keyfile = config.get('KHAOS', 'sshkeyfile')
rowcount = config.getint('CONFIG', 'rowcount')
ks_query = config.get('CONFIG', 'ks_query')
auth_provider = PlainTextAuthProvider(
    username=config.get('CONFIG', 'clusteruser'),
    password=config.get('CONFIG', 'clusterpass'))

if config.getint('CONFIG', 'sslenabled') == 0:
    ssl_opts = None
else:
    ssl_opts = {
        'ca_certs': config.get('CONFIG', 'sslca'),
        'ssl_version': PROTOCOL_TLSv1,
        'cert_reqs': CERT_OPTIONAL
    }

#End Configuration
profile1 = ExecutionProfile(
    load_balancing_policy=DCAwareRoundRobinPolicy(local_dc=localDC,
                                                  used_hosts_per_remote_dc=3),
Beispiel #12
0
def main():
    module = AnsibleModule(argument_spec={
        'login_user': {
            'required': True,
            'type': 'str'
        },
        'login_password': {
            'required': True,
            'no_log': True,
            'type': 'str'
        },
        'login_hosts': {
            'required': True,
            'type': 'list'
        },
        'login_port': {
            'default': 9042,
            'type': 'int'
        },
        'permission': {
            'required':
            False,
            'choices': [
                "all", "create", "alter", "drop", "select", "modify",
                "authorize"
            ]
        },
        'role': {
            'required': True,
            'aliases': ['name']
        },
        'inherit_role': {
            'required': False,
            'default': None
        },
        'keyspace': {
            'required': False,
            'default': None,
            'type': 'str'
        },
        'all_keyspaces': {
            'default': False,
            'type': 'bool'
        },
        'mode': {
            'default': "grant",
            'choices': ["grant", "revoke"]
        }
    },
                           supports_check_mode=True)
    login_user = module.params["login_user"]
    login_password = module.params["login_password"]
    login_hosts = module.params["login_hosts"]
    login_port = module.params["login_port"]
    permission = module.params["permission"]
    role = module.params["role"]
    inherit_role = module.params["inherit_role"]
    keyspace = module.params["keyspace"]
    all_keyspaces = module.params["all_keyspaces"]
    mode = module.params["mode"]

    if not cassandra_dep_found:
        module.fail_json(msg="the python cassandra-driver module is required")

    session = None
    changed = False
    try:
        if not login_user:
            cluster = Cluster(login_hosts, port=login_port)
        else:
            auth_provider = PlainTextAuthProvider(username=login_user,
                                                  password=login_password)
            cluster = Cluster(login_hosts,
                              auth_provider=auth_provider,
                              protocol_version=2,
                              port=login_port)
        session = cluster.connect()
        session.row_factory = dict_factory
    except Exception, e:
        module.fail_json(
            msg=
            "unable to connect to cassandra, check login_user and login_password are correct. Exception message: %s"
            % e)
Beispiel #13
0
 def __init__(self, user, password, ips):
     auth_provider = PlainTextAuthProvider(user, password)
     cluster = Cluster(ips, auth_provider=auth_provider)
     self.session = cluster.connect(keyspace="bookstore")
Beispiel #14
0
#!/usr/bin/python3

import discord
import asyncio
import re

from dse.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT
from dse.auth import PlainTextAuthProvider

#Configuration
contactpoints = ['6.7.11.9']
auth_provider = PlainTextAuthProvider(username='******',
                                      password='******')

cluster = Cluster(contact_points=contactpoints, auth_provider=auth_provider)

session = cluster.connect()

client = discord.Client()


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')


@client.event
async def on_message(message):
Beispiel #15
0
                   keyfile='/etc/ssl/SelfSignRootCA/SelfSignRootCA.key',
                   ssl_version=ssl.PROTOCOL_TLSv1)

is_ssl = True
is_login_required = True
login_user = '******'
login_password = '******'
login_hosts = '10.200.176.188'
login_port = '9042'

try:
    if not is_login_required:
        cluster = Cluster([login_hosts], port=login_port)

    else:
        auth_provider = PlainTextAuthProvider(username=login_user,
                                              password=login_password)

        if is_ssl:
            cluster = Cluster([login_hosts],
                              auth_provider=auth_provider,
                              protocol_version=3.3,
                              port=login_port,
                              ssl_options=ssl_options)
        else:
            cluster = Cluster([login_hosts],
                              auth_provider=auth_provider,
                              protocol_version=3.3,
                              port=login_port)

    session = cluster.connect()
    session.row_factory = dict_factory
from dse.cluster import Cluster
from dse.auth import PlainTextAuthProvider
import sys

print(sys.version)

cloud_config = {
    'secure_connect_bundle':
    '/Users/matthew.miller/cassandra/secure-connect-mattsdb2.zip'
}
cluster = Cluster(cloud=cloud_config,
                  auth_provider=PlainTextAuthProvider('mmiller', 'cassandra'))
session = cluster.connect()

row = session.execute("select release_version from system.local").one()
if row:
    print(row[0])
else:
    print("An error occurred.")
 def __init__(self, user, password, ips, keyspace):
     self._auth_provider = PlainTextAuthProvider(user, password)
     self._cluster = Cluster(ips, auth_provider=self._auth_provider)
     self.session = self._cluster.connect(keyspace=keyspace)