Updating .bashrc and Environment Variables During Vagrant Backup

I use Vagrant to create boxes with python, pip, virtualenv, virtualenvwrapper and some requirements. The reverse shell script adds the necessary lines for virtualenvwrapper to .bashrc . He does a very basic check that they are not there so that they do not duplicate them with each position:

 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 

This seems to work fine; after initialization is complete, the lines are in .bashrc , and I can ssh in the field and use virtualenvwrapper.

However, during initialization, virtualenvwrapper does not work. After the section above, the following example checks the pin requirements file and tries to install it using virtualenvwrapper:

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

But this begets:

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

If I try and echo $WORKON_HOME from this shell script, nothing will appear.

What am I missing to have these environment variables so that virtualenvwrapper will work?

UPDATE: Further attempts ... it seems that executing source /home/vagrant/.bashrc does not affect my shell script - I can put echo "hello" in the .bashrc , and this isn the output during preparation (but if I ran source /home/vagrant/.bashrc at login.

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

UPDATE 2: Removed the $BASHRC_PATH variable, which confused the problem.

UPDATE 3: One more question . I got an answer about why source /home/vagrant/.bashrc didnโ€™t work: the first part of the .bashrc file did not allow it to do anything when launched โ€œnon-interactivelyโ€ in this way.

+3
python shell vagrant virtualenvwrapper
source share
4 answers

The script firewall will execute as root, so home dir (~) will be / root. In your script, if you define BASHRC_PATH = / home / vagrant, then I believe that your actions will work: adding and then searching from /home/vagrant/.bashrc.

Update:

Scratch of my previous idea ^^, because BASHRC_PATH is already configured correctly.

Alternatively, we could use .profile or .bash_profile. Here's a simplified example that sets the FOO environment variable, making it available during preparation and after ssh login:

Vagrantfile

 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 

results

 $ 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 
+2
source share

I found a solution, but I don't know if this is the best. He feels a little wrong repeating things, but ...

I am still adding these lines to .bashrc , so virtualenvwrapper will work if I ssh into the machine. But, since source /home/vagrant/.bashrc is ineffective while the script is running, I have to explicitly repeat these three commands:

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

(As an aside, I also realized that during wandering around $HOME is /root , and not /home/vagrant , which I assumed.)

+2
source share

The .bashrc byte in the Ubuntu field does not work. You should create .bash_profile and add:

 if [ -f ~/.bashrc ]; then . ~/.bashrc fi 
0
source share

As mentioned in your other Q , Vagrant forbids interactive shells during preparation - apparently only for some mailboxes (you need to reference this though). For me, this affects the official fields of Ubuntu Trusty and Xenial.

However, you can simulate the interactive bash shell using sudo -H -u USER_HERE bash -i -c 'YOUR COMMAND HERE'

Reply from: fooobar.com/questions/415051 / ...

This helped me install Ruby via rbenv and Node via nvm when setting up the Ubuntu / trusty64 and xenial64 fields.

0
source share

All Articles