Class: Puppet::Resource::Catalog::StaticCompiler

Inherits:
Compiler show all
Defined in:
lib/puppet/indirector/catalog/static_compiler.rb

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary

Attributes inherited from Compiler

#code

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods inherited from Compiler

#extract_facts_from_request, #filter, #initialize, #networked?

Methods included from Util

absolute_path?, benchmark, chuser, deterministic_rand, deterministic_rand_int, exit_on_fail, logmethods, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, symbolizehash, thinmark, uri_to_path, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods inherited from Indirector::Terminus

abstract_terminus?, #allow_remote_requests?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, model, #model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type, #validate, #validate_key, #validate_model

Methods included from Util::InstanceLoader

#instance_docs, #instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Resource::Catalog::Compiler

Instance Method Details

#add_children(request, catalog, resource, file) ⇒ Object

Generate children resources for a recursive file and add them to the catalog.

Parameters:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 118

def add_children(request, catalog, resource, file)
  children = get_child_resources(request, catalog, resource, file)
  # get_child_resources() returned early because source is not
  # a directory, but we still need to replace the metadata of the
  # resource, so we do it here before returning.
  if children.nil?
    (request, resource, file)
    return
  end

  remove_existing_resources(children, catalog)

  children.each do |name, res|
    catalog.add_resource res
    catalog.add_edge(resource, res)
  end
end

#find(request) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 38

def find(request)
  return nil unless catalog = super

  raise "Did not get catalog back" unless catalog.is_a?(model)

  catalog.resources.find_all { |res| res.type == "File" }.each do |resource|
    next if resource[:ensure] == 'absent'

    next unless source = resource[:source]
    next unless source =~ /^puppet:/

    file = resource.to_ral

    if file.recurse?
      add_children(request, catalog, resource, file)
    else
      (request, resource, file)
    end
  end

  catalog
end

#find_and_replace_metadata(request, resource, file) ⇒ Object

Take a resource with a fileserver based file source remove the source parameter, and insert the file metadata into the resource.

This method acts to do the fileserver metadata retrieval in advance, while the file source is local and doesn’t require an HTTP request. It retrieves the file metadata for a given file resource, removes the source parameter from the resource, inserts the metadata into the file resource, and uploads the file contents of the source to the file bucket.

Parameters:

  • request (Puppet::Indirector::Request)

    The request for the catalog

  • resource (Puppet::Resource)

    The resource to replace the metadata in

  • file (Puppet::Type::File)

    The file RAL associated with the resource



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 73

def (request, resource, file)
  # We remove URL info from it, so it forces a local copy
  # rather than routing through the network.
  # Weird, but true.
  newsource = file[:source][0].sub("puppet:///", "")
  file[:source][0] = newsource

  raise "Could not get metadata for #{resource[:source]}" unless  = file.parameter(:source).

  (request, resource, )
end

#get_child_resources(request, catalog, resource, file) ⇒ Array<Puppet::Resource>

Given a recursive file resource, recursively generate its children resources

Parameters:

Returns:



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
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 144

def get_child_resources(request, catalog, resource, file)
  sourceselect = file[:sourceselect]
  children = {}

  source = resource[:source]

  # This is largely a copy of recurse_remote in File
  total = file[:source].collect do |src|
    next unless result = file.perform_recursion(src)
    return if top = result.find { |r| r.relative_path == "." } and top.ftype != "directory"
    result.each { |data| data.source = "#{src}/#{data.relative_path}" }
    break result if result and ! result.empty? and sourceselect == :first
    result
  end.flatten.compact

  # This only happens if we have sourceselect == :all
  unless sourceselect == :first
    found = []
    total.reject! do |data|
      result = found.include?(data.relative_path)
      found << data.relative_path unless found.include?(data.relative_path)
      result
    end
  end

  parent_meta = nil
  total.each do |meta|
    # This is the top-level parent directory
    if meta.relative_path == "."
      parent_meta = meta
      next
    end
    child = children[meta.relative_path] ||=
      Puppet::Resource.new(:file, File.join(file[:path], meta.relative_path))

    # I think this is safe since it's a URL, not an actual file
    child[:source] = source + "/" + meta.relative_path
    resource.each do |param, value|
      # These should never be passed to our children.
      unless [:parent, :ensure, :recurse, :recurselimit, :target, :alias, :source].include? param
        child[param] = value
      end
    end
    (request, child, meta)
  end
  (request, resource, parent_meta)

  children
end

#remove_existing_resources(children, catalog) ⇒ Object

Remove any recursed file resources already in the catalog

Parameters:



198
199
200
201
202
203
204
205
206
207
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 198

def remove_existing_resources(children, catalog)
  relative_paths = children.keys
  relative_paths.each do |relative_path|
    child = children[relative_path]
    if catalog.resource(child.ref)
      Puppet.debug("Resource #{child.ref} already managed, removing from recursed children")
      children.delete(relative_path)
    end
  end
end

#replace_metadata(request, resource, metadata) ⇒ Object

Rewrite a given file resource with the metadata from a fileserver based file

This performs the actual metadata rewrite for the given file resource and uploads the content of the source file to the filebucket.

Parameters:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 93

def (request, resource, )
  [:owner, :group].each do |param|
    resource[param] ||= .send(param).to_s
  end
  resource[:mode] ||= .send(:mode).to_s(8)

  resource[:ensure] = .ftype
  if .ftype == "file"
    unless resource[:content]
      resource[:content] = .checksum
      resource[:checksum] = .checksum_type
    end
  end

  store_content(request, resource) if resource[:ensure] == "file"
  old_source = resource.delete(:source)
  Puppet.info "Metadata for #{resource} in catalog for '#{request.key}' added from '#{old_source}'"
end

#store_content(request, resource) ⇒ Object

Retrieve the source of a file resource using a fileserver based source and upload it to the filebucket.

Parameters:



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 214

def store_content(request, resource)
  @summer ||= Puppet::Util::Checksums

  type = @summer.sumtype(resource[:content])
  sum = @summer.sumdata(resource[:content])

  if Puppet::FileBucket::File.indirection.find("#{type}/#{sum}")
    Puppet.info "Content for '#{resource[:source]}' already exists"
  else
    Puppet.info "Storing content for source '#{resource[:source]}'"
    content = Puppet::FileServing::Content.indirection.find(resource[:source], {:environment => request.environment})
    file = Puppet::FileBucket::File.new(content.content)
    Puppet::FileBucket::File.indirection.save(file)
  end
end