Class: Kdeploy::Executor
- Inherits:
-
Object
- Object
- Kdeploy::Executor
- Defined in:
- lib/kdeploy/executor.rb
Instance Method Summary collapse
- #execute(command) ⇒ Object
-
#initialize(host_config) ⇒ Executor
constructor
A new instance of Executor.
- #upload(source, destination) ⇒ Object
- #upload_template(source, destination, variables = {}) ⇒ Object
Constructor Details
#initialize(host_config) ⇒ Executor
Returns a new instance of Executor.
8 9 10 11 12 13 14 15 |
# File 'lib/kdeploy/executor.rb', line 8 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
#execute(command) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kdeploy/executor.rb', line 17 def execute(command) Net::SSH.start(@ip, @user, ) do |ssh| stdout = String.new stderr = String.new ssh.open_channel do |channel| channel.exec(command) do |_ch, success| raise "Could not execute command: #{command}" unless success channel.on_data do |_ch, data| stdout << data end channel.on_extended_data do |_ch, _type, data| stderr << data end end end ssh.loop { stdout: stdout.strip, stderr: stderr.strip, command: command } end rescue Net::SSH::AuthenticationFailed => e raise "SSH authentication failed: #{e.}" rescue StandardError => e raise "SSH execution failed: #{e.}" end |
#upload(source, destination) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/kdeploy/executor.rb', line 48 def upload(source, destination) Net::SCP.start(@ip, @user, ) do |scp| scp.upload!(source, destination) end rescue StandardError => e raise "SCP upload failed: #{e.}" end |
#upload_template(source, destination, variables = {}) ⇒ Object
56 57 58 59 60 |
# File 'lib/kdeploy/executor.rb', line 56 def upload_template(source, destination, variables = {}) Template.render_and_upload(self, source, destination, variables) rescue StandardError => e raise "Template upload failed: #{e.}" end |