Esempio n. 1
0
 def _http_request(self, api, method='GET', params='', headers=None, timeout=120):
     if not headers:
         headers=self._create_headers()
     end_time = time.time() + timeout
     while True:
         try:
             response, content = httplib2.Http().request(api, method, params, headers)
             if response['status'] in ['200', '201', '202']:
                 return True, content
             else:
                 try:
                     json_parsed = json.loads(content)
                 except:
                     json_parsed = {}
                 reason = "unknown"
                 status = False
                 if "error" in json_parsed:
                     reason = json_parsed["error"]
                     status = reason
                 elif "errors" in json_parsed:
                     errors = [error for _,error in json_parsed["errors"].iteritems()]
                     reason = ", ".join(errors)
                     status = reason
                 log.error('{0} error {1} reason: {2} {3}'.format(api, response['status'], reason, content))
                 return status, content
         except socket.error as e:
             log.error(e)
             if time.time() > end_time:
                 raise ServerUnavailableException(ip=self.ip)
         except httplib2.ServerNotFoundError as e:
             log.error(e)
             if time.time() > end_time:
                 raise ServerUnavailableException(ip=self.ip)
         time.sleep(1)
Esempio n. 2
0
 def get_nodes(self):
     nodes = []
     api = self.baseUrl + 'pools/default'
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         #if status is 200 then it was a success otherwise it was a failure
         if response['status'] == '400':
             #extract the error
             #self.log.error('unable to retrieve nodesStatuses')
             print 'unable to retrieve nodesStatuses'
         elif response['status'] == '200':
             parsed = json.loads(content)
             if "nodes" in parsed:
                 for json_node in parsed["nodes"]:
                     node = RestParser().parse_get_nodes_response(json_node)
                     node.rest_username = self.username
                     node.rest_password = self.password
                     node.port = self.port
                     if node.ip == "127.0.0.1":
                         node.ip = self.ip
                     nodes.append(node)
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
     return nodes
