nonebot-plugin-awsmgmt 0.1.1__tar.gz → 0.1.2__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: nonebot-plugin-awsmgmt
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: A nonebot2 plugin for managing AWS EC2, Lightsail, and Cost Explorer via commands.
5
5
  Author-email: Maximilian Wu <me@maxng.cc>
6
6
  Requires-Python: >=3.8
@@ -10,10 +10,37 @@ Description-Content-Type: text/markdown
10
10
 
11
11
  # nonebot_plugin_awsmgmt
12
12
 
13
- A NoneBot2 plugin for AWS management.
13
+ 一个用于 AWS 管理的 NoneBot2 插件。
14
14
 
15
15
  ## 使用方法
16
16
 
17
+ ---
18
+ ### EC2
19
+ - `/ec2_start [target]`
20
+ - `/ec2_stop [target]`
21
+ - `/ec2_reboot [target]`
22
+ - `/ec2_status [target]`
23
+
24
+ **Target (目标):**
25
+ - `tag:Key:Value`: 例如 `tag:Project:MyProject`
26
+ - `id:i-xxxx`: 例如 `id:i-0fd0acc80b595ac71`
27
+
28
+ 如果未提供 `target`,插件将使用您在配置中设置的 `aws_default_target_tag`(如果已设置)。
29
+
30
+ ---
31
+ ### Lightsail
32
+ - `/lightsail_list`
33
+ - `/lightsail_start <实例名称>`
34
+ - `/lightsail_stop <实例名称>`
35
+
36
+ ---
37
+ ### Cost Explorer (成本管理器)
38
+ - `/aws_cost today` (今日成本)
39
+ - `/aws_cost month` (本月成本)
40
+ - `/aws_cost month by_service` (按服务划分的本月成本)
41
+
42
+ ## 安装
43
+
17
44
  1. **安装插件**
18
45
 
19
46
  ```bash
@@ -33,12 +60,12 @@ A NoneBot2 plugin for AWS management.
33
60
 
34
61
  3. **配置 AWS 凭证**
35
62
 
36
- NoneBot2 项目的 `.env` 文件中配置 AWS 访问密钥和秘密访问密钥:
63
+ 在您的 NoneBot2 项目的 `.env` 文件中配置您的 AWS 访问密钥和秘密访问密钥:
37
64
 
38
65
  ```
39
66
  AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
40
67
  AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
41
- AWS_REGION_NAME=your-aws-region # 例如:us-east-1
68
+ AWS_REGION_NAME=your-aws-region # 例如: us-east-1
42
69
  ```
43
70
 
44
71
  ## AWS 侧配置
@@ -1,9 +1,36 @@
1
1
  # nonebot_plugin_awsmgmt
2
2
 
3
- A NoneBot2 plugin for AWS management.
3
+ 一个用于 AWS 管理的 NoneBot2 插件。
4
4
 
5
5
  ## 使用方法
6
6
 
7
+ ---
8
+ ### EC2
9
+ - `/ec2_start [target]`
10
+ - `/ec2_stop [target]`
11
+ - `/ec2_reboot [target]`
12
+ - `/ec2_status [target]`
13
+
14
+ **Target (目标):**
15
+ - `tag:Key:Value`: 例如 `tag:Project:MyProject`
16
+ - `id:i-xxxx`: 例如 `id:i-0fd0acc80b595ac71`
17
+
18
+ 如果未提供 `target`,插件将使用您在配置中设置的 `aws_default_target_tag`(如果已设置)。
19
+
20
+ ---
21
+ ### Lightsail
22
+ - `/lightsail_list`
23
+ - `/lightsail_start <实例名称>`
24
+ - `/lightsail_stop <实例名称>`
25
+
26
+ ---
27
+ ### Cost Explorer (成本管理器)
28
+ - `/aws_cost today` (今日成本)
29
+ - `/aws_cost month` (本月成本)
30
+ - `/aws_cost month by_service` (按服务划分的本月成本)
31
+
32
+ ## 安装
33
+
7
34
  1. **安装插件**
8
35
 
9
36
  ```bash
@@ -23,12 +50,12 @@ A NoneBot2 plugin for AWS management.
23
50
 
24
51
  3. **配置 AWS 凭证**
25
52
 
26
- NoneBot2 项目的 `.env` 文件中配置 AWS 访问密钥和秘密访问密钥:
53
+ 在您的 NoneBot2 项目的 `.env` 文件中配置您的 AWS 访问密钥和秘密访问密钥:
27
54
 
