pixelarraylib 1.0.0__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.
- arraylib/__init__.py +36 -0
- arraylib/__main__.py +126 -0
- arraylib/aliyun/__init__.py +0 -0
- arraylib/aliyun/aliyun_email.py +130 -0
- arraylib/aliyun/billing.py +477 -0
- arraylib/aliyun/content_scanner.py +253 -0
- arraylib/aliyun/domain.py +434 -0
- arraylib/aliyun/eci.py +47 -0
- arraylib/aliyun/ecs.py +68 -0
- arraylib/aliyun/fc.py +142 -0
- arraylib/aliyun/oss.py +649 -0
- arraylib/aliyun/sms.py +59 -0
- arraylib/aliyun/sts.py +124 -0
- arraylib/db_utils/mysql.py +544 -0
- arraylib/db_utils/redis.py +373 -0
- arraylib/decorators/__init__.py +13 -0
- arraylib/decorators/decorators.py +194 -0
- arraylib/gitlab/__init__.py +0 -0
- arraylib/gitlab/code_analyzer.py +344 -0
- arraylib/gitlab/pypi_package_manager.py +61 -0
- arraylib/monitor/__init__.py +0 -0
- arraylib/monitor/feishu.py +132 -0
- arraylib/net/request.py +143 -0
- arraylib/scripts/__init__.py +22 -0
- arraylib/scripts/collect_code_to_txt.py +327 -0
- arraylib/scripts/create_test_case_files.py +100 -0
- arraylib/scripts/nginx_proxy_to_ecs.py +119 -0
- arraylib/scripts/remove_empty_lines.py +120 -0
- arraylib/scripts/summary_code_count.py +430 -0
- arraylib/system/__init__.py +0 -0
- arraylib/system/common.py +390 -0
- pixelarraylib-1.0.0.dist-info/METADATA +141 -0
- pixelarraylib-1.0.0.dist-info/RECORD +37 -0
- pixelarraylib-1.0.0.dist-info/WHEEL +5 -0
- pixelarraylib-1.0.0.dist-info/entry_points.txt +2 -0
- pixelarraylib-1.0.0.dist-info/licenses/LICENSE +21 -0
- pixelarraylib-1.0.0.dist-info/top_level.txt +1 -0
arraylib/aliyun/eci.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import traceback
|
|
3
|
+
from typing import Optional, Dict, Any, List
|
|
4
|
+
from alibabacloud_eci20180808.client import Client as EciClient
|
|
5
|
+
from alibabacloud_eci20180808.models import (
|
|
6
|
+
CreateContainerGroupRequest,
|
|
7
|
+
DescribeContainerGroupsRequest,
|
|
8
|
+
DeleteContainerGroupRequest,
|
|
9
|
+
UpdateContainerGroupRequest,
|
|
10
|
+
RestartContainerGroupRequest,
|
|
11
|
+
)
|
|
12
|
+
from alibabacloud_tea_openapi import models as open_api_models
|
|
13
|
+
from alibabacloud_tea_util import models as util_models
|
|
14
|
+
from arraylib.monitor.feishu import Feishu
|
|
15
|
+
|
|
16
|
+
feishu_alert = Feishu("devtoolkit服务报警")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ECIUtils:
|
|
20
|
+
def __init__(self, region_id: str, access_key_id: str, access_key_secret: str):
|
|
21
|
+
"""
|
|
22
|
+
description:
|
|
23
|
+
初始化ECI工具类
|
|
24
|
+
parameters:
|
|
25
|
+
region_id(str): 地域ID
|
|
26
|
+
access_key_id(str): 访问密钥ID
|
|
27
|
+
access_key_secret(str): 访问密钥Secret
|
|
28
|
+
"""
|
|
29
|
+
self.region_id = region_id
|
|
30
|
+
self.access_key_id = access_key_id
|
|
31
|
+
self.access_key_secret = access_key_secret
|
|
32
|
+
self.client = self._create_client()
|
|
33
|
+
|
|
34
|
+
def _create_client(self) -> EciClient:
|
|
35
|
+
"""
|
|
36
|
+
description:
|
|
37
|
+
创建ECI客户端
|
|
38
|
+
return:
|
|
39
|
+
EciClient: ECI客户端实例
|
|
40
|
+
"""
|
|
41
|
+
config = open_api_models.Config(
|
|
42
|
+
access_key_id=self.access_key_id,
|
|
43
|
+
access_key_secret=self.access_key_secret,
|
|
44
|
+
region_id=self.region_id,
|
|
45
|
+
endpoint=f"eci.{self.region_id}.aliyuncs.com",
|
|
46
|
+
)
|
|
47
|
+
return EciClient(config)
|
arraylib/aliyun/ecs.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
from datetime import datetime, timedelta
|
|
2
|
+
import json
|
|
3
|
+
from alibabacloud_cms20190101.client import Client as Cms20190101Client
|
|
4
|
+
from alibabacloud_tea_openapi import models as open_api_models
|
|
5
|
+
from alibabacloud_cms20190101 import models as cms_20190101_models
|
|
6
|
+
from alibabacloud_tea_util import models as util_models
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ECSUtils:
|
|
10
|
+
def __init__(self, access_key_id: str, access_key_secret: str, region_id: str):
|
|
11
|
+
self.cms_client = Cms20190101Client(
|
|
12
|
+
open_api_models.Config(
|
|
13
|
+
type="access_key",
|
|
14
|
+
access_key_id=access_key_id,
|
|
15
|
+
access_key_secret=access_key_secret,
|
|
16
|
+
endpoint=f"metrics.{region_id}.aliyuncs.com",
|
|
17
|
+
)
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def get_memory_utilization(self, instance_id: str, delta_minutes: int = 1) -> float:
|
|
21
|
+
"""
|
|
22
|
+
description:
|
|
23
|
+
获取实例的内存使用率
|
|
24
|
+
parameters:
|
|
25
|
+
instance_id(str): 实例ID
|
|
26
|
+
delta_minutes(int): 时间差 默认1分钟
|
|
27
|
+
return:
|
|
28
|
+
memory_utilization(float): 内存使用率 取值范围0-1
|
|
29
|
+
"""
|
|
30
|
+
describe_metric_list_request = cms_20190101_models.DescribeMetricListRequest(
|
|
31
|
+
namespace="acs_ecs_dashboard",
|
|
32
|
+
metric_name="memory_usedutilization",
|
|
33
|
+
dimensions=f'{{"instanceId": "{instance_id}"}}',
|
|
34
|
+
start_time=datetime.now() - timedelta(minutes=delta_minutes),
|
|
35
|
+
end_time=datetime.now(),
|
|
36
|
+
)
|
|
37
|
+
runtime = util_models.RuntimeOptions()
|
|
38
|
+
response = self.cms_client.describe_metric_list_with_options(
|
|
39
|
+
describe_metric_list_request, runtime
|
|
40
|
+
)
|
|
41
|
+
monitor_list = json.loads(response.body.datapoints)
|
|
42
|
+
max_value = max(monitor_list, key=lambda x: x["timestamp"])
|
|
43
|
+
return max_value["Average"] * 0.01
|
|
44
|
+
|
|
45
|
+
def get_cpu_utilization(self, instance_id: str, delta_minutes: int = 1) -> float:
|
|
46
|
+
"""
|
|
47
|
+
description:
|
|
48
|
+
获取实例的CPU使用率
|
|
49
|
+
parameters:
|
|
50
|
+
instance_id(str): 实例ID
|
|
51
|
+
delta_minutes(int): 时间差 默认1分钟
|
|
52
|
+
return:
|
|
53
|
+
cpu_utilization(float): 内存使用率 取值范围0-1
|
|
54
|
+
"""
|
|
55
|
+
describe_metric_list_request = cms_20190101_models.DescribeMetricListRequest(
|
|
56
|
+
namespace="acs_ecs_dashboard",
|
|
57
|
+
metric_name="CPUUtilization",
|
|
58
|
+
dimensions=f'{{"instanceId": "{instance_id}"}}',
|
|
59
|
+
start_time=datetime.now() - timedelta(minutes=delta_minutes),
|
|
60
|
+
end_time=datetime.now(),
|
|
61
|
+
)
|
|
62
|
+
runtime = util_models.RuntimeOptions()
|
|
63
|
+
response = self.cms_client.describe_metric_list_with_options(
|
|
64
|
+
describe_metric_list_request, runtime
|
|
65
|
+
)
|
|
66
|
+
monitor_list = json.loads(response.body.datapoints)
|
|
67
|
+
max_value = max(monitor_list, key=lambda x: x["timestamp"])
|
|
68
|
+
return max_value["Average"] * 0.01
|
arraylib/aliyun/fc.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
from tempfile import TemporaryDirectory
|
|
4
|
+
import traceback
|
|
5
|
+
|
|
6
|
+
from alibabacloud_fc20230330.client import Client as FC20230330Client
|
|
7
|
+
from alibabacloud_tea_openapi import models as open_api_models
|
|
8
|
+
from alibabacloud_darabonba_stream.client import Client as StreamClient
|
|
9
|
+
from alibabacloud_fc20230330 import models as fc20230330_models
|
|
10
|
+
from alibabacloud_tea_util import models as util_models
|
|
11
|
+
from arraylib.monitor.feishu import Feishu
|
|
12
|
+
from arraylib.system.common import execute_command, file_to_base64
|
|
13
|
+
|
|
14
|
+
feishu_alert = Feishu("devtoolkit服务报警")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class FCUtils:
|
|
18
|
+
def __init__(self, access_key_id, access_key_secret, account_id, region_id):
|
|
19
|
+
self.client = FC20230330Client(
|
|
20
|
+
open_api_models.Config(
|
|
21
|
+
access_key_id=access_key_id,
|
|
22
|
+
access_key_secret=access_key_secret,
|
|
23
|
+
endpoint=f"{account_id}.{region_id}.fc.aliyuncs.com",
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
def invoke_function(
|
|
28
|
+
self,
|
|
29
|
+
function_name: str,
|
|
30
|
+
params: dict,
|
|
31
|
+
retry: int = 5,
|
|
32
|
+
timeout: int = 6000 * 1000,
|
|
33
|
+
) -> tuple[str, bool]:
|
|
34
|
+
last_exception = None
|
|
35
|
+
for i in range(retry):
|
|
36
|
+
try:
|
|
37
|
+
body_stream = StreamClient.read_from_string(json.dumps(params))
|
|
38
|
+
invoke_function_headers = fc20230330_models.InvokeFunctionHeaders(
|
|
39
|
+
x_fc_invocation_type="Sync", x_fc_log_type="None"
|
|
40
|
+
)
|
|
41
|
+
invoke_function_request = fc20230330_models.InvokeFunctionRequest(
|
|
42
|
+
qualifier="LATEST", body=body_stream
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
runtime = util_models.RuntimeOptions()
|
|
46
|
+
runtime.connect_timeout = 60
|
|
47
|
+
runtime.read_timeout = timeout
|
|
48
|
+
response = self.client.invoke_function_with_options(
|
|
49
|
+
function_name,
|
|
50
|
+
invoke_function_request,
|
|
51
|
+
invoke_function_headers,
|
|
52
|
+
runtime,
|
|
53
|
+
)
|
|
54
|
+
if not response or response.status_code != 200:
|
|
55
|
+
last_exception = f"aliyun fc invoke_function error: {i} {response}"
|
|
56
|
+
# 如果不是最后一次重试,则继续重试
|
|
57
|
+
if i < retry - 1:
|
|
58
|
+
continue
|
|
59
|
+
else:
|
|
60
|
+
return None, False
|
|
61
|
+
return response.body.read().decode(), True
|
|
62
|
+
except Exception as e:
|
|
63
|
+
last_exception = (
|
|
64
|
+
f"aliyun fc invoke_function error: {i} {traceback.format_exc()}"
|
|
65
|
+
)
|
|
66
|
+
# 如果不是最后一次重试,则继续重试
|
|
67
|
+
if i < retry - 1:
|
|
68
|
+
continue
|
|
69
|
+
else:
|
|
70
|
+
return None, False
|
|
71
|
+
# 如果所有重试都失败,返回最后一次的异常信息
|
|
72
|
+
feishu_alert.send(
|
|
73
|
+
f"aliyun fc invoke_function failed after {retry} retries. Last error: {last_exception}"
|
|
74
|
+
)
|
|
75
|
+
return None, False
|
|
76
|
+
|
|
77
|
+
def update_function(
|
|
78
|
+
self,
|
|
79
|
+
function_name: str,
|
|
80
|
+
dir_path: str,
|
|
81
|
+
retry: int = 5,
|
|
82
|
+
) -> tuple[str, bool]:
|
|
83
|
+
last_exception = None
|
|
84
|
+
for i in range(retry):
|
|
85
|
+
try:
|
|
86
|
+
update_function_input_vpcconfig = fc20230330_models.VPCConfig(
|
|
87
|
+
security_group_id="sg-2ze2gmwyq9wxt34xbbli",
|
|
88
|
+
v_switch_ids=["vsw-2ze0wbhhcu9c0veyf2y1m"],
|
|
89
|
+
vpc_id="vpc-2zewkxvljq1091qa8x1um",
|
|
90
|
+
)
|
|
91
|
+
with TemporaryDirectory() as temp_dir:
|
|
92
|
+
code_zip_path = os.path.join(temp_dir, "code.zip")
|
|
93
|
+
_, success = execute_command(["zip", "-r", code_zip_path, dir_path])
|
|
94
|
+
if not success:
|
|
95
|
+
continue
|
|
96
|
+
update_function_input_input_code_location = (
|
|
97
|
+
fc20230330_models.InputCodeLocation(
|
|
98
|
+
zip_file=file_to_base64(code_zip_path)
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
update_function_input = fc20230330_models.UpdateFunctionInput(
|
|
102
|
+
handler="index.handler",
|
|
103
|
+
code=update_function_input_input_code_location,
|
|
104
|
+
timeout=6000,
|
|
105
|
+
disk_size=10240,
|
|
106
|
+
internet_access=True,
|
|
107
|
+
cpu=16,
|
|
108
|
+
runtime="python3.12",
|
|
109
|
+
instance_concurrency=1,
|
|
110
|
+
memory_size=16384,
|
|
111
|
+
vpc_config=update_function_input_vpcconfig,
|
|
112
|
+
)
|
|
113
|
+
update_function_request = fc20230330_models.UpdateFunctionRequest(
|
|
114
|
+
body=update_function_input
|
|
115
|
+
)
|
|
116
|
+
runtime = util_models.RuntimeOptions()
|
|
117
|
+
headers = {}
|
|
118
|
+
response = self.client.update_function_with_options(
|
|
119
|
+
function_name, update_function_request, headers, runtime
|
|
120
|
+
)
|
|
121
|
+
if not response or response.status_code != 200:
|
|
122
|
+
last_exception = f"aliyun fc update_function error: {i} {response}"
|
|
123
|
+
# 如果不是最后一次重试,则继续重试
|
|
124
|
+
if i < retry - 1:
|
|
125
|
+
continue
|
|
126
|
+
else:
|
|
127
|
+
return False
|
|
128
|
+
return True
|
|
129
|
+
except Exception as e:
|
|
130
|
+
last_exception = (
|
|
131
|
+
f"aliyun fc update_function error: {i} {traceback.format_exc()}"
|
|
132
|
+
)
|
|
133
|
+
# 如果不是最后一次重试,则继续重试
|
|
134
|
+
if i < retry - 1:
|
|
135
|
+
continue
|
|
136
|
+
else:
|
|
137
|
+
return False
|
|
138
|
+
# 如果所有重试都失败,返回最后一次的异常信息
|
|
139
|
+
feishu_alert.send(
|
|
140
|
+
f"aliyun fc update_function failed after {retry} retries. Last error: {last_exception}"
|
|
141
|
+
)
|
|
142
|
+
return False
|