Search code examples
rubypluginsdevelopment-environmentprovisioningvagrant

Specify Vagrantfile path explicity, if not plugin


Is there any way to explicity specify the path of a Vagrantfile? My company wants to do something like this: For testing on a confluence machine, type a command like vagrant spinup confluence, and then point that to a Vagrantfile in a different directory that contains the confluence environment, and then brings up all of these machines.

However, it doesn't look like there is any way to explicitly state what Vagrantfile to use, and I'm somewhat (very) new at ruby, so I'm having a hard time writing my own plugin for it. Does anyone have recommendations on what to do? Or has anyone done something similar to this?


Solution

  • There is no need to have a separate Vagrantfile, you can just define multiple VM's in the same file. See the documentation here: http://docs.vagrantup.com/v2/multi-machine/index.html

    If you are just using one VM in your 'normal' environment and one VM for your 'confluence' environment then it is simply a case of just defining each VM and vagrant up-ing the specific VM.

    If you have multiple machines that make up each of your environments then you have two options, you can use regular expressions and make sure you name and type the commands correctly or you can put a bit of logic into your Vagrantfile to make it easier for people.

    For example with a little bit of a hack in your Vagrantfile you can do the following:

    Vagrant.configure('2') do |config|
    
        if ARGV[1] == 'confluence'
            ARGV.delete_at(1)
            confluence = true
        else
            confluence = false
        end
    
        config.vm.provider :virtualbox do |virtualbox, override|
    
            #virtualbox.gui = true
    
            virtualbox.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
            virtualbox.customize ["modifyvm", :id, "--memory", 512]
    
            override.vm.box = 'Ubuntu 12.10 x64 Server'
            override.vm.box_url = 'http://goo.gl/wxdwM'
    
        end
    
        if confluence == false
    
            config.vm.define :normal1 do |normal1|
    
                normal1.vm.hostname = 'normal1'
                normal1.vm.network :private_network, ip: "192.168.1.1"
    
            end
    
            config.vm.define :normal2 do |normal2|
    
                normal2.vm.hostname = 'normal2'
                normal2.vm.network :private_network, ip: "192.168.1.2"
    
            end
    
        end
    
        if confluence == true
    
            config.vm.define :confluence1 do |confluence1|
    
                confluence1.vm.hostname = 'confluence1'
                confluence1.vm.network :private_network, ip: "192.168.1.3"
    
            end
    
            config.vm.define :confluence2 do |confluence2|
    
                confluence2.vm.hostname = 'confluence2'
                confluence2.vm.network :private_network, ip: "192.168.1.4"
    
            end
    
        end
    
    end
    

    Now vagrant up brings up your normal vm's and vagrant up confluence brings up your confluence vm's!