Class: Producer::Core::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/producer/core/remote.rb,
lib/producer/core/remote/fs.rb,
lib/producer/core/remote/environment.rb

Direct Known Subclasses

Testing::MockRemote

Defined Under Namespace

Classes: Environment, FS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ Remote

Returns a new instance of Remote.



7
8
9
# File 'lib/producer/core/remote.rb', line 7

def initialize(hostname)
  @hostname = hostname
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



4
5
6
# File 'lib/producer/core/remote.rb', line 4

def hostname
  @hostname
end

#sessionObject



11
12
13
# File 'lib/producer/core/remote.rb', line 11

def session
  @session ||= Net::SSH.start(@hostname, user_name)
end

Instance Method Details

#cleanupObject



51
52
53
# File 'lib/producer/core/remote.rb', line 51

def cleanup
  session.close if @session
end

#configObject



15
16
17
# File 'lib/producer/core/remote.rb', line 15

def config
  @config ||= Net::SSH::Config.for(@hostname)
end

#environmentObject



47
48
49
# File 'lib/producer/core/remote.rb', line 47

def environment
  Environment.string_to_hash(execute 'env')
end

#execute(command, output = '', error_output = '') ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/producer/core/remote.rb', line 27

def execute(command, output = '', error_output = '')
  session.open_channel do |channel|
    channel.exec command do |ch, success|
      ch.on_data do |c, data|
        output << data
      end

      ch.on_extended_data do |c, type, data|
        error_output << data
      end

      ch.on_request 'exit-status' do |c, data|
        exit_status = data.read_long
        fail RemoteCommandExecutionError, command if exit_status != 0
      end
    end
  end.wait
  output
end

#fsObject



23
24
25
# File 'lib/producer/core/remote.rb', line 23

def fs
  @fs ||= Remote::FS.new(session.sftp.connect)
end

#user_nameObject



19
20
21
# File 'lib/producer/core/remote.rb', line 19

def user_name
  config[:user] || Etc.getlogin
end