diff options
author | Tim Smith <tsmith@chef.io> | 2020-04-20 10:38:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-20 10:38:03 -0700 |
commit | 34297510fa11cdb0c10e833a9d5c5282e82f1278 (patch) | |
tree | cafbd1714401e237a94a23d6c48638c594d97605 /lib/chef/provider | |
parent | c6f2d2720d712e912bad5735be324605642131f3 (diff) | |
parent | adcc1c6d0fe1b4cb34dc981b1819177545b30463 (diff) | |
download | chef-34297510fa11cdb0c10e833a9d5c5282e82f1278.tar.gz |
Merge pull request #9627 from chef/Kapil/GitHub-2860_exception_raised_in_define_resource_requirements
git resource: Fix for exceptions raised in why-run mode
Diffstat (limited to 'lib/chef/provider')
-rw-r--r-- | lib/chef/provider/git.rb | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/lib/chef/provider/git.rb b/lib/chef/provider/git.rb index ce7483aa80..9a86f26f60 100644 --- a/lib/chef/provider/git.rb +++ b/lib/chef/provider/git.rb @@ -41,6 +41,20 @@ class Chef end def define_resource_requirements + unless new_resource.user.nil? + requirements.assert(:all_actions) do |a| + a.assertion do + begin + get_homedir(new_resource.user) + rescue ArgumentError + false + end + end + a.whyrun("User #{new_resource.user} does not exist, this run will fail unless it has been previously created. Assuming it would have been created.") + a.failure_message(Chef::Exceptions::User, "#{new_resource.user} required by resource #{new_resource.name} does not exist") + end + end + # Parent directory of the target must exist. requirements.assert(:checkout, :sync) do |a| dirname = ::File.dirname(cwd) @@ -313,18 +327,7 @@ class Chef # Certain versions of `git` misbehave if git configuration is # inaccessible in $HOME. We need to ensure $HOME matches the # user who is executing `git` not the user running Chef. - env["HOME"] = - begin - require "etc" unless defined?(Etc) - case new_resource.user - when Integer - Etc.getpwuid(new_resource.user).dir - else - Etc.getpwnam(new_resource.user.to_s).dir - end - rescue ArgumentError # user not found - raise Chef::Exceptions::User, "Could not determine HOME for specified user '#{new_resource.user}' for resource '#{new_resource.name}'" - end + env["HOME"] = get_homedir(new_resource.user) end run_opts[:group] = new_resource.group if new_resource.group env["GIT_SSH"] = new_resource.ssh_wrapper if new_resource.ssh_wrapper @@ -355,6 +358,20 @@ class Chef new_resource.repository end end + + # Returns the home directory of the user + # @param [String] user must be a string. + # @return [String] the home directory of the user. + # + def get_homedir(user) + require "etc" unless defined?(Etc) + case user + when Integer + Etc.getpwuid(user).dir + else + Etc.getpwnam(user.to_s).dir + end + end end end end |