Skip to content

Commit

Permalink
Adding concurrency option to control the number of concurrent hosts t…
Browse files Browse the repository at this point in the history
…hat SSH is run on; also updating gemspec dependency for net-ssh-multi to address race conditions affecting that gem and concurrency.
  • Loading branch information
dudemcbacon committed Apr 14, 2015
1 parent c8c42db commit 3909c8e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hammer_cli_foreman_ssh.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Gem::Specification.new do |s|

s.add_dependency 'hammer_cli', '>= 0.0.6'
s.add_dependency 'hammer_cli_foreman'
s.add_dependency 'net-ssh-multi'
s.add_dependency 'net-ssh-multi', '>= 1.2.1'
end
11 changes: 10 additions & 1 deletion lib/hammer_cli_foreman/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Command < HammerCLIForeman::Command

command_name 'SSH to hosts'
option %w(-c --command), 'COMMAND', _('Command to execute'), :attribute_name => :command, :required => true
option %w(-n --concurrent), 'CONCURRENCY', _('Number of concurrent SSH sessions'), :attribute_name => :concurrent
option %w(-u --user), 'USER', _('Execute as user'), :attribute_name => :user, :default => ENV['USER']
option %w(-s --search), 'FILTER', _('Filter hosts based on a filter'), :attribute_name => :search
option '--[no-]dns', :flag, _('Use DNS to resolve IP addresses'), :attribute_name => :use_dns
Expand All @@ -28,6 +29,13 @@ def request_params
end

def execute
if concurrent && concurrent.to_i == 0
warn _("specify 1 or more concurrent hosts")
return HammerCLI::EX_OK
end

limit = concurrent.nil? ? concurrent : concurrent.to_i

puts _("About to execute: #{command} as user #{user}\n" +
"on the following #{hosts.size} hosts: #{host_names.join(', ')}")

Expand All @@ -39,7 +47,8 @@ def execute
ssh_options = { :user => user, :auth_methods => ['publickey'] }
ssh_options[:keys] = [identity_file] unless identity_file.to_s.empty?

Net::SSH::Multi.start(:on_error => :warn) do |session|
Net::SSH::Multi.start(:concurrent_connections => limit, :on_error => :warn) do |session|
puts _("executing on #{limit} concurrent host(s)") if limit.to_i > 0
targets.each { |s| session.use s, ssh_options }
session.exec command
session.loop
Expand Down

0 comments on commit 3909c8e

Please sign in to comment.