Class: PolySSH::CommandBuilder

Inherits:
Object
  • Object
show all
Includes:
VisitorBase
Defined in:
lib/polyssh/command_builder.rb

Instance Method Summary collapse

Methods included from VisitorBase

#do_visit

Constructor Details

#initializeCommandBuilder



6
7
8
9
10
# File 'lib/polyssh/command_builder.rb', line 6

def initialize 
  @commands = []
  @last_command = ""
  @baseport = 18000 + rand(2000)
end

Instance Method Details

#get_portObject

Detect next available port



57
58
59
60
61
62
# File 'lib/polyssh/command_builder.rb', line 57

def get_port
  @baseport += 1
  while !port_open?(@baseport) do  
    @baseport += 1
  end
end

#port_open?(port) ⇒ Boolean

Test if given port is locally used



65
66
67
# File 'lib/polyssh/command_builder.rb', line 65

def port_open?(port)
    !system("lsof -i:#{port}", out: '/dev/null')
end

#visit_polyssh_nodeentry(node_entry) ⇒ Object

Visit each node entry



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/polyssh/command_builder.rb', line 27

def visit_polyssh_nodeentry node_entry
  cmd=""
  if node_entry.next.nil? then
    cmd = ("ssh " +
           "-o ForwardAgent=yes " +
           "-o UserKnownHostsFile=/dev/null " +
           "-o StrictHostKeyChecking=no " +
           "-p #{@baseport} " +
           "-l #{node_entry.user} " +
           "localhost " 
          )
    @last_command = cmd
  else
    get_port
    next_entry = node_entry.next 
    cmd = (
      "ssh " +
      "-o ForwardAgent=yes " +
      "-o UserKnownHostsFile=/dev/null " +
      "-o StrictHostKeyChecking=no " +
      "-N " +
      "-L#{@baseport}:#{next_entry.host}:#{next_entry.port} " +
      "-l #{node_entry.user} #{node_entry.host} "
    )
    @commands << cmd
    next_entry.accept(self)
  end
end

#visit_polyssh_nodelist(node_list) ⇒ Object

Visit node list



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/polyssh/command_builder.rb', line 13

def visit_polyssh_nodelist node_list
    if node_list.count > 0 then
      node_list.first.accept(self)
    end
    @commands.each do |cmd|
      puts cmd
      fork { exec cmd + " >/dev/null 2>&1 " }
      sleep 2
    end
    puts @last_command 
    system @last_command
end