kdeploy 0.3.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3be52250612f3eb627a5044db454c87f7258396f9a0c066be85df9ff8f33b9d
4
- data.tar.gz: c802622e109e4e5d0620456a732e2b8b870b77ec0e422fe461ff1edf6993cd7e
3
+ metadata.gz: 3e4e44b572cceb902d9267bfac535bceffcb116b0e5ef8f4de78aafece8dfead
4
+ data.tar.gz: c57baa1fccd958c63f60253c682c975bb993f6cc5802ac73d96ed797aea527cc
5
5
  SHA512:
6
- metadata.gz: cc0fba2a35c4bff14238ef9c4f088d1f2fc52f1625e3c7e38394d5100d57db3556fc634af21afcb4b991b1f24e6339c1649c8cc52a58a20a29eb0b8e24d8e956
7
- data.tar.gz: 52cb3084e8bf51f50e92556b13e241451dd6c6bfa6df7ed9041a042ea96eb4f1fe5c8d310af9e706a3e2396eea963f0bc552e6c72efb5aa29eead6da014f553b
6
+ metadata.gz: 9a194eb2b3b5f89853493f4ec34e0053db6c1ae25dddeca768bedc9809eb51ad138aafce09c0bcd3f80a43c5872ad02b57b98bfbc0a0f324634a7015425d037a
7
+ data.tar.gz: 97880efd10a46ac87bec1de2312d338ddb2603eac34610e4cd17cd505f7dc8648844b001a37e95633b9410b08f3f55aaa887f11e888d0ea8bf5a66a64451c730
data/README.md CHANGED
@@ -12,6 +12,7 @@ A lightweight agentless deployment automation tool written in Ruby.
12
12
  - 🔄 **ERB Template Support**: Dynamic configuration generation
13
13
  - 🎯 **Role-based Deployment**: Target specific server roles
14
14
  - 🔍 **Dry Run Mode**: Preview tasks before execution
15
+ - 🎨 **Color-coded Output**: Green for success, Red for errors, Yellow for warnings
15
16
 
16
17
  ## 📦 Installation
17
18
 
@@ -66,11 +67,43 @@ end
66
67
  3. Run the deployment:
67
68
 
68
69
  ```bash
70
+ # Execute all tasks in deploy.rb
69
71
  $ kdeploy execute deploy.rb
72
+
73
+ # Execute a specific task
74
+ $ kdeploy execute deploy.rb deploy_web
70
75
  ```
71
76
 
72
77
  ## 📖 Usage Guide
73
78
 
79
+ ### Task Execution
80
+
81
+ ```bash
82
+ # Execute all tasks in the file
83
+ kdeploy execute deploy.rb
84
+
85
+ # Execute a specific task
86
+ kdeploy execute deploy.rb deploy_web
87
+
88
+ # Execute with dry run (preview mode)
89
+ kdeploy execute deploy.rb --dry-run
90
+
91
+ # Execute on specific hosts
92
+ kdeploy execute deploy.rb --limit web01,web02
93
+
94
+ # Execute with custom parallel count
95
+ kdeploy execute deploy.rb --parallel 5
96
+ ```
97
+
98
+ When executing without specifying a task name (`kdeploy execute deploy.rb`), Kdeploy will:
99
+ 1. Execute all defined tasks in the file
100
+ 2. Run tasks in the order they were defined
101
+ 3. Show task name before each task execution
102
+ 4. Display color-coded output for better readability:
103
+ - 🟢 Green: Normal output and success messages
104
+ - 🔴 Red: Errors and failure messages
105
+ - 🟡 Yellow: Warnings and notices
106
+
74
107
  ### Host Definition
75
108
 
76
109
  ```ruby
@@ -156,22 +189,6 @@ task :deploy_config do
156
189
  end
157
190
  ```
158
191
 
