summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorAdam Jacob <adam@hjksolutions.com>2008-07-28 17:57:49 -0700
committerAdam Jacob <adam@hjksolutions.com>2008-07-28 17:57:49 -0700
commit8d4c3be22c5dbb030e96bcbac12679de7a09b0fe (patch)
tree6fd5282dccb98e63ce8fae5277e7cadf8acc204a /lib/chef
parentd734c714c1e7598ddba40b47c26d10f002e06420 (diff)
downloadchef-8d4c3be22c5dbb030e96bcbac12679de7a09b0fe.tar.gz
Updating search tickets
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/client.rb34
-rw-r--r--lib/chef/compile.rb14
-rw-r--r--lib/chef/config.rb2
-rw-r--r--lib/chef/cookbook.rb4
-rw-r--r--lib/chef/cookbook_loader.rb14
-rw-r--r--lib/chef/platform.rb1
-rw-r--r--lib/chef/provider/template.rb69
-rw-r--r--lib/chef/recipe.rb2
-rw-r--r--lib/chef/resource.rb2
-rw-r--r--lib/chef/resource/template.rb52
10 files changed, 192 insertions, 2 deletions
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index 0ae7ef70e6..681260eee2 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -28,6 +28,7 @@ class Chef
attr_accessor :node, :registration, :safe_name
+ # Creates a new Chef::Client.
def initialize()
@node = nil
@safe_name = nil
@@ -35,14 +36,27 @@ class Chef
@rest = Chef::REST.new(Chef::Config[:registration_url])
end
+ # Do a full run for this Chef::Client. Calls:
+ #
+ # * build_node
+ # * register
+ # * authenticate
+ # * do_attribute_files
+ # * save_node
+ # * converge
+ #
+ # In that order.
def run
build_node
register
authenticate
+ do_attribute_files
save_node
converge
end
+ # Builds a new node object for this client. Starts with querying for the FQDN of the current
+ # host, then merges in the facts from Facter.
def build_node(node_name=nil)
node_name ||= Facter["fqdn"].value ? Facter["fqdn"].value : Facter["hostname"].value
@safe_name = node_name.gsub(/\./, '_')
@@ -63,6 +77,10 @@ class Chef
@node
end
+ # If this node has been registered before, this method will fetch the current registration
+ # data.
+ #
+ # If it has not, we register it by calling create_registration.
def register
@registration = nil
begin
@@ -81,12 +99,15 @@ class Chef
end
end
+ # Generates a random secret, stores it in the Chef::Filestore with the "registration" key,
+ # and posts our nodes registration information to the server.
def create_registration
@secret = random_password(40)
Chef::FileStore.store("registration", @safe_name, { "secret" => @secret })
@rest.post_rest("registrations", { :id => @safe_name, :password => @secret })
end
+ # Authenticates the node via OpenID.
def authenticate
response = @rest.post_rest('openid/consumer/start', {
"openid_identifier" => "#{Chef::Config[:openid_url]}/openid/server/node/#{@safe_name}",
@@ -98,10 +119,22 @@ class Chef
)
end
+ # Gets all the attribute files included in all the cookbooks available on the server,
+ # and executes them.
+ def do_attribute_files
+ af_list = @rest.get_rest('cookbooks/_attribute_files')
+ af_list.each do |af|
+ @node.instance_eval(af["contents"], "#{af['cookbook']}/#{af['name']}", 1)
+ end
+ end
+
+ # Updates the current node configuration on the server.
def save_node
@rest.put_rest("nodes/#{@safe_name}", @node)
end
+ # Compiles the full list of recipes for the server, and passes it to an instance of
+ # Chef::Runner.converge.
def converge
results = @rest.get_rest("nodes/#{@safe_name}/compile")
results["collection"].resources.each do |r|
@@ -112,6 +145,7 @@ class Chef
end
protected
+ # Generates a random password of "len" length.
def random_password(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newpass = ""
diff --git a/lib/chef/compile.rb b/lib/chef/compile.rb
index 800d89c6b0..83192d4f26 100644
--- a/lib/chef/compile.rb
+++ b/lib/chef/compile.rb
@@ -27,6 +27,8 @@ class Chef
attr_accessor :node, :cookbook_loader, :collection, :definitions
+ # Creates a new Chef::Compile object. This object gets used by the Chef Server to generate
+ # a fully compiled recipe list for a node.
def initialize()
@node = nil
@cookbook_loader = Chef::CookbookLoader.new
@@ -34,6 +36,10 @@ class Chef
@definitions = Hash.new
end
+ # Looks up the node via the "name" argument, first from CouchDB, then by calling
+ # Chef::Node.find_file(name)
+ #
+ # The first step in compiling the catalog. Results available via the node accessor.
def load_node(name)
Chef::Log.debug("Loading Chef Node #{name} from CouchDB")
@node = Chef::Node.load(name)
@@ -42,6 +48,10 @@ class Chef
@node
end
+ # Load all the definitions, from every cookbook, so they are available when we process
+ # the recipes.
+ #
+ # Results available via the definitions accessor.
def load_definitions()
@cookbook_loader.each do |cookbook|
hash = cookbook.load_definitions
@@ -49,6 +59,10 @@ class Chef
end
end
+ # Load all the recipes specified in the node data (loaded via load_node, above.)
+ #
+ # The results are available via the collection accessor (which returns a Chef::ResourceCollection
+ # object)
def load_recipes
@node.recipes.each do |recipe|
rmatch = recipe.match(/(.+?)::(.+)/)
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index 5ad4cdb2af..cf4faf727e 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -50,6 +50,8 @@ class Chef
:couchdb_url => "http://localhost:5984",
:registration_url => "http://localhost:4000",
:openid_url => "http://localhost:4001",
+ :template_url => "http://localhost:4000",
+ :remotefile_url => "http://localhost:4000",
:couchdb_database => "chef",
:openid_store_path => "/var/chef/openid/db",
:openid_cstore_path => "/var/chef/openid/cstore",
diff --git a/lib/chef/cookbook.rb b/lib/chef/cookbook.rb
index 75ab8f4ad6..b7c6506a99 100644
--- a/lib/chef/cookbook.rb
+++ b/lib/chef/cookbook.rb
@@ -21,13 +21,15 @@
class Chef
class Cookbook
- attr_accessor :attribute_files, :definition_files, :name
+ attr_accessor :attribute_files, :definition_files, :template_files, :remote_files, :name
attr_reader :recipe_files
def initialize(name)
@name = name
@attribute_files = Array.new
@definition_files = Array.new
+ @template_files = Array.new
+ @remote_files = Array.new
@recipe_files = Array.new
@recipe_names = Hash.new
@loaded_attributes = false
diff --git a/lib/chef/cookbook_loader.rb b/lib/chef/cookbook_loader.rb
index 95edecfab8..ee4e1891e3 100644
--- a/lib/chef/cookbook_loader.rb
+++ b/lib/chef/cookbook_loader.rb
@@ -42,6 +42,8 @@ class Chef
:attribute_files => Array.new,
:definition_files => Array.new,
:recipe_files => Array.new,
+ :template_files => Array.new,
+ :remote_files => Array.new,
}
end
ignore_regexes = load_ignore_file(File.join(cookbook, "ignore"))
@@ -61,6 +63,16 @@ class Chef
cookbook_settings[cookbook_name][:recipe_files],
cookbook_settings[cookbook_name][:ignore_regexes]
)
+ load_files_unless_basename(
+ File.join(cookbook, "templates", "*.erb"),
+ cookbook_settings[cookbook_name][:template_files],
+ cookbook_settings[cookbook_name][:ignore_regexes]
+ )
+ load_files_unless_basename(
+ File.join(cookbook, "files", "*"),
+ cookbook_settings[cookbook_name][:remote_files],
+ cookbook_settings[cookbook_name][:ignore_regexes]
+ )
end
end
cookbook_settings.each_key do |cookbook|
@@ -68,6 +80,8 @@ class Chef
@cookbook[cookbook].attribute_files = cookbook_settings[cookbook][:attribute_files]
@cookbook[cookbook].definition_files = cookbook_settings[cookbook][:definition_files]
@cookbook[cookbook].recipe_files = cookbook_settings[cookbook][:recipe_files]
+ @cookbook[cookbook].template_files = cookbook_settings[cookbook][:template_files]
+ @cookbook[cookbook].remote_files = cookbook_settings[cookbook][:remote_files]
end
end
diff --git a/lib/chef/platform.rb b/lib/chef/platform.rb
index 55e85ae354..4114d5960b 100644
--- a/lib/chef/platform.rb
+++ b/lib/chef/platform.rb
@@ -35,6 +35,7 @@ class Chef
:file => Chef::Provider::File,
:directory => Chef::Provider::Directory,
:link => Chef::Provider::Link,
+ :template => Chef::Provider::Template,
}
}
diff --git a/lib/chef/provider/template.rb b/lib/chef/provider/template.rb
new file mode 100644
index 0000000000..47ffc810bf
--- /dev/null
+++ b/lib/chef/provider/template.rb
@@ -0,0 +1,69 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require File.join(File.dirname(__FILE__), "file")
+require 'uri'
+
+class Chef
+ class Provider
+ class Template < Chef::Provider::File
+
+ def action_create
+ r = Chef::REST.new(Chef::Config[:template_url])
+ template_url = nil
+ if @new_resource.template =~ /^http/
+ template_url = @new_resource.template
+ else
+ template_url = "cookbooks/#{@new_resource.cookbook_name}/templates/#{@new_resource.template}"
+ end
+ template = r.get_rest(template_url)
+
+
+ unless ::File.exists?(@new_resource.path)
+ Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
+ ::File.open(@new_resource.path, "w+") { |f| }
+ @new_resource.updated = true
+ end
+ set_owner if @new_resource.owner != nil
+ set_group if @new_resource.group != nil
+ set_mode if @new_resource.mode != nil
+ end
+
+ def action_delete
+ if ::File.exists?(@new_resource.path) && ::File.writable?(@new_resource.path)
+ Chef::Log.info("Deleting #{@new_resource} at #{@new_resource.path}")
+ ::File.delete(@new_resource.path)
+ @new_resource.updated = true
+ else
+ raise "Cannot delete #{@new_resource} at #{@new_resource_path}!"
+ end
+ end
+
+ def action_touch
+ action_create
+ time = Time.now
+ Chef::Log.info("Updating #{@new_resource} with new atime/mtime of #{time}")
+ ::File.utime(time, time, @new_resource.path)
+ @new_resource.updated = true
+ end
+
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/chef/recipe.rb b/lib/chef/recipe.rb
index 47a194eda7..c161bbdae6 100644
--- a/lib/chef/recipe.rb
+++ b/lib/chef/recipe.rb
@@ -105,6 +105,8 @@ class Chef
args << @collection
args << @node
resource = eval(rname).new(*args)
+ resource.cookbook_name = @cookbook_name
+ resource.recipe_name = @recipe_name
resource.params = @params
resource.instance_eval(&block)
rescue Exception => e
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index e42b791b1d..40d711f969 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -28,7 +28,7 @@ class Chef
include Chef::Mixin::CheckHelper
include Chef::Mixin::ParamsValidate
- attr_accessor :actions, :params, :provider, :updated, :allowed_actions, :collection
+ attr_accessor :actions, :params, :provider, :updated, :allowed_actions, :collection, :cookbook_name, :recipe_name
attr_reader :resource_name, :source_line, :node
def initialize(name, collection=nil, node=nil)
diff --git a/lib/chef/resource/template.rb b/lib/chef/resource/template.rb
new file mode 100644
index 0000000000..25df8c712d
--- /dev/null
+++ b/lib/chef/resource/template.rb
@@ -0,0 +1,52 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+
+class Chef
+ class Resource
+ class Template < Chef::Resource::File
+
+ def initialize(name, collection=nil, node=nil)
+ @resource_name = :template
+ super(name, collection, node)
+ @action = "create"
+ @template = nil
+ @variables = Hash.new
+ end
+
+ def template(file=nil)
+ set_or_return(
+ :template,
+ file,
+ :kind_of => [ String ]
+ )
+ end
+
+ def variables(args=nil)
+ set_or_return(
+ :vars,
+ args,
+ :kind_of => [ Hash ]
+ )
+ end
+
+ end
+ end
+end \ No newline at end of file