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.



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

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.



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

def hostname
  @hostname
end

#ipObject

Returns the value of attribute ip.



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

def ip
  @ip
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.connect(options = {}) ⇒ Object



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

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

Instance Method Details

#closeObject



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

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

#connect(_options = {}) ⇒ Object



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

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

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



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
68
# File 'lib/beaker/local_connection.rb', line 38

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



85
86
87
# File 'lib/beaker/local_connection.rb', line 85

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

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



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

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



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

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