Class: Simp::Cli::Commands::Runpuppet

Inherits:
Simp::Cli
  • Object
show all
Defined in:
lib/simp/cli/commands/runpuppet.rb

Constant Summary

Constants inherited from Simp::Cli

VERSION

Class Method Summary collapse

Methods inherited from Simp::Cli

help, menu

Class Method Details

.run(args = Array.new) ⇒ Object

Raises:

  • (Simp::Runpuppet::Error)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/simp/cli/commands/runpuppet.rb', line 47

def self.run(args = Array.new)
  super

  raise Simp::Runpuppet::Error.new("SIMP RunPuppet cannot be run as 'root'.") if Process.uid == 0

  host_list = Array.new
  if @gen_host_list
    host_list = %x{cd /;sudo /usr/sbin/puppetca --list --all}.split("\n").map do |host|
      host.split(/\(.*\)/).first.split(/\s+/).last.delete('"')
    end
  else
    File.open(@host_file).each_line do |line|
      host_list << line.chomp
    end
  end
  host_list.compact!

  system("echo '#{ "Please review the lists of hosts to run puppet on:\n - #{host_list.join("\n - ")}" }' | less -F")

  if Utils.yes_or_no("Run Puppet on all of the listed hosts?", false)
    host_errors = Array.new
    if @gen_host_list
      File.open(@host_file, 'w') do
        host_list.each { |host| file.puts host }
      end
    end

    puts "This may take some time..."
    %x{pssh -f -t #{@timeout} -p #{@max_parallel} -h #{@host_file} -OStrictHostKeyChecking=no "cd /; sudo /usr/sbin/puppetd --test"}.each_line do |line|
      puts line
      if line =~ /\[\d+\].*\[FAILURE\]\s([A-Za-z0-9\-\.]+).*/
        host_errors << $1
      end
    end

    if host_errors.empty?
      puts "Successfully ran Puppet for the #{host_list.size} hosts listed in #{@host_file}."
    else
      timestamp = Time.new.strftime("%Y%m%d%H%M")
      filepath = File.expand_path("#{@conf_dir}/pssh_error#{timestamp}")
      FileUtils.mkpath(File.dirname(filepath))
      File.open(filepath, 'w') do file
        host_errors.each { |err| file.puts err }
      end
      raise "Errors while running Puppet, outputting list of hosts with errors to #{@conf_dir}/pssh_error#{timestamp}"
    end
  end
end