summaryrefslogtreecommitdiffstats
path: root/lib/puppet/transportable.rb
blob: f7b839c14a1c4d05f2577edacf84f1db92627bb4 (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
require 'puppet'
require 'yaml'

module Puppet
  # The transportable objects themselves.  Basically just a hash with some
  # metadata and a few extra methods.  I used to have the object actually
  # be a subclass of Hash, but I could never correctly dump them using
  # YAML.
  class TransObject
    include Enumerable
    attr_accessor :type, :name, :file, :line, :catalog

    attr_writer :tags

    %w{has_key? include? length delete empty? << [] []=}.each { |method|
      define_method(method) do |*args|
        @params.send(method, *args)
      end
    }

    def each
      @params.each { |p,v| yield p, v }
    end

    def initialize(name,type)
      @type = type.to_s.downcase
      @name = name
      @params = {}
      @tags = []
    end

    def longname
      [@type,@name].join('--')
    end

    def ref
      @ref ||= Puppet::Resource.new(@type, @name)
      @ref.to_s
    end

    def tags
      @tags
    end

    # Convert a defined type into a component.
    def to_component
      trans = TransObject.new(ref, :component)
      @params.each { |param,value|
        next unless Puppet::Type::Component.valid_parameter?(param)
        Puppet.debug "Defining #{param} on #{ref}"
        trans[param] = value
      }
      trans.catalog = self.catalog
      Puppet::Type::Component.create(trans)
    end

    def to_hash
      @params.dup
    end

    def to_s
      "#{@type}(#{@name}) => #{super}"
    end

    def to_manifest
      "%s { '%s':\n%s\n}" % [self.type.to_s, self.name,
        @params.collect { |p, v|
          if v.is_a? Array
            "    #{p} => [\'#{v.join("','")}\']"
          else
            "    #{p} => \'#{v}\'"
          end
        }.join(",\n")
        ]
    end

    # Create a normalized resource from our TransObject.
    def to_resource
      result = Puppet::Resource.new(type, name, :parameters => @params.dup)
      result.tag(*tags)

      result
    end

    def to_yaml_properties
      instance_variables.reject { |v| %w{@ref}.include?(v) }
    end

    def to_ref
      ref
    end

    def to_ral
      to_resource.to_ral
    end
  end

  # Just a linear container for objects.  Behaves mostly like an array, except
  # that YAML will correctly dump them even with their instance variables.
  class TransBucket
    include Enumerable

    attr_accessor :name, :type, :file, :line, :classes, :keyword, :top, :catalog

    %w{delete shift include? length empty? << []}.each { |method|
      define_method(method) do |*args|
        #Puppet.warning "Calling #{method} with #{args.inspect}"
        @children.send(method, *args)
        #Puppet.warning @params.inspect
      end
    }

    # Recursively yield everything.
    def delve(&block)
      @children.each do |obj|
        block.call(obj)
        if obj.is_a? self.class
          obj.delve(&block)
        else
          obj
        end
      end
    end

    def each
      @children.each { |c| yield c }
    end

    # Turn our heirarchy into a flat list
    def flatten
      @children.collect do |obj|
        if obj.is_a? Puppet::TransBucket
          obj.flatten
        else
          obj
        end
      end.flatten
    end

    def initialize(children = [])
      @children = children
    end

    def push(*args)
      args.each { |arg|
        case arg
        when Puppet::TransBucket, Puppet::TransObject
          # nada
        else
          raise Puppet::DevError,
            "TransBuckets cannot handle objects of type #{arg.class}"
        end
      }
      @children += args
    end

    # Convert to a parseable manifest
    def to_manifest
      unless self.top
        raise Puppet::DevError, "No keyword; cannot convert to manifest" unless @keyword
      end

      str = "#{@keyword} #{@name} {\n%s\n}"
      str % @children.collect { |child|
        child.to_manifest
      }.collect { |str|
        if self.top
          str
        else
          str.gsub(/^/, "    ") # indent everything once
        end
      }.join("\n\n") # and throw in a blank line
    end

    def to_yaml_properties
      instance_variables
    end

    # Create a resource graph from our structure.
    def to_catalog(clear_on_failure = true)
      catalog = Puppet::Resource::Catalog.new(Facter.value("hostname"))

      # This should really use the 'delve' method, but this
      # whole class is going away relatively soon, hopefully,
      # so it's not worth it.
      delver = proc do |obj|
        obj.catalog = catalog
        unless container = catalog.resource(obj.to_ref)
          container = obj.to_ral
          catalog.add_resource container
        end
        obj.each do |child|
          child.catalog = catalog
          unless resource = catalog.resource(child.to_ref)
            resource = child.to_ral
            catalog.add_resource resource
          end

          catalog.add_edge(container, resource)
          delver.call(child) if child.is_a?(self.class)
        end
      end

      begin
        delver.call(self)
        catalog.finalize
      rescue => detail
        # This is important until we lose the global resource references.
        catalog.clear if (clear_on_failure)
        raise
      end

      catalog
    end

    def to_ref
      unless defined?(@ref)
        if self.type and self.name
          @ref = Puppet::Resource.new(self.type, self.name)
        elsif self.type and ! self.name # This is old-school node types
          @ref = Puppet::Resource.new("node", self.type)
        elsif ! self.type and self.name
          @ref = Puppet::Resource.new("component", self.name)
        else
          @ref = nil
        end
      end
      @ref.to_s if @ref
    end

    def to_ral
      to_resource.to_ral
    end

    # Create a normalized resource from our TransObject.
    def to_resource
      params = defined?(@parameters) ? @parameters.dup : {}
      Puppet::Resource.new(type, name, :parameters => params)
    end

    def param(param,value)
      @parameters ||= {}
      @parameters[param] = value
    end

  end
end