Class: Kdeploy::Executor
- Inherits:
-
Object
- Object
- Kdeploy::Executor
- Defined in:
- lib/kdeploy/executor.rb
Overview
SSH/SCP executor for remote command execution and file operations
Instance Method Summary collapse
- #build_command_result(stdout, stderr, command) ⇒ Object
- #execute(command, use_sudo: nil) ⇒ Object
- #execute_command_on_ssh(ssh, command) ⇒ Object
-
#initialize(host_config) ⇒ Executor
constructor
A new instance of Executor.
- #setup_channel_handlers(channel, stdout, stderr) ⇒ Object
- #upload(source, destination) ⇒ Object
- #upload_template(source, destination, variables = {}) ⇒ Object
Constructor Details
#initialize(host_config) ⇒ Executor
Returns a new instance of Executor.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/kdeploy/executor.rb', line 9 def initialize(host_config) @host = host_config[:name] @user = host_config[:user] @ip = host_config[:ip] @password = host_config[:password] @key = host_config[:key] @port = host_config[:port] # 新增端口支持 @use_sudo = host_config[:use_sudo] || false @sudo_password = host_config[:sudo_password] end |
Instance Method Details
#build_command_result(stdout, stderr, command) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/kdeploy/executor.rb', line 58 def build_command_result(stdout, stderr, command) { stdout: stdout.strip, stderr: stderr.strip, command: command } end |
#execute(command, use_sudo: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kdeploy/executor.rb', line 20 def execute(command, use_sudo: nil) use_sudo = @use_sudo if use_sudo.nil? command = wrap_with_sudo(command) if use_sudo Net::SSH.start(@ip, @user, ) do |ssh| execute_command_on_ssh(ssh, command) end rescue Net::SSH::AuthenticationFailed => e raise SSHError.new("SSH authentication failed: #{e.}", e) rescue StandardError => e raise SSHError.new("SSH execution failed: #{e.}", e) end |
#execute_command_on_ssh(ssh, command) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kdeploy/executor.rb', line 33 def execute_command_on_ssh(ssh, command) stdout = String.new stderr = String.new ssh.open_channel do |channel| channel.exec(command) do |_ch, success| raise SSHError, "Could not execute command: #{command}" unless success setup_channel_handlers(channel, stdout, stderr) end end ssh.loop build_command_result(stdout, stderr, command) end |
#setup_channel_handlers(channel, stdout, stderr) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/kdeploy/executor.rb', line 48 def setup_channel_handlers(channel, stdout, stderr) channel.on_data do |_ch, data| stdout << data end channel.on_extended_data do |_ch, _type, data| stderr << data end end |
#upload(source, destination) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/kdeploy/executor.rb', line 66 def upload(source, destination) Net::SCP.start(@ip, @user, ) do |scp| scp.upload!(source, destination) end rescue StandardError => e raise SCPError.new("SCP upload failed: #{e.}", e) end |
#upload_template(source, destination, variables = {}) ⇒ Object
74 75 76 77 78 |
# File 'lib/kdeploy/executor.rb', line 74 def upload_template(source, destination, variables = {}) Template.render_and_upload(self, source, destination, variables) rescue StandardError => e raise TemplateError.new("Template upload failed: #{e.}", e) end |