kdeploy 0.1.0 → 0.2.0

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.
data/README.md CHANGED
@@ -8,100 +8,102 @@
8
8
  \/ \/\__,_|\___| .__/|_|\___/ \__, |
9
9
  |_| |___/
10
10
 
11
- ⚡ Lightweight Agentless Deployment Tool
12
- 🚀 Deploy with confidence, scale with ease
11
+ ⚡ Lightweight Agentless Deployment Tool
12
+ 🚀 Deploy with confidence, scale with ease
13
13
  ```
14
14
 
15
- [![Gem Version](https://badge.fury.io/rb/kdeploy.svg)](https://badge.fury.io/rb/kdeploy)
16
- [![Build Status](https://github.com/kevin197011/kdeploy/workflows/CI/badge.svg)](https://github.com/kevin197011/kdeploy/actions)
15
+ [![Gem Version](https://badge.fury.io/rb/kdeploy.svg)](https://rubygems.org/gems/kdeploy)
16
+ [![CI](https://github.com/kevin197011/kdeploy/actions/workflows/ci.yml/badge.svg)](https://github.com/kevin197011/kdeploy/actions/workflows/ci.yml)
17
17
 
18
- Kdeploy 是一个现代化的轻量级 agentless 运维部署工具,类似于 ChefPuppet Ansible。它采用优雅的 Ruby DSL 语法,支持并发执行、统计分析和完整的部署生命周期管理。
18
+ Kdeploy is a modern, lightweight agentless deployment tool similar to Chef, Puppet, and Ansible. It uses an elegant Ruby DSL syntax and supports concurrent execution, statistical analysis, and complete deployment lifecycle management.
19
19
 
20
- ## ✨ 特性
20
+ [中文文档](README_CN.md)
21
21
 
22
- - 🚀 **轻量级**: 无需在目标服务器安装 agent,基于 SSH 连接
23
- - 🎨 **现代界面**: 美观的 ASCII 艺术字和彩色输出界面
24
- - 🔧 **DSL 语法**: 简洁优雅的 Ruby DSL 配置语法
25
- - ⚡ **并发执行**: 支持多主机并发操作和智能并发控制
26
- - 🛠️ **批量操作**: 高效的批量 Shell 命令执行
27
- - 🔒 **安全连接**: 基于 SSH 的安全连接,支持密钥认证
28
- - 📝 **实时输出**: 命令执行结果实时显示,包含详细执行时间统计
29
- - 🎯 **角色管理**: 灵活的基于角色的主机分组管理
30
- - 📋 **Inventory 管理**: 强大的 YAML 格式主机清单,支持群组和变量继承
31
- - 🧩 **Heredoc 语法**: 支持多行 Shell 脚本的 heredoc 语法
32
- - 🎨 **ERB 模板**: 内置 ERB 模板引擎,支持动态配置文件生成
33
- - 🏷️ **变量替换**: 支持 `{{variable}}` 和 `${variable}` 双重模板语法
34
- - 🖥️ **混合执行**: 支持本地命令执行和混合部署场景
35
- - 📊 **统计分析**: 自动收集部署统计,支持性能分析和趋势监控
36
- - 🔄 **脚本库**: 丰富的预制脚本模板,覆盖完整部署生命周期
37
- - 🧩 **模块化架构**: 支持脚本模块化组织,可复用通用任务组件
38
- - 🛡️ **错误恢复**: 智能错误处理和自动重试机制
22
+ ## Features
39
23
 
40
- ## 📦 安装
24
+ - 🚀 **Lightweight**: No agent installation required on target servers, SSH-based connections
25
+ - 🎨 **Modern Interface**: Beautiful ASCII art and colored output interface
26
+ - 🔧 **DSL Syntax**: Concise and elegant Ruby DSL configuration syntax
27
+ - ⚡ **Concurrent Execution**: Support for multi-host parallel operations and intelligent concurrency control
28
+ - 🛠️ **Batch Operations**: Efficient batch Shell command execution
29
+ - 🔒 **Secure Connection**: SSH-based secure connections with key authentication
30
+ - 📝 **Real-time Output**: Command execution results with real-time display and detailed timing statistics
31
+ - 🎯 **Role Management**: Flexible role-based host grouping management
32
+ - 📋 **Inventory Management**: Powerful YAML format host inventory with group and variable inheritance
33
+ - 🧩 **Heredoc Syntax**: Support for multi-line Shell scripts using heredoc syntax
34
+ - 🎨 **ERB Templates**: Built-in ERB template engine for dynamic configuration file generation
35
+ - 🏷️ **Variable Substitution**: Support for both `{{variable}}` and `${variable}` template syntax
36
+ - 🖥️ **Mixed Execution**: Support for local command execution and hybrid deployment scenarios
37
+ - 📊 **Statistics Analysis**: Automatic deployment statistics collection with performance analysis and trend monitoring
38
+ - 🔄 **Script Library**: Rich pre-made script templates covering the complete deployment lifecycle
39
+ - 🧩 **Modular Architecture**: Support for modular script organization with reusable task components
40
+ - 🛡️ **Error Recovery**: Intelligent error handling and automatic retry mechanism
41
41
 
42
- 将以下内容添加到您的 Gemfile:
42
+ ## 📦 Installation
43
+
44
+ Add this line to your application's Gemfile:
43
45
 
44
46
  ```ruby
45
47
  gem 'kdeploy'
46
48
  ```
47
49
 
48
- 然后执行:
50
+ Then execute:
49
51
 
50
52
  ```bash
51
- $ bundle install
53
+ bundle install
52
54
  ```
53
55
 
54
- 或者直接安装:
56
+ Or install it directly:
55
57
 
56
58
  ```bash