Esempio n. 3
0
 def node_statuses(self):
     nodes = []
     api = self.baseUrl + 'nodeStatuses'
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         #if status is 200 then it was a success otherwise it was a failure
         if response['status'] == '400':
             #extract the error
             #self.log.error('unable to retrieve nodesStatuses')
             print 'unable to retrieve nodesStatuses'
         elif response['status'] == '200':
             parsed = json.loads(content)
             for key in parsed:
                 #each key contain node info
                 value = parsed[key]
                 #get otp,get status
                 node = OtpNode(id=value['otpNode'], status=value['status'])
                 if node.ip == '127.0.0.1':
                     node.ip = self.ip
                 node.port = int(key[key.rfind(":") + 1:])
                 node.replication = value['replication']
                 nodes.append(node)
                 #let's also populate the membase_version_info
         return nodes
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
Esempio n. 4
0
 def get_bucket_stats(self, bucket='default'):
     api = "{0}{1}{2}{3}".format(self.baseUrl, 'pools/default/buckets/',
                                 bucket, "/stats")
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         if response['status'] == '400':
             #self.log.error('get_bucket error {0}'.format(content))
             print 'get_bucket error {0}'.format(content)
         elif response['status'] == '200':
             parsed = json.loads(content)
             #let's just return the samples
             #we can also go through the list
             #and for each item remove all items except the first one ?
             op = parsed["op"]
             samples = op["samples"]
             stats = {}
             #for each sample
             for stat_name in samples:
                 if samples[stat_name]:
                     last_sample = len(samples[stat_name]) - 1
                     if last_sample:
                         stats[stat_name] = samples[stat_name][last_sample]
             return stats
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
Esempio n. 5
0
 def get_bucket_stats_for_node(self, bucket='default', node_ip=None):
     if not Node:
         #self.log.error('node_ip not specified')
         print 'node_ip not specified'
         return None
     api = "{0}{1}{2}{3}{4}{5}".format(self.baseUrl,
                                       'pools/default/buckets/', bucket,
                                       "/nodes/", node_ip, ":8091/stats")
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         if response['status'] == '400':
             #self.log.error('get_bucket error {0}'.format(content))
             print 'get_bucket error {0}'.format(content)
         elif response['status'] == '200':
             parsed = json.loads(content)
             #let's just return the samples
             #we can also go through the list
             #and for each item remove all items except the first one ?
             op = parsed["op"]
             samples = op["samples"]
             stats = {}
             #for each sample
             for stat_name in samples:
                 stats[stat_name] = samples[stat_name][0]
             return stats
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
Esempio n. 6
0
 def log_client_error(self, post):
     api = self.baseUrl + 'logClientError'
     try:
         response, content = httplib2.Http().request(
             api, 'POST', body=post, headers=self._create_headers())
         #if status is 200 then it was a success otherwise it was a failure
         if response['status'] == '400':
             print 'unable to logClientError'
             #extract the error
             #self.log.error('unable to logClientError')
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
Esempio n. 7
0
 def get_pools_info(self):
     api = self.baseUrl + 'pools'
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         if response['status'] == '400':
             #self.log.error('get_pools error {0}'.format(content))
             print 'get_pools error {0}'.format(content)
         elif response['status'] == '200':
             parsed = json.loads(content)
             return parsed
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
Esempio n. 8
0
 def get_pools(self):
     version = None
     api = self.baseUrl + 'pools'
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         if response['status'] == '400':
             #self.log.error('get_pools error {0}'.format(content))
             print 'get_pools error {0}'.format(content)
         elif response['status'] == '200':
             parsed = json.loads(content)
             version = MembaseServerVersion(parsed['implementationVersion'],
                                            parsed['componentsVersion'])
         return version
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
Esempio n. 9
0
 def cluster_status(self):
     parsed = []
     api = self.baseUrl + 'pools/default'
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         #if status is 200 then it was a success otherwise it was a failure
         if response['status'] == '400':
             #extract the error
             #self.log.error('unable to retrieve pools/default')
             print 'unable to retrieve pools/default'
         elif response['status'] == '200':
             parsed = json.loads(content)
         return parsed
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
Esempio n. 10
0
 def get_nodes_self(self):
     node = None
     api = self.baseUrl + 'nodes/self'
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         #if status is 200 then it was a success otherwise it was a failure
         if response['status'] == '400':
             #extract the error
             #self.log.error('unable to retrieve nodesStatuses')
             print 'unable to retrieve nodesStatuses'
         elif response['status'] == '200':
             parsed = json.loads(content)
             node = RestParser().parse_get_nodes_response(parsed)
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
     return node
Esempio n. 11
0
 def get_bucket(self, bucket='default'):
     bucketInfo = None
     api = '{0}{1}{2}'.format(self.baseUrl, 'pools/default/buckets/',
                              bucket)
     try:
         if self.password:
             response, content = httplib2.Http().request(
                 api, headers=self._create_headers())
         else:
             response, content = httplib2.Http().request(api)
         if response['status'] == '400':
             #self.log.error('get_bucket error {0}'.format(content))
             print 'get_bucket error {0}'.format(content)
         elif response['status'] == '200':
             bucketInfo = RestParser().parse_get_bucket_response(content)
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
     return bucketInfo
Esempio n. 12
0
 def get_buckets(self):
     #get all the buckets
     buckets = []
     api = '{0}{1}'.format(self.baseUrl, 'pools/default/buckets/')
     try:
         response, content = httplib2.Http().request(
             api, headers=self._create_headers())
         if response['status'] == '400':
             #self.log.error('get_buckets error {0}'.format(content))
             print 'get_buckets error {0}'.format(content)
         elif response['status'] == '200':
             parsed = json.loads(content)
             # for each elements
             for item in parsed:
                 bucketInfo = RestParser().parse_get_bucket_json(item)
                 buckets.append(bucketInfo)
             return buckets
     except socket.error:
         raise ServerUnavailableException(ip=self.ip)
     except httplib2.ServerNotFoundError:
         raise ServerUnavailableException(ip=self.ip)
     return buckets