Update trigger docs with more info on types
This commit is contained in:
parent
64e21a1215
commit
6d91dfd025
|
@ -102,11 +102,12 @@ hooks, and actions.
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
A trigger can be one of two types:
|
A trigger can be one of three types:
|
||||||
|
|
||||||
* `type` (symbol) - Optional
|
* `type` (symbol) - Optional
|
||||||
- `:action` - Action triggers run before or after a Vagrant action
|
- `:action` - Action triggers run before or after a Vagrant action
|
||||||
- `:command` - Command triggers run before or after a Vagrant subcommand
|
- `: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.
|
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.
|
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
|
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:
|
you can guard the new trigger based on the version of Vagrant:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
@ -137,39 +138,42 @@ end
|
||||||
Command typed triggers can be defined for any valid Vagrant subcommand. They will always
|
Command typed triggers can be defined for any valid Vagrant subcommand. They will always
|
||||||
run before or after the subcommand.
|
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
|
```ruby
|
||||||
config.trigger.before :status, type: :command do |t|
|
config.trigger.before :up, type: :command do |t|
|
||||||
t.info = "Getting the status of your guests..."
|
t.info = "Before command!"
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
The difference between this and the default behavior is that these triggers are
|
For a more detailed example, please check out the [examples](/docs/triggers/usage.html#commands)
|
||||||
not attached to any specific guest, and will always run before or after the given
|
page for more.
|
||||||
command.
|
|
||||||
|
|
||||||
### Hooks
|
### Hooks
|
||||||
|
|
||||||
TODO: This still needs to be filled in.
|
|
||||||
|
|
||||||
<div class="alert alert-warning">
|
<div class="alert alert-warning">
|
||||||
<strong>Advanced topic!</strong> This is an advanced topic for use only if
|
<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
|
you want to execute triggers around Vagrant hooks. If you are just getting
|
||||||
started with Vagrant and triggers, you may safely skip this section.
|
started with Vagrant and triggers, you may safely skip this section.
|
||||||
</div>
|
</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
|
A simple example would be running a trigger on a given hook called `action_hook_name`.
|
||||||
and after each provisioner:
|
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
config.trigger.before :provisioner_run, type: :action do |t|
|
config.trigger.after :action_hook_name, type: :hook do |t|
|
||||||
t.info = "Before the provision of the guest!!!"
|
t.info = "After action hook!"
|
||||||
end
|
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">
|
<div class="alert alert-warning">
|
||||||
<strong>Advanced topic!</strong> This is an advanced topic for use only if
|
<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.
|
started with Vagrant and triggers, you may safely skip this section.
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
Actions in this case refer to the Vagrant class `#Action`, which is used internally
|
Action typed triggers can be defined for any valid Vagrant action class. Actions
|
||||||
to Vagrant and in every Vagrant plugin.
|
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.
|
||||||
|
|
|
@ -134,8 +134,67 @@ end
|
||||||
|
|
||||||
### Typed Triggers
|
### Typed Triggers
|
||||||
|
|
||||||
Show some examples that use:
|
Below are some basic examples of using `:type` triggers. They cover commands, hooks,
|
||||||
|
and actions.
|
||||||
|
|
||||||
* actions
|
It is important to note that while `command` triggers will be a fairly common use case,
|
||||||
* hooks
|
both `action` and `hook` triggers are more complicated and are a more advanced use case.
|
||||||
* commands
|
|
||||||
|
#### 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
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue