使用Salt的virtualenv.managed或pip.installed设置Python虚拟环境?

4 投票
1 回答
4276 浏览
提问于 2025-04-18 01:23

我正在尝试使用 pip.installedvirtualenv.managed 来设置一个虚拟环境,这个虚拟环境是通过一个之前用 git.latest 安装的需求文件来创建的。下面是使用 pip.installed 的版本:

mysite-env:
  virtualenv.managed:
    - name: /opt/django/mysite
    - cwd: /opt/django/mysite
    - user: mysite
    - runas: mysite
    - system_site_packages: True

mysite-reqs:
  pip.installed:
    - no_index: True
    - find_links: http://pypi.mysite.com/
    - requirements: /opt/django/mysite/requirements/production.txt
    - pip_exists_action: switch
    - cwd: /opt/django/mysite
    - pip_bin: /opt/django/mysite/bin/pip
    - bin_env: /opt/django/mysite
    - user: mysite
    - watch:
      - git: mysite-project

当我尝试运行这个状态时,我遇到了这个错误:

          ID: mysite-reqs
    Function: pip.installed
      Result: False
     Comment: Unable to process requirements file /opt/django/mysite/requirements/production.txt. Error: Ignoring indexes: https://pypi.python.org/simple/
              Could not open requirements file: [Errno 2] No such file or directory: '/tmp/requirements.txt'
              Storing debug log for failure in /home/mysite/.pip/pip.log

但我传递给需求的路径并不是这个。这看起来有点像是个bug,可能是因为 /tmp/requirements.txt 是硬编码的,它期望我使用 salt:// 的网址,而不是本地文件路径。

我是不是做错了什么?这是个bug吗?有没有好的解决办法?

1 个回答

4

今天是我第一次使用Saltstack和Python,我在网上搜索环境配置的帮助时,发现了你的问题。

我点击的下一个搜索结果也出现了和你一样的错误,建议这个问题是因为SaltStack把环境移动到了/tmp目录进行权限更改。

解决办法是把以下内容添加到你的salt状态中:

no_chown: True

希望这能帮到你。

撰写回答