itsadok
itsadok

Reputation: 29342

How to check for a precondition when starting a vagrant machine?

I have a multi-machine Vagrantfile, and I would like to do a quick check when the user runs vagrant up machineB, and exit with an error message if it fails.

The specific test I have in mind is to curl some URL and verify a 200 response, but I don't think the details should matter.

The idea is to save the user the time it takes to start the machine, sync some folders and run the provision script, only to discover that a required resource is not available.

So far the best idea I have is to put this check at the beginning of the provision script, but I'd like to do it earlier.

I know I can just check at the beginning of the Vagrantfile, kind of like how it is done here, but then the check will run on every vagrant command, and I'd like it to run specifically only when trying to start machineB.

Upvotes: 1

Views: 198

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53793

  1. You can run your condition in the specific block for your machineB so it will run only when you call commands for machineB

  2. You can check ARGV[0] argument from command line to make sure it is up

this will look something

Vagrant.configure("2") do |config|

  config.vm.box = xxx
  config.ssh.username = xxx

  config.vm.define "machineA" do |db|
    db.vm.hostname = xxx
    p system ("curl http://www.google.fr")
    and your condition here
  end

  config.vm.define "machineB", primary: true do |app|
    app.vm.hostname = xxx

    if "up".eql? ARGV[0]
      p system ("curl http://www.google.fr")
      and your condition here
    end
  end

not sure exactly what you want to end up with the curl but you could just use the net/http lib

Upvotes: 1

Related Questions