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

Inherits:
Indirector::Code show all
Defined in:
lib/vendor/puppet/indirector/catalog/static_compiler.rb

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

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?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type

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, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

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

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vendor/puppet/indirector/catalog/static_compiler.rb', line 60

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



6
7
8
# File 'lib/vendor/puppet/indirector/catalog/static_compiler.rb', line 6

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

#find(request) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vendor/puppet/indirector/catalog/static_compiler.rb', line 10

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



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vendor/puppet/indirector/catalog/static_compiler.rb', line 30

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) ⇒ Object



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

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

  # 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
    (host, children[meta.relative_path], meta)
  end

  children
end

#remove_existing_resources(children, catalog) ⇒ Object



114
115
116
117
118
# File 'lib/vendor/puppet/indirector/catalog/static_compiler.rb', line 114

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



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

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



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

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