Is it possible to define what happens prior to vag

2019-07-19 05:38发布

问题:

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



标签: chef vagrant