57
- $ gem install kdeploy
59
+ gem install kdeploy
58
60
  ```
59
61
 
60
- ## 🚀 快速开始
62
+ ## 🚀 Quick Start
61
63
 
62
- ### 1. 初始化项目
64
+ ### 1. Initialize Project
63
65
 
64
66
  ```bash
65
- $ kdeploy init myapp
66
- $ cd myapp
67
+ kdeploy init myapp
68
+ cd myapp
67
69
  ```
68
70
 
69
- kdeploy 会自动创建完整的项目结构,包含多个实用脚本范例:
71
+ Kdeploy will automatically create a complete project structure with several useful script examples:
70
72
 
71
73
  ```
72
74
  myapp/
73
- ├── deploy.rb # 主部署脚本
74
- ├── inventory.yml # 主机清单配置
75
- ├── config/ # 配置文件目录
76
- │ └── kdeploy.yml # 全局配置文件
77
- ├── scripts/ # 🆕 完整脚本库
78
- │ ├── common_tasks.rb # 🆕 通用任务模块(可复用)
79
- │ ├── setup.rb # 服务器初始化
80
- │ ├── database.rb # 数据库管理
81
- │ ├── backup.rb # 备份操作
82
- │ ├── monitoring.rb # 监控和健康检查
83
- │ ├── rollback.rb # 回滚操作
84
- │ └── cleanup.rb # 清理维护
85
- └── templates/ # ERB模板文件
86
- ├── nginx.conf.erb # Nginx配置模板
87
- ├── app.service.erb # Systemd服务模板
88
- ├── deploy.sh.erb # 部署脚本模板
89
- └── backup.sh.erb # 备份脚本模板
90
- ```
91
-
92
- ### 2. 配置服务器清单
93
-
94
- 编辑 `inventory.yml` 配置主机清单:
75
+ ├── deploy.rb # Main deployment script
76
+ ├── inventory.yml # Host inventory configuration
77
+ ├── config/ # Configuration directory
78
+ │ └── kdeploy.yml # Global configuration file
79
+ ├── scripts/ # 🆕 Complete script library
80
+ │ ├── common_tasks.rb # 🆕 Common task module (reusable)
81
+ │ ├── setup.rb # Server initialization
82
+ │ ├── database.rb # Database management
83
+ │ ├── backup.rb # Backup operations
84
+ │ ├── monitoring.rb # Monitoring and health checks
85
+ │ ├── rollback.rb # Rollback operations
86
+ │ └── cleanup.rb # Cleanup maintenance
87
+ └── templates/ # ERB template files
88
+ ├── nginx.conf.erb # Nginx configuration template
89
+ ├── app.service.erb # Systemd service template
90
+ ├── deploy.sh.erb # Deployment script template
91
+ └── backup.sh.erb # Backup script template
92
+ ```
93
+
94
+ ### 2. Configure Server Inventory
95
+
96
+ Edit `inventory.yml` to configure host inventory:
95
97
 
96
98
  ```yaml
97
- # 全局变量
99
+ # Global variables
98
100
  vars:
99
101
  application: myapp
100
102
  version: 1.0.0
101
103
  deploy_to: /opt/myapp
102
104
  environment: production
103
105
 
104
- # 主机群组
106
+ # Host groups
105
107
  groups:
106
108
  webservers:
107
109
  hosts:
@@ -125,7 +127,7 @@ groups:
125
127
  vars:
126
128
  environment: production
127
129
 
128
- # 主机详细配置
130
+ # Host configurations
129
131
  hosts:
130
132
  web1.example.com:
131
133
  user: deploy
@@ -159,136 +161,133 @@ hosts:
159
161
  master: true
160
162
  ```
161
163
 
162
- ### 3. 使用预制脚本
164
+ ### 3. Use Pre-made Scripts
163
165
 
164
- kdeploy 提供了完整的脚本库,覆盖部署的各个阶段:
166
+ Kdeploy provides a complete script library covering various deployment stages:
165
167
 
166
- #### 🔧 服务器初始化
168
+ #### 🔧 Server Initialization
167
169
  ```bash
168
- # 首次部署前执行,安装依赖和配置环境
169
- $ kdeploy deploy scripts/setup.rb
170
+ # Execute before first deployment to install dependencies and configure environment
171
+ kdeploy deploy scripts/setup.rb
170
172
  ```
171
173
 
172
- #### 💾 数据库管理
174
+ #### 💾 Database Management
173
175
  ```bash
174
- # 数据库相关操作
175
- $ kdeploy deploy scripts/database.rb # 创建数据库、迁移、备份
176
+ # Database operations
177
+ kdeploy deploy scripts/database.rb # Create database, migrate, backup
176
178
  ```
177
179
 
178
- #### 🚀 应用部署
180
+ #### 🚀 Application Deployment
179
181
  ```bash
180
- # 主要应用部署
181
- $ kdeploy deploy deploy.rb
182
+ # Main application deployment
183
+ kdeploy deploy deploy.rb
182
184
  ```
183
185
 
184
- #### 📊 健康监控
186
+ #### 📊 Health Monitoring
185
187
  ```bash
186
- # 系统和应用健康检查
187
- $ kdeploy deploy scripts/monitoring.rb
188
+ # System and application health checks
189
+ kdeploy deploy scripts/monitoring.rb
188
190
  ```
189
191
 
190
- #### 💾 备份操作
192
+ #### 💾 Backup Operations
191
193
  ```bash
192
- # 应用和数据备份
193
- $ kdeploy deploy scripts/backup.rb
194
+ # Application and data backup
195
+ kdeploy deploy scripts/backup.rb
194
196
  ```
