kdeploy 1.2.8 → 1.2.9

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: dfcdcad209af73c3d5cb1d272a49a9e8c5cabf784616b3f05c43c1c75b4fef25
4
- data.tar.gz: 8583a28f2a36e460a64617beb30f8b1e1054f649d8188ba15ab9b989bd2e5269
3
+ metadata.gz: acdfc170983f536cbc11351f4bcc1c55d3eae2c0c11fd00e245b1de8fb27023e
4
+ data.tar.gz: 01cb5edb4625625bd39ce011b4c658fa04d38c54e58745b51fec6f86f81186d0
5
5
  SHA512:
6
- metadata.gz: 6f6f3409c9932e9d9477ab07a3966dfa254883f2324c2e6a9e409a30e1c54506e4aca9f9dc868de1ff3f4ec56d9b56fc64027b24ac9809a762184938211cd132
7
- data.tar.gz: d6ed8e62f6bb0b74360aee9214714eedbffbb4329cea170248c0f53c045eab2a882e46134172d71d0721eaf466710b0a86af585f3e0c7ebc88078446b2441c23
6
+ metadata.gz: 5cf3bcbaf739597384afbf38223d7ceae24a7dbe0519563c56de1fe00dcdd709ada2f49e6529edf8eb405c45240fa459cd9da6d84558f00d217931d9f5a6d93c
7
+ data.tar.gz: 3534c6c126d01c89836bcad4bdb7293552911ab7ddbb285ad23bf7a8ab39ac40ff759be894fcc6a36599f0f11549b0e2f19388a20b370f70a9e53f9feca10ffb
data/README.md CHANGED
@@ -259,6 +259,13 @@ host "web03",
259
259
  ip: "10.0.0.3",
260
260
  key: "~/.ssh/id_rsa",
261
261
  port: 2222
262
+
263
+ # 使用 sudo 的主机(所有命令自动使用 sudo)
264
+ host "web04",
265
+ user: "ubuntu",
266
+ ip: "10.0.0.4",
267
+ key: "~/.ssh/id_rsa",
268
+ use_sudo: true
262
269
  ```
263
270
 
264
271
  #### 主机配置选项
@@ -270,6 +277,8 @@ host "web03",
270
277
  | `key` | String | 否* | SSH 私钥文件路径 |
271
278
  | `password` | String | 否* | SSH 密码 |
272
279
  | `port` | Integer | 否 | SSH 端口(默认: 22) |
280
+ | `use_sudo` | Boolean | 否 | 是否对所有命令自动使用 sudo(默认: false) |
281
+ | `sudo_password` | String | 否 | sudo 密码(如果需要密码验证) |
273
282
 
274
283
  \* 身份验证需要 `key` 或 `password` 之一。
275
284
 
@@ -394,6 +403,44 @@ run <<~SHELL
394
403
  SHELL
395
404
  ```
396
405
 
406
+ **参数:**
407
+ - `command`: 要执行的命令字符串
408
+ - `sudo`: 布尔值,是否使用 sudo 执行此命令(可选,默认: nil,继承主机配置)
409
+
410
+ **sudo 使用方式:**
411
+
412
+ 1. **在主机级别配置**(所有命令自动使用 sudo):
413
+ ```ruby
414
+ host "web01",
415
+ user: "ubuntu",
416
+ ip: "10.0.0.1",
417
+ key: "~/.ssh/id_rsa",
418
+ use_sudo: true # 所有命令自动使用 sudo
419
+ ```
420
+
421
+ 2. **在命令级别配置**(仅特定命令使用 sudo):
422
+ ```ruby
423
+ task :deploy do
424
+ run "systemctl restart nginx", sudo: true # 仅此命令使用 sudo
425
+ run "echo 'Deployed'" # 此命令不使用 sudo
426
+ end
427
+ ```
428
+
429
+ 3. **使用 sudo 密码**(如果需要密码验证):
430
+ ```ruby
431
+ host "web01",
432
+ user: "ubuntu",
433
+ ip: "10.0.0.1",
434
+ key: "~/.ssh/id_rsa",
435
+ use_sudo: true,
436
+ sudo_password: "your-sudo-password" # 仅在需要密码时配置
437
+ ```
438
+
439
+ **注意:**
440
+ - 如果命令已经以 `sudo` 开头,工具不会重复添加
441
+ - 推荐使用 NOPASSWD 配置 sudo,避免在配置文件中存储密码
442
+ - 命令级别的 `sudo` 选项会覆盖主机级别的 `use_sudo` 配置
443
+
397
444
  **最佳实践**: 对多行命令使用 heredoc (`<<~SHELL`) 以提高可读性。
398
445
 
399
446
  #### `upload` - 上传文件
data/README_EN.md CHANGED
@@ -259,6 +259,13 @@ host "web03",
259
259
  ip: "10.0.0.3",
260
260
  key: "~/.ssh/id_rsa",
261
261
  port: 2222
262
+
263
+ # Host with sudo (all commands automatically use sudo)
264
+ host "web04",
265
+ user: "ubuntu",
266
+ ip: "10.0.0.4",
267
+ key: "~/.ssh/id_rsa",
268
+ use_sudo: true
262
269
  ```
263
270
 
264
271
  #### Host Configuration Options
@@ -270,6 +277,8 @@ host "web03",
270
277
  | `key` | String | No* | Path to SSH private key file |
271
278
  | `password` | String | No* | SSH password |
272
279
  | `port` | Integer | No | SSH port (default: 22) |
280
+ | `use_sudo` | Boolean | No | Automatically use sudo for all commands (default: false) |
281
+ | `sudo_password` | String | No | sudo password (if password authentication is required) |
273
282
 
274
283
  \* Either `key` or `password` is required for authentication.
275
284
 
@@ -394,6 +403,44 @@ run <<~SHELL
394
403
  SHELL
395
404
  ```
396
405
 
406
+ **Parameters:**
407
+ - `command`: Command string to execute
408
+ - `sudo`: Boolean, whether to execute this command with sudo (optional, default: nil, inherits host configuration)
409
+
410
+ **Using sudo:**
411
+
412
+ 1. **At host level** (all commands automatically use sudo):
413
+ ```ruby
414
+ host "web01",
415
+ user: "ubuntu",
416
+ ip: "10.0.0.1",
417
+ key: "~/.ssh/id_rsa",
418
+ use_sudo: true # All commands automatically use sudo
419
+ ```
420
+
421
+ 2. **At command level** (only specific commands use sudo):
422
+ ```ruby
423
+ task :deploy do
424
+ run "systemctl restart nginx", sudo: true # Only this command uses sudo
425
+ run "echo 'Deployed'" # This command does not use sudo
426
+ end
427
+ ```
428
+
429
+ 3. **With sudo password** (if password authentication is required):
430
+ ```ruby
431
+ host "web01",
432
+ user: "ubuntu",
433
+ ip: "10.0.0.1",
434
+ key: "~/.ssh/id_rsa",
435
+ use_sudo: true,
436
+ sudo_password: "your-sudo-password" # Only configure if password is required
437
+ ```
438
+
439
+ **Notes:**
440
+ - If the command already starts with `sudo`, the tool will not add it again
441
+ - It's recommended to use NOPASSWD sudo configuration to avoid storing passwords in configuration files
442
+ - Command-level `sudo` option overrides host-level `use_sudo` configuration
443
+
397
444
  **Best Practice**: Use heredoc (`<<~SHELL`) for multi-line commands to improve readability.
398
445
 
399
446
  #### `upload` - Upload Files
@@ -9,12 +9,14 @@ module Kdeploy
9
9
  end
10
10
 
11
11
  def execute_run(command, host_name)
12
- show_command_header(host_name, :run, command[:command])
12
+ cmd = command[:command]
13
+ use_sudo = command[:sudo]
14
+ show_command_header(host_name, :run, cmd)
13
15
  result, duration = measure_time do
14
- @executor.execute(command[:command])
16
+ @executor.execute(cmd, use_sudo: use_sudo)
15
17
  end
16
18
  show_command_output(result)
17
- { command: command[:command], output: result, duration: duration, type: :run }
19
+ { command: cmd, output: result, duration: duration, type: :run }
18
20
  end
19
21
 
20
22
  def execute_upload(command, host_name)
data/lib/kdeploy/dsl.rb CHANGED
@@ -63,9 +63,9 @@ module Kdeploy
63
63
  }
64
64
  end
65
65
 
66
- def run(command)
66
+ def run(command, sudo: nil)
67
67
  @kdeploy_commands ||= []
68
- @kdeploy_commands << { type: :run, command: command }
68
+ @kdeploy_commands << { type: :run, command: command, sudo: sudo }
69
69
  end
