Skip to content

Instantly share code, notes, and snippets.

@WhatsARanjit
Created October 30, 2017 20:30
Show Gist options
  • Save WhatsARanjit/7f67151a6362f94c1872d979c36c465e to your computer and use it in GitHub Desktop.
Save WhatsARanjit/7f67151a6362f94c1872d979c36c465e to your computer and use it in GitHub Desktop.
Parenting files
[root@puppet2017 ~]# echo -e "/tmp/foo\n/tmp/bar" > /tmp/whitelist
[root@puppet2017 ~]# ruby /root/test.rb
App_file[/tmp/foo]
App_file[/tmp/bar]
#!/opt/puppetlabs/puppet/bin/ruby
require 'puppet'
Puppet.initialize_settings
Puppet::Type.newtype(:app_file) do
ensurable
newparam(:path, :namevar => true) do
validate do |value|
unless Puppet::Util.absolute_path?(value)
fail Puppet::Error, _("File paths must be fully qualified, not '%{path}'") % { path: value }
end
end
end
end
Puppet::Type.type(:app_file).provide(
:file,
:parent => Puppet::Type.type(:file).provider(:posix)
) do
def self.instances
f = File.read '/tmp/whitelist'
f.split("\n").collect do |whitef|
res_hash = Hash.new
res_hash[:ensure] = :present
res_hash[:path] = whitef
res_hash[:name] = whitef
new(res_hash)
end
end
def self.prefetch(resources)
fs = instances
resources.keys.each do |file|
if provider = fs.find{ |f| f.path == file }
resources[file].provider = provider
end
end
end
def exists?
@property_hash[:ensure] == :present
end
end
puts Puppet::Type.type(:app_file).instances.to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment