def parallel_run_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.get("X-CORRELATION-ID") if global_id is None: Logger.error("Missing X-CORRELATION-ID") return return_failed(-1, "X-CORRELATION-ID is empyt"), 400 source = request.headers.get("X-SOURCE") if source is None: return return_failed(-1, "X-SOURCE is empyt"), 400 Logger.debug("Run request json:" + json.dumps(req_json) + str(global_id)) parallel_nodes = conform_param(req_json, "nodes", list) task_name = conform_param(req_json, "name", basestring) tasks = conform_param(req_json, "tasks", list) tasktype = conform_param(req_json, "tasktype", basestring, default_value="ansible_task") params = conform_param(req_json, "params", dict, {}, True) user_name = conform_param(req_json, "user", basestring, allowNone=True) fork_num = conform_param(req_json, "fork_num", int, allowNone=True) headers = { 'content-type': 'application/json', 'X-CORRELATION-ID': request.headers.get("X-CORRELATION-ID"), 'X-SOURCE': request.headers.get("X-SOURCE"), 'Authorization': 'Basic bmlra2lfdGVzdDAwMkBzaW5hLmNuOjEyMzIyMw==', 'Cache-Control': 'no-cache' } task = Service.get_task_by_name(task_name) if task is not None: Logger.error("task name is duplicate:" + task_name) return return_failed(-1, "task name is duplicate"), 400 task_id = Service.new_task({"name": task_name}) return_list = [] for nodes in parallel_nodes: req_json['nodes'] = [nodes] req_json['come_from_master'] = 1 req_json['mtask_id'] = task_id r = requests.post("http://%s:8000/api/run" % (nodes), data=json.dumps(req_json), headers=headers, timeout=60) return_list.append(r.json()['content']['id']) # check task name duplicate return return_success(content={"id": task_id}), 200 except JsonEncodeException as e: Logger.error("try run_task exception ---------@ ") return return_failed(-1, "json encode error"), 400 except ParamErrorException as e: Logger.error("try run_task exception --------------> %s" % (str(e))) return return_failed(-1, "param error, error: " + e.message), 400 except Exception as e: Logger.error("try run_task exception --------------> %s" % (str(e))) return return_failed(-1, e.message), 500
def run_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.get("X-CORRELATION-ID") if global_id is None: Logger.error("Missing X-CORRELATION-ID") return return_failed(-1, "X-CORRELATION-ID is empyt"), 400 source = request.headers.get("X-SOURCE") if source is None: return return_failed(-1, "X-SOURCE is empyt"), 400 Logger.debug("Run request json:" + json.dumps(req_json) + str(global_id)) task_name = conform_param(req_json, "name", basestring) nodes = conform_param(req_json, "nodes", list) tasks = conform_param(req_json, "tasks", list) tasktype = conform_param(req_json, "tasktype", basestring, default_value="ansible_task") params = conform_param(req_json, "params", dict, {}, True) user_name = conform_param(req_json, "user", basestring, allowNone=True) fork_num = conform_param(req_json, "fork_num", int, allowNone=True) # check task name duplicate task = Service.get_task_by_name(task_name) if task is not None: Logger.error("task name is duplicate:" + task_name) return return_failed(-1, "task name is duplicate"), 400 task_id = Service.new_task({"name": task_name}) #submit task Worker.submit( AnsibleTask(task_id=str(task_id), name=task_name, hosts=nodes, tasks=tasks, tasktype=tasktype, params=params, user=user_name, forks=fork_num, global_id=global_id, source=source, result="")) return return_success(content={"id": task_id}), 200 except JsonEncodeException as e: Logger.error("try run_task exception ---------@ ") return return_failed(-1, "json encode error"), 400 except ParamErrorException as e: Logger.error("try run_task exception --------------> %s" % (str(e))) return return_failed(-1, "param error, error: " + e.message), 400 except Exception as e: Logger.error("try run_task exception --------------> %s" % (str(e))) return return_failed(-1, e.message), 500
def stop_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("Stop 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) #stop task by id 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, "task not found"), 404 task_id = task.id Worker.stop(str(task_id)) # update task status Service.update_task(task_id, status=Service.STATUS_STOPPED) return return_success(), 200 except JsonEncodeException: Logger.error("try stop_task exception --------------@") return return_failed(-1, "json encode error"), 400 except Exception as e: Logger.error("try stop_task exception --------------> %s" % (str(e))) return return_failed(-1, e.message), 400
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