28
55
  ```
29
56
  AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
30
57
  AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
31
- AWS_REGION_NAME=your-aws-region # 例如:us-east-1
58
+ AWS_REGION_NAME=your-aws-region # 例如: us-east-1
32
59
  ```
33
60
 
34
61
  ## AWS 侧配置
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nonebot-plugin-awsmgmt"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "A nonebot2 plugin for managing AWS EC2, Lightsail, and Cost Explorer via commands."
5
5
  authors = [
6
6
  { name = "Maximilian Wu", email = "me@maxng.cc" }
@@ -103,6 +103,62 @@ def format_ec2_status(instance: Dict[str, Any]) -> str:
103
103
 
104
104
  # --- EC2 Handlers --- #
105
105
 
106
+ async def ec2_operation(matcher: Matcher, args: Message, operation: str):
107
+ """Helper function to handle EC2 start, stop, and reboot operations."""
108
+ target_type, value1, value2 = await parse_ec2_target(matcher, args)
109
+
110
+ states_map = {
111
+ "start": (["stopped"], "running", "start_instances", "instance_running"),
112
+ "stop": (["running"], "stopped", "stop_instances", "instance_stopped"),
113
+ "reboot": (["running"], "running", "reboot_instances", None), # Reboot does not have a waiter
114
+ }
115
+
116
+ if operation not in states_map:
117
+ await matcher.finish("Invalid operation.")
118
+
119
+ states, target_status, op_func_name, waiter_name = states_map[operation]
120
+
121
+ if target_type == "tag":
122
+ instances = await ec2_manager.get_instances_by_tag(value1, value2, states=states)
123
+ else:
124
+ instances = await ec2_manager.get_instances_by_id([value1], states=states)
125
+
126
+ if not instances:
127
+ await matcher.finish(f"No instances to {operation}.")
128
+
129
+ instance_ids = [inst['InstanceId'] for inst in instances]
130
+ await matcher.send(f"Sending {operation} command to instances:\n" + "\n".join(instance_ids))
131
+
132
+ op_func = getattr(ec2_manager, op_func_name)
133
+ await op_func(instance_ids)
134
+
135
+ if waiter_name:
136
+ await matcher.send("Waiting for them to be " + ("rebooted" if operation == "reboot" else f"{target_status}..."))
137
+ start_time = time.time()
138
+ async with ec2_manager.session.client("ec2") as ec2:
139
+ waiter = ec2.get_waiter(waiter_name)
140
+ await waiter.wait(InstanceIds=instance_ids)
141
+ elapsed_time = time.time() - start_time
142
+ await matcher.finish(f"Successfully {operation}ed instances in {elapsed_time:.2f} seconds.")
143
+ else: # For reboot
144
+ await matcher.finish(f"Successfully sent reboot command to instances.")
145
+
146
+
147
+ @ec2_start_matcher.handle()
148
+ @handle_non_finish_exceptions("An error occurred while starting EC2 instances.")
149
+ async def handle_ec2_start(matcher: Matcher, args: Message = CommandArg()):
150
+ await ec2_operation(matcher, args, "start")
151
+
152
+ @ec2_stop_matcher.handle()
153
+ @handle_non_finish_exceptions("An error occurred while stopping EC2 instances.")
154
+ async def handle_ec2_stop(matcher: Matcher, args: Message = CommandArg()):
155
+ await ec2_operation(matcher, args, "stop")
156
+
157
+ @ec2_reboot_matcher.handle()
158
+ @handle_non_finish_exceptions("An error occurred while rebooting EC2 instances.")
159
+ async def handle_ec2_reboot(matcher: Matcher, args: Message = CommandArg()):
160
+ await ec2_operation(matcher, args, "reboot")
161
+
106
162
  @ec2_status_matcher.handle()
107
163
  @handle_non_finish_exceptions("An error occurred while fetching EC2 status.")
108
164
  async def handle_ec2_status(matcher: Matcher, args: Message = CommandArg()):
@@ -123,6 +179,7 @@ async def handle_ec2_status(matcher: Matcher, args: Message = CommandArg()):
123
179
 
124
180
  # --- Lightsail Handlers ---
125
181
 
182
+
126
183
  @lightsail_list_matcher.handle()
127
184
  @handle_non_finish_exceptions("An error occurred listing Lightsail instances.")
128
185
  async def handle_lightsail_list(matcher: Matcher):