简体   繁体   English

结构-以其他用户身份运行,但非sudo

[英]Fabric - run as an a different user, but non-sudo

I want to do 我想要做

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

using fabric. 使用面料。

If I do 如果我做

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

I get 我懂了

The directory '/home/coby/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. 目录“ /home/coby/.cache/pip/http”或其父目录不属于当前用户,并且缓存已被禁用。 Please check the permissions and owner of that directory. 请检查该目录的权限和所有者。 If executing pip with sudo, you may want sudo's -H flag. 如果使用sudo执行pip,则可能需要sudo的-H标志。

followed by an exception with 跟一个异常

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 works. UPD: pip install psycopg2 gunicorn --no-cache-dir有效。

In shell you run source after sudo which is right because source is a shell builtin that affects the current environment. 在shell中,在sudo之后运行source是正确的,因为source是影响当前环境的内置shell。

You have to do the same with fabric: run sudo() and call source inside it, not outside. 您必须对fabric执行相同的操作:运行sudo()并在其内部而不是外部调用source

Your current fab code is equivalent to 您当前的工厂代码等同于

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM