~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to lib/puppet/application/puppetd.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet'
 
2
require 'puppet/application'
 
3
require 'puppet/agent'
 
4
require 'puppet/daemon'
 
5
require 'puppet/configurer'
 
6
require 'puppet/network/client'
 
7
 
 
8
Puppet::Application.new(:puppetd) do
 
9
 
 
10
    should_parse_config
 
11
 
 
12
    attr_accessor :explicit_waitforcert, :args, :agent, :daemon
 
13
 
 
14
    preinit do
 
15
        # Do an initial trap, so that cancels don't get a stack trace.
 
16
        trap(:INT) do
 
17
            $stderr.puts "Cancelling startup"
 
18
            exit(0)
 
19
        end
 
20
 
 
21
        {
 
22
            :waitforcert => 120,  # Default to checking for certs every 5 minutes
 
23
            :onetime => false,
 
24
            :verbose => false,
 
25
            :debug => false,
 
26
            :centrallogs => false,
 
27
            :setdest => false,
 
28
            :enable => false,
 
29
            :disable => false,
 
30
            :client => true,
 
31
            :fqdn => nil,
 
32
            :serve => []
 
33
        }.each do |opt,val|
 
34
            options[opt] = val
 
35
        end
 
36
 
 
37
        @explicit_waitforcert = false
 
38
        @args = {}
 
39
        @daemon = Puppet::Daemon.new
 
40
        @daemon.argv = ARGV.dup
 
41
    end
 
42
 
 
43
    option("--centrallogging")
 
44
    option("--disable")
 
45
    option("--enable")
 
46
    option("--debug","-d")
 
47
    option("--fqdn FQDN","-f")
 
48
    option("--test","-t")
 
49
    option("--verbose","-v")
 
50
 
 
51
    option("--serve HANDLER", "-s") do |arg|
 
52
        if Puppet::Network::Handler.handler(arg)
 
53
            options[:serve] << arg.to_sym
 
54
        else
 
55
            raise "Could not find handler for %s" % arg
 
56
        end
 
57
    end
 
58
 
 
59
    option("--no-client") do |arg|
 
60
        options[:client] = false
 
61
    end
 
62
 
 
63
    option("--onetime", "-o") do |arg|
 
64
        options[:onetime] = true
 
65
        options[:waitforcert] = 0 unless @explicit_waitforcert
 
66
    end
 
67
 
 
68
    option("--logdest DEST", "-l DEST") do |arg|
 
69
        begin
 
70
            Puppet::Util::Log.newdestination(arg)
 
71
            options[:setdest] = true
 
72
        rescue => detail
 
73
            if Puppet[:debug]
 
74
                puts detail.backtrace
 
75
            end
 
76
            $stderr.puts detail.to_s
 
77
        end
 
78
    end
 
79
 
 
80
    option("--waitforcert WAITFORCERT", "-w") do |arg|
 
81
        options[:waitforcert] = arg.to_i
 
82
        @explicit_waitforcert = true
 
83
    end
 
84
 
 
85
    option("--port PORT","-p") do |arg|
 
86
        @args[:Port] = arg
 
87
    end
 
88
 
 
89
    dispatch do
 
90
        return :onetime if options[:onetime]
 
91
        return :main
 
92
    end
 
93
 
 
94
    command(:onetime) do
 
95
        unless options[:client]
 
96
            $stderr.puts "onetime is specified but there is no client"
 
97
            exit(43)
 
98
        end
 
99
 
 
100
        @daemon.set_signal_traps
 
101
 
 
102
        begin
 
103
            @agent.run
 
104
        rescue => detail
 
105
            if Puppet[:trace]
 
106
                puts detail.backtrace
 
107
            end
 
108
            Puppet.err detail.to_s
 
109
        end
 
110
        exit(0)
 
111
    end
 
112
 
 
113
    command(:main) do
 
114
        Puppet.notice "Starting Puppet client version %s" % [Puppet.version]
 
115
 
 
116
        @daemon.start
 
117
    end
 
118
 
 
119
    # Enable all of the most common test options.
 
120
    def setup_test
 
121
        Puppet.settings.handlearg("--ignorecache")
 
122
        Puppet.settings.handlearg("--no-usecacheonfailure")
 
123
        Puppet.settings.handlearg("--no-splay")
 
124
        Puppet.settings.handlearg("--show_diff")
 
125
        Puppet.settings.handlearg("--no-daemonize")
 
