Class: Beaker::LocalConnection
- Inherits:
-
Object
- Object
- Beaker::LocalConnection
- Defined in:
- lib/beaker/local_connection.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #connect(options = {}) ⇒ Object
- #execute(command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Object
-
#initialize(options = {}) ⇒ LocalConnection
constructor
A new instance of LocalConnection.
- #scp_from(source, target, options = {}) ⇒ Object
- #scp_to(source, target, _options = {}) ⇒ Object
- #with_env(env) ⇒ Object
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 = {} @logger = [:logger] @ssh_env_file = File.([:ssh_env_file]) @hostname = 'localhost' @ip = '127.0.0.1' @options = end |
Instance Attribute Details
#hostname ⇒ Object
Returns the value of attribute hostname.
6 7 8 |
# File 'lib/beaker/local_connection.rb', line 6 def hostname @hostname end |
#ip ⇒ Object
Returns the value of attribute ip.
6 7 8 |
# File 'lib/beaker/local_connection.rb', line 6 def ip @ip end |
#logger ⇒ Object
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 = {} connection = new connection.connect connection end |
Instance Method Details
#close ⇒ Object
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 = {} @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, = {}, 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| k =~ /^BUNDLE|^RUBY|^GEM/ } 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, = {}) scp_to(target, source, ) 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, = {}) 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 |