Class: Pec2::Pssh
- Inherits:
-
Object
- Object
- Pec2::Pssh
- Defined in:
- lib/pec2/pssh.rb
Instance Method Summary collapse
- #exec_pssh_command(command) ⇒ Object
-
#initialize(options, servers, parallel = 1) ⇒ Pssh
constructor
A new instance of Pssh.
Constructor Details
#initialize(options, servers, parallel = 1) ⇒ Pssh
Returns a new instance of Pssh.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pec2/pssh.rb', line 8 def initialize(, servers, parallel = 1) @parallel = parallel color_index = 0 colors = String.colors.select{ |color| !color.to_s.start_with?('light_') && !color.to_s.include?('red') && !color.to_s.include?('yellow') } @servers = servers.map { |server| result = {} result[:host] = server result[:color] = colors[color_index] if colors.size == color_index + 1 color_index = 0 else color_index = color_index + 1 end result } @user = [:user] @print = [:print] @sudo_password = [:sudo_password] = { verify_host_key: false, user_known_hosts_file: '/dev/null', } @logger = Logger.new(STDOUT) end |
Instance Method Details
#exec_pssh_command(command) ⇒ Object
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 |
# File 'lib/pec2/pssh.rb', line 35 def exec_pssh_command(command) return false if command.nil? || command.empty? error_servers = [] Parallel.each(@servers, in_threads: @parallel) do |server| begin Net::SSH.start(server[:host], @user, ) do |ssh| channel = ssh.open_channel do |channel, success| channel.on_data do |channel, data| if data =~ /^\[sudo\] password for / channel.send_data "#{@sudo_password}\n" else data.to_s.lines.each do |line| if @print print %Q{#{server[:host]}:#{line}}.colorize(server[:color]) end end end end channel.request_pty channel.exec(command) channel.wait end channel.wait end rescue => e error_servers << server[:host] puts "\n#{e.message}\n#{e.backtrace.join("\n")}" end end if error_servers.size > 0 @logger.error "error servers => #{error_servers.join(', ')}".colorize(:red) end return true end |