summaryrefslogtreecommitdiffstats
path: root/lib/puppet/application/agent.rb
blob: a754ed399ae73151d2be61c3d339c7421314ef7c (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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
require 'puppet/application'

class Puppet::Application::Agent < Puppet::Application

    should_parse_config
    run_mode :agent

    attr_accessor :explicit_waitforcert, :args, :agent, :daemon, :host

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

        {
            :waitforcert => 120,  # Default to checking for certs every 5 minutes
            :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

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

    option("--centrallogging")
    option("--disable")
    option("--enable")
    option("--debug","-d")
    option("--fqdn FQDN","-f")
    option("--test","-t")
    option("--verbose","-v")

    option("--fingerprint")
    option("--digest DIGEST")

    option("--serve HANDLER", "-s") do |arg|
        if Puppet::Network::Handler.handler(arg)
            options[:serve] << arg.to_sym
        else
            raise "Could not find handler for %s" % arg
        end
    end

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

    option("--onetime", "-o") do |arg|
        Puppet[:onetime] = true
        options[:waitforcert] = 0 unless @explicit_waitforcert
    end

    option("--detailed-exitcodes") do |arg|
        options[:detailed_exitcodes] = true
    end

    option("--logdest DEST", "-l DEST") do |arg|
        begin
            Puppet::Util::Log.newdestination(arg)
            options[:setdest] = true
        rescue => detail
            if Puppet[:debug]
                puts detail.backtrace
            end
            $stderr.puts detail.to_s
        end
    end

    option("--waitforcert WAITFORCERT", "-w") do |arg|
        options[:waitforcert] = arg.to_i
        @explicit_waitforcert = true
    end

    option("--port PORT","-p") do |arg|
        @args[:Port] = arg
    end

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

    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
        Puppet.notice fingerprint
    end

    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
            if Puppet[:trace]
                puts detail.backtrace
            end
            Puppet.err detail.to_s
        end

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

    def main
        Puppet.notice "Starting Puppet client version %s" % [Puppet.version]

        @daemon.start
    end

    # Enable all of the most common test options.
    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
        options[:waitforcert] = 0 unless @explicit_waitforcert
    end

    # Handle the logging settings.
    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

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

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

    def setup_listen
        unless FileTest.exists?(Puppet[:authconfig])
            Puppet.err "Will not start without authorization file %s" %
                Puppet[: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

    def setup
        setup_test if options[:test]

        setup_logs

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

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

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

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

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

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

        # 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

        # 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.
        if Puppet[:daemonize]
            @daemon.daemonize
        end

        @host = Puppet::SSL::Host.new
        unless options[:fingerprint]
            cert = @host.wait_for_cert(options[:waitforcert])
        end

        @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
end