Update trigger docs with more info on types

This commit is contained in:
Brian Cain 2019-02-06 16:29:40 -08:00
parent 64e21a1215
commit 6d91dfd025
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 94 additions and 21 deletions

View File

@ -102,11 +102,12 @@ hooks, and actions.
</div>
A trigger can be one of two types:
A trigger can be one of three types:
* `type` (symbol) - Optional
- `:action` - Action triggers run before or after a Vagrant action
- `:command` - Command triggers run before or after a Vagrant subcommand
- `:hook` - Action hook triggers run before or after a Vagrant hook
These types determine when and where a defined trigger will execute.
@ -121,7 +122,7 @@ end
Triggers _without_ the type option will run before or after a Vagrant guest.
Older Vagrant versions will unfortunetly not be able to properly parse the new
`:type` option. To prevent older clients from failing to parse your Vagrantfile,
`:type` option. If you are worried about older clients failing to parse your Vagrantfile,
you can guard the new trigger based on the version of Vagrant:
```ruby
@ -137,39 +138,42 @@ end
Command typed triggers can be defined for any valid Vagrant subcommand. They will always
run before or after the subcommand.
The difference between this and the default behavior is that these triggers are
not attached to any specific guest, and will always run before or after the given
command. A simple example might be running a trigger before the up command to give
a simple message to the user:
```ruby
config.trigger.before :status, type: :command do |t|
t.info = "Getting the status of your guests..."
config.trigger.before :up, type: :command do |t|
t.info = "Before command!"
end
```
The difference between this and the default behavior is that these triggers are
not attached to any specific guest, and will always run before or after the given
command.
For a more detailed example, please check out the [examples](/docs/triggers/usage.html#commands)
page for more.
### Hooks
TODO: This still needs to be filled in.
<div class="alert alert-warning">
<strong>Advanced topic!</strong> This is an advanced topic for use only if
you want to execute triggers around Vagrant hooks. If you are just getting
started with Vagrant and triggers, you may safely skip this section.
</div>
Hook typed triggers can be defined for any valid Vagrant action hook that is defined.
For example, you could write up a Vagrant trigger that runs before
and after each provisioner:
A simple example would be running a trigger on a given hook called `action_hook_name`.
```ruby
config.trigger.before :provisioner_run, type: :action do |t|
t.info = "Before the provision of the guest!!!"
config.trigger.after :action_hook_name, type: :hook do |t|
t.info = "After action hook!"
end
```
### Actions
For a more detailed example, please check out the [examples](/docs/triggers/usage.html#hooks)
page for more.
TODO: This still needs to be filled in.
### Actions
<div class="alert alert-warning">
<strong>Advanced topic!</strong> This is an advanced topic for use only if
@ -177,5 +181,15 @@ TODO: This still needs to be filled in.
started with Vagrant and triggers, you may safely skip this section.
</div>
Actions in this case refer to the Vagrant class `#Action`, which is used internally
to Vagrant and in every Vagrant plugin.
Action typed triggers can be defined for any valid Vagrant action class. Actions
in this case refer to the Vagrant class `#Action`, which is used internally to
Vagrant and in every Vagrant plugin.
```ruby
config.trigger.before :"Action::Class::Name", type: :action do |t|
t.info = "Before action class!
end
```
For a more detailed example, please check out the [examples](/docs/triggers/usage.html#actions)
page for more.

View File

@ -134,8 +134,67 @@ end
### Typed Triggers
Show some examples that use:
Below are some basic examples of using `:type` triggers. They cover commands, hooks,
and actions.
* actions
* hooks
* commands
It is important to note that while `command` triggers will be a fairly common use case,
both `action` and `hook` triggers are more complicated and are a more advanced use case.
#### Commands
The most common use case for typed triggers are with `command`. These kinds of
triggers allow you to run something before or after a subcommand in Vagrant.
```ruby
config.trigger.after :status, type: :command do |t|
t.info = "Showing status of all VMs!"
end
```
Because they are specifically for subcommands, they do not work with any guest
operations like `run_remote` or if you define the trigger as a guest trigger.
#### Hooks
Below is an example of a Vagrant trigger that runs before and after each defined
provisioner:
```ruby
config.trigger.before :provisioner_run, type: :hook do |t|
t.info = "Before the provision!"
end
config.vm.provision "file", source: "scripts/script.sh", destination: "/test/script.sh"
config.vm.provision "shell", inline: <<-SHELL
echo "Provision the guest!"
SHELL
```
Notice how this trigger runs before _each_ provisioner defined for the guest:
```shell
==> guest: Running provisioner: Sandbox (file)...
==> vargant: Running hook triggers before provisioner_run ...
==> vargant: Running trigger...
==> vargant: Before the provision!
guest: /home/hashicorp/vagrant-sandbox/scripts/script.sh => /home/vagrant/test/script.sh
==> guest: Running provisioner: shell...
==> vargant: Running hook triggers before provisioner_run ...
==> vargant: Running trigger...
==> vargant: Before the provision!
guest: Running: inline script
guest: Provision the guest!
```
#### Actions
With action typed triggers, you can fire off triggers before or after certain
Action classes. A simple example of this might be warning the user when Vagrant
invokes the `GracefulHalt` action.
```ruby
config.trigger.before :"Vagrant::Action::Builtin::GracefulHalt", type: :action do |t|
t.warn = "Vagrant is halting your guest..."
end
```