iac-code 0.1.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.
Files changed (184) hide show
  1. iac_code/__init__.py +2 -0
  2. iac_code/acp/__init__.py +97 -0
  3. iac_code/acp/convert.py +423 -0
  4. iac_code/acp/http_sse.py +448 -0
  5. iac_code/acp/mcp.py +54 -0
  6. iac_code/acp/metrics.py +71 -0
  7. iac_code/acp/server.py +662 -0
  8. iac_code/acp/session.py +446 -0
  9. iac_code/acp/slash_registry.py +125 -0
  10. iac_code/acp/state.py +99 -0
  11. iac_code/acp/tools.py +112 -0
  12. iac_code/acp/types.py +13 -0
  13. iac_code/acp/version.py +26 -0
  14. iac_code/agent/__init__.py +19 -0
  15. iac_code/agent/agent_loop.py +640 -0
  16. iac_code/agent/agent_tool.py +269 -0
  17. iac_code/agent/agent_types.py +87 -0
  18. iac_code/agent/message.py +153 -0
  19. iac_code/agent/system_prompt.py +313 -0
  20. iac_code/cli/__init__.py +3 -0
  21. iac_code/cli/headless.py +114 -0
  22. iac_code/cli/main.py +246 -0
  23. iac_code/cli/output_formats.py +125 -0
  24. iac_code/commands/__init__.py +93 -0
  25. iac_code/commands/auth.py +1055 -0
  26. iac_code/commands/clear.py +34 -0
  27. iac_code/commands/compact.py +43 -0
  28. iac_code/commands/debug.py +45 -0
  29. iac_code/commands/effort.py +116 -0
  30. iac_code/commands/exit.py +10 -0
  31. iac_code/commands/help.py +49 -0
  32. iac_code/commands/model.py +130 -0
  33. iac_code/commands/registry.py +245 -0
  34. iac_code/commands/resume.py +49 -0
  35. iac_code/commands/tasks.py +41 -0
  36. iac_code/config.py +304 -0
  37. iac_code/i18n/__init__.py +141 -0
  38. iac_code/i18n/locales/zh/LC_MESSAGES/messages.po +1355 -0
  39. iac_code/memory/__init__.py +1 -0
  40. iac_code/memory/memory_manager.py +92 -0
  41. iac_code/memory/memory_tools.py +88 -0
  42. iac_code/providers/__init__.py +1 -0
  43. iac_code/providers/anthropic_provider.py +284 -0
  44. iac_code/providers/base.py +128 -0
  45. iac_code/providers/dashscope_provider.py +47 -0
  46. iac_code/providers/deepseek_provider.py +36 -0
  47. iac_code/providers/manager.py +399 -0
  48. iac_code/providers/openai_provider.py +344 -0
  49. iac_code/providers/retry.py +58 -0
  50. iac_code/providers/stream_watchdog.py +47 -0
  51. iac_code/providers/thinking.py +164 -0
  52. iac_code/services/__init__.py +1 -0
  53. iac_code/services/agent_factory.py +127 -0
  54. iac_code/services/cloud_credentials.py +22 -0
  55. iac_code/services/context_manager.py +221 -0
  56. iac_code/services/providers/__init__.py +1 -0
  57. iac_code/services/providers/aliyun.py +232 -0
  58. iac_code/services/session_index.py +281 -0
  59. iac_code/services/session_storage.py +245 -0
  60. iac_code/services/telemetry/__init__.py +66 -0
  61. iac_code/services/telemetry/attributes.py +84 -0
  62. iac_code/services/telemetry/client.py +330 -0
  63. iac_code/services/telemetry/config.py +76 -0
  64. iac_code/services/telemetry/constants.py +75 -0
  65. iac_code/services/telemetry/content_serializer.py +124 -0
  66. iac_code/services/telemetry/events.py +42 -0
  67. iac_code/services/telemetry/fallback.py +59 -0
  68. iac_code/services/telemetry/identity.py +73 -0
  69. iac_code/services/telemetry/metrics.py +62 -0
  70. iac_code/services/telemetry/names.py +199 -0
  71. iac_code/services/telemetry/sanitize.py +88 -0
  72. iac_code/services/telemetry/sink.py +67 -0
  73. iac_code/services/telemetry/tracing.py +38 -0
  74. iac_code/services/telemetry/types.py +13 -0
  75. iac_code/services/token_budget.py +54 -0
  76. iac_code/services/token_counter.py +76 -0
  77. iac_code/skills/__init__.py +1 -0
  78. iac_code/skills/bundled/__init__.py +94 -0
  79. iac_code/skills/bundled/iac_aliyun/SKILL.md +192 -0
  80. iac_code/skills/bundled/iac_aliyun/__init__.py +16 -0
  81. iac_code/skills/bundled/iac_aliyun/references/cloud-products/ecs.md +167 -0
  82. iac_code/skills/bundled/iac_aliyun/references/cloud-products/oss.md +69 -0
  83. iac_code/skills/bundled/iac_aliyun/references/cloud-products/rds.md +95 -0
  84. iac_code/skills/bundled/iac_aliyun/references/cloud-products/redis.md +100 -0
  85. iac_code/skills/bundled/iac_aliyun/references/cloud-products/slb.md +60 -0
  86. iac_code/skills/bundled/iac_aliyun/references/cloud-products/vpc.md +54 -0
  87. iac_code/skills/bundled/iac_aliyun/references/ros-template.md +155 -0
  88. iac_code/skills/bundled/iac_aliyun/references/template-parameters.md +206 -0
  89. iac_code/skills/bundled/iac_aliyun/references/terraform-template.md +101 -0
  90. iac_code/skills/bundled/iac_aliyun/scripts/tf2ros.py +77 -0
  91. iac_code/skills/bundled/simplify.py +28 -0
  92. iac_code/skills/discovery.py +136 -0
  93. iac_code/skills/frontmatter.py +119 -0
  94. iac_code/skills/listing.py +92 -0
  95. iac_code/skills/loader.py +42 -0
  96. iac_code/skills/processor.py +81 -0
  97. iac_code/skills/renderer.py +157 -0
  98. iac_code/skills/skill_definition.py +82 -0
  99. iac_code/skills/skill_tool.py +261 -0
  100. iac_code/state/__init__.py +5 -0
  101. iac_code/state/app_state.py +122 -0
  102. iac_code/tasks/__init__.py +1 -0
  103. iac_code/tasks/notification_queue.py +28 -0
  104. iac_code/tasks/task_state.py +66 -0
  105. iac_code/tasks/task_tools.py +114 -0
  106. iac_code/tools/__init__.py +8 -0
  107. iac_code/tools/base.py +226 -0
  108. iac_code/tools/bash.py +133 -0
  109. iac_code/tools/cloud/__init__.py +0 -0
  110. iac_code/tools/cloud/aliyun/__init__.py +0 -0
  111. iac_code/tools/cloud/aliyun/aliyun_api.py +510 -0
  112. iac_code/tools/cloud/aliyun/aliyun_doc_search.py +145 -0
  113. iac_code/tools/cloud/aliyun/endpoints.yml +343 -0
  114. iac_code/tools/cloud/aliyun/ros_client.py +56 -0
  115. iac_code/tools/cloud/aliyun/ros_stack.py +633 -0
  116. iac_code/tools/cloud/aliyun/ros_stack_instances.py +247 -0
  117. iac_code/tools/cloud/base_api.py +162 -0
  118. iac_code/tools/cloud/base_stack.py +242 -0
  119. iac_code/tools/cloud/registry.py +20 -0
  120. iac_code/tools/cloud/types.py +105 -0
  121. iac_code/tools/edit_file.py +121 -0
  122. iac_code/tools/glob.py +103 -0
  123. iac_code/tools/grep.py +254 -0
  124. iac_code/tools/list_files.py +104 -0
  125. iac_code/tools/read_file.py +127 -0
  126. iac_code/tools/result_storage.py +39 -0
  127. iac_code/tools/tool_executor.py +165 -0
  128. iac_code/tools/web_fetch.py +177 -0
  129. iac_code/tools/write_file.py +88 -0
  130. iac_code/types/__init__.py +40 -0
  131. iac_code/types/permissions.py +26 -0
  132. iac_code/types/skill_source.py +11 -0
  133. iac_code/types/stream_events.py +227 -0
  134. iac_code/ui/__init__.py +5 -0
  135. iac_code/ui/banner.py +110 -0
  136. iac_code/ui/components/__init__.py +0 -0
  137. iac_code/ui/components/dialog.py +142 -0
  138. iac_code/ui/components/divider.py +20 -0
  139. iac_code/ui/components/fuzzy_picker.py +308 -0
  140. iac_code/ui/components/progress_bar.py +54 -0
  141. iac_code/ui/components/search_box.py +165 -0
  142. iac_code/ui/components/select.py +319 -0
  143. iac_code/ui/components/status_icon.py +42 -0
  144. iac_code/ui/components/tabs.py +128 -0
  145. iac_code/ui/core/__init__.py +0 -0
  146. iac_code/ui/core/in_place_render.py +129 -0
  147. iac_code/ui/core/input_history.py +118 -0
  148. iac_code/ui/core/key_event.py +41 -0
  149. iac_code/ui/core/prompt_input.py +507 -0
  150. iac_code/ui/core/raw_input.py +302 -0
  151. iac_code/ui/core/screen.py +80 -0
  152. iac_code/ui/dialogs/__init__.py +0 -0
  153. iac_code/ui/dialogs/global_search.py +178 -0
  154. iac_code/ui/dialogs/history_search.py +100 -0
  155. iac_code/ui/dialogs/model_picker.py +280 -0
  156. iac_code/ui/dialogs/quick_open.py +108 -0
  157. iac_code/ui/dialogs/resume_picker.py +749 -0
  158. iac_code/ui/keybindings/__init__.py +0 -0
  159. iac_code/ui/keybindings/manager.py +124 -0
  160. iac_code/ui/renderer.py +1535 -0
  161. iac_code/ui/repl.py +772 -0
  162. iac_code/ui/spinner.py +112 -0
  163. iac_code/ui/suggestions/__init__.py +0 -0
  164. iac_code/ui/suggestions/aggregator.py +171 -0
  165. iac_code/ui/suggestions/command_provider.py +43 -0
  166. iac_code/ui/suggestions/directory_provider.py +95 -0
  167. iac_code/ui/suggestions/file_provider.py +121 -0
  168. iac_code/ui/suggestions/shell_history_provider.py +108 -0
  169. iac_code/ui/suggestions/token_extractor.py +77 -0
  170. iac_code/ui/suggestions/types.py +45 -0
  171. iac_code/ui/transcript_view.py +199 -0
  172. iac_code/utils/__init__.py +0 -0
  173. iac_code/utils/background_housekeeping.py +53 -0
  174. iac_code/utils/cleanup.py +68 -0
  175. iac_code/utils/json_utils.py +60 -0
  176. iac_code/utils/log.py +150 -0
  177. iac_code/utils/project_paths.py +74 -0
  178. iac_code/utils/tool_input_parser.py +62 -0
  179. iac_code-0.1.0.dist-info/LICENSE +201 -0
  180. iac_code-0.1.0.dist-info/METADATA +64 -0
  181. iac_code-0.1.0.dist-info/RECORD +184 -0
  182. iac_code-0.1.0.dist-info/WHEEL +5 -0
  183. iac_code-0.1.0.dist-info/entry_points.txt +2 -0
  184. iac_code-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,95 @@
