vagrant-multi-hostsupdater 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 533512cd17c6ca13e71dbe1813d3270d750b887d
4
+ data.tar.gz: 209416ad79ae954b48fd15bf0963cbfb555233fb
5
+ SHA512:
6
+ metadata.gz: e54c18e70573ad52cb72f08ffa780af88f6b7cf0d7d52efc5fc4f2707f86ecc851c6a507b159c54aec153ed661f439fde9181163bcf1e9e30cbe9297fb941a63
7
+ data.tar.gz: 20e512ae6e36f2c0e65b4f0e2d4a152be63288c859adebb1c1d55914bc8f6fcf841b919807aa5cc6d2a70bf7dd78c4ed6311cf7080d306fdb1042836bb6453d0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,50 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "vagrant-hostsupdater"
5
+ s.version = 0.2
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = "Falk Kühnel"
8
+ s.email = "fk@cogitatio.de"
9
+ s.homepage = "https://github.com/cogitatio/vagrant-hostsupdater"
10
+ s.summary = "Enables Vagrant to update hosts file on the host machine"
11
+ s.description = "Enables Vagrant to update hosts file on the host machine"
12
+
13
+
14
+ # s.add_development_dependency "rake"
15
+
16
+ # # The following block of code determines the files that should be included
17
+ # # in the gem. It does this by reading all the files in the directory where
18
+ # # this gemspec is, and parsing out the ignored files from the gitignore.
19
+ # # Note that the entire gitignore(5) syntax is not supported, specifically
20
+ # # the "!" syntax, but it should mostly work correctly.
21
+ # root_path = File.dirname(__FILE__)
22
+ # all_files = Dir.chdir(root_path) { Dir.glob("**/{*,.*}") }
23
+ # all_files.reject! { |file| [".", ".."].include?(File.basename(file)) }
24
+ # gitignore_path = File.join(root_path, ".gitignore")
25
+ # gitignore = File.readlines(gitignore_path)
26
+ # gitignore.map! { |line| line.chomp.strip }
27
+ # gitignore.reject! { |line| line.empty? || line =~ /^(#|!)/ }
28
+
29
+ # unignored_files = all_files.reject do |file|
30
+ # # Ignore any directories, the gemspec only cares about files
31
+ # next true if File.directory?(file)
32
+
33
+ # # Ignore any paths that match anything in the gitignore. We do
34
+ # # two tests here:
35
+ # #
36
+ # # - First, test to see if the entire path matches the gitignore.
37
+ # # - Second, match if the basename does, this makes it so that things
38
+ # # like '.DS_Store' will match sub-directories too (same behavior
39
+ # # as git).
40
+ # #
41
+ # gitignore.any? do |ignore|
42
+ # File.fnmatch(ignore, file, File::FNM_PATHNAME) ||
43
+ # File.fnmatch(ignore, File.basename(file), File::FNM_PATHNAME)
44
+ # end
45
+ # end
46
+
47
+ # s.files = unignored_files
48
+ # s.executables = unignored_files.map { |f| f[/^bin\/(.*)/, 1] }.compact
49
+ # s.require_path = 'lib'
50
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Falk Kühnel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Vagrant::Hostsupdater
2
+
3
+ This plugin adds an entry to your /etc/hosts file on the host system.
4
+
5
+ On **up**, **resume** and **reload** commands, it tries to add the information, if its not already existant in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.
6
+
7
+ On **halt** and **destroy**, those entries will be removed again.
8
+ By setting the remove\_on\_suspend option, you can have them removed on **suspend**, too:
9
+
10
+ config.hostsupdater.remove_on_suspend = true
11
+
12
+
13
+
14
+ ## Installation
15
+
16
+ $ vagrant plugin install vagrant-hostsupdater
17
+
18
+ Uninstall it with:
19
+
20
+ $ vagrant plugin uninstall vagrant-hostsupdater
21
+
22
+ ## Usage
23
+
24
+ At the moment, the only things you need, are the hostname and a :private_network network with a fixed ip.
25
+
26
+ config.vm.network :private_network, ip: "192.168.3.10"
27
+ config.vm.hostname = "www.testing.de"
28
+ config.hostsupdater.aliases = ["alias.testing.de", "alias2.somedomain.com"]
29
+
30
+ This ip and the hostname will be used for the entry in the /etc/hosts file.
31
+
32
+ ## Versions
33
+
34
+ ### 0.0.11
35
+ * bugfix: Fix additional new lines being added to hosts file (Thanks to vincentmac)
36
+
37
+ ### 0.0.10
38
+ * bugfix: wrong path on Windows systems (Thanks to Im0rtality)
39
+
40
+ ### 0.0.9
41
+ * bugfix: now not trying to remove anything if no machine id is given
42
+
43
+ ### 0.0.8
44
+ * trying to use proper windows hosts file
45
+
46
+ ### 0.0.7
47
+ * using hashed uids now to identify hosts entries (you might need to remove previous hostentries manually)
48
+ * fixed removing of host entries
49
+
50
+ ### 0.0.6
51
+ * no sudo, if /etc/hosts is writeable
52
+
53
+ ### 0.0.5
54
+ * option added to not remove hosts on suspend, adding hosts on resume (Thanks to Svelix)
55
+
56
+ ### 0.0.4
57
+ * fixed problem with removing hosts entries on destroy command (Thanks to Andy Bohne)
58
+
59
+ ### 0.0.3
60
+ * added aliases config option to define additional hostnames
61
+
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,11 @@
1
+ require "vagrant-hostsupdater/version"
2
+ require "vagrant-hostsupdater/plugin"
3
+
4
+ module VagrantPlugins
5
+ module HostsUpdater
6
+ def self.source_root
7
+ @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,20 @@
1
+ module VagrantPlugins
2
+ module HostsUpdater
3
+ module Action
4
+ class CacheHosts
5
+ include HostsUpdater
6
+
7
+ def initialize(app, env)
8
+ @app = app
9
+ @machine = env[:machine]
10
+ end
11
+
12
+ def call(env)
13
+ cacheHostEntries
14
+ @app.call(env)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ module VagrantPlugins
2
+ module HostsUpdater
3
+ module Action
4
+ class RemoveHosts
5
+ include HostsUpdater
6
+
7
+ def initialize(app, env)
8
+ @app = app
9
+ @machine = env[:machine]
10
+ @ui = env[:ui]
11
+ end
12
+
13
+ def call(env)
14
+ machine_action = env[:machine_action]
15
+ if machine_action != :destroy || !@machine.id
16
+ if machine_action != :suspend || @machine.config.hostsupdater.remove_on_suspend
17
+ @ui.info "Removing hosts"
18
+ removeHostEntries
19
+ else
20
+ @ui.info "Removing hosts on suspend disabled"
21
+ end
22
+ end
23
+ @app.call(env)
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "../HostsUpdater"
2
+ module VagrantPlugins
3
+ module HostsUpdater
4
+ module Action
5
+ class UpdateHosts
6
+ include HostsUpdater
7
+
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @ui = env[:ui]
13
+ end
14
+
15
+ def call(env)
16
+ @ui.info "Checking for host entries"
17
+ @app.call(env)
18
+ addHostEntries()
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,129 @@
1
+ module VagrantPlugins
2
+ module MultiHostsUpdater
3
+ module MultiHostsUpdater
4
+ @@hosts_path = Vagrant::Util::Platform.windows? ? File.expand_path('system32/drivers/etc/hosts', ENV['windir']) : '/etc/hosts'
5
+
6
+ def getIps
7
+ ips = []
8
+ @machine.config.vm.networks.each do |network|
9
+ key, options = network[0], network[1]
10
+ ip = options[:ip] if key == :private_network
11
+ ips.push(ip) if ip
12
+ end
13
+ return ips
14
+ end
15
+
16
+ # Get hostnames by specific IP.
17
+ # This option is only valid if a Hash is provided
18
+ # from the `config.MultiHostsUpdater.aliases` parameter
19
+ def getHostnames(ip=nil)
20
+
21
+ hostnames = []
22
+ if @machine.config.MultiHostsUpdater.aliases.is_a?(Hash)
23
+ hostnames = @machine.config.MultiHostsUpdater.aliases[ip] || hostnames
24
+ else
25
+ hostnames = Array(@machine.config.vm.hostname)
26
+ hostnames.concat(@machine.config.MultiHostsUpdater.aliases)
27
+ end
28
+
29
+ return hostnames
30
+ end
31
+
32
+ def addHostEntries()
33
+ ips = getIps
34
+ file = File.open(@@hosts_path, "rb")
35
+ hostsContents = file.read
36
+ uuid = @machine.id
37
+ name = @machine.name
38
+ entries = []
39
+ ips.each do |ip|
40
+ hostnames = getHostnames(ip)
41
+ hostEntries = getHostEntries(ip, hostnames, name, uuid)
42
+ hostEntries.each do |hostEntry|
43
+ escapedEntry = Regexp.quote(hostEntry)
44
+ if !hostsContents.match(/#{escapedEntry}/)
45
+ @ui.info "adding to (#@@hosts_path) : #{hostEntry}"
46
+ entries.push(hostEntry)
47
+ end
48
+ end
49
+ end
50
+ addToHosts(entries)
51
+ end
52
+
53
+ def cacheHostEntries
54
+ @machine.config.MultiHostsUpdater.id = @machine.id
55
+ end
56
+
57
+ def removeHostEntries
58
+ if !@machine.id and !@machine.config.MultiHostsUpdater.id
59
+ @ui.warn "No machine id, nothing removed from #@@hosts_path"
60
+ return
61
+ end
62
+ file = File.open(@@hosts_path, "rb")
63
+ hostsContents = file.read
64
+ uuid = @machine.id || @machine.config.MultiHostsUpdater.id
65
+ hashedId = Digest::MD5.hexdigest(uuid)
66
+ if hostsContents.match(/#{hashedId}/)
67
+ removeFromHosts
68
+ end
69
+ end
70
+
71
+ def host_entry(ip, hostnames, name, uuid = self.uuid)
72
+ %Q(#{ip} #{hostnames.join(' ')} #{signature(name, uuid)})
73
+ end
74
+
75
+ def getHostEntries(ip, hostnames, name, uuid = self.uuid)
76
+ entries = []
77
+ hostnames.each do |hostname|
78
+ entries.push(%Q(#{ip} #{hostname} #{signature(name, uuid)}))
79
+ end
80
+ return entries
81
+ end
82
+
83
+ def addToHosts(entries)
84
+ return if entries.length == 0
85
+ content = entries.join("\n").strip
86
+ if !File.writable?(@@hosts_path)
87
+ sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
88
+ else
89
+ content = "\n" + content
90
+ hostsFile = File.open(@@hosts_path, "a")
91
+ hostsFile.write(content)
92
+ hostsFile.close()
93
+ end
94
+ end
95
+
96
+ def removeFromHosts(options = {})
97
+ uuid = @machine.id || @machine.config.MultiHostsUpdater.id
98
+ hashedId = Digest::MD5.hexdigest(uuid)
99
+ if !File.writable?(@@hosts_path)
100
+ sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
101
+ else
102
+ hosts = ""
103
+ File.open(@@hosts_path).each do |line|
104
+ hosts << line unless line.include?(hashedId)
105
+ end
106
+ hostsFile = File.open(@@hosts_path, "w")
107
+ hostsFile.write(hosts)
108
+ hostsFile.close()
109
+ end
110
+ end
111
+
112
+
113
+
114
+ def signature(name, uuid = self.uuid)
115
+ hashedId = Digest::MD5.hexdigest(uuid)
116
+ %Q(# VAGRANT: #{hashedId} (#{name}) / #{uuid})
117
+ end
118
+
119
+ def sudo(command)
120
+ return if !command
121
+ if Vagrant::Util::Platform.windows?
122
+ `#{command}`
123
+ else
124
+ `sudo #{command}`
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,30 @@
1
+ module VagrantPlugins
2
+ module HostsUpdater
3
+ class Command < Vagrant.plugin('2', :command)
4
+ #include HostsFile
5
+
6
+ def execute
7
+ # options = {}
8
+ # opts = OptionParser.new do |o|
9
+ # o.banner = 'Usage: vagrant hostmanager [vm-name]'
10
+ # o.separator ''
11
+
12
+ # o.on('--provider provider', String,
13
+ # 'Update machines with the specific provider.') do |provider|
14
+ # options[:provider] = provider
15
+ # end
16
+ # end
17
+
18
+ # argv = parse_options(opts)
19
+ # options[:provider] ||= @env.default_provider
20
+
21
+ # generate(@env, options[:provider].to_sym)
22
+
23
+ # with_target_vms(argv, options) do |machine|
24
+ # update(machine)
25
+ # end
26
+ puts "ran command"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module HostsUpdater
5
+ class Config < Vagrant.plugin("2", :config)
6
+
7
+ # Array of hostnames to add or a Map[String => Array] of IPs to hostnames.
8
+ #
9
+ # Array syntax: aliases = ['foo.com', 'bar.com']
10
+ # Map syntax: aliases = {'10.0.0.1' => ['foo.com', 'bar.com'], '10.0.0.2' => ['baz.com', 'bat.com']}
11
+ attr_accessor :aliases
12
+ attr_accessor :id
13
+ attr_accessor :remove_on_suspend
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,53 @@
1
+ require "vagrant-hostsupdater/Action/UpdateHosts"
2
+ require "vagrant-hostsupdater/Action/CacheHosts"
3
+ require "vagrant-hostsupdater/Action/RemoveHosts"
4
+
5
+ module VagrantPlugins
6
+ module HostsUpdater
7
+ class Plugin < Vagrant.plugin('2')
8
+ name 'HostsUpdater'
9
+ description <<-DESC
10
+ This plugin manages the /etc/hosts file for the host machine. An entry is
11
+ created for the hostname attribute in the vm.config.
12
+ DESC
13
+
14
+ config(:hostsupdater) do
15
+ require_relative 'config'
16
+ Config
17
+ end
18
+
19
+ action_hook(:hostsupdater, :machine_action_up) do |hook|
20
+ hook.append(Action::UpdateHosts)
21
+ end
22
+
23
+ action_hook(:hostsupdater, :machine_action_halt) do |hook|
24
+ hook.append(Action::RemoveHosts)
25
+ end
26
+
27
+ action_hook(:hostsupdater, :machine_action_suspend) do |hook|
28
+ hook.append(Action::RemoveHosts)
29
+ end
30
+
31
+ action_hook(:hostsupdater, :machine_action_destroy) do |hook|
32
+ hook.prepend(Action::CacheHosts)
33
+ end
34
+
35
+ action_hook(:hostsupdater, :machine_action_destroy) do |hook|
36
+ hook.append(Action::RemoveHosts)
37
+ end
38
+
39
+ action_hook(:hostsupdater, :machine_action_reload) do |hook|
40
+ hook.append(Action::UpdateHosts)
41
+ end
42
+
43
+ action_hook(:hostsupdater, :machine_action_resume) do |hook|
44
+ hook.append(Action::UpdateHosts)
45
+ end
46
+
47
+ command(:hostsupdater) do
48
+ require_relative 'command'
49
+ Command
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module MultiHostsUpdater
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-multi-hostsupdater/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-multi-hostsupdater"
8
+ spec.version = VagrantPlugins::MultiHostsUpdater::VERSION
9
+ spec.authors = ["Falk Kühnel"]
10
+ spec.email = ["fk@cogitatio.de"]
11
+ spec.description = %q{Enables Vagrant to update hosts file on the host machine}
12
+ spec.summary = %q{Enables Vagrant to update hosts file on the host machine}
13
+ spec.homepage = "https://github.com/cogitatio/vagrant-multi-hostsupdater"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-multi-hostsupdater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Falk Kühnel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Enables Vagrant to update hosts file on the host machine
42
+ email:
43
+ - fk@cogitatio.de
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/vagrant-multi-hostsupdater.rb
54
+ - lib/vagrant-multi-hostsupdater/Action/CacheHosts.rb
55
+ - lib/vagrant-multi-hostsupdater/Action/RemoveHosts.rb
56
+ - lib/vagrant-multi-hostsupdater/Action/UpdateHosts.rb
57
+ - lib/vagrant-multi-hostsupdater/HostsUpdater.rb
58
+ - lib/vagrant-multi-hostsupdater/command.rb
59
+ - lib/vagrant-multi-hostsupdater/config.rb
60
+ - lib/vagrant-multi-hostsupdater/plugin.rb
61
+ - lib/vagrant-multi-hostsupdater/version.rb
62
+ - vagrant-multi-hostsupdater.gemspec
63
+ homepage: https://github.com/cogitatio/vagrant-multi-hostsupdater
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Enables Vagrant to update hosts file on the host machine
87
+ test_files: []