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

Inherits:
Indirector::Code 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 included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

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

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, deterministic_rand, execfail, execpipe, execute, exit_on_fail, logmethods, memory, path_to_uri, pretty_backtrace, proxy, 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 included from Util::Docs

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

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Instance Method Details

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

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

Parameters:



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

def add_children(host, catalog, resource, file)
  file = resource.to_ral

  children = get_child_resources(host, catalog, resource, file)

  remove_existing_resources(children, catalog)

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

#compilerObject



38
39
40
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 38

def compiler
  @compiler ||= indirection.terminus(:compiler)
end

#find(request) ⇒ Object



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

def find(request)
  return nil unless catalog = compiler.find(request)

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

  catalog.resources.find_all { |res| res.type == "File" }.each do |resource|
    next unless source = resource[:source]
    next unless source =~ /^puppet:/

    file = resource.to_ral

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

  catalog
end

#find_and_replace_metadata(host, 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:

  • host (String)

    The host name of the node requesting this catalog

  • resource (Puppet::Resource)

    The resource to replace the metadata in

  • file (Puppet::Type::File)

    The file RAL associated with the resource



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

def (host, 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).

  (host, resource, )
end

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

Given a recursive file resource, recursively generate its children resources

Parameters:

Returns:



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

def get_child_resources(host, 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 |source|
    next unless result = file.perform_recursion(source)
    return if top = result.find { |r| r.relative_path == "." } and top.ftype != "directory"
    result.each { |data| data.source = "#{source}/#{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

  total.each do |meta|
    # This is the top-level parent directory
    if meta.relative_path == "."
      (host, resource, meta)
      next
    end
    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
    children[meta.relative_path][: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
        children[meta.relative_path][param] = value
      end
    end
    (host, children[meta.relative_path], meta)
  end

  children
end

#remove_existing_resources(children, catalog) ⇒ Object

Remove any file resources in the catalog that will be duplicated by the given file resources.

Parameters:



192
193
194
195
196
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 192

def remove_existing_resources(children, catalog)
  existing_names = catalog.resources.collect { |r| r.to_s }
  both = (existing_names & children.keys).inject({}) { |hash, name| hash[name] = true; hash }
  both.each { |name| children.delete(name) }
end

#replace_metadata(host, 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:



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

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

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

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

#store_content(resource) ⇒ Object

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

Parameters:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/puppet/indirector/catalog/static_compiler.rb', line 202

def store_content(resource)
  @summer ||= Object.new
  @summer.extend(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])
    file = Puppet::FileBucket::File.new(content.content)
    Puppet::FileBucket::File.indirection.save(file)
  end
end