Class: Kdeploy::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/executor.rb

Instance Method Summary collapse

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, ssh_options) do |ssh|
    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

        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 SSHError.new("SSH authentication failed: #{e.message}", e)
rescue StandardError => e
  raise SSHError.new("SSH execution failed: #{e.message}", 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, ssh_options) do |scp|
    scp.upload!(source, destination)
  end
rescue StandardError => e
  raise SCPError.new("SCP upload failed: #{e.message}", 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 TemplateError.new("Template upload failed: #{e.message}", e)
end