提问人:runningraptor 提问时间:8/14/2023 更新时间:8/14/2023 访问量:34
如何在 Chef 配方中为 python 设置虚拟环境?
How do I set up a virtual env for python in Chef recipe?
问:
目标是将存储库克隆到要运行 Python 脚本的计算机。我需要安装要求。但是,我收到以下警告:
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
以下是我试图摆脱警告的尝试,但我收到以下错误:
had an error: Errno::ENOENT: No such file or directory - /var/log/virtual-environment/bin/pip
我不确定如何修改代码来解决此问题?
# Cookbook: chefcookbookname
# Attribute file: attributes/default.rb
default['chefcookbookname']['venv_path'] = '/var/log/virtual-environment'
# Cookbook:: chefcookbookname
# Recipe:: default
package 'git'
package 'python3-pip'
package 'python3.10-venv'
directory '/var/log/tmp' do
action :create
recursive true
end
directory '/var/log/myrepo' do
action :create
recursive true
end
directory '/var/log/virtual-environment' do
action :create
recursive true
end
venv_path = node['chefcookbookname']['venv_path']
directory venv_path do
action :create
end
execute 'create_virtualenv' do
command "python3 -m venv #{venv_path}"
not_if { ::File.exist?(venv_path) }
end
git 'clone_repository' do
repository 'https://repo.git'
reference 'main'
destination '/var/log/myrepo'
action :sync
end
execute 'move_requirements' do
command "mv /var/log/myrepo/requirements.txt #{venv_path}"
action :run
only_if { ::File.exist?("/var/log/myrepo/requirements.txt") }
end
execute 'install_python_dependencies' do
command "#{venv_path}/bin/pip install -r #{venv_path}/requirements.txt"
only_if { ::File.exist?("#{venv_path}/requirements.txt") }
environment(
'VIRTUAL_ENV' => venv_path,
'PATH' => "#{venv_path}/bin:#{ENV['PATH']}"
)
end
答:
0赞
Emmanuel Iturbide
11/25/2023
#1
execute 命令可以采用名为“user”的属性,该属性是用于启动新进程的用户标识的用户名。
例如,如果您的用户被调用foo
execute "python3 -m venv #{source_path}/virtualenv" do
cwd '/path/where/to/create/venv'
user 'foo'
not_if { ::Dir.exist?("#{source_path}/virtualenv") }
action :run
end
评论