Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions lib/puppet/provider/mkdirp/ruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
require 'fileutils'
require 'etc'

Puppet::Type.type(:mkdirp).provide(:mkdirp) do

confine :osfamily => [:RedHat]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should work on any POSIX system. You might just confine to not windows. Though windows support could be added by updating the check to see if the path is valid. You might be able to call the stdlib function directly to check.

https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/lib/puppet/parser/functions/validate_absolute_path.rb

confine :operatingsystemmajrelease => [5, 6]

def create
if resource[:path]
then
$path = resource[:path]
else
$path = resource[:name]
end

if resource[:owner]
then
$owner = resource[:owner]
else
$owner = 'root'
end

if resource[:group]
then
$group = resource[:group]
else
$group = 'root'
end

if resource[:mode]
then
$mode = resource[:mode].to_i(8)
else
$mode = 0755
end

if resource[:recurse]
then
$recurse = resource[:recurse].to_i
else
$recurse = 1
end

unless File.directory? $path
FileUtils.mkdir_p($path)
end

$parent = $path
if $parent[0] != '/'
raise ArgumentError, "%s is not a absolute path" % parent
end

for i in 1..$recurse
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? Since line 46 runs mkdir -p that should handle setting the permissions.

if $parent.count('/') == 1
raise ArgumentError, "please check the recurse number, this would change permissions for %s" % $parent
end
FileUtils.chown($owner, $group, $parent)
FileUtils.chmod($mode, $parent)
$parent = File.expand_path("..", $parent)
end
end

def exists?
if resource[:path]
then
$path = resource[:path]
else
$path = resource[:name]
end

if resource[:owner]
then
$owner = resource[:owner]
else
$owner = 'root'
end

if resource[:group]
then
$group = resource[:group]
else
$group = 'root'
end

if resource[:mode]
then
$mode = resource[:mode]
else
$mode = '0755'
end

if resource[:recurse]
then
$recurse = resource[:recurse].to_i
else
$recurse = 1
end

if File.directory?($path)
$file_ex = true
else
return false
end

$parent = $path
if $parent[0] != '/'
raise ArgumentError, "%s is not a absolute path" % parent
end

for i in 1..$recurse
if Etc.getpwuid(File.stat($parent).uid).name == $owner
$owner_ex = true
else
$owner_ex = false
end

if Etc.getgrgid(File.stat($parent).gid).name == $group
$group_ex = true
else
$group_ex = false
end

if File.stat($parent).mode.to_s(8)[1..4] == $mode
$mode_ex = true
else
$mode_ex = false
end
$parent = File.expand_path('..', $parent)
end

if $file_ex == true and $owner_ex == true and $group_ex == true and $mode_ex == true
return true
else
return false
end
end

def destroy
if resource[:path]
then
$path = resource[:path]
else
$path = resource[:name]
end
FileUtils.rm_rf($path)
end
end
43 changes: 43 additions & 0 deletions lib/puppet/type/mkdirp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Puppet::Type.newtype(:mkdirp) do
ensurable

newparam(:name) do
desc "name of the directory"
isnamevar
end

newparam(:path) do
desc "path to the dircetory"
validate do |value|
unless value[0] == '/'
raise ArgumentError, "%s is not a absolute path" % value
end
end
end

newparam(:owner) do
desc "owner of the dircetory"
validate do |value|
unless value =~ /^\w+/
raise ArgumentError, "%s is not a valid user name" % value
end
end
end

newparam(:group) do
desc "group of the dircetory"
validate do |value|
unless value =~ /^\w+/
raise ArgumentError, "%s is not a valid group name" % value
end
end
end

newparam(:mode) do
desc "mode of the dircetory"
end

newparam(:recurse) do
desc "sets the permissons for n parent folders"
end
end
26 changes: 26 additions & 0 deletions manifests/mkdir.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This should replace the common::mkdir_p function
#
# the advantage is you can set the owner,group and permission
# for the specified directory or n-parent directories
#
# Example usage:
#
# mkdirp { 'example_dir':
# ensure => present,
# path => '/tmp/path/to/the/dir',
# owner => 'root',
# group => 'root',
# mode => '0755',
# recurse => '2',
# } # the recurse param sets the permissions for n-parent folders
#
# file { 'example_file':
# ensure => 'present',
# path => '/tmp/path/to/the/dir/filename',
# require => Mkdirp['example_dir'],
# }

# a known bug:
# if a value gets updated puppet will show this as "created"
#
# Notice: /Stage[main]/Mkdirp/Mkdirp[example_dir]/ensure: created