diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-08-16 17:54:07 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-08-16 17:54:07 -0700 |
commit | d1202ca3b100a6e1caaf6eeacec5976adf147f54 (patch) | |
tree | f4a647520c485113537f0a735fadf6280700634d /lib/chef/provider | |
parent | 643b1a00f7405866cf0b30a3f26ff6d03bee932a (diff) | |
download | chef-d1202ca3b100a6e1caaf6eeacec5976adf147f54.tar.gz |
Added template support, changed license to Apache v 2
Diffstat (limited to 'lib/chef/provider')
-rw-r--r-- | lib/chef/provider/directory.rb | 26 | ||||
-rw-r--r-- | lib/chef/provider/file.rb | 61 | ||||
-rw-r--r-- | lib/chef/provider/link.rb | 26 | ||||
-rw-r--r-- | lib/chef/provider/template.rb | 97 |
4 files changed, 122 insertions, 88 deletions
diff --git a/lib/chef/provider/directory.rb b/lib/chef/provider/directory.rb index da6770e47b..34257504fc 100644 --- a/lib/chef/provider/directory.rb +++ b/lib/chef/provider/directory.rb @@ -1,22 +1,20 @@ # # 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. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # -# 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 +# http://www.apache.org/licenses/LICENSE-2.0 # +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# require File.join(File.dirname(__FILE__), "file") diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb index 7bb729cd87..d8644f50ee 100644 --- a/lib/chef/provider/file.rb +++ b/lib/chef/provider/file.rb @@ -1,25 +1,24 @@ # # 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. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # -# 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 +# http://www.apache.org/licenses/LICENSE-2.0 # +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# require 'digest/md5' require 'etc' +require 'fileutils' class Chef class Provider @@ -32,17 +31,18 @@ class Chef @current_resource.owner(cstats.uid) @current_resource.group(cstats.gid) @current_resource.mode("%o" % (cstats.mode & 007777)) - checksum + @current_resource.checksum(checksum(@current_resource.path)) end @current_resource end - def checksum + def checksum(file) digest = Digest::MD5.new - IO.foreach(@current_resource.path) do |line| - digest << line + fh = ::File.open(file) + fh.each do |line| + digest.update(line) end - @current_resource.checksum(digest.hexdigest) + digest.hexdigest end # Compare the ownership of a file. Returns true if they are the same, false if they are not. @@ -125,6 +125,7 @@ class Chef def action_delete if ::File.exists?(@new_resource.path) && ::File.writable?(@new_resource.path) + backup Chef::Log.info("Deleting #{@new_resource} at #{@new_resource.path}") ::File.delete(@new_resource.path) @new_resource.updated = true @@ -141,6 +142,28 @@ class Chef @new_resource.updated = true end + def backup + if @new_resource.backup + time = Time.now + savetime = time.strftime("%Y%m%d%H%M%S") + backup_filename = "#{@new_resource.path}.chef-#{savetime}" + Chef::Log.info("Backing up #{@new_resource} to #{backup_filename}") + FileUtils.cp(@new_resource.path, backup_filename) + + # Clean up after the number of backups + slice_number = @new_resource.backup - 1 + backup_files = Dir["#{@new_resource.path}.chef-*"].sort { |a,b| b <=> a } + if backup_files.length >= @new_resource.backup + remainder = backup_files.slice(slice_number..-1) + remainder.each do |backup_to_delete| + Chef::Log.info("Removing backup of #{@new_resource} at #{backup_to_delete}") + FileUtils.rm(backup_to_delete) + end + end + + end + end + end end end
\ No newline at end of file diff --git a/lib/chef/provider/link.rb b/lib/chef/provider/link.rb index 4a1254de9b..843a4ad009 100644 --- a/lib/chef/provider/link.rb +++ b/lib/chef/provider/link.rb @@ -1,22 +1,20 @@ # # 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. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # -# 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 +# http://www.apache.org/licenses/LICENSE-2.0 # +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# class Chef class Provider diff --git a/lib/chef/provider/template.rb b/lib/chef/provider/template.rb index 47ffc810bf..78736f2882 100644 --- a/lib/chef/provider/template.rb +++ b/lib/chef/provider/template.rb @@ -1,25 +1,25 @@ # # 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. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # -# 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 +# http://www.apache.org/licenses/LICENSE-2.0 # +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# require File.join(File.dirname(__FILE__), "file") require 'uri' +require 'erubis' +require 'tempfile' class Chef class Provider @@ -27,43 +27,58 @@ class Chef def action_create r = Chef::REST.new(Chef::Config[:template_url]) - template_url = nil - if @new_resource.template =~ /^http/ - template_url = @new_resource.template + + template_url = generate_url(@new_resource.template) + raw_template_file = r.get_rest(template_url, true) + + template_file = render_template(raw_template_file.path) + + update = false + if ::File.exists?(@new_resource.path) + @new_resource.checksum(self.checksum(template_file.path)) + if @new_resource.checksum != @current_resource.checksum + Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}") + Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}") + update = true + end else - template_url = "cookbooks/#{@new_resource.cookbook_name}/templates/#{@new_resource.template}" + Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}") + update = true 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 + if update + FileUtils.cp(template_file.path, @new_resource.path) 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 render_template(file) + eruby = Erubis::Eruby.new(::File.read(file)) + context = @new_resource.variables + context[:node] = @node + output = eruby.evaluate(context) + final_tempfile = Tempfile.new("chef-rendered-template") + final_tempfile.puts(output) + final_tempfile.close + final_tempfile + end + + def generate_url(url) + template_url = nil + if url =~ /^http/ + template_url = url + else + template_url = "cookbooks/#{@new_resource.cookbook_name}/templates?" + template_url += "id=#{url}" + platform, version = Chef::Platform.find_platform_and_version(@node) + template_url += "&platform=#{platform}&version=#{version}" 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 - + return template_url end end end
\ No newline at end of file |