vagrant-etchosts 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /tmp
2
+ /pkg
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # vagrant-etchosts
2
+
3
+ This is a Vagrant 1.4+ (at least, that's all I've tested it on) plugin to
4
+ add and remove entries from `/etc/hosts` on the machine on which Vagrant is
5
+ running.
6
+
7
+ It differs from the other plugins available to do this job in several ways:
8
+
9
+ * It can detect and use dynamic IP addresses (DHCP FTW);
10
+
11
+ * It doesn't *require* any plugin-specific configuration (it uses
12
+ `config.vm.hostname` and the IP address assigned dynamically), although
13
+ you can specify additional aliases if you choose;
14
+
15
+ * It works on modern versions of Vagrant.
16
+
17
+ Its limitations are:
18
+
19
+ * **It *only* updates the *host's* `hosts` file**: I'm using
20
+ `vagrant-libvirt`, and libvirt provides a `dnsmasq` setup that will
21
+ respond to DNS requests with entries from `/etc/hosts`. If you need to
22
+ have your guests `/etc/hosts` played with, patches are welcome.
23
+
24
+ * **It doesn't handle statically-assigned addresses or multiple networks**:
25
+ I haven't yet had a need for other addressing schemes, so I haven't added
26
+ them yet. If you want it, send me a patch or wait until I've found a
27
+ need for multiple networks and/or statically assigned addresses.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler/gem_helper'
2
+
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,42 @@
1
+ require 'tempfile'
2
+
3
+ module VagrantPlugins::EtcHosts
4
+ class AddEtcHostsEntry
5
+ ETC_HOSTS = '/etc/hosts'
6
+
7
+ def initialize(app, env)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ @app.call(env)
13
+
14
+ # Best place to find the network config? In the SSH config.
15
+ addr = env[:machine].ssh_info[:host]
16
+ fqdn = env[:machine].config.vm.hostname
17
+ id = env[:machine].id
18
+
19
+ if fqdn.nil?
20
+ fqdn = env[:machine].name.to_s
21
+ end
22
+
23
+ shortname = fqdn.split('.').first
24
+ shortname = "" if shortname == fqdn
25
+
26
+ tmp = Tempfile.open('vagrant-etchosts', env[:tmp_path], 'w')
27
+
28
+ begin
29
+ # Dump existing /etc/hosts content into our temp file
30
+ tmp.write(File.read(ETC_HOSTS))
31
+ tmp << "#{addr} #{fqdn} #{shortname} # VAGRANT ID: #{id}\n"
32
+
33
+ tmp.close
34
+ `sudo cp #{tmp.path} /etc/hosts`
35
+ `sudo pkill -HUP dnsmasq`
36
+ ensure
37
+ tmp.close rescue nil
38
+ tmp.unlink rescue nil
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ module VagrantPlugins::EtcHosts
2
+ class RemoveEtcHostsEntry
3
+ ETC_HOSTS = '/etc/hosts'
4
+
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ id = env[:machine].id
11
+ return if id.nil? or id.empty?
12
+
13
+ tmp = Tempfile.open('vagrant-etchosts', env[:tmp_path], 'w')
14
+
15
+ begin
16
+ tmp.write(File.readlines(ETC_HOSTS).reject { |l| l =~ /# VAGRANT ID: #{id}$/ }.join(""))
17
+
18
+ tmp.close
19
+ `sudo cp #{tmp.path} /etc/hosts`
20
+ `sudo pkill -HUP dnsmasq`
21
+ ensure
22
+ tmp.close rescue nil
23
+ tmp.unlink rescue nil
24
+ end
25
+
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ require 'vagrant-etchosts/action/add_etc_hosts_entry'
2
+ require 'vagrant-etchosts/action/remove_etc_hosts_entry'
3
+
4
+ module VagrantPlugins::EtcHosts
5
+ class Plugin < Vagrant.plugin("2")
6
+ name 'EtcHosts'
7
+ description <<-DESC.gsub(/^\t\t\t/, '')
8
+ This plugin manages the /etc/hosts file on the host machine.
9
+ DESC
10
+
11
+ action_hook(:etchosts, :machine_action_up) do |hook|
12
+ hook.after ::Vagrant::Action::Builtin::SetHostname, ::VagrantPlugins::EtcHosts::AddEtcHostsEntry
13
+ end
14
+
15
+ action_hook(:etchosts, :machine_action_destroy) do |hook|
16
+ hook.prepend VagrantPlugins::EtcHosts::RemoveEtcHostsEntry
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module VagrantPlugins
2
+ module EtcHosts
3
+ VERSION_MAJOR = 0
4
+ VERSION_MINOR = 0
5
+ VERSION_PATCH = 0
6
+
7
+ VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module VagrantPlugins
2
+ module EtcHosts
3
+ end
4
+ end
5
+
6
+ require 'vagrant-etchosts/plugin'
7
+ require 'vagrant-etchosts/version'
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $: << File.expand_path('../lib', __FILE__)
5
+
6
+ require 'vagrant-etchosts/version'
7
+
8
+ Gem::Specification.new do |gem|
9
+ gem.name = 'vagrant-etchosts'
10
+ gem.version = VagrantPlugins::EtcHosts::VERSION
11
+ gem.authors = ['Matt Palmer']
12
+ gem.email = ['theshed+vagrant-etchosts@hezmatt.org']
13
+ gem.description = %q{A Vagrant plugin that manages the /etc/hosts file on the host}
14
+ gem.summary = gem.description
15
+ gem.license = 'GPLv3'
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ['lib']
20
+
21
+ gem.add_development_dependency 'bundler', '~> 1.3'
22
+ gem.add_development_dependency 'rake'
23
+ end
24
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-etchosts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Palmer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A Vagrant plugin that manages the /etc/hosts file on the host
47
+ email:
48
+ - theshed+vagrant-etchosts@hezmatt.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - README.md
55
+ - Rakefile
56
+ - lib/vagrant-etchosts.rb
57
+ - lib/vagrant-etchosts/action/add_etc_hosts_entry.rb
58
+ - lib/vagrant-etchosts/action/remove_etc_hosts_entry.rb
59
+ - lib/vagrant-etchosts/plugin.rb
60
+ - lib/vagrant-etchosts/version.rb
61
+ - vagrant-etchosts.gemspec
62
+ homepage:
63
+ licenses:
64
+ - GPLv3
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: A Vagrant plugin that manages the /etc/hosts file on the host
87
+ test_files: []
88
+ has_rdoc: