Module: VagrantPlugins::MultiHostsUpdater::MultiHostsUpdater

Defined in:
lib/vagrant-multi-hostsupdater/HostsUpdater.rb

Constant Summary collapse

@@hosts_path =
Vagrant::Util::Platform.windows? ? File.expand_path('system32/drivers/etc/hosts', ENV['windir']) : '/etc/hosts'

Instance Method Summary collapse

Instance Method Details

#addHostEntriesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 32

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



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 83

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

#cacheHostEntriesObject



53
54
55
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 53

def cacheHostEntries
  @machine.config.MultiHostsUpdater.id = @machine.id
end

#getHostEntries(ip, hostnames, name, uuid = self.uuid) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 75

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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 19

def getHostnames(ip=nil)

  hostnames = []
  if @machine.config.MultiHostsUpdater.aliases.is_a?(Hash)
    hostnames = @machine.config.MultiHostsUpdater.aliases[ip] || hostnames
  else
    hostnames = Array(@machine.config.vm.hostname)
    hostnames.concat(@machine.config.MultiHostsUpdater.aliases)
  end

  return hostnames
end

#getIpsObject



6
7
8
9
10
11
12
13
14
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 6

def getIps
  ips = []
  @machine.config.vm.networks.each do |network|
    key, options = network[0], network[1]
    ip = options[:ip] if key == :private_network
    ips.push(ip) if ip
  end
  return ips
end

#host_entry(ip, hostnames, name, uuid = self.uuid) ⇒ Object



71
72
73
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 71

def host_entry(ip, hostnames, name, uuid = self.uuid)
  %Q(#{ip}  #{hostnames.join(' ')}  #{signature(name, uuid)})
end

#removeFromHosts(options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 96

def removeFromHosts(options = {})
  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

#removeHostEntriesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 57

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



114
115
116
117
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 114

def signature(name, uuid = self.uuid)
  hashedId = Digest::MD5.hexdigest(uuid)
  %Q(# VAGRANT: #{hashedId} (#{name}) / #{uuid})
end

#sudo(command) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/vagrant-multi-hostsupdater/HostsUpdater.rb', line 119

def sudo(command)
  return if !command
  if Vagrant::Util::Platform.windows?
    `#{command}`
  else
    `sudo #{command}`
  end
end