-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup Tasks for Shell Provisioner #2538
Comments
You mean you want a script to run on the host? |
Yes, that would be ideal. It is also entirely possible that I'm thinking about this backward and I haven't handled kill scripts correctly yet. |
A pre-destroy script kinda would be cool, so it would be possible to export data before the VM is destroyed. |
This would be great. I'm thinking something like:
For now, I'm looking at using https://github.com/emyl/vagrant-triggers |
I could use a way to call a guest side script (similar to provision) that was run as part of the destroy process.
vagrant-triggers is nice but it's a host side script. I'd like to use a guest side script. |
+1 for this. pre-destroy script to backup db of guest (in case one accidentally deletes the VM) |
I have files that are written to a synced folder (Drupal's settings.php file) during provisioning that, if they aren't deleted during |
Currently relying increasingly on vagrant-triggers but would really like to see a plugin-less option become available beyond Chef (we're using Ansible). |
Same as @thedavidmeister :
My pear files end up in a shared folder and need cleaning up before running |
Am I correct that this can be done today with the vagrant-triggers plugin? |
The vagrant triggers plugin is not quite good enough for all usecases. |
Can you give an example of some that it doesn't satisfy?
|
I want to be able to perform a MySql Dump before the vm is destroyed. The project I'm working on is heavily tied to the database, so I want to persist database changes automatically when the vm is shut down or destroyed. Because vagrant-triggers runs on the host it doesn't have access to the utilities installed on the VM that perform this operation. I could install mysql utilities on the host machine, but that kind of defeats the purpose of using vagrant in the first place. |
Why can't you run the command via WinRM or SSH?
|
Actually if you want to run a command on the VM, you can use run_remote as opposed to run as specified by https://github.com/emyl/vagrant-triggers/blob/master/lib/vagrant-triggers/dsl.rb in vagrant-triggers. However I think vagrant-triggers does not allow you to run a parameterised script, like the shell provisioner does. Ultimately the shell provisioner offers more flexibility, it would be nice if there was a mechanism to do the reverse. Also, with vagrant-triggers, you cannot run further vagrant commands - at one point in the past, I wanted to run vagrant box repackage after a vagrant suspend, but had to delegate this to a controlling wrapper bash script in the end, as vagrant cant call itself, but this isn't likely to ever be functionality it provides I realise that :-) |
vagrant-triggers also adds a further dependency to projects that rely on this kind of functionality. I'd like to just specify vagrant as a dependency in our project rather than having to list any vagrant plugins as well. Less dependencies, less complexity, more goodness. |
True, but it seems like the architecture is to start with plugins and move to embedded functionally when it is generic enough. To deal with this, we have created a package with installs vagrant and ensures that the require plugins are installed.
|
I believe Vagrant-triggers can ask for user input it that helps.
|
I was not aware that vagrant-triggers would allow you to run commands on the guest machine. I will give this a try for my application. I think everyone would agree that this feature is desirable, that it is silly to duplicate efforts, and that if the feature exists but is not fully baked it should remain a plugin. If or when it is a fully baked feature it would be nice to move it to the set of core features. |
@kadaan - asking for user input via "ask" et. al. is fine, but generally I shy away from anything that involves user input, as having everything automated is my preferred approach. I can put many 100s of "run" commands in my Vagrantfile in a trigger block, but it is much preferable if I have a lot of work to do on shutdown to call out to a script with parameters, particularly if conditional logic is involved. I'm not knocking vagrant-triggers, it's useful, but if either this, or vagrant itself could on destroying a box call out to a shell script on the host with parameters, that would be the feature that I'm after. I personally dont care where its implemented, but its not implemented anywhere yet. In my view its simpler to either let a plugin handle all shell provisioning and deprovisioning, or let vagrant handle all shell provisioning and deprovisioning - half and half doesnt really make sense to me. |
Fair enough. For what it is worth I need the same so that I can remove machine from LDAP.
|
I use that in order to unregister my box in puppet master and that works quite well 👍
|
I just noticed its almost 2 years since I raised this (via #1302). Do you think it's likely the core team will look at it any time soon? Or would it be better to attempt a PR of my own? Thanks |
I think this should be a core feature due to its utility and varied use-cases, however using the vagrant-triggers plugin is an option. To implement this functionality using vagrant-triggers plugin, in your vagrantfile you can add: config.trigger.before :destroy do
info "Dumping the database before destroying the VM..."
run_remote "command"
end If you prefer to use a shell script to contain all your teardown/cleanup tasks: config.trigger.before :destroy do
info "Dumping the database before destroying the VM..."
run_remote "bash /vagrant/cleanup.sh"
end ... where /vagrant/cleanup.sh is a script that will exist on both the host and the guest, because vagrant shares the directory containing the vagrantfile on the host, with /vagrant on the guest by default. |
@gtmtech FWIW internally we added support for vagrantfile specified parameters which can be specified in a similar way to triggers. We use it to allow users to pass in their credentials way earlier in |
As mentioned back in Nov 11, there's still a bunch of things that vagrant-triggers doesn't address, including the ability with provisioning to run a script rather than just run individual commands (which I believe is still the case with vagrant-triggers - had a quick look and doesn't look like they've added this functionality yet). I know vagrant-triggers is amazingly useful, but I dont think I've ever been convinced that anything will be as neat as the ability to run a parameterised deprovisioning shell script, just like you can run a provisioning one. I'm still a +1 for this. |
Just did a quick tally, and 45 people have +1'd this feature request, in case that pits it against other requests. |
@gtmtech I believe what I mentioned would allow you to accomplish your goals. You can define required parameters, in the vagrantfile, for any arbitrary command. These parameters are available in the vagrant-triggers definitions. The trigger can run a script on the host or guest. For example you could define a username parameter for Does this accomplish what you want? |
I just noticed the following message on Does that mean that the functionality is present? If so, could it please be documented? |
It's only available if the provisioner supports it -- AFAIK the shell provsioner does not. |
This helps with some confusion caused in GH-2538, since the output says: > Running cleanup tasks for 'shell' provisioner... But that's actually not true. It is running the cleanup tasks iff the provisioner defined a cleanup task. This commit changes the provisioner_cleanup middleware to only run cleanup tasks if the subclass defines a cleanup task. The reason we can't just check if the provisioner `respond_to?` the `cleanup` method is because the parent provisioner base class (which all provisioners inherit from) defines a blank cleanup method. This is important because it means we never risk calling an unimplemented cleanup function, and it also helps define the public API for a provisioner.
Related: #3849 |
As #3849 encompasses enhancement I'm going to close this one up and keep the other open. Cheers! |
Support for cleanup tasks was added via #1302 in Vagrant 1.3.0 and only the Chef provisioner was given a built in method to handle this functionality.
It would be excellent if the built in shell provisioner also had the ability to run either an inline script or file on
vagrant destroy
.The text was updated successfully, but these errors were encountered: