tehK
tehK

Reputation: 2355

How to make virtual boxes in Vagrant optional?

i got a small problem with Vagrant.
In the Vagrant file, we defined a list of several virtual boxes that basically use the same configuration and are set up by vagrant:

boxes = [
{
  :name => "box1",
  :vmnumber => "1",
  :mem => "256",
  :cpu => "1"
},
{
  :name => "box2",
  :vmnumber => "6",
  :mem => "256",
  :cpu => "1"
},
...
]

... and in the same format a second list called boxes_optional. When calling vagrant up, for example, vagrant loops both lists and starts the VMs. So far, so good.

My problem is that I want to give the user the option to not start the boxes in boxes_optional. As far as I know you can't add more parameters to the vagrant up command and you can only query environment variables via ENV[variable], but that's kind of inconvenient.

Is there mybe another more elegant way of making those boxes optional? I'm quite new to Vagrant and Ruby and I haven't been able to google a nice solution, so I'm basically looking for ideas here.

Thanks and best regards,

/tehK

Upvotes: 1

Views: 152

Answers (2)

Eugene Petrov
Eugene Petrov

Reputation: 1598

updated answer

Played a little bit with vagrant, was almost writing a plugin, but found out in its source that it actually not so strict on its arguments if they go before command and prepended with dash or double dash(to differ from command).

So you need to call it as follows:

$ vagrant --without-optional-boxes up

# or, works too: #

$ vagrant -without-optional-boxes up

And now it will not bark on unknown argument while you are still able to get it from ARGV:

boxes += optional_boxes unless (ARGV[0] == "--without-optional-boxes")

Upvotes: 2

harald
harald

Reputation: 6126

You should be able to something like this:

boxes += optional_boxes if ENV['START_OPTIONAL_BOXES']

boxes.each |box| do
  # start box here
end

That way you will merge both lists into one if the START_OPTIONAL_BOXES env var is set, and only start the mandatory boxes if not.

Upvotes: 0

Related Questions