vagrant-hostmanager-rethinc 1.8.9.1

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,228 @@
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
+ Do you like what we do? Consider supporting us through Patreon. All of the money goes directly back into growing our collection of open source and free software.
12
+ [![Patreon](https://img.shields.io/badge/patreon-donate-red.svg)](https://www.patreon.com/devopsgroup)
13
+
14
+ Installation
15
+ ------------
16
+
17
+ $ vagrant plugin install vagrant-hostmanager
18
+
19
+ Usage
20
+ -----
21
+ To update the `hosts` file on each active machine, run the following
22
+ command:
23
+
24
+ $ vagrant hostmanager
25
+
26
+ The plugin hooks into the `vagrant up` and `vagrant destroy` commands
27
+ automatically.
28
+ When a machine enters or exits the running state , all active
29
+ machines with the same provider will have their `hosts` file updated
30
+ accordingly. Set the `hostmanager.enabled` attribute to `true` in the
31
+ Vagrantfile to activate this behavior.
32
+
33
+ To update the host's `hosts` file, set the `hostmanager.manage_host`
34
+ attribute to `true`.
35
+
36
+ To update the guests' `hosts` file, set the `hostmanager.manage_guest`
37
+ attribute to `true`.
38
+
39
+ A machine's IP address is defined by either the static IP for a private
40
+ network configuration or by the SSH host configuration. To disable
41
+ using the private network IP address, set `config.hostmanager.ignore_private_ip`
42
+ to true.
43
+
44
+ A machine's host name is defined by `config.vm.hostname`. If this is not
45
+ set, it falls back to the symbol defining the machine in the Vagrantfile.
46
+
47
+ If the `hostmanager.include_offline` attribute is set to `true`, boxes that are
48
+ up or have a private ip configured will be added 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.vm.define 'example-box' do |node|
63
+ node.vm.hostname = 'example-box-hostname'
64
+ node.vm.network :private_network, ip: '192.168.42.42'
65
+ node.hostmanager.aliases = %w(example-box.localdomain example-box-alias)
66
+ end
67
+ end
68
+ ```
69
+
70
+ ### Provisioner
71
+
72
+ Starting at version 1.5.0, `vagrant up` runs hostmanager before any provisioning occurs.
73
+ If you would like hostmanager to run after or during your provisioning stage,
74
+ you can use hostmanager as a provisioner. This allows you to use the provisioning
75
+ order to ensure that hostmanager runs when desired. The provisioner will collect
76
+ hosts from boxes with the same provider as the running box.
77
+
78
+ Example:
79
+
80
+ ```ruby
81
+ # Disable the default hostmanager behavior
82
+ config.hostmanager.enabled = false
83
+
84
+ # ... possible provisioner config before hostmanager ...
85
+
86
+ # hostmanager provisioner
87
+ config.vm.provision :hostmanager
88
+
89
+ # ... possible provisioning config after hostmanager ...
90
+ ```
91
+
92
+ Custom IP resolver
93
+ ------------------
94
+
95
+ You can customize way, how host manager resolves IP address
96
+ for each machine. This might be handy in case of aws provider,
97
+ where host name is stored in ssh_info hash of each machine.
98
+ This causes generation of invalid /etc/hosts file.
99
+
100
+ Custom IP resolver gives you oportunity to calculate IP address
101
+ for each machine by yourself, giving You also access to the machine that is
102
+ updating /etc/hosts. For example:
103
+
104
+ ```ruby
105
+ config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
106
+ if hostname = (vm.ssh_info && vm.ssh_info[:host])
107
+ `host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
108
+ end
109
+ end
110
+ ```
111
+
112
+ Passwordless sudo
113
+ -----------------
114
+
115
+ To avoid being asked for the password every time the hosts file is updated,
116
+ enable passwordless sudo for the specific command that hostmanager uses to
117
+ update the hosts file.
118
+
119
+ - Add the following snippet to the sudoers file (e.g.
120
+ `/etc/sudoers.d/vagrant_hostmanager`):
121
+
122
+ ```
123
+ Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp <home-directory>/.vagrant.d/tmp/hosts.local /etc/hosts
124
+ %<admin-group> ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE
125
+ ```
126
+
127
+ Replace `<home-directory>` with your actual home directory (e.g.
128
+ `/home/joe`) and `<admin-group>` with the group that is used by the system
129
+ for sudo access (usually `sudo` on Debian/Ubuntu systems and `wheel`
130
+ on Fedora/Red Hat systems).
131
+
132
+ - If necessary, add yourself to the `<admin-group>`:
133
+
134
+ ```
135
+ usermod -aG <admin-group> <user-name>
136
+ ```
137
+
138
+ Replace `<admin-group>` with the group that is used by the system for sudo
139
+ access (see above) and `<user-name>` with you user name.
140
+
141
+ Windows support
142
+ ---------------
143
+
144
+ Hostmanager will detect Windows guests and hosts and use the appropriate
145
+ path for the ```hosts``` file: ```%WINDIR%\System32\drivers\etc\hosts```
146
+
147
+ By default on a Windows host, the ```hosts``` file is not writable without
148
+ elevated privileges. If hostmanager detects that it cannot overwrite the file,
149
+ it will attempt to do so with elevated privileges, causing the
150
+ [UAC](http://en.wikipedia.org/wiki/User_Account_Control) prompt to appear.
151
+
152
+ To avoid the UAC prompt, open ```%WINDIR%\System32\drivers\etc\``` in
153
+ Explorer, right-click the hosts file, go to Properties > Security > Edit
154
+ and give your user Modify permission.
155
+
156
+ ### UAC limitations
157
+
158
+ Due to limitations caused by UAC, cancelling out of the UAC prompt will not cause any
159
+ visible errors, however the ```hosts``` file will not be updated.
160
+
161
+
162
+ Compatibility
163
+ -------------
164
+ This Vagrant plugin has been tested with the following host and guest operating system combinations.
165
+
166
+ Date Tested | Vagrant Version | vagrant-hostmanager Version | Host (Workstation) Operating System | Guest (VirtualBox) Operating System
167
+ ------------|-----------------|-----------------------------|-------------------------------------|--------------------------------------
168
+ 03/23/2016 | 1.8.1 | 1.8.1 | Ubuntu 14.04 LTS | CentOS 7.2
169
+ 03/22/2016 | 1.8.1 | 1.8.1 | OS X 10.11.4 | CentOS 7.2
170
+ 05/03/2017 | 1.9.4 | 1.8.6 | macOS 10.12.4 | Windows Server 2012 R2
171
+
172
+
173
+ Troubleshooting
174
+ -------------
175
+ * Version 1.1 of the plugin prematurely introduced a feature to hook into
176
+ commands other than `vagrant up` and `vagrant destroy`. Version 1.1 broke support
177
+ for some providers. Version 1.2 reverts this feature until a suitable implementation
178
+ supporting all providers is available.
179
+
180
+ * Potentially breaking change in v1.5.0: the running order on `vagrant up` has changed
181
+ so that hostmanager runs before provisioning takes place. This ensures all hostnames are
182
+ available to the guest when it is being provisioned
183
+ (see [#73](https://github.com/devopsgroup-io/vagrant-hostmanager/issues/73)).
184
+ Previously, hostmanager would run as the very last action. If you depend on the old behavior,
185
+ see the [provisioner](#provisioner) section.
186
+
187
+
188
+ Contribute
189
+ ----------
190
+ To contribute, fork then clone the repository, and then the following:
191
+
192
+ **Developing**
193
+
194
+ 1. Ideally, install the version of Vagrant as defined in the `Gemfile`
195
+ 1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/)
196
+ 2. Currently the Bundler version is locked to 1.14.6, please install this version.
197
+ * `gem install bundler -v '1.14.6'`
198
+ 3. Then install vagrant-hostmanager dependancies:
199
+ * `bundle _1.14.6_ install`
200
+
201
+ **Testing**
202
+
203
+ 1. Build and package your newly developed code:
204
+ * `rake gem:build`
205
+ 2. Then install the packaged plugin:
206
+ * `vagrant plugin install pkg/vagrant-hostmanager-*.gem`
207
+ 3. Once you're done testing, roll-back to the latest released version:
208
+ * `vagrant plugin uninstall vagrant-hostmanager`
209
+ * `vagrant plugin install vagrant-hostmanager`
210
+ 4. Once you're satisfied developing and testing your new code, please submit a pull request for review.
211
+
212
+ **Releasing**
213
+
214
+ To release a new version of vagrant-hostmanager you will need to do the following:
215
+
216
+ *(only contributors of the GitHub repo and owners of the project at RubyGems will have rights to do this)*
217
+
218
+ 1. First, bump, commit, and push the version in ~/lib/vagrant-hostmanager/version.rb:
219
+ * Follow [Semantic Versioning](http://semver.org/).
220
+ 2. Then, create a matching GitHub Release (this will also create a tag):
221
+ * Preface the version number with a `v`.
222
+ * https://github.com/devopsgroup-io/vagrant-hostmanager/releases
223
+ 3. You will then need to build and push the new gem to RubyGems:
224
+ * `rake gem:build`
225
+ * `gem push pkg/vagrant-hostmanager-1.6.1.gem`
226
+ 4. Then, when John Doe runs the following, they will receive the updated vagrant-hostmanager plugin:
227
+ * `vagrant plugin update`
228
+ * `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,52 @@
1
+ require 'vagrant-hostmanager-rethinc/hosts_file/updater'
2
+ require 'vagrant-hostmanager-rethinc/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-rethinc::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-rethinc/hosts_file/updater'
2
+ require 'vagrant-hostmanager-rethinc/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-rethinc::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-rethinc/hosts_file/updater'
2
+ require 'vagrant-hostmanager-rethinc/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-rethinc::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,31 @@
1
+ require 'vagrant-hostmanager-rethinc/action/update_all'
2
+ require 'vagrant-hostmanager-rethinc/action/update_guest'
3
+ require 'vagrant-hostmanager-rethinc/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,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-rethinc: 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-rethinc [vm-name]'
14
+ o.separator ''
15
+ o.version = VagrantPlugins::HostManager::VERSION
16
+ o.program_name = 'vagrant hostmanager-rethinc'
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
@@ -0,0 +1,85 @@
1
+ module VagrantPlugins
2
+ module HostManager
3
+ class Config < Vagrant.plugin('2', :config)
4
+ attr_accessor :enabled
5
+ attr_accessor :manage_host
6
+ attr_accessor :manage_guest
7
+ attr_accessor :ignore_private_ip
8
+ attr_accessor :aliases
9
+ attr_accessor :include_offline
10
+ attr_accessor :ip_resolver
11
+
12
+ alias_method :enabled?, :enabled
13
+ alias_method :include_offline?, :include_offline
14
+ alias_method :manage_host?, :manage_host
15
+ alias_method :manage_guest?, :manage_guest
16
+
17
+ def initialize
18
+ @enabled = UNSET_VALUE
19
+ @manage_host = UNSET_VALUE
20
+ @manage_guest = UNSET_VALUE
21
+ @ignore_private_ip = UNSET_VALUE
22
+ @include_offline = UNSET_VALUE
23
+ @aliases = UNSET_VALUE
24
+ @ip_resolver = UNSET_VALUE
25
+ end
26
+
27
+ def finalize!
28
+ @enabled = false if @enabled == UNSET_VALUE
29
+ @manage_host = false if @manage_host == UNSET_VALUE
30
+ @manage_guest = true if @manage_guest == UNSET_VALUE
31
+ @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
32
+ @include_offline = false if @include_offline == UNSET_VALUE
33
+ @aliases = [] if @aliases == UNSET_VALUE
34
+ @ip_resolver = nil if @ip_resolver == UNSET_VALUE
35
+
36
+ @aliases = [ @aliases ].flatten
37
+ end
38
+
39
+ def validate(machine)
40
+ errors = []
41
+
42
+ errors << validate_bool('hostmanager.enabled', @enabled)
43
+ errors << validate_bool('hostmanager.manage_host', @manage_host)
44
+ errors << validate_bool('hostmanager.manage_guest', @manage_guest)
45
+ errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip)
46
+ errors << validate_bool('hostmanager.include_offline', @include_offline)
47
+ errors.compact!
48
+
49
+ # check if aliases option is an Array
50
+ if !machine.config.hostmanager.aliases.kind_of?(Array) &&
51
+ !machine.config.hostmanager.aliases.kind_of?(String)
52
+ errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
53
+ :config_key => 'hostmanager.aliases',
54
+ :is_class => aliases.class.to_s,
55
+ })
56
+ end
57
+
58
+ if !machine.config.hostmanager.ip_resolver.nil? &&
59
+ !machine.config.hostmanager.ip_resolver.kind_of?(Proc)
60
+ errors << I18n.t('vagrant_hostmanager.config.not_a_proc', {
61
+ :config_key => 'hostmanager.ip_resolver',
62
+ :is_class => ip_resolver.class.to_s,
63
+ })
64
+ end
65
+
66
+ errors.compact!
67
+ { "HostManager configuration" => errors }
68
+ end
69
+
70
+ private
71
+
72
+ def validate_bool(key, value)
73
+ if ![TrueClass, FalseClass].include?(value.class) &&
74
+ value != UNSET_VALUE
75
+ I18n.t('vagrant_hostmanager.config.not_a_bool', {
76
+ :config_key => key,
77
+ :value => value.class.to_s
78
+ })
79
+ else
80
+ nil
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,6 @@
1
+ module VagrantPlugins
2
+ module HostManager
3
+ module Errors
4
+ end
5
+ end
6
+ end