Last active
April 26, 2018 18:32
-
-
Save natemccurdy/293a383758281544a39c958863c4e46a to your computer and use it in GitHub Desktop.
A better alternative to "recuse => true" to set permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define recurse_file_permissions ( | |
String[1] $target_dir = $title, | |
Optional[String[1]] $file_mode = undef, | |
Optional[String[1]] $dir_mode = undef, | |
Optional[String[1]] $owner = undef, | |
Optional[String[1]] $group = undef, | |
) { | |
if $facts['os']['family'] == 'windows' { | |
fail("${module_name} does not support Windows") | |
} | |
unless $file_mode or $dir_mode or $owner or $group { | |
fail('At least one of file_mode, dir_mode, owner, or group is required') | |
} | |
if $dir_mode { | |
exec { "Set perms of ${target_dir} directories to ${dir_mode}": | |
command => "find ${target_dir}/ -type d ! -perm ${dir_mode} -exec chmod -c ${dir_mode} {} \\;", | |
onlyif => "find ${target_dir}/ -type d ! -perm ${dir_mode} | grep '.*'", | |
path => $facts['path'], | |
logoutput => true, | |
loglevel => 'info', | |
} | |
} | |
if $file_mode { | |
exec { "Set perms of ${target_dir} contents to ${file_mode}": | |
command => "find ${target_dir}/ -type f ! -perm ${file_mode} -exec chmod -c ${file_mode} {} \\;", | |
onlyif => "find ${target_dir}/ -type f ! -perm ${file_mode} | grep '.*'", | |
path => $facts['path'], | |
logoutput => true, | |
loglevel => 'info', | |
} | |
} | |
if $owner and $group { | |
exec { "Set owner and group of ${target_dir} contents to ${owner}:${group}": | |
command => "find ${target_dir}/ \\( ! -user ${owner} -or ! -group ${group} \\) -exec chown ${owner}:${group} -c {} \\;", | |
onlyif => "find ${target_dir}/ \\( ! -user ${owner} -or ! -group ${group} \\) | grep '.*'", | |
path => $facts['path'], | |
logoutput => true, | |
loglevel => 'info', | |
} | |
} elsif $owner { | |
exec { "Set owner of ${target_dir} contents to ${owner}": | |
command => "find ${target_dir}/ \\( ! -user ${owner} \\) -exec chown ${owner} -c {} \\;", | |
onlyif => "find ${target_dir}/ \\( ! -user ${owner} \\) | grep '.*'", | |
path => $facts['path'], | |
logoutput => true, | |
loglevel => 'info', | |
} | |
} elsif $group { | |
exec { "Set group of ${target_dir} contents to ${group}": | |
command => "find ${target_dir}/ \\( ! -group ${group} \\) -exec chgrp ${group} -c {} \\;", | |
onlyif => "find ${target_dir}/ \\( ! -group ${group} \\) | grep '.*'", | |
path => $facts['path'], | |
logoutput => true, | |
loglevel => 'info', | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment