def main(session=None): dcmp_dags = session.query(DcmpDag).order_by(DcmpDag.dag_name).all() for dcmp_dag in dcmp_dags: if dcmp_dag.version != dcmp_dag.approved_version: dcmp_dag.approve_conf(session=session) session.commit() dag_converter.refresh_dags()
def main(session=None): dcmp_dags = session.query(DcmpDag).order_by(DcmpDag.dag_name).all() for dcmp_dag in dcmp_dags: if dcmp_dag.version != dcmp_dag.approved_version: dcmp_dag.approve_conf(session=session) session.commit() check_date = datetime.datetime.now() - datetime.timedelta(minutes=1) dags_to_delete = session.query(DcmpDagConf).filter( DcmpDagConf.action == "delete", DcmpDagConf.created_at >= check_date).all() for dag in dags_to_delete: count = delete_dag.delete_dag(dag.dag_name) print("Removed {} record(s)".format(count)) dag_converter.refresh_dags()
def api(self, session=None): user = get_current_user() api = request.args.get("api") if api == "delete_dag": if not can_access_approver(): return jsonify({ "code": -100, "detail": "no permission", }) dag_name = request.args.get("dag_name") if dag_name: dcmp_dag = session.query(DcmpDag).filter( DcmpDag.dag_name == dag_name, ).first() if dcmp_dag: dcmp_dag.delete_conf(user=user, session=session) session.commit() dag_converter.refresh_dags() return jsonify({ "code": 0, "detail": "succeeded", }) else: return jsonify({ "code": -1, "detail": "dag name required", }) elif api == "update_dag": dag_name = request.args.get("dag_name") data = request.get_json() try: data = dag_converter.clean_dag_dict(data, strict=True) except Exception as e: logging.exception("api.update_dag") return jsonify({ "code": -2, "detail": e.message, }) new_dag_name = data["dag_name"] if new_dag_name != dag_name: if dag_name and not can_access_approver(): return jsonify({ "code": -100, "detail": "no permission to change dag name", }) dcmp_dag = session.query(DcmpDag).filter( DcmpDag.dag_name == new_dag_name, ).first() if dcmp_dag: return jsonify({ "code": -3, "detail": "dag name duplicated", }) DcmpDag.create_or_update_conf(data, user=user, session=session) if new_dag_name != dag_name and dag_name: dcmp_dag = session.query(DcmpDag).filter( DcmpDag.dag_name == dag_name, ).first() if dcmp_dag: dcmp_dag.delete_conf(user=user, session=session) session.commit() if not need_approver(): dag_converter.refresh_dags() return jsonify({ "code": 0, "detail": "succeeded", }) elif api == "approve_dag": if not can_access_approver(): return jsonify({ "code": -100, "detail": "no permission", }) dag_name = request.args.get("dag_name") version = request.args.get("version") try: version = int(version) except Exception as e: return jsonify({ "code": -5, "detail": "version invalid", }) if version <= 0: return jsonify({ "code": -5, "detail": "version invalid", }) if dag_name: dcmp_dag = session.query(DcmpDag).filter( DcmpDag.dag_name == dag_name, ).first() if dcmp_dag: if version > dcmp_dag.version or version <= dcmp_dag.approved_version: return jsonify({ "code": -5, "detail": "version invalid", }) if not dcmp_settings.DAG_CREATION_MANAGER_CAN_APPROVE_SELF and dcmp_dag.last_editor_user_id == user.id: return jsonify({ "code": -6, "detail": "can not approve yourself", }) dcmp_dag.approve_conf(version=version, user=user, session=session) session.commit() dag_converter.refresh_dags() return jsonify({ "code": 0, "detail": "succeeded", }) else: return jsonify({ "code": -2, "detail": "dag does not exists", }) else: return jsonify({ "code": -1, "detail": "dag name required", }) elif api == "get_dag": dag_name = request.args.get("dag_name") version = request.args.get("version") if dag_name: dcmp_dag = session.query(DcmpDag).filter( DcmpDag.dag_name == dag_name, ).first() if dcmp_dag: conf = dcmp_dag.get_conf(version=version, session=session) if conf: return jsonify({ "code": 0, "detail": { "conf": conf }, }) else: return jsonify({ "code": -3, "detail": "version does not exists", }) else: return jsonify({ "code": -2, "detail": "dag does not exists", }) else: return jsonify({ "code": -1, "detail": "dag name required", }) return jsonify({ "code": 0, "detail": "succeeded", }) elif api == "render_task_conf": data = request.get_json() try: ti = dag_converter.create_task_instance_by_task_conf(data) except Exception as e: logging.exception("api.render_task_conf") return jsonify({ "code": -2, "detail": e.message, }) res = self.render_ti(ti, result_format=request.args.get( "format", None)) return jsonify({ "code": 0, "detail": res, }) elif api == "dry_run_task_conf": data = request.get_json() try: ti = dag_converter.create_task_instance_by_task_conf(data) except Exception as e: logging.exception("api.dry_run_task_conf") return jsonify({ "code": -2, "detail": e.message, }) rendered = self.render_ti(ti, result_format=request.args.get( "format", None)) with LogStreamContext() as stream: ti.dry_run() log = stream.getvalue() return jsonify({ "code": 0, "detail": { "rendered": rendered, "log": log }, }) elif api == "end_editing": dag_name = request.args.get("dag_name") if dag_name: dcmp_dag = session.query(DcmpDag).filter( DcmpDag.dag_name == dag_name, ).first() if dcmp_dag and dcmp_dag.editing and user.id == dcmp_dag.editing_user_id: dcmp_dag.end_editing() session.commit() return jsonify({ "code": 0, "detail": "succeeded", }) return jsonify({ "code": -1000, "detail": "no such api", })