diff options
| author | Luke Kanies <luke@madstop.com> | 2009-01-28 17:11:19 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2009-02-06 18:08:42 -0600 |
| commit | fc14b81f99adc9c9308a26d322adaa59a7b7716d (patch) | |
| tree | dc731383d3195e37ea3658b9cbc7b0c428579d9f /lib/puppet/configurer | |
| parent | e8be6dcad2150769b51bf81e95c57491921e68c1 (diff) | |
| download | puppet-fc14b81f99adc9c9308a26d322adaa59a7b7716d.tar.gz puppet-fc14b81f99adc9c9308a26d322adaa59a7b7716d.tar.xz puppet-fc14b81f99adc9c9308a26d322adaa59a7b7716d.zip | |
Splitting the Agent class into Agent and Configurer
Once I went to add runinterval support to the Agent class,
I realized it's really two classes: One that handles starting,
stopping, running, et al (still called Agent), and one that
handles downloading the catalog, running it, etc. (now
called Configurer).
This commit includes some additional code, but 95% of it is just moving code around.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/configurer')
| -rw-r--r-- | lib/puppet/configurer/downloader.rb | 79 | ||||
| -rw-r--r-- | lib/puppet/configurer/fact_handler.rb | 48 | ||||
| -rw-r--r-- | lib/puppet/configurer/plugin_handler.rb | 25 |
3 files changed, 152 insertions, 0 deletions
diff --git a/lib/puppet/configurer/downloader.rb b/lib/puppet/configurer/downloader.rb new file mode 100644 index 000000000..f1c4c03b1 --- /dev/null +++ b/lib/puppet/configurer/downloader.rb @@ -0,0 +1,79 @@ +require 'puppet/agent' +require 'puppet/resource/catalog' + +class Puppet::Configurer::Downloader + attr_reader :name, :path, :source, :ignore + + # Determine the timeout value to use. + def self.timeout + timeout = Puppet[:configtimeout] + case timeout + when String: + if timeout =~ /^\d+$/ + timeout = Integer(timeout) + else + raise ArgumentError, "Configuration timeout must be an integer" + end + when Integer: # nothing + else + raise ArgumentError, "Configuration timeout must be an integer" + end + + return timeout + end + + # Evaluate our download, returning the list of changed values. + def evaluate + Puppet.info "Retrieving #{name}" + + files = [] + begin + Timeout.timeout(self.class.timeout) do + catalog.apply do |trans| + trans.changed?.find_all do |resource| + yield resource if block_given? + files << resource[:path] + end + end + end + rescue Puppet::Error, Timeout::Error => detail + puts detail.backtrace if Puppet[:debug] + Puppet.err "Could not retrieve #{name}: %s" % detail + end + + return files + end + + def initialize(name, path, source, ignore = nil) + @name, @path, @source, @ignore = name, path, source, ignore + end + + def catalog + catalog = Puppet::Resource::Catalog.new + catalog.add_resource(file) + catalog + end + + def file + args = default_arguments.merge(:path => path, :source => source) + args[:ignore] = ignore if ignore + Puppet::Type.type(:file).create(args) + end + + private + + def default_arguments + { + :path => path, + :recurse => true, + :source => source, + :tag => name, + :owner => Process.uid, + :group => Process.gid, + :purge => true, + :force => true, + :backup => false, + :noop => false + } + end +end diff --git a/lib/puppet/configurer/fact_handler.rb b/lib/puppet/configurer/fact_handler.rb new file mode 100644 index 000000000..9435cb22a --- /dev/null +++ b/lib/puppet/configurer/fact_handler.rb @@ -0,0 +1,48 @@ +require 'puppet/indirector/facts/facter' + +# Break out the code related to facts. This module is +# just included into the agent, but having it here makes it +# easier to test. +module Puppet::Configurer::FactHandler + def download_fact_plugins? + Puppet[:factsync] + end + + def upload_facts + # XXX down = Puppet[:downcasefacts] + + reload_facter() + + # This works because puppetd configures Facts to use 'facter' for + # finding facts and the 'rest' terminus for caching them. Thus, we'll + # compile them and then "cache" them on the server. + Puppet::Node::Facts.find(Puppet[:certname]) + end + + # Retrieve facts from the central server. + def download_fact_plugins + return unless download_fact_plugins? + + Puppet::Configurer::Downloader.new("fact", Puppet[:factsource], Puppet[:factdest], Puppet[:factsignore]).evaluate + end + + # Clear out all of the loaded facts and reload them from disk. + # NOTE: This is clumsy and shouldn't be required for later (1.5.x) versions + # of Facter. + def reload_facter + Facter.clear + + # Reload everything. + if Facter.respond_to? :loadfacts + Facter.loadfacts + elsif Facter.respond_to? :load + Facter.load + else + Puppet.warning "You should upgrade your version of Facter to at least 1.3.8" + end + + # This loads all existing facts and any new ones. We have to remove and + # reload because there's no way to unload specific facts. + Puppet::Node::Facts::Facter.load_fact_plugins() + end +end diff --git a/lib/puppet/configurer/plugin_handler.rb b/lib/puppet/configurer/plugin_handler.rb new file mode 100644 index 000000000..cadf300fd --- /dev/null +++ b/lib/puppet/configurer/plugin_handler.rb @@ -0,0 +1,25 @@ +# Break out the code related to plugins. This module is +# just included into the agent, but having it here makes it +# easier to test. +module Puppet::Configurer::PluginHandler + def download_plugins? + Puppet[:pluginsync] + end + + # Retrieve facts from the central server. + def download_plugins + return nil unless download_plugins? + Puppet::Configurer::Downloader.new("plugin", Puppet[:pluginsource], Puppet[:plugindest], Puppet[:pluginsignore]).evaluate.each { |file| load_plugin(file) } + end + + def load_plugin(file) + return if FileTest.directory?(file) + + begin + Puppet.info "Loading downloaded plugin %s" % file + load file + rescue Exception => detail + Puppet.err "Could not load downloaded file %s: %s" % [file, detail] + end + end +end |
