2013-09-03 18:08:28 +00:00
|
|
|
---
|
2016-01-19 18:08:53 +00:00
|
|
|
layout: "docs"
|
2013-09-06 16:50:43 +00:00
|
|
|
page_title: "Custom Hosts - Plugin Development"
|
2013-09-03 18:08:28 +00:00
|
|
|
sidebar_current: "plugins-hosts"
|
2016-01-19 18:08:53 +00:00
|
|
|
description: |-
|
2016-01-19 19:54:13 +00:00
|
|
|
This page documents how to add new host OS detection to Vagrant, allowing
|
|
|
|
Vagrant to properly execute host-specific operations on new operating systems.
|
|
|
|
Prior to reading this, you should be familiar with the plugin development
|
|
|
|
basics.
|
2013-09-03 18:08:28 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Plugin Development: Hosts
|
|
|
|
|
2014-01-08 05:31:17 +00:00
|
|
|
This page documents how to add new host OS detection to Vagrant, allowing
|
|
|
|
Vagrant to properly execute host-specific operations on new operating systems.
|
|
|
|
Prior to reading this, you should be familiar
|
2016-01-19 18:08:53 +00:00
|
|
|
with the [plugin development basics](/docs/plugins/development-basics.html).
|
|
|
|
|
|
|
|
<div class="alert alert-warning">
|
2016-01-19 19:54:13 +00:00
|
|
|
<strong>Warning: Advanced Topic!</strong> Developing plugins is an
|
|
|
|
advanced topic that only experienced Vagrant users who are reasonably
|
|
|
|
comfortable with Ruby should approach.
|
2013-09-03 18:08:28 +00:00
|
|
|
</div>
|
|
|
|
|
2014-01-08 05:31:17 +00:00
|
|
|
Vagrant has some features that require host OS-specific actions, such as
|
|
|
|
exporting NFS folders. These tasks vary from operating system to operating
|
|
|
|
system. Vagrant uses host detection as well as
|
2016-01-19 18:08:53 +00:00
|
|
|
[host capabilities](/docs/plugins/host-capabilities.html) to perform these
|
2014-01-08 05:31:17 +00:00
|
|
|
host OS-specific operations.
|
|
|
|
|
2013-09-03 18:08:28 +00:00
|
|
|
## Definition Component
|
|
|
|
|
|
|
|
Within the context of a plugin definition, new hosts can be defined
|
|
|
|
like so:
|
|
|
|
|
|
|
|
```ruby
|
2014-01-08 05:31:17 +00:00
|
|
|
host "ubuntu" do
|
2013-09-03 18:08:28 +00:00
|
|
|
require_relative "host"
|
|
|
|
Host
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2014-01-08 05:31:17 +00:00
|
|
|
Hosts are defined with the `host` method. The first argument is the
|
2016-01-19 18:08:53 +00:00
|
|
|
name of the host. This name is not actually used anywhere, but may in the
|
2014-01-08 05:31:17 +00:00
|
|
|
future, so choose something helpful. Then, the block argument returns a
|
2013-09-03 18:08:28 +00:00
|
|
|
class that implements the `Vagrant.plugin(2, :host)` interface.
|
|
|
|
|
|
|
|
## Implementation
|
|
|
|
|
2014-01-08 05:31:17 +00:00
|
|
|
Implementations of hosts subclass `Vagrant.plugin("2", "host")`. Within
|
|
|
|
this implementation, only the `detect?` method needs to be implemented.
|
|
|
|
|
|
|
|
The `detect?` method is called by Vagrant very early on in its initialization
|
2014-12-17 15:00:49 +00:00
|
|
|
process to determine if the OS that Vagrant is running on is this host.
|
2014-01-08 05:31:17 +00:00
|
|
|
If you detect that it is your operating system, return `true` from `detect?`.
|
|
|
|
Otherwise, return `false`.
|
|
|
|
|
|
|
|
```
|
|
|
|
class MyHost < Vagrant.plugin("2", "host")
|
|
|
|
def detect?(environment)
|
|
|
|
File.file?("/etc/arch-release")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
After detecting an OS, that OS is used for various
|
2016-01-19 18:08:53 +00:00
|
|
|
[host capabilities](/docs/plugins/host-capabilities.html) that may be
|
2014-01-08 05:31:17 +00:00
|
|
|
required.
|
|
|
|
|
|
|
|
## Host Inheritance
|
|
|
|
|
|
|
|
Vagrant also supports a form of inheritance for hosts, since sometimes
|
|
|
|
operating systems stem from a common root. A good example of this is Linux
|
|
|
|
is the root of Debian, which further is the root of Ubuntu in many cases.
|
|
|
|
Inheritance allows hosts to share a lot of common behavior while allowing
|
|
|
|
distro-specific overrides.
|
|
|
|
|
|
|
|
Inheritance is not done via standard Ruby class inheritance because Vagrant
|
2016-01-19 18:08:53 +00:00
|
|
|
uses a custom [capability-based](/docs/plugins/host-capabilities.html) system.
|
2014-01-08 05:31:17 +00:00
|
|
|
Vagrant handles inheritance dispatch for you.
|
|
|
|
|
|
|
|
To subclass another host, specify that host's name as a second parameter
|
|
|
|
in the host definition:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
host "ubuntu", "debian" do
|
|
|
|
require_relative "host"
|
|
|
|
Host
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
With the above component, the "ubuntu" host inherits from "debian." When
|
|
|
|
a capability is looked up for "ubuntu", all capabilities from "debian" are
|
|
|
|
also available, and any capabilities in "ubuntu" override parent capabilities.
|
2013-09-03 18:08:28 +00:00
|
|
|
|
2014-01-08 05:31:17 +00:00
|
|
|
When detecting operating systems with `detect?`, Vagrant always does a
|
|
|
|
depth-first search by searching the children operating systems before
|
|
|
|
checking their parents. Therefore, it is guaranteed in the above example
|
|
|
|
that the `detect?` method on "ubuntu" will be called before "debian."
|