How to run ruby script based on a specific provider in Vagrantfile ?

527 views
Skip to first unread message

zach...@gmail.com

unread,
Mar 27, 2014, 7:28:53 AM3/27/14
to vagra...@googlegroups.com
Hi,
 
I've a single Vagrantfile that is used for multiple providers (e.g., VirtualBox, Softlayer, etc).
 
1. I would like to call a different ruby script (or ruby function) for each profvider from the Vagrantfile. How I can check the provider in the Vagrantfile ?
 
2. In addition, I would like to run the ruby script if and only if the provision flag is set. That is, if vagrant is NOT executed with the --no-provision flag. How I can check the provision flag in the Vagrantfile ?
 
Thanks In Advance,
 
-- Idan Zach

Alvaro Miranda Aguilera

unread,
Mar 27, 2014, 7:53:41 PM3/27/14
to vagra...@googlegroups.com
hello,

for q1, I have seen things like this:

config.vm.provider :vmware_fusion do |vb, override|
end
perhaps you can test inside a data block like that?

for 2, you mean ruby script as  script.rb? if yes, then the shell inline will be executed only when the machine is provisioned

you can copy the script into the guest, and execute it like:
config.vm.provision :file, :source => "/path/myawesomescript", 
:destination => "/myawesomescript"
config.vm.provision :shell, :inline => "ruby /myawesomescript"

Alvaro.


--
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zach...@gmail.com

unread,
Mar 30, 2014, 3:14:45 AM3/30/14
to vagra...@googlegroups.com
Hello, Alvaro,
 
Thanks for your response, but both options don't work for me:
 
1. Even if you call a ruby function/command inside the provider-specific block, the function/command is executed (and multiple times) for any provider.
 
   # VirtualBox provider-specific configuration
   server.vm.provider :virtualbox do |vb|
      vb.landrush.enable
      puts "Hello: my provider is VirtualBox"
   end
   In the example below, the message "Hello: my provider is VirtualBox" is printed a few times even if I start vagrant with the following command:
   vagrant up --provider softlayer
 
2. Here, I want to run the ruby script (or ruby function) on the local machine and NOT on the guest machine, thus your idea does not work for me.
 
Actually, I want to run a ruby script which prepares a dynamic file in the sync directory for the provision script. Thus, I want to run the script locally just before the provisioning.
 
 
 
Regards,
 
-- Idan

Alvaro Miranda Aguilera

unread,
Mar 30, 2014, 5:45:41 PM3/30/14
to vagra...@googlegroups.com
Hello there,

got it, on the host.

for the multiple running thing, which if found also, I did a dirty trick.

Icreated a variable:

#variable used to provide information only once
give_info ||=true

then inside the provider :virtualbox block i did

            unless give_info==false
              puts "on first boot shared disks will be created, this will take some time"
              give_info=false

to execute that only once every time the Vagrant file is readed.

As for the provision, I am not sure how check how to know if we are in a provision call or no..

We may want to look at the provisioner code.. what ever you find, please report back!

Alvaro.

Alvaro Miranda Aguilera

unread,
Mar 30, 2014, 5:58:21 PM3/30/14
to vagra...@googlegroups.com
Hello there, seems I find something you can test.

This is the commint when --no-provision was enabled.

https://github.com/mitchellh/vagrant/commit/1770ad1ee5c45eb46c0d1d70e9ec8ffb3bbc48f6

so, can you check env["provision.enabled"] ?

Here someone whote a custom provider, that may help you write something you need

https://github.com/mitchellh/vagrant/issues/591



The magic:

  14
+          # We set this here so that even if this value is changed in the future,
  15
+          # it stays constant to what we expect here in this moment.
  16
+          enabled = env["provision.enabled"]
  17
+          if enabled
  18
+            # Instantiate and prepare the provisioners. Preparation must happen here
  19
+            # so that shared folders and such can properly take effect.
  20
+            provisioners = enabled_provisioners
  21
+            provisioners.map { |p| p.prepare }
  22
+          end

Reply all
Reply to author
Forward
0 new messages