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
14
15
16
17
18
19
20
21
22
# File 'lib/producer/core/remote.rb', line 11

def session
  @session ||= begin
    check_hostname!
    Net::SSH.start @hostname, user_name, verify_host_key: :always
  rescue Net::SSH::HostKeyError => e
    error = '`%s\' authenticity can\'t be established' % hostname
    case e
      when Net::SSH::HostKeyUnknown then fail RemoteUnknownError, error
      when Net::SSH::HostKeyMismatch then fail RemoteMismatchError, error
    end
  end
end

Instance Method Details

#cleanupObject



60
61
62
# File 'lib/producer/core/remote.rb', line 60

def cleanup
  session.close if @session
end

#configObject



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

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

#environmentObject



56
57
58
# File 'lib/producer/core/remote.rb', line 56

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

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/producer/core/remote.rb', line 36

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



32
33
34
# File 'lib/producer/core/remote.rb', line 32

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

#user_nameObject



28
29
30
# File 'lib/producer/core/remote.rb', line 28

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