1
+ # 阿里云 RDS 选型指南
2
+
3
+ ## 引擎选择
4
+
5
+ | 引擎 | 推荐场景 | 说明 |
6
+ |------|----------|------|
7
+ | MySQL | Web 应用、电商、SaaS | 生态最广,开源友好 |
8
+ | PostgreSQL | 复杂查询、GIS、分析 | 功能最全,支持 JSON、扩展 |
9
+ | SQL Server | .NET 应用、企业系统 | Windows 生态,需授权费 |
10
+ | MariaDB | MySQL 替代 | 兼容 MySQL,开源 |
11
+
12
+ **推荐选择**:优先 MySQL 8.0 或 PostgreSQL 15,除非有特殊需求。
13
+
14
+ ## 版本推荐
15
+
16
+ - MySQL:**8.0**(长期支持,性能优于 5.7)
17
+ - PostgreSQL:**15** 或 **16**
18
+ - SQL Server:2019 Enterprise/Standard
19
+
20
+ ## 系列(Edition)
21
+
22
+ | 系列 | 可用区 | 适用 | 说明 |
23
+ |------|--------|------|------|
24
+ | 基础版(Basic) | 单 AZ | 开发/测试 | 无主备,成本低,不建议生产用 |
25
+ | 高可用版(HA) | 双 AZ | 生产 | 主备架构,自动切换,推荐 |
26
+ | 三节点企业版(Cluster) | 三 AZ | 金融/关键系统 | 一主两备,RPO=0 |
27
+
28
+ ## 规格推荐
29
+
30
+ | 场景 | 推荐规格 | 存储 |
31
+ |------|----------|------|
32
+ | 开发/测试 | rds.mysql.s1.small (1c2g) | 20 GB |
33
+ | 小型应用 | mysql.n2.medium.1 (2c4g) | 100 GB |
34
+ | 中型应用 | mysql.n4.large.1 (4c8g) | 200 GB |
35
+ | 高并发/大数据 | mysql.n8.xlarge.1 (8c32g) | 500+ GB |
36
+
37
+ ## 存储类型
38
+
39
+ - **ESSD PL1**:推荐,高 IO,适合大多数生产场景
40
+ - **SSD**:性能略低,成本低,适合测试环境
41
+ - **ESSD PL2/PL3**:极高 IO,适合高并发写入场景
42
+
43
+ ## 网络配置
44
+
45
+ - 生产环境:只开放内网访问,禁止公网直连
46
+ - 开发/测试:可临时开放公网,IP 白名单严格限制
47
+ - 使用安全组或 IP 白名单控制访问来源
48
+
49
+ ## 备份策略
50
+
51
+ - 自动备份:开启,保留 7 天以上
52
+ - 备份时间:业务低峰期(如凌晨 2-4 点)
53
+ - 跨地域备份:关键数据开启,防止地域级灾难
54
+
55
+ ## 账户类型与权限
56
+
57
+ - `MasterUserType: Super`:超级账户,自动拥有所有数据库的全部权限,**不需要**创建 `ALIYUN::RDS::AccountPrivilege` 资源
58
+ - `MasterUserType: Normal`:普通账户,需通过 `ALIYUN::RDS::AccountPrivilege` 单独授权数据库访问权限
59
+
60
+ ## 最佳实践
61
+
62
+ - 连接池:应用层使用连接池(如 HikariCP、pgBouncer),避免连接数暴涨
63
+ - 慢查询:开启慢查询日志,阈值 1 秒
64
+ - 参数调优:生产环境根据业务调整 `innodb_buffer_pool_size` 等核心参数
65
+ - 只读实例:读多写少场景添加只读实例分担压力
66
+
67
+ ## 库存相关属性(模板中须参数化为 Parameters)
68
+
69
+ | 属性 | 说明 |
70
+ |------|------|
71
+ | ZoneId | 可用区 |
72
+ | DBInstanceClass | 数据库实例规格 |
73
+ | DBInstanceStorageType | 存储类型 |
74
+
75
+ ## 可用性查询
76
+
77
+ ### 查询可用 RDS 规格
78
+
79
+ ```
80
+ aliyun_api(product="rds", action="DescribeAvailableClasses", params={
81
+ "Engine": "MySQL",
82
+ "EngineVersion": "8.0",
83
+ "DBInstanceStorageType": "cloud_essd",
84
+ "Category": "HighAvailability",
85
+ "CommodityCode": "bards"
86
+ })
87
+ ```
88
+
89
+ > Engine 可选 MySQL / PostgreSQL / SQLServer / MariaDB。Category 可选 Basic / HighAvailability / cluster。CommodityCode 按量付费用 bards,包年包月用 rds。
90
+
91
+ ### 筛选逻辑
92
+
93
+ 1. 从返回结果中,找出可用的实例规格和可用区
94
+ 2. 按「规格推荐」表优先匹配
95
+ 3. 推荐规格不可用时,选同系列中最接近 vCPU/内存配置的可用规格
@@ -0,0 +1,100 @@
1
+ # 阿里云 Redis 选型指南
2
+
3
+ ## 版本选择
4
+
5
+ | 版本 | 说明 | 推荐 |
6
+ |------|------|------|
7
+ | Redis 7.0 | 最新,Function 支持,性能提升 | 新项目首选 |
8
+ | Redis 6.0 | 多线程 IO,稳定 | 已有项目 |
9
+ | Redis 5.0 | 成熟稳定 | 兼容性要求高 |
10
+
11
+ ## 产品系列
12
+
13
+ ### 社区版(开源兼容)
14
+
15
+ | 架构 | 说明 | 适用场景 |
16
+ |------|------|----------|
17
+ | 标准版-单副本 | 单节点,无主备 | 开发/测试,低成本 |
18
+ | 标准版-双副本 | 主备架构,自动故障切换 | 生产环境基础选项 |
19
+ | 集群版 | 数据分片,水平扩展 | 大容量(> 64 GB)、高并发 |
20
+ | 读写分离版 | 主节点写,多只读节点 | 读多写少,扩展读能力 |
21
+
22
+ ### 企业版
23
+
24
+ - 持久内存型:内存 + 持久化,比标准版成本低 40%
25
+ - 存储型:海量数据,冷热数据混合
26
+
27
+ ## 规格推荐
28
+
29
+ | 场景 | 推荐规格 | 内存 |
30
+ |------|----------|------|
31
+ | 开发/测试 | redis.master.micro.default | 1 GB |
32
+ | 小型应用(Session/缓存) | redis.master.small.default | 2 GB |
33
+ | 中型应用 | redis.master.mid.default | 8 GB |
34
+ | 高并发应用 | redis.master.large.default | 16 GB |
35
+ | 大容量需求 | 集群版,按分片数扩展 | 32 GB+ |
36
+
37
+ ## 网络配置
38
+
39
+ - 生产环境:仅开放内网访问,禁止公网直连
40
+ - 安全 IP 白名单:限制为应用服务器网段
41
+ - SSL/TLS:敏感数据场景开启传输加密
42
+
43
+ ## 持久化配置
44
+
45
+ - RDB(快照):定期全量快照,恢复快,数据可能有少量丢失
46
+ - AOF(追加日志):近实时持久化,数据更安全,性能略低
47
+ - 推荐:生产环境开启 RDB + AOF 双持久化
48
+
49
+ ## 集群版选型
50
+
51
+ 适用条件(满足任一即考虑集群版):
52
+ - 单实例内存需求 > 64 GB
53
+ - QPS 超过 10 万
54
+ - Key 数量超过 1 亿
55
+
56
+ 集群版注意事项:
57
+ - 不支持跨 Slot 的多 Key 操作(MSET、Pipeline 需同 Slot)
58
+ - 不支持 SELECT 切换 DB(固定 DB 0)
59
+ - Lua 脚本需确保所有 Key 在同一 Slot
60
+
61
+ ## 读写分离版
62
+
63
+ - 适合读 QPS 远大于写 QPS 的场景(如新闻、商品详情)
64
+ - 只读节点数量:1-5 个,按读流量动态调整
65
+ - 主节点和只读节点数据同步有微秒级延迟,读业务需容忍
66
+
67
+ ## 最佳实践
68
+
69
+ - Key 命名规范:`{业务}:{模块}:{ID}`,如 `user:session:12345`
70
+ - 设置合理 TTL,避免 Key 无限增长(内存打满)
71
+ - 大 Value(> 10 KB)拆分或存 OSS,Redis 存引用
72
+ - 禁止生产环境执行 `KEYS *`、`FLUSHALL` 等危险命令
73
+ - 监控内存使用率,超过 80% 及时扩容
74
+ - 慢查询阈值设为 10 ms,定期分析优化
75
+
76
+ ## 库存相关属性(模板中须参数化为 Parameters)
77
+
78
+ | 属性 | 说明 |
79
+ |------|------|
80
+ | ZoneId | 可用区 |
81
+ | InstanceClass | 实例规格 |
82
+
83
+ ## 可用性查询
84
+
85
+ ### 查询可用 Redis 规格
86
+
87
+ ```
88
+ aliyun_api(product="r-kvstore", action="DescribeAvailableResource", params={
89
+ "InstanceChargeType": "PostPaid",
90
+ "Engine": "Redis"
91
+ })
92
+ ```
93
+
94
+ > InstanceChargeType 也可传 "PrePaid"(包年包月)。
95
+
96
+ ### 筛选逻辑
97
+
98
+ 1. 从返回结果中,找出可用的实例规格和可用区
99
+ 2. 按「规格推荐」表优先匹配
100
+ 3. 推荐规格不可用时,选同内存量级中最接近的可用规格
@@ -0,0 +1,60 @@
1
+ # 阿里云负载均衡选型指南
2
+
3
+ ## 产品对比
4
+
5
+ | 产品 | 层级 | 协议 | 适用场景 |
6
+ |------|------|------|----------|
7
+ | CLB(传统型) | L4/L7 | TCP/UDP/HTTP/HTTPS | 通用场景,成本低 |
8
+ | ALB(应用型) | L7 | HTTP/HTTPS/QUIC | 高级路由、微服务网关 |
9
+ | NLB(网络型) | L4 | TCP/UDP/TCPSSL | 超高性能、极低延迟 |
10
+
11
+ ### 选型决策树
12
+
13
+ ```
14
+ 需要 L7 路由(URL/Header/Cookie 转发)?
15
+ ├── 是 → ALB(应用型负载均衡)
16
+ │ - 基于内容的路由
17
+ │ - 重写/重定向
18
+ │ - WAF 集成
19
+ │ - WebSocket/HTTP2
20
+ └── 否 → 需要超高性能(百万 QPS / 极低延迟)?
21
+ ├── 是 → NLB(网络型负载均衡)
22
+ │ - 保持客户端源 IP
23
+ │ - UDP 支持
24
+ └── 否 → CLB(传统型负载均衡)
25
+ - 成本最低
26
+ - 配置简单
27
+ - TCP/HTTP 均支持
28
+ ```
29
+
30
+ ## CLB 配置要点
31
+
32
+ 涉及 3 个资源类型:
33
+ - `ALIYUN::SLB::LoadBalancer`:创建实例,关键属性 `AddressType`(internet/intranet)、`LoadBalancerSpec`
34
+ - `ALIYUN::SLB::Listener`:配置监听,关键属性 `Protocol`、`ListenerPort`、`BackendServerPort`、`HealthCheck`
35
+ - `ALIYUN::SLB::BackendServerAttachment`:绑定后端服务器
36
+
37
+ ## ALB 配置要点
38
+
39
+ 涉及 3 个资源类型:
40
+ - `ALIYUN::ALB::LoadBalancer`:创建实例,需配置 `ZoneMappings`(至少 2 个可用区)
41
+ - `ALIYUN::ALB::ServerGroup`:配置服务器组和健康检查
42
+ - `ALIYUN::ALB::Listener`:配置监听,通过 `DefaultActions` 的 `ForwardGroup` 关联 ServerGroup
43
+
44
+ ## HTTPS 配置建议
45
+
46
+ - 证书上传到阿里云证书管理服务(SSL Certificates Service)
47
+ - SLB/ALB 监听器配置 HTTPS,引用证书 ID
48
+ - HTTP → HTTPS 重定向:ALB 支持配置监听器重定向规则
49
+
50
+ ## 健康检查最佳实践
51
+
52
+ - 配置专用健康检查接口(如 `/health`),返回 200 即视为健康
53
+ - 阈值设置:健康阈值 3 次,不健康阈值 3 次,间隔 5 秒
54
+ - 避免用业务接口做健康检查(防止误判)
55
+
56
+ ## 会话保持
57
+
58
+ - CLB:支持基于 Cookie 的会话保持
59
+ - ALB:支持基于 Cookie 和源 IP 的会话保持
60
+ - 无状态应用:不需要会话保持,水平扩展更灵活
@@ -0,0 +1,54 @@
1
+ # 阿里云 VPC 网络规划指南
2
+
3
+ ## CIDR 规划原则
4
+
5
+ - 避免与线下网络或其他 VPC 重叠
6
+ - 预留足够地址空间(按 3-5 年规划)
7
+ - 推荐网段:`10.0.0.0/8`、`172.16.0.0/12`、`192.168.0.0/16`
8
+
9
+ ### 推荐规划
10
+
11
+ | 环境 | VPC CIDR | 说明 |
12
+ |------|----------|------|
13
+ | 生产 | 10.0.0.0/16 | 65,534 个地址 |
14
+ | 预发布 | 10.1.0.0/16 | 独立隔离 |
15
+ | 开发/测试 | 10.2.0.0/16 | |
16
+ | 个人/小项目 | 192.168.0.0/24 | 254 个地址,够用即可 |
17
+
18
+ ## VSwitch 规划
19
+
20
+ 每个可用区建议创建独立 VSwitch,按用途分层:
21
+
22
+ ```
23
+ VPC: 10.0.0.0/16
24
+ ├── 可用区 A
25
+ │ ├── 公网层 VSwitch: 10.0.1.0/24 (SLB、NAT、跳板机)
26
+ │ ├── 应用层 VSwitch: 10.0.2.0/24 (ECS、容器)
27
+ │ └── 数据层 VSwitch: 10.0.3.0/24 (RDS、Redis、OSS VPC Endpoint)
28
+ └── 可用区 B
29
+ ├── 公网层 VSwitch: 10.0.11.0/24
30
+ ├── 应用层 VSwitch: 10.0.12.0/24
31
+ └── 数据层 VSwitch: 10.0.13.0/24
32
+ ```
33
+
34
+ ## 安全组设计
35
+
36
+ ### 分层安全组策略
37
+
38
+ 生产环境推荐按层级划分安全组:公网层、应用层、数据层。通过 `SourceGroupId` 实现层间引用,仅允许上层流量流入下层:
39
+
40
+ - **公网层 SG**:仅允许 80/443,`SourceCidrIp: 0.0.0.0/0`
41
+ - **应用层 SG**:仅允许来自公网层 SG 的流量,`SourceGroupId: !Ref PublicSG`
42
+ - **数据层 SG**:仅允许来自应用层 SG 的流量,`SourceGroupId: !Ref AppSG`
43
+
44
+ ## NAT 网关
45
+
46
+ - 公网访问:ECS 无公网 IP 时,通过 NAT 网关(`ALIYUN::VPC::NatGateway`)SNAT 访问公网
47
+ - 公网入站:通过 DNAT 规则映射端口到内网(不推荐,优先用 SLB)
48
+
49
+ ## 网络访问控制建议
50
+
51
+ - **最小权限原则**:安全组只开放必要端口和来源
52
+ - **禁止 0.0.0.0/0 用于管理端口**:SSH(22)、RDP(3389) 严格限制来源
53
+ - **数据层不出公网**:RDS、Redis 等仅内网访问
54
+ - **VPC 对等连接**:跨 VPC 访问使用 VPC Peering,不通过公网
@@ -0,0 +1,155 @@
1
+ # ROS 模板最佳实践
2
+
3
+ ## 常用资源
4
+
5
+ - ALIYUN::ECS::VPC: 创建专有网络
6
+ - ALIYUN::ECS::VSwitch: 创建交换机
7
+ - ALIYUN::ECS::SecurityGroup: 创建安全组(同时支持安全组规则)
8
+ - ALIYUN::ECS::InstanceGroup: 创建N个ECS实例(通过 `MaxAmount` 指定数量)
9
+
10
+ ## 在实例中执行命令
11
+
12
+ **不要使用 UserData + WaitCondition**。根据场景选择:
13
+
14
+ - **自定义命令** → `ALIYUN::ECS::RunCommand` + `CommandContent`
15
+ - **公共命令** → `ALIYUN::ECS::Invocation` + `CommandName`
16
+
17
+ ```yaml
18
+ # 自定义命令
19
+ RunSetup:
20
+ Type: ALIYUN::ECS::RunCommand
21
+ Properties:
22
+ InstanceIds:
23
+ - !Ref WebServer
24
+ Type: RunShellScript
25
+ Sync: true
26
+ Timeout: 600
27
+ ContentEncoding: PlainText
28
+ CommandContent: |
29
+ #!/bin/bash
30
+ yum install -y nginx
31
+ systemctl enable nginx
32
+ systemctl start nginx
33
+
34
+ # 公共命令
35
+ InstallOpenClaw:
36
+ Type: ALIYUN::ECS::Invocation
37
+ Properties:
38
+ InstanceIds:
39
+ - !Ref WebServer
40
+ CommandName: ACS-ECS-InstallOpenClaw-for-linux.sh
41
+ Sync: true
42
+ Timeout: 600
43
+ ```
44
+
45
+ ### 嵌套栈
46
+
47
+ ```yaml
48
+ Resources:
49
+ NetworkStack:
50
+ Type: ALIYUN::ROS::Stack
51
+ Properties:
52
+ TemplateBody:
53
+ Parameters:
54
+ CidrBlock: 192.168.0.0/16
55
+ ```
56
+
57
+ ## 条件部署
58
+
59
+ 通过 Conditions 实现环境差异化:
60
+
61
+ ```yaml
62
+ Conditions:
63
+ IsProd: !Equals [!Ref Env, prod]
64
+ Resources:
65
+ ProdBucket:
66
+ Type: ALIYUN::OSS::Bucket
67
+ Condition: IsProd
68
+ Properties:
69
+ BucketName: prod-bucket
70
+ ```
71
+
72
+ ## 资源引用
73
+
74
+ - 通过 `!Ref` 和 `!GetAtt` 引用参数和资源属性,避免硬编码
75
+ - 使用 `DependsOn` 声明隐式依赖无法表达的顺序关系
76
+
77
+ ## 常用函数
78
+
79
+ 基础函数(Ref、Fn::GetAtt、Fn::Join、Fn::If、Fn::Equals 等)LLM 已熟悉,以下是容易用错的复杂函数。其他函数参考 aliyun_doc_search(keywords="ROS 函数", category_id=28850)。
80
+
81
+ ### Fn::Sub
82
+
83
+ 字符串变量替换。`${VarName}` 支持引用参数、伪参数(如 `${ALIYUN::StackId}`)、资源属性(如 `${Resource.Attr}`)。用 `${!VarName}` 保留字面量不替换。
84
+
85
+ ```yaml
86
+ # 简写
87
+ !Sub "string-${VarName}"
88
+
89
+ # 带自定义变量映射
90
+ Fn::Sub:
91
+ - "string-${Var1}-${Var2}"
92
+ - Var1: value1
93
+ Var2: value2
94
+ ```
95
+
96
+ ### Fn::Select
97
+
98
+ 从列表按索引/切片,或从字典按 Key 选取。索引从 0 开始,支持负数。第三参数为默认值(可选)。
99
+
100
+ ```yaml
101
+ # 按索引
102
+ !Select [index, list, defaultValue]
103
+
104
+ # 切片(start 默认 0,stop 默认 N,step 默认 1)
105
+ !Select ["start:stop:step", list]
106
+
107
+ # 按 Key
108
+ !Select [key, map, defaultValue]
109
+ ```
110
+
111
+ ### Fn::Replace
112
+
113
+ 替换字符串中的子串,支持多个替换映射。
114
+
115
+ ```yaml
116
+ Fn::Replace:
117
+ - oldStr1: newStr1
118
+ oldStr2: newStr2
119
+ - originalString
120
+ ```
121
+
122
+ ### Fn::Jq
123
+
124
+ 对 JSON 数据执行 jq 查询。
125
+
126
+ ```yaml
127
+ Fn::Jq:
128
+ - First/All
129
+ - jqScript
130
+ - jsonObject
131
+ ```
132
+
133
+ - **First**:返回 jq 查询的第一个匹配值(字符串)
134
+ - **All**:返回 jq 查询的所有匹配值(列表)
135
+
136
+ jqScript 支持 jq 完整语法,包括管道 `|`、过滤 `select()`、变换 `{key: .field}` 等。
137
+
138
+ ## Outputs
139
+
140
+ 所有输出变量必须定义 Label。应用访问链接使用 `Console.` 前缀,会在 ROS 控制台概览页的「使用信息」中展示:
141
+
142
+ ```yaml
143
+ Outputs:
144
+ EcsPublicIp:
145
+ Description: ECS 公网 IP
146
+ Label: ECS Public IP
147
+ Value: !GetAtt MyEcs.PublicIp
148
+ Console.NginxUrl:
149
+ Description: Nginx 访问地址
150
+ Label: Nginx URL
151
+ Value:
152
+ !Sub
153
+ - http://${Ip}
154
+ - Ip: !Select [0, !GetAtt MyEcs.PublicIps]
155
+ ```
@@ -0,0 +1,206 @@
1
+ # ROS 模板参数规范
2
+
3
+ 本规范适用于所有通过 ROS 部署的模板。Terraform 模板通过 tf2ros 转换后,同样在生成的 ROS 模板中添加 Parameters 和 Metadata。
4
+
5
+ ## 参数化要求
6
+
7
+ 库存相关的 Parameters 不设 Default 和 AllowedValues,部署前通过可用性查询确定。
8
+
9
+ 选填参数设置 `Default: null`,用户可跳过不填。
10
+
11
+ 敏感参数设置 NoEcho: true。
12
+
13
+ ## AssociationProperty
14
+
15
+ 所有 Parameters **必须**添加 AssociationProperty,让 ROS 控制台自动关联候选值。通过 AssociationPropertyMetadata 实现参数间联动过滤。
16
+
17
+ ### 高频参考表
18
+
19
+ | 参数用途 | AssociationProperty | 关键 Metadata |
20
+ |---------|-------------------|--------------|
21
+ | 地域 | ALIYUN::ECS::RegionId | - |
22
+ | ECS 可用区 | ALIYUN::ECS::ZoneId | RegionId |
23
+ | ECS 实例规格 | ALIYUN::ECS::Instance::InstanceType | RegionId, ZoneId, InstanceChargeType |
24
+ | ECS 镜像 | ALIYUN::ECS::Image::ImageId | RegionId, InstanceType |
25
+ | 系统盘类型 | ALIYUN::ECS::Disk::SystemDiskCategory | RegionId, ZoneId, InstanceType |
26
+ | 数据盘类型 | ALIYUN::ECS::Disk::DataDiskCategory | RegionId, ZoneId, InstanceType |
27
+ | VPC | ALIYUN::ECS::VPC::VPCId | RegionId |
28
+ | 交换机 | ALIYUN::VPC::VSwitch::VSwitchId | RegionId, ZoneId, VpcId |
29
+ | 安全组 | ALIYUN::ECS::SecurityGroup::SecurityGroupId | RegionId, VpcId |
30
+ | 密钥对 | ALIYUN::ECS::KeyPair::KeyPairName | RegionId |
31
+ | 密码 | ALIYUN::ECS::Instance::Password | - |
32
+ | RDS 引擎 | ALIYUN::RDS::Engine::EngineId | - |
33
+ | RDS 引擎版本 | ALIYUN::RDS::Engine::EngineVersion | Engine |
34
+ | RDS 实例规格 | ALIYUN::RDS::Instance::InstanceType | RegionId, ZoneId, Engine, EngineVersion, DBInstanceStorageType, Category |
35
+ | Redis 实例规格 | ALIYUN::Redis::Instance::InstanceType | RegionId, ZoneId, InstanceChargeType |
36
+ | SLB 实例规格 | ALIYUN::SLB::Instance::InstanceType | RegionId, ZoneId |
37
+ | 付费类型 | ChargeType | - |
38
+
39
+ > 不在此表中的 AssociationProperty,使用 aliyun_doc_search(keywords="AssociationProperty <产品名>", category_id=28850) 搜索 ROS 文档获取。
40
+
41
+ ## Label
42
+
43
+ 为每个 Parameter 添加 Label,提供中英文显示名:
44
+
45
+ ```yaml
46
+ Label:
47
+ en: Zone ID
48
+ zh-cn: 可用区
49
+ ```
50
+
51
+ ## 参数分组
52
+
53
+ 使用 Metadata 的 ParameterGroups 将参数按逻辑分组,提升控制台体验:
54
+
55
+ ```yaml
56
+ Metadata:
57
+ ALIYUN::ROS::Interface:
58
+ ParameterGroups:
59
+ - Parameters:
60
+ - VpcId
61
+ - ZoneId
62
+ - VSwitchId
63
+ Label:
64
+ default: 网络配置
65
+ - Parameters:
66
+ - InstanceType
67
+ - SystemDiskCategory
68
+ Label:
69
+ default: ECS 配置
70
+ - Parameters:
71
+ - DBInstanceClass
72
+ - DBInstanceStorageType
73
+ Label:
74
+ default: RDS 配置
75
+ ```
76
+
77
+ 所有 Parameters 都应归入某个分组,按资源类型或功能模块分类。
78
+
79
+ ## 完整示例
80
+
81
+ ```yaml
82
+ ROSTemplateFormatVersion: '2015-09-01'
83
+ Metadata:
84
+ ALIYUN::ROS::Interface:
85
+ ParameterGroups:
86
+ - Parameters:
87
+ - ZoneId
88
+ - VpcId
89
+ - VSwitchId
90
+ Label:
91
+ default: 网络配置
92
+ - Parameters:
93
+ - InstanceType
94
+ - SystemDiskCategory
95
+ Label:
96
+ default: ECS 配置
97
+ Parameters:
98
+ ZoneId:
99
+ Type: String
100
+ Label:
101
+ en: Zone ID
102
+ zh-cn: 可用区
103
+ AssociationProperty: ALIYUN::ECS::ZoneId
104
+ AssociationPropertyMetadata:
105
+ RegionId: ${ALIYUN::Region}
106
+ VpcId:
107
+ Type: String
108
+ Label:
109
+ en: VPC
110
+ zh-cn: 专有网络
111
+ AssociationProperty: ALIYUN::ECS::VPC::VPCId
112
+ AssociationPropertyMetadata:
113
+ RegionId: ${ALIYUN::Region}
114
+ VSwitchId:
115
+ Type: String
116
+ Label:
117
+ en: VSwitch
118
+ zh-cn: 交换机
119
+ AssociationProperty: ALIYUN::VPC::VSwitch::VSwitchId
120
+ AssociationPropertyMetadata:
121
+ ZoneId: ${ZoneId}
122
+ VpcId: ${VpcId}
123
+ InstanceType:
124
+ Type: String
125
+ Label:
126
+ en: Instance Type
127
+ zh-cn: 实例规格
128
+ AssociationProperty: ALIYUN::ECS::Instance::InstanceType
129
+ AssociationPropertyMetadata:
130
+ ZoneId: ${ZoneId}
131
+ InstanceChargeType: PostPaid
132
+ SystemDiskCategory:
133
+ Type: String
134
+ Label:
135
+ en: System Disk Category
136
+ zh-cn: 系统盘类型
137
+ AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
138
+ AssociationPropertyMetadata:
139
+ ZoneId: ${ZoneId}
140
+ InstanceType: ${InstanceType}
141
+ ```
142
+
143
+ Metadata 中用 `${ParamName}` 引用其他参数,`${ALIYUN::Region}` 引用栈所在地域。
144
+
145
+ ## Terraform 模板的参数规范
146
+
147
+ Terraform 模板通过 ROS 部署时,参数元信息通过以下方式定义(不需要转换后再修改 ROS 模板):
148
+
149
+ ### 变量级:description 中嵌入 JSON
150
+
151
+ AssociationProperty、AssociationPropertyMetadata、Label 写在 variable 的 `description` 中:
152
+
153
+ ```hcl
154
+ variable "zone_id" {
155
+ type = string
156
+ description = <<EOT
157
+ {
158
+ "AssociationProperty": "ALIYUN::ECS::ZoneId",
159
+ "Label": {
160
+ "en": "Zone ID",
161
+ "zh-cn": "可用区"
162
+ }
163
+ }
164
+ EOT
165
+ }
166
+
167
+ variable "instance_type" {
168
+ type = string
169
+ description = <<EOT
170
+ {
171
+ "AssociationProperty": "ALIYUN::ECS::Instance::InstanceType",
172
+ "AssociationPropertyMetadata": {
173
+ "ZoneId": "$${zone_id}",
174
+ "InstanceChargeType": "$${instance_charge_type}"
175
+ },
176
+ "Label": {
177
+ "en": "Instance Type",
178
+ "zh-cn": "实例规格"
179
+ }
180
+ }
181
+ EOT
182
+ }
183
+ ```
184
+
185
+ > description 必须是合法 JSON。Metadata 中用 `$${var_name}` 引用其他变量(双 `$$` 避免 Terraform 插值)。
186
+
187
+ ### 模板级:.metadata 文件
188
+
189
+ 在 Terraform 目录下创建 `.metadata` 文件(JSON 格式),结构与 ROS Metadata 完全一致,用于定义 ParameterGroups 等。该文件会被 tf2ros.py 自动收入 Workspace 中(这是 ROS Terraform 模板的标准做法):
190
+
191
+ ```json
192
+ {
193
+ "ALIYUN::ROS::Interface": {
194
+ "ParameterGroups": [
195
+ {
196
+ "Parameters": ["zone_id", "vpc_id", "vswitch_id"],
197
+ "Label": {"default": "网络配置"}
198
+ },
199
+ {
200
+ "Parameters": ["instance_type", "system_disk_category"],
201
+ "Label": {"default": "ECS 配置"}
202
+ }
203
+ ]
204
+ }
205
+ }
206
+ ```