Vagrant抱怨设置(vmx,自定义)

时间:2014-12-05 02:07:40

标签: ruby yaml virtualbox vagrantfile vmware-fusion

我真的希望能够在Linux机器和mac机箱上运行单个虚拟机,而无需更改Vagrantfile。我已经整理了一个YAML设置,我通常喜欢帮助配置盒子(或以后:盒子)。我在运行Vagrantfile时遇到问题。提前谢谢!

我的调用是:

vagrant up --provider=virtualbox

vagrant up --provider=vmware_fusion

YAML配置文件看起来有点像这样:

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文​​件看起来有点像这样:

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

我已经在这几天工作了,而且我仍然没有满意地运转起来。我正在使用这个Vagrant文​​件崩溃Ruby。

上面设置的当前错误是:

>>> 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 个答案:

答案 0 :(得分:0)

此vagrant文​​件解决了以下问题:

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