Update docs with some basic trigger examples

This commit is contained in:
Brian Cain 2018-04-04 16:20:27 -07:00
parent e0806f623c
commit c35e0a4f9a
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
3 changed files with 66 additions and 5 deletions

View File

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

View File

@ -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
```

View File

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