summaryrefslogtreecommitdiff
path: root/devdocs/vagrant/triggers%2Fusage.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/vagrant/triggers%2Fusage.html')
-rw-r--r--devdocs/vagrant/triggers%2Fusage.html81
1 files changed, 81 insertions, 0 deletions
diff --git a/devdocs/vagrant/triggers%2Fusage.html b/devdocs/vagrant/triggers%2Fusage.html
new file mode 100644
index 00000000..1a369d26
--- /dev/null
+++ b/devdocs/vagrant/triggers%2Fusage.html
@@ -0,0 +1,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 &gt; /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">
+ &copy; 2010&ndash;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>