159
- ### Command Line Options
160
-
161
- ```bash
162
- # Execute with dry run
163
- kdeploy execute deploy.rb --dry-run
164
-
165
- # Limit to specific hosts
166
- kdeploy execute deploy.rb --limit web01,web02
167
-
168
- # Set parallel execution count
169
- kdeploy execute deploy.rb --parallel 5
170
-
171
- # Execute specific task
172
- kdeploy execute deploy.rb deploy_web
173
- ```
174
-
175
192
  ## 🔧 Development
176
193
 
177
194
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/exe/kdeploy CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "kdeploy"
4
+ require 'kdeploy'
5
5
 
6
- Kdeploy::CLI.start(ARGV)
6
+ Kdeploy::CLI.start(ARGV)
data/lib/kdeploy/cli.rb CHANGED
@@ -103,7 +103,7 @@ module Kdeploy
103
103
 
104
104
  runner = Runner.new(hosts, self.class.kdeploy_tasks, parallel: options[:parallel])
105
105
  results = runner.run(task)
106
- print_results(results)
106
+ print_results(results, task)
107
107
  end
108
108
  rescue StandardError => e
109
109
  puts Kdeploy::Banner.show_error(e.message)
@@ -122,11 +122,11 @@ module Kdeploy
122
122
  end
123
123
 
124
124
  def filter_hosts(limit, task_hosts)
125
- hosts = self.class.kdeploy_hosts.select { |name, _| task_hosts.include?(name) }
125
+ hosts = self.class.kdeploy_hosts.slice(*task_hosts)
126
126
  return hosts unless limit
127
127
 
128
128
  host_names = limit.split(',').map(&:strip)
129
- hosts.select { |name, _| host_names.include?(name) }
129
+ hosts.slice(*host_names)
130
130
  end
131
131
 
132
132
  def print_dry_run(hosts, task_name)
@@ -167,10 +167,13 @@ module Kdeploy
167
167
  end
168
168
  end
169
169
 
170
- def print_results(results)
170
+ def print_results(results, task_name)
171
171
  puts Kdeploy::Banner.show
172
172
  pastel = Pastel.new
173
173
 
174
+ puts "#{pastel.bright_cyan('Task:')} #{pastel.bright_white(task_name)}"
175
+ puts
176
+
174
177
  results.each do |host, result|
175
178
  status = if result[:status] == :success
176
179
  pastel.green('✓ Success')
@@ -184,10 +187,42 @@ module Kdeploy
184
187
  result[:output].each do |cmd|
185
188
  puts " #{pastel.bright_yellow('$')} #{cmd[:command]}"
186
189
  if cmd[:output].is_a?(Hash)
187
- puts " #{cmd[:output][:stdout]}" unless cmd[:output][:stdout].empty?
188
- puts " #{pastel.red(cmd[:output][:stderr])}" unless cmd[:output][:stderr].empty?
190
+ unless cmd[:output][:stdout].empty?
191
+ cmd[:output][:stdout].each_line do |line|
192
+ line = line.strip
193
+ next if line.empty?
194
+
195
+ # 根据输出内容的特征来决定颜色
196
+ colored_line = if line.match?(/error|fail|fatal|critical/i)
197
+ pastel.red(line)
198
+ elsif line.match?(/warn|deprecat|notice/i)
199
+ pastel.yellow(line)
200
+ else
201
+ pastel.green(line)
202
+ end
203
+ puts " #{colored_line}"
204
+ end
205
+ end
206
+ unless cmd[:output][:stderr].empty?
207
+ cmd[:output][:stderr].each_line do |line|
208
+ puts " #{pastel.red(line)}" unless line.strip.empty?
209
+ end
210
+ end
189
211
  elsif cmd[:output]
190
- puts " #{cmd[:output]}"
212
+ cmd[:output].each_line do |line|
213
+ line = line.strip
214
+ next if line.empty?
215
+
216
+ # 根据输出内容的特征来决定颜色
217
+ colored_line = if line.match?(/error|fail|fatal|critical/i)
218
+ pastel.red(line)
219
+ elsif line.match?(/warn|deprecat|notice/i)
220
+ pastel.yellow(line)
221
+ else
222
+ pastel.green(line)
223
+ end
224
+ puts " #{colored_line}"
225
+ end
191
226
  end
192
227
  puts