70
70
 
71
71
  def upload(source, destination)
@@ -13,9 +13,14 @@ module Kdeploy
13
13
  @password = host_config[:password]
14
14
  @key = host_config[:key]
15
15
  @port = host_config[:port] # 新增端口支持
16
+ @use_sudo = host_config[:use_sudo] || false
17
+ @sudo_password = host_config[:sudo_password]
16
18
  end
17
19
 
18
- def execute(command)
20
+ def execute(command, use_sudo: nil)
21
+ use_sudo = @use_sudo if use_sudo.nil?
22
+ command = wrap_with_sudo(command) if use_sudo
23
+
19
24
  Net::SSH.start(@ip, @user, ssh_options) do |ssh|
20
25
  execute_command_on_ssh(ssh, command)
21
26
  end
@@ -99,5 +104,19 @@ module Kdeploy
99
104
  def add_port_option(options)
100
105
  options[:port] = @port if @port
101
106
  end
107
+
108
+ def wrap_with_sudo(command)
109
+ # 如果命令已经以 sudo 开头,不重复添加
110
+ return command if command.strip.start_with?('sudo')
111
+
112
+ if @sudo_password
113
+ # 使用 echo 和管道传递密码给 sudo -S
114
+ # 注意:密码会出现在进程列表中,建议使用 NOPASSWD 配置
115
+ escaped_password = @sudo_password.gsub("'", "'\"'\"'").gsub('$', '\\$').gsub('`', '\\`')
116
+ "echo '#{escaped_password}' | sudo -S #{command}"
117
+ else
118
+ "sudo #{command}"
119
+ end
120
+ end
102
121
  end
103
122
  end
@@ -31,6 +31,8 @@ module Kdeploy
31
31
  # Define hosts
32
32
  host 'web01', user: 'ubuntu', ip: '10.0.0.1', key: '~/.ssh/id_rsa'
33
33
  host 'web02', user: 'ubuntu', ip: '10.0.0.2', key: '~/.ssh/id_rsa'
34
+ # Example: Host with sudo enabled (all commands automatically use sudo)
35
+ # host 'web03', user: 'ubuntu', ip: '10.0.0.3', key: '~/.ssh/id_rsa', use_sudo: true
34
36
  host 'db01', user: 'root', ip: '10.0.0.3', key: '~/.ssh/id_rsa'
35
37
 
36
38
  # Define roles
@@ -39,6 +41,8 @@ module Kdeploy
39
41
 
40
42
  # Define deployment task for web servers
41
43
  task :deploy_web, roles: :web do
44
+ # Example: Using sudo option for specific command
45
+ # run "systemctl stop nginx", sudo: true
42
46
  run <<~SHELL
43
47
  sudo systemctl stop nginx
44
48
  echo "Deploying..."
@@ -78,6 +82,8 @@ module Kdeploy
78
82
 
79
83
  # Define task for all hosts
80
84
  task :update do
85
+ # Example: Using sudo option for specific command
86
+ # run "apt-get update && apt-get upgrade -y", sudo: true
81
87
  run <<~SHELL
82
88
  sudo apt-get update && sudo apt-get upgrade -y
83
89
  SHELL
@@ -189,6 +195,22 @@ module Kdeploy
189
195
  worker_processes: 4
190
196
  ```
191
197
 
198
+ ## 🔐 Using Sudo
199
+
200
+ Kdeploy supports sudo execution in two ways:
201
+
202
+ 1. **Host-level configuration** (all commands automatically use sudo):
203
+ ```ruby
204
+ host 'web01', user: 'ubuntu', ip: '10.0.0.1', key: '~/.ssh/id_rsa', use_sudo: true
205
+ ```
206
+
207
+ 2. **Command-level configuration** (only specific commands use sudo):
208
+ ```ruby
209
+ run "systemctl restart nginx", sudo: true
210
+ ```
211
+
212
+ See the main documentation for more details on sudo usage.
213
+
192
214
  ## 🚀 Usage
193
215
 
194
216
  ### Task Execution
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Kdeploy module for version management
4
4
  module Kdeploy
5
- VERSION = '1.2.8' unless const_defined?(:VERSION)
5
+ VERSION = '1.2.9' unless const_defined?(:VERSION)
6
6
  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: 1.2.8
4
+ version: 1.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kk