codosdk 1.0.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codosdk-1.0.0.data/data/share/doc/codo_sdk/README.md +50 -0
- codosdk-1.0.0.dist-info/LICENSE +674 -0
- codosdk-1.0.0.dist-info/METADATA +35 -0
- codosdk-1.0.0.dist-info/RECORD +45 -0
- codosdk-1.0.0.dist-info/WHEEL +6 -0
- codosdk-1.0.0.dist-info/top_level.txt +2 -0
- opssdk/__init__.py +0 -0
- opssdk/utils/__init__.py +24 -0
- websdk2/__init__.py +0 -0
- websdk2/api_set.py +43 -0
- websdk2/apis/__init__.py +9 -0
- websdk2/apis/admin_apis.py +160 -0
- websdk2/apis/agent_apis.py +44 -0
- websdk2/apis/cmdb_apis.py +22 -0
- websdk2/apis/kerrigan_apis.py +21 -0
- websdk2/apis/mgv4_apis.py +95 -0
- websdk2/apis/task_apis.py +80 -0
- websdk2/application.py +77 -0
- websdk2/base_handler.py +275 -0
- websdk2/cache.py +128 -0
- websdk2/cache_context.py +38 -0
- websdk2/client.py +173 -0
- websdk2/cloud/__init__.py +0 -0
- websdk2/cloud/qcloud_api.py +57 -0
- websdk2/cloud/ucloud_api.py +73 -0
- websdk2/cloud_utils.py +26 -0
- websdk2/configs.py +97 -0
- websdk2/consts.py +212 -0
- websdk2/db_context.py +150 -0
- websdk2/error.py +50 -0
- websdk2/fetch_coroutine.py +12 -0
- websdk2/jwt_token.py +135 -0
- websdk2/ldap.py +117 -0
- websdk2/logger.py +42 -0
- websdk2/model_utils.py +156 -0
- websdk2/mqhelper.py +120 -0
- websdk2/program.py +20 -0
- websdk2/salt_api.py +107 -0
- websdk2/sqlalchemy_pagination.py +73 -0
- websdk2/tools.py +198 -0
- websdk2/utils/__init__.py +257 -0
- websdk2/utils/cc_crypto.py +85 -0
- websdk2/utils/date_format.py +35 -0
- websdk2/utils/pydantic_utils.py +55 -0
- websdk2/web_logs.py +127 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Version : 0.0.1
|
|
5
|
+
Contact : 191715030@qq.com
|
|
6
|
+
Author : shenshuo
|
|
7
|
+
Date : 2023/4/17 17:07
|
|
8
|
+
Desc : 加密
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import base64
|
|
12
|
+
from cryptography.fernet import Fernet
|
|
13
|
+
from ..consts import const
|
|
14
|
+
from ..configs import configs
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class AESCryptoV3:
|
|
18
|
+
"""
|
|
19
|
+
usage: mc = AESCryptoV3() 实例化
|
|
20
|
+
mc.my_encrypt('ceshi') 对字符串ceshi进行加密
|
|
21
|
+
mc.my_decrypt('') 对密文进行解密
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, key: str = 'W1zFCF-pnUXi1zRtfgNkHmM3qv_3zvCkVSx68vXqks4='):
|
|
25
|
+
# 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度
|
|
26
|
+
if not isinstance(key, bytes): key = key.encode('utf-8')
|
|
27
|
+
if len(key) > 32:
|
|
28
|
+
key = key[0:32]
|
|
29
|
+
else:
|
|
30
|
+
key = key.rjust(32, b'0')
|
|
31
|
+
|
|
32
|
+
self.key = base64.urlsafe_b64encode(key)
|
|
33
|
+
self.f = Fernet(self.key)
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def create_key(self):
|
|
37
|
+
return Fernet.generate_key()
|
|
38
|
+
|
|
39
|
+
def my_encrypt(self, text: str):
|
|
40
|
+
if isinstance(text, str): text = text.encode('utf-8')
|
|
41
|
+
return self.f.encrypt(text).decode('utf-8')
|
|
42
|
+
|
|
43
|
+
def my_decrypt(self, text: str):
|
|
44
|
+
if isinstance(text, str): text = text.encode('utf-8')
|
|
45
|
+
return self.f.decrypt(text).decode('utf-8')
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class AESCryptoV4:
|
|
49
|
+
"""
|
|
50
|
+
usage: mc = AESCryptoV4() 实例化
|
|
51
|
+
mc.my_encrypt('ceshi') 对字符串ceshi进行加密
|
|
52
|
+
mc.my_decrypt('') 对密文进行解密
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, key: str = None):
|
|
56
|
+
# 如果没有提供密钥,则使用默认密钥
|
|
57
|
+
if key is None:
|
|
58
|
+
# 若没有提供密钥,则生成一个新密钥
|
|
59
|
+
key = configs.get(const.AES_CRYPTO_KEY, 'W1zFCF-pnUXi1zRtfgNkHmM3qv_3zvCkVSx68vXqks4=')
|
|
60
|
+
|
|
61
|
+
# 确保密钥是字节类型
|
|
62
|
+
if not isinstance(key, bytes):
|
|
63
|
+
key = key.encode('utf-8')
|
|
64
|
+
|
|
65
|
+
if len(key) > 32:
|
|
66
|
+
key = key[0:32]
|
|
67
|
+
else:
|
|
68
|
+
key = key.rjust(32, b'0')
|
|
69
|
+
# 创建Fernet对象
|
|
70
|
+
self.f = Fernet(base64.urlsafe_b64encode(key))
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def create_key(self):
|
|
74
|
+
return Fernet.generate_key()
|
|
75
|
+
|
|
76
|
+
def my_encrypt(self, text: str):
|
|
77
|
+
if isinstance(text, str): text = text.encode('utf-8')
|
|
78
|
+
return self.f.encrypt(text).decode('utf-8')
|
|
79
|
+
|
|
80
|
+
def my_decrypt(self, text: str):
|
|
81
|
+
if isinstance(text, str): text = text.encode('utf-8')
|
|
82
|
+
return self.f.decrypt(text).decode('utf-8')
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
mcv4 = AESCryptoV4()
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*-coding:utf-8-*-
|
|
3
|
+
""""
|
|
4
|
+
Author : shenshuo
|
|
5
|
+
Date : 2023年2月5日13:37:54
|
|
6
|
+
Desc : 日期时间格式化处理
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from datetime import datetime, timedelta
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def date_format_to8(start_date: str = None, end_date: str = None) -> tuple:
|
|
13
|
+
"""
|
|
14
|
+
# iview 前端日期时间段数据格式化
|
|
15
|
+
start_time_tuple, end_time_tuple = date_format_to8(start_date, end_date)
|
|
16
|
+
# 查询语句中使用
|
|
17
|
+
session.query(dbA).filter(dbA.create_time.between(start_time_tuple, end_time_tuple).all()
|
|
18
|
+
"""
|
|
19
|
+
date_format_1 = "%Y-%m-%dT%H:%M:%S.%fZ"
|
|
20
|
+
date_format_2 = "%Y-%m-%d"
|
|
21
|
+
|
|
22
|
+
if not start_date:
|
|
23
|
+
start_date = (datetime.now() - timedelta(days=30)).strftime(date_format_2)
|
|
24
|
+
if not end_date:
|
|
25
|
+
end_date = (datetime.now() + timedelta(days=1)).strftime(date_format_2)
|
|
26
|
+
|
|
27
|
+
for date_format in [date_format_1, date_format_2]:
|
|
28
|
+
try:
|
|
29
|
+
start_time_tuple = datetime.strptime(start_date, date_format) + timedelta(hours=8)
|
|
30
|
+
end_time_tuple = datetime.strptime(end_date, date_format) + timedelta(hours=8)
|
|
31
|
+
return start_time_tuple, end_time_tuple
|
|
32
|
+
except ValueError:
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
raise ValueError(f"Unable to parse the dates. Expected formats are {date_format_1} and {date_format_2}.")
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Version : 0.0.8
|
|
5
|
+
Contact : 191715030@qq.com
|
|
6
|
+
Author : shenshuo
|
|
7
|
+
Date : 2021/1/26 20:28
|
|
8
|
+
Desc : https://github.com/tiangolo/pydantic-sqlalchemy
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
####
|
|
12
|
+
from typing import Container, Optional, Type
|
|
13
|
+
from pydantic import BaseConfig, BaseModel, create_model, ValidationError
|
|
14
|
+
from sqlalchemy.inspection import inspect
|
|
15
|
+
from sqlalchemy.orm.properties import ColumnProperty
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### 删除的时候一般只有id
|
|
19
|
+
class PydanticDel(BaseModel):
|
|
20
|
+
id: int
|
|
21
|
+
|
|
22
|
+
class PydanticDelList(BaseModel):
|
|
23
|
+
id_list: list[int]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class OrmConfig(BaseConfig):
|
|
27
|
+
orm_mode = True
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def sqlalchemy_to_pydantic(db_model: Type, *, config: Type = OrmConfig, exclude: Container[str] = []) -> Type[
|
|
31
|
+
BaseModel]:
|
|
32
|
+
mapper = inspect(db_model)
|
|
33
|
+
fields = {}
|
|
34
|
+
for attr in mapper.attrs:
|
|
35
|
+
if isinstance(attr, ColumnProperty):
|
|
36
|
+
if attr.columns:
|
|
37
|
+
name = attr.key
|
|
38
|
+
if name in exclude:
|
|
39
|
+
continue
|
|
40
|
+
column = attr.columns[0]
|
|
41
|
+
python_type: Optional[type] = None
|
|
42
|
+
if hasattr(column.type, "impl"):
|
|
43
|
+
if hasattr(column.type.impl, "python_type"):
|
|
44
|
+
python_type = column.type.impl.python_type
|
|
45
|
+
elif hasattr(column.type, "python_type"):
|
|
46
|
+
python_type = column.type.python_type
|
|
47
|
+
assert python_type, f"Could not infer python_type for {column}"
|
|
48
|
+
default = None
|
|
49
|
+
if column.default is None and not column.nullable:
|
|
50
|
+
default = ...
|
|
51
|
+
fields[name] = (python_type, default)
|
|
52
|
+
pydantic_model = create_model(
|
|
53
|
+
db_model.__name__, __config__=config, **fields # type: ignore
|
|
54
|
+
)
|
|
55
|
+
return pydantic_model
|
websdk2/web_logs.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*-coding:utf-8-*-
|
|
3
|
+
'''
|
|
4
|
+
Author : ss
|
|
5
|
+
date : 2018-3-19
|
|
6
|
+
role : web log
|
|
7
|
+
'''
|
|
8
|
+
|
|
9
|
+
import logging
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
import time
|
|
13
|
+
import tornado.log
|
|
14
|
+
from shortuuid import uuid
|
|
15
|
+
|
|
16
|
+
log_fmt = ''.join(('PROGRESS:%(progress_id) -5s %(levelname) ', '-10s %(asctime)s %(name) -25s %(funcName) '
|
|
17
|
+
'-30s LINE.NO:%(lineno) -5d : %(message)s'))
|
|
18
|
+
log_key = 'logger_key'
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def singleton(class_):
|
|
22
|
+
instances = {}
|
|
23
|
+
|
|
24
|
+
def getinstance(*args, **kwargs):
|
|
25
|
+
if class_ not in instances:
|
|
26
|
+
instances[class_] = class_(*args, **kwargs)
|
|
27
|
+
return instances[class_]
|
|
28
|
+
|
|
29
|
+
return getinstance
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ProgressLogFilter(logging.Filter):
|
|
33
|
+
def filter(self, record):
|
|
34
|
+
record.progress_id = Logger().progress_id
|
|
35
|
+
return True
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@singleton
|
|
39
|
+
class Logger(object):
|
|
40
|
+
def __init__(self, progress_id='', log_file='/tmp/xxx.log'):
|
|
41
|
+
self.__log_key = log_key
|
|
42
|
+
self.progress_id = progress_id
|
|
43
|
+
self.log_file = log_file
|
|
44
|
+
|
|
45
|
+
def read_log(self, log_level, log_message):
|
|
46
|
+
###创建一个logger
|
|
47
|
+
if self.progress_id == '':
|
|
48
|
+
Logger().progress_id = str(uuid())
|
|
49
|
+
else:
|
|
50
|
+
Logger().progress_id = self.progress_id
|
|
51
|
+
logger = logging.getLogger(self.__log_key)
|
|
52
|
+
logger.addFilter(ProgressLogFilter())
|
|
53
|
+
logger.setLevel(logging.DEBUG)
|
|
54
|
+
|
|
55
|
+
###创建一个handler用于输出到终端
|
|
56
|
+
th = logging.StreamHandler()
|
|
57
|
+
th.setLevel(logging.DEBUG)
|
|
58
|
+
|
|
59
|
+
###定义handler的输出格式
|
|
60
|
+
formatter = logging.Formatter(log_fmt)
|
|
61
|
+
th.setFormatter(formatter)
|
|
62
|
+
|
|
63
|
+
###给logger添加handler
|
|
64
|
+
logger.addHandler(th)
|
|
65
|
+
|
|
66
|
+
###记录日志
|
|
67
|
+
level_dic = {'debug': logger.debug, 'info': logger.info, 'warning': logger.warning, 'error': logger.error,
|
|
68
|
+
'critical': logger.critical}
|
|
69
|
+
level_dic[log_level](log_message)
|
|
70
|
+
|
|
71
|
+
th.flush()
|
|
72
|
+
logger.removeHandler(th)
|
|
73
|
+
|
|
74
|
+
def write_log(self, log_level, log_message):
|
|
75
|
+
###创建一个logger
|
|
76
|
+
###创建一个logger
|
|
77
|
+
if self.progress_id == '':
|
|
78
|
+
Logger().progress_id = str(uuid())
|
|
79
|
+
else:
|
|
80
|
+
Logger().progress_id = self.progress_id
|
|
81
|
+
logger = logging.getLogger(self.__log_key)
|
|
82
|
+
logger.addFilter(ProgressLogFilter())
|
|
83
|
+
logger.setLevel(logging.DEBUG)
|
|
84
|
+
|
|
85
|
+
###建立日志目录
|
|
86
|
+
log_dir = os.path.dirname(self.log_file)
|
|
87
|
+
if not os.path.isdir(log_dir):
|
|
88
|
+
os.makedirs(log_dir)
|
|
89
|
+
|
|
90
|
+
###创建一个handler用于写入日志文件
|
|
91
|
+
fh = logging.FileHandler(self.log_file)
|
|
92
|
+
fh.setLevel(logging.DEBUG)
|
|
93
|
+
|
|
94
|
+
###定义handler的输出格式
|
|
95
|
+
formatter = logging.Formatter(log_fmt)
|
|
96
|
+
fh.setFormatter(formatter)
|
|
97
|
+
|
|
98
|
+
###给logger添加handler
|
|
99
|
+
logger.addHandler(fh)
|
|
100
|
+
|
|
101
|
+
###记录日志
|
|
102
|
+
level_dic = {'debug': logger.debug, 'info': logger.info, 'warning': logger.warning, 'error': logger.error,
|
|
103
|
+
'critical': logger.critical}
|
|
104
|
+
level_dic[log_level](log_message)
|
|
105
|
+
|
|
106
|
+
###删除重复记录
|
|
107
|
+
fh.flush()
|
|
108
|
+
logger.removeHandler(fh)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
ins_log = Logger()
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def timeit(func):
|
|
115
|
+
def wrapper(*args, **kwargs):
|
|
116
|
+
start_time = time.time()
|
|
117
|
+
result = func(*args, **kwargs)
|
|
118
|
+
end_time = time.time()
|
|
119
|
+
duration = end_time - start_time
|
|
120
|
+
ins_log.read_log('info', '%s execute duration :%.3f second' % (str(func), duration))
|
|
121
|
+
return result
|
|
122
|
+
|
|
123
|
+
return wrapper
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
ins_log.write_log('info', 'xxxx')
|
|
127
|
+
ins_log.read_log('info', 'xxxx')
|