summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorKapil Chouhan <kapil.chouhan@msystechnologies.com>2020-04-15 18:08:00 +0530
committerKapil Chouhan <kapil.chouhan@msystechnologies.com>2020-04-17 17:07:34 +0530
commitadcc1c6d0fe1b4cb34dc981b1819177545b30463 (patch)
tree40801fc5df64692c13d7ba73af24bc340f1e6ecc /lib/chef
parentcb4383bb80656be29db6194308d6a913894ad218 (diff)
downloadchef-adcc1c6d0fe1b4cb34dc981b1819177545b30463.tar.gz
Fix for exception raised in define resource requirementsKapil/GitHub-2860_exception_raised_in_define_resource_requirements
Signed-off-by: Kapil Chouhan <kapil.chouhan@msystechnologies.com>
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/provider/git.rb41
1 files changed, 29 insertions, 12 deletions
diff --git a/lib/chef/provider/git.rb b/lib/chef/provider/git.rb
index 1ef1481c9e..431c19449b 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