126
        options[:verbose] = true
 
127
        options[:onetime] = true
 
128
        options[:waitforcert] = 0
 
129
    end
 
130
 
 
131
    # Handle the logging settings.
 
132
    def setup_logs
 
133
        if options[:debug] or options[:verbose]
 
134
            Puppet::Util::Log.newdestination(:console)
 
135
            if options[:debug]
 
136
                Puppet::Util::Log.level = :debug
 
137
            else
 
138
                Puppet::Util::Log.level = :info
 
139
            end
 
140
        end
 
141
 
 
142
        unless options[:setdest]
 
143
            Puppet::Util::Log.newdestination(:syslog)
 
144
        end
 
145
    end
 
146
 
 
147
    def enable_disable_client(agent)
 
148
        if options[:enable]
 
149
            agent.enable
 
150
        elsif options[:disable]
 
151
            agent.disable
 
152
        end
 
153
        exit(0)
 
154
    end
 
155
 
 
156
    def setup_listen
 
157
        unless FileTest.exists?(Puppet[:authconfig])
 
158
            Puppet.err "Will not start without authorization file %s" %
 
159
                Puppet[:authconfig]
 
160
            exit(14)
 
161
        end
 
162
 
 
163
        handlers = nil
 
164
 
 
165
        if options[:serve].empty?
 
166
            handlers = [:Runner]
 
167
        else
 
168
            handlers = options[:serve]
 
169
        end
 
170
 
 
171
        require 'puppet/network/server'
 
172
        # No REST handlers yet.
 
173
        server = Puppet::Network::Server.new(:xmlrpc_handlers => handlers, :port => Puppet[:puppetport])
 
174
 
 
175
        @daemon.server = server
 
176
    end
 
177
 
 
178
    setup do
 
179
        setup_test if options[:test]
 
180
 
 
181
        setup_logs
 
182
 
 
183
        if Puppet.settings.print_configs?
 
184
            exit(Puppet.settings.print_configs ? 0 : 1)
 
185
        end
 
186
 
 
187
        # If noop is set, then also enable diffs
 
188
        if Puppet[:noop]
 
189
            Puppet[:show_diff] = true
 
190
        end
 
191
 
 
192
        args[:Server] = Puppet[:server]
 
193
        if options[:fqdn]
 
194
            args[:FQDN] = options[:fqdn]
 
195
            Puppet[:certname] = options[:fqdn]
 
196
        end
 
197
 
 
198
        if options[:centrallogs]
 
199
            logdest = args[:Server]
 
200
 
 
201
            if args.include?(:Port)
 
202
                logdest += ":" + args[:Port]
 
203
            end
 
204
            Puppet::Util::Log.newdestination(logdest)
 
205
        end
 
206
 
 
207
        Puppet.settings.use :main, :puppetd, :ssl
 
208
 
 
209
        # We need to specify a ca location for things to work, but
 
210
        # until the REST cert transfers are working, it needs to
 
211
        # be local.
 
212
        Puppet::SSL::Host.ca_location = :remote
 
213
 
 
214
        Puppet::Transaction::Report.terminus_class = :rest
 
215
 
 
216
        Puppet::Resource::Catalog.terminus_class = :rest
 
217
        Puppet::Resource::Catalog.cache_class = :yaml
 
218
 
 
219
        Puppet::Node::Facts.terminus_class = :facter
 
220
 
 
221
        # We need tomake the client either way, we just don't start it
 
222
        # if --no-client is set.
 
223
        @agent = Puppet::Agent.new(Puppet::Configurer)
 
224
 
 
225
        enable_disable_client(@agent) if options[:enable] or options[:disable]
 
226
 
 
227
        @daemon.agent = agent if options[:client]
 
228
 
 
229
        # It'd be nice to daemonize later, but we have to daemonize before the
 
230
        # waitforcert happens.
 
231
        if Puppet[:daemonize]
 
232
            @daemon.daemonize
 
233
        end
 
234
 
 
235
        host = Puppet::SSL::Host.new
 
236
        cert = host.wait_for_cert(options[:waitforcert])
 
237
 
 
238
        @objects = []
 
239
 
 
240
        # This has to go after the certs are dealt with.
 
241
        if Puppet[:listen]
 
242
            unless options[:onetime]
 
243
                setup_listen
 
244
            else
 
245
                Puppet.notice "Ignoring --listen on onetime run"
 
246
            end
 
247
        end
 
248
    end
 
249
end