Class: Puppet::Application::Kick

Inherits:
Puppet::Application show all
Defined in:
lib/vendor/puppet/application/kick.rb

Constant Summary

Constants inherited from Puppet::Application

DOCPATTERN

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Instance Attribute Summary collapse

Attributes inherited from Puppet::Application

#command_line, #options

Instance Method Summary collapse

Methods inherited from Puppet::Application

[], banner, clear!, clear?, #configure_indirector_routes, controlled_run, exit, find, #handlearg, interrupted?, #name, option, option_parser_commands, #parse_options, restart!, restart_requested?, #run, run_mode, #set_run_mode, #setup_logs, should_not_parse_config, should_parse_config, should_parse_config?, #should_parse_config?, stop!, stop_requested?

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Constructor Details

#initialize(*args) ⇒ Kick

Returns a new instance of Kick.



282
283
284
285
286
287
# File 'lib/vendor/puppet/application/kick.rb', line 282

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

Instance Attribute Details

#classesObject

Returns the value of attribute classes.



7
8
9
# File 'lib/vendor/puppet/application/kick.rb', line 7

def classes
  @classes
end

#hostsObject

Returns the value of attribute hosts.



7
8
9
# File 'lib/vendor/puppet/application/kick.rb', line 7

def hosts
  @hosts
end

#tagsObject

Returns the value of attribute tags.



7
8
9
# File 'lib/vendor/puppet/application/kick.rb', line 7

def tags
  @tags
end

Instance Method Details

#helpObject



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
# File 'lib/vendor/puppet/application/kick.rb', line 40

def help
  <<-'HELP'

puppet-kick(8) -- Remotely control puppet agent
========

SYNOPSIS
--------
Trigger a puppet agent run on a set of hosts.


USAGE
-----
puppet kick [-a|--all] [-c|--class <class>] [-d|--debug] [-f|--foreground]
[-h|--help] [--host <host>] [--no-fqdn] [--ignoreschedules]
[-t|--tag <tag>] [--test] [-p|--ping] <host> [<host> [...]]


DESCRIPTION
-----------
This script can be used to connect to a set of machines running 'puppet
agent' and trigger them to run their configurations. The most common
usage would be to specify a class of hosts and a set of tags, and
'puppet kick' would look up in LDAP all of the hosts matching that
class, then connect to each host and trigger a run of all of the objects
with the specified tags.

If you are not storing your host configurations in LDAP, you can specify
hosts manually.

You will most likely have to run 'puppet kick' as root to get access to
the SSL certificates.

'puppet kick' reads 'puppet master''s configuration file, so that it can
copy things like LDAP settings.


USAGE NOTES
-----------
Puppet kick needs the puppet agent on the target machine to be running as a
daemon, be configured to listen for incoming network connections, and have an
appropriate security configuration.

The specific changes required are:

* Set `listen = true` in the agent's `puppet.conf` file (or `--listen` on the
command line)
* Configure the node's firewall to allow incoming connections on port 8139
* Insert the following stanza at the top of the node's `auth.conf` file:

      # Allow puppet kick access
      path    /run
      method  save
      auth    any
      allow   workstation.example.com

This example would allow the machine `workstation.example.com` to trigger a
Puppet run; adjust the "allow" directive to suit your site. You may also use
`allow *` to allow anyone to trigger a Puppet run, but that makes it possible
to interfere with your site by triggering excessive Puppet runs.

See `http://docs.puppetlabs.com/guides/rest_auth_conf.html` for more details
about security settings.

OPTIONS
-------
Note that any configuration parameter that's valid in the configuration
file is also a valid long argument. For example, 'ssldir' is a valid
configuration parameter, so you can specify '--ssldir <directory>' as an
argument.

See the configuration file documentation at
http://docs.puppetlabs.com/references/latest/configuration.html for
the full list of acceptable parameters. A commented list of all
configuration options can also be generated by running puppet master
with '--genconfig'.

* --all:
Connect to all available hosts. Requires LDAP support at this point.

* --class:
Specify a class of machines to which to connect. This only works if
you have LDAP configured, at the moment.

* --debug:
Enable full debugging.

* --foreground:
Run each configuration in the foreground; that is, when connecting to
a host, do not return until the host has finished its run. The default
is false.

* --help:
Print this help message

* --host:
A specific host to which to connect. This flag can be specified more
than once.

* --ignoreschedules:
Whether the client should ignore schedules when running its
configuration. This can be used to force the client to perform work it
would not normally perform so soon. The default is false.

* --parallel:
How parallel to make the connections. Parallelization is provided by
forking for each client to which to connect. The default is 1, meaning
serial execution.

* --tag:
Specify a tag for selecting the objects to apply. Does not work with
the --test option.

* --test:
Print the hosts you would connect to but do not actually connect. This
option requires LDAP support at this point.

* --ping:
Do a ICMP echo against the target host. Skip hosts that don't respond
to ping.


EXAMPLE
-------
  $ sudo puppet kick -p 10 -t remotefile -t webserver host1 host2


AUTHOR
------
Luke Kanies


COPYRIGHT
---------
Copyright (c) 2011 Puppet Labs, LLC Licensed under the Apache 2.0 License

  HELP
end

#mainObject



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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/vendor/puppet/application/kick.rb', line 189

def main
  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 = safe_posix_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)
          failures << host if $CHILD_STATUS.exitstatus != 0
          print "#{host} finished with exit code #{$CHILD_STATUS.exitstatus}\n"
        else
          $stderr.puts "Could not find host for PID #{pid} with status #{$CHILD_STATUS.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: #{failures.join(", ")}"
          exit(3)
        end
      end
    end
  end
end

#preinitObject



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/vendor/puppet/application/kick.rb', line 289

def preinit
  [:INT, :TERM].each do |signal|
    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

#run_commandObject



179
180
181
182
# File 'lib/vendor/puppet/application/kick.rb', line 179

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

#run_for_host(host) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/vendor/puppet/application/kick.rb', line 239

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

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

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

  case result
  when "success";
    exit(0)
  when "running"
    $stderr.puts "Host #{host} is already running"
    exit(3)
  else
    $stderr.puts "Host #{host} returned unknown answer '#{result}'"
    exit(12)
  end
end

#setupObject

Raises:



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/vendor/puppet/application/kick.rb', line 303

def setup
  raise Puppet::Error.new("Puppet kick is not supported on Microsoft Windows") if Puppet.features.microsoft_windows?

  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.indirection.search("whatever", :fqdn => options[:fqdn]).collect { |node| node.name }
      puts "all: #{@hosts.join(", ")}"
    else
      @hosts = []
      @classes.each do |klass|
        list = Puppet::Node.indirection.search("whatever", :fqdn => options[:fqdn], :class => klass).collect { |node| node.name }
        puts "#{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|
    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

#testObject



184
185
186
187
# File 'lib/vendor/puppet/application/kick.rb', line 184

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