Class: Puppet::Configurer

Inherits:
Object show all
Extended by:
Util
Includes:
FactHandler, PluginHandler, Util
Defined in:
lib/vendor/puppet/configurer.rb

Defined Under Namespace

Modules: FactHandler, PluginHandler Classes: Downloader

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 PluginHandler

#download_plugins, #download_plugins?, #load_plugin

Methods included from FactHandler

#download_fact_plugins, #download_fact_plugins?, #facts_for_uploading, #find_facts

Constructor Details

#initializeConfigurer

Just so we can specify that we are “the” instance.



65
66
67
68
69
70
71
# File 'lib/vendor/puppet/configurer.rb', line 65

def initialize
  Puppet.settings.use(:main, :ssl, :agent)

  self.class.instance = self
  @running = false
  @splayed = false
end

Class Attribute Details

.instanceObject

Puppetd should only have one instance running, and we need a way to retrieve it.



27
28
29
# File 'lib/vendor/puppet/configurer.rb', line 27

def instance
  @instance
end

Instance Attribute Details

#compile_timeObject (readonly)

Returns the value of attribute compile_time.



17
18
19
# File 'lib/vendor/puppet/configurer.rb', line 17

def compile_time
  @compile_time
end

Class Method Details

.lockfile_pathObject

How to lock instances of this class.



32
33
34
# File 'lib/vendor/puppet/configurer.rb', line 32

def self.lockfile_path
  Puppet[:puppetdlockfile]
end

.to_sObject

Provide more helpful strings to the logging that the Agent does



20
21
22
# File 'lib/vendor/puppet/configurer.rb', line 20

def self.to_s
  "Puppet configuration client"
end

Instance Method Details

#clearObject



36
37
38
39
# File 'lib/vendor/puppet/configurer.rb', line 36

def clear
  @catalog.clear(true) if @catalog
  @catalog = nil
end

#convert_catalog(result, duration) ⇒ Object

Convert a plain resource catalog into our full host catalog.



100
101
102
103
104
105
106
107
# File 'lib/vendor/puppet/configurer.rb', line 100

def convert_catalog(result, duration)
  catalog = result.to_ral
  catalog.finalize
  catalog.retrieval_duration = duration
  catalog.write_class_file
  catalog.write_resource_file
  catalog
end

#dostorageObject

Initialize and load storage



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vendor/puppet/configurer.rb', line 50

def dostorage
    Puppet::Util::Storage.load
    @compile_time ||= Puppet::Util::Storage.cache(:configuration)[:compile_time]
rescue => detail
    puts detail.backtrace if Puppet[:trace]
    Puppet.err "Corrupt state file #{Puppet[:statefile]}: #{detail}"
    begin
      ::File.unlink(Puppet[:statefile])
      retry
    rescue => detail
      raise Puppet::Error.new("Cannot remove #{Puppet[:statefile]}: #{detail}")
    end
end

#execute_postrun_commandObject



41
42
43
# File 'lib/vendor/puppet/configurer.rb', line 41

def execute_postrun_command
  execute_from_setting(:postrun_command)
end

#execute_prerun_commandObject



45
46
47
# File 'lib/vendor/puppet/configurer.rb', line 45

def execute_prerun_command
  execute_from_setting(:prerun_command)
end

#prepare(options) ⇒ Object

Prepare for catalog retrieval. Downloads everything necessary, etc.



74
75
76
77
78
79
80
# File 'lib/vendor/puppet/configurer.rb', line 74

def prepare(options)
  dostorage

  download_plugins unless options[:skip_plugin_download]

  download_fact_plugins unless options[:skip_plugin_download]
end

#retrieve_and_apply_catalog(options, fact_options) ⇒ Object

Retrieve (optionally) and apply a catalog. If a catalog is passed in the options, then apply that one, otherwise retrieve it.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vendor/puppet/configurer.rb', line 111

def retrieve_and_apply_catalog(options, fact_options)
  unless catalog = (options.delete(:catalog) || retrieve_catalog(fact_options))
    Puppet.err "Could not retrieve catalog; skipping run"
    return
  end

  report = options[:report]
  report.configuration_version = catalog.version
  report.environment = Puppet[:environment]

  benchmark(:notice, "Finished catalog run") do
    catalog.apply(options)
  end

  report.finalize_report
  report
end

#retrieve_catalog(fact_options) ⇒ Object

Get the remote catalog, yo. Returns nil if no catalog can be found.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vendor/puppet/configurer.rb', line 83

def retrieve_catalog(fact_options)
  fact_options ||= {}
  # First try it with no cache, then with the cache.
  unless (Puppet[:use_cached_catalog] and result = retrieve_catalog_from_cache(fact_options)) or result = retrieve_new_catalog(fact_options)
    if ! Puppet[:usecacheonfailure]
      Puppet.warning "Not using cache on failed catalog"
      return nil
    end
    result = retrieve_catalog_from_cache(fact_options)
  end

  return nil unless result

  convert_catalog(result, @duration)
end

#run(options = {}) ⇒ Object

The code that actually runs the catalog. This just passes any options on to the catalog, which accepts :tags and :ignoreschedules.



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

def run(options = {})
  options[:report] ||= Puppet::Transaction::Report.new("apply")
  report = options[:report]

  Puppet::Util::Log.newdestination(report)
  begin
    prepare(options)

    if Puppet::Resource::Catalog.indirection.terminus_class == :rest
      # This is a bit complicated.  We need the serialized and escaped facts,
      # and we need to know which format they're encoded in.  Thus, we
      # get a hash with both of these pieces of information.
      fact_options = facts_for_uploading
    end

    # set report host name now that we have the fact
    report.host = Puppet[:node_name_value]

    begin
      execute_prerun_command or return nil
      retrieve_and_apply_catalog(options, fact_options)
    rescue SystemExit,NoMemoryError
      raise
    rescue => detail
      puts detail.backtrace if Puppet[:trace]
      Puppet.err "Failed to apply catalog: #{detail}"
      return nil
    ensure
      execute_postrun_command or return nil
    end
  ensure
    # Make sure we forget the retained module_directories of any autoload
    # we might have used.
    Thread.current[:env_module_directories] = nil
  end
ensure
  Puppet::Util::Log.close(report)
  send_report(report)
end

#save_last_run_summary(report) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/vendor/puppet/configurer.rb', line 181

def save_last_run_summary(report)
  mode = Puppet.settings.setting(:lastrunfile).mode
  Puppet::Util.replace_file(Puppet[:lastrunfile], mode) do |fh|
    fh.print YAML.dump(report.raw_summary)
  end
rescue => detail
  puts detail.backtrace if Puppet[:trace]
  Puppet.err "Could not save last run local report: #{detail}"
end

#send_report(report) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/vendor/puppet/configurer.rb', line 172

def send_report(report)
  puts report.summary if Puppet[:summarize]
  save_last_run_summary(report)
  Puppet::Transaction::Report.indirection.save(report) if Puppet[:report]
rescue => detail
  puts detail.backtrace if Puppet[:trace]
  Puppet.err "Could not send report: #{detail}"
end