kdeploy 1.2.5 → 1.2.7
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.
- checksums.yaml +4 -4
- data/README.md +371 -419
- data/README_EN.md +1031 -0
- data/lib/kdeploy/cli.rb +7 -1
- data/lib/kdeploy/configuration.rb +53 -0
- data/lib/kdeploy/executor.rb +2 -2
- data/lib/kdeploy/version.rb +2 -1
- metadata +2 -43
data/README.md
CHANGED
|
@@ -8,147 +8,149 @@
|
|
|
8
8
|
\/ \/\__,_|\___| .__/|_|\___/ \__, |
|
|
9
9
|
|_| |___/
|
|
10
10
|
|
|
11
|
-
⚡
|
|
12
|
-
🚀
|
|
11
|
+
⚡ 轻量级无代理部署工具
|
|
12
|
+
🚀 自信部署,轻松扩展
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
一个用 Ruby 编写的轻量级、无代理的部署自动化工具。Kdeploy 使您能够使用 SSH 在多个服务器上部署应用程序、管理配置和执行任务,而无需在目标机器上安装任何代理或守护进程。
|
|
16
16
|
|
|
17
17
|
[](https://rubygems.org/gems/kdeploy)
|
|
18
18
|
[](https://github.com/kevin197011/kdeploy/actions/workflows/gem-push.yml)
|
|
19
19
|
[](https://opensource.org/licenses/MIT)
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- [
|
|
26
|
-
- [
|
|
27
|
-
- [
|
|
28
|
-
- [
|
|
29
|
-
- [
|
|
30
|
-
- [
|
|
31
|
-
- [
|
|
32
|
-
- [
|
|
33
|
-
- [
|
|
34
|
-
- [
|
|
35
|
-
- [
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
45
|
-
-
|
|
46
|
-
-
|
|
47
|
-
-
|
|
48
|
-
-
|
|
49
|
-
-
|
|
50
|
-
-
|
|
51
|
-
-
|
|
52
|
-
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
21
|
+
**语言**: [English](README_EN.md) | [中文](README.md)
|
|
22
|
+
|
|
23
|
+
## 目录
|
|
24
|
+
|
|
25
|
+
- [功能特性](#-功能特性)
|
|
26
|
+
- [安装](#-安装)
|
|
27
|
+
- [快速开始](#-快速开始)
|
|
28
|
+
- [使用指南](#-使用指南)
|
|
29
|
+
- [配置](#-配置)
|
|
30
|
+
- [高级用法](#-高级用法)
|
|
31
|
+
- [错误处理](#-错误处理)
|
|
32
|
+
- [最佳实践](#-最佳实践)
|
|
33
|
+
- [故障排除](#-故障排除)
|
|
34
|
+
- [架构](#-架构)
|
|
35
|
+
- [开发](#-开发)
|
|
36
|
+
- [贡献](#-贡献)
|
|
37
|
+
- [许可证](#-许可证)
|
|
38
|
+
|
|
39
|
+
## 🌟 功能特性
|
|
40
|
+
|
|
41
|
+
### 核心功能
|
|
42
|
+
|
|
43
|
+
- 🔑 **无代理远程部署**: 使用 SSH 进行安全的远程执行,无需安装代理
|
|
44
|
+
- 📝 **优雅的 Ruby DSL**: 简单而富有表现力的任务定义语法
|
|
45
|
+
- 🚀 **并发执行**: 跨多个主机的高效并行任务处理
|
|
46
|
+
- 📤 **文件上传支持**: 通过 SCP 轻松部署文件和模板
|
|
47
|
+
- 📊 **任务状态跟踪**: 实时执行监控,提供详细输出
|
|
48
|
+
- 🔄 **ERB 模板支持**: 支持变量替换的动态配置生成
|
|
49
|
+
- 🎯 **基于角色的部署**: 针对特定服务器角色进行有组织的部署
|
|
50
|
+
- 🔍 **试运行模式**: 在执行前预览任务,不进行实际更改
|
|
51
|
+
- 🎨 **彩色输出**: 直观的颜色方案(绿色:成功,红色:错误,黄色:警告)
|
|
52
|
+
- ⚙️ **灵活的主机定位**: 在特定主机、角色或所有主机上执行任务
|
|
53
|
+
- 🔐 **多种身份验证方法**: 支持 SSH 密钥和密码身份验证
|
|
54
|
+
- 📈 **执行时间跟踪**: 监控任务执行持续时间以进行性能分析
|
|
55
|
+
|
|
56
|
+
### 技术特性
|
|
57
|
+
|
|
58
|
+
- **线程安全执行**: 基于 `concurrent-ruby` 实现可靠的并行处理
|
|
59
|
+
- **自定义错误处理**: 详细的错误类型,便于调试
|
|
60
|
+
- **配置管理**: 集中式配置,提供合理的默认值
|
|
61
|
+
- **可扩展架构**: 模块化设计,易于扩展
|
|
62
|
+
- **Shell 自动补全**: 支持 Bash 和 Zsh 的自动补全
|
|
63
|
+
|
|
64
|
+
## 📦 安装
|
|
65
|
+
|
|
66
|
+
### 要求
|
|
65
67
|
|
|
66
68
|
- Ruby >= 2.7.0
|
|
67
|
-
- SSH
|
|
68
|
-
- SSH
|
|
69
|
+
- 对目标服务器的 SSH 访问权限
|
|
70
|
+
- 已配置 SSH 密钥或密码身份验证
|
|
69
71
|
|
|
70
|
-
###
|
|
72
|
+
### 通过 RubyGems 安装
|
|
71
73
|
|
|
72
74
|
```bash
|
|
73
75
|
gem install kdeploy
|
|
74
76
|
```
|
|
75
77
|
|
|
76
|
-
###
|
|
78
|
+
### 通过 Bundler 安装
|
|
77
79
|
|
|
78
|
-
|
|
80
|
+
将以下行添加到应用程序的 `Gemfile` 中:
|
|
79
81
|
|
|
80
82
|
```ruby
|
|
81
83
|
gem 'kdeploy'
|
|
82
84
|
```
|
|
83
85
|
|
|
84
|
-
|
|
86
|
+
然后执行:
|
|
85
87
|
|
|
86
88
|
```bash
|
|
87
89
|
bundle install
|
|
88
90
|
```
|
|
89
91
|
|
|
90
|
-
###
|
|
92
|
+
### 验证安装
|
|
91
93
|
|
|
92
94
|
```bash
|
|
93
95
|
kdeploy version
|
|
94
96
|
```
|
|
95
97
|
|
|
96
|
-
|
|
98
|
+
您应该看到版本信息和横幅。
|
|
97
99
|
|
|
98
|
-
### Shell
|
|
100
|
+
### Shell 自动补全
|
|
99
101
|
|
|
100
|
-
Kdeploy
|
|
102
|
+
Kdeploy 在安装期间自动配置 shell 自动补全。如果需要,可以手动添加到 shell 配置中:
|
|
101
103
|
|
|
102
|
-
|
|
104
|
+
**对于 Bash** (`~/.bashrc`):
|
|
103
105
|
```bash
|
|
104
106
|
source "$(gem contents kdeploy | grep kdeploy.bash)"
|
|
105
107
|
```
|
|
106
108
|
|
|
107
|
-
|
|
109
|
+
**对于 Zsh** (`~/.zshrc`):
|
|
108
110
|
```bash
|
|
109
111
|
source "$(gem contents kdeploy | grep kdeploy.zsh)"
|
|
110
112
|
autoload -Uz compinit && compinit
|
|
111
113
|
```
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
1.
|
|
115
|
-
2.
|
|
115
|
+
添加配置后:
|
|
116
|
+
1. 对于 Bash: `source ~/.bashrc`
|
|
117
|
+
2. 对于 Zsh: `source ~/.zshrc`
|
|
116
118
|
|
|
117
|
-
|
|
118
|
-
-
|
|
119
|
-
-
|
|
120
|
-
-
|
|
119
|
+
现在您可以使用 Tab 补全:
|
|
120
|
+
- 命令: `kdeploy [TAB]`
|
|
121
|
+
- 文件路径: `kdeploy execute [TAB]`
|
|
122
|
+
- 选项: `kdeploy execute deploy.rb [TAB]`
|
|
121
123
|
|
|
122
|
-
## 🚀
|
|
124
|
+
## 🚀 快速开始
|
|
123
125
|
|
|
124
|
-
### 1.
|
|
126
|
+
### 1. 初始化新项目
|
|
125
127
|
|
|
126
128
|
```bash
|
|
127
129
|
kdeploy init my-deployment
|
|
128
130
|
```
|
|
129
131
|
|
|
130
|
-
|
|
131
|
-
- `deploy.rb` -
|
|
132
|
-
- `config/` -
|
|
133
|
-
- `README.md` -
|
|
132
|
+
这将创建一个新目录,包含:
|
|
133
|
+
- `deploy.rb` - 主部署配置文件
|
|
134
|
+
- `config/` - 配置文件和模板目录
|
|
135
|
+
- `README.md` - 项目文档
|
|
134
136
|
|
|
135
|
-
### 2.
|
|
137
|
+
### 2. 配置主机和任务
|
|
136
138
|
|
|
137
|
-
|
|
139
|
+
编辑 `deploy.rb`:
|
|
138
140
|
|
|
139
141
|
```ruby
|
|
140
|
-
#
|
|
142
|
+
# 定义主机
|
|
141
143
|
host "web01", user: "ubuntu", ip: "10.0.0.1", key: "~/.ssh/id_rsa"
|
|
142
144
|
host "web02", user: "ubuntu", ip: "10.0.0.2", key: "~/.ssh/id_rsa"
|
|
143
145
|
|
|
144
|
-
#
|
|
146
|
+
# 定义角色
|
|
145
147
|
role :web, %w[web01 web02]
|
|
146
148
|
|
|
147
|
-
#
|
|
149
|
+
# 定义部署任务
|
|
148
150
|
task :deploy, roles: :web do
|
|
149
151
|
run <<~SHELL
|
|
150
152
|
sudo systemctl stop nginx
|
|
151
|
-
echo "
|
|
153
|
+
echo "正在部署应用程序..."
|
|
152
154
|
SHELL
|
|
153
155
|
|
|
154
156
|
upload_template "./config/nginx.conf.erb", "/etc/nginx/nginx.conf",
|
|
@@ -159,64 +161,64 @@ task :deploy, roles: :web do
|
|
|
159
161
|
end
|
|
160
162
|
```
|
|
161
163
|
|
|
162
|
-
### 3.
|
|
164
|
+
### 3. 运行部署
|
|
163
165
|
|
|
164
166
|
```bash
|
|
165
167
|
kdeploy execute deploy.rb deploy
|
|
166
168
|
```
|
|
167
169
|
|
|
168
|
-
## 📖
|
|
170
|
+
## 📖 使用指南
|
|
169
171
|
|
|
170
|
-
###
|
|
172
|
+
### 命令参考
|
|
171
173
|
|
|
172
174
|
#### `kdeploy init [DIR]`
|
|
173
175
|
|
|
174
|
-
|
|
176
|
+
初始化新的部署项目。
|
|
175
177
|
|
|
176
178
|
```bash
|
|
177
|
-
#
|
|
179
|
+
# 在当前目录初始化
|
|
178
180
|
kdeploy init .
|
|
179
181
|
|
|
180
|
-
#
|
|
182
|
+
# 在指定目录初始化
|
|
181
183
|
kdeploy init my-deployment
|
|
182
184
|
```
|
|
183
185
|
|
|
184
186
|
#### `kdeploy execute TASK_FILE [TASK]`
|
|
185
187
|
|
|
186
|
-
|
|
188
|
+
从配置文件执行部署任务。
|
|
187
189
|
|
|
188
|
-
|
|
190
|
+
**基本用法:**
|
|
189
191
|
```bash
|
|
190
|
-
#
|
|
192
|
+
# 执行文件中的所有任务
|
|
191
193
|
kdeploy execute deploy.rb
|
|
192
194
|
|
|
193
|
-
#
|
|
195
|
+
# 执行特定任务
|
|
194
196
|
kdeploy execute deploy.rb deploy_web
|
|
195
197
|
```
|
|
196
198
|
|
|
197
|
-
|
|
198
|
-
- `--limit HOSTS`:
|
|
199
|
-
- `--parallel NUM`:
|
|
200
|
-
- `--dry-run`:
|
|
199
|
+
**选项:**
|
|
200
|
+
- `--limit HOSTS`: 限制执行到特定主机(逗号分隔)
|
|
201
|
+
- `--parallel NUM`: 并行执行数量(默认: 10)
|
|
202
|
+
- `--dry-run`: 预览模式 - 显示将要执行的操作而不实际执行
|
|
201
203
|
|
|
202
|
-
|
|
204
|
+
**示例:**
|
|
203
205
|
```bash
|
|
204
|
-
#
|
|
206
|
+
# 预览部署而不执行
|
|
205
207
|
kdeploy execute deploy.rb deploy_web --dry-run
|
|
206
208
|
|
|
207
|
-
#
|
|
209
|
+
# 仅在特定主机上执行
|
|
208
210
|
kdeploy execute deploy.rb deploy_web --limit web01,web02
|
|
209
211
|
|
|
210
|
-
#
|
|
212
|
+
# 使用自定义并行数量
|
|
211
213
|
kdeploy execute deploy.rb deploy_web --parallel 5
|
|
212
214
|
|
|
213
|
-
#
|
|
215
|
+
# 组合选项
|
|
214
216
|
kdeploy execute deploy.rb deploy_web --limit web01 --parallel 3 --dry-run
|
|
215
217
|
```
|
|
216
218
|
|
|
217
219
|
#### `kdeploy version`
|
|
218
220
|
|
|
219
|
-
|
|
221
|
+
显示版本信息。
|
|
220
222
|
|
|
221
223
|
```bash
|
|
222
224
|
kdeploy version
|
|
@@ -224,34 +226,34 @@ kdeploy version
|
|
|
224
226
|
|
|
225
227
|
#### `kdeploy help [COMMAND]`
|
|
226
228
|
|
|
227
|
-
|
|
229
|
+
显示帮助信息。
|
|
228
230
|
|
|
229
231
|
```bash
|
|
230
|
-
#
|
|
232
|
+
# 显示一般帮助
|
|
231
233
|
kdeploy help
|
|
232
234
|
|
|
233
|
-
#
|
|
235
|
+
# 显示特定命令的帮助
|
|
234
236
|
kdeploy help execute
|
|
235
237
|
```
|
|
236
238
|
|
|
237
|
-
###
|
|
239
|
+
### 主机定义
|
|
238
240
|
|
|
239
|
-
####
|
|
241
|
+
#### 基本主机配置
|
|
240
242
|
|
|
241
243
|
```ruby
|
|
242
|
-
#
|
|
244
|
+
# 使用 SSH 密钥的单个主机
|
|
243
245
|
host "web01",
|
|
244
246
|
user: "ubuntu",
|
|
245
247
|
ip: "10.0.0.1",
|
|
246
248
|
key: "~/.ssh/id_rsa"
|
|
247
249
|
|
|
248
|
-
#
|
|
250
|
+
# 使用密码身份验证的主机
|
|
249
251
|
host "web02",
|
|
250
252
|
user: "admin",
|
|
251
253
|
ip: "10.0.0.2",
|
|
252
254
|
password: "your-password"
|
|
253
255
|
|
|
254
|
-
#
|
|
256
|
+
# 使用自定义 SSH 端口的主机
|
|
255
257
|
host "web03",
|
|
256
258
|
user: "ubuntu",
|
|
257
259
|
ip: "10.0.0.3",
|
|
@@ -259,22 +261,22 @@ host "web03",
|
|
|
259
261
|
port: 2222
|
|
260
262
|
```
|
|
261
263
|
|
|
262
|
-
####
|
|
264
|
+
#### 主机配置选项
|
|
263
265
|
|
|
264
|
-
|
|
|
265
|
-
|
|
266
|
-
| `user` | String |
|
|
267
|
-
| `ip` | String |
|
|
268
|
-
| `key` | String |
|
|
269
|
-
| `password` | String |
|
|
270
|
-
| `port` | Integer |
|
|
266
|
+
| 选项 | 类型 | 必需 | 描述 |
|
|
267
|
+
|------|------|------|------|
|
|
268
|
+
| `user` | String | 是 | SSH 用户名 |
|
|
269
|
+
| `ip` | String | 是 | 服务器 IP 地址或主机名 |
|
|
270
|
+
| `key` | String | 否* | SSH 私钥文件路径 |
|
|
271
|
+
| `password` | String | 否* | SSH 密码 |
|
|
272
|
+
| `port` | Integer | 否 | SSH 端口(默认: 22) |
|
|
271
273
|
|
|
272
|
-
\*
|
|
274
|
+
\* 身份验证需要 `key` 或 `password` 之一。
|
|
273
275
|
|
|
274
|
-
####
|
|
276
|
+
#### 动态主机定义
|
|
275
277
|
|
|
276
278
|
```ruby
|
|
277
|
-
#
|
|
279
|
+
# 以编程方式定义多个主机
|
|
278
280
|
%w[web01 web02 web03].each do |name|
|
|
279
281
|
host name,
|
|
280
282
|
user: "ubuntu",
|
|
@@ -282,7 +284,7 @@ host "web03",
|
|
|
282
284
|
key: "~/.ssh/id_rsa"
|
|
283
285
|
end
|
|
284
286
|
|
|
285
|
-
#
|
|
287
|
+
# 从外部源定义主机
|
|
286
288
|
require 'yaml'
|
|
287
289
|
hosts_config = YAML.load_file('hosts.yml')
|
|
288
290
|
hosts_config.each do |name, config|
|
|
@@ -290,35 +292,35 @@ hosts_config.each do |name, config|
|
|
|
290
292
|
end
|
|
291
293
|
```
|
|
292
294
|
|
|
293
|
-
###
|
|
295
|
+
### 角色管理
|
|
294
296
|
|
|
295
|
-
|
|
297
|
+
角色允许您对主机进行分组,并在任务中集体定位它们。
|
|
296
298
|
|
|
297
299
|
```ruby
|
|
298
|
-
#
|
|
300
|
+
# 定义角色
|
|
299
301
|
role :web, %w[web01 web02 web03]
|
|
300
302
|
role :db, %w[db01 db02]
|
|
301
303
|
role :cache, %w[cache01]
|
|
302
304
|
role :all, %w[web01 web02 web03 db01 db02 cache01]
|
|
303
305
|
|
|
304
|
-
#
|
|
306
|
+
# 在任务中使用角色
|
|
305
307
|
task :deploy_web, roles: :web do
|
|
306
|
-
#
|
|
308
|
+
# 在所有 Web 服务器上执行
|
|
307
309
|
end
|
|
308
310
|
|
|
309
311
|
task :backup_db, roles: :db do
|
|
310
|
-
#
|
|
312
|
+
# 在所有数据库服务器上执行
|
|
311
313
|
end
|
|
312
314
|
|
|
313
|
-
#
|
|
315
|
+
# 多个角色
|
|
314
316
|
task :deploy_all, roles: [:web, :cache] do
|
|
315
|
-
#
|
|
317
|
+
# 在 Web 和缓存服务器上执行
|
|
316
318
|
end
|
|
317
319
|
```
|
|
318
320
|
|
|
319
|
-
###
|
|
321
|
+
### 任务定义
|
|
320
322
|
|
|
321
|
-
####
|
|
323
|
+
#### 基本任务
|
|
322
324
|
|
|
323
325
|
```ruby
|
|
324
326
|
task :hello do
|
|
@@ -326,7 +328,7 @@ task :hello do
|
|
|
326
328
|
end
|
|
327
329
|
```
|
|
328
330
|
|
|
329
|
-
####
|
|
331
|
+
#### 基于角色的任务
|
|
330
332
|
|
|
331
333
|
```ruby
|
|
332
334
|
task :deploy_web, roles: :web do
|
|
@@ -334,7 +336,7 @@ task :deploy_web, roles: :web do
|
|
|
334
336
|
end
|
|
335
337
|
```
|
|
336
338
|
|
|
337
|
-
####
|
|
339
|
+
#### 特定主机任务
|
|
338
340
|
|
|
339
341
|
```ruby
|
|
340
342
|
task :maintenance, on: %w[web01] do
|
|
@@ -346,44 +348,44 @@ task :maintenance, on: %w[web01] do
|
|
|
346
348
|
end
|
|
347
349
|
```
|
|
348
350
|
|
|
349
|
-
####
|
|
351
|
+
#### 多命令任务
|
|
350
352
|
|
|
351
353
|
```ruby
|
|
352
354
|
task :deploy, roles: :web do
|
|
353
|
-
#
|
|
355
|
+
# 停止服务
|
|
354
356
|
run "sudo systemctl stop nginx"
|
|
355
357
|
|
|
356
|
-
#
|
|
358
|
+
# 上传配置
|
|
357
359
|
upload "./config/nginx.conf", "/etc/nginx/nginx.conf"
|
|
358
360
|
|
|
359
|
-
#
|
|
361
|
+
# 启动服务
|
|
360
362
|
run "sudo systemctl start nginx"
|
|
361
363
|
|
|
362
|
-
#
|
|
364
|
+
# 验证状态
|
|
363
365
|
run "sudo systemctl status nginx"
|
|
364
366
|
end
|
|
365
367
|
```
|
|
366
368
|
|
|
367
|
-
####
|
|
369
|
+
#### 任务选项
|
|
368
370
|
|
|
369
|
-
|
|
|
370
|
-
|
|
371
|
-
| `roles` | Symbol/Array |
|
|
372
|
-
| `on` | Array |
|
|
371
|
+
| 选项 | 类型 | 描述 |
|
|
372
|
+
|------|------|------|
|
|
373
|
+
| `roles` | Symbol/Array | 在具有指定角色的主机上执行 |
|
|
374
|
+
| `on` | Array | 在特定主机上执行 |
|
|
373
375
|
|
|
374
|
-
|
|
376
|
+
**注意**: 如果未指定 `roles` 或 `on`,任务将在所有已定义的主机上执行。
|
|
375
377
|
|
|
376
|
-
###
|
|
378
|
+
### 命令类型
|
|
377
379
|
|
|
378
|
-
#### `run` -
|
|
380
|
+
#### `run` - 执行 Shell 命令
|
|
379
381
|
|
|
380
|
-
|
|
382
|
+
在远程服务器上执行命令。
|
|
381
383
|
|
|
382
384
|
```ruby
|
|
383
|
-
#
|
|
385
|
+
# 单行命令
|
|
384
386
|
run "sudo systemctl restart nginx"
|
|
385
387
|
|
|
386
|
-
#
|
|
388
|
+
# 多行命令(推荐用于复杂命令)
|
|
387
389
|
run <<~SHELL
|
|
388
390
|
cd /var/www/app
|
|
389
391
|
git pull origin main
|
|
@@ -392,24 +394,24 @@ run <<~SHELL
|
|
|
392
394
|
SHELL
|
|
393
395
|
```
|
|
394
396
|
|
|
395
|
-
|
|
397
|
+
**最佳实践**: 对多行命令使用 heredoc (`<<~SHELL`) 以提高可读性。
|
|
396
398
|
|
|
397
|
-
#### `upload` -
|
|
399
|
+
#### `upload` - 上传文件
|
|
398
400
|
|
|
399
|
-
|
|
401
|
+
将文件上传到远程服务器。
|
|
400
402
|
|
|
401
403
|
```ruby
|
|
402
404
|
upload "./config/nginx.conf", "/etc/nginx/nginx.conf"
|
|
403
405
|
upload "./scripts/deploy.sh", "/tmp/deploy.sh"
|
|
404
406
|
```
|
|
405
407
|
|
|
406
|
-
|
|
407
|
-
- `source`:
|
|
408
|
-
- `destination`:
|
|
408
|
+
**参数:**
|
|
409
|
+
- `source`: 本地文件路径
|
|
410
|
+
- `destination`: 远程文件路径
|
|
409
411
|
|
|
410
|
-
#### `upload_template` -
|
|
412
|
+
#### `upload_template` - 上传 ERB 模板
|
|
411
413
|
|
|
412
|
-
|
|
414
|
+
上传并渲染 ERB 模板,支持变量替换。
|
|
413
415
|
|
|
414
416
|
```ruby
|
|
415
417
|
upload_template "./config/nginx.conf.erb", "/etc/nginx/nginx.conf",
|
|
@@ -418,18 +420,18 @@ upload_template "./config/nginx.conf.erb", "/etc/nginx/nginx.conf",
|
|
|
418
420
|
worker_processes: 4
|
|
419
421
|
```
|
|
420
422
|
|
|
421
|
-
|
|
422
|
-
- `source`:
|
|
423
|
-
- `destination`:
|
|
424
|
-
- `variables`:
|
|
423
|
+
**参数:**
|
|
424
|
+
- `source`: 本地 ERB 模板文件路径
|
|
425
|
+
- `destination`: 远程文件路径
|
|
426
|
+
- `variables`: 用于模板渲染的变量哈希
|
|
425
427
|
|
|
426
|
-
###
|
|
428
|
+
### 模板支持
|
|
427
429
|
|
|
428
|
-
Kdeploy
|
|
430
|
+
Kdeploy 支持 ERB(嵌入式 Ruby)模板,用于动态配置生成。
|
|
429
431
|
|
|
430
|
-
####
|
|
432
|
+
#### 创建模板
|
|
431
433
|
|
|
432
|
-
|
|
434
|
+
创建 ERB 模板文件(例如,`config/nginx.conf.erb`):
|
|
433
435
|
|
|
434
436
|
```erb
|
|
435
437
|
user nginx;
|
|
@@ -465,7 +467,7 @@ http {
|
|
|
465
467
|
}
|
|
466
468
|
```
|
|
467
469
|
|
|
468
|
-
####
|
|
470
|
+
#### 使用模板
|
|
469
471
|
|
|
470
472
|
```ruby
|
|
471
473
|
task :deploy_config do
|
|
@@ -477,17 +479,17 @@ task :deploy_config do
|
|
|
477
479
|
end
|
|
478
480
|
```
|
|
479
481
|
|
|
480
|
-
####
|
|
482
|
+
#### 模板特性
|
|
481
483
|
|
|
482
|
-
-
|
|
483
|
-
-
|
|
484
|
-
-
|
|
485
|
-
-
|
|
486
|
-
- Ruby
|
|
484
|
+
- 完整的 ERB 语法支持
|
|
485
|
+
- 变量替换
|
|
486
|
+
- 条件逻辑
|
|
487
|
+
- 循环和迭代
|
|
488
|
+
- Ruby 代码执行
|
|
487
489
|
|
|
488
|
-
###
|
|
490
|
+
### 清单块
|
|
489
491
|
|
|
490
|
-
|
|
492
|
+
使用 `inventory` 块来组织主机定义:
|
|
491
493
|
|
|
492
494
|
```ruby
|
|
493
495
|
inventory do
|
|
@@ -497,28 +499,28 @@ inventory do
|
|
|
497
499
|
end
|
|
498
500
|
```
|
|
499
501
|
|
|
500
|
-
## ⚙️
|
|
502
|
+
## ⚙️ 配置
|
|
501
503
|
|
|
502
|
-
###
|
|
504
|
+
### 默认配置
|
|
503
505
|
|
|
504
|
-
Kdeploy
|
|
506
|
+
Kdeploy 使用可自定义的合理默认值:
|
|
505
507
|
|
|
506
|
-
-
|
|
507
|
-
- **SSH
|
|
508
|
-
-
|
|
508
|
+
- **默认并行数量**: 10 个并发执行
|
|
509
|
+
- **SSH 超时**: 30 秒
|
|
510
|
+
- **主机密钥验证**: 禁用(为方便起见,在生产环境中启用)
|
|
509
511
|
|
|
510
|
-
###
|
|
512
|
+
### 环境变量
|
|
511
513
|
|
|
512
|
-
|
|
514
|
+
您可以使用环境变量覆盖默认值:
|
|
513
515
|
|
|
514
516
|
```bash
|
|
515
517
|
export KDEPLOY_PARALLEL=5
|
|
516
518
|
export KDEPLOY_SSH_TIMEOUT=60
|
|
517
519
|
```
|
|
518
520
|
|
|
519
|
-
###
|
|
521
|
+
### 配置文件
|
|
520
522
|
|
|
521
|
-
|
|
523
|
+
对于项目特定的配置,创建 `.kdeploy.yml`:
|
|
522
524
|
|
|
523
525
|
```yaml
|
|
524
526
|
parallel: 5
|
|
@@ -526,11 +528,13 @@ ssh_timeout: 60
|
|
|
526
528
|
verify_host_key: true
|
|
527
529
|
```
|
|
528
530
|
|
|
529
|
-
|
|
531
|
+
配置文件会自动从当前目录向上查找,直到找到 `.kdeploy.yml` 文件。
|
|
530
532
|
|
|
531
|
-
|
|
533
|
+
## 🔧 高级用法
|
|
532
534
|
|
|
533
|
-
|
|
535
|
+
### 条件执行
|
|
536
|
+
|
|
537
|
+
在部署文件中使用 Ruby 条件:
|
|
534
538
|
|
|
535
539
|
```ruby
|
|
536
540
|
task :deploy do
|
|
@@ -546,38 +550,38 @@ task :deploy do
|
|
|
546
550
|
end
|
|
547
551
|
```
|
|
548
552
|
|
|
549
|
-
###
|
|
553
|
+
### 循环主机
|
|
550
554
|
|
|
551
555
|
```ruby
|
|
552
|
-
#
|
|
556
|
+
# 根据主机执行不同的命令
|
|
553
557
|
task :custom_setup do
|
|
554
558
|
@hosts.each do |name, config|
|
|
555
559
|
if name.start_with?('web')
|
|
556
|
-
run "echo 'Web
|
|
560
|
+
run "echo 'Web 服务器: #{name}'"
|
|
557
561
|
elsif name.start_with?('db')
|
|
558
|
-
run "echo '
|
|
562
|
+
run "echo '数据库服务器: #{name}'"
|
|
559
563
|
end
|
|
560
564
|
end
|
|
561
565
|
end
|
|
562
566
|
```
|
|
563
567
|
|
|
564
|
-
###
|
|
568
|
+
### 任务中的错误处理
|
|
565
569
|
|
|
566
570
|
```ruby
|
|
567
571
|
task :deploy do
|
|
568
|
-
run "sudo systemctl stop nginx" || raise "
|
|
572
|
+
run "sudo systemctl stop nginx" || raise "停止 nginx 失败"
|
|
569
573
|
upload "./config/nginx.conf", "/etc/nginx/nginx.conf"
|
|
570
|
-
run "sudo systemctl start nginx" || raise "
|
|
574
|
+
run "sudo systemctl start nginx" || raise "启动 nginx 失败"
|
|
571
575
|
end
|
|
572
576
|
```
|
|
573
577
|
|
|
574
|
-
###
|
|
578
|
+
### 使用外部库
|
|
575
579
|
|
|
576
580
|
```ruby
|
|
577
581
|
require 'yaml'
|
|
578
582
|
require 'json'
|
|
579
583
|
|
|
580
|
-
#
|
|
584
|
+
# 从外部文件加载配置
|
|
581
585
|
config = YAML.load_file('config.yml')
|
|
582
586
|
|
|
583
587
|
task :deploy do
|
|
@@ -587,86 +591,48 @@ task :deploy do
|
|
|
587
591
|
end
|
|
588
592
|
```
|
|
589
593
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
While Kdeploy doesn't have built-in task dependencies, you can achieve this with Ruby:
|
|
593
|
-
|
|
594
|
-
```ruby
|
|
595
|
-
task :setup do
|
|
596
|
-
run "echo 'Setting up...'"
|
|
597
|
-
end
|
|
598
|
-
|
|
599
|
-
task :deploy do
|
|
600
|
-
# Manually call setup task
|
|
601
|
-
self.class.kdeploy_tasks[:setup][:block].call.each do |cmd|
|
|
602
|
-
case cmd[:type]
|
|
603
|
-
when :run
|
|
604
|
-
run cmd[:command]
|
|
605
|
-
when :upload
|
|
606
|
-
upload cmd[:source], cmd[:destination]
|
|
607
|
-
end
|
|
608
|
-
end
|
|
609
|
-
|
|
610
|
-
run "echo 'Deploying...'"
|
|
611
|
-
end
|
|
612
|
-
```
|
|
613
|
-
|
|
614
|
-
## 🚨 Error Handling
|
|
615
|
-
|
|
616
|
-
### Error Types
|
|
617
|
-
|
|
618
|
-
Kdeploy provides specific error types for better debugging:
|
|
594
|
+
## 🚨 错误处理
|
|
619
595
|
|
|
620
|
-
|
|
621
|
-
- `Kdeploy::HostNotFoundError` - Host not found
|
|
622
|
-
- `Kdeploy::SSHError` - SSH operation failed
|
|
623
|
-
- `Kdeploy::SCPError` - SCP upload failed
|
|
624
|
-
- `Kdeploy::TemplateError` - Template rendering failed
|
|
625
|
-
- `Kdeploy::ConfigurationError` - Configuration error
|
|
626
|
-
- `Kdeploy::FileNotFoundError` - File not found
|
|
596
|
+
### 错误类型
|
|
627
597
|
|
|
628
|
-
|
|
598
|
+
Kdeploy 提供特定的错误类型以便更好地调试:
|
|
629
599
|
|
|
630
|
-
|
|
631
|
-
-
|
|
632
|
-
-
|
|
633
|
-
-
|
|
634
|
-
-
|
|
600
|
+
- `Kdeploy::TaskNotFoundError` - 任务未找到
|
|
601
|
+
- `Kdeploy::HostNotFoundError` - 主机未找到
|
|
602
|
+
- `Kdeploy::SSHError` - SSH 操作失败
|
|
603
|
+
- `Kdeploy::SCPError` - SCP 上传失败
|
|
604
|
+
- `Kdeploy::TemplateError` - 模板渲染失败
|
|
605
|
+
- `Kdeploy::ConfigurationError` - 配置错误
|
|
606
|
+
- `Kdeploy::FileNotFoundError` - 文件未找到
|
|
635
607
|
|
|
636
|
-
###
|
|
608
|
+
### 错误输出
|
|
637
609
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
end
|
|
644
|
-
rescue Kdeploy::SSHError => e
|
|
645
|
-
puts "SSH Error: #{e.message}"
|
|
646
|
-
# Handle error
|
|
647
|
-
end
|
|
648
|
-
```
|
|
610
|
+
错误显示包括:
|
|
611
|
+
- 红色颜色编码
|
|
612
|
+
- 详细的错误消息
|
|
613
|
+
- 主机信息
|
|
614
|
+
- 原始错误上下文
|
|
649
615
|
|
|
650
|
-
## 💡
|
|
616
|
+
## 💡 最佳实践
|
|
651
617
|
|
|
652
|
-
### 1.
|
|
618
|
+
### 1. 对多行命令使用 Heredoc
|
|
653
619
|
|
|
654
620
|
```ruby
|
|
655
|
-
# ✅
|
|
621
|
+
# ✅ 好的做法
|
|
656
622
|
run <<~SHELL
|
|
657
623
|
cd /var/www/app
|
|
658
624
|
git pull origin main
|
|
659
625
|
bundle install
|
|
660
626
|
SHELL
|
|
661
627
|
|
|
662
|
-
# ❌
|
|
628
|
+
# ❌ 避免
|
|
663
629
|
run "cd /var/www/app && git pull origin main && bundle install"
|
|
664
630
|
```
|
|
665
631
|
|
|
666
|
-
### 2.
|
|
632
|
+
### 2. 使用角色进行组织
|
|
667
633
|
|
|
668
634
|
```ruby
|
|
669
|
-
# ✅
|
|
635
|
+
# ✅ 好的做法 - 使用角色进行组织
|
|
670
636
|
role :web, %w[web01 web02]
|
|
671
637
|
role :db, %w[db01 db02]
|
|
672
638
|
|
|
@@ -674,274 +640,259 @@ task :deploy_web, roles: :web do
|
|
|
674
640
|
# ...
|
|
675
641
|
end
|
|
676
642
|
|
|
677
|
-
# ❌
|
|
643
|
+
# ❌ 避免 - 硬编码主机名
|
|
678
644
|
task :deploy do
|
|
679
|
-
#
|
|
645
|
+
# 难以维护
|
|
680
646
|
end
|
|
681
647
|
```
|
|
682
648
|
|
|
683
|
-
### 3.
|
|
649
|
+
### 3. 使用模板进行动态配置
|
|
684
650
|
|
|
685
651
|
```ruby
|
|
686
|
-
# ✅
|
|
652
|
+
# ✅ 好的做法 - 使用模板
|
|
687
653
|
upload_template "./config/nginx.conf.erb", "/etc/nginx/nginx.conf",
|
|
688
654
|
domain_name: "example.com",
|
|
689
655
|
port: 3000
|
|
690
656
|
|
|
691
|
-
# ❌
|
|
657
|
+
# ❌ 避免 - 硬编码值
|
|
692
658
|
run "echo 'server_name example.com;' > /etc/nginx/nginx.conf"
|
|
693
659
|
```
|
|
694
660
|
|
|
695
|
-
### 4.
|
|
661
|
+
### 4. 部署前验证
|
|
696
662
|
|
|
697
663
|
```ruby
|
|
698
664
|
task :deploy do
|
|
699
|
-
#
|
|
700
|
-
run "nginx -t" || raise "Nginx
|
|
665
|
+
# 验证配置
|
|
666
|
+
run "nginx -t" || raise "Nginx 配置无效"
|
|
701
667
|
|
|
702
|
-
#
|
|
668
|
+
# 部署
|
|
703
669
|
upload "./config/nginx.conf", "/etc/nginx/nginx.conf"
|
|
704
670
|
run "sudo systemctl reload nginx"
|
|
705
671
|
end
|
|
706
672
|
```
|
|
707
673
|
|
|
708
|
-
### 5.
|
|
674
|
+
### 5. 使用试运行进行测试
|
|
709
675
|
|
|
710
|
-
|
|
676
|
+
在实际部署之前,始终使用 `--dry-run` 进行测试:
|
|
711
677
|
|
|
712
678
|
```bash
|
|
713
679
|
kdeploy execute deploy.rb deploy_web --dry-run
|
|
714
680
|
```
|
|
715
681
|
|
|
716
|
-
### 6.
|
|
682
|
+
### 6. 正确组织文件
|
|
717
683
|
|
|
718
684
|
```
|
|
719
685
|
project/
|
|
720
|
-
├── deploy.rb #
|
|
721
|
-
├── config/ #
|
|
722
|
-
│ ├── nginx.conf.erb #
|
|
723
|
-
│ └── app.conf #
|
|
724
|
-
└── scripts/ #
|
|
686
|
+
├── deploy.rb # 主部署文件
|
|
687
|
+
├── config/ # 配置文件
|
|
688
|
+
│ ├── nginx.conf.erb # 模板
|
|
689
|
+
│ └── app.conf # 静态配置
|
|
690
|
+
└── scripts/ # 辅助脚本
|
|
725
691
|
└── deploy.sh
|
|
726
692
|
```
|
|
727
693
|
|
|
728
|
-
### 7.
|
|
694
|
+
### 7. 版本控制
|
|
729
695
|
|
|
730
|
-
-
|
|
731
|
-
-
|
|
732
|
-
-
|
|
696
|
+
- 提交 `deploy.rb` 和模板
|
|
697
|
+
- 使用 `.gitignore` 处理敏感文件
|
|
698
|
+
- 将密钥存储在环境变量中
|
|
733
699
|
|
|
734
|
-
### 8.
|
|
700
|
+
### 8. 并行执行
|
|
735
701
|
|
|
736
|
-
|
|
702
|
+
根据您的基础设施调整并行数量:
|
|
737
703
|
|
|
738
704
|
```bash
|
|
739
|
-
#
|
|
705
|
+
# 对于许多主机,增加并行数量
|
|
740
706
|
kdeploy execute deploy.rb deploy --parallel 20
|
|
741
707
|
|
|
742
|
-
#
|
|
708
|
+
# 对于有限资源,减少
|
|
743
709
|
kdeploy execute deploy.rb deploy --parallel 3
|
|
744
710
|
```
|
|
745
711
|
|
|
746
|
-
## 🔍
|
|
747
|
-
|
|
748
|
-
### Common Issues
|
|
749
|
-
|
|
750
|
-
#### SSH Authentication Failed
|
|
712
|
+
## 🔍 故障排除
|
|
751
713
|
|
|
752
|
-
|
|
714
|
+
### 常见问题
|
|
753
715
|
|
|
754
|
-
|
|
755
|
-
1. Verify SSH key path is correct
|
|
756
|
-
2. Check key permissions: `chmod 600 ~/.ssh/id_rsa`
|
|
757
|
-
3. Test SSH connection manually: `ssh user@host`
|
|
758
|
-
4. Verify username and IP address
|
|
716
|
+
#### SSH 身份验证失败
|
|
759
717
|
|
|
760
|
-
|
|
718
|
+
**问题**: `SSH authentication failed`
|
|
761
719
|
|
|
762
|
-
|
|
720
|
+
**解决方案**:
|
|
721
|
+
1. 验证 SSH 密钥路径是否正确
|
|
722
|
+
2. 检查密钥权限: `chmod 600 ~/.ssh/id_rsa`
|
|
723
|
+
3. 手动测试 SSH 连接: `ssh user@host`
|
|
724
|
+
4. 验证用户名和 IP 地址
|
|
763
725
|
|
|
764
|
-
|
|
765
|
-
1. Verify host names in task match defined hosts
|
|
766
|
-
2. Check role definitions
|
|
767
|
-
3. Verify `--limit` option if used
|
|
726
|
+
#### 主机未找到
|
|
768
727
|
|
|
769
|
-
|
|
728
|
+
**问题**: `No hosts found for task`
|
|
770
729
|
|
|
771
|
-
|
|
730
|
+
**解决方案**:
|
|
731
|
+
1. 验证任务中的主机名是否与已定义的主机匹配
|
|
732
|
+
2. 检查角色定义
|
|
733
|
+
3. 如果使用了 `--limit` 选项,请验证
|
|
772
734
|
|
|
773
|
-
|
|
774
|
-
1. Test commands manually on target server
|
|
775
|
-
2. Check user permissions (may need sudo)
|
|
776
|
-
3. Verify command syntax
|
|
777
|
-
4. Check server logs
|
|
735
|
+
#### 命令执行失败
|
|
778
736
|
|
|
779
|
-
|
|
737
|
+
**问题**: 远程服务器上的命令失败
|
|
780
738
|
|
|
781
|
-
|
|
739
|
+
**解决方案**:
|
|
740
|
+
1. 在目标服务器上手动测试命令
|
|
741
|
+
2. 检查用户权限(可能需要 sudo)
|
|
742
|
+
3. 验证命令语法
|
|
743
|
+
4. 检查服务器日志
|
|
782
744
|
|
|
783
|
-
|
|
784
|
-
1. Verify ERB syntax in template
|
|
785
|
-
2. Check all required variables are provided
|
|
786
|
-
3. Validate template file exists
|
|
787
|
-
4. Test template rendering locally
|
|
745
|
+
#### 模板渲染错误
|
|
788
746
|
|
|
789
|
-
|
|
747
|
+
**问题**: 模板上传失败
|
|
790
748
|
|
|
791
|
-
|
|
749
|
+
**解决方案**:
|
|
750
|
+
1. 验证模板中的 ERB 语法
|
|
751
|
+
2. 检查是否提供了所有必需的变量
|
|
752
|
+
3. 验证模板文件是否存在
|
|
753
|
+
4. 在本地测试模板渲染
|
|
792
754
|
|
|
793
|
-
|
|
794
|
-
1. Check network connectivity
|
|
795
|
-
2. Verify firewall rules
|
|
796
|
-
3. Increase timeout in configuration
|
|
797
|
-
4. Check SSH service on target server
|
|
755
|
+
#### 连接超时
|
|
798
756
|
|
|
799
|
-
|
|
757
|
+
**问题**: SSH 连接超时
|
|
800
758
|
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
759
|
+
**解决方案**:
|
|
760
|
+
1. 检查网络连接
|
|
761
|
+
2. 验证防火墙规则
|
|
762
|
+
3. 在配置中增加超时时间
|
|
763
|
+
4. 检查目标服务器上的 SSH 服务
|
|
806
764
|
|
|
807
|
-
|
|
765
|
+
## 🏗️ 架构
|
|
808
766
|
|
|
809
|
-
|
|
810
|
-
- Review example projects
|
|
811
|
-
- Read the documentation
|
|
812
|
-
- Ask in discussions
|
|
767
|
+
### 核心组件
|
|
813
768
|
|
|
814
|
-
|
|
769
|
+
- **CLI** (`cli.rb`): 使用 Thor 的命令行界面
|
|
770
|
+
- **DSL** (`dsl.rb`): 用于任务定义的领域特定语言
|
|
771
|
+
- **Executor** (`executor.rb`): SSH/SCP 执行引擎
|
|
772
|
+
- **Runner** (`runner.rb`): 并发任务执行协调器
|
|
773
|
+
- **CommandExecutor** (`command_executor.rb`): 单个命令执行
|
|
774
|
+
- **CommandGrouper** (`command_grouper.rb`): 命令分组逻辑
|
|
775
|
+
- **Template** (`template.rb`): ERB 模板渲染
|
|
776
|
+
- **Output** (`output.rb`): 输出格式化和显示
|
|
777
|
+
- **Configuration** (`configuration.rb`): 配置管理
|
|
778
|
+
- **Errors** (`errors.rb`): 自定义错误类型
|
|
815
779
|
|
|
816
|
-
###
|
|
780
|
+
### 执行流程
|
|
817
781
|
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
- **Template** (`template.rb`): ERB template rendering
|
|
825
|
-
- **Output** (`output.rb`): Output formatting and display
|
|
826
|
-
- **Configuration** (`configuration.rb`): Configuration management
|
|
827
|
-
- **Errors** (`errors.rb`): Custom error types
|
|
782
|
+
1. **解析配置**: 加载并解析 `deploy.rb`
|
|
783
|
+
2. **解析主机**: 根据任务定义确定目标主机
|
|
784
|
+
3. **分组命令**: 按类型分组命令以提高执行效率
|
|
785
|
+
4. **并发执行**: 跨主机并行运行任务
|
|
786
|
+
5. **收集结果**: 收集执行结果和状态
|
|
787
|
+
6. **显示输出**: 格式化并向用户显示结果
|
|
828
788
|
|
|
829
|
-
###
|
|
789
|
+
### 并发模型
|
|
830
790
|
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
6. **Display Output**: Format and display results to user
|
|
791
|
+
Kdeploy 使用带有固定线程池的 `concurrent-ruby`:
|
|
792
|
+
- 默认: 10 个并发执行
|
|
793
|
+
- 可通过 `--parallel` 选项配置
|
|
794
|
+
- 线程安全的结果收集
|
|
795
|
+
- 自动资源清理
|
|
837
796
|
|
|
838
|
-
|
|
797
|
+
## 🔧 开发
|
|
839
798
|
|
|
840
|
-
|
|
841
|
-
- Default: 10 concurrent executions
|
|
842
|
-
- Configurable via `--parallel` option
|
|
843
|
-
- Thread-safe result collection
|
|
844
|
-
- Automatic resource cleanup
|
|
845
|
-
|
|
846
|
-
## 🔧 Development
|
|
847
|
-
|
|
848
|
-
### Setup Development Environment
|
|
799
|
+
### 设置开发环境
|
|
849
800
|
|
|
850
801
|
```bash
|
|
851
|
-
#
|
|
802
|
+
# 克隆仓库
|
|
852
803
|
git clone https://github.com/kevin197011/kdeploy.git
|
|
853
804
|
cd kdeploy
|
|
854
805
|
|
|
855
|
-
#
|
|
806
|
+
# 安装依赖
|
|
856
807
|
bundle install
|
|
857
808
|
|
|
858
|
-
#
|
|
809
|
+
# 运行测试
|
|
859
810
|
bundle exec rspec
|
|
860
811
|
|
|
861
|
-
#
|
|
812
|
+
# 运行控制台
|
|
862
813
|
bin/console
|
|
863
814
|
```
|
|
864
815
|
|
|
865
|
-
###
|
|
816
|
+
### 项目结构
|
|
866
817
|
|
|
867
818
|
```
|
|
868
819
|
kdeploy/
|
|
869
820
|
├── lib/
|
|
870
821
|
│ └── kdeploy/
|
|
871
|
-
│ ├── cli.rb # CLI
|
|
872
|
-
│ ├── dsl.rb # DSL
|
|
873
|
-
│ ├── executor.rb # SSH/SCP
|
|
874
|
-
│ ├── runner.rb #
|
|
875
|
-
│ ├── command_executor.rb #
|
|
876
|
-
│ ├── command_grouper.rb #
|
|
877
|
-
│ ├── template.rb #
|
|
878
|
-
│ ├── output.rb #
|
|
879
|
-
│ ├── configuration.rb #
|
|
880
|
-
│ ├── errors.rb #
|
|
822
|
+
│ ├── cli.rb # CLI 接口
|
|
823
|
+
│ ├── dsl.rb # DSL 定义
|
|
824
|
+
│ ├── executor.rb # SSH/SCP 执行器
|
|
825
|
+
│ ├── runner.rb # 任务运行器
|
|
826
|
+
│ ├── command_executor.rb # 命令执行器
|
|
827
|
+
│ ├── command_grouper.rb # 命令分组器
|
|
828
|
+
│ ├── template.rb # 模板处理器
|
|
829
|
+
│ ├── output.rb # 输出接口
|
|
830
|
+
│ ├── configuration.rb # 配置
|
|
831
|
+
│ ├── errors.rb # 错误类型
|
|
881
832
|
│ └── ...
|
|
882
|
-
├── spec/ #
|
|
883
|
-
├── exe/ #
|
|
884
|
-
├── sample/ #
|
|
885
|
-
└── README.md #
|
|
833
|
+
├── spec/ # 测试
|
|
834
|
+
├── exe/ # 可执行文件
|
|
835
|
+
├── sample/ # 示例项目
|
|
836
|
+
└── README.md # 本文档
|
|
886
837
|
```
|
|
887
838
|
|
|
888
|
-
###
|
|
839
|
+
### 运行测试
|
|
889
840
|
|
|
890
841
|
```bash
|
|
891
|
-
#
|
|
842
|
+
# 运行所有测试
|
|
892
843
|
bundle exec rspec
|
|
893
844
|
|
|
894
|
-
#
|
|
845
|
+
# 运行特定测试文件
|
|
895
846
|
bundle exec rspec spec/kdeploy_spec.rb
|
|
896
847
|
|
|
897
|
-
#
|
|
848
|
+
# 运行覆盖率
|
|
898
849
|
COVERAGE=true bundle exec rspec
|
|
899
850
|
```
|
|
900
851
|
|
|
901
|
-
###
|
|
852
|
+
### 构建 Gem
|
|
902
853
|
|
|
903
854
|
```bash
|
|
904
|
-
#
|
|
855
|
+
# 构建 gem
|
|
905
856
|
gem build kdeploy.gemspec
|
|
906
857
|
|
|
907
|
-
#
|
|
858
|
+
# 本地安装
|
|
908
859
|
gem install ./kdeploy-*.gem
|
|
909
860
|
```
|
|
910
861
|
|
|
911
|
-
###
|
|
862
|
+
### 代码风格
|
|
912
863
|
|
|
913
|
-
|
|
864
|
+
项目使用 RuboCop 进行代码风格检查:
|
|
914
865
|
|
|
915
866
|
```bash
|
|
916
|
-
#
|
|
867
|
+
# 检查风格
|
|
917
868
|
bundle exec rubocop
|
|
918
869
|
|
|
919
|
-
#
|
|
870
|
+
# 自动修复问题
|
|
920
871
|
bundle exec rubocop -a
|
|
921
872
|
```
|
|
922
873
|
|
|
923
|
-
## 🤝
|
|
874
|
+
## 🤝 贡献
|
|
924
875
|
|
|
925
|
-
|
|
876
|
+
欢迎贡献!请遵循以下步骤:
|
|
926
877
|
|
|
927
|
-
1. **Fork
|
|
928
|
-
2.
|
|
929
|
-
3.
|
|
930
|
-
4.
|
|
931
|
-
5.
|
|
932
|
-
6.
|
|
878
|
+
1. **Fork 仓库**
|
|
879
|
+
2. **创建功能分支**: `git checkout -b feature/my-new-feature`
|
|
880
|
+
3. **进行更改**: 遵循代码风格并添加测试
|
|
881
|
+
4. **提交更改**: 使用约定式提交消息
|
|
882
|
+
5. **推送到分支**: `git push origin feature/my-new-feature`
|
|
883
|
+
6. **创建 Pull Request**: 提供清晰的更改描述
|
|
933
884
|
|
|
934
|
-
###
|
|
885
|
+
### 贡献指南
|
|
935
886
|
|
|
936
|
-
-
|
|
937
|
-
-
|
|
938
|
-
-
|
|
939
|
-
-
|
|
940
|
-
-
|
|
887
|
+
- 遵循现有代码风格
|
|
888
|
+
- 为新功能添加测试
|
|
889
|
+
- 更新文档
|
|
890
|
+
- 确保所有测试通过
|
|
891
|
+
- 遵循约定式提交格式
|
|
941
892
|
|
|
942
|
-
###
|
|
893
|
+
### 提交消息格式
|
|
943
894
|
|
|
944
|
-
|
|
895
|
+
遵循 [约定式提交](https://www.conventionalcommits.org/):
|
|
945
896
|
|
|
946
897
|
```
|
|
947
898
|
<type>(<scope>): <subject>
|
|
@@ -951,17 +902,17 @@ Follow [Conventional Commits](https://www.conventionalcommits.org/):
|
|
|
951
902
|
<footer>
|
|
952
903
|
```
|
|
953
904
|
|
|
954
|
-
|
|
905
|
+
类型: `feat`, `fix`, `docs`, `style`, `refactor`, `test`, `chore`
|
|
955
906
|
|
|
956
|
-
## 📚
|
|
907
|
+
## 📚 示例
|
|
957
908
|
|
|
958
|
-
###
|
|
909
|
+
### 示例项目
|
|
959
910
|
|
|
960
|
-
|
|
911
|
+
查看 [示例项目](https://github.com/kevin197011/kdeploy-app) 以获取完整的部署设置。
|
|
961
912
|
|
|
962
|
-
###
|
|
913
|
+
### 常见部署场景
|
|
963
914
|
|
|
964
|
-
#### Web
|
|
915
|
+
#### Web 应用程序部署
|
|
965
916
|
|
|
966
917
|
```ruby
|
|
967
918
|
host "web01", user: "deploy", ip: "10.0.0.1", key: "~/.ssh/id_rsa"
|
|
@@ -978,7 +929,7 @@ task :deploy_app, roles: :web do
|
|
|
978
929
|
end
|
|
979
930
|
```
|
|
980
931
|
|
|
981
|
-
####
|
|
932
|
+
#### 数据库备份
|
|
982
933
|
|
|
983
934
|
```ruby
|
|
984
935
|
host "db01", user: "postgres", ip: "10.0.0.10", key: "~/.ssh/id_rsa"
|
|
@@ -994,7 +945,7 @@ task :backup, roles: :db do
|
|
|
994
945
|
end
|
|
995
946
|
```
|
|
996
947
|
|
|
997
|
-
####
|
|
948
|
+
#### 配置管理
|
|
998
949
|
|
|
999
950
|
```ruby
|
|
1000
951
|
task :update_config, roles: :web do
|
|
@@ -1007,23 +958,24 @@ task :update_config, roles: :web do
|
|
|
1007
958
|
end
|
|
1008
959
|
```
|
|
1009
960
|
|
|
1010
|
-
## 📝
|
|
961
|
+
## 📝 许可证
|
|
1011
962
|
|
|
1012
|
-
|
|
963
|
+
该 gem 在 [MIT 许可证](https://opensource.org/licenses/MIT) 条款下作为开源提供。
|
|
1013
964
|
|
|
1014
|
-
## 🔗
|
|
965
|
+
## 🔗 链接
|
|
1015
966
|
|
|
1016
967
|
- **GitHub**: https://github.com/kevin197011/kdeploy
|
|
1017
968
|
- **RubyGems**: https://rubygems.org/gems/kdeploy
|
|
1018
969
|
- **Issues**: https://github.com/kevin197011/kdeploy/issues
|
|
1019
|
-
-
|
|
970
|
+
- **示例项目**: https://github.com/kevin197011/kdeploy-app
|
|
1020
971
|
|
|
1021
|
-
## 🙏
|
|
972
|
+
## 🙏 致谢
|
|
1022
973
|
|
|
1023
|
-
-
|
|
1024
|
-
-
|
|
1025
|
-
-
|
|
974
|
+
- 使用 [Thor](https://github.com/rails/thor) 构建 CLI
|
|
975
|
+
- 使用 [net-ssh](https://github.com/net-ssh/net-ssh) 进行 SSH 操作
|
|
976
|
+
- 由 [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) 提供并发支持
|
|
1026
977
|
|
|
1027
978
|
---
|
|
1028
979
|
|
|
1029
|
-
|
|
980
|
+
**为 DevOps 社区用 ❤️ 制作**
|
|
981
|
+
|