Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent wiping of addresses of other providers #169

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7c06c81
Fix for issue #108, add a flag to manage guest hosts file.
Nov 14, 2014
ef2f01c
Improve README.md for Passwordless sudo.
tjanez Oct 13, 2015
270c15d
Prevent wiping of addresses of other providers
Nov 6, 2015
7715a76
Updating Contribute section to be specific.
seth-reeser Dec 30, 2015
117dc4b
v1.7.0 release.
seth-reeser Dec 30, 2015
c92cff0
Update README.md
seth-reeser Jan 15, 2016
dcd0351
Merge pull request #167 from tjanez/readme-passwordless-sudo
seth-reeser Jan 20, 2016
11677f2
Update README.md
seth-reeser Jan 27, 2016
dd43278
Ensure permissions on /etc/hosts stay intact
Jan 29, 2016
d1ba474
Merge pull request #176 from criticalstack/fix-selinux-permissions-on…
seth-reeser Jan 29, 2016
f1b0174
Releasing v1.7.1
seth-reeser Jan 29, 2016
3b840be
Merge pull request #125 from damienjoldersma/feature/manage-guest-flag
seth-reeser Feb 11, 2016
6f4c0d7
Releasing v1.8.0
seth-reeser Feb 11, 2016
a179758
Fixes #177 - Manage the guest by default.
seth-reeser Feb 11, 2016
ab8b041
Prevent wiping of addresses of other providers
Nov 6, 2015
2b5e0ae
Merge branch 'master' of github.com:lomignet/vagrant-hostmanager
Feb 19, 2016
494a3e3
Merge pull request #117 from pbitty/separate_aliases_option
seth-reeser Jul 22, 2015
13d95ba
Add option add_current_fqdn
Feb 19, 2016
9b7f385
refactor to have machine list in its own method
Feb 19, 2016
abac3e8
handle short name and removing from 127.*
Feb 19, 2016
43a88f8
remove old debug statement
Feb 19, 2016
aa39351
Prepare vagrant-hostoverseer release
Feb 22, 2016
b30f835
prepare public release
Feb 22, 2016
2a54f78
Add git url in gemspec
Feb 22, 2016
ae39240
Learning how to publish ruby gems...
Feb 22, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handle short name and removing from 127.*
  • Loading branch information
Guillaume committed Feb 19, 2016
commit abac3e841237ae42ca3411c15fc04cdfafd7fca3
64 changes: 50 additions & 14 deletions lib/vagrant-hostmanager/hosts_file/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_hosts_file_entry(machine, resolving_machine)
ip = get_ip_address(machine, resolving_machine)

unless ip.nil?
names = get_names(machine)
names = get_names(machine, resolving_machine)
if @current_machine_config.hostmanager.aliases_on_separate_lines
names.map { |a| "#{ip}\t#{a}" }.join("\n") + "\n"
else
Expand All @@ -117,32 +117,35 @@ def get_ip_address(machine, resolving_machine)
end
end

# get all names, in the right order (fqdn first if relevant)
def get_names(machine)
# get all names including aliases, in the right order (fqdn first if relevant)
def get_names(machine, resolving_machine)
host = machine.config.vm.hostname || machine.name
aliases = machine.config.hostmanager.aliases
all_names = [host] + aliases

# Optionally prepend current fqdn as well. Useful with hostname set outside
# vagrant (eg. aws)
if @current_machine_config.hostmanager.add_current_fqdn
fqdn = get_fqdn(machine)
unless fqdn.nil?
# put fqdn in front, removing it from the rest of the line if present
all_names = [fqdn] + all_names.select { |a| a != fqdn }
end
dns = get_dns(machine)
# put fqdn in front
all_names = dns + all_names
end
all_names

all_names.uniq # order is kept b uniq
end

def get_fqdn(machine)
fqdn = nil
# return fqdn *and* short name
def get_dns(machine)
names = []
unless machine.nil?
machine.communicate.execute('/bin/hostname -f') do |type, hostname|
fqdn = hostname.strip
names += [hostname.strip]
end
machine.communicate.execute('/bin/hostname') do |type, hostname|
names += [hostname.strip]
end
end
fqdn
names
end

def get_machines
Expand Down Expand Up @@ -176,7 +179,40 @@ def get_new_content(header, footer, body, old_content)
footer_pattern = Regexp.quote(footer)
pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
# Replace existing block or append
old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block
newcontent = old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block

### remove name duplication in 127.*
cleancontent = ''

all_names = []
# First, extract back names from the body. If a name appear there, it
# should not be anywhere else.
body.each_line do |line|
tokens = line.sub(/#.*$/, '').strip.split(/\s+/)
ip = tokens.shift
unless tokens.empty?
all_names += tokens
end
end
puts(all_names)

# Then remove those names from any 127.* lines
newcontent.each_line do |line|
if line =~ /^\s*127\./
# Here be dragons.
tokens = line.sub(/#.*$/, '').strip.split(/\s+/)
ip=tokens.shift
dedup = tokens - all_names
unless dedup.empty?
cleancontent += "#{ip}\t" + dedup.join(' ') + "\n"
end
else
cleancontent += line
end
end

cleancontent

end

def read_or_create_id
Expand Down