blob: 1a369d2651ac5542148cab3f7f9156eddc557d02 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
<h1 id="basic-usage"> Basic Usage </h1> <p>Below are some very simple examples of how to use Vagrant Triggers.</p> <h2 id="examples"> Examples </h2> <p>The following is a basic example of two global triggers. One that runs <em>before</em> the <code>:up</code> command and one that runs <em>after</em> the <code>:up</code> command:</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">Vagrant.configure("2") do |config|
config.trigger.before :up do |trigger|
trigger.name = "Hello world"
trigger.info = "I am running before vagrant up!!"
end
config.trigger.after :up do |trigger|
trigger.name = "Hello world"
trigger.info = "I am running after vagrant up!!"
end
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
end
end
</pre></div>
<p>These will run before and after each defined guest in the Vagrantfile.</p> <p>Running a remote script to save a database on your host before <strong>destroy</strong>ing a guest:</p> <div class="highlight"><pre class="highlight ruby" data-language="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
</pre></div>
<p>Now that the trigger is defined, running the <strong>destroy</strong> command will fire off the defined trigger before Vagrant destroys the machine.</p> <div class="highlight"><pre class="highlight shell" data-language="shell">$ vagrant destroy ubuntu
</pre></div>
<p>An example of defining three triggers that start and stop tinyproxy on your host machine using homebrew:</p> <div class="highlight"><pre class="highlight shell" data-language="shell">#/bin/bash
# start-tinyproxy.sh
brew services start tinyproxy
</pre></div>
<div class="highlight"><pre class="highlight shell" data-language="shell">#/bin/bash
# stop-tinyproxy.sh
brew services stop tinyproxy
</pre></div>
<div class="highlight"><pre class="highlight ruby" data-language="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
</pre></div>
<p>Running <code>vagrant up</code> would fire the before trigger to start tinyproxy, where as running either <code>vagrant destroy</code> or <code>vagrant halt</code> would stop tinyproxy.</p> <h3 id="ruby-option"> Ruby Option </h3> <p>Triggers can also be defined to run Ruby, rather than bash or powershell. An example of this might be using a Ruby option to get more information from the <code>VBoxManage</code> tool. In this case, we are printing the <code>ostype</code> defined for thte guest after it has been brought up.</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">Vagrant.configure("2") do |config|
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
ubuntu.trigger.after :up do |trigger|
trigger.info = "More information with ruby magic"
trigger.ruby do |env,machine|
puts `VBoxManage showvminfo #{machine.id} --machinereadable | grep ostype`
end
end
end
end
</pre></div>
<p>If you are defining your triggers using the hash syntax, you must use the <code>Proc</code> type for defining a ruby trigger.</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">Vagrant.configure("2") do |config|
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
ubuntu.trigger.after :up,
info: "More information with ruby magic",
ruby: proc{|env,machine| puts `VBoxManage showvminfo #{machine.id} --machinereadable | grep ostype`}
end
end
</pre></div><div class="_attribution">
<p class="_attribution-p">
© 2010–2018 Mitchell Hashimoto<br>Licensed under the MPL 2.0 License.<br>
<a href="https://www.vagrantup.com/docs/triggers/usage.html" class="_attribution-link">https://www.vagrantup.com/docs/triggers/usage.html</a>
</p>
</div>
|