@@ -16,24 +16,45 @@ def getIps
16
16
return ips
17
17
end
18
18
19
- def getHostnames
20
- hostnames = Array ( @machine . config . vm . hostname )
21
- if @machine . config . hostsupdater . aliases
22
- hostnames . concat ( @machine . config . hostsupdater . aliases )
19
+ # Get a hash of hostnames indexed by ip, e.g. { 'ip1': ['host1'], 'ip2': ['host2', 'host3'] }
20
+ def getHostnames ( ips )
21
+ hostnames = Hash . new { |h , k | h [ k ] = [ ] }
22
+
23
+ case @machine . config . hostsupdater . aliases
24
+ when Array
25
+ # simple list of aliases to link to all ips
26
+ ips . each do |ip |
27
+ hostnames [ ip ] += @machine . config . hostsupdater . aliases
28
+ end
29
+ when Hash
30
+ # complex definition of aliases for various ips
31
+ @machine . config . hostsupdater . aliases . each do |ip , hosts |
32
+ hostnames [ ip ] += Array ( hosts )
33
+ end
23
34
end
35
+
36
+ # handle default hostname(s) if not already specified in the aliases
37
+ Array ( @machine . config . vm . hostname ) . each do |host |
38
+ if hostnames . none? { |k , v | v . include? ( host ) }
39
+ ips . each do |ip |
40
+ hostnames [ ip ] . unshift host
41
+ end
42
+ end
43
+ end
44
+
24
45
return hostnames
25
46
end
26
47
27
- def addHostEntries ( )
48
+ def addHostEntries
28
49
ips = getIps
29
- hostnames = getHostnames
50
+ hostnames = getHostnames ( ips )
30
51
file = File . open ( @@hosts_path , "rb" )
31
52
hostsContents = file . read
32
53
uuid = @machine . id
33
54
name = @machine . name
34
55
entries = [ ]
35
56
ips . each do |ip |
36
- hostnames . each do |hostname |
57
+ hostnames [ ip ] . each do |hostname |
37
58
entryPattern = hostEntryPattern ( ip , hostname )
38
59
39
60
if hostsContents . match ( /#{ entryPattern } / )
0 commit comments