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).