From 00364eb915de42d5d969b75fa640d66e87747671 Mon Sep 17 00:00:00 2001 From: Jeff Blaine Date: Thu, 12 Sep 2013 18:09:08 -0400 Subject: [PATCH] Introducing delete_lines --- README.md | 8 +++- libraries/provider_delete_lines.rb | 77 ++++++++++++++++++++++++++++++ libraries/resource_delete_lines.rb | 50 +++++++++++++++++++ 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 libraries/provider_delete_lines.rb create mode 100644 libraries/resource_delete_lines.rb diff --git a/README.md b/README.md index aaf2cb17..c5847204 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,17 @@ the resoures. line "Why hello there, you beautiful person, you." end + delete_lines "remove hash-comments from /some/file" do + path "/some/file" + pattern "^#.*" + end + # Notes So far, the only resource implemented are - append_if_no_line. + append_if_no_line replace_or_add + delete_lines More to follow. diff --git a/libraries/provider_delete_lines.rb b/libraries/provider_delete_lines.rb new file mode 100644 index 00000000..6bd6d8ac --- /dev/null +++ b/libraries/provider_delete_lines.rb @@ -0,0 +1,77 @@ +# +# Cookbook Name:: line +# Library:: provider_delete_lines +# +# Author:: Sean OMeara +# Author:: Jeff Blaine +# Copyright 2012-2013, Opscode, Inc. +# +# 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 +# +# 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 'fileutils' +require 'tempfile' + +class Chef + class Provider + class DeleteLines < Chef::Provider + + def load_current_resource + end + + def action_edit + regex = /#{new_resource.pattern}/ + + if ::File.exists?(new_resource.path) then + begin + f = ::File.open(new_resource.path, "r+") + + file_owner = f.lstat.uid + file_group = f.lstat.gid + file_mode = f.lstat.mode + + temp_file = Tempfile.new('foo') + + modified = false + + f.lines.each do |line| + if line =~ regex then + modified = true + else + temp_file.puts line + end + end + + f.close + + if modified then + temp_file.rewind + FileUtils.copy_file(temp_file.path,new_resource.path) + FileUtils.chown(file_owner,file_group,new_resource.path) + FileUtils.chmod(file_mode,new_resource.path) + new_resource.updated_by_last_action(true) + end + + ensure + temp_file.close + temp_file.unlink + end + end # ::File.exists + end # def action_edit + + def action_nothing + end + + end + end +end diff --git a/libraries/resource_delete_lines.rb b/libraries/resource_delete_lines.rb new file mode 100644 index 00000000..f203a812 --- /dev/null +++ b/libraries/resource_delete_lines.rb @@ -0,0 +1,50 @@ +# Cookbook Name:: line +# Library:: resource_delete_lines +# +# Author:: Sean OMeara +# Author:: Jeff Blaine +# Copyright 2012-2013, Opscode, Inc. +# +# 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 +# +# 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 Resource + class DeleteLines < Chef::Resource + + def initialize(name, run_context=nil) + super + @resource_name = :delete_lines + @action = :edit + @allowed_actions.push(:edit,:nothing) + end + + def path(arg=nil) + set_or_return( + :path, + arg, + :kind_of => String + ) + end + + def pattern(arg=nil) + set_or_return( + :line, + arg, + :kind_of => String + ) + end + + end + end +end