在 Vagrant 配置期间更新 .bashrc 和环境变量 [英] Updating .bashrc and environment variables during Vagrant provisioning

查看:34
本文介绍了在 Vagrant 配置期间更新 .bashrc 和环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Vagrant 设置一个包含 python、pip、virtualenv、virtualenvwrapper 和一些要求的盒子.配置 shell 脚本将 virtualenvwrapper 所需的行 添加到 <代码>.bashrc.它会进行非常基本的检查,以确保它们尚未存在,因此不会在每个条款中都复制它们:

I'm using Vagrant to set up a box with python, pip, virtualenv, virtualenvwrapper and some requirements. A provisioning shell script adds the required lines for virtualenvwrapper to .bashrc. It does a very basic check that they're not already there, so that it doesn't duplicate them with every provision:

if ! grep -Fq "WORKON_HOME" /home/vagrant/.bashrc; then
    echo 'export WORKON_HOME=/home/vagrant/.virtualenvs' >> /home/vagrant/.bashrc
    echo 'export PROJECT_HOME=/home/vagrant/Devel' >> /home/vagrant/.bashrc
    echo 'source /usr/local/bin/virtualenvwrapper.sh' >> /home/vagrant/.bashrc

    source /home/vagrant/.bashrc
fi

似乎工作正常;配置完成后,这些行在 .bashrc 中,我可以通过 ssh 连接到盒子并使用 virtualenvwrapper.

That seems to work fine; after provisioning is finished, the lines are in .bashrc, and I can ssh to the box and use virtualenvwrapper.

但是,virtualenvwrapper 在配置期间不起作用.在上述部分之后,接下来检查 pip 要求文件并尝试使用 virtualenvwrapper 进行安装:

However, virtualenvwrapper doesn't work during provisioning. After the section above, this next checks for a pip requirements file and tries to install with virtualenvwrapper:

if [[ -f /vagrant/requirements.txt ]]; then
    mkvirtualenv 'myvirtualenv' -r /vagrant/requirements.txt
fi

但这会产生:

==> default: /tmp/vagrant-shell: line 50: mkvirtualenv: command not found

如果我尝试从该 shell 脚本中回显 $WORKON_HOME,则不会出现任何内容.

If I try and echo $WORKON_HOME from that shell script, nothing appears.

让这些环境变量可用,以便运行 virtualenvwrapper,我缺少什么?

What am I missing to have those environment variables available, so virtualenvwrapper will run?

更新:进一步的尝试......似乎在我的shell脚本中做 source/home/vagrant/.bashrc 没有效果 - 我可以把 .bashrc 文件中回显 "hello" ,并且在配置期间不会输出(但是如果我运行 source/home/vagrant/.bashrc登录时.

UPDATE: Further attempts... it seems that doing source /home/vagrant/.bashrc has no effect in my shell script - I can put echo "hello" in the .bashrc file , and that isn't output during provisioning (but is if I run source /home/vagrant/.bashrc when logged in.

我也在 shell 脚本中尝试了 su -c "source/home/vagrant/.bashrc" vagrant 但这并没有什么不同.

I've also tried su -c "source /home/vagrant/.bashrc" vagrant in the shell script but that is no different.

更新 2: 删除了 $BASHRC_PATH 变量,这会混淆问题.

UPDATE 2: Removed the $BASHRC_PATH variable, which was confusing the issue.

更新 3:另一个问题 我得到了关于为什么 source/home/vagrant/.bashrc 不起作用的答案:第一部分.bashrc 文件阻止它在以这种方式非交互"运行时执行任何操作.

UPDATE 3: In another question I got the answer as to why source /home/vagrant/.bashrc wasn't working: the first part of the .bashrc file prevented it from doing anything when run "not interactively" in that way.

推荐答案

Vagrant 脚本供应器将以 root 身份运行,因此它的主目录 (~) 将是/root.在您的脚本中,如果您定义 BASHRC_PATH=/home/vagrant,那么我相信您的步骤会起作用:附加到/home/vagrant/.bashrc,然后从/home/vagrant/.bashrc 获取.

更新:

从我之前的想法开始^^ 因为 BASHRC_PATH 已经正确设置.

Scratching my earlier idea ^^ because BASHRC_PATH is already set correctly.

作为替代,我们可以使用 .profile 或 .bash_profile.这是一个设置环境变量 FOO 的简化示例,使其在配置期间和 ssh 登录后可用:

As an alternative we could use .profile or .bash_profile. Here's a simplified example which sets environment variable FOO, making it available during provisioning and after ssh login:

流浪文件

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise32"

  $prov_script = <<SCRIPT
if ! grep -q "export FOO" /home/vagrant/.profile; then
  sudo echo "export FOO=bar" >> /home/vagrant/.profile
  echo "before source, FOO=$FOO"
  source /home/vagrant/.profile
  echo "after source, FOO=$FOO"
fi
SCRIPT

  config.vm.provision "shell", inline: $prov_script
end

结果

$ vagrant up
...
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: before source, FOO=
==> default: after source, FOO=bar
$ vagrant ssh -c 'echo $FOO'
bar
$ vagrant ssh -c 'tail -n 1 ~/.profile'
export FOO=bar

这篇关于在 Vagrant 配置期间更新 .bashrc 和环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