195
197
 
196
- #### 🔙 回滚操作
198
+ #### 🔙 Rollback Operations
197
199
  ```bash
198
- # 紧急回滚和恢复
199
- $ kdeploy deploy scripts/rollback.rb
200
+ # Emergency rollback and recovery
201
+ kdeploy deploy scripts/rollback.rb
200
202
  ```
201
203
 
202
- #### 🧹 清理维护
204
+ #### 🧹 Cleanup Maintenance
203
205
  ```bash
204
- # 系统清理和维护
205
- $ kdeploy deploy scripts/cleanup.rb
206
+ # System cleanup and maintenance
207
+ kdeploy deploy scripts/cleanup.rb
206
208
  ```
207
209
 
208
- #### 🧩 通用任务模块
210
+ #### 🧩 Common Task Module
209
211
  ```bash
210
- # 使用通用任务模块进行基础设置
211
- $ kdeploy deploy scripts/common_tasks.rb --task setup_environment
212
-
213
- # 执行安全加固
214
- $ kdeploy deploy scripts/common_tasks.rb --task security_hardening
212
+ # Use common task module for basic setup
213
+ kdeploy deploy scripts/common_tasks.rb --task setup_environment
215
214
 
216
- # 在主脚本中引入模块化任务
217
- # deploy.rb 中自动包含: include 'scripts/common_tasks.rb' if File.exist?('scripts/common_tasks.rb')
215
+ # Execute security hardening
216
+ kdeploy deploy scripts/common_tasks.rb --task security_hardening
218
217
  ```
219
218
 
220
- ### 4. 部署工作流示例
219
+ ### 4. Deployment Workflow Example
221
220
 
222
221
  ```bash
223
- # 1. 验证配置
224
- $ kdeploy validate deploy.rb
222
+ # 1. Validate configuration
223
+ kdeploy validate deploy.rb
225
224
 
226
- # 2. 首次服务器设置
227
- $ kdeploy deploy scripts/setup.rb
225
+ # 2. First-time server setup
226
+ kdeploy deploy scripts/setup.rb
228
227
 
229
- # 3. 设置数据库
230
- $ kdeploy deploy scripts/database.rb
228
+ # 3. Setup database
229
+ kdeploy deploy scripts/database.rb
231
230
 
232
- # 4. 执行应用部署
233
- $ kdeploy deploy deploy.rb --verbose
231
+ # 4. Execute application deployment
232
+ kdeploy deploy deploy.rb --verbose
234
233
 
235
- # 5. 健康检查
236
- $ kdeploy deploy scripts/monitoring.rb
234
+ # 5. Health check
235
+ kdeploy deploy scripts/monitoring.rb
237
236
 
238
- # 6. 查看统计信息
239
- $ kdeploy stats summary
237
+ # 6. View statistics
238
+ kdeploy stats summary
240
239
  ```
241
240
 
242
- ## 📊 统计和监控功能
241
+ ## 📊 Statistics and Monitoring
243
242
 
244
- Kdeploy 提供了强大的内置统计功能,自动跟踪所有部署活动:
243
+ Kdeploy provides powerful built-in statistics functionality that automatically tracks all deployment activities:
245
244
 
246
- ### 📈 自动收集的统计数据
245
+ ### 📈 Automatically Collected Statistics
247
246
 
248
- - ✅ **成功/失败统计**: 部署、任务和命令级别的成功失败率
249
- - ⏱️ **性能指标**: 详细的执行时间和性能数据
250
- - 📅 **历史趋势**: 按日期分组的性能趋势分析
251
- - 🎯 **失败分析**: 识别最常失败的任务和错误模式
252
- - 🌍 **全局统计**: 跨会话的累计统计数据
247
+ - ✅ **Success/Failure Statistics**: Success rates at deployment, task, and command levels
248
+ - ⏱️ **Performance Metrics**: Detailed execution time and performance data
249
+ - 📅 **Historical Trends**: Performance trend analysis grouped by date
250
+ - 🎯 **Failure Analysis**: Identify most common failing tasks and error patterns
251
+ - 🌍 **Global Statistics**: Cumulative statistics across sessions
253
252
 
254
- ### 🔍 统计命令
253
+ ### 🔍 Statistics Commands
255
254
 
256
255
  ```bash
257
- # 查看统计概要
258
- $ kdeploy stats summary
256
+ # View statistics summary
257
+ kdeploy stats summary
259
258
 
260
- # 查看部署统计
261
- $ kdeploy stats deployments
259
+ # View deployment statistics
260
+ kdeploy stats deployments
262
261
 
263
- # 查看任务统计
264
- $ kdeploy stats tasks
262
+ # View task statistics
263
+ kdeploy stats tasks
265
264
 
266
- # 查看失败统计
267
- $ kdeploy stats failures
265
+ # View failure statistics
266
+ kdeploy stats failures
268
267
 
269
- # 查看性能趋势
270
- $ kdeploy stats trends
268
+ # View performance trends
269
+ kdeploy stats trends
271
270
 
272
- # 查看全局统计
273
- $ kdeploy stats global
271
+ # View global statistics
272
+ kdeploy stats global
274
273
 
275
- # 导出统计数据
276
- $ kdeploy stats export --export monthly_report.json
274
+ # Export statistics data
275
+ kdeploy stats export --export monthly_report.json
277
276
 
278
- # 清空统计数据
279
- $ kdeploy stats clear
277
+ # Clear statistics data
278
+ kdeploy stats clear
280
279
 
281
- # 指定时间范围(天数)
282
- $ kdeploy stats summary --days 7
280
+ # Specify time range (days)
281
+ kdeploy stats summary --days 7
283
282
 
284
- # JSON格式输出
285
- $ kdeploy stats tasks --format json
283
+ # JSON format output
284
+ kdeploy stats tasks --format json
286
285
  ```
287
286
 
288
- ### 📊 统计输出示例
287
+ ### 📊 Statistics Output Example
289
288
 
290
289
  ```bash
291
- $ kdeploy stats summary
290
+ kdeploy stats summary
292
291
 
293
292
  📊 Kdeploy Statistics Summary (Last 30 days)
294
293
  =====================================================
@@ -311,720 +310,82 @@ $ kdeploy stats summary
311
310
  Most Active Day: 2025-06-28 (8 deployments)
312
311
  ```
313
312
 
314
- ## 🔧 模块化脚本组织
315
-
316
- Kdeploy 支持模块化脚本组织,让您可以将复杂的部署逻辑拆分成可重用的组件。
317
-
318
- ### 📋 模块化引入语法
319
-
320
- 在 `deploy.rb` 主脚本中,您可以引入其他脚本文件:
321
-
322
- ```ruby
323
- # 引入通用任务模块(如果文件存在)
324
- include 'scripts/common_tasks.rb' if File.exist?('scripts/common_tasks.rb')
325
-
326
- # 引入项目特定任务
327
- include 'scripts/myapp_tasks.rb' if File.exist?('scripts/myapp_tasks.rb')
328
-
329
- # 引入环境特定任务
330
- include 'scripts/production_tasks.rb' if File.exist?('scripts/production_tasks.rb')
331
- include 'scripts/staging_tasks.rb' if File.exist?('scripts/staging_tasks.rb')
332
- ```
333
-
334
- ### 🏗️ common_tasks.rb - 通用任务库
335
-
336
- Kdeploy 自动生成 `scripts/common_tasks.rb` 文件,包含常用的基础设施任务:
337
-
338
- #### 环境设置任务
339
- ```ruby
340
- # 基础环境设置
341
- task 'setup_environment', on: :all do
342
- run 'sudo timedatectl set-timezone UTC', ignore_errors: true
343
- run 'sudo apt-get update -qq', ignore_errors: true
344
- run 'sudo apt-get install -y -qq htop curl wget vim git unzip', ignore_errors: true
345
- end
346
-
347
- # SSL证书配置
348
- task 'setup_ssl', on: :webservers do
349
- run 'sudo apt-get install -y certbot python3-certbot-nginx', ignore_errors: true
350
- run 'sudo certbot --nginx --dry-run -d {{hostname}}', ignore_errors: true
351
- end
352
-
353
- # 日志轮转配置
354
- task 'setup_log_rotation', on: :all do
355
- run <<~LOGROTATE
356
- sudo tee /etc/logrotate.d/{{application}} > /dev/null << 'EOF'
357
- {{deploy_to}}/shared/logs/*.log {
358
- daily
359
- missingok
360
- rotate 52
361
- compress
362
- delaycompress
363
- notifempty
364
- create 644 {{user}} {{user}}
365
- }
366
- EOF
367
- LOGROTATE
368
- end
369
- ```
370
-
371
- #### 安全加固任务
372
- ```ruby
373
- # 系统安全强化
374
- task 'security_hardening', on: :all do
375
- # 禁用root登录
376
- run 'sudo sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config', ignore_errors: true
377
-
378
- # 配置防火墙
379
- run 'sudo ufw --force reset', ignore_errors: true
380
- run 'sudo ufw default deny incoming', ignore_errors: true
381
- run 'sudo ufw default allow outgoing', ignore_errors: true
382
- run 'sudo ufw allow ssh', ignore_errors: true
383
- run 'sudo ufw allow {{nginx_port || 80}}', ignore_errors: true
384
- run 'sudo ufw --force enable', ignore_errors: true
385
-
386
- # 安装fail2ban
387
- run 'sudo apt-get install -y fail2ban', ignore_errors: true
388
- run 'sudo systemctl enable fail2ban', ignore_errors: true
389
- end
390
- ```
391
-
392
- #### 应急处理任务
393
- ```ruby
394
- # 紧急停止所有服务
395
- task 'emergency_stop', on: :all do
396
- run 'echo "🚨 Emergency stop initiated on {{hostname}}"'
397
- run 'sudo systemctl stop {{application}}', ignore_errors: true
398
- run 'sudo systemctl stop nginx', ignore_errors: true
399
- end
400
-
401
- # 紧急启动所有服务
402
- task 'emergency_start', on: :all do
403
- run 'echo "🚨 Emergency start initiated on {{hostname}}"'
404
- run 'sudo systemctl start nginx', ignore_errors: true
405
- run 'sudo systemctl start {{application}}', ignore_errors: true
406
- end
407
- ```
408
-
409
- ### 🔗 使用模块化任务
410
-
411
- #### 在主脚本中调用模块化任务
412
-
413
- ```ruby
414
- # deploy.rb - 主部署脚本
415
- include 'scripts/common_tasks.rb' if File.exist?('scripts/common_tasks.rb')
416
-
417
- # 完整部署工作流,使用通用任务
418
- task 'full_setup_with_common_tasks' do
419
- run_task 'pre_deploy_checks' # 来自 common_tasks.rb
420
- run_task 'setup_environment' # 来自 common_tasks.rb
421
- run_task 'security_hardening' # 来自 common_tasks.rb
422
- run_task 'performance_tuning' # 来自 common_tasks.rb
423
- run_task 'deploy' # 来自当前文件
424
- run_task 'verify_deployment' # 来自 common_tasks.rb
425
- end
426
-
427
- # 应急处理演示
428
- task 'emergency_procedures_demo' do
429
- # 这些任务来自 common_tasks.rb:
430
- # run_task 'emergency_stop' # 停止所有服务
431
- # run_task 'emergency_start' # 启动所有服务
432
- # run_task 'health_check_all' # 综合健康检查
433
- end
434
- ```
435
-
436
- #### 单独执行模块化任务
437
-
438
- ```bash
439
- # 执行通用环境设置
440
- kdeploy deploy scripts/common_tasks.rb --task setup_environment
441
-
442
- # 执行安全加固
443
- kdeploy deploy scripts/common_tasks.rb --task security_hardening
444
-
445
- # 应急操作
446
- kdeploy deploy scripts/common_tasks.rb --task emergency_stop
447
- kdeploy deploy scripts/common_tasks.rb --task emergency_start
448
-
449
- # 部署验证
450
- kdeploy deploy scripts/common_tasks.rb --task verify_deployment
451
- ```
452
-
453
- ### 📁 项目特定任务模块
454
-
455
- 创建项目特定的任务文件:
456
-
457
- ```ruby
458
- # scripts/myapp_tasks.rb
459
- task 'install_nodejs', on: :webservers do
460
- run 'curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -'
461
- run 'sudo apt-get install -y nodejs'
462
- run 'node --version && npm --version'
463
- end
464
-
465
- task 'build_frontend', on: :webservers do
466
- run 'cd {{deploy_to}} && npm install'
467
- run 'cd {{deploy_to}} && npm run build'
468
- end
469
-
470
- task 'deploy_myapp' do
471
- run_task 'setup_environment' # 来自 common_tasks.rb
472
- run_task 'install_nodejs' # 来自当前文件
473
- run_task 'build_frontend' # 来自当前文件
474
- run_task 'verify_deployment' # 来自 common_tasks.rb
475
- end
476
- ```
477
-
478
- ### 🌍 环境特定任务
479
-
480
- ```ruby
481
- # scripts/production_tasks.rb
482
- task 'production_ssl_setup', on: :webservers do
483
- run 'sudo certbot --nginx -d {{hostname}} --non-interactive --agree-tos --email admin@{{hostname}}'
484
- end
485
-
486
- task 'production_monitoring', on: :all do
487
- run 'sudo apt-get install -y datadog-agent'
488
- run 'sudo systemctl enable datadog-agent'
489
- end
490
-
491
- # scripts/staging_tasks.rb
492
- task 'staging_debug_mode', on: :webservers do
493
- run 'export DEBUG=true'
494
- run 'export LOG_LEVEL=debug'
495
- end
496
- ```
497
-
498
- ### 💡 模块化最佳实践
499
-
500
- 1. **按功能分组**: 将相关任务放在同一个模块中
501
- 2. **条件引入**: 使用 `if File.exist?` 确保文件存在性检查
502
- 3. **任务命名**: 使用清晰的任务名称,避免冲突
503
- 4. **文档注释**: 为每个模块添加详细的使用说明
504
- 5. **测试验证**: 使用 `kdeploy validate` 验证脚本语法
505
-
506
- ### 🚀 模块化工作流示例
507
-
508
- ```bash
509
- # 1. 完整的生产环境部署
510
- kdeploy deploy deploy.rb --task full_setup_with_common_tasks
511
-
512
- # 2. 分步骤执行
513
- kdeploy deploy scripts/common_tasks.rb --task setup_environment
514
- kdeploy deploy scripts/common_tasks.rb --task security_hardening
515
- kdeploy deploy deploy.rb --task deploy
516
- kdeploy deploy scripts/common_tasks.rb --task verify_deployment
517
-
518
- # 3. 环境特定配置
519
- kdeploy deploy scripts/production_tasks.rb --task production_ssl_setup
520
-
521
- # 4. 应急处理
522
- kdeploy deploy scripts/common_tasks.rb --task emergency_stop
523
- kdeploy deploy scripts/common_tasks.rb --task emergency_start
524
- ```
525
-
526
- 这种模块化组织方式让您可以:
527
- - 🔄 **复用代码**: 在多个项目间共享通用任务
528
- - 🎯 **专注职责**: 每个模块专注特定功能领域
529
- - 🔧 **灵活组合**: 根据需要灵活组合不同模块
530
- - 📝 **易于维护**: 模块化结构便于维护和测试
531
-
532
- ## 🛠️ 脚本库详解
533
-
534
- ### setup.rb - 服务器初始化脚本
535
-
536
- 完整的服务器环境准备:
537
-
538
- ```ruby
539
- # 安装系统依赖
540
- task 'install_dependencies', on: :all do
541
- run 'sudo apt-get update'
542
- run 'sudo apt-get install -y curl git build-essential'
543
- run 'sudo apt-get install -y nginx postgresql redis-server'
544
- end
545
-
546
- # 创建应用用户
547
- task 'setup_user', on: :all do
548
- run 'sudo useradd -m -s /bin/bash {{user}}', allow_failure: true
549
- run 'sudo mkdir -p /home/{{user}}/.ssh'
550
- run 'sudo cp ~/.ssh/authorized_keys /home/{{user}}/.ssh/'
551
- run 'sudo chown -R {{user}}:{{user}} /home/{{user}}/.ssh'
552
- end
553
-
554
- # 配置防火墙
555
- task 'setup_firewall', on: :all do
556
- run 'sudo ufw allow ssh'
557
- run 'sudo ufw allow {{nginx_port || 80}}'
558
- run 'sudo ufw --force enable'
559
- end
560
- ```
561
-
562
- ### database.rb - 数据库管理脚本
563
-
564
- 数据库生命周期管理:
565
-
566
- ```ruby
567
- # 创建数据库
568
- task 'create_database', on: :databases do
569
- run 'sudo -u postgres createdb {{application}}_{{environment}}', allow_failure: true
570
- run 'sudo -u postgres createuser {{application}}_user', allow_failure: true
571
- run %(sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE {{application}}_{{environment}} TO {{application}}_user;")
572
- end
573
-
574
- # 数据库迁移
575
- task 'migrate', on: :databases do
576
- run 'cd {{deploy_to}} && npm run migrate'
577
- end
578
-
579
- # 数据库备份
580
- task 'backup', on: :databases do
581
- run 'mkdir -p /backup/{{application}}'
582
- run 'pg_dump {{application}}_{{environment}} > /backup/{{application}}/backup_$(date +%Y%m%d_%H%M%S).sql'
583
- end
584
- ```
585
-
586
- ### monitoring.rb - 监控健康检查脚本
587
-
588
- 全面的系统和应用监控:
589
-
590
- ```ruby
591
- # 系统健康检查
592
- task 'system_health', on: :all do
593
- run 'echo "=== System Health for {{hostname}} ==="'
594
- run 'uptime'
595
- run 'df -h'
596
- run 'free -h'
597
- run 'ps aux --sort=-%cpu | head -10'
598
- end
599
-
600
- # 应用健康检查
601
- task 'app_health', on: :webservers do
602
- run 'systemctl status {{application}}', allow_failure: true
603
- run 'curl -f http://localhost:{{app_port}}/health || echo "Health check failed"',
604
- timeout: 10, allow_failure: true
605
- end
606
-
607
- # 性能监控
608
- task 'performance_check', on: :webservers do
609
- run 'curl -w "Connect: %{time_connect}s, Total: %{time_total}s\\n" -s -o /dev/null http://localhost:{{app_port}}/',
610
- timeout: 15, allow_failure: true
611
- end
612
- ```
613
-
614
- ### rollback.rb - 回滚操作脚本
615
-
616
- 灵活的回滚和恢复机制:
617
-
618
- ```ruby
619
- # 快速回滚到上一版本
620
- task 'rollback', on: :webservers do
621
- run 'cd {{deploy_to}} && git log --oneline -5'
622
- run 'cd {{deploy_to}} && git reset --hard HEAD~1'
623
- run 'sudo systemctl restart {{application}}'
624
- end
625
-
626
- # 回滚到指定版本
627
- task 'rollback_to_commit', on: :webservers do |hosts, commit_hash|
628
- commit_hash ||= ENV['COMMIT_HASH']
629
- raise 'Please specify COMMIT_HASH environment variable' unless commit_hash
630
-
631
- run "cd {{deploy_to}} && git reset --hard #{commit_hash}"
632
- run 'sudo systemctl restart {{application}}'
633
- end
634
-
635
- # 维护模式
636
- task 'maintenance_on', on: :webservers do
637
- run 'echo "maintenance" > {{deploy_to}}/public/maintenance.txt'
638
- run 'sudo nginx -s reload'
639
- end
640
- ```
641
-
642
- ## 🎨 模板系统
643
-
644
- ### ERB 模板示例
645
-
646
- `templates/nginx.conf.erb`:
647
- ```erb
648
- # Nginx configuration for <%= application %>
649
- server {
650
- listen <%= nginx_port || 80 %>;
651
- server_name <%= hostname %>;
652
- root <%= deploy_to %>/public;
653
-
654
- location / {
655
- try_files $uri $uri/ @app;
656
- }
657
-
658
- location @app {
659
- proxy_pass http://127.0.0.1:<%= app_port || 3000 %>;
660
- proxy_set_header Host $host;
661
- proxy_set_header X-Real-IP $remote_addr;
662
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
663
- }
664
-
665
- # Static files cache
666
- location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
667
- expires 1y;
668
- add_header Cache-Control "public, immutable";
669
- }
670
-
671
- access_log /var/log/nginx/<%= application %>_access.log;
672
- error_log /var/log/nginx/<%= application %>_error.log;
673
- }
674
- ```
675
-
676
- ### 使用模板
677
-
678
- ```ruby
679
- task 'configure_nginx', on: :webservers do
680
- upload_template 'nginx.conf', '/etc/nginx/sites-available/{{application}}'
681
- run 'sudo ln -sf /etc/nginx/sites-available/{{application}} /etc/nginx/sites-enabled/'
682
- run 'sudo nginx -t && sudo systemctl reload nginx'
683
- end
684
- ```
685
-
686
- ## 🖥️ 命令行接口
687
-
688
- ### 主要命令
689
-
690
- ```bash
691
- # 项目管理
692
- kdeploy init [PROJECT_NAME] # 初始化新项目
693
- kdeploy validate SCRIPT # 验证部署脚本
694
- kdeploy config # 显示当前配置
695
-
696
- # 部署执行
697
- kdeploy deploy SCRIPT # 执行部署脚本
698
- kdeploy execute SCRIPT # 执行脚本(别名)
699
-
700
- # 统计分析
701
- kdeploy stats summary # 统计概要
702
- kdeploy stats deployments # 部署统计
703
- kdeploy stats tasks # 任务统计
704
- kdeploy stats failures # 失败统计
705
- kdeploy stats trends # 性能趋势
706
- kdeploy stats global # 全局统计
707
- kdeploy stats export # 导出数据
708
- kdeploy stats clear # 清空统计
709
-
710
- # 帮助信息
711
- kdeploy version # 显示版本
712
- kdeploy help # 显示帮助
713
- kdeploy help [COMMAND] # 特定命令帮助
714
- ```
715
-
716
- ### 命令选项
717
-
718
- ```bash
719
- # 配置选项
720
- -c, --config FILE # 指定配置文件
721
- -i, --inventory FILE # 指定inventory文件
722
- -v, --verbose # 详细输出模式
723
- -l, --log-file FILE # 指定日志文件
724
-
725
- # 执行选项
726
- --dry-run # 干运行模式
727
- --parallel # 并行执行
728
-
729
- # 统计选项
730
- --days N # 指定天数范围
731
- --format FORMAT # 输出格式(table|json)
732
- --export FILE # 导出文件路径
733
- ```
734
-
735
- ## 📋 DSL 语法参考
736
-
737
- ### 基础语法
738
-
739
- ```ruby
740
- # 加载主机清单
741
- inventory 'inventory.yml'
742
-
743
- # 定义变量
744
- set 'application', 'myapp'
745
- set 'version', '1.0.0'
746
-
747
- # 定义主机
748
- host '192.168.1.100', user: 'deploy', port: 22
749
-
750
- # 定义任务
751
- task 'deploy', on: :webservers do
752
- run 'echo "Deploying to {{hostname}}"'
753
- end
754
-
755
- # 本地命令
756
- local 'echo "Starting deployment"'
757
- ```
758
-
759
- ### 高级语法
760
-
761
- ```ruby
762
- # 条件执行
763
- task 'conditional_task', on: :all do
764
- run 'echo "Production server"', only: :production
765
- run 'echo "Staging server"', except: :production
766
- end
767
-
768
- # 错误处理
769
- task 'robust_task', on: :all do
770
- run 'risky_command',
771
- allow_failure: true,
772
- timeout: 30,
773
- retry_count: 3
774
- end
775
-
776
- # 并发控制
777
- task 'parallel_task', on: :all, parallel: true, max_concurrent: 5 do
778
- run 'long_running_command'
779
- end
780
-
781
- # Heredoc 语法
782
- task 'complex_task', on: :all do
783
- run <<~SCRIPT
784
- echo "Starting complex deployment"
785
- cd {{deploy_to}}
786
-
787
- if [ -d .git ]; then
788
- git pull origin {{branch}}
789
- else
790
- git clone {{repo_url}} .
791
- fi
792
-
793
- npm install --production
794
- sudo systemctl restart {{application}}
795
- echo "Deployment completed"
796
- SCRIPT
797
- end
798
- ```
799
-
800
- ### 变量和模板
801
-
802
- ```ruby
803
- # 变量使用(两种语法)
804
- run 'echo "Application: {{application}}"'
805
- run 'echo "Version: ${version}"'
806
-
807
- # 内置变量
808
- run 'echo "Hostname: {{hostname}}"'
809
- run 'echo "User: {{user}}"'
810
- run 'echo "Port: {{port}}"'
811
-
812
- # 模板上传
813
- upload_template 'config.erb', '/etc/myapp/config.yml'
814
- ```
815
-
816
- ## ⚙️ 配置文件
817
-
818
- 创建 `config/kdeploy.yml`:
819
-
820
- ```yaml
821
- # 最大并发任务数
822
- max_concurrent_tasks: 10
823
-
824
- # 网络超时设置
825
- ssh_timeout: 30
826
- command_timeout: 300
827
-
828
- # 重试设置
829
- retry_count: 3
830
- retry_delay: 1
831
-
832
- # 日志设置
833
- log_level: info
834
- log_file: kdeploy.log
835
-
836
- # 默认设置
837
- default_user: deploy
838
- default_port: 22
839
-
840
- # SSH选项
841
- ssh_options:
842
- verify_host_key: never
843
- non_interactive: true
844
- use_agent: true
845
- forward_agent: false
846
-
847
- # 路径设置
848
- inventory_file: inventory.yml
849
- template_dir: templates
850
- ```
851
-
852
- ## 🎯 实际使用案例
853
-
854
- ### Node.js 应用部署
855
-
856
- ```ruby
857
- # deploy.rb
858
- inventory 'inventory.yml'
859
-
860
- task 'deploy_nodejs', on: :webservers do
861
- run <<~DEPLOY
862
- echo "Deploying Node.js app to {{hostname}}"
863
-
864
- # 备份当前版本
865
- if [ -d "{{deploy_to}}/current" ]; then
866
- sudo mv {{deploy_to}}/current {{deploy_to}}/backup_$(date +%s)
867
- fi
868
-
869
- # 创建部署目录
870
- sudo mkdir -p {{deploy_to}}
871
- cd {{deploy_to}}
872
-
873
- # 获取最新代码
874
- if [ -d ".git" ]; then
875
- git fetch origin
876
- git reset --hard origin/{{branch}}
877
- else
878
- git clone {{repo_url}} .
879
- git checkout {{branch}}
880
- fi
881
-
882
- # 安装依赖
883
- npm install --production
884
-
885
- # 构建应用
886
- npm run build
887
-
888
- # 重启服务
889
- sudo systemctl restart {{application}}
890
-
891
- echo "✅ Deployment completed successfully"
892
- DEPLOY
893
- end
894
-
895
- # 健康检查
896
- task 'health_check', on: :webservers do
897
- run 'curl -f http://localhost:{{app_port}}/health',
898
- timeout: 30,
899
- retry_count: 3
900
- end
901
- ```
902
-
903
- ### Docker 应用部署
904
-
905
- ```ruby
906
- task 'deploy_docker', on: :webservers do
907
- run <<~DOCKER_DEPLOY
908
- echo "Deploying Docker application"
909
-
910
- # 拉取最新镜像
911
- docker pull {{image_name}}:{{version}}
912
-
913
- # 停止旧容器
914
- docker stop {{application}} || true
915
- docker rm {{application}} || true
916
-
917
- # 启动新容器
918
- docker run -d \
919
- --name {{application}} \
920
- -p {{app_port}}:3000 \
921
- -e NODE_ENV={{environment}} \
922
- {{image_name}}:{{version}}
923
-
924
- # 等待容器启动
925
- sleep 10
926
-
927
- echo "✅ Docker deployment completed"
928
- DOCKER_DEPLOY
929
- end
930
- ```
931
-
932
- ### 数据库迁移
933
-
934
- ```ruby
935
- task 'database_migration', on: :databases do
936
- run <<~MIGRATION
937
- echo "Running database migration"
938
-
939
- # 备份数据库
940
- pg_dump {{application}}_{{environment}} > \
941
- /backup/pre_migration_$(date +%Y%m%d_%H%M%S).sql
942
-
943
- # 执行迁移
944
- cd {{deploy_to}}
945
- NODE_ENV={{environment}} npm run migrate
946
-
947
- echo "✅ Database migration completed"
948
- MIGRATION
949
- end
950
- ```
951
-
952
- ## 🔧 故障排除
313
+ ## 🔧 Troubleshooting
953
314
 
