mirror of
https://github.com/veops/cmdb.git
synced 2025-08-07 22:17:37 +08:00
前后端全面升级
This commit is contained in:
69
cmdb-api/api/commands/click_acl.py
Normal file
69
cmdb-api/api/commands/click_acl.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import click
|
||||
from flask.cli import with_appcontext
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def init_acl():
|
||||
from api.models.acl import Role
|
||||
from api.models.acl import App
|
||||
from api.tasks.acl import role_rebuild
|
||||
from api.lib.perm.acl.const import ACL_QUEUE
|
||||
|
||||
roles = Role.get_by(to_dict=False)
|
||||
apps = App.get_by(to_dict=False)
|
||||
for role in roles:
|
||||
if role.app_id:
|
||||
role_rebuild.apply_async(args=(role.id, role.app_id), queue=ACL_QUEUE)
|
||||
else:
|
||||
for app in apps:
|
||||
role_rebuild.apply_async(args=(role.id, app.id), queue=ACL_QUEUE)
|
||||
|
||||
|
||||
# @click.command()
|
||||
# @with_appcontext
|
||||
# def acl_clean():
|
||||
# from api.models.acl import Resource
|
||||
# from api.models.acl import Permission
|
||||
# from api.models.acl import RolePermission
|
||||
#
|
||||
# perms = RolePermission.get_by(to_dict=False)
|
||||
#
|
||||
# for r in perms:
|
||||
# perm = Permission.get_by_id(r.perm_id)
|
||||
# if perm and perm.app_id != r.app_id:
|
||||
# resource_id = r.resource_id
|
||||
# resource = Resource.get_by_id(resource_id)
|
||||
# perm_name = perm.name
|
||||
# existed = Permission.get_by(resource_type_id=resource.resource_type_id, name=perm_name, first=True,
|
||||
# to_dict=False)
|
||||
# if existed is not None:
|
||||
# other = RolePermission.get_by(rid=r.rid, perm_id=existed.id, resource_id=resource_id)
|
||||
# if not other:
|
||||
# r.update(perm_id=existed.id)
|
||||
# else:
|
||||
# r.soft_delete()
|
||||
# else:
|
||||
# r.soft_delete()
|
||||
#
|
||||
#
|
||||
# @click.command()
|
||||
# @with_appcontext
|
||||
# def acl_has_resource_role():
|
||||
# from api.models.acl import Role
|
||||
# from api.models.acl import App
|
||||
# from api.lib.perm.acl.cache import HasResourceRoleCache
|
||||
# from api.lib.perm.acl.role import RoleCRUD
|
||||
#
|
||||
# roles = Role.get_by(to_dict=False)
|
||||
# apps = App.get_by(to_dict=False)
|
||||
# for role in roles:
|
||||
# if role.app_id:
|
||||
# res = RoleCRUD.recursive_resources(role.id, role.app_id)
|
||||
# if res.get('resources') or res.get('groups'):
|
||||
# HasResourceRoleCache.add(role.id, role.app_id)
|
||||
# else:
|
||||
# for app in apps:
|
||||
# res = RoleCRUD.recursive_resources(role.id, app.id)
|
||||
# if res.get('resources') or res.get('groups'):
|
||||
# HasResourceRoleCache.add(role.id, app.id)
|
@@ -1,7 +1,10 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
|
||||
import copy
|
||||
import datetime
|
||||
import json
|
||||
import time
|
||||
|
||||
import click
|
||||
from flask import current_app
|
||||
@@ -10,6 +13,7 @@ from flask.cli import with_appcontext
|
||||
import api.lib.cmdb.ci
|
||||
from api.extensions import db
|
||||
from api.extensions import rd
|
||||
from api.lib.cmdb.ci_type import CITypeTriggerManager
|
||||
from api.lib.cmdb.const import PermEnum
|
||||
from api.lib.cmdb.const import REDIS_PREFIX_CI
|
||||
from api.lib.cmdb.const import REDIS_PREFIX_CI_RELATION
|
||||
@@ -28,12 +32,13 @@ from api.models.acl import ResourceType
|
||||
from api.models.cmdb import CI
|
||||
from api.models.cmdb import CIRelation
|
||||
from api.models.cmdb import CIType
|
||||
from api.models.cmdb import CITypeTrigger
|
||||
from api.models.cmdb import PreferenceRelationView
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def init_cache():
|
||||
def cmdb_init_cache():
|
||||
db.session.remove()
|
||||
|
||||
if current_app.config.get("USE_ES"):
|
||||
@@ -92,7 +97,7 @@ def init_cache():
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def init_acl():
|
||||
def cmdb_init_acl():
|
||||
_app = AppCache.get('cmdb') or App.create(name='cmdb')
|
||||
app_id = _app.id
|
||||
|
||||
@@ -173,7 +178,6 @@ def add_user(user, password, mail, is_admin):
|
||||
assert user is not None
|
||||
assert password is not None
|
||||
assert mail is not None
|
||||
print((user, password, is_admin))
|
||||
UserCRUD.add(username=user, password=password, email=mail, is_admin=is_admin)
|
||||
|
||||
|
||||
@@ -195,3 +199,67 @@ def del_user(user):
|
||||
|
||||
u = User.get_by(username=user, first=True, to_dict=False)
|
||||
u and UserCRUD.delete(u.uid)
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def cmdb_counter():
|
||||
from api.lib.cmdb.cache import CMDBCounterCache
|
||||
|
||||
while True:
|
||||
try:
|
||||
db.session.remove()
|
||||
|
||||
CMDBCounterCache.reset()
|
||||
except:
|
||||
import traceback
|
||||
print(traceback.format_exc())
|
||||
|
||||
time.sleep(60)
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def cmdb_trigger():
|
||||
current_day = datetime.datetime.today().strftime("%Y-%m-%d")
|
||||
trigger2cis = dict()
|
||||
trigger2completed = dict()
|
||||
|
||||
i = 0
|
||||
while True:
|
||||
db.session.remove()
|
||||
if datetime.datetime.today().strftime("%Y-%m-%d") != current_day:
|
||||
trigger2cis = dict()
|
||||
trigger2completed = dict()
|
||||
current_day = datetime.datetime.today().strftime("%Y-%m-%d")
|
||||
|
||||
if i == 360 or i == 0:
|
||||
i = 0
|
||||
try:
|
||||
triggers = CITypeTrigger.get_by(to_dict=False)
|
||||
|
||||
for trigger in triggers:
|
||||
ready_cis = CITypeTriggerManager.waiting_cis(trigger)
|
||||
if trigger.id not in trigger2cis:
|
||||
trigger2cis[trigger.id] = (trigger, ready_cis)
|
||||
else:
|
||||
cur = trigger2cis[trigger.id]
|
||||
cur_ci_ids = {i.ci_id for i in cur[1]}
|
||||
trigger2cis[trigger.id] = (trigger, cur[1] + [i for i in ready_cis if i.ci_id not in cur_ci_ids
|
||||
and i.ci_id not in trigger2completed[trigger.id]])
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
for tid in trigger2cis:
|
||||
trigger, cis = trigger2cis[tid]
|
||||
for ci in copy.deepcopy(cis):
|
||||
if CITypeTriggerManager.trigger_notify(trigger, ci):
|
||||
trigger2completed.setdefault(trigger.id, set()).add(ci.ci_id)
|
||||
|
||||
for _ci in cis:
|
||||
if _ci.ci_id == ci.ci_id:
|
||||
cis.remove(_ci)
|
||||
|
||||
i += 1
|
||||
time.sleep(10)
|
||||
|
@@ -78,7 +78,7 @@ def clean():
|
||||
"""
|
||||
for dirpath, dirnames, filenames in os.walk("."):
|
||||
for filename in filenames:
|
||||
if filename.endswith(".pyc") or filename.endswith(".pyo"):
|
||||
if filename.endswith(".pyc") or filename.endswith(".pyo") or filename.endswith(".c"):
|
||||
full_pathname = os.path.join(dirpath, filename)
|
||||
click.echo("Removing {}".format(full_pathname))
|
||||
os.remove(full_pathname)
|
||||
@@ -110,7 +110,7 @@ def urls(url, order):
|
||||
column_length = 1
|
||||
else:
|
||||
rules = sorted(
|
||||
current_app.url_map.iter_rules(), key=lambda x: getattr(x, order)
|
||||
current_app.url_map.iter_rules(), key=lambda rule: getattr(rule, order)
|
||||
)
|
||||
for rule in rules:
|
||||
rows.append((rule.rule, rule.endpoint, None))
|
||||
|
164
cmdb-api/api/commands/init_common_setting.py
Normal file
164
cmdb-api/api/commands/init_common_setting.py
Normal file
@@ -0,0 +1,164 @@
|
||||
import click
|
||||
from flask import current_app
|
||||
from flask.cli import with_appcontext
|
||||
from werkzeug.datastructures import MultiDict
|
||||
|
||||
from api.lib.common_setting.acl import ACLManager
|
||||
from api.lib.common_setting.employee import EmployeeAddForm
|
||||
from api.lib.common_setting.resp_format import ErrFormat
|
||||
from api.models.common_setting import Employee, Department
|
||||
|
||||
|
||||
class InitEmployee(object):
|
||||
"""
|
||||
初始化员工
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.log = current_app.logger
|
||||
|
||||
def import_user_from_acl(self):
|
||||
"""
|
||||
从ACL导入用户
|
||||
"""
|
||||
|
||||
acl = ACLManager('acl')
|
||||
user_list = acl.get_all_users()
|
||||
|
||||
username_list = [e['username'] for e in Employee.get_by()]
|
||||
|
||||
for user in user_list:
|
||||
if user['username'] in username_list:
|
||||
continue
|
||||
try:
|
||||
form = EmployeeAddForm(MultiDict(user))
|
||||
if not form.validate():
|
||||
raise Exception(
|
||||
','.join(['{}: {}'.format(filed, ','.join(msg)) for filed, msg in form.errors.items()]))
|
||||
data = form.data
|
||||
data['acl_uid'] = user['uid']
|
||||
data['block'] = 1 if user['block'] else 0
|
||||
data.pop('password')
|
||||
Employee.create(
|
||||
**data
|
||||
)
|
||||
except Exception as e:
|
||||
self.log.error(ErrFormat.acl_import_user_failed.format(user['username'], str(e)))
|
||||
self.log.error(e)
|
||||
|
||||
|
||||
class InitDepartment(object):
|
||||
def __init__(self):
|
||||
self.log = current_app.logger
|
||||
|
||||
def init(self):
|
||||
self.init_wide_company()
|
||||
|
||||
def hard_delete(self, department_id, department_name):
|
||||
existed_deleted_list = Department.query.filter(
|
||||
Department.department_name == department_name,
|
||||
Department.department_id == department_id,
|
||||
Department.deleted == 1,
|
||||
).all()
|
||||
for existed in existed_deleted_list:
|
||||
existed.delete()
|
||||
|
||||
def get_department(self, department_name):
|
||||
return Department.query.filter(
|
||||
Department.department_name == department_name,
|
||||
Department.deleted == 0,
|
||||
).order_by(Department.created_at.asc()).first()
|
||||
|
||||
def run(self, department_id, department_name, department_parent_id):
|
||||
self.hard_delete(department_id, department_name)
|
||||
|
||||
res = self.get_department(department_name)
|
||||
if res:
|
||||
if res.department_id == department_id:
|
||||
return
|
||||
else:
|
||||
new_d = res.update(
|
||||
department_id=department_id,
|
||||
department_parent_id=department_parent_id,
|
||||
)
|
||||
return
|
||||
|
||||
Department.create(
|
||||
department_id=department_id,
|
||||
department_name=department_name,
|
||||
department_parent_id=department_parent_id,
|
||||
)
|
||||
new_d = self.get_department(department_name)
|
||||
|
||||
if new_d.department_id != department_id:
|
||||
new_d = new_d.update(
|
||||
department_id=department_id,
|
||||
department_parent_id=department_parent_id,
|
||||
)
|
||||
self.log.info(f"初始化 {department_name} 部门成功.")
|
||||
|
||||
def run_common(self, department_id, department_name, department_parent_id):
|
||||
try:
|
||||
self.run(department_id, department_name, department_parent_id)
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"init {department_name} err:")
|
||||
current_app.logger.error(e)
|
||||
raise Exception(e)
|
||||
|
||||
def init_wide_company(self):
|
||||
"""
|
||||
创建 id 0, name 全公司 的部门
|
||||
"""
|
||||
department_id = 0
|
||||
department_name = '全公司'
|
||||
department_parent_id = -1
|
||||
|
||||
self.run_common(department_id, department_name, department_parent_id)
|
||||
|
||||
def create_acl_role_with_department(self):
|
||||
"""
|
||||
当前所有部门,在ACL创建 role
|
||||
"""
|
||||
acl = ACLManager('acl')
|
||||
role_name_map = {role['name']: role for role in acl.get_all_roles()}
|
||||
|
||||
d_list = Department.query.filter(
|
||||
Department.deleted == 0, Department.department_parent_id != -1).all()
|
||||
for department in d_list:
|
||||
if department.acl_rid > 0:
|
||||
continue
|
||||
|
||||
role = role_name_map.get(department.department_name)
|
||||
if role is None:
|
||||
payload = {
|
||||
'app_id': 'acl',
|
||||
'name': department.department_name,
|
||||
}
|
||||
role = acl.create_role(payload)
|
||||
|
||||
acl_rid = role.get('id') if role else 0
|
||||
|
||||
department.update(
|
||||
acl_rid=acl_rid
|
||||
)
|
||||
info = f"update department acl_rid: {acl_rid}"
|
||||
current_app.logger.info(info)
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def init_import_user_from_acl():
|
||||
"""
|
||||
从ACL导入用户
|
||||
"""
|
||||
InitEmployee().import_user_from_acl()
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def init_department():
|
||||
"""
|
||||
初始化 部门
|
||||
"""
|
||||
InitDepartment().init()
|
||||
InitDepartment().create_acl_role_with_department()
|
Reference in New Issue
Block a user