Hi all,

As my first foray into types and providers (and as suggested on the -users 
list), here is a patch for Onyx Point's Concat type which implements insync? 
for both concat_build and concat_fragment. It's probably ugly, and I need to 
learn, so feedback is welcome :)

Trevor, I'll do a pull request on GitHub shortly.

Cheers,
Greg

diff --git a/lib/puppet/provider/concat_build/build.rb 
b/lib/puppet/provider/concat_build/build.rb
index b9e7539..01b054d 100644
--- a/lib/puppet/provider/concat_build/build.rb
+++ b/lib/puppet/provider/concat_build/build.rb
@@ -21,8 +21,9 @@ Puppet::Type.type(:concat_build ).provide :concat_build do
 
   desc "concat_build provider"
 
-  def build_file
-    if 
File.directory?("/var/lib/puppet/concat/fragments/#{@resource[:name]}") then
+  def build_file(build = false)
+    if 
File.directory?("/var/lib/puppet/concat/fragments/#{@resource[:name]}") and 
not build then
+      # Just diff'ing - build the file but don't move it yet
       begin
         FileUtils.mkdir_p("/var/lib/puppet/concat/output")
 
@@ -101,18 +102,22 @@ Puppet::Type.type(:concat_build ).provide 
:concat_build do
 
         f.close
 
-       
 
FileUtils.touch("/var/lib/puppet/concat/fragments/#{@resource[:name]}/.~concat_fragments")
-        if @resource[:target] and check_onlyif then
-          debug "Copying 
/var/lib/puppet/concat/output/#{@resource[:name]}.out to 
#{@resource[:target]}"
-         
 FileUtils.cp("/var/lib/puppet/concat/output/#{@resource[:name]}.out", 
@resource[:target])
-        elsif @resource[:target] then
-          debug "Not copying to #{@resource[:target]}, 'onlyif' check 
failed"
-        elsif @resource[:onlyif] then
-          debug "Specified 'onlyif' without 'target', ignoring."
-        end
       rescue Exception => e
         fail Puppet::Error, e
       end
+    elsif 
File.directory?("/var/lib/puppet/concat/fragments/#{@resource[:name]}") and 
build then
+      # This time for real - move the built file into the fragments dir
+     
 
FileUtils.touch("/var/lib/puppet/concat/fragments/#{@resource[:name]}/.~concat_fragments")
+      if @resource[:target] and check_onlyif then
+        debug "Copying 
/var/lib/puppet/concat/output/#{@resource[:name]}.out to 
#{@resource[:target]}"
+       
 FileUtils.cp("/var/lib/puppet/concat/output/#{@resource[:name]}.out", 
@resource[:target])
+      elsif @resource[:target] then
+        debug "Not copying to #{@resource[:target]}, 'onlyif' check failed"
+      elsif @resource[:onlyif] then
+        debug "Specified 'onlyif' without 'target', ignoring."
+      end
     elsif not @resource.quiet? then
       fail Puppet::Error, "The fragments directory at 
'/var/lib/puppet/concat/fragments/#{@resource[:name]}' does not exist!"
     end
diff --git a/lib/puppet/type/concat_build.rb 
b/lib/puppet/type/concat_build.rb
index 1670322..72e2be0 100644
--- a/lib/puppet/type/concat_build.rb
+++ b/lib/puppet/type/concat_build.rb
@@ -16,6 +16,9 @@
 # You should have received a copy of the GNU General Public License along 
with
 # this program.  If not, see <http://www.gnu.org/licenses/>.
 #
+
+include Puppet::Util::Diff
+
 Puppet::Type.newtype(:concat_build) do
   @doc = "Build file from fragments"
 
@@ -158,11 +161,20 @@ Puppet::Type.newtype(:concat_build) do
     end
 
     def insync?(is)
-      return false
+      # Build the temporary file, and then diff it against the actual one
+      provider.build_file(false)
+      diffs = 
diff(@resource[:target],"/var/lib/puppet/concat/output/#{@resource[:name]}.out")
 
+      if diffs != "" then
+        puts diffs
+        return false
+      else
+        return true
+      end
     end
 
     def sync
-      provider.build_file
+      # Move the tempfile into place
+      provider.build_file(true)
     end
 
     def change_to_s(currentvalue, newvalue)
diff --git a/lib/puppet/type/concat_fragment.rb 
b/lib/puppet/type/concat_fragment.rb
index 2ac7fab..705cfe9 100644
--- a/lib/puppet/type/concat_fragment.rb
+++ b/lib/puppet/type/concat_fragment.rb
@@ -16,6 +16,9 @@
 # You should have received a copy of the GNU General Public License along 
with
 # this program.  If not, see <http://www.gnu.org/licenses/>.
 #
+
+include Puppet::Util::Diff
+
 Puppet::Type.newtype(:concat_fragment) do
   @doc = "Create a concat fragment"
 
@@ -26,7 +29,19 @@ Puppet::Type.newtype(:concat_fragment) do
     end
 
     def insync?(is)
-      return false
+      group = @resource[:name].split('+').first
+      fragment = @resource[:name].split('+')[1..-1].join('+')
+      frag_file = "/var/lib/puppet/concat/fragments/#{group}/#{fragment}"
+
+      if File.exist?(frag_file)
+        data = File.read(frag_file)
+        if data == @resource[:content] then
+          debug "Disk contents differ from resource content for 
#{@resource[:name]}"
+          return true
+        end
+      else
+        return false
+      end
     end
 
     def sync

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/puppet-dev/-/SzJqVjZCZ1dUOVlK.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to