Class: Beaker::LocalConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/local_connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LocalConnection

Returns a new instance of LocalConnection.



7
8
9
10
11
12
13
# File 'lib/beaker/local_connection.rb', line 7

def initialize options = {}
  @logger = options[:logger]
  @ssh_env_file = File.expand_path(options[:ssh_env_file])
  @hostname = 'localhost'
  @ip = '127.0.0.1'
  @options = options
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



5
6
7
# File 'lib/beaker/local_connection.rb', line 5

def hostname
  @hostname
end

#ipObject

Returns the value of attribute ip.



5
6
7
# File 'lib/beaker/local_connection.rb', line 5

def ip
  @ip
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/beaker/local_connection.rb', line 5

def logger
  @logger
end

Class Method Details

.connect(options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/beaker/local_connection.rb', line 15

def self.connect options = {}
  connection = new options
  connection.connect
  connection
end

Instance Method Details

#closeObject



25
26
27
# File 'lib/beaker/local_connection.rb', line 25

def close
  @logger.debug "Local connection, no connection to close"
end

#connect(_options = {}) ⇒ Object



21
22
23
# File 'lib/beaker/local_connection.rb', line 21

def connect _options = {}
  @logger.debug "Local connection, no connection to start"
end

#execute(command, _options = {}, stdout_callback = nil, _stderr_callback = stdout_callback) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/beaker/local_connection.rb', line 37

def execute command, _options = {}, stdout_callback = nil, _stderr_callback = stdout_callback
  result = Result.new(@hostname, command)
  envs = {}
  if File.readable?(@ssh_env_file)
    File.foreach(@ssh_env_file) do |line|
      key, value = line.split('=')
      envs[key] = value
    end
  end

  begin
    clean_env = ENV.reject { |k| /^BUNDLE|^RUBY|^GEM/.match?(k) }

    with_env(clean_env) do
      std_out, std_err, status = Open3.capture3(envs, command)
      result.stdout << std_out
      result.stderr << std_err
      result.exit_code = status.exitstatus
      @logger.info(result.stdout) unless result.stdout.empty?
      @logger.info(result.stderr) unless result.stderr.empty?
    end
  rescue => e
    result.stderr << e.inspect
    @logger.info(result.stderr)
    result.exit_code = 1
  end

  result.finalize!
  @logger.last_result = result
  result
end

#scp_from(source, target, options = {}) ⇒ Object



83
84
85
# File 'lib/beaker/local_connection.rb', line 83

def scp_from(source, target, options = {})
  scp_to(target, source, options)
end

#scp_to(source, target, _options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/beaker/local_connection.rb', line 69

def scp_to(source, target, _options = {})
  result = Result.new(@hostname, [source, target])
  begin
    FileUtils.cp_r source, target
  rescue Errno::ENOENT => e
    @logger.warn "#{e.class} error in cp'ing. Forcing the connection to close, which should " \
                 "raise an error."
  end

  result.stdout << "  CP'ed file #{source} to #{target}"
  result.exit_code = 0
  result
end

#with_env(env) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/beaker/local_connection.rb', line 29

def with_env(env)
  backup = ENV.to_hash
  ENV.replace(env)
  yield
ensure
  ENV.replace(backup)
end