项目同步接口,可触发从运维后台同步项目信息
This commit is contained in:
parent
bae428c975
commit
be964c54a4
|
@ -12,6 +12,8 @@ api = Api(project)
|
||||||
# 项目模型的视图
|
# 项目模型的视图
|
||||||
api.add_resource(views.ProjectViews, '/item/', endpoint="project")
|
api.add_resource(views.ProjectViews, '/item/', endpoint="project")
|
||||||
api.add_resource(views.ProjectDetailViews, '/item/<string:pk>/', endpoint="project-detail")
|
api.add_resource(views.ProjectDetailViews, '/item/<string:pk>/', endpoint="project-detail")
|
||||||
|
# 同步项目信息(从ops1同步)
|
||||||
|
api.add_resource(operation.ProjectSyncView, '/item/sync/', endpoint="project-sync")
|
||||||
|
|
||||||
# 渠道模型视图
|
# 渠道模型视图
|
||||||
api.add_resource(views.ChannelViews, '/channel/', endpoint="channel")
|
api.add_resource(views.ChannelViews, '/channel/', endpoint="channel")
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from flask import current_app as app
|
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.models import Host
|
||||||
from models.asset.fields import HostFields
|
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.views import CreateMixin, RetrieveMixin
|
||||||
from common.permission import token_header_required
|
from common.permission import token_header_required
|
||||||
from common.utils import make_response
|
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):
|
class ServerSyncView(CreateMixin):
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue