Class: Minionizer::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/minionizer/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minion, ssh_connector = Net::SSH, scp_connector = Net::SCP, command_executor = CommandExecution) ⇒ Session

Returns a new instance of Session.



8
9
10
11
12
13
14
15
16
# File 'lib/minionizer/session.rb', line 8

def initialize(minion,
               ssh_connector=Net::SSH,
               scp_connector=Net::SCP,
               command_executor=CommandExecution)
  @minion = minion
  @ssh_connector = ssh_connector
  @scp_connector = scp_connector
  @command_executor = command_executor
end

Instance Attribute Details

#command_executorObject (readonly)

Returns the value of attribute command_executor.



4
5
6
# File 'lib/minionizer/session.rb', line 4

def command_executor
  @command_executor
end

#minionObject (readonly)

Returns the value of attribute minion.



4
5
6
# File 'lib/minionizer/session.rb', line 4

def minion
  @minion
end

#scp_connectorObject (readonly)

Returns the value of attribute scp_connector.



4
5
6
# File 'lib/minionizer/session.rb', line 4

def scp_connector
  @scp_connector
end

#ssh_connectorObject (readonly)

Returns the value of attribute ssh_connector.



4
5
6
# File 'lib/minionizer/session.rb', line 4

def ssh_connector
  @ssh_connector
end

Instance Method Details

#exec(*commands) ⇒ Object



29
30
31
32
33
# File 'lib/minionizer/session.rb', line 29

def exec(*commands)
  options = commands.last.is_a?(Hash) ? commands.pop : {}
  results = commands.map { |command| execution(command, options).call }
  results.length == 1 ? results.first : results
end

#scp(local_path, remote_path) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/minionizer/session.rb', line 35

def scp(local_path, remote_path)
  if with_sudo?
    tmp_filename = "#{SecureRandom.hex}.minionizer_tempfile"
    scp_connection.upload!(local_path, "#{tmp_filename}")
    exec("mv #{tmp_filename} #{remote_path}")
  else
    scp_connection.upload!(local_path, remote_path)
  end
end

#sudo(*commands) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/minionizer/session.rb', line 18

def sudo(*commands)
  @with_sudo = true
  if commands.any?
    return exec(*commands)
  else
    yield self
  end
ensure
  @with_sudo = false
end