Class: Vagrant::Faster::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/faster/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Action

Returns a new instance of Action.



4
5
6
# File 'lib/vagrant/faster/action.rb', line 4

def initialize(app, env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant/faster/action.rb', line 8

def call(env)
  vm = env[:vm] || env[:machine]

  cpus, mem = optimal_settings
  if cpus > 0 && mem > 0
    env[:ui].warn "[vagrant-faster] Setting CPUs: #{cpus}, Memory: #{mem}"
    vm.provider_config.customizations << ["pre-boot", ["modifyvm", :id, "--memory", mem]]
    vm.provider_config.customizations << ["pre-boot", ["modifyvm", :id, "--cpus", cpus]]
  else
    env[:ui].warn "[vagrant-faster] was unable to detect optimal settings for your machine"
  end

  @app.call env
end

#optimal_settingsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant/faster/action.rb', line 23

def optimal_settings
  host = RbConfig::CONFIG['host_os']

  cpus = -1
  mem = -1

  begin
    if host =~ /darwin/
      cpus = `sysctl -n hw.ncpu`.to_i
      mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024
    elsif host =~ /linux/
      cpus = `nproc`.to_i
      mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024
    elsif host =~ /mswin|mingw|cygwin/
      cpus = `wmic cpu Get NumberOfCores`.split[1].to_i
      mem = `wmic computersystem Get TotalPhysicalMemory`.split[1].to_i / 1024 / 1024
    end

    # Give VM 1/4 system memory & access to half of cpu cores on the host
    cpus = cpus / 2 if cpus > 1
    mem  = mem / 4  if mem > 2048
  rescue; end

  [ cpus, mem ]
end