diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-04-08 00:57:17 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-04-08 00:57:17 -0700 |
commit | 95eb1375f0647accd1b1a1569a7d0e3acc4a61e4 (patch) | |
tree | a58d6fddde4d7818dc33511ff8275c7b9f69f1c6 /lib/chef/node.rb | |
parent | 434f25ba07b5c0c50baa1e15b14a945bba3c3c3b (diff) | |
download | chef-95eb1375f0647accd1b1a1569a7d0e3acc4a61e4.tar.gz |
Adding chef-solo command, config examples, Chef::Log class, Chef::Log::Formatter, Chef::Compile, and all the tests
Diffstat (limited to 'lib/chef/node.rb')
-rw-r--r-- | lib/chef/node.rb | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/lib/chef/node.rb b/lib/chef/node.rb index d5896fbd95..7cb42effd0 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -1,7 +1,6 @@ # # Chef::Node # -# # Author:: Adam Jacob (<adam@hjksolutions.com>) # Copyright:: Copyright (c) 2008 HJK Solutions, LLC # License:: GNU General Public License version 2 or later @@ -24,6 +23,9 @@ require File.join(File.dirname(__FILE__), "mixin", "check_helper") require File.join(File.dirname(__FILE__), "mixin", "from_file") +require 'rubygems' +require 'json' + class Chef class Node @@ -39,6 +41,42 @@ class Chef @recipe_list = Array.new end + # Find a Chef::Node by fqdn. Will search first for Chef::Config["node_path"]/fqdn.rb, then + # hostname.rb, then default.rb. + # + # Returns a new Chef::Node object. + # + # Raises an ArgumentError if it cannot find the node. + def self.find(fqdn) + node_file = nil + host_parts = fqdn.split(".") + hostname = host_parts[0] + + if File.exists?(File.join(Chef::Config[:node_path], "#{fqdn}.rb")) + node_file = File.join(Chef::Config[:node_path], "#{fqdn}.rb") + elsif File.exists?(File.join(Chef::Config[:node_path], "#{hostname}.rb")) + node_file = File.join(Chef::Config[:node_path], "#{hostname}.rb") + elsif File.exists?(File.join(Chef::Config[:node_path], "default.rb")) + node_file = File.join(Chef::Config[:node_path], "default.rb") + else + raise ArgumentError, "Cannot find a node matching #{fqdn}, not even with default.rb!" + end + chef_node = Chef::Node.new() + chef_node.from_file(node_file) + chef_node + end + + # Returns an array of nodes available, based on the list of files present. + def self.list + results = Array.new + Dir[File.join(Chef::Config[:node_path], "*.rb")].sort.each do |file| + mr = file.match(/^.+\/(.+)\.rb$/) + node_name = mr[1] + results << node_name + end + results + end + # Set the name of this Node, or return the current name. def name(arg=nil) set_if_args(@name, arg) do |a| @@ -62,6 +100,11 @@ class Chef end end + # Set an attribute of this node + def []=(attrib, value) + @attribute[attrib] = value + end + # Iterates over each attribute, passing the attribute and value to the block. def each_attribute(&block) @attribute.each do |k,v| @@ -108,5 +151,22 @@ class Chef end end + # Serialize this Node as json + def to_json() + result_object = { + "name" => @name, + "type" => "Chef::Node", + "attributes" => Hash.new, + "recipes" => Array.new + } + each_attribute do |a,v| + result_object["attributes"][a] = v + end + recipes.each do |r| + result_object["recipes"] << r + end + result_object.to_json + end + end end
\ No newline at end of file |