opscode/chef — pull request #2349 by drags (2014-11-04 04:55:18+00:00)
Title: Reset $HOME to user running chef-client when running via sudo

### Version: 11.16.4

### Environment: Ubuntu 14.04 in EC2

### Scenario:
[What you are trying to achieve and you can't?]
Trying to pull a docker image using the docker cookbook from the Chef Supermarket. Unfortunately, when running via `knife bootstrap` and logging in as a non-root user the `sudo` command does not use the `-H` argument in order to update the `$HOME` environment variable to become the home directory of the user that is being `sudo`'d to.

In this scenario `knife bootstrap -x ubuntu my-host.mydomain.tld --sudo -r 'role[base]'` kicks off the chef install as expected and begins running the converged cookbooks, however when it gets to the `docker pull` step it fails because `$HOME` is still set to `/home/ubuntu/`, and the `docker` command searches `$HOME` for a `.dockercfg`.

This is one example of how this issue will affect `execute`/`bash` resources which expect a well formed `env` for the running commands.

### Steps to Reproduce:
Create an `execute` resource which runs a command that searches for its configuration within $HOME. Run the chef-client from a different user using `sudo -u chef-client`. Compare this with `sudo -H -u chef-client`.

A terse reproduction example:

someuser@host $ sudo -u root chef-apply -s -l debug
execute "env" do
command "env"
end
^D
...snip...
HOME=/home/someuser
...snip...

someuser@host $ sudo -u root -H chef-apply -s -l debug
execute "env" do
command "env"
end
...snip...
HOME=/root
...snip...

### Expected Result:
The docker image to be pulled

### Actual Result:
The docker pull command errors with a "not found" error (in this case, indicating a docker repository that the user does not have access to)