193
228
  end
data/lib/kdeploy/dsl.rb CHANGED
@@ -79,20 +79,16 @@ module Kdeploy
79
79
  hosts = Set.new
80
80
 
81
81
  # 添加指定的主机
82
- if task[:hosts]
83
- task[:hosts].each do |host|
84
- hosts.add(host) if kdeploy_hosts.key?(host)
85
- end
82
+ task[:hosts]&.each do |host|
83
+ hosts.add(host) if kdeploy_hosts.key?(host)
86
84
  end
87
85
 
88
86
  # 添加角色中的主机
89
- if task[:roles]
90
- task[:roles].each do |role|
91
- next unless role_hosts = kdeploy_roles[role]
87
+ task[:roles]&.each do |role|
88
+ next unless (role_hosts = kdeploy_roles[role])
92
89
 
93
- role_hosts.each do |host|
94
- hosts.add(host) if kdeploy_hosts.key?(host)
95
- end
90
+ role_hosts.each do |host|
91
+ hosts.add(host) if kdeploy_hosts.key?(host)
96
92
  end
97
93
  end
98
94
 
@@ -28,8 +28,8 @@ module Kdeploy
28
28
  # frozen_string_literal: true
29
29
 
30
30
  # Define hosts
31
- host "web01", user: "ubuntu", ip: "10.0.0.1", key: "~/.ssh/id_rsa"
32
- host "web02", user: "ubuntu", ip: "10.0.0.2", key: "~/.ssh/id_rsa"
31
+ host 'web01', user: 'ubuntu', ip: '10.0.0.1', key: '~/.ssh/id_rsa'
32
+ host 'web02', user: 'ubuntu', ip: '10.0.0.2', key: '~/.ssh/id_rsa'
33
33
 
34
34
  # Define roles
35
35
  role :web, %w[web01 web02]
@@ -37,48 +37,48 @@ module Kdeploy
37
37
 
38
38
  # Define inventory
39
39
  inventory do
40
- host "db01", user: "root", ip: "10.0.0.3", key: "~/.ssh/id_rsa"
40
+ host 'db01', user: 'root', ip: '10.0.0.3', key: '~/.ssh/id_rsa'
41
41
  end
42
42
 
43
43
  # Define deployment task for web servers
44
44
  task :deploy_web, roles: :web do
45
45
  # Stop service
46
- run "sudo systemctl stop nginx"
46
+ run 'sudo systemctl stop nginx'
47
47
 
48
48
  # Upload configuration using ERB template
49
- upload_template "./config/nginx.conf.erb", "/etc/nginx/nginx.conf",
50
- domain_name: "example.com",
49
+ upload_template './config/nginx.conf.erb', '/etc/nginx/nginx.conf',
50
+ domain_name: 'example.com',
51
51
  port: 3000,
52
52
  worker_processes: 4,
53
53
  worker_connections: 2048
54
54
 
55
55
  # Upload static configuration
56
- upload "./config/app.conf", "/etc/nginx/conf.d/app.conf"
56
+ upload './config/app.conf', '/etc/nginx/conf.d/app.conf'
57
57
 
58
58
  # Restart service
59
- run "sudo systemctl start nginx"
59
+ run 'sudo systemctl start nginx'
60
60
 
61
61
  # Check status
62
- run "sudo systemctl status nginx"
62
+ run 'sudo systemctl status nginx'
63
63
  end
64
64
 
65
65
  # Define backup task for database servers
66
66
  task :backup_db, roles: :db do
67
- run "tar -czf /tmp/backup.tar.gz /var/lib/postgresql/data"
68
- run "aws s3 cp /tmp/backup.tar.gz s3://my-backups/"
69
- run "rm /tmp/backup.tar.gz"
67
+ run 'tar -czf /tmp/backup.tar.gz /var/lib/postgresql/data'
68
+ run 'aws s3 cp /tmp/backup.tar.gz s3://my-backups/'
69
+ run 'rm /tmp/backup.tar.gz'
70
70
  end
71
71
 
72
72
  # Define task for specific hosts
73
73
  task :maintenance, on: %w[web01] do
74
- run "sudo systemctl stop nginx"
75
- run "sudo apt-get update && sudo apt-get upgrade -y"
76
- run "sudo systemctl start nginx"
74
+ run 'sudo systemctl stop nginx'
75
+ run 'sudo apt-get update && sudo apt-get upgrade -y'
76
+ run 'sudo systemctl start nginx'
77
77
  end
78
78
 
79
79
  # Define task for all hosts
80
80
  task :update do
81
- run "sudo apt-get update && sudo apt-get upgrade -y"
81
+ run 'sudo apt-get update && sudo apt-get upgrade -y'
82
82
  end
83
83
  RUBY
84
84
  end
@@ -159,7 +159,7 @@ module Kdeploy
159
159
 
160
160
  This is a deployment project created with Kdeploy.
161
161
 
162
- ## Structure
162
+ ## 📁 Structure
163
163
 
164
164
  ```
165
165
  .
@@ -170,7 +170,7 @@ module Kdeploy
170
170
  └── README.md # This file
171
171
  ```
172
172
 
173
- ## Configuration Templates
173
+ ## 🔧 Configuration Templates
174
174
 
175
175
  The project uses ERB templates for dynamic configuration. For example, in `nginx.conf.erb`:
176
176
 
@@ -187,27 +187,57 @@ module Kdeploy
187
187
  worker_processes: 4
188
188
  ```
189
189
 
190
- ## Usage
190
+ ## 🚀 Usage
191
+
192
+ ### Task Execution
191
193
 
192
194
  ```bash
193
- # Show what would be done
194
- kdeploy execute deploy.rb --dry-run
195
+ # Execute all tasks in the file
196
+ kdeploy execute deploy.rb
195
197
 
196
- # Deploy to web servers
198
+ # Execute a specific task
197
199
  kdeploy execute deploy.rb deploy_web
198
200
 
199
- # Backup database
200
- kdeploy execute deploy.rb backup_db
201
-
202
- # Run maintenance on web01
203
- kdeploy execute deploy.rb maintenance
201
+ # Execute with dry run (preview mode)
202
+ kdeploy execute deploy.rb --dry-run
204
203
 
205
- # Update all hosts
206
- kdeploy execute deploy.rb update
204
+ # Execute on specific hosts
205
+ kdeploy execute deploy.rb --limit web01,web02
207
206
 
208
- # Deploy to specific hosts
209
- kdeploy execute deploy.rb deploy_web --limit web01,web02
207
+ # Execute with custom parallel count
208
+ kdeploy execute deploy.rb --parallel 5
210
209
  ```
210
+
211
+ When executing without specifying a task name (`kdeploy execute deploy.rb`), Kdeploy will:
212
+ 1. Execute all defined tasks in the file
213
+ 2. Run tasks in the order they were defined
214
+ 3. Show task name before each task execution
215
+ 4. Display color-coded output for better readability:
216
+ - 🟢 Green: Normal output and success messages
217
+ - 🔴 Red: Errors and failure messages
218
+ - 🟡 Yellow: Warnings and notices
219
+
220
+ ### Available Tasks
221
+
222
+ - **deploy_web**: Deploy and configure Nginx web servers
223
+ ```bash
224
+ kdeploy execute deploy.rb deploy_web
225
+ ```
226
+
227
+ - **backup_db**: Backup database to S3
228
+ ```bash
229
+ kdeploy execute deploy.rb backup_db
230
+ ```
231
+
232
+ - **maintenance**: Run maintenance on specific host
233
+ ```bash
234
+ kdeploy execute deploy.rb maintenance
235
+ ```
236
+
237
+ - **update**: Update all hosts
238
+ ```bash
239
+ kdeploy execute deploy.rb update
240
+ ```
211
241
  MD
212
242
  end
213
243
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'erb'
4
4
  require 'ostruct'
5
+ require 'tempfile'
5
6
 
6
7
  module Kdeploy
7
8
  class Template
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kdeploy
4
- VERSION = '0.3.0'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kdeploy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Norton