Module: VagrantPlugins::MultiHostsUpdater::MultiHostsUpdater
- Included in:
- Action::CacheHosts, Action::RemoveHosts, Action::UpdateHosts
- Defined in:
- lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb
Constant Summary collapse
- @@hosts_path =
Vagrant::Util::Platform.windows? ? File.('system32/drivers/etc/hosts', ENV['windir']) : '/etc/hosts'
Instance Method Summary collapse
- #addHostEntries ⇒ Object
- #addToHosts(entries) ⇒ Object
- #cacheHostEntries ⇒ Object
- #getHostEntries(ip, hostnames, name, uuid = self.uuid) ⇒ Object
-
#getHostnames(ip = nil) ⇒ Object
Get hostnames by specific IP.
- #getIps ⇒ Object
- #host_entry(ip, hostnames, name, uuid = self.uuid) ⇒ Object
- #removeFromHosts(options = {}) ⇒ Object
- #removeHostEntries ⇒ Object
- #signature(name, uuid = self.uuid) ⇒ Object
- #sudo(command) ⇒ Object
Instance Method Details
#addHostEntries ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 34 def addHostEntries() ips = getIps file = File.open(@@hosts_path, "rb") hostsContents = file.read uuid = @machine.id name = @machine.name entries = [] ips.each do |ip| hostnames = getHostnames(ip) hostEntries = getHostEntries(ip, hostnames, name, uuid) hostEntries.each do |hostEntry| escapedEntry = Regexp.quote(hostEntry) if !hostsContents.match(/#{escapedEntry}/) @ui.info "adding to (#@@hosts_path) : #{hostEntry}" entries.push(hostEntry) end end end addToHosts(entries) end |
#addToHosts(entries) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 85 def addToHosts(entries) return if entries.length == 0 content = entries.join("\n").strip if !File.writable?(@@hosts_path) sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path')) else content = "\n" + content hostsFile = File.open(@@hosts_path, "a") hostsFile.write(content) hostsFile.close() end end |
#cacheHostEntries ⇒ Object
55 56 57 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 55 def cacheHostEntries @machine.config.multihostsupdater.id = @machine.id end |
#getHostEntries(ip, hostnames, name, uuid = self.uuid) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 77 def getHostEntries(ip, hostnames, name, uuid = self.uuid) entries = [] hostnames.each do |hostname| entries.push(%Q(#{ip} #{hostname} #{signature(name, uuid)})) end return entries end |
#getHostnames(ip = nil) ⇒ Object
Get hostnames by specific IP. This option is only valid if a Hash is provided from the ‘config.multihostsupdater.aliases` parameter
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 22 def getHostnames(ip=nil) hostnames = [] if @machine.config.multihostsupdater.aliases.is_a?(Hash) hostnames = @machine.config.multihostsupdater.aliases[ip] || hostnames elsif @machine.config.multihostsupdater.aliases.is_a?(Array) hostnames = Array(@machine.config.vm.hostname) if !@machine.config.vm.hostname.nil? hostnames.concat(@machine.config.multihostsupdater.aliases) end return hostnames end |
#getIps ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 6 def getIps ips = [] if @machine.config.multihostsupdater.force_ips.is_a?(Array) return @machine.config.multihostsupdater.force_ips end @machine.config.vm.networks.each do |network| key, = network[0], network[1] ip = [:ip] if key == :private_network ips.push(ip) if ip end return ips end |
#host_entry(ip, hostnames, name, uuid = self.uuid) ⇒ Object
73 74 75 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 73 def host_entry(ip, hostnames, name, uuid = self.uuid) %Q(#{ip} #{hostnames.join(' ')} #{signature(name, uuid)}) end |
#removeFromHosts(options = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 98 def removeFromHosts( = {}) uuid = @machine.id || @machine.config.multihostsupdater.id hashedId = Digest::MD5.hexdigest(uuid) if !File.writable?(@@hosts_path) sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path)) else hosts = "" File.open(@@hosts_path).each do |line| hosts << line unless line.include?(hashedId) end hostsFile = File.open(@@hosts_path, "w") hostsFile.write(hosts) hostsFile.close() end end |
#removeHostEntries ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 59 def removeHostEntries if !@machine.id and !@machine.config.multihostsupdater.id @ui.warn "No machine id, nothing removed from #@@hosts_path" return end file = File.open(@@hosts_path, "rb") hostsContents = file.read uuid = @machine.id || @machine.config.multihostsupdater.id hashedId = Digest::MD5.hexdigest(uuid) if hostsContents.match(/#{hashedId}/) removeFromHosts end end |
#signature(name, uuid = self.uuid) ⇒ Object
116 117 118 119 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 116 def signature(name, uuid = self.uuid) hashedId = Digest::MD5.hexdigest(uuid) %Q(# VAGRANT: #{hashedId} (#{name}) / #{uuid}) end |
#sudo(command) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/vagrant-multi-hostsupdater/MultiHostsUpdater.rb', line 121 def sudo(command) return if !command if Vagrant::Util::Platform.windows? `#{command}` else `sudo #{command}` end end |