您可以在 Vagrantfile 的顶部添加以下内容,VALIDATE_ACTIONS
并使用您希望需要 VM 的操作进行更新。vagrant destroy
请注意,如果只定义了一个 VM ,这仍将允许该命令(没有指定的 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