954
- ### 常见问题
315
+ ### Common Issues
955
316
 
956
- 1. **SSH 连接失败**
317
+ 1. **SSH Connection Failed**
957
318
  ```bash
958
- # 检查SSH连接
319
+ # Check SSH connection
959
320
  ssh -vvv user@hostname
960
321
 
961
- # 验证密钥
322
+ # Verify keys
962
323
  ssh-add -l
963
324
  ```
964
325
 
965
- 2. **权限问题**
326
+ 2. **Permission Issues**
966
327
  ```ruby
967
- # 使用sudo执行命令
328
+ # Use sudo for commands
968
329
  run 'sudo systemctl restart nginx'
969
330
 
970
- # 检查文件权限
331
+ # Check file permissions
971
332
  run 'ls -la {{deploy_to}}'
972
333
  ```
973
334
 
974
- 3. **超时问题**
335
+ 3. **Timeout Issues**
975
336
  ```ruby
976
- # 增加超时时间
337
+ # Increase timeout
977
338
  run 'long_command', timeout: 300
978
339
 
979
- # 或在配置文件中设置
340
+ # Or set in configuration file
980
341
  command_timeout: 600
981
342
  ```
982
343
 
983
- ### 调试技巧
344
+ ### Debugging Tips
984
345
 
985
346
  ```bash
986
- # 详细输出模式
347
+ # Verbose output mode
987
348
  kdeploy deploy script.rb --verbose
988
349
 
989
- # 干运行模式
350
+ # Dry run mode
990
351
  kdeploy deploy script.rb --dry-run
991
352
 
992
- # 查看配置
353
+ # View configuration
993
354
  kdeploy config
994
355
 
995
- # 验证脚本
356
+ # Validate script
996
357
  kdeploy validate script.rb
997
358
 
998
- # 查看日志
359
+ # View logs
999
360
  tail -f kdeploy.log
1000
361
  ```
