From c35e0a4f9a502a36d59e86d19581f1e90bdf27b2 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Wed, 4 Apr 2018 16:20:27 -0700 Subject: [PATCH] Update docs with some basic trigger examples --- lib/vagrant/plugin/v2/trigger.rb | 1 - website/source/docs/triggers/index.html.md | 4 +- website/source/docs/triggers/usage.html.md | 66 +++++++++++++++++++++- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/plugin/v2/trigger.rb b/lib/vagrant/plugin/v2/trigger.rb index 4fe0d038e..30b420a60 100644 --- a/lib/vagrant/plugin/v2/trigger.rb +++ b/lib/vagrant/plugin/v2/trigger.rb @@ -196,7 +196,6 @@ module Vagrant if on_error == :halt @logger.debug("Trigger run encountered an error. Halting on error...") - # Raise proper Vagrant error to avoid ugly stacktrace raise e else @logger.debug("Trigger run encountered an error. Continuing on anyway...") diff --git a/website/source/docs/triggers/index.html.md b/website/source/docs/triggers/index.html.md index e9fca84d4..ac24fb635 100644 --- a/website/source/docs/triggers/index.html.md +++ b/website/source/docs/triggers/index.html.md @@ -71,8 +71,8 @@ triggers will only run on the configured guest. An example of a guest only trigg config.vm.define "ubuntu" do |ubuntu| ubuntu.vm.box = "ubuntu" ubuntu.trigger.before :destroy do |trigger| - trigger.warn "Dumping database to /vagrant/outfile" - trigger.run_remote "pg_dump dbname > /vagrant/outfile" + trigger.warn = "Dumping database to /vagrant/outfile" + trigger.run_remote {inline: "pg_dump dbname > /vagrant/outfile"} end end ``` diff --git a/website/source/docs/triggers/usage.html.md b/website/source/docs/triggers/usage.html.md index 6a81182ac..a8cb54d0f 100644 --- a/website/source/docs/triggers/usage.html.md +++ b/website/source/docs/triggers/usage.html.md @@ -3,8 +3,70 @@ layout: "docs" page_title: "Vagrant Triggers Usage" sidebar_current: "triggers-usage" description: |- - Description goes here + Various Vagrant Triggers examples --- -# Usage +# Basic Usage +Below are some very simple examples of how to use Vagrant Triggers. + +## Examples + +Running a remote script to save a database on your host before __destroy__ing a +guest: + +```ruby +Vagrant.configure("2") do |config| + config.vm.define "ubuntu" do |ubuntu| + ubuntu.vm.box = "ubuntu" + + ubuntu.trigger.before :destroy do |trigger| + trigger.warn = "Dumping database to /vagrant/outfile" + trigger.run_remote = {inline: "pg_dump dbname > /vagrant/outfile"} + end + end +end +``` + +Now that the trigger is defined, running the __destroy__ command will fire off +the defined trigger before Vagrant destroys the machine. + +```shell +$ vagrant destroy ubuntu +``` + +An example of defining three triggers that start and stop tinyproxy on your host +machine using homebrew: + +```shell +#/bin/bash +# start-tinyproxy.sh +brew services start tinyproxy +``` + +```shell +#/bin/bash +# stop-tinyproxy.sh +brew services stop tinyproxy +``` + +```ruby +Vagrant.configure("2") do |config| + config.vm.define "ubuntu" do |ubuntu| + ubuntu.vm.box = "ubuntu" + + ubuntu.trigger.before :up do |trigger| + trigger.info = "Starting tinyproxy..." + trigger.run = {path: "start-tinyproxy.sh"} + end + + ubuntu.trigger.after :destroy, :halt do |trigger| + trigger.info = "Stopping tinyproxy..." + trigger.run = {path: "stop-tinyproxy.sh"} + end + end +end +``` + +Running `vagrant up` would fire the before trigger to start tinyproxy, where as +running either `vagrant destroy` or `vagrant halt` would stop tinyproxy.