diff --git a/src/controller/asset/host.py b/src/controller/asset/host.py index 1db5dd8..cface57 100644 --- a/src/controller/asset/host.py +++ b/src/controller/asset/host.py @@ -5,7 +5,7 @@ from flask import request from flask_restful import reqparse from models.asset import fields as assetField -from models.asset import models as assetModel +from models.asset import host as assetModel from common.views import ListCreateViewSet, DetailViewSet from common.permission import session_or_token_required diff --git a/src/controller/project/operation.py b/src/controller/project/operation.py index 8c5faec..2528a74 100644 --- a/src/controller/project/operation.py +++ b/src/controller/project/operation.py @@ -3,7 +3,7 @@ import datetime from flask_restful import Resource, reqparse, marshal, fields as F -from models.asset.models import Host +from models.asset.host import Host from models.asset.fields import HostFields from models.project import fields from models.project.models import Project, Channel, Server, Version diff --git a/src/controller/project/views.py b/src/controller/project/views.py index 1a0a3e8..dd34f73 100644 --- a/src/controller/project/views.py +++ b/src/controller/project/views.py @@ -4,7 +4,7 @@ from flask import request from flask_restful import inputs from models.project import fields -from models.asset import models as assetModel +from models.asset import host as assetModel from models.project.models import Project, Channel, Server from common.views import ListMixin, CreateMixin, ListCreateViewSet, DetailViewSet from common.permission import session_or_token_required diff --git a/src/models/asset/fields.py b/src/models/asset/fields.py index e6e2ba5..0239eb8 100644 --- a/src/models/asset/fields.py +++ b/src/models/asset/fields.py @@ -26,82 +26,3 @@ HostSimpleFields = { "public_ip": fields.String, "private_ip": fields.String, } - -DatabaseServerFields = { - "id": fields.String, - "name": fields.String, - "domain": fields.String, - "host": fields.String, - "manage": fields.String, - "data": fields.Raw, - "tags": fields.List(fields.String), - "labels": fields.Raw, -} - - -databaseDetailFields = { - "id": "", - "name": fields.String, - "domain": fields.String, - "host": fields.String, - "manage": fields.String, - "data": fields.Raw, - "tags": fields.List(fields.String), - "labels": fields.Raw, -} - -DatabaseFields = { - "id": fields.String, - "name": fields.String, - "username": fields.String, - "password": fields.String, - "data": fields.Raw, - "tags": fields.List(fields.String), - "labels": fields.Raw, -} - -MySQLInstanceFields = { - "port": fields.Integer, - "username": fields.String, - "password": fields.String, - "storage": fields.Integer, - "memory": fields.Integer, - "core": fields.Integer, - "databases": fields.List(fields.Nested(DatabaseFields)) -} -# 合并 DatabaseServerFields -MySQLInstanceFields.update(DatabaseServerFields) - -RedisInstanceFields = { - "port": fields.Integer, - "memory": fields.Integer, - "replicas": fields.Integer, - "password": fields.String, -} -# 合并 DatabaseServerFields -RedisInstanceFields.update(DatabaseServerFields) - -MiddlewareFields = { - "id": fields.String, - "host": fields.String, - "manage": fields.String, - "data": fields.Raw, - "tags": fields.List(fields.String), - "labels": fields.Raw, -} - -NginxInstanceFields = { - "port": fields.Integer, - "url": fields.String, -} -NginxInstanceFields.update(MiddlewareFields) - -# CDN 序列化字段 -CDNFields = { - "id": fields.String, - "domain": fields.String, - "data": fields.Raw, - "manage": fields.String, - "tags": fields.List(fields.String), - "labels": fields.Raw, -} diff --git a/src/models/asset/host.py b/src/models/asset/host.py new file mode 100644 index 0000000..2100faf --- /dev/null +++ b/src/models/asset/host.py @@ -0,0 +1,30 @@ +from bson import ObjectId +import mongoengine as mongo + +from common.document import DocumentBase +from common.validator import is_ipaddr, is_hex_string + + +class Host(DocumentBase): + + # STATUS = { + # "no": "" + # } + + public_ip = mongo.StringField(max_length=64, required=True, unique=True, validation=is_ipaddr) + private_ip = mongo.StringField(max_length=64, default="") + minion_id = mongo.StringField(max_length=64, default="") # 需要唯一,先留空 + weights = mongo.IntField(default=40) + # status = mongo.StringField() + # spec = mongo.EmbeddedDocumentField(Spec) + + cpu_num = mongo.IntField(default=1) # cpu物理个数 + cpu_core = mongo.IntField(default=1) # 每个cpu的核心数 + memory = mongo.IntField(default=0) # 内存大小,单位GB + data = mongo.DictField(default=dict) + + # 标记和标签 + tags = mongo.ListField(mongo.StringField(), default=list) # tags 默认是空列表 + labels = mongo.DictField(default=dict) + + created = mongo.DateTimeField() diff --git a/src/models/asset/models.py b/src/models/asset/models.py deleted file mode 100644 index 8bfae6d..0000000 --- a/src/models/asset/models.py +++ /dev/null @@ -1,109 +0,0 @@ -from bson import ObjectId -import mongoengine as mongo - -from common.document import DocumentBase -from common.validator import is_ipaddr, is_hex_string - - -class Host(DocumentBase): - - # STATUS = { - # "no": "" - # } - - public_ip = mongo.StringField(max_length=64, required=True, unique=True, validation=is_ipaddr) - private_ip = mongo.StringField(max_length=64, default="") - minion_id = mongo.StringField(max_length=64, default="") # 需要唯一,先留空 - weights = mongo.IntField(default=40) - # status = mongo.StringField() - # spec = mongo.EmbeddedDocumentField(Spec) - - cpu_num = mongo.IntField(default=1) # cpu物理个数 - cpu_core = mongo.IntField(default=1) # 每个cpu的核心数 - memory = mongo.IntField(default=0) # 内存大小,单位GB - data = mongo.DictField(default=dict) - - # 标记和标签 - tags = mongo.ListField(mongo.StringField(), default=list) # tags 默认是空列表 - labels = mongo.DictField(default=dict) - - created = mongo.DateTimeField() - - -class DatabaseServer(DocumentBase): - """数据库服务器实例,包括mysql、redis、mongodb""" - meta = {'allow_inheritance': True} - name = mongo.StringField(required=True) - domain = mongo.StringField(required=False) # 域名连接的地址 - host = mongo.StringField(required=True) # 内网连接地址 - # 管理者,预留 - manage = mongo.StringField(max_length=128, required=True) - data = mongo.DictField(default=dict) - # 标记和标签 - tags = mongo.ListField(mongo.StringField(), default=list) # tags 默认是空列表 - labels = mongo.DictField(default=dict) - - -class Database(mongo.EmbeddedDocument): - """数据库对象的结构描述,一个数据库实例下包含多个数据库,并授权不同用户""" - # 为数据库对象生成id,非主键 - # 查询方式: MySQLInstance.objects.filter(databases__id='61adfcd5726440e0eb8b28bc').first() - id = mongo.ObjectIdField(required=True, default=ObjectId) - name = mongo.StringField(required=True) - username = mongo.StringField(default="") - password = mongo.StringField(default="") - data = mongo.DictField(default=dict) - tags = mongo.ListField(mongo.StringField(), default=list) # tags 默认是空列表 - labels = mongo.DictField(default=dict) - - -class MySQLInstance(DatabaseServer): - """MySQL数据库实例""" - # 库名、连接的用户名密码 - port = mongo.IntField(default=3306) - username = mongo.StringField(max_length=32, required=True) - password = mongo.StringField(max_length=128, required=True) - # 配置 - storage = mongo.IntField(required=True) - memory = mongo.IntField(required=True) - core = mongo.IntField(required=True) - # 实例数据库,包含多个数据库 - databases = mongo.EmbeddedDocumentListField(Database) - - -class RedisInstance(DatabaseServer): - """Redis数据库实例""" - # 内存大小、副本数、密码 - port = mongo.IntField(default=6379) - memory = mongo.IntField(required=True) - replicas = mongo.IntField(required=True, default=0) - password = mongo.StringField(max_length=128, default="") - - -class Middleware(DocumentBase): - """中间件""" - meta = {'allow_inheritance': True} - host = mongo.StringField(required=True) - # 管理者,预留 - manage = mongo.StringField(max_length=128, required=True) - data = mongo.DictField(default=dict) - # 标记和标签 - tags = mongo.ListField(mongo.StringField(), default=list) # tags 默认是空列表 - labels = mongo.DictField(default=dict) - - -class NginxInstance(Middleware): - """nginx服务器""" - port = mongo.IntField(default=80) - url = mongo.StringField(max_length=128, default="/") - - -class CDN(DocumentBase): - """cdn域名""" - meta = {'collection': 'cdn'} - domain = mongo.StringField(max_length=256, required=True) # cdn域名 - manage = mongo.StringField(max_length=128, default="") - data = mongo.DictField(default=dict) - # 标记和标签 - tags = mongo.ListField(mongo.StringField(), default=list) # tags 默认是空列表 - labels = mongo.DictField(default=dict) diff --git a/src/models/project/models.py b/src/models/project/models.py index acce387..ef81e51 100644 --- a/src/models/project/models.py +++ b/src/models/project/models.py @@ -5,7 +5,7 @@ from settings import common from common.document import DocumentBase from common.validator import isalnum, is_hex_string -from models.asset.models import Host, DatabaseServer +from models.asset.host import Host class Project(DocumentBase): @@ -249,73 +249,3 @@ class Server(DocumentBase): if hosts: return queryset.filter(host_id__in=hosts) return cls.objects.none() - - @property - def game_db(self) -> dict: - """给机器的返回,数据库集合是 DatabaseServer 的子类,可以通过它找到,不管是""" - if self.game_db_id: - instance = DatabaseServer.objects(databases__id=self.game_db_id).first() - if not instance: - return {} - database = instance.databases.filter(id=self.game_db_id).first() - data = dict(database.to_mongo()) - if "id" in data: - data.pop("id") - data["host"] = instance.host - data["port"] = instance.port - return data - return {} - - @property - def admin_db(self) -> dict: - """给机器的返回,数据库集合是 DatabaseServer 的子类,可以通过它找到,不管是""" - if self.admin_db_id: - instance = DatabaseServer.objects(databases__id=self.admin_db_id).first() - if not instance: - return {} - database = instance.databases.filter(id=self.admin_db_id).first() - data = dict(database.to_mongo()) - if "id" in data: - data.pop("id") - data["host"] = instance.host - data["port"] = instance.port - return data - return {} - - @property - def game_database(self) -> dict: - """给前端的返回,去除密码等敏感信息""" - pop_item = ["id", "username", "password"] - if self.game_db_id: - instance = DatabaseServer.objects(databases__id=self.game_db_id).first() - if not instance: - return {} - database = instance.databases.filter(id=self.game_db_id).first() - data = dict(database.to_mongo()) - for item in pop_item: - if item in data: - data.pop(item) - data["id"] = str(database.id) - data["host"] = instance.host - data["port"] = instance.port - return data - return {} - - @property - def admin_database(self) -> dict: - """给前端的返回,去除密码等敏感信息""" - pop_item = ["username", "password"] - if self.admin_db_id: - instance = DatabaseServer.objects(databases__id=self.admin_db_id).first() - if not instance: - return {} - database = instance.databases.filter(id=self.admin_db_id).first() - data = dict(database.to_mongo()) - for item in pop_item: - if item in data: - data.pop(item) - data["id"] = str(database.id) - data["host"] = instance.host - data["port"] = instance.port - return data - return {}