def getlog(): try: #read http params req_json = request.get_json(force=True, silent=True) if req_json is None: raise JsonEncodeException global_id = request.headers["X-CORRELATION-ID"] if global_id is None: Logger.error("X-CORRELATION-ID is Empty") return_failed(-1, "X-CORRELATION-ID is Empty"), 400 source = request.headers["X-SOURCE"] if source is None: Logger.error("X-CORRELATION-ID is Empty") return_failed(-1, "X-SOURCE is Empty"), 400 Logger.debug("Check request json:" + json.dumps(req_json) + str(global_id)) host = conform_param(req_json, "host", param_type=basestring, allowNone=True) source = conform_param(req_json, "source", param_type=basestring, allowNone=True) logs = None #load task by id or name if host is None: if source is None: raise ParamErrorException("source not found") else: logs = Service.get_log_by_globalid_source_host( global_id, source) if logs is None: return return_failed( -1, "no log found for specified name:" + source), 404 else: logs = Service.get_log_by_globalid_source_host( global_id, source, host) if logs is None: return return_failed( -1, "no task found for specified global_id source host:" + str(global_id)), 404 ret_log = [] ok_dict = { AnsibleTask.STEP_SUBMIT: '1.Submmit ansible task and be ready to start OK', AnsibleTask.STEP_SSH: '2.Download ssh_keys OK', AnsibleTask.STEP_INIT: '3.Init ansible config OK', AnsibleTask.STEP_LOAD: '4.Load play for task OK', AnsibleTask.STEP_RUN: '5.Begin execute play for task' } for log in logs: task = Service.get_task_by_id(log.task_id) # failed_dict = { # AnsibleTask.STEP_SUBMIT: '1.Submmit ansible task and be ready to start ...',AnsibleTask.STEP_SSH:'2.Download ssh_keys ...', # AnsibleTask.STEP_INIT: '3.Init ansible config ...', AnsibleTask.STEP_LOAD: '4.Load play for task ...', # AnsibleTask.STEP_RUN: '5.Execute play for task ...' # } step_ret = [] i = AnsibleTask.STEP_SUBMIT while i <= task.step: step_ret.append(ok_dict[i]) i += 1 # if task.step < AnsibleTask.STEP_RUN: # step_ret.append(failed_dict[task.step+1]) ret_log.extend(step_ret) ret_log.append( "global_id = %s, source = %s, create_time = %s, end_time = %s, host = %s, result = %s " % (log.global_id, log.source, log.create_time, log.end_time, log.host, log.task_status)) if log.task_status == "failed": redict = json.loads(log.log) tmps = "--->Run task failed!!!<--- \n\t" if "results" not in redict.keys(): tmps += "message: " if "msg" in redict.keys(): tmps += redict["msg"] elif "stderr" in redict.keys(): tmps += redict["stderr"] else: tmps += "no error msg out,maybe db op error" tmps += "\n\t" ret_log.append(tmps) continue for i in redict["results"]: if "msg" in redict.keys(): #if i["msg"] != "": tmps += "message: " tmps += i["msg"] tmps += "\n\t" ret_log.append(tmps) continue if log.task_status == "unreacheable": tmps = "--->Run task unreacheable!!!<--- \n\t" tmps += log.log tmps += "\n\t" ret_log.append(tmps) continue if log.task_status == "ok": tmps = "--->Run task ok!!!<--- \n\t" tmps += log.log tmps += "\n\t" ret_log.append(tmps) continue if log.task_status == "start": tmps = log.log tmps += "\n\t" ret_log.append(tmps) continue return return_success(content={"log": ret_log}), 200 except JsonEncodeException: Logger.error("try getlog exception --------------@") return return_failed(-1, "json encode error"), 400 except Exception as e: Logger.error("try getlog exception --------------$> %s" % (str(e))) return return_failed(-1, e.message), 500
def check_task(): try: #read http params req_json = request.get_json(force=True, silent=True) if req_json is None: raise JsonEncodeException global_id = request.headers["X-CORRELATION-ID"] if global_id is None: return_failed(-1, "X-CORRELATION-ID is Empty"), 400 source = request.headers["X-SOURCE"] if source is None: return_failed(-1, "X-SOURCE is Empty"), 400 Logger.debug("Check request json:" + json.dumps(req_json) + str(global_id)) task_id = conform_param(req_json, "id", param_type=int, allowNone=True) task_name = conform_param(req_json, "name", param_type=basestring, allowNone=True) #load task by id or name if task_id is None: if task_name is None: raise ParamErrorException("key task_id/task_name not found") else: task = Service.get_task_by_name(task_name) if task is None: return return_failed( -1, "no task found for specified name:" + task_name), 404 else: task = Service.get_task_by_id(task_id) if task is None: return return_failed( -1, "no task found for specified id:" + str(task_id)), 404 node_list = Service.check_task(task_id=str(task.id)) #return status data ret_node = [] for node in node_list: ret_node.append( dict( ip=node.ip, status=node.status, #log=json.loads(node.log), log=node.log, )) ret_task = dict( id=task.id, status=task.status, err=task.err, ) return return_success(content={ "task": ret_task, "nodes": ret_node }), 200 except JsonEncodeException: Logger.error("try check_task exception --------------@") return return_failed(-1, "json encode error"), 400 except Exception as e: Logger.error("try check_task exception -------------->$ %s" % (str(e))) return return_failed(-1, e.message), 500