From e3abcd6b5330959016b43115a8bf1b1cef668d30 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Mon, 20 Jul 2015 16:18:52 -0400 Subject: Add package_manifest resource. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch converts the write_package_names function into a proper resource. Using the write_package_names only works if the function comes last in the puppet manifest. By making the same functionality a custom resource we allow for it to exist anywhere in the manifest and provide the same functionality. The new syntax would be: package_manifest{'/tmp/foo': ensure => present} Co-Authored-By: Martin Mágr Change-Id: If3e03b1983fed47082fac8ce63f975557dbc503c --- lib/puppet/provider/package_manifest/flat_file.rb | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/puppet/provider/package_manifest/flat_file.rb (limited to 'lib/puppet/provider/package_manifest') diff --git a/lib/puppet/provider/package_manifest/flat_file.rb b/lib/puppet/provider/package_manifest/flat_file.rb new file mode 100644 index 0000000..96e033b --- /dev/null +++ b/lib/puppet/provider/package_manifest/flat_file.rb @@ -0,0 +1,39 @@ + +require 'set' + + +Puppet::Type.type(:package_manifest).provide(:flat_file) do + + desc "Write package manifest to a flat file" + + def exists? + # exists? is always run before create, so we can create package list here + @packages = resource.catalog.resources.collect { |r| + r.name if r.type == :package + }.compact.sort + + exists = File.exist?(resource[:path]) + if exists + new_content = Set.new @packages + old_content = Set.new( + File.open(resource[:path], 'r').each_line.collect{ |pkg| pkg.strip() } + ) + exists = new_content == old_content + end + exists + end + + def create + FileUtils.mkdir_p(File.dirname(resource[:path])) + File.open(resource[:path], 'w') do |f| + @packages.each do |pkg_name| + f.puts(pkg_name) + end + end + end + + def destroy + File.delete(resource[:path]) + end + +end -- cgit 1.2.3-korg