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
47
48
# 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 "Could not execute command: #{command}" unless success

        channel.on_data do |_ch, data|
          print data # 实时输出
          stdout << data
        end

        channel.on_extended_data do |_ch, _type, data|
          print 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.message}"
rescue StandardError => e
  raise "SSH execution failed: #{e.message}"
end

#upload(source, destination) ⇒ Object



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

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



58
59
60
61
62
# File 'lib/kdeploy/executor.rb', line 58

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