Class: Pec2::Pssh

Inherits:
Object
  • Object
show all
Defined in:
lib/pec2/pssh.rb

Instance Method Summary collapse

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
34
# File 'lib/pec2/pssh.rb', line 8

def initialize(options, 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
  }
  @color = options[:color]
  @user = options[:user]
  @print = options[:print]
  @sudo_password = options[:sudo_password]
  @ssh_options = {
    verify_host_key: :never,
    user_known_hosts_file: '/dev/null',
  }
  @logger = Logger.new(STDOUT)
end

Instance Method Details

#exec_pssh_command(command) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pec2/pssh.rb', line 36

def exec_pssh_command(command)
  return false if command.nil? || command.empty?
  exit_status = {}
  Parallel.map(@servers, in_threads: @parallel) do |server|
    exit_status[server[:host]] = exec_ssh(server, command)
  end
  errors = exit_status.select {|k, v| v != 0 }
  if errors.size > 0
    @logger.error "error servers => #{errors.keys.join(', ')}".colorize(:red)
    return false
  end
  return true
end