Skip to content
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

Ensure $HOME is correct before a ruby build (possibly from git.) #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 12 additions & 7 deletions libraries/chef_mixin_rbenv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions providers/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down