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
# 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.exec!(command) do |_channel, stream, data|
      case stream
      when :stdout
        stdout << data
      when :stderr
        stderr << data
      end
    end

    {
      stdout: stdout.strip,
      stderr: stderr.strip,
      command: command
    }
  end
rescue Net::SSH::AuthenticationFailed => e
  raise "SSH authentication failed: #{e.message}"
rescue StandardError => e
  raise "SSH execution failed: #{e.message}"
end

#upload(source, destination) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/kdeploy/executor.rb', line 43

def upload(source, destination)
  Net::SCP.start(@ip, @user, ssh_options) do |scp|
    scp.upload!(source, destination)
  end
rescue StandardError => e
  raise "SCP upload failed: #{e.message}"
end

#upload_template(source, destination, variables = {}) ⇒ Object



51
52
53
54
55
# File 'lib/kdeploy/executor.rb', line 51

def upload_template(source, destination, variables = {})
  Template.render_and_upload(self, source, destination, variables)
rescue StandardError => e
  raise "Template upload failed: #{e.message}"
end