Is it possible to define what happens prior to vagrant destroy?

一个人想着一个人 提交于 2020-01-15 06:50:33

问题


I have a Vagrantfile that clones the required cookbook repos on vagrant up. The problem is, it also does this on vagrant destroy. Is there a way to define 'pre-construction' and 'pre-destruction' commands or does the Vagrantfile just get run in totality each time vagrant is invoked regardless of the parameters?


回答1:


I don't know about a destroy hook, but you can detect first time and post-destoy actions in the Vagrantfile by checking the existence of the machine ID:

File.exists?(".vagrant/machines/default/virtualbox/id")

I use this to drop in the public key for the machine, which allows me to setup a secure SSH key automatically:

if ! File.exists?(".vagrant/machines/default/virtualbox/id")
    # Then this machine is brannd new.
    system "cp #{KEYS_DIR}/workstation.pub #{OUR_DIR}"
end



回答2:


Instead of writing your own cookbook dependency manager I strongly suggest using Berkshelf or librarian. Both have plugins for Vagrant which already have the functionality you're looking for.

I started using librarian (simpler, focuses on doing one thing well), but have switched to Berkshelf (it's like adding extra blades to my knife command).



来源:https://stackoverflow.com/questions/18924691/is-it-possible-to-define-what-happens-prior-to-vagrant-destroy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!