summaryrefslogtreecommitdiffstats
path: root/lib/puppet/server/master.rb
blob: 5f3700978c1eb0dda5849eb3cfca34e5f384e23c (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
require 'openssl'
require 'puppet'
require 'puppet/parser/interpreter'
require 'puppet/sslcertificates'
require 'xmlrpc/server'

module Puppet
class Server
    class MasterError < Puppet::Error; end
    class Master < Handler
        attr_accessor :ast, :local
        attr_reader :ca

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

        def initialize(hash = {})

            # FIXME this should all be s/:File/:Manifest/g or something
            # build our AST
            @file = hash[:File] || Puppet[:manifest]
            hash.delete(:File)

            @filestamp = nil
            @filetimeout = hash[:FileTimeout] || 60
            parsefile

            if hash[:Local]
                @local = hash[:Local]
            else
                @local = false
            end

            if hash.include?(:CA) and hash[:CA]
                @ca = Puppet::SSLCertificates::CA.new()
            else
                @ca = nil
            end
        end

        def getconfig(facts, client = nil, clientip = nil)
            parsefile
            if client
                #Puppet.warning request.inspect
            end
            if @local
                # we don't need to do anything, since we should already
                # have raw objects
                Puppet.debug "Our client is local"
            else
                Puppet.debug "Our client is remote"

                # XXX this should definitely be done in the protocol, somehow
                begin
                    facts = Marshal::load(CGI.unescape(facts))
                rescue => detail
                    puts "AAAAA"
                    puts detail
                    exit
                end
            end

            Puppet.debug("Creating interpreter")

            begin
                interpreter = Puppet::Parser::Interpreter.new(
                    :ast => @ast,
                    :facts => facts
                )
            rescue => detail
                return detail.to_s
            end

            Puppet.debug("Running interpreter")
            begin
                retobjects = interpreter.run()
            rescue => detail
                Puppet.err detail.to_s
                return ""
            end

            if @local
                return retobjects
            else
                return CGI.escape(Marshal::dump(retobjects))
            end
        end

        private

        def parsefile
            if @filestamp and FileTest.exists?(@file)
                if @filetimeout and @filestatted
                    if Time.now - @filestatted > @filetimeout
                        tmp = File.stat(@file).ctime

                        @filestatted = Time.now
                        if tmp == @filestamp
                            return
                        else
                            Puppet.notice "Reloading file"
                        end
                    else
                        return
                    end
                end
            end

            unless FileTest.exists?(@file)
                if @ast
                    Puppet.warning "Manifest %s has disappeared" % @file
                    return
                else
                    raise Puppet::Error, "Manifest %s must exist" % @file
                end
            end
            # should i be creating a new parser each time...?
            @parser = Puppet::Parser::Parser.new()
            @parser.file = @file
            @ast = @parser.parse

            @filestamp = File.stat(@file).ctime
        end
    end
end
end