Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally provision ("vagrant provision") on "vagrant up"

Since I work in a team, I want to make the process of working with vagrant (and chef-solo) as smooth as possible.
When someone in the team changes the chef cookbooks, roles or whatever, I want all the other team members to automatically "receive" the changes by making vagrant re-provision (like running "vagrant provision") on the next "vagrant up", without every team member needing to know about it and re-provision manually.

I thought of accomplishing this by having the git post-receive hook script touch a file on the root project folder. Then, when "vagrant up" is called, it will check for this file existence. If the file exists - it will re-provision once the "up" is finished running.

Is there a better option of doing this? How?
If not, how can I do this ?

I know this question (Force Vagrant to re-provision or download a new box on next vagrant up) exists, but it's not exactly my situation (I'm interested specifically in provisioning, and less in the virtualbox machine itself - box, etc..).

like image 931
Doron Avatar asked Jun 17 '14 09:06

Doron


2 Answers

Vagrant supports adding provisioners which run on every vagrant up, by adding :run => "always" to the provision line.

You could add a provisioner which checks if any changes need to be made, then runs them. (Or, if the changes are repeatable, just runs them every time).

"Run Always" is documented here: https://docs.vagrantup.com/v2/provisioning/basic_usage.html

like image 61
Ben XO Avatar answered Oct 01 '22 19:10

Ben XO


You could add something like the following to the top of your Vagrantfile:

# On vagrant up check that we have the latest repo changes
if ARGV[0] == "up" && ARGV.join(" ") != "up --no-provision"
  gs = `git status` # command to check repo status
  gs.scan(/.*Your branch is behind.*/m) { |mtch| # check for out of date message
    puts "Your Vagrant files are out of date... fetching..."
    `git pull origin master` # command to fetch newest files.
    exec "vagrant up --no-provision;vagrant provision" # Re-run vagrant up and provision
  }
end

Assuming you use Git to store all the Vagrant files and cookbooks etc, this should run a Git status (it may need a fetch as well) and check the message for any hint that the files are out of date... if they are out of date it will pull the latest files and then re run vagrant up with a --no-provision before running a vagrant provision to re-provision the server.

like image 23
Matt Cooper Avatar answered Oct 01 '22 18:10

Matt Cooper