Module: Rcmd
- Defined in:
- lib/rcmd.rb,
lib/rcmd/db.rb,
lib/rcmd/version.rb
Overview
:notnew:
Defined Under Namespace
Classes: DB
Constant Summary collapse
- VERSION =
VERSION- Version number string "1.6.4"
Class Attribute Summary collapse
-
.command ⇒ Object
String containing the command to be used.
-
.debug ⇒ Object
Boolean for debug output.
-
.host_list ⇒ Object
An array of hosts to run the given command on.
-
.nthreads ⇒ Object
Prefered/requested number of threads to be used.
-
.quiet ⇒ Object
Boolean for disabling STDOUT output.
-
.threads ⇒ Object
Array containing the current iterations thread objects.
-
.user ⇒ Object
What user should we connect as.
Class Method Summary collapse
-
.run_command ⇒ Object
Main method for this module which should be called after the correct variables have been set.
Class Attribute Details
.command ⇒ Object
String containing the command to be used. (Manditory)
35 36 37 |
# File 'lib/rcmd.rb', line 35 def command @command end |
.debug ⇒ Object
Boolean for debug output
44 45 46 |
# File 'lib/rcmd.rb', line 44 def debug @debug end |
.host_list ⇒ Object
An array of hosts to run the given command on. (Manditory)
32 33 34 |
# File 'lib/rcmd.rb', line 32 def host_list @host_list end |
.nthreads ⇒ Object
Prefered/requested number of threads to be used. (Manditory)
26 27 28 |
# File 'lib/rcmd.rb', line 26 def nthreads @nthreads end |
.quiet ⇒ Object
Boolean for disabling STDOUT output. SDTERR is always displayed. (Optional)
38 39 40 |
# File 'lib/rcmd.rb', line 38 def quiet @quiet end |
.threads ⇒ Object
Array containing the current iterations thread objects.
41 42 43 |
# File 'lib/rcmd.rb', line 41 def threads @threads end |
.user ⇒ Object
What user should we connect as. (Manditory)
29 30 31 |
# File 'lib/rcmd.rb', line 29 def user @user end |
Class Method Details
.run_command ⇒ Object
Main method for this module which should be called after the correct variables have been set.
We iterate over the host list until it is empty, creating all needed threads based upon the prefered number of threads for execution. Before creating the threads, the method first checks if the preferred number of threads is greater then the number of hosts remaining in the host list. If false (threads > num hosts) then the number of remaining hosts becomes the thread count. This prevents spawning of unneeded threads.
Manditory values to be set
-
:user- User to run the command as -
:command- Command to be executed -
:host_list- Array containing the hosts for command execution. -
:nthreads- Preferred max number of threads
Optional Values
-
:quiet- Do not print to STDOUT. STDERR is always printed
Specifically for the method only
-
:threads- Array of the current threads
Example
require 'rcmd'
Rcmd.host_list= ["host1", "host2", "host3", "host4", "host 5", "host6"]
Rcmd.user= 'root'
Rcmd.command= 'rpm -qa kernel\*'
Rcmd.nthreads= 6
Rcmd.run_command
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rcmd.rb', line 80 def Rcmd.run_command() if not @command raise ArgumentError.new("No command set for execution") end if not @host_list.count >= 1 raise ArgumentError.new("host_list must contain at least one system") end @host_list.each do |host| @queue << host end until @queue.empty? # Don't start more threads then hosts. num_threads = @nthreads <= @queue.length ? @nthreads : @queue.length # Prepare threads @threads = [ ] num_threads.times do |i| @threads[i] = Thread.new { begin = { :user => @user, :host => @queue.pop, :password => nil, :quiet => @quiet, :debug => @debug } STDERR.print "DEBUG :: Connecting to #{conn_options[:host]}\n" if [:debug] Net::SSH.start([:host], [:user], :password => [:passwd]) do |session| # Open channel for input/output control session.open_channel do |channel| channel.on_data do |ch, data| # Print recieved data if quiet is not true STDOUT.print "#{conn_options[:host]} :: #{data}" unless [:quiet] end channel.on_extended_data do |ch,type,data| # Always print stderr data STDERR.print "#{conn_options[:host]} :: ERROR :: #{data}" end # Execute command channel.exec @command end # Loop until command completes session.loop end rescue STDERR.print "#{conn_options[:host]} :: CONNECT ERROR :: Unable to connect to host!\n" end } # Execute threads end @threads.each { |t| t.join } end unless @threads.each.map {|t| t.alive?}.none? sleep 1 end end |