Class: Hotdog::Commands::Pssh

Inherits:
Search show all
Defined in:
lib/hotdog/commands/pssh.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::MASK_DATABASE, BaseCommand::MASK_QUERY, BaseCommand::PERSISTENT_DB

Instance Attribute Summary

Attributes inherited from BaseCommand

#application, #logger, #options

Instance Method Summary collapse

Methods inherited from Search

#evaluate, #get_hosts_with_search_tags, #parse

Methods inherited from BaseCommand

#execute, #fixed_string?, #initialize, #reload

Constructor Details

This class inherits a constructor from Hotdog::Commands::BaseCommand

Instance Method Details

#run(args = []) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hotdog/commands/pssh.rb', line 12

def run(args=[])
  ssh_option = {
    options: [],
    user: nil,
    port: nil,
    identity_file: nil,
    max_parallelism: nil,
  }

  optparse.on("-o SSH_OPTION", "Passes this string to ssh command through shell. This option may be given multiple times") do |option|
    ssh_option[:options] += [option]
  end
  optparse.on("-i SSH_IDENTITY_FILE", "SSH identity file path") do |path|
    ssh_option[:identity_file] = path
  end
  optparse.on("-p PORT", "Port of the remote host", Integer) do |port|
    ssh_option[:port] = port
  end
  optparse.on("-u SSH_USER", "SSH login user name") do |user|
    ssh_option[:user] = user
  end
  optparse.on("-P PARALLELISM", "Max parallelism", Integer) do |n|
    ssh_option[:max_parallelism] = n
  end

  use_color = STDOUT.tty?

  search_args = []
  optparse.order!(args) {|search_arg| search_args.push(search_arg) }
  expression = search_args.join(" ").strip
  if expression.empty? || args.empty?
    exit(1)
  end

  begin
    node = parse(expression)
  rescue Parslet::ParseFailed => error
    STDERR.puts("syntax error: " + error.cause.ascii_tree)
    exit(1)
  end

  result = evaluate(node, self).sort
  result, fields = get_hosts(result)

  if result.empty?
    STDERR.puts("no match found: #{search_args.join(" ")}")
    exit(1)
  end

  addresses = result.map {|host| [host.first, host.last] }

  # build ssh command
  cmdline = ["ssh"]
  ssh_option[:options].each do |option|
    cmdline << "-o" << option
  end
  if path = ssh_option[:identity_file]
    cmdline << "-i" << Shellwords.escape(path)
  end
  if port = ssh_option[:port]
    cmdline << "-p" << port.to_s
  end
  if ssh_option[:forward_agent]
    cmdline << "-A"
  end

  cmdline << "-o" << "BatchMode=yes"

  user = ssh_option[:user]

  threads = ssh_option[:max_parallelism] || addresses.size
  stats = Parallel.map(addresses, in_threads: threads) do |address,name|
    if use_color
      header = "\e[0;36m#{name}\e[00m"
    else
      header = name
    end

    c = cmdline.dup
    if user
      c << (user + "@" + address)
    else
      c << address
    end

    c.concat(args)

    IO.popen([*c, in: :close, err: [:child, :out]]) do |io|
      io.each_line {|line|
        STDOUT.write "#{header}: #{line}"
      }
    end
    $?.success?  # $? is thread-local variable
  end

  unless stats.all? {|success| success }
    exit(1)
  end
end