Fabric - 作为不同的用户运行,但非sudo

时间:2017-08-03 04:25:32

标签: python pip virtualenv sudo fabric

我想做

sudo su - some_user
source virtualenv/bin/activate
pip install psycopg2 gunicorn django-modeltranslation

使用面料。

如果我这样做

with sudo_settings(sudo_user="some_user"):
     with prefix("source virtualenv/bin/activate"):
         sudo("pip install psycopg2 gunicorn django-modeltranslation")

我得到了

  

目录' /home/coby/.cache/pip/http'或其父目录不归当前用户所有,并且已禁用缓存。请检查该目录的权限和所有者。如果用sudo执行pip,你可能需要sudo' -H标志。

后跟

的例外
Traceback (most recent call last):
File "/<home>/some_user/virtualenv/lib/python3.5/site-packages/pip/basecommand.py", line 215, in main
 status = self.run(options, args)
File "/<home>/some_user/virtualenv/lib/python3.5/site-packages/pip/commands/install.py", line 324, in run
 requirement_set.prepare_files(finder)
File "/<home>/some_user/virtualenv/lib/python3.5/site-packages/pip/req/req_set.py", line 380, in prepare_files
 ignore_dependencies=self.ignore_dependencies))
File "/<home>/some_user/virtualenv/lib/python3.5/site-packages/pip/req/req_set.py", line 554, in _prepare_file
 require_hashes
File "/<home>/some_user/virtualenv/lib/python3.5/site-packages/pip/req/req_install.py", line 281, in populate_link
 self.link = self._wheel_cache.cached_wheel(self.link, self.name)
File "/<home>/some_user/virtualenv/lib/python3.5/site-packages/pip/wheel.py", line 68, in cached_wheel
 self._cache_dir, link, self._format_control, package_name)
File "/<home>/some_user/virtualenv/lib/python3.5/site-packages/pip/wheel.py", line 129, in cached_wheel
 wheel_names = os.listdir(root)
PermissionError: [Errno 13] Permission denied: '/<home>/<sudo_user>/.cache/pip/wheels/cd/07/05/fa31adf8c1d71f5979e4329601d36c883f063ed0e99e5e29fa'

UPD: pip install psycopg2 gunicorn --no-cache-dir有效。

1 个答案:

答案 0 :(得分:0)

在shell中,您在source之后运行sudo,这是正确的,因为source是一个影响当前环境的shell内置。

您必须对结构做同样的事情:运行sudo()并在其中调用source,而不是在外面。

您当前的fab代码相当于

source virtualenv/bin/activate
sudo su - some_user
pip install psycopg2 gunicorn