Class: VagrantPlugins::HostManager::HostsFile::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-hostmanager/hosts_file/updater.rb

Defined Under Namespace

Modules: WindowsSupport

Instance Method Summary collapse

Constructor Details

#initialize(global_env, provider) ⇒ Updater

Returns a new instance of Updater.



9
10
11
12
13
14
15
# File 'lib/vagrant-hostmanager/hosts_file/updater.rb', line 9

def initialize(global_env, provider)
  @global_env = global_env
  @config = Util.get_config(@global_env)
  @provider = provider
  @logger = Log4r::Logger.new('vagrant::hostmanager::updater')
  @logger.debug("init updater")
end

Instance Method Details

#update_guest(machine) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant-hostmanager/hosts_file/updater.rb', line 17

def update_guest(machine)
  return unless machine.communicate.ready?

  if (machine.communicate.test("uname -s | grep SunOS"))
    realhostfile = "/etc/inet/hosts"
    line_endings = "lf"
  elsif (machine.communicate.test("test -d $Env:SystemRoot"))
    windir = ""
    machine.communicate.execute("echo %SYSTEMROOT%", {:shell => :cmd}) do |type, contents|
      windir << contents.gsub("\r\n", '') if type == :stdout
    end
    realhostfile = "#{windir}\\System32\\drivers\\etc\\hosts"
    line_endings = "crlf"
  else
    realhostfile = "/etc/hosts"
    line_endings = "lf"
  end

  # download and modify file with Vagrant-managed entries
  file = @global_env.tmp_path.join("hosts.#{machine.name}")
  machine.communicate.download(realhostfile, file)

  @logger.debug("file is: #{file.to_s}")
  @logger.debug("class of file is: #{file.class}")

  if update_file(file, machine, false, line_endings)
    # upload modified file and remove temporary file
    machine.communicate.upload(file.to_s, "/tmp/hosts.#{machine.name}")
    if windir
      machine.communicate.sudo("mv -force /tmp/hosts.#{machine.name} #{realhostfile}")
    else
      machine.communicate.sudo("cat /tmp/hosts.#{machine.name} > #{realhostfile} && rm /tmp/hosts.#{machine.name}")
    end
  end

end

#update_hostObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vagrant-hostmanager/hosts_file/updater.rb', line 54

def update_host
  # copy and modify hosts file on host with Vagrant-managed entries
  file = @global_env.tmp_path.join('hosts.local')

  if WindowsSupport.windows?
    # lazily include windows Module
    class << self
      include WindowsSupport unless include? WindowsSupport
    end
    hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
    copy_proc = Proc.new { windows_copy_file(file, hosts_location) }
    line_endings = "crlf"
  else
    hosts_location = '/etc/hosts'
    copy_proc = Proc.new { `[ -w #{hosts_location} ] && cat #{file} > #{hosts_location} || sudo cp #{file} #{hosts_location}` }
    line_endings = "lf"
  end

  FileUtils.cp(hosts_location, file)

  if update_file(file, nil, true, line_endings)
    copy_proc.call
  end
end