summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network/handler/configuration.rb
blob: 2c72d3d2b865e8873c5d3c48bc3248f4a4f58885 (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
require 'openssl'
require 'puppet'
require 'puppet/parser/interpreter'
require 'puppet/sslcertificates'
require 'xmlrpc/server'
require 'yaml'

class Puppet::Network::Handler
    class Configuration < Handler
        desc "Puppet's configuration compilation interface.  Passed a node name
            or other key, retrieves information about the node (using the ``node_source``)
            and returns a compiled configuration."

        include Puppet::Util

        attr_accessor :local

        @interface = XMLRPC::Service::Interface.new("configuration") { |iface|
                iface.add_method("string configuration(string)")
                iface.add_method("string version()")
        }

        # Compile a node's configuration.
        def configuration(key, client = nil, clientip = nil)
            # If we want to use the cert name as our key
            if Puppet[:node_name] == 'cert' and client
                key = client
            end

            # Note that this is reasonable, because either their node source should actually
            # know about the node, or they should be using the ``none`` node source, which
            # will always return data.
            unless node = node_handler.details(key)
                raise Puppet::Error, "Could not find node '%s'" % key
            end

            # Add any external data to the node.
            add_node_data(node)

            configuration = compile(node)

            return translate(configuration)
        end

        def initialize(options = {})
            if options[:Local]
                @local = options[:Local]
            else
                @local = false
            end

            # Just store the options, rather than creating the interpreter
            # immediately.  Mostly, this is so we can create the interpreter
            # on-demand, which is easier for testing.
            @options = options

            set_server_facts
        end

        # Are we running locally, or are our clients networked?
        def local?
            self.local
        end

        # Return the configuration version.
        def version(client = nil, clientip = nil)
            if client and node = node_handler.details(client)
                update_node_check(node)
                return interpreter.configuration_version(node)
            else
                # Just return something that will always result in a recompile, because
                # this is local.
                return (Time.now + 1000).to_i
            end
        end

        private

        # Add any extra data necessary to the node.
        def add_node_data(node)
            # Merge in our server-side facts, so they can be used during compilation.
            node.fact_merge(@server_facts)

            # Add any specified classes to the node's class list.
            if classes = @options[:Classes]
                classes.each do |klass|
                    node.classes << klass
                end
            end
        end

        # Compile the actual configuration.
        def compile(node)
            # Pick the benchmark level.
            if local?
                level = :none
            else
                level = :notice
            end

            # Ask the interpreter to compile the configuration.
            str = "Compiled configuration for %s" % node.name
            if node.environment
                str += " in environment %s" % node.environment
            end
            config = nil
            benchmark(level, "Compiled configuration for %s" % node.name) do
                begin
                    config = interpreter.compile(node)
                rescue Puppet::Error => detail
                    if Puppet[:trace]
                        puts detail.backtrace
                    end
                    unless local?
                        Puppet.err detail.to_s
                    end
                    raise XMLRPC::FaultException.new(
                        1, detail.to_s
                    )
                end
            end

            return config
        end

        # Create our interpreter object.
        def create_interpreter(options)
            args = {}

            # Allow specification of a code snippet or of a file
            if code = options[:Code]
                args[:Code] = code
            elsif options[:Manifest]
                args[:Manifest] = options[:Manifest]
            end

            args[:Local] = local?

            if options.include?(:UseNodes)
                args[:UseNodes] = options[:UseNodes]
            elsif @local
                args[:UseNodes] = false
            end

            # This is only used by the cfengine module, or if --loadclasses was
            # specified in +puppet+.
            if options.include?(:Classes)
                args[:Classes] = options[:Classes]
            end

            return Puppet::Parser::Interpreter.new(args)
        end

        # Create/return our interpreter.
        def interpreter
            unless defined?(@interpreter) and @interpreter
                @interpreter = create_interpreter(@options)
            end
            @interpreter
        end

        # Create a node handler instance for looking up our nodes.
        def node_handler
            unless defined?(@node_handler)
                @node_handler = Puppet::Network::Handler.handler(:node).create
            end
            @node_handler
        end

        # Initialize our server fact hash; we add these to each client, and they
        # won't change while we're running, so it's safe to cache the values.
        def set_server_facts
            @server_facts = {}

            # Add our server version to the fact list
            @server_facts["serverversion"] = Puppet.version.to_s

            # And then add the server name and IP
            {"servername" => "fqdn",
                "serverip" => "ipaddress"
            }.each do |var, fact|
                if value = Facter.value(fact)
                    @server_facts[var] = value
                else
                    Puppet.warning "Could not retrieve fact %s" % fact
                end
            end

            if @server_facts["servername"].nil?
                host = Facter.value(:hostname)
                if domain = Facter.value(:domain)
                    @server_facts["servername"] = [host, domain].join(".")
                else
                    @server_facts["servername"] = host
                end
            end
        end

        # Translate our configuration appropriately for sending back to a client.
        def translate(config)
            if local?
                config
            else
                CGI.escape(config.to_yaml(:UseBlock => true))
            end
        end

        # Mark that the node has checked in. FIXME this needs to be moved into
        # the Node class, or somewhere that's got abstract backends.
        def update_node_check(node)
            if Puppet.features.rails? and Puppet[:storeconfigs]
                Puppet::Rails.connect

                host = Puppet::Rails::Host.find_or_create_by_name(node.name)
                host.last_freshcheck = Time.now
                host.save
            end
        end
    end
end