我想在做流浪汉破坏后执行自定义ruby代码。
代码:
class OnDestroyMiddleware
def initialize(app, env)
@app = app
end
def call(env)
env["config"].vm.provisioners.each do |provisioner|
env.ui.info "Attempting to remove client #{provisioner.config.node_name}"
`knife client show #{provisioner.config.node_name}`
if $?.to_i == 0
env.ui.info "Removing client #{provisioner.config.node_name}"
`knife client delete #{provisioner.config.node_name} -y`
end
env.ui.info "Attempting to remove node #{provisioner.config.node_name}"
`knife node show #{provisioner.config.node_name}`
if $?.to_i == 0
env.ui.info "Removing node #{provisioner.config.node_name}"
`knife node delete #{provisioner.config.node_name} -y`
end
end
@app.call(env)
end
end
我试过这个但是没有工作:
Vagrant::Action[:destroy].use(OnDestroyMiddleware)
所有这些代码都来自https://gist.github.com/skippy/1010660
我在Vagrant :: Action [:destroy]
上遇到了这个错误消息:NoMethodError:未定义的方法`[]'对于Vagrant ::动作:模块
答案 0 :(得分:2)
您可以查看vagrant trigger plugin。它允许您在特定的vagrant命令之后运行代码。
首先你需要安装插件
$ vagrant plugin install vagrant-triggers
您将直接在Vagrantfile中添加代码,例如
Vagrant.configure("2") do |config|
# Your existing Vagrant configuration
...
# run some script before the guest is destroyed
config.trigger.after :destroy do
info "Attempting to remove client..."
run "knife client show .."
end