Class: Puppet::Application::Agent

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

Constant Summary

Constants inherited from Puppet::Application

DOCPATTERN

Instance Attribute Summary collapse

Attributes inherited from Puppet::Application

#command_line, #options

Instance Method Summary collapse

Methods inherited from Puppet::Application

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

Methods included from Util

activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, which, withumask

Methods included from Util::POSIX

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

Constructor Details

This class inherits a constructor from Puppet::Application

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



8
9
10
# File 'lib/puppet/application/agent.rb', line 8

def agent
  @agent
end

#argsObject

Returns the value of attribute args.



8
9
10
# File 'lib/puppet/application/agent.rb', line 8

def args
  @args
end

#daemonObject

Returns the value of attribute daemon.



8
9
10
# File 'lib/puppet/application/agent.rb', line 8

def daemon
  @daemon
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/puppet/application/agent.rb', line 8

def host
  @host
end

Instance Method Details

#enable_disable_client(agent) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/puppet/application/agent.rb', line 161

def enable_disable_client(agent)
  if options[:enable]
    agent.enable
  elsif options[:disable]
    agent.disable
  end
  exit(0)
end

#fingerprintObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/puppet/application/agent.rb', line 92

def fingerprint
  unless cert = host.certificate || host.certificate_request
    $stderr.puts "Fingerprint asked but no certificate nor certificate request have yet been issued"
    exit(1)
    return
  end
  unless fingerprint = cert.fingerprint(options[:digest])
    raise ArgumentError, "Could not get fingerprint for digest '#{options[:digest]}'"
  end
  puts fingerprint
end

#mainObject



129
130
131
132
133
# File 'lib/puppet/application/agent.rb', line 129

def main
  Puppet.notice "Starting Puppet client version #{Puppet.version}"

  @daemon.start
end

#onetimeObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/puppet/application/agent.rb', line 104

def onetime
  unless options[:client]
    $stderr.puts "onetime is specified but there is no client"
    exit(43)
    return
  end

  @daemon.set_signal_traps

  begin
    report = @agent.run
  rescue => detail
    puts detail.backtrace if Puppet[:trace]
    Puppet.err detail.to_s
  end

  if not report
    exit(1)
  elsif options[:detailed_exitcodes] then
    exit(report.exit_status)
  else
    exit(0)
  end
end

#preinitObject



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
# File 'lib/puppet/application/agent.rb', line 10

def preinit
  # Do an initial trap, so that cancels don't get a stack trace.
  Signal.trap(:INT) do
    $stderr.puts "Cancelling startup"
    exit(0)
  end

  {
    :waitforcert => nil,
    :detailed_exitcodes => false,
    :verbose => false,
    :debug => false,
    :centrallogs => false,
    :setdest => false,
    :enable => false,
    :disable => false,
    :client => true,
    :fqdn => nil,
    :serve => [],
    :digest => :MD5,
    :fingerprint => false,
  }.each do |opt,val|
    options[opt] = val
  end

  @args = {}
  require 'puppet/daemon'
  @daemon = Puppet::Daemon.new
  @daemon.argv = ARGV.dup
end

#run_commandObject



86
87
88
89
90
# File 'lib/puppet/application/agent.rb', line 86

def run_command
  return fingerprint if options[:fingerprint]
  return onetime if Puppet[:onetime]
  main
end

#setupObject



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
238
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
# File 'lib/puppet/application/agent.rb', line 197

def setup
  setup_test if options[:test]

  setup_logs

  exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?

  # If noop is set, then also enable diffs
  Puppet[:show_diff] = true if Puppet[:noop]

  args[:Server] = Puppet[:server]
  if options[:fqdn]
    args[:FQDN] = options[:fqdn]
    Puppet[:certname] = options[:fqdn]
  end

  if options[:centrallogs]
    logdest = args[:Server]

    logdest += ":" + args[:Port] if args.include?(:Port)
    Puppet::Util::Log.newdestination(logdest)
  end

  Puppet.settings.use :main, :agent, :ssl

  # Always ignoreimport for agent. It really shouldn't even try to import,
  # but this is just a temporary band-aid.
  Puppet[:ignoreimport] = true

  # We need to specify a ca location for all of the SSL-related i
  # indirected classes to work; in fingerprint mode we just need
  # access to the local files and we don't need a ca.
  Puppet::SSL::Host.ca_location = options[:fingerprint] ? :none : :remote

  Puppet::Transaction::Report.terminus_class = :rest
  # we want the last report to be persisted locally
  Puppet::Transaction::Report.cache_class = :yaml

  # Override the default; puppetd needs this, usually.
  # You can still override this on the command-line with, e.g., :compiler.
  Puppet[:catalog_terminus] = :rest

  # Override the default.
  Puppet[:facts_terminus] = :facter

  Puppet::Resource::Catalog.cache_class = :yaml


  # We need tomake the client either way, we just don't start it
  # if --no-client is set.
  require 'puppet/agent'
  require 'puppet/configurer'
  @agent = Puppet::Agent.new(Puppet::Configurer)

  enable_disable_client(@agent) if options[:enable] or options[:disable]

  @daemon.agent = agent if options[:client]

  # It'd be nice to daemonize later, but we have to daemonize before the
  # waitforcert happens.
  @daemon.daemonize if Puppet[:daemonize]

  setup_host

  @objects = []

  # This has to go after the certs are dealt with.
  if Puppet[:listen]
    unless Puppet[:onetime]
      setup_listen
    else
      Puppet.notice "Ignoring --listen on onetime run"
    end
  end
end

#setup_hostObject



191
192
193
194
195
# File 'lib/puppet/application/agent.rb', line 191

def setup_host
  @host = Puppet::SSL::Host.new
  waitforcert = options[:waitforcert] || (Puppet[:onetime] ? 0 : 120)
  cert = @host.wait_for_cert(waitforcert) unless options[:fingerprint]
end

#setup_listenObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/puppet/application/agent.rb', line 170

def setup_listen
  unless FileTest.exists?(Puppet[:rest_authconfig])
    Puppet.err "Will not start without authorization file #{Puppet[:rest_authconfig]}"
    exit(14)
  end

  handlers = nil

  if options[:serve].empty?
    handlers = [:Runner]
  else
    handlers = options[:serve]
  end

  require 'puppet/network/server'
  # No REST handlers yet.
  server = Puppet::Network::Server.new(:xmlrpc_handlers => handlers, :port => Puppet[:puppetport])

  @daemon.server = server
end

#setup_logsObject

Handle the logging settings.



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/puppet/application/agent.rb', line 148

def setup_logs
  if options[:debug] or options[:verbose]
    Puppet::Util::Log.newdestination(:console)
    if options[:debug]
      Puppet::Util::Log.level = :debug
    else
      Puppet::Util::Log.level = :info
    end
  end

  Puppet::Util::Log.newdestination(:syslog) unless options[:setdest]
end

#setup_testObject

Enable all of the most common test options.



136
137
138
139
140
141
142
143
144
145
# File 'lib/puppet/application/agent.rb', line 136

def setup_test
  Puppet.settings.handlearg("--ignorecache")
  Puppet.settings.handlearg("--no-usecacheonfailure")
  Puppet.settings.handlearg("--no-splay")
  Puppet.settings.handlearg("--show_diff")
  Puppet.settings.handlearg("--no-daemonize")
  options[:verbose] = true
  Puppet[:onetime] = true
  options[:detailed_exitcodes] = true
end