summaryrefslogtreecommitdiffstats
path: root/lib/puppet/dsl.rb
blob: 378078ebfc41b91ff1c11f556491edfb40bd79d5 (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
# Just quick mess-around to see what a DSL would look like.
# 
# This is what the executable could look like:
##!/usr/bin/ruby
#
#require 'puppet'
#
#require 'puppet/dsl'
#
#Puppet::DSL.import(ARGV[0])
#
#bucket = Puppet::TransBucket.new
#bucket.type = "top"
#bucket.keyword = "class"
#
#Puppet::DSL.find_all do |name, sub|
#    sub.included
#end.each do |name, sub|
#    bucket.push sub.export
#end
#
#puts bucket.to_manifest
#
# And here's what an example config could look like:
#

##!/usr/bin/ruby
#
#class Base
#    file "/etc/passwd",
#        :owner => "root",
#        :group => "root",
#        :mode => 0644,
#        :source => "puppet://puppet/..."
#
#
#
#end
#
#class BSD < Base
#    file "/etc/passwd",
#        :group => "wheel"
#end
#
#include :bsd
#

class Puppet::DSL
    @@subs = {}
    @name = :DSLClass
    class << self
        include Enumerable
        attr_accessor :included, :name, :objects

        def each
            @@subs.each do |name, sub|
                yield name, sub
            end
        end

        def export
            bucket = nil
            if superclass() != Puppet::DSL
                bucket = superclass.export
            else
                bucket = Puppet::TransBucket.new
                bucket.keyword = "class"
                bucket.type = self.name
            end

            @objects.each do |type, ary|
                ary.each do |name, obj|
                    if pobj = bucket.find { |sobj| obj.name == sobj.name &&  obj.type == sobj.type }
                        obj.each do |param, value|
                            pobj[param] = value
                        end
                    else
                        bucket.push obj
                    end
                end
            end

            return bucket
        end

        def include(name)
            if ary = @@subs.find { |n, s| n == name }
                ary[1].included = true
            else
                raise "Could not find class %s" % name
            end
        end

        def inherited(sub)
            name = sub.to_s.downcase.gsub(/.+::/, '').intern
            @@subs[name] = sub
            sub.name = name
            sub.initvars

            sub
        end

        def initvars
            #if superclass() == Puppet::DSL
                @objects = {}
            #else
            #    @objects = superclass.objects
            #end
        end


        def import(file)
            text = File.read(file)
            # If they don't specify a parent class, then specify one
            # for them.
            text.gsub!(/^class \S+\s*$/) do |match|
                "#{match} < Puppet::DSL"
            end
            eval(text, binding)
        end

        def method_missing(method, *args)
            if klass = Puppet::Type.type(method)
                method = method.intern if method.is_a? String
                @objects[method] ||= {}

                names = args.shift
                hash = args.shift
                names = [names] unless names.is_a? Array
                names.each do |name|
                    unless obj = @objects[method][name]
                        obj = Puppet::TransObject.new(name, method)
                        @objects[method][name] = obj
                    end

                    hash.each do |param, value|
                        if obj[param]
                            raise "Cannot override %s in %s[%s]" %
                                [param, method, name]
                        else
                            obj[param] = value
                        end
                    end
                end
            else
                raise "No type %s" % method
            end
        end
    end
end

# $Id$