Skip to content

Commit

Permalink
Merge branch 'master' of github.com:veops/cmdb into dev_ui
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-liang0615 committed Jul 25, 2023
2 parents 662facc + 005be47 commit b4bdc33
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ docker-compose up -d

---

_**欢迎关注我们的公众号,点击联系我们,加入微信、qq运维群,获得更多产品、行业相关资讯**_
_**欢迎关注我们的公众号,点击联系我们,加入微信、qq运维群(336164978),获得更多产品、行业相关资讯**_

![公众号](docs/images/qrcode_for_gzh.jpg)
3 changes: 3 additions & 0 deletions cmdb-api/api/commands/click_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
@click.command()
@with_appcontext
def init_acl():
"""
acl init
"""
from api.models.acl import Role
from api.models.acl import App
from api.tasks.acl import role_rebuild
Expand Down
37 changes: 37 additions & 0 deletions cmdb-api/api/commands/click_cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from api.lib.perm.acl.user import UserCRUD
from api.models.acl import App
from api.models.acl import ResourceType
from api.models.cmdb import Attribute
from api.models.cmdb import CI
from api.models.cmdb import CIRelation
from api.models.cmdb import CIType
Expand Down Expand Up @@ -200,6 +201,9 @@ def del_user(user):
@click.command()
@with_appcontext
def cmdb_counter():
"""
Dashboard calculations
"""
from api.lib.cmdb.cache import CMDBCounterCache

while True:
Expand All @@ -217,6 +221,9 @@ def cmdb_counter():
@click.command()
@with_appcontext
def cmdb_trigger():
"""
Trigger execution
"""
current_day = datetime.datetime.today().strftime("%Y-%m-%d")
trigger2cis = dict()
trigger2completed = dict()
Expand Down Expand Up @@ -259,3 +266,33 @@ def cmdb_trigger():

i += 1
time.sleep(10)


@click.command()
@with_appcontext
def cmdb_index_table_upgrade():
"""
Migrate data from tables c_value_integers, c_value_floats, and c_value_datetime
"""
for attr in Attribute.get_by(to_dict=False):
if attr.value_type not in {ValueTypeEnum.TEXT, ValueTypeEnum.JSON}:
attr.update(is_index=True)

from api.models.cmdb import CIValueInteger, CIIndexValueInteger
from api.models.cmdb import CIValueFloat, CIIndexValueFloat
from api.models.cmdb import CIValueDateTime, CIIndexValueDateTime

for i in CIValueInteger.get_by(to_dict=False):
CIIndexValueInteger.create(ci_id=i.ci_id, attr_id=i.attr_id, value=i.value, commit=False)
i.delete(commit=False)
db.session.commit()

for i in CIValueFloat.get_by(to_dict=False):
CIIndexValueFloat.create(ci_id=i.ci_id, attr_id=i.attr_id, value=i.value, commit=False)
i.delete(commit=False)
db.session.commit()

for i in CIValueDateTime.get_by(to_dict=False):
CIIndexValueDateTime.create(ci_id=i.ci_id, attr_id=i.attr_id, value=i.value, commit=False)
i.delete(commit=False)
db.session.commit()
6 changes: 3 additions & 3 deletions cmdb-api/api/commands/init_common_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):

def import_user_from_acl(self):
"""
从ACL导入用户
Import users from ACL
"""

acl = ACLManager('acl')
Expand Down Expand Up @@ -149,7 +149,7 @@ def create_acl_role_with_department(self):
@with_appcontext
def init_import_user_from_acl():
"""
从ACL导入用户
Import users from ACL
"""
InitEmployee().import_user_from_acl()

Expand All @@ -158,7 +158,7 @@ def init_import_user_from_acl():
@with_appcontext
def init_department():
"""
初始化 部门
Department initialization
"""
InitDepartment().init()
InitDepartment().create_acl_role_with_department()
4 changes: 4 additions & 0 deletions cmdb-api/api/lib/cmdb/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from api.lib.perm.acl.acl import is_app_admin
from api.lib.perm.acl.acl import validate_permission
from api.models.cmdb import Attribute
from api.models.cmdb import CIType
from api.models.cmdb import CITypeAttribute
from api.models.cmdb import CITypeAttributeGroupItem
from api.models.cmdb import PreferenceShowAttributes
Expand Down Expand Up @@ -315,6 +316,9 @@ def delete(_id):
attr = Attribute.get_by_id(_id) or abort(404, ErrFormat.attribute_not_found.format("id={}".format(_id)))
name = attr.name

if CIType.get_by(unique_id=attr.id, first=True, to_dict=False) is not None:
return abort(400, ErrFormat.attribute_is_unique_id)

if attr.uid and attr.uid != g.user.uid:
return abort(403, ErrFormat.cannot_delete_attribute)

Expand Down
1 change: 1 addition & 0 deletions cmdb-api/api/lib/cmdb/resp_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ErrFormat(CommonErrFormat):
argument_file_not_found = "文件似乎并未上传"

