From be964c54a4d1435ed745544190c8d0bb85876551 Mon Sep 17 00:00:00 2001 From: chenzuoqing Date: Thu, 16 Dec 2021 19:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=90=8C=E6=AD=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E5=8F=AF=E8=A7=A6=E5=8F=91=E4=BB=8E=E8=BF=90?= =?UTF-8?q?=E7=BB=B4=E5=90=8E=E5=8F=B0=E5=90=8C=E6=AD=A5=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/v1/project.py | 2 ++ src/controller/project/operation.py | 13 ++++++- src/service/project/__init__.py | 0 src/service/project/sync.py | 53 +++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/service/project/__init__.py create mode 100644 src/service/project/sync.py diff --git a/src/api/v1/project.py b/src/api/v1/project.py index 3250181..10ef33d 100644 --- a/src/api/v1/project.py +++ b/src/api/v1/project.py @@ -12,6 +12,8 @@ api = Api(project) # 项目模型的视图 api.add_resource(views.ProjectViews, '/item/', endpoint="project") api.add_resource(views.ProjectDetailViews, '/item//', endpoint="project-detail") +# 同步项目信息(从ops1同步) +api.add_resource(operation.ProjectSyncView, '/item/sync/', endpoint="project-sync") # 渠道模型视图 api.add_resource(views.ChannelViews, '/channel/', endpoint="channel") diff --git a/src/controller/project/operation.py b/src/controller/project/operation.py index bed67c8..19933cc 100644 --- a/src/controller/project/operation.py +++ b/src/controller/project/operation.py @@ -1,7 +1,7 @@ import datetime from flask import current_app as app -from flask_restful import reqparse, marshal, fields as F +from flask_restful import Resource, reqparse, marshal, fields as F from models.asset.models import Host from models.asset.fields import HostFields @@ -10,6 +10,17 @@ from models.project.models import Project, Channel, Server, Version from common.views import CreateMixin, RetrieveMixin from common.permission import token_header_required from common.utils import make_response +from service.project.sync import sync_project_for_ops1 + + +class ProjectSyncView(Resource): + """从 OPS1 同步项目,触发接口""" + + def get(self): + success = sync_project_for_ops1() + if success: + return make_response(200, code=1000, msg="success") + return make_response(200, code=1001, msg="有项目同步失败!") class ServerSyncView(CreateMixin): diff --git a/src/service/project/__init__.py b/src/service/project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/service/project/sync.py b/src/service/project/sync.py new file mode 100644 index 0000000..27f2f49 --- /dev/null +++ b/src/service/project/sync.py @@ -0,0 +1,53 @@ +import logging + +import requests + +from settings.common import CONFIG +from models.project.models import Project + +logger = logging.getLogger("common") + + +def sync_project_for_ops1() -> bool: + """从 ops1 同步项目,返回成功或失败""" + # 从配置文件获取 OPS1 信息 + ops_conf = CONFIG.get("ops1") + server = ops_conf.get("server") + headers = ops_conf.get("headers", {}) + uri = ops_conf.get("project_list", "") + try: + res = requests.get(f"{server}{uri}", headers=headers) + res.raise_for_status() + project_list = res.json() + except: + logger.exception(f"请求ops1失败: {server}") + return False + + success = True + # 获取 model 定义的全部字段 + model_fields = list(Project._fields.keys()) + for proj in project_list: + # 名称判断,是否合法 + fullname = proj.get("name") + if not fullname: + continue + try: + name, fork = fullname.split('/') + except: + continue + + params = {"name": name, "fork": fork} + for field_name, value in proj.items(): + if field_name in ("id", "name") or field_name not in model_fields: + continue + else: + params[field_name] = value + try: + # 更新或创建项目对象,以 name 和 fork 唯一组合查询 + Project.update_or_create(name=name, fork=fork, defaults=params) + except: + logger.exception(f"保存项目:{name}/{fork}失败: {params}") + success = False + continue + logger.info(f"保存项目:{name}/{fork}成功: {params}") + return success