def run(self): exc_info = None try: while not self.threading_exception_queue.empty(): exc_info = self.threading_exception_queue.get(block=False) logging.info('get exc_info: %s' % str(exc_info)) if exc_info is None: continue e = exc_info[1] if isinstance(e, HTTPAPIError): pass elif isinstance(e, HTTPError): e = HTTPAPIError(e.status_code) else: e = HTTPAPIError(500) exception = "".join( [ln for ln in traceback.format_exception(*exc_info)]) logging.error(e) self._send_error_email(exception) except: logging.error('exc_info : %s' % str(exc_info)) exc_type, exc_obj, exc_trace = exc_info # deal with the exception logging.error(exc_type) logging.error(exc_obj) logging.error(exc_trace) self._send_error_email(exc_type + exc_obj + exc_trace)
def post(self): args = self.get_all_arguments() logging.info('all_arguments: %s' % str(args)) container_name = args.get('containerName') if not container_name: raise HTTPAPIError(status_code=417, error_detail="no container_name argument!",\ notification = "direct", \ log_message= "no container_name argument!",\ response = "please check params!") exists = self.container_opers.check_container_exists(container_name) if not exists: raise HTTPAPIError(status_code=417, error_detail="container %s not exist!" % container_name,\ notification = "direct", \ log_message= "container %s not exist!" % container_name,\ response = "please check!") stat = self.container_opers.get_container_stat(container_name) if stat == Status.started: massage = {} massage.setdefault("status", stat) massage.setdefault("message", "no need this operation, the container has been started!") self.finish(massage) return self.container_opers.start(container_name) return_message = {} return_message.setdefault("message", "due to start a container need a little time, please wait and check the result~") self.finish(return_message)
def cluster_resoure(self, cluster, resource_type): zkOper = Requests_ZkOpers() exists = zkOper.check_containerCluster_exists(cluster) if not exists: error_message = 'container cluster %s not exist, please check your cluster name' % cluster raise HTTPAPIError(status_code=417, error_detail=error_message, notification="direct", log_message=error_message, response=error_message) container_node_list = zkOper.retrieve_container_list(cluster) result = [] for container_node in container_node_list: resource = {} resource_value = zkOper.retrieve_container_resource(cluster, container_node, resource_type) host_ip = self.container_opers.get_host_ip_from_zk(cluster, container_node) container_name = self.container_opers.get_container_name_from_zk(cluster, container_node) resource.setdefault('value', resource_value) resource.setdefault('hostIp', host_ip) resource.setdefault('containerName', container_name) result.append(resource) return result
def post(self): ''' function: create admin user url example: curl -d "adminUser=root&adminPassword=root" "http://localhost:8888/admin/user" ''' requestParam = {} args = self.request.arguments logging.info("args :" + str(args)) for key in args: value = args[key][0] if key == 'adminPassword': value = base64.encodestring(value).strip('\n') requestParam.setdefault(key, value) if requestParam['adminUser'] == '' or requestParam[ 'adminPassword'] == '': raise HTTPAPIError(status_code=401, error_detail="username or password is empty", notification="direct", log_message="username or password is empty", response="username or password is empty") if requestParam != {}: self.confOpers.setValue(options.cluster_property, requestParam) result = {} result.setdefault("message", "creating admin user successful!") self.finish(result)
def write_error(self, status_code, **kwargs): """Override to implement custom error pages.""" debug = self.settings.get("debug", False) try: exc_info = kwargs.pop('exc_info') e = exc_info[1] if isinstance(e, HTTPAPIError): pass elif isinstance(e, UserVisiableException): user_message = e.__str__() e = HTTPAPIError(417, error_detail=user_message) status_code = e.status_code elif isinstance(e, HTTPError): e = HTTPAPIError(e.status_code) else: e = HTTPAPIError(500) exception = "".join( [ln for ln in traceback.format_exception(*exc_info)]) if status_code == 500 and not debug: logging.error(e) self._send_error_email(exception) if debug: e.response["exception"] = exception self.clear() self.set_status(200) # always return 200 OK for API errors self.set_header("Content-Type", "application/json; charset=UTF-8") self.finish(str(e)) except Exception: logging.error(traceback.format_exc()) return super(APIHandler, self).write_error(status_code, **kwargs)
def check_container_name(self, container_name): exists = self.container_opers.check_container_exists(container_name) if not exists: error_message = 'container %s not exist, please check your container name' % container_name raise HTTPAPIError(status_code=417, error_detail=error_message, notification="direct", log_message=error_message, response=error_message)
def check_host_ip(self, host_ip): exist = self.server_opers.host_exist(host_ip) if not exist: error_message = 'server %s not exist, please check your server ip' % host_ip raise HTTPAPIError(status_code=417, error_detail=error_message, notification="direct", log_message=error_message, response=error_message)
def check_cluster(self, name): zk_op = self.get_zkoper() if zk_op.cluster_exists(name) or not name: raise HTTPAPIError( status_code=417, error_detail="server has belong to a cluster,\ should be not create new cluster!", notification="direct", log_message="server has belong to a cluster,should be not \ create new cluster!", response="the server has belonged to a cluster,should be \ not create new cluster!")
def post(self): """ eg. curl --user root:root -d "containerName=d-mcl-zz2-n-3&componentType=mcluster" /container/manager/status """ args = self.get_all_arguments() container_name = args.get('containerName') component_type = args.get('componentType') if not (container_name and component_type): raise HTTPAPIError(status_code=417, error_detail="no containerName or componentType argument!",\ notification = "direct", \ log_message= "no containerName or componentType argument!",\ response = "please check params!") ret = self.container_opers.manager_status_validate(component_type, container_name) result = {} result.setdefault("message", ret) self.finish(result)
def post(self): args = self.get_all_arguments() logging.info('all_arguments: %s' % str(args)) container_name = args.get('containerName') if not container_name: raise HTTPAPIError(status_code=400, error_detail="no container_name argument!",\ notification = "direct", \ log_message= "no container_name argument!",\ response = "please check params!") exists = self.container_opers.check_container_exists(container_name) if not exists: massage = {} massage.setdefault("status", "not exist") massage.setdefault("message", "no need this operation, there is no such a container!") self.finish(massage) return self.container_opers.destroy(container_name) return_message = {} return_message.setdefault("message", "remove container has been done but need some time, please wait a moment and check the result!") self.finish(return_message)
def prepare(self): super(APIErrorHandler, self).prepare() raise HTTPAPIError(404)