attribute_not_found = "属性 {} 不存在!"
attribute_is_unique_id = "该属性是模型的唯一标识,不能被删除!"
attribute_value_type_cannot_change = "属性的值类型不允许修改!"
attribute_list_value_cannot_change = "多值不被允许修改!"
attribute_index_cannot_change = "修改索引 非管理员不被允许!"
Expand Down
26 changes: 13 additions & 13 deletions cmdb-api/api/lib/cmdb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import datetime
import json
import re

import six
from markupsafe import escape
Expand All @@ -12,6 +13,8 @@
from api.lib.cmdb.cache import AttributeCache
from api.lib.cmdb.const import ValueTypeEnum

TIME_RE = re.compile(r"^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$")


def string2int(x):
return int(float(x))
Expand All @@ -31,7 +34,7 @@ class ValueTypeMap(object):
ValueTypeEnum.INT: string2int,
ValueTypeEnum.FLOAT: float,
ValueTypeEnum.TEXT: lambda x: escape(x).encode('utf-8').decode('utf-8'),
ValueTypeEnum.TIME: lambda x: escape(x).encode('utf-8').decode('utf-8'),
ValueTypeEnum.TIME: lambda x: TIME_RE.findall(escape(x).encode('utf-8').decode('utf-8'))[0],
ValueTypeEnum.DATETIME: str2datetime,
ValueTypeEnum.DATE: str2datetime,
ValueTypeEnum.JSON: lambda x: json.loads(x) if isinstance(x, six.string_types) and x else x,
Expand Down Expand Up @@ -61,15 +64,11 @@ class ValueTypeMap(object):
ValueTypeEnum.INT: model.IntegerChoice,
ValueTypeEnum.FLOAT: model.FloatChoice,
ValueTypeEnum.TEXT: model.TextChoice,
ValueTypeEnum.TIME: model.TextChoice,
}

table = {
ValueTypeEnum.INT: model.CIValueInteger,
ValueTypeEnum.TEXT: model.CIValueText,
ValueTypeEnum.DATETIME: model.CIValueDateTime,
ValueTypeEnum.DATE: model.CIValueDateTime,
ValueTypeEnum.TIME: model.CIValueText,
ValueTypeEnum.FLOAT: model.CIValueFloat,
ValueTypeEnum.JSON: model.CIValueJson,
'index_{0}'.format(ValueTypeEnum.INT): model.CIIndexValueInteger,
'index_{0}'.format(ValueTypeEnum.TEXT): model.CIIndexValueText,
Expand All @@ -81,12 +80,7 @@ class ValueTypeMap(object):
}

table_name = {
ValueTypeEnum.INT: 'c_value_integers',
ValueTypeEnum.TEXT: 'c_value_texts',
ValueTypeEnum.DATETIME: 'c_value_datetime',
ValueTypeEnum.DATE: 'c_value_datetime',
ValueTypeEnum.TIME: 'c_value_texts',
ValueTypeEnum.FLOAT: 'c_value_floats',
ValueTypeEnum.JSON: 'c_value_json',
'index_{0}'.format(ValueTypeEnum.INT): 'c_value_index_integers',
'index_{0}'.format(ValueTypeEnum.TEXT): 'c_value_index_texts',
Expand Down Expand Up @@ -117,17 +111,23 @@ def __init__(self, attr_name=None, attr=None, is_index=None):
@property
def table(self):
attr = AttributeCache.get(self.attr_name) if not self.attr else self.attr
if self.is_index is None:
if attr.value_type != ValueTypeEnum.TEXT and attr.value_type != ValueTypeEnum.JSON:
self.is_index = True
elif self.is_index is None:
self.is_index = attr.is_index

i = "index_{0}".format(attr.value_type) if self.is_index else attr.value_type

return ValueTypeMap.table.get(i)

@property
def table_name(self):
attr = AttributeCache.get(self.attr_name) if not self.attr else self.attr
if self.is_index is None:
if attr.value_type != ValueTypeEnum.TEXT and attr.value_type != ValueTypeEnum.JSON:
self.is_index = True
elif self.is_index is None:
self.is_index = attr.is_index

i = "index_{0}".format(attr.value_type) if self.is_index else attr.value_type

return ValueTypeMap.table_name.get(i)
9 changes: 9 additions & 0 deletions cmdb-api/api/models/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ class CIIndexValueDateTime(Model):


class CIValueInteger(Model):
"""
Deprecated in a future version
"""
__tablename__ = "c_value_integers"

ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
Expand All @@ -261,6 +264,9 @@ class CIValueInteger(Model):


class CIValueFloat(Model):
"""
Deprecated in a future version
"""
__tablename__ = "c_value_floats"

ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
Expand All @@ -283,6 +289,9 @@ class CIValueText(Model):


class CIValueDateTime(Model):
"""
Deprecated in a future version
"""
__tablename__ = "c_value_datetime"

ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ services:
gunicorn --workers=3 autoapp:app -b 0.0.0.0:5000 -D
flask cmdb-init-cache
flask cmdb-init-acl
flask cmdb-counter
nohup flask cmdb-counter > counter.log 2>&1 &
celery worker -A celery_worker.celery -E -Q one_cmdb_async --concurrency=2 -D
celery worker -A celery_worker.celery -E -Q acl_async --concurrency=2
Expand Down

0 comments on commit b4bdc33

Please sign in to comment.