summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/package_manifest/flat_file.rb
blob: 96e033be8f3c0f3d8100d1c14332f130b9875f5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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