summaryrefslogtreecommitdiffstats
path: root/lib/puppet/application/kick.rb
blob: 8c168b3baaea2f693fad9e1a37864bd2c07945e9 (plain)
1
2
3
4
5
6
7
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
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
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'puppet/application'

class Puppet::Application::Kick < Puppet::Application

    should_not_parse_config

    attr_accessor :hosts, :tags, :classes

    option("--all","-a")
    option("--foreground","-f")
    option("--debug","-d")
    option("--ping","-P")
    option("--test")

    option("--host HOST") do |arg|
        @hosts << arg
    end

    option("--tag TAG", "-t") do |arg|
        @tags << arg
    end

    option("--class CLASS", "-c") do |arg|
        @classes << arg
    end

    option("--no-fqdn", "-n") do |arg|
        options[:fqdn] = false
    end

    option("--parallel PARALLEL", "-p") do |arg|
        begin
            options[:parallel] = Integer(arg)
        rescue
            $stderr.puts "Could not convert %s to an integer" % arg.inspect
            exit(23)
        end
    end

    def run_command
        @hosts += command_line.args
        options[:test] ? test : main
    end

    def test
        puts "Skipping execution in test mode"
        exit(0)
    end

    def main
        require 'puppet/network/client'

        Puppet.warning "Failed to load ruby LDAP library. LDAP functionality will not be available" unless Puppet.features.ldap?
        require 'puppet/util/ldap/connection'

        todo = @hosts.dup

        failures = []

        # Now do the actual work
        go = true
        while go
            # If we don't have enough children in process and we still have hosts left to
            # do, then do the next host.
            if @children.length < options[:parallel] and ! todo.empty?
                host = todo.shift
                pid = fork do
                    run_for_host(host)
                end
                @children[pid] = host
            else
                # Else, see if we can reap a process.
                begin
                    pid = Process.wait

                    if host = @children[pid]
                        # Remove our host from the list of children, so the parallelization
                        # continues working.
                        @children.delete(pid)
                        if $?.exitstatus != 0
                            failures << host
                        end
                        print "%s finished with exit code %s\n" % [host, $?.exitstatus]
                    else
                        $stderr.puts "Could not find host for PID %s with status %s" % [pid, $?.exitstatus]
                    end
                rescue Errno::ECHILD
                    # There are no children left, so just exit unless there are still
                    # children left to do.
                    next unless todo.empty?

                    if failures.empty?
                        puts "Finished"
                        exit(0)
                    else
                        puts "Failed: %s" % failures.join(", ")
                        exit(3)
                    end
                end
            end
        end
    end

    def run_for_host(host)
        if options[:ping]
            out = %x{ping -c 1 #{host}}
            unless $? == 0
                $stderr.print "Could not contact %s\n" % host
                next
            end
        end

        require 'puppet/run'
        Puppet::Run.indirection.terminus_class = :rest
        port = Puppet[:puppetport]
        url = ["https://#{host}:#{port}", "production", "run", host].join('/')

        print "Triggering %s\n" % host
        begin
            run_options = {
                :tags => @tags,
                :background => ! options[:foreground],
                :ignoreschedules => options[:ignoreschedules]
            }
            run = Puppet::Run.new( run_options ).save( url )
            puts "Getting status"
            result = run.status
            puts "status is #{result}"
        rescue => detail
            puts detail.backtrace if Puppet[:trace]
            $stderr.puts "Host %s failed: %s\n" % [host, detail]
            exit(2)
        end

        case result
        when "success";
            exit(0)
        when "running"
            $stderr.puts "Host %s is already running" % host
            exit(3)
        else
            $stderr.puts "Host %s returned unknown answer '%s'" % [host, result]
            exit(12)
        end
    end

    def initialize(*args)
        super
        @hosts = []
        @classes = []
        @tags = []
    end

    def preinit
        [:INT, :TERM].each do |signal|
            trap(signal) do
                $stderr.puts "Cancelling"
                exit(1)
            end
        end
        options[:parallel] = 1
        options[:verbose] = true
        options[:fqdn] = true
        options[:ignoreschedules] = false
        options[:foreground] = false
    end

    def setup
        if options[:debug]
            Puppet::Util::Log.level = :debug
        else
            Puppet::Util::Log.level = :info
        end

        # Now parse the config
        Puppet.parse_config

        if Puppet[:node_terminus] == "ldap" and (options[:all] or @classes)
            if options[:all]
                @hosts = Puppet::Node.search("whatever", :fqdn => options[:fqdn]).collect { |node| node.name }
                puts "all: %s" % @hosts.join(", ")
            else
                @hosts = []
                @classes.each do |klass|
                    list = Puppet::Node.search("whatever", :fqdn => options[:fqdn], :class => klass).collect { |node| node.name }
                    puts "%s: %s" % [klass, list.join(", ")]

                    @hosts += list
                end
            end
        elsif ! @classes.empty?
            $stderr.puts "You must be using LDAP to specify host classes"
            exit(24)
        end

        @children = {}

        # If we get a signal, then kill all of our children and get out.
        [:INT, :TERM].each do |signal|
            trap(signal) do
                Puppet.notice "Caught #{signal}; shutting down"
                @children.each do |pid, host|
                    Process.kill("INT", pid)
                end

                waitall

                exit(1)
            end
        end

    end

end