Vagrant complaining about settings (vmx, customize)

1k Views Asked by At

I'd really like to be able to run a single virtual machine on both a linux box and a mac box with no changes to the Vagrantfile. I've put together a YAML setup that I generally like to help configure the box (or later: boxes). I am having trouble running the Vagrantfile. Thanks in advance!

My invocation is:

vagrant up --provider=virtualbox

or

vagrant up --provider=vmware_fusion

YAML configuration file looks a bit like this:

CentOS64:
    ip: "192.168.33.110"
    properties:
        memory: 8192
        cpus: 4
    forwards:
        - host: 8080
          guest: 8080
        - host: 9115
          guest: 9115
    providers:
        virtualbox: "http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box"
        vmware_fusion: "https://dl.dropbox.com/u/5721940/vagrant-boxes/vagrant-centos-6.4-x86_64-vmware_fusion.box"
    provisions:
        pre:
            - provision: shell
              path: setup_users.sh
              privileged: true
            - provision: shell
              path: install_packages.sh
              privileged: true
            - provision: shell
              path: update_rcs.sh
              privileged: true
        post:
            ... installs application ...

Vagrant file looks a bit like this:

require 'yaml'

settings = YAML.load_file "vagrant_config.yml"
default_provider = ENV['VAGRANT_DEFAULT_PROVIDER']

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  settings.each do |name, cfg|
    config.vm.define name do |agent|
      agent.vm.network :private_network, ip: cfg['ip']
      cfg['forwards'].each do |port|
        agent.vm.network "forwarded_port", guest: port['guest'], host: port['host']
      end
      agent.vm.provider :vmware_fusion do |v, override|
        override.vm.box = name + "_fusion"
        override.vm.box_url = cfg['providers']['vmware_fusion']
        cfg['properties'].each do |prop, val|
          if prop == "cpus"
            prop = "numvcpus"
          elsif prop == "memory"
            prop = "memsize"
          end
          override.vmx[prop] = val
        end
      end
      agent.vm.provider :virtualbox do |v, override|
        override.vm.box = name + "_vb"
        override.vm.box_url = cfg['providers']['virtualbox']
        cfg['properties'].each do |prop, val|
          if prop == "numvcpus"
            prop = "cpus"
          elsif prop == "memsize"
            prop = "memory"
          end
          agent.vm.customize ["modifyvm", :id, "--#{prop}", "#{val}"]
        end
      end
      cfg['provisions'].each do |prov|
        agent.vm.provision prov['provision'] do |p|
          # setup keys that do not directly map
          if prov.has_key?('recipes')
            prov['recipes'].each do |r|
              p.add_recipe r
            end
            prov.delete('recipes')
          end
          # setup 1:1 mapping keys
          prov.each do |key, val|
            if key != 'provision'
              p.instance_variable_set("@#{key}", val)
            end
          end
        end
      end
      if cfg.has_key?("shares")
        cfg['shares'].each do |sname, share|
          share['host'] = File.expand_path(share['host'])
          agent.vm.synced_folder share['host'], share['guest'], owner: share['owner'], group: share['group']
        end
      end
    end
  end
end

I've been working on this a couple days now, and I am still not up and running satisfactorily. I'm crash coursing Ruby with this Vagrant file.

My current errors with the setup above are:

>>> vagrant destroy --force; vagrant up
VM must be created before running this command. Run `vagrant up` first.
Bringing machine 'CentOS64' up with 'vmware_fusion' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The following settings shouldn't exist: customize

Vagrant:
* Unknown configuration section 'vmx'.
1

There are 1 best solutions below

0
On BEST ANSWER

This vagrantfile fixes the issues:

require 'yaml'

settings = YAML.load_file "vagrant_config.yml"

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  settings.each do |name, cfg|
    config.vm.define name do |agent|
      agent.vm.box = name
      cfg['forwards'].each do |port|
        agent.vm.network "forwarded_port", guest: port['guest'], host: port['host']
      end
      agent.vm.provider :vmware_fusion do |v, override|
        v.box = name + "_fusion"
        v.box_url = cfg['providers']['vmware_fusion']
        cfg['properties'].each do |prop, val|
          if prop == "cpus"
            prop = "numvcpus"
          elsif prop == "memory"
            prop = "memsize"
          end
          v.vmx[prop] = val
        end
        v.vmx['fixed-address'] = cfg['ip']
      end
      agent.vm.provider :virtualbox do |v, override|
        override.vm.box = name + "_vb"
        override.vm.box_url = cfg['providers']['virtualbox']
        override.vm.network :private_network, ip: cfg['ip']
        cfg['properties'].each do |prop, val|
          if prop == "numvcpus"
            prop = "cpus"
          elsif prop == "memsize"
            prop = "memory"
          end
          v.customize ["modifyvm", :id, "--#{prop}", "#{val}"]
        end
      end
      cfg['provisions'].each do |prov|
        agent.vm.provision prov['provision'] do |p|
          # setup keys that do not directly map
          if prov.has_key?('recipes')
            prov['recipes'].each do |r|
              p.add_recipe r
            end
            prov.delete('recipes')
          end
          # setup 1:1 mapping keys
          prov.each do |key, val|
            if key != 'provision'
              p.instance_variable_set("@#{key}", val)
            end
          end
        end
      end
    end
  end
end