-
Notifications
You must be signed in to change notification settings - Fork 33
Provider mkdirp #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghoneycutt
wants to merge
2
commits into
ghoneycutt:master
Choose a base branch
from
sharlander:provider_mkdirp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Provider mkdirp #35
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| 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] | ||
| 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 | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? Since line 46 runs |
||
| 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 | ||
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
| 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 |
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
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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