Skip to content

Commit

Permalink
Add autorequire on parent ACL and files
Browse files Browse the repository at this point in the history
  • Loading branch information
roidelapluie committed Sep 23, 2015
1 parent 01d86cb commit 439bcd0
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions lib/puppet/type/acl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'set'
require 'pathname'

Puppet::Type.newtype(:acl) do
desc <<-EOT
Expand Down Expand Up @@ -67,11 +68,41 @@
defaultto :lazy
end

autorequire(:file) do
if self[:path]
[self[:path]]
# Credits to @itdoesntwork
# http://stackoverflow.com/questions/26878341/how-do-i-tell-if-one-path-is-an-ancestor-of-another
def self.is_descendant?(a, b)
a_list = File.expand_path(a).split('/')
b_list = File.expand_path(b).split('/')

b_list[0..a_list.size-1] == a_list and b_list != a_list
end

# Snippet based on upstream Puppet (ASL 2.0)
[:acl, :file].each do | autorequire_type |
autorequire(autorequire_type) do
req = []
path = Pathname.new(self[:path])
if autorequire_type != :acl
if self[:recursive] == :true
catalog.resources.find_all { |r|
r.is_a?(Puppet::Type.type(autorequire_type)) and self.class.is_descendant?(self[:path], r[:path])
}.each do | found |
req << found[:path]
end
end
req << self[:path]
end
if !path.root?
# Start at our parent, to avoid autorequiring ourself
parents = path.parent.enum_for(:ascend)
if found = parents.find { |p| catalog.resource(autorequire_type, p.to_s) }
req << found.to_s
end
end
req
end
end
# End of Snippet

newproperty(:permission, :array_matching => :all) do
desc "ACL permission(s)."
Expand Down Expand Up @@ -105,7 +136,7 @@ def strip_perms(pl)
value = []
pl.each do |perm|
if !(perm =~ /^(((u(ser)?)|(g(roup)?)|(m(ask)?)|(o(ther)?)):):/)
perm = perm.split(':')[0..-2].join(':')
perm = perm.split(':',-1)[0..-2].join(':')
value << perm
end
end
Expand Down Expand Up @@ -216,8 +247,7 @@ def self.pick_default_perms(acl)
end

def newchild(path)
full_path = ::File.join(self[:path], path)
options = @original_parameters.merge(:name => full_path).reject { |param, value| value.nil? }
options = @original_parameters.merge(:name => path).reject { |param, value| value.nil? }
unless File.directory?(options[:name]) then
options[:permission] = self.class.pick_default_perms(options[:permission]) if options.include?(:permission)
end
Expand All @@ -229,13 +259,23 @@ def newchild(path)

def generate
return [] unless self[:recursive] == :true and self[:recursemode] == :deep
return [] unless File.directory?(self[:path])
results = []
Dir.chdir(self[:path]) do
Dir['**/*'].each do |path|
results << newchild(path)
paths = Set.new()
if File.directory?(self[:path])
Dir.chdir(self[:path]) do
Dir['**/*'].each do |path|
paths << ::File.join(self[:path], path)
end
end
end
catalog.resources.find_all { |r|
r.is_a?(Puppet::Type.type(:file)) and self.class.is_descendant?(self[:path], r[:path])
}.each do | found |
paths << found[:path]
end
paths.each { | path |
results << newchild(path)
}
results
end

Expand Down

0 comments on commit 439bcd0

Please sign in to comment.