vagrant-hostmanager-ext 1.8.7.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,236 @@
1
+ Vagrant Host Manager
2
+ ====================
3
+
4
+ [![Gem](https://img.shields.io/gem/v/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager)
5
+ [![Gem](https://img.shields.io/gem/dt/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager)
6
+ [![Gem](https://img.shields.io/gem/dtv/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager)
7
+ [![Twitter](https://img.shields.io/twitter/url/https/github.com/devopsgroup-io/vagrant-hostmanager.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20Vagrant%20plugin%21&url=https%3A%2F%2Fgithub.com%devopsgroup-io%2Fvagrant-hostmanager&hashtags=vagrant%hostmanager&original_referer=)
8
+
9
+ `vagrant-hostmanager` is a Vagrant plugin that manages the `hosts` file on guest machines (and optionally the host). Its goal is to enable resolution of multi-machine environments deployed with a cloud provider where IP addresses are not known in advance.
10
+
11
+ Installation
12
+ ------------
13
+
14
+ $ vagrant plugin install vagrant-hostmanager
15
+
16
+ Usage
17
+ -----
18
+ To update the `hosts` file on each active machine, run the following
19
+ command:
20
+
21
+ $ vagrant hostmanager
22
+
23
+ The plugin hooks into the `vagrant up` and `vagrant destroy` commands
24
+ automatically.
25
+ When a machine enters or exits the running state , all active
26
+ machines with the same provider will have their `hosts` file updated
27
+ accordingly. Set the `hostmanager.enabled` attribute to `true` in the
28
+ Vagrantfile to activate this behavior.
29
+
30
+ To update the host's `hosts` file, set the `hostmanager.manage_host`
31
+ attribute to `true`.
32
+
33
+ To update the guests' `hosts` file, set the `hostmanager.manage_guest`
34
+ attribute to `true`.
35
+
36
+ A machine's IP address is defined by either the static IP for a private
37
+ network configuration or by the SSH host configuration. To disable
38
+ using the private network IP address, set `config.hostmanager.ignore_private_ip`
39
+ to true.
40
+
41
+ A machine's host name is defined by `config.vm.hostname`. If this is not
42
+ set, it falls back to the symbol defining the machine in the Vagrantfile.
43
+
44
+ If the `hostmanager.include_offline` attribute is set to `true`, boxes that are
45
+ up or have a private ip configured will be added to the hosts file.
46
+
47
+ If the `hostmanager.extra_hosts` attribute is set, boxes defined will be added
48
+ to the hosts file
49
+
50
+ In addition, the `hostmanager.aliases` configuration attribute can be used
51
+ to provide aliases for your host names.
52
+
53
+ Example configuration:
54
+
55
+ ```ruby
56
+ Vagrant.configure("2") do |config|
57
+ config.hostmanager.enabled = true
58
+ config.hostmanager.manage_host = true
59
+ config.hostmanager.manage_guest = true
60
+ config.hostmanager.ignore_private_ip = false
61
+ config.hostmanager.include_offline = true
62
+ config.hostmanager.extra_hosts = [
63
+ [
64
+ '192.168.42.41', [
65
+ 'external-box.localdomain',
66
+ 'subdomain.external-box.localdomain',
67
+ ]
68
+ ],
69
+ ]
70
+ config.vm.define 'example-box' do |node|
71
+ node.vm.hostname = 'example-box-hostname'
72
+ node.vm.network :private_network, ip: '192.168.42.42'
73
+ node.hostmanager.aliases = %w(example-box.localdomain example-box-alias)
74
+ end
75
+ end
76
+ ```
77
+
78
+ ### Provisioner
79
+
80
+ Starting at version 1.5.0, `vagrant up` runs hostmanager before any provisioning occurs.
81
+ If you would like hostmanager to run after or during your provisioning stage,
82
+ you can use hostmanager as a provisioner. This allows you to use the provisioning
83
+ order to ensure that hostmanager runs when desired. The provisioner will collect
84
+ hosts from boxes with the same provider as the running box.
85
+
86
+ Example:
87
+
88
+ ```ruby
89
+ # Disable the default hostmanager behavior
90
+ config.hostmanager.enabled = false
91
+
92
+ # ... possible provisioner config before hostmanager ...
93
+
94
+ # hostmanager provisioner
95
+ config.vm.provision :hostmanager
96
+
97
+ # ... possible provisioning config after hostmanager ...
98
+ ```
99
+
100
+ Custom IP resolver
101
+ ------------------
102
+
103
+ You can customize way, how host manager resolves IP address
104
+ for each machine. This might be handy in case of aws provider,
105
+ where host name is stored in ssh_info hash of each machine.
106
+ This causes generation of invalid /etc/hosts file.
107
+
108
+ Custom IP resolver gives you oportunity to calculate IP address
109
+ for each machine by yourself, giving You also access to the machine that is
110
+ updating /etc/hosts. For example:
111
+
112
+ ```ruby
113
+ config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
114
+ if hostname = (vm.ssh_info && vm.ssh_info[:host])
115
+ `host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
116
+ end
117
+ end
118
+ ```
119
+
120
+ Passwordless sudo
121
+ -----------------
122
+
123
+ To avoid being asked for the password every time the hosts file is updated,
124
+ enable passwordless sudo for the specific command that hostmanager uses to
125
+ update the hosts file.
126
+
127
+ - Add the following snippet to the sudoers file (e.g.
128
+ `/etc/sudoers.d/vagrant_hostmanager`):
129
+
130
+ ```
131
+ Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp <home-directory>/.vagrant.d/tmp/hosts.local /etc/hosts
132
+ %<admin-group> ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE
133
+ ```
134
+
135
+ Replace `<home-directory>` with your actual home directory (e.g.
136
+ `/home/joe`) and `<admin-group>` with the group that is used by the system
137
+ for sudo access (usually `sudo` on Debian/Ubuntu systems and `wheel`
138
+ on Fedora/Red Hat systems).
139
+
140
+ - If necessary, add yourself to the `<admin-group>`:
141
+
142
+ ```
143
+ usermod -aG <admin-group> <user-name>
144
+ ```
145
+
146
+ Replace `<admin-group>` with the group that is used by the system for sudo
147
+ access (see above) and `<user-name>` with you user name.
148
+
149
+ Windows support
150
+ ---------------
151
+
152
+ Hostmanager will detect Windows guests and hosts and use the appropriate
153
+ path for the ```hosts``` file: ```%WINDIR%\System32\drivers\etc\hosts```
154
+
155
+ By default on a Windows host, the ```hosts``` file is not writable without
156
+ elevated privileges. If hostmanager detects that it cannot overwrite the file,
157
+ it will attempt to do so with elevated privileges, causing the
158
+ [UAC](http://en.wikipedia.org/wiki/User_Account_Control) prompt to appear.
159
+
160
+ To avoid the UAC prompt, open ```%WINDIR%\System32\drivers\etc\``` in
161
+ Explorer, right-click the hosts file, go to Properties > Security > Edit
162
+ and give your user Modify permission.
163
+
164
+ ### UAC limitations
165
+
166
+ Due to limitations caused by UAC, cancelling out of the UAC prompt will not cause any
167
+ visible errors, however the ```hosts``` file will not be updated.
168
+
169
+
170
+ Compatibility
171
+ -------------
172
+ This Vagrant plugin has been tested with the following host and guest operating system combinations.
173
+
174
+ Date Tested | Vagrant Version | vagrant-hostmanager Version | Host (Workstation) Operating System | Guest (VirtualBox) Operating System
175
+ ------------|-----------------|-----------------------------|-------------------------------------|--------------------------------------
176
+ 03/23/2016 | 1.8.1 | 1.8.1 | Ubuntu 14.04 LTS | CentOS 7.2
177
+ 03/22/2016 | 1.8.1 | 1.8.1 | OS X 10.11.4 | CentOS 7.2
178
+ 05/03/2017 | 1.9.4 | 1.8.6 | macOS 10.12.4 | Windows Server 2012 R2
179
+
180
+
181
+ Troubleshooting
182
+ -------------
183
+ * Version 1.1 of the plugin prematurely introduced a feature to hook into
184
+ commands other than `vagrant up` and `vagrant destroy`. Version 1.1 broke support
185
+ for some providers. Version 1.2 reverts this feature until a suitable implementation
186
+ supporting all providers is available.
187
+
188
+ * Potentially breaking change in v1.5.0: the running order on `vagrant up` has changed
189
+ so that hostmanager runs before provisioning takes place. This ensures all hostnames are
190
+ available to the guest when it is being provisioned
191
+ (see [#73](https://github.com/devopsgroup-io/vagrant-hostmanager/issues/73)).
192
+ Previously, hostmanager would run as the very last action. If you depend on the old behavior,
193
+ see the [provisioner](#provisioner) section.
194
+
195
+
196
+ Contribute
197
+ ----------
198
+ To contribute, fork then clone the repository, and then the following:
199
+
200
+ **Developing**
201
+
202
+ 1. Ideally, install the version of Vagrant as defined in the `Gemfile`
203
+ 1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/)
204
+ 2. Currently the Bundler version is locked to 1.14.6, please install this version.
205
+ * `gem install bundler -v '1.14.6'`
206
+ 3. Then install vagrant-hostmanager dependancies:
207
+ * `bundle _1.14.6_ install`
208
+
209
+ **Testing**
210
+
211
+ 1. Build and package your newly developed code:
212
+ * `rake gem:build`
213
+ 2. Then install the packaged plugin:
214
+ * `vagrant plugin install pkg/vagrant-hostmanager-*.gem`
215
+ 3. Once you're done testing, roll-back to the latest released version:
216
+ * `vagrant plugin uninstall vagrant-hostmanager`
217
+ * `vagrant plugin install vagrant-hostmanager`
218
+ 4. Once you're satisfied developing and testing your new code, please submit a pull request for review.
219
+
220
+ **Releasing**
221
+
222
+ To release a new version of vagrant-hostmanager you will need to do the following:
223
+
224
+ *(only contributors of the GitHub repo and owners of the project at RubyGems will have rights to do this)*
225
+
226
+ 1. First, bump the version in ~/lib/vagrant-hostmanager/version.rb:
227
+ * Follow [Semantic Versioning](http://semver.org/).
228
+ 2. Then, create a matching GitHub Release (this will also create a tag):
229
+ * Preface the version number with a `v`.
230
+ * https://github.com/devopsgroup-io/vagrant-hostmanager/releases
231
+ 3. You will then need to build and push the new gem to RubyGems:
232
+ * `rake gem:build`
233
+ * `gem push pkg/vagrant-hostmanager-1.6.1.gem`
234
+ 4. Then, when John Doe runs the following, they will receive the updated vagrant-hostmanager plugin:
235
+ * `vagrant plugin update`
236
+ * `vagrant plugin update vagrant-hostmanager`
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_helper'
2
+
3
+ # Change to the directory of this file.
4
+ Dir.chdir(File.expand_path("../", __FILE__))
5
+
6
+ namespace :gem do
7
+ Bundler::GemHelper.install_tasks
8
+ end
9
+
10
+ task :test do
11
+ sh 'bash test/test.sh'
12
+ end
@@ -0,0 +1 @@
1
+ vagrant-hostmanager.rb
@@ -0,0 +1,14 @@
1
+ require 'vagrant-hostmanager/plugin'
2
+ require 'vagrant-hostmanager/version'
3
+ require 'vagrant-hostmanager/errors'
4
+
5
+ module VagrantPlugins
6
+ module HostManager
7
+ def self.source_root
8
+ @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
9
+ end
10
+
11
+ I18n.load_path << File.expand_path('locales/en.yml', source_root)
12
+ I18n.reload!
13
+ end
14
+ end
@@ -0,0 +1,31 @@
1
+ require 'vagrant-hostmanager/action/update_all'
2
+ require 'vagrant-hostmanager/action/update_guest'
3
+ require 'vagrant-hostmanager/action/update_host'
4
+
5
+ module VagrantPlugins
6
+ module HostManager
7
+ module Action
8
+ include Vagrant::Action::Builtin
9
+
10
+ def self.update_all
11
+ Vagrant::Action::Builder.new.tap do |builder|
12
+ builder.use ConfigValidate
13
+ builder.use UpdateAll
14
+ end
15
+ end
16
+
17
+ def self.update_guest
18
+ Vagrant::Action::Builder.new.tap do |builder|
19
+ builder.use ConfigValidate
20
+ builder.use UpdateGuest
21
+ end
22
+ end
23
+
24
+ def self.update_host
25
+ Vagrant::Action::Builder.new.tap do |builder|
26
+ builder.use UpdateHost
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,52 @@
1
+ require 'vagrant-hostmanager/hosts_file/updater'
2
+ require 'vagrant-hostmanager/util'
3
+
4
+ module VagrantPlugins
5
+ module HostManager
6
+ module Action
7
+ class UpdateAll
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @global_env = @machine.env
13
+ @provider = @machine.provider_name
14
+ @config = Util.get_config(@global_env)
15
+ @updater = HostsFile::Updater.new(@global_env, @provider)
16
+ @logger = Log4r::Logger.new('vagrant::hostmanager::update_all')
17
+ end
18
+
19
+ def call(env)
20
+ # skip if machine is not active on destroy action
21
+ return @app.call(env) if !@machine.id && env[:machine_action] == :destroy
22
+
23
+ # check config to see if the hosts file should be update automatically
24
+ return @app.call(env) unless @config.hostmanager.enabled?
25
+ @logger.info 'Updating /etc/hosts file automatically'
26
+
27
+ @app.call(env)
28
+
29
+ # update /etc/hosts file on active machines
30
+ if @machine.config.hostmanager.manage_guest?
31
+ env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests')
32
+ @global_env.active_machines.each do |name, p|
33
+ if p == @provider
34
+ machine = @global_env.machine(name, p)
35
+ state = machine.state
36
+ if ['active','running'].include?(state.short_description)
37
+ @updater.update_guest(machine)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # update /etc/hosts files on host if enabled
44
+ if @machine.config.hostmanager.manage_host?
45
+ env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
46
+ @updater.update_host
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ require 'vagrant-hostmanager/hosts_file/updater'
2
+ require 'vagrant-hostmanager/util'
3
+
4
+ module VagrantPlugins
5
+ module HostManager
6
+ module Action
7
+ class UpdateGuest
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ global_env = env[:global_env]
12
+ @config = Util.get_config(global_env)
13
+ @machine = env[:machine]
14
+ @updater = HostsFile::Updater.new(@machine.env, env[:provider])
15
+ @logger = Log4r::Logger.new('vagrant::hostmanager::update_guest')
16
+ end
17
+
18
+ def call(env)
19
+ if @config.hostmanager.manage_guest?
20
+ env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', {
21
+ :name => @machine.name
22
+ })
23
+ @updater.update_guest(@machine)
24
+
25
+ @app.call(env)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ require 'vagrant-hostmanager/hosts_file/updater'
2
+ require 'vagrant-hostmanager/util'
3
+
4
+ module VagrantPlugins
5
+ module HostManager
6
+ module Action
7
+ class UpdateHost
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+
12
+ global_env = env[:global_env]
13
+ @config = Util.get_config(global_env)
14
+ @updater = HostsFile::Updater.new(global_env, env[:provider])
15
+
16
+ @logger = Log4r::Logger.new('vagrant::hostmanager::update_host')
17
+ end
18
+
19
+ def call(env)
20
+ if @config.hostmanager.manage_host?
21
+ env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
22
+ @updater.update_host
23
+ end
24
+
25
+ @app.call(env)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ module VagrantPlugins
2
+ module HostManager
3
+ class Command < Vagrant.plugin('2', :command)
4
+
5
+ # Show description when `vagrant list-commands` is triggered
6
+ def self.synopsis
7
+ "plugin: vagrant-hostmanager: manages the /etc/hosts file within a multi-machine environment"
8
+ end
9
+
10
+ def execute
11
+ options = {}
12
+ opts = OptionParser.new do |o|
13
+ o.banner = 'Usage: vagrant hostmanager [vm-name]'
14
+ o.separator ''
15
+ o.version = VagrantPlugins::HostManager::VERSION
16
+ o.program_name = 'vagrant hostmanager'
17
+
18
+ o.on('--provider provider', String,
19
+ 'Update machines with the specific provider.') do |provider|
20
+ options[:provider] = provider.to_sym
21
+ end
22
+ end
23
+
24
+ argv = parse_options(opts)
25
+ options[:provider] ||= @env.default_provider
26
+
27
+ # update /etc/hosts file for specified guest machines
28
+ with_target_vms(argv, options) do |machine|
29
+ @env.action_runner.run(Action.update_guest, {
30
+ :global_env => @env,
31
+ :machine => machine,
32
+ :provider => options[:provider]
33
+ })
34
+ end
35
+
36
+ # update /etc/hosts file for host
37
+ @env.action_runner.run(Action.update_host, {
38
+ :global_env => @env,
39
+ :provider => options[:provider]
40
+ })
41
+ end
42
+ end
43
+ end
44
+ end