Is it possible to execute ruby code inside a Vagrantfile?

1,758 views
Skip to first unread message

Luis Mayorga

unread,
Mar 20, 2014, 4:25:16 PM3/20/14
to vagra...@googlegroups.com
I need to detect which platform is running vagrant. (We have vagrant windows/mac users) and need to setup some initial variable values for some of the configuration parameters.

Terrance Shepherd

unread,
Mar 20, 2014, 4:32:06 PM3/20/14
to vagra...@googlegroups.com
Yes, just put in the code.


On Thu, Mar 20, 2014 at 4:25 PM, Luis Mayorga <lmayor...@gmail.com> wrote:
I need to detect which platform is running vagrant. (We have vagrant windows/mac users) and need to setup some initial variable values for some of the configuration parameters.

--
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.

Jeremy Voorhis

unread,
Mar 20, 2014, 4:32:32 PM3/20/14
to vagra...@googlegroups.com
The Vagrantfile is just a Ruby file that gets evaluated within a special context that implements the Vagrant DSL. You can definitely execute arbitrary Ruby code to specialize your VM.


On Thu, Mar 20, 2014 at 1:25 PM, Luis Mayorga <lmayor...@gmail.com> wrote:
I need to detect which platform is running vagrant. (We have vagrant windows/mac users) and need to setup some initial variable values for some of the configuration parameters.

--
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.



--
Jeremy Voorhis

Luis Mayorga

unread,
Mar 20, 2014, 4:50:21 PM3/20/14
to vagra...@googlegroups.com
This worked!!!

if Gem.win_platform?

Thanks


--
You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/lhd9_WfySXo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagrant-up+...@googlegroups.com.

Alvaro Miranda Aguilera

unread,
Mar 20, 2014, 6:28:39 PM3/20/14
to vagra...@googlegroups.com
in one Vagrantfile I found on github,. they use this.

      if RUBY_PLATFORM.include? 'linux'
          b.vm.network :bridged, :bridge => "eth0"
      elseif RUBY_PLATFORM.include? 'darwin'
          b.vm.network :bridged, :bridge => "en1: Wi-Fi (AirPort)"
      end

I assume, you can check if there is a in/windows or something.

Alvaro.

blong

unread,
Sep 25, 2014, 11:12:28 AM9/25/14
to vagra...@googlegroups.com
I'm new to Ruby, so pardon my ignorance, but is there a way to load rubygems and require additional libraries?  For example, let's say I wanted to assign %25 of the CPU cores to Vagrant.  I need something that's platform independent, I was thinking something like this:

    require 'rubygems'
   
require 'facter'
   
Facter.loadfacts
   
    config
.vm.provider "virtualbox" do |v|
     
# Give VM 25% of system cpu cores
      v
.customize ["modifyvm", :id, "--cpus", [((Facter.processorcount) / 4).to_i, 1].max ]
   
end

Any thoughts on how I can achieve this?

Thanks,
blong

Alvaro Miranda Aguilera

unread,
Sep 25, 2014, 6:33:04 PM9/25/14
to vagra...@googlegroups.com
Hello,

Try the attached on top of your Vagrantfile.

Vagrant includes his own ruby and some gems, I did copy this from celluloid/cpucount, and tested on OSX.

then, on the config block you can do something like this:


config.vm.provider "virtualbox" do |v|
     
# Give VM 25% of system cpu cores
      if @cores
        v.customize ["modifyvm", :id, "--cpus", @cores/4.to_i ]
      end
    end

cores.txt

blong

unread,
Sep 26, 2014, 9:24:38 AM9/26/14
to vagra...@googlegroups.com
Hi Alvaro,

Thanks for your suggestion.  Is there a place where documentation for variables like @cores
is available?  I tried something similar, per your suggestion (below), however the output is always : "Unable to detect CPU cores, will assign 1 CPU to the VM".

    # Give VM 25% of system cpu cores, with a minimum of 1
   
if @cores
      puts
"Detected " + @cores + " CPU cores"
      v
.customize ["modifyvm", :id, "--cpus", max([@cores/4.to_i, 1]) ]
   
else
      puts
"Unable to detect CPU cores, will assign 1 CPU to the VM"
   
end

Thanks again,
blong

Alvaro Miranda Aguilera

unread,
Sep 26, 2014, 4:41:59 PM9/26/14
to vagra...@googlegroups.com
did you put the code I sent attached to the email to the beggining to vagrantfile?

@code is defined/created in the ruby code i sent





# -*- mode: ruby -*-
# vi: set ft=ruby : 

require 'rbconfig'  

case RbConfig::CONFIG['host_os'][/^[A-Za-z]+/]  
when 'darwin'   
  @cores = Integer(`/usr/sbin/sysctl hw.ncpu`[/\d+/])   
when 'linux'
  @cores = if File.exists?("/sys/devices/system/cpu/present")   
File.read("/sys/devices/system/cpu/present").split('-').last.to_i+1 
  else  
Dir["/sys/devices/system/cpu/cpu*"].select { |n| n=~/cpu\d+/ }.count
  end   
when 'mingw', 'mswin'   
  @cores = Integer(ENV["NUMBER_OF_PROCESSORS"][/\d+/])  
else
  @cores = nil  
end 

puts @cores 

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2" 
cores.txt

blong

unread,
Sep 28, 2014, 10:20:18 PM9/28/14
to vagra...@googlegroups.com
Ah, I did not (at first).  Thanks for pointing that out, I completely missed the attachment earlier.  This worked perfectly for me after declaring the "@cores" variable!

Jamie Jackson

unread,
Oct 3, 2014, 3:06:36 PM10/3/14
to vagra...@googlegroups.com, lmayor...@gmail.com
FWIW, here's how I'm detecting OS and bitness: https://gist.github.com/jamiejackson/39787215b5fc6ed22e7e
Reply all
Reply to author
Forward
0 new messages