Class: Puppet::TransBucket

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/vendor/puppet/transportable.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children = []) ⇒ TransBucket

Returns a new instance of TransBucket.



140
141
142
# File 'lib/vendor/puppet/transportable.rb', line 140

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

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def catalog
  @catalog
end

#classesObject

Returns the value of attribute classes.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def classes
  @classes
end

#fileObject

Returns the value of attribute file.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def file
  @file
end

#keywordObject

Returns the value of attribute keyword.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def keyword
  @keyword
end

#lineObject

Returns the value of attribute line.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def line
  @line
end

#nameObject

Returns the value of attribute name.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def name
  @name
end

#topObject

Returns the value of attribute top.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def top
  @top
end

#typeObject

Returns the value of attribute type.



103
104
105
# File 'lib/vendor/puppet/transportable.rb', line 103

def type
  @type
end

Instance Method Details

#delve(&block) ⇒ Object

Recursively yield everything.



114
115
116
117
118
119
120
121
122
123
# File 'lib/vendor/puppet/transportable.rb', line 114

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

#eachObject



125
126
127
# File 'lib/vendor/puppet/transportable.rb', line 125

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

#flattenObject

Turn our heirarchy into a flat list



130
131
132
133
134
135
136
137
138
# File 'lib/vendor/puppet/transportable.rb', line 130

def flatten
  @children.collect do |obj|
    if obj.is_a? Puppet::TransBucket
      obj.flatten
    else
      obj
    end
  end.flatten
end

#param(param, value) ⇒ Object



241
242
243
244
# File 'lib/vendor/puppet/transportable.rb', line 241

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

#push(*args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/vendor/puppet/transportable.rb', line 144

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

#to_catalog(clear_on_failure = true) ⇒ Object

Create a resource graph from our structure.



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
# File 'lib/vendor/puppet/transportable.rb', line 180

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

#to_manifestObject

Convert to a parseable manifest



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/vendor/puppet/transportable.rb', line 158

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

#to_ralObject



231
232
233
# File 'lib/vendor/puppet/transportable.rb', line 231

def to_ral
  to_resource.to_ral
end

#to_refObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/vendor/puppet/transportable.rb', line 216

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

#to_resourceObject

Create a normalized resource from our TransObject.



236
237
238
239
# File 'lib/vendor/puppet/transportable.rb', line 236

def to_resource
  params = defined?(@parameters) ? @parameters.dup : {}
  Puppet::Resource.new(type, name, :parameters => params)
end

#to_yaml_propertiesObject



175
176
177
# File 'lib/vendor/puppet/transportable.rb', line 175

def to_yaml_properties
  instance_variables
end