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) ⇒ 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 |
# 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] # 新增端口支持 end |
Instance Method Details
#build_command_result(stdout, stderr, command) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/kdeploy/executor.rb', line 53 def build_command_result(stdout, stderr, command) { stdout: stdout.strip, stderr: stderr.strip, command: command } end |
#execute(command) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/kdeploy/executor.rb', line 18 def execute(command) 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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/kdeploy/executor.rb', line 28 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
43 44 45 46 47 48 49 50 51 |
# File 'lib/kdeploy/executor.rb', line 43 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
61 62 63 64 65 66 67 |
# File 'lib/kdeploy/executor.rb', line 61 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
69 70 71 72 73 |
# File 'lib/kdeploy/executor.rb', line 69 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 |