fnichol/chef-rbenv — pull request #73 by sds (2014-02-11 04:57:35+00:00)
Title: Install user-specific gems as user

When specifying a gem to be installed for a user, the created gem
directory and its files would be owned by root instead of the user.

This is due to the fact that the `install_via_gem_command` and
`uninstall_via_gem_command` methods defined by the upstream Chef repo
don't specify a `user` to run the `gem install` command as. This has
been reported by the issue in [1], but there has been no response after
many months.

Unfortunately, there isn't a standard way of getting around this without
either monkey patching those definitions to specify the `user` option
when calling `shell_out!`, or committing some invasive changes to the
upstream Chef repository (specifically the `GemEnvironment` class that
the `RbenvGemEnvironment` extends).

A simple (but perhaps unorthodox) way around this is to fork the process
and change its `uid` to the desired username before executing the
installation/uninstallation code. This is accomplished by defining a
`run_as_user` helper that takes a username and a block and executes that
block in a forked process as the given username.

It tastes awful, and it works.

[1] https://github.com/fnichol/chef-rbenv/issues/61