禁用框中的Vagrant globbing

时间:2013-07-23 08:29:51

标签: vagrant

有没有办法禁用“vm globbing”?也就是说,如果我在Vagrant文​​件中定义了两个vm,:dev:prod,并且我运行vagrant reload,我希望vagrant拒绝执行命令,因为我没有指定一个框。

为清楚起见,我的设置如下:

Vagrant.configure("2") do |config|
  config.berkshelf.enabled = true

  config.vm.define :dev do |dev|
  end

  config.vm.define :prod do |prod|
  end
end

1 个答案:

答案 0 :(得分:2)

您可以在Vagrant文​​件的顶部添加以下内容,使用您要求VM的操作更新VALIDATE_ACTIONS。请注意,如果只定义了一个VM,这仍然允许命令vagrant destroy(没有指定的VM)。

# Actions to validate
VALIDATE_ACTIONS = [ 'halt', 'destroy' ]

# Override default actions to check machine list
class ValidateCommand < Vagrant.plugin(2, :command)
  class << self
    attr_accessor :delegate_action
  end

  def initialize argv, env
    super(argv, env)
    @argv = argv
  end

  def execute
    vms = []
    with_target_vms(@argv, :reverse => true) do | machine |
      vms << machine
    end
    if vms.size > 1
      puts 'Please specify a single VM'
      1
    else
      with_target_vms(@argv, :reverse => true) do | machine |
        machine.action(ValidateCommand.delegate_action)
      end
      0
    end
  end
end

# Wrap validate command in plugin and invoke for overridden actions
class ValidatePlugin < Vagrant.plugin(2)
  name "Validate"
  VALIDATE_ACTIONS.each do | action |
    command action do
      ValidateCommand.delegate_action = action
      ValidateCommand
    end
  end
end
相关问题