Vagrant + Chef : How to execute a task totally in the context of another user?


#1

Hi,

After I checked out a git repo, I want to run a python script inside the
repo for a user account ‘abc’. It will install a ‘.project’ file in
/home/abc.

Here is how I define the task:

bash “install_project” do

cwd “/home/abc/apps/project”

user "abc"

code <<-EOH

 sudo -u abc python ./install.py

 EOH

only_if { ::File.exists?("/home/abc/apps/project") }

end

However it fails with a permission exception:
OSError: [Errno 13] Permission denied: ‘/home/vagrant/.project’

Apparently the command is executed in the context of vagrant

I have tried a few alternative commands, but they all failed with the
permission problem e.g.

$ sudo -u abc -s bash -i -c “/usr/bin/python ./install.py”

Installing “project”…

Traceback (most recent call last):

File “./install.py”, line 67, in

shutil.copytree('template', dest)

File “/usr/lib/python2.7/shutil.py”, line 177, in copytree

os.makedirs(dst)

File “/usr/lib/python2.7/os.py”, line 157, in makedirs

mkdir(name, mode)

OSError: [Errno 13] Permission denied: ‘/home/vagrant/.project’

How can I get the “python install.py” to run in the user ‘abc’ session?

Cheers, Tony