summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/configuration
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-10-15 19:36:32 -0500
committerLuke Kanies <luke@madstop.com>2007-10-15 19:36:32 -0500
commita815f7888b021a46332c23450795f057533d0093 (patch)
treea2f8081e47750c7782db9c3751ec4091f3f7424f /lib/puppet/indirector/configuration
parent694f98b4d9e7172cec58d407bc5aeae7861e1a06 (diff)
downloadpuppet-a815f7888b021a46332c23450795f057533d0093.tar.gz
puppet-a815f7888b021a46332c23450795f057533d0093.tar.xz
puppet-a815f7888b021a46332c23450795f057533d0093.zip
Reorganizing the file structure for indirection terminus types.
Previously, for example, the configuration terminus that was a subclass of 'code' would have been stored at lib/puppet/indirector/code/configuration and would have had to have been named 'configuration'. Now, the subclass can be named however the author prefers, and it must be stored at lib/puppet/indirector/configuration/<name>.rb, where <name> is the name you've chosen for the terminus type. The name only matters insomuch as it is used to load the file from disk and find the appropriate class when asked. The additional restriction is that the class constant for the terminus type must have its name as the last word, and the indirection must be the second to last word. Thus, in our example, we can choose any class constant that ends with Configuration::Code; given that there's only one Configuration class at this point, it makes the most sense to define the class as Puppet::Node::Configuration::Code. This is somewhat awkward, because of the class's location on disk, but the only other real option is to autogenerate a Puppet::Indirector::Configuration class constant, which is, I think, uglier.
Diffstat (limited to 'lib/puppet/indirector/configuration')
-rw-r--r--lib/puppet/indirector/configuration/compiler.rb174
-rw-r--r--lib/puppet/indirector/configuration/yaml.rb24
2 files changed, 198 insertions, 0 deletions
diff --git a/lib/puppet/indirector/configuration/compiler.rb b/lib/puppet/indirector/configuration/compiler.rb
new file mode 100644
index 000000000..9fc8a7939
--- /dev/null
+++ b/lib/puppet/indirector/configuration/compiler.rb
@@ -0,0 +1,174 @@
+require 'puppet/node'
+require 'puppet/node/configuration'
+require 'puppet/indirector/code'
+require 'puppet/parser/interpreter'
+require 'yaml'
+
+class Puppet::Node::Configuration::Compiler < Puppet::Indirector::Code
+ desc "Puppet's configuration compilation interface. Passed a node name
+ or other key, retrieves information about the node (using the ``node_source``)
+ and returns a compiled configuration."
+
+ include Puppet::Util
+
+ attr_accessor :code
+
+ # Compile a node's configuration.
+ def find(key, client = nil, clientip = nil)
+ if key.is_a?(Puppet::Node)
+ node = key
+ else
+ node = find_node(key)
+ end
+
+ if configuration = compile(node)
+ return configuration.to_transportable
+ else
+ # This shouldn't actually happen; we should either return
+ # a config or raise an exception.
+ return nil
+ end
+ end
+
+ def initialize
+ set_server_facts
+ end
+
+ # Create/return our interpreter.
+ def interpreter
+ unless defined?(@interpreter) and @interpreter
+ @interpreter = create_interpreter
+ end
+ @interpreter
+ end
+
+ # Is our compiler part of a network, or are we just local?
+ def networked?
+ $0 =~ /puppetmasterd/
+ end
+
+ # Return the configuration version. Here we're returning the
+ # latest of the node, fact, or parse date. These are the
+ # three things that go into compiling a client configuration,
+ # so changes in any of them result in changes.
+ # LAK:FIXME Note that this only works when all three sources
+ # use timestamps; once one of them moves to using real versions,
+ # the comparison stops working.
+ def version(key)
+ if node = Puppet::Node.search(key)
+ return [Puppet::Node.version(key).to_f, Puppet::Node::Facts.version(key).to_f, interpreter.configuration_version(node).to_f].sort[-1]
+ else
+ # This is the standard for "got nothing for ya".
+ 0
+ end
+ end
+
+ private
+
+ # Add any extra data necessary to the node.
+ def add_node_data(node)
+ # Merge in our server-side facts, so they can be used during compilation.
+ node.merge(@server_facts)
+ end
+
+ # Compile the actual configuration.
+ def compile(node)
+ # Ask the interpreter to compile the configuration.
+ str = "Compiled configuration for %s" % node.name
+ if node.environment
+ str += " in environment %s" % node.environment
+ end
+ config = nil
+
+ loglevel = networked? ? :notice : :none
+
+ benchmark(loglevel, "Compiled configuration for %s" % node.name) do
+ begin
+ config = interpreter.compile(node)
+ rescue Puppet::Error => detail
+ Puppet.err(detail.to_s) if networked?
+ raise
+ end
+ end
+
+ return config
+ end
+
+ # Create our interpreter object.
+ def create_interpreter
+ return Puppet::Parser::Interpreter.new
+ end
+
+ # Turn our host name into a node object.
+ def find_node(key)
+ # If we want to use the cert name as our key
+ # LAK:FIXME This needs to be figured out somehow, but it requires the routing.
+ #if Puppet[:node_name] == 'cert' and client
+ # key = client
+ #end
+
+ # Note that this is reasonable, because either their node source should actually
+ # know about the node, or they should be using the ``none`` node source, which
+ # will always return data.
+ unless node = Puppet::Node.search(key)
+ raise Puppet::Error, "Could not find node '%s'" % key
+ end
+
+ # Add any external data to the node.
+ add_node_data(node)
+
+ node
+ end
+
+ # Initialize our server fact hash; we add these to each client, and they
+ # won't change while we're running, so it's safe to cache the values.
+ def set_server_facts
+ @server_facts = {}
+
+ # Add our server version to the fact list
+ @server_facts["serverversion"] = Puppet.version.to_s
+
+ # And then add the server name and IP
+ {"servername" => "fqdn",
+ "serverip" => "ipaddress"
+ }.each do |var, fact|
+ if value = Facter.value(fact)
+ @server_facts[var] = value
+ else
+ Puppet.warning "Could not retrieve fact %s" % fact
+ end
+ end
+
+ if @server_facts["servername"].nil?
+ host = Facter.value(:hostname)
+ if domain = Facter.value(:domain)
+ @server_facts["servername"] = [host, domain].join(".")
+ else
+ @server_facts["servername"] = host
+ end
+ end
+ end
+
+ # Translate our configuration appropriately for sending back to a client.
+ # LAK:FIXME This method should probably be part of the protocol, but it
+ # shouldn't be here.
+ def translate(config)
+ unless networked?
+ config
+ else
+ CGI.escape(config.to_yaml(:UseBlock => true))
+ end
+ end
+
+ # Mark that the node has checked in. LAK:FIXME this needs to be moved into
+ # the Node class, or somewhere that's got abstract backends.
+ def update_node_check(node)
+ if Puppet.features.rails? and Puppet[:storeconfigs]
+ Puppet::Rails.connect
+
+ host = Puppet::Rails::Host.find_or_create_by_name(node.name)
+ host.last_freshcheck = Time.now
+ host.save
+ end
+ end
+end
diff --git a/lib/puppet/indirector/configuration/yaml.rb b/lib/puppet/indirector/configuration/yaml.rb
new file mode 100644
index 000000000..1330aaffa
--- /dev/null
+++ b/lib/puppet/indirector/configuration/yaml.rb
@@ -0,0 +1,24 @@
+require 'puppet/node/configuration'
+require 'puppet/indirector/yaml'
+
+class Puppet::Node::Configuration::Yaml < Puppet::Indirector::Yaml
+ desc "Store configurations as flat files, serialized using YAML."
+
+ private
+
+ # Override these, because yaml doesn't want to convert our self-referential
+ # objects. This is hackish, but eh.
+ def from_yaml(text)
+ if config = YAML.load(text)
+ # We can't yaml-dump classes.
+ #config.edgelist_class = Puppet::Relationship
+ return config
+ end
+ end
+
+ def to_yaml(config)
+ # We can't yaml-dump classes.
+ #config.edgelist_class = nil
+ YAML.dump(config)
+ end
+end