def _createConn(self): try: cluster = Cluster(self.connection_string, bucket_class=CouchbaseBucket) cluster.authenticate(PasswordAuthenticator(self.bucket, 'password')) self.cb = cluster.open_bucket(self.bucket) except BucketNotFoundError: raise
def create_clients(cb_cluster_ip, cb_username, cb_password): """ Creates couchbase server bucket clients and store it to CLIENT dict Returns boolean to specify whether the creation of clients succeeded """ client_creation_success = True try: cb_cluster = Cluster("couchbase://{0}". format(cb_cluster_ip)) cb_cluster.authenticate(PasswordAuthenticator(cb_username, cb_password)) except Exception as cluster_err: print("Error while connecting to couchbase cluster couchbase://{0} {1}".format(cb_cluster_ip, cluster_err)) client_creation_success = False return client_creation_success # Initialize the list of buckets to which client will be created buckets_to_create_client = [qeTestSuitesBucketName] for tem_view in VIEWS: buckets_to_create_client.append(tem_view["bucket"]) for bucket in buckets_to_create_client: try: client = cb_cluster.open_bucket(bucket, lockmode=LOCKMODE_WAIT) CLIENTS[bucket] = client except Exception as cb_bucket_error: print("Failed to open bucket {0} {1}".format(bucket, cb_bucket_error)) client_creation_success = False break return client_creation_success
def __init__(self): # Constructor _cluster = Cluster(app.config['DB_ADDRESS']) _auth = PasswordAuthenticator(app.config['DB_USER'], app.config['DB_PASSWORD']) _cluster.authenticate(_auth) self.db = _cluster.open_bucket(app.config['DB_BUCKET'])
def __init__(self, bucket_name, host, username, password, creds=None): self._host = host self.username = username self._userpass = password # couch_bucket_url = 'couchbase://{0}/{1}'.format(host, bucket_name) cluster = Cluster('couchbase://{0}'.format(host)) authenticator = PasswordAuthenticator(username, password) cluster.authenticate(authenticator) # print(bucket_name) self._cb = cluster.open_bucket(bucket_name) if self.bucket_name is None: self.bucket_name = bucket_name self._cb.n1ql_timeout = 5000 * 1000 self._cb.timeout = 5000 * 1000 self._cb.config_total_timeout = 5000 * 1000 # self._cb.cross_bucket = True self._cb.operationTimeout = 5000 * 1000 if creds is not None: self._creds = creds
class CBDoc: def __init__(self): config = read_config() self.cb_server = config.get("couchbase", "couchbase.server") self.cb_bucket = config.get("couchbase", "couchbase.bucket") self.cb_username = config.get("couchbase", "couchbase.username") self.cb_userpassword = config.get("couchbase", "couchbase.userpassword") try: self.cb_cluster = Cluster('couchbase://' + self.cb_server) self.cb_auth = PasswordAuthenticator(self.cb_username, self.cb_userpassword) self.cb_cluster.authenticate(self.cb_auth) self.cb = self.cb_cluster.open_bucket(self.cb_bucket) except Exception as e: log.error('Connection Failed: %s ' % self.cb_server) log.error(e) def get_doc(self, doc_key): try: return self.cb.get(doc_key) except Exception as e: log.error('Error while getting doc %s !' % doc_key) log.error(e) def save_dynvm_doc(self, doc_key, doc_value): try: log.info(doc_value) self.cb.upsert(doc_key, doc_value) log.info("%s added/updated successfully" % doc_key) except Exception as e: log.error('Document with key: %s saving error' % doc_key) log.error(e)
def get_bucket(bucket_id): cluster = Cluster('couchbase://10.101.3.79:8091') authenticator = PasswordAuthenticator('buckets', 'buckets') cluster.authenticate(authenticator) cb = cluster.open_bucket(bucket_id) return cb
class Couchbase(DependencyProvider): def __init__(self, bucket): self.cluster = None self.authenticator = None self.bucket = bucket def setup(self): config = self.container.config[COUCHBASE_KEY] uri = urlparse(config['URI']) params = urlencode(config.get('CLIENT_CONFIG'), {}) self.authenticator = PasswordAuthenticator(uri.username, uri.password) self.cluster = Cluster('{}://{}?{}'.format(uri.scheme, uri.hostname, params)) def start(self): self.cluster.authenticate(self.authenticator) def stop(self): self.cluster = None def kill(self): self.cluster = None def get_dependency(self, worker_ctx): return self.cluster.open_bucket(self.bucket)
def start_database(): from couchbase.cluster import Cluster from couchbase.cluster import PasswordAuthenticator cluster = Cluster('couchbase://localhost') authenticator = PasswordAuthenticator('couchbase', 'couchbase') cluster.authenticate(authenticator) return cluster
def create_xattr_data(self, type="system"): cluster = Cluster('couchbase://'+str(self.master.ip)) authenticator = PasswordAuthenticator(self.username, self.password) cluster.authenticate(authenticator) cb = cluster.open_bucket('default') docs = self.get_meta_ids() self.log.info("Docs: " + str(docs[0:5])) xattr_data = [] self.log.info("Adding xattrs to data") val = 0 for doc in docs: if type == "system": rv = cb.mutate_in(doc["id"], SD.upsert('_system1', val, xattr=True, create_parents=True)) xattr_data.append({'_system1': val}) rv = cb.mutate_in(doc["id"], SD.upsert('_system2', {'field1': val, 'field2': val*val}, xattr=True, create_parents=True)) xattr_data.append({'_system2': {'field1': val, 'field2': val*val}}) rv = cb.mutate_in(doc["id"], SD.upsert('_system3', {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}, xattr=True, create_parents=True)) xattr_data.append({'_system3': {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}}) if type == "user": rv = cb.mutate_in(doc["id"], SD.upsert('user1', val, xattr=True, create_parents=True)) xattr_data.append({'user1': val}) rv = cb.mutate_in(doc["id"], SD.upsert('user2', {'field1': val, 'field2': val*val}, xattr=True, create_parents=True)) xattr_data.append({'user2': {'field1': val, 'field2': val*val}}) rv = cb.mutate_in(doc["id"], SD.upsert('user3', {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}, xattr=True, create_parents=True)) xattr_data.append({'user3': {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}}) val = val + 1 self.log.info("Completed adding " + type + "xattrs to data to " + str(val) + " docs") return xattr_data
def action(self, includes: dict, variables: dict) -> any: from couchbase.cluster import Cluster, PasswordAuthenticator body = self.simple_input(variables) in_data = body['request'] conf = in_data['conf'] cluster = Cluster('couchbase://' + conf['host']) if 'user' in conf and 'password' in conf: cluster.authenticate( PasswordAuthenticator(conf['user'], conf['password'])) bucket = cluster.open_bucket(conf['bucket']) if 'put' in in_data: put = in_data['put'] result = bucket.upsert(put['key'], put['value']) if result.success: return variables, {} else: raise RuntimeError(result.errstr) if 'get' in in_data: get = in_data['get'] result = bucket.get(get['key'], quiet=True) if result is None: raise RuntimeError('no data found for key ' + get['key']) return variables, result.value if 'delete' in in_data: delete = in_data['delete'] bucket.remove(delete['key'], quiet=True) return variables, {} if 'query' in in_data: query = in_data['query'] res = [row for row in bucket.n1ql_query(query)] if len(res) == 1: return variables, res[0] return variables, res raise RuntimeError('nothing to do: ' + str(in_data))
def configure_cb(): cluster = Cluster('couchbase://localhost') authenticator = PasswordAuthenticator( os.environ['CB_USERNAME'], os.environ['CB_PASSWORD'] ) cluster.authenticate(authenticator) return cluster
def _createConn(self): try: print("-->connection_string:{}".format(self.connection_string)) cluster = Cluster(self.connection_string, bucket_class=CouchbaseBucket) #cluster = Cluster("couchbases://cb.5ff6f20a-1550-4d54-9174-8a01a32edd87.dp.cloud # .couchbase.com?ssl" # "=no_verify", bucket_class=CouchbaseBucket) print("-->bucket={},username={},password={}".format( self.bucket, self.username, self.password)) cluster.authenticate( PasswordAuthenticator(self.username, self.password)) self.cb = cluster.open_bucket(self.bucket) print("-->cb={}".format(self.cb)) self.cb.upsert('testkey', 'testvalue') print(self.cb.get('testkey')) except BucketNotFoundError: raise except AuthError: # Try using default user created by the tests, if any, in case there is no user with bucket name in the # cluster. print("Auth Error!") try: cluster = Cluster(self.connection_string, bucket_class=CouchbaseBucket) cluster.authenticate( PasswordAuthenticator(self.username, self.password)) self.cb = cluster.open_bucket(self.bucket) except AuthError: raise
def _get_cb_connection(self): # instance_type = self.config['GLOBAL']['INSTANCE_TYPE'] cluster = Cluster(self.CB_URL) authenticator = PasswordAuthenticator(self.CB_APPS_USER, self.CB_APPS_PASSWORD) cluster.authenticate(authenticator) return cluster.open_bucket(self.CB_INSTANCE, lockmode=LOCKMODE_WAIT)
def uploadDoc(self): # connect to cb cluster try: connection = "couchbase://" + self.server if "ip6" in self.server or self.server.startswith("["): connection = connection + "?ipv6=allow" cluster = Cluster(connection) authenticator = PasswordAuthenticator(self.username, self.password) cluster.authenticate(authenticator) cb = cluster.open_bucket(self.bucket) cb.timeout = 100 except Exception as e: logging.error("Connection error\n" + traceback.format_exc()) json_docs = {} for i in range(self.startseqnum, self.startseqnum + self.num_docs): self.createContent() dockey = self.keyprefix + str(i) json_docs[dockey] = self.json_objs_dict BYTES_PER_BATCH = 1024 * 256 # 256K batches = [] cur_batch = {} cur_size = 0 batches.append(cur_batch) for key, value in json_docs.items(): cur_batch[key] = value cur_size += len(key) + len(value) + 24 if cur_size > BYTES_PER_BATCH: cur_batch = {} batches.append(cur_batch) cur_size = 0 num_completed = 0 while batches: batch = batches[-1] try: cb.upsert_multi(batch) num_completed += len(batch) batches.pop() except CouchbaseTransientError as e: logging.error(e) ok, fail = e.split_results() new_batch = {} for key in fail: new_batch[key] = all_data[key] batches.pop() batches.append(new_batch) num_completed += len(ok) logging.info("Retrying {}/{} items".format( len(new_batch), len(ok))) logging.info("Completed {}/{} items".format( num_completed, len(json_docs))) if self.xattrs: logging.info("Upserting xattrs") self.add_xattrs(cb) if self.filter: logging.info("Upserting filter_expressions.json") cb.upsert("filter_expressions", self.filters_json_objs_dict)
def check_service_up(self,service,nodes): self.sleep(20) if service == "nserv": output = x509main()._execute_command_clientcert(nodes.ip, url='/pools/default', port=8091, headers=' -u Administrator:password ', client_cert=False, curl=True, plain_curl=True) output = json.loads(output) self.log.info("Print output of command is {0}".format(output)) self.assertEqual(output['rebalanceStatus'], 'none', "The Web request has failed on port 8091") elif service == "index": output = x509main()._execute_command_clientcert(nodes.ip, url='/getIndexStatus', port=9102, headers='-u Administrator:password', client_cert=False, curl=True, verb='GET', plain_curl=True) output = json.loads(output) self.log.info("Print output of command is {0}".format(output)) self.assertEqual(output["code"],'success',"The Index Service is not up") elif service == "query": output = x509main()._execute_command_clientcert(nodes.ip, url='/query/service', port=8093, headers='-u Administrator:password ', client_cert=False, curl=True, verb='GET', plain_curl=True, data="statement='create index idx1 on default(name)'") self.assertEqual(json.loads(output)['status'], "success", "Create Index Failed on port 8093") elif service == "fts": idx = {"sourceName": "default","sourceType": "couchbase","type": "fulltext-index"} output = x509main()._execute_command_clientcert(nodes.ip, url='/api/index/default_idx', port=8094, headers=" -XPUT -H \"Content-Type: application/json\" -u Administrator:password ", client_cert=False, curl=True, verb='GET', plain_curl=True, data="'" + json.dumps(idx) + "'") self.assertEqual(json.loads(output)['status'], "ok", "Issue with creating FTS index with client Cert") elif service == "cbas": cmd = "curl -v " + \ " -s -u Administrator:password --data pretty=true --data-urlencode 'statement=create dataset on default' " + \ "http://{0}:{1}/_p/cbas/query/service ". \ format(nodes.ip, 8091) self.log.info("Running command : {0}".format(cmd)) output = subprocess.check_output(cmd, shell=True) self.assertEqual(json.loads(output)['status'], "success", "Create CBAS Index Failed") elif service == "eventing": cmd = "curl -v -X GET -u Administrator:password http://{0}:8096/api/v1/status" \ .format(nodes.ip) self.log.info("Running command : {0}".format(cmd)) output = subprocess.check_output(cmd, shell=True) self.assertEqual(json.loads(output)['num_eventing_nodes'], 1, "Eventing Node is not up") elif service == "kv": cluster = Cluster("couchbase://{0}".format(nodes.ip)) authenticator = PasswordAuthenticator("Administrator", "password") cluster.authenticate(authenticator) cb = cluster.open_bucket("default") cb.upsert("key1", "value1") self.assertEqual(cb.get("key1").value, "value1")
def get_cluster(username: str, password: str, host="couchbase", port="8091"): # cluster_url="couchbase://couchbase" # username = "******" # password = "******" cluster_url = get_cluster_couchbase_url(host=host, port=port) cluster = Cluster(cluster_url) authenticator = PasswordAuthenticator(username, password) cluster.authenticate(authenticator) return cluster
class GreenBoardCluster(GenericOps): def __init__(self): self.greenboard_cluster = Cluster('couchbase://%s:%s' % (GREENBOARD_DB_HOST, "8091")) self.greenboard_authenticator = PasswordAuthenticator( GREENBOARD_DB_USERNAME, GREENBOARD_DB_PASSWORD) self.greenboard_cluster.authenticate(self.greenboard_authenticator) def get_greenboard_cluster(self): return self.get_greenboard_cluster()
def get_bucket(): cluster = Cluster( "couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300" ) authenticator = PasswordAuthenticator("username", "password") cluster.authenticate(authenticator) bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT) bucket.timeout = 30 bucket.n1ql_timeout = 300 return bucket
def connect(): host_name = main.get_host_name() bucket = main.get_bucket_name() password = main.get_bucket_password() cluster = Cluster("couchbase://" + host_name + "?operation_timeout=60") authenticator = PasswordAuthenticator(bucket, password) cluster.authenticate(authenticator) return cluster.open_bucket(bucket)
def create_connection(self, bucket_name): """ Create bucket connections. 5 bucket connections are created per instance. :return: Nothing """ cluster = Cluster(self.spec) auth = PasswordAuthenticator(self.user, self.password) cluster.authenticate(auth) self.bucket_connection = cluster.open_bucket(bucket_name, lockmode=LOCKMODE_WAIT) self.bucket_connection.timeout = self.timeout
def get_cluster(): try: cluster = Cluster('couchbase://{}'.format(host)) authenticator = PasswordAuthenticator('Administrator', 'password') cluster.authenticate(authenticator) return cluster except Exception: cluster = Cluster( 'couchbase://{}'.format(host), ClusterOptions(PasswordAuthenticator('Administrator', 'password'))) return cluster
def _create_cluster(self): connargs = self.make_connargs() connstr = ConnectionString.parse(str(connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', '') # Can I open a new bucket via open_bucket? cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password})) return cluster, bucket
def _create_cluster(self): connargs = self.make_connargs() connstr = ConnectionString.parse(str(connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', '') # Can I open a new bucket via open_bucket? cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password},cluster_password=self.cluster_info.admin_password, cluster_username=self.cluster_info.admin_username)) return cluster, bucket
class Couchbase(Database): def __init__(self): # Define connection parameters. authenticator = PasswordAuthenticator('root', 'administrator') self.cluster = Cluster('couchbase://localhost') self.cluster.authenticate(authenticator=authenticator) def store_job_state(self, product_id): cluster = self.cluster.open_bucket('job') # Learn how to do things with this. return
def connect_to_bucket(host, username, password, bucket): ''' This function takes four arguments to connect to bucket. host, username, password and bucket name. Returns connection instance of the specified bucket. ''' if "couchbase" not in host: host = "couchbase://" + str(host) cluster = Cluster(host) authenticator = PasswordAuthenticator(username, password) cluster.authenticate(authenticator) cb = cluster.open_bucket(bucket) return cb
def _create_cluster_clean(self, authenticator): connargs = self.make_connargs() connstr = ConnectionString.parse(str(connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', None) keys_to_skip = authenticator.get_credentials(bucket)['options'].keys() for entry in keys_to_skip: connstr.clear_option(entry) cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password})) return cluster, bucket
def InsertData(DBName,BucketName, Data): #Database config config = getConfig() cluster=Cluster(config.get('couchbase', 'cluster')) userName=config.get('couchbase', 'DAG') Pwd=config.get('couchbase', 'Pwd') #couchbase authentification cluster.authenticate(PasswordAuthenticator(userName, Pwd)) #open the database cb = cluster.open_bucket(DBName) #open the bucket name to load data dbkey=cluster.open_bucket(BucketName) #insert data to the bucket dbkey.upsert_multi(Data)
def data_ingestion(data, cb_user, cb_pwd, cb_host, cb_bucket): ''' Ingests data into pipeline ''' cluster = Cluster('couchbase://' + cb_host) authenticator = PasswordAuthenticator(cb_user, cb_pwd) cluster.authenticate(authenticator) cb_conn = cluster.open_bucket(cb_bucket) # Pushing entire data sequentially # sequential_run(cb_conn, data) # Pushing entire data parallely parallel_run(cb_conn, data)
def __init__(self, mapper, host, bucket_name, read_only=True): # connection_str = "couchbase://%s/%s" % (host, bucket_name) # self.bucket = Bucket(connection_str) self.read_only = read_only self.mapper = mapper # self.bucket_name = bucket_name cluster = Cluster('couchbase://%s' % host) authenticator = PasswordAuthenticator('test', 'bollocks') cluster.authenticate(authenticator) self.bucket = cluster.open_bucket(bucket_name) self.mapper = mapper
def Getn1ql(bucket, user, password, limit, cluster): #function to get records using n1ql cluster = Cluster('couchbase://{}'.format(cluster)) authenticator = PasswordAuthenticator(user, password) cluster.authenticate(authenticator) bucket_out = cluster.open_bucket('{}'.format(bucket)) from couchbase.n1ql import N1QLQuery query = N1QLQuery("""SELECT * FROM `{}` limit {} """.format( bucket, limit)) query.timeout = 3600 result = '' for doc in bucket_out.n1ql_query(query): result += str(doc) return result
def _create_cluster_clean(self, authenticator): connargs = self.make_connargs() connstr = ConnectionString.parse(str( connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', None) keys_to_skip = authenticator.get_credentials(bucket)['options'].keys() for entry in keys_to_skip: connstr.clear_option(entry) cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password})) return cluster, bucket
def getFailedJobs(options): cluster = Cluster('couchbase://172.23.98.63') authenticator = PasswordAuthenticator(repo_username, repo_password) cluster.authenticate(authenticator) bkt = cluster.open_bucket('server') url = "http://qa.sc.couchbase.com/job/{}/".format(job_map[options.type]) #bkt = Bucket('couchbase://172.23.109.245/server',username=repo_username,password=repo_password) query='SELECT component,subComponent,name,build_id,url from server ' \ 'WHERE os=$os and `build`=$build and failCount >=(totalCount*$fail_percent)/100 ' \ 'and url=$url order by component' q = N1QLQuery(query, os=options.os, build=options.version, fail_percent=options.fail_percent, url=url) if options.component is not None: query='SELECT component,subComponent,name,build_id,url from server ' \ 'WHERE os=$os and `build`=$build and failCount >=(totalCount*$fail_percent)/100 ' \ 'and url=$url and component=$component' \ ' order by component' q = N1QLQuery(query, os=options.os, build=options.version, fail_percent=options.fail_percent, component=str.upper(options.component), url=url) retryJob = {} print("Running query:{}".format(q)) count = 0 for row in bkt.n1ql_query(q): count += 1 print("failed job: {}".format(row)) component = row["component"] #print component if component not in retryJob: retryJob[component] = [] if "subComponent" in row: subcomponent = row["subComponent"] #print "subcomponent:", subcomponent sub = retryJob[component] sub.append(subcomponent) #print retryJob else: subcomponent = getSubcomponent(row["build_id"], options) #print "subcomponent from jenkins:",subcomponent sub = retryJob[component] sub.append(subcomponent) print("total failed jobs:{}".format(count)) return retryJob
def _createConn(self): try: cluster = Cluster(self.connection_string, bucket_class=CouchbaseBucket) cluster.authenticate(PasswordAuthenticator(self.bucket, 'password')) self.cb = cluster.open_bucket(self.bucket) except BucketNotFoundError: raise except AuthError: # Try using default user created by the tests, if any, in case there is no user with bucket name in the # cluster. try: cluster = Cluster(self.connection_string, bucket_class=CouchbaseBucket) cluster.authenticate(PasswordAuthenticator("cbadminbucket", 'password')) self.cb = cluster.open_bucket(self.bucket) except AuthError: raise
def create_connections(self): """ Create bucket connections. 5 bucket connections are created per instance. :return: Nothing """ for i in range(0, self.threads): if self.cb_version > '5': cluster = Cluster(self.spec) auth = PasswordAuthenticator(self.user, self.password) cluster.authenticate(auth) bucket = cluster.open_bucket(self.bucket_name, lockmode=LOCKMODE_WAIT) bucket.timeout = self.timeout self.connections.append(bucket) else: bucket = Bucket('{0}/{1}'.format(self.spec, self.bucket_name), lockmode=LOCKMODE_WAIT) bucket.timeout = self.timeout self.connections.append(bucket)
#!/usr/bin/env python from couchbase.cluster import Cluster, PasswordAuthenticator from couchbase.exceptions import NotFoundError def tst(cb, key): try: cb.remove(key) except NotFoundError: print ('Nothing to delete') rv = cb.upsert(key, {'hello': 'world'}) cas = rv.cas print(rv) item = cb.get(key) print(item) cluster = Cluster('couchbase://localhost:12000') cluster.authenticate(PasswordAuthenticator('Administrator', 'asdasd')) cb = cluster.open_bucket('test') tst(cb, 'blah')
def _connect(self): """Establish a connection to the Couchbase cluster.""" cluster = Cluster('http://{}:{}'.format(self.host, self.port)) authenticator = PasswordAuthenticator('Administrator', self.password) cluster.authenticate(authenticator) self.client = cluster.open_bucket(self.bucket)