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 16, 2015
1 parent 52b0de7 commit fb2f345
Show file tree
Hide file tree
Showing 2 changed files with 8 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 @@ -14,5 +14,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
8 changes: 7 additions & 1 deletion lib/hammer_cli_foreman_ssh/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ 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 do |o|
Integer(o)
end
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 +31,8 @@ def request_params
end

def execute
signal_usage_error(_("specify 1 or more concurrent hosts")) if (!concurrent.nil? && concurrent < 1)

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

Expand All @@ -39,7 +44,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 => concurrent, :on_error => :warn) do |session|
logger.info(_("executing on #{concurrent} concurrent host(s)")) if concurrent.to_i > 0
targets.each { |s| session.use s, ssh_options }
session.exec command
session.loop
Expand Down

0 comments on commit fb2f345

Please sign in to comment.