mirror of
https://github.com/veops/cmdb.git
synced 2025-08-07 22:17:37 +08:00
Modify code organization
This commit is contained in:
1
cmdb-api/api/commands/__init__.py
Normal file
1
cmdb-api/api/commands/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# -*- coding:utf-8 -*-
|
137
cmdb-api/api/commands/click_cmdb.py
Normal file
137
cmdb-api/api/commands/click_cmdb.py
Normal file
@@ -0,0 +1,137 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
|
||||
import json
|
||||
|
||||
import click
|
||||
from flask import current_app
|
||||
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.const import PermEnum
|
||||
from api.lib.cmdb.const import REDIS_PREFIX_CI
|
||||
from api.lib.cmdb.const import REDIS_PREFIX_CI_RELATION
|
||||
from api.lib.cmdb.const import ResourceTypeEnum
|
||||
from api.lib.cmdb.const import RoleEnum
|
||||
from api.lib.cmdb.const import ValueTypeEnum
|
||||
from api.lib.exception import AbortException
|
||||
from api.lib.perm.acl.acl import ACLManager
|
||||
from api.lib.perm.acl.cache import AppCache
|
||||
from api.lib.perm.acl.resource import ResourceCRUD
|
||||
from api.lib.perm.acl.resource import ResourceTypeCRUD
|
||||
from api.lib.perm.acl.role import RoleCRUD
|
||||
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 PreferenceRelationView
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def init_cache():
|
||||
db.session.remove()
|
||||
|
||||
if current_app.config.get("USE_ES"):
|
||||
from api.extensions import es
|
||||
from api.models.cmdb import Attribute
|
||||
from api.lib.cmdb.utils import ValueTypeMap
|
||||
attributes = Attribute.get_by(to_dict=False)
|
||||
for attr in attributes:
|
||||
other = dict()
|
||||
other['index'] = True if attr.is_index else False
|
||||
if attr.value_type == ValueTypeEnum.TEXT:
|
||||
other['analyzer'] = 'ik_max_word'
|
||||
other['search_analyzer'] = 'ik_smart'
|
||||
if attr.is_index:
|
||||
other["fields"] = {
|
||||
"keyword": {
|
||||
"type": "keyword",
|
||||
"ignore_above": 256
|
||||
}
|
||||
}
|
||||
try:
|
||||
es.update_mapping(attr.name, ValueTypeMap.es_type[attr.value_type], other)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
cis = CI.get_by(to_dict=False)
|
||||
for ci in cis:
|
||||
if current_app.config.get("USE_ES"):
|
||||
res = es.get_index_id(ci.id)
|
||||
if res:
|
||||
continue
|
||||
else:
|
||||
res = rd.get([ci.id], REDIS_PREFIX_CI)
|
||||
if res and list(filter(lambda x: x, res)):
|
||||
continue
|
||||
|
||||
m = api.lib.cmdb.ci.CIManager()
|
||||
ci_dict = m.get_ci_by_id_from_db(ci.id, need_children=False, use_master=False)
|
||||
|
||||
if current_app.config.get("USE_ES"):
|
||||
es.create(ci_dict)
|
||||
else:
|
||||
rd.create_or_update({ci.id: json.dumps(ci_dict)}, REDIS_PREFIX_CI)
|
||||
|
||||
ci_relations = CIRelation.get_by(to_dict=False)
|
||||
relations = dict()
|
||||
for cr in ci_relations:
|
||||
relations.setdefault(cr.first_ci_id, {}).update({cr.second_ci_id: cr.second_ci.type_id})
|
||||
for i in relations:
|
||||
relations[i] = json.dumps(relations[i])
|
||||
if relations:
|
||||
rd.create_or_update(relations, REDIS_PREFIX_CI_RELATION)
|
||||
|
||||
db.session.remove()
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def init_acl():
|
||||
app_id = AppCache.get('cmdb').id
|
||||
# 1. add resource type
|
||||
for resource_type in ResourceTypeEnum.all():
|
||||
try:
|
||||
ResourceTypeCRUD.add(app_id, resource_type, '', PermEnum.all())
|
||||
except AbortException:
|
||||
pass
|
||||
|
||||
# 2. add role
|
||||
try:
|
||||
RoleCRUD.add_role(RoleEnum.CONFIG, app_id, True)
|
||||
except AbortException:
|
||||
pass
|
||||
try:
|
||||
RoleCRUD.add_role(RoleEnum.CMDB_READ_ALL, app_id, False)
|
||||
except AbortException:
|
||||
pass
|
||||
|
||||
# 3. add resource and grant
|
||||
ci_types = CIType.get_by(to_dict=False)
|
||||
type_id = ResourceType.get_by(name=ResourceTypeEnum.CI, first=True, to_dict=False).id
|
||||
for ci_type in ci_types:
|
||||
try:
|
||||
ResourceCRUD.add(ci_type.name, type_id, app_id)
|
||||
except AbortException:
|
||||
pass
|
||||
|
||||
ACLManager().grant_resource_to_role(ci_type.name,
|
||||
RoleEnum.CMDB_READ_ALL,
|
||||
ResourceTypeEnum.CI,
|
||||
[PermEnum.READ])
|
||||
|
||||
relation_views = PreferenceRelationView.get_by(to_dict=False)
|
||||
type_id = ResourceType.get_by(name=ResourceTypeEnum.RELATION_VIEW, first=True, to_dict=False).id
|
||||
for view in relation_views:
|
||||
try:
|
||||
ResourceCRUD.add(view.name, type_id, app_id)
|
||||
except AbortException:
|
||||
pass
|
||||
|
||||
ACLManager().grant_resource_to_role(view.name,
|
||||
RoleEnum.CMDB_READ_ALL,
|
||||
ResourceTypeEnum.RELATION_VIEW,
|
||||
[PermEnum.READ])
|
152
cmdb-api/api/commands/common.py
Normal file
152
cmdb-api/api/commands/common.py
Normal file
@@ -0,0 +1,152 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Click commands."""
|
||||
import os
|
||||
from glob import glob
|
||||
from subprocess import call
|
||||
|
||||
import click
|
||||
from flask import current_app
|
||||
from flask.cli import with_appcontext
|
||||
from werkzeug.exceptions import MethodNotAllowed, NotFound
|
||||
|
||||
from api.extensions import db
|
||||
|
||||
HERE = os.path.abspath(os.path.dirname(__file__))
|
||||
PROJECT_ROOT = os.path.join(HERE, os.pardir, os.pardir)
|
||||
TEST_PATH = os.path.join(PROJECT_ROOT, "tests")
|
||||
|
||||
|
||||
@click.command()
|
||||
def test():
|
||||
"""Run the tests."""
|
||||
import pytest
|
||||
|
||||
rv = pytest.main([TEST_PATH, "--verbose"])
|
||||
exit(rv)
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.option(
|
||||
"-f",
|
||||
"--fix-imports",
|
||||
default=True,
|
||||
is_flag=True,
|
||||
help="Fix imports using isort, before linting",
|
||||
)
|
||||
@click.option(
|
||||
"-c",
|
||||
"--check",
|
||||
default=False,
|
||||
is_flag=True,
|
||||
help="Don't make any changes to files, just confirm they are formatted correctly",
|
||||
)
|
||||
def lint(fix_imports, check):
|
||||
"""Lint and check code style with black, flake8 and isort."""
|
||||
skip = ["node_modules", "requirements", "migrations"]
|
||||
root_files = glob("*.py")
|
||||
root_directories = [
|
||||
name for name in next(os.walk("."))[1] if not name.startswith(".")
|
||||
]
|
||||
files_and_directories = [
|
||||
arg for arg in root_files + root_directories if arg not in skip
|
||||
]
|
||||
|
||||
def execute_tool(description, *args):
|
||||
"""Execute a checking tool with its arguments."""
|
||||
command_line = list(args) + files_and_directories
|
||||
click.echo("{}: {}".format(description, " ".join(command_line)))
|
||||
rv = call(command_line)
|
||||
if rv != 0:
|
||||
exit(rv)
|
||||
|
||||
isort_args = ["-rc"]
|
||||
black_args = []
|
||||
if check:
|
||||
isort_args.append("-c")
|
||||
black_args.append("--check")
|
||||
if fix_imports:
|
||||
execute_tool("Fixing import order", "isort", *isort_args)
|
||||
execute_tool("Formatting style", "black", *black_args)
|
||||
execute_tool("Checking code style", "flake8")
|
||||
|
||||
|
||||
@click.command()
|
||||
def clean():
|
||||
"""Remove *.pyc and *.pyo files recursively starting at current directory.
|
||||
|
||||
Borrowed from Flask-Script, converted to use Click.
|
||||
"""
|
||||
for dirpath, dirnames, filenames in os.walk("."):
|
||||
for filename in filenames:
|
||||
if filename.endswith(".pyc") or filename.endswith(".pyo"):
|
||||
full_pathname = os.path.join(dirpath, filename)
|
||||
click.echo("Removing {}".format(full_pathname))
|
||||
os.remove(full_pathname)
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.option("--url", default=None, help="Url to test (ex. /static/image.png)")
|
||||
@click.option(
|
||||
"--order", default="rule", help="Property on Rule to order by (default: rule)"
|
||||
)
|
||||
@with_appcontext
|
||||
def urls(url, order):
|
||||
"""Display all of the url matching routes for the project.
|
||||
|
||||
Borrowed from Flask-Script, converted to use Click.
|
||||
"""
|
||||
rows = []
|
||||
column_headers = ("Rule", "Endpoint", "Arguments")
|
||||
|
||||
if url:
|
||||
try:
|
||||
rule, arguments = current_app.url_map.bind("localhost").match(
|
||||
url, return_rule=True
|
||||
)
|
||||
rows.append((rule.rule, rule.endpoint, arguments))
|
||||
column_length = 3
|
||||
except (NotFound, MethodNotAllowed) as e:
|
||||
rows.append(("<{}>".format(e), None, None))
|
||||
column_length = 1
|
||||
else:
|
||||
rules = sorted(
|
||||
current_app.url_map.iter_rules(), key=lambda x: getattr(x, order)
|
||||
)
|
||||
for rule in rules:
|
||||
rows.append((rule.rule, rule.endpoint, None))
|
||||
column_length = 2
|
||||
|
||||
str_template = ""
|
||||
table_width = 0
|
||||
|
||||
if column_length >= 1:
|
||||
max_rule_length = max(len(r[0]) for r in rows)
|
||||
max_rule_length = max_rule_length if max_rule_length > 4 else 4
|
||||
str_template += "{:" + str(max_rule_length) + "}"
|
||||
table_width += max_rule_length
|
||||
|
||||
if column_length >= 2:
|
||||
max_endpoint_length = max(len(str(r[1])) for r in rows)
|
||||
max_endpoint_length = max_endpoint_length if max_endpoint_length > 8 else 8
|
||||
str_template += " {:" + str(max_endpoint_length) + "}"
|
||||
table_width += 2 + max_endpoint_length
|
||||
|
||||
if column_length >= 3:
|
||||
max_arguments_length = max(len(str(r[2])) for r in rows)
|
||||
max_arguments_length = max_arguments_length if max_arguments_length > 9 else 9
|
||||
str_template += " {:" + str(max_arguments_length) + "}"
|
||||
table_width += 2 + max_arguments_length
|
||||
|
||||
click.echo(str_template.format(*column_headers[:column_length]))
|
||||
click.echo("-" * table_width)
|
||||
|
||||
for row in rows:
|
||||
click.echo(str_template.format(*row[:column_length]))
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def db_setup():
|
||||
"""create tables
|
||||
"""
|
||||
db.create_all()
|
Reference in New Issue
Block a user