1001
362
 
1002
- ## 🤝 贡献
363
+ ## 🤝 Contributing
1003
364
 
1004
- 我们欢迎社区贡献!请参考以下指南:
365
+ We welcome community contributions! Please follow these guidelines:
1005
366
 
1006
- 1. Fork 项目
1007
- 2. 创建功能分支 (`git checkout -b feature/amazing-feature`)
1008
- 3. 提交更改 (`git commit -m 'Add amazing feature'`)
1009
- 4. 推送到分支 (`git push origin feature/amazing-feature`)
1010
- 5. 创建 Pull Request
367
+ 1. Fork the project
368
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
369
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
370
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
371
+ 5. Create a Pull Request
1011
372
 
1012
- ## 📄 许可证
373
+ ## 📄 License
1013
374
 
1014
- 本项目采用 MIT 许可证。详情请参阅 [LICENSE](LICENSE) 文件。
375
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
1015
376
 
1016
- ## 🔗 相关链接
377
+ ## 🔗 Links
1017
378
 
1018
- - [项目主页](https://github.com/kevin197011/kdeploy)
1019
- - [文档中心](https://github.com/kevin197011/kdeploy/wiki)
1020
- - [问题反馈](https://github.com/kevin197011/kdeploy/issues)
1021
- - [发布日志](https://github.com/kevin197011/kdeploy/releases)
379
+ - [Project Homepage](https://github.com/kevin197011/kdeploy)
380
+ - [Documentation](https://github.com/kevin197011/kdeploy/wiki)
381
+ - [Issue Tracker](https://github.com/kevin197011/kdeploy/issues)
382
+ - [Release Notes](https://github.com/kevin197011/kdeploy/releases)
1022
383
 
1023
- ## 💬 社区支持
384
+ ## 💬 Community Support
1024
385
 
1025
- - GitHub Issues: [报告问题](https://github.com/kevin197011/kdeploy/issues)
1026
- - GitHub Discussions: [社区讨论](https://github.com/kevin197011/kdeploy/discussions)
386
+ - GitHub Issues: [Report Issues](https://github.com/kevin197011/kdeploy/issues)
387
+ - GitHub Discussions: [Community Discussions](https://github.com/kevin197011/kdeploy/discussions)
1027
388
 
1028
389
  ---
1029
390
 
1030
- **Kdeploy** - 让部署变得简单而强大 🚀
391
+ **Kdeploy** - Making deployment simple and powerful 🚀