From 398f74fdd469524a5644adad855169819ffb2033 Mon Sep 17 00:00:00 2001 From: James Coleman Date: Tue, 17 Jun 2014 11:17:20 -0400 Subject: [PATCH] Ensure $HOME is correct before a ruby build (possibly from git.) This is a continuation of the work in db913928da8a389ff8edd98309dfb00ea3fa9fd8 which added a workaround for a git bug when using git to clone the rbenv install. But the bug is also present when a ruby build happens from a git source. This patch ensures that $HOME is temporarily changed to the home directory for `node[:rbenv][:user]` during the ruby build process. --- libraries/chef_mixin_rbenv.rb | 19 ++++++++++++------- providers/ruby.rb | 5 +++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/libraries/chef_mixin_rbenv.rb b/libraries/chef_mixin_rbenv.rb index 94247e0..c2bb7da 100644 --- a/libraries/chef_mixin_rbenv.rb +++ b/libraries/chef_mixin_rbenv.rb @@ -107,6 +107,15 @@ def rbenv_root_path node[:rbenv][:root_path] end + def home_directory_for_user(username) + begin + require 'etc' + Etc.getpwnam(username).dir + rescue ArgumentError # user not found + "/home/#{username}" + end + end + # Ensures $HOME is temporarily set to the given user. The original # $HOME is preserved and re-set after the block has been yielded # to. @@ -119,18 +128,13 @@ def rbenv_root_path # https://github.com/git/git/commit/4698c8feb1bb56497215e0c10003dd046df352fa # def with_home_for_user(username, &block) - time = Time.now.to_i + home_directory_for_user = home_directory_for_user(username) ruby_block "set HOME for #{username} at #{time}" do block do ENV['OLD_HOME'] = ENV['HOME'] - ENV['HOME'] = begin - require 'etc' - Etc.getpwnam(username).dir - rescue ArgumentError # user not found - "/home/#{username}" - end + ENV['HOME'] = home_directory_for_user end end @@ -139,6 +143,7 @@ def with_home_for_user(username, &block) ruby_block "unset HOME for #{username} #{time}" do block do ENV['HOME'] = ENV['OLD_HOME'] + ENV.delete('OLD_HOME') end end end diff --git a/providers/ruby.rb b/providers/ruby.rb index 0de4d28..e301640 100644 --- a/providers/ruby.rb +++ b/providers/ruby.rb @@ -27,11 +27,16 @@ else Chef::Log.info "rbenv_ruby[#{new_resource.name}] is building, this may take a while..." + original_env_home = ENV['HOME'] + ENV['HOME'] = home_directory_for_user(node[:rbenv][:user]) + start_time = Time.now out = new_resource.patch ? rbenv_command("install --patch #{new_resource.name}", patch: new_resource.patch) : rbenv_command("install #{new_resource.name}") + ENV['HOME'] = original_env_home + unless out.exitstatus == 0 raise Chef::Exceptions::ShellCommandFailed, "\n" + out.format_for_exception end