Module: VagrantPlugins::GoodHosts::GoodHosts

Included in:
Action::RemoveHosts, Action::UpdateHosts
Defined in:
lib/vagrant-goodhosts/GoodHosts.rb

Instance Method Summary collapse

Instance Method Details

#addHostEntriesObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vagrant-goodhosts/GoodHosts.rb', line 98

def addHostEntries
  ips = getIps
  hostnames = getHostnames(ips)
  error = false
  errorText = ""
  cli = get_cli
  ips.each do |ip|
    hostnames[ip].each do |hostname|
        ip_address = ip
        if !ip_address.nil?
          @ui.info "[vagrant-goodhosts] - found entry for: #{ip_address} #{hostname}"
          if cli.include? ".exe"
              stdin, stdout, stderr, wait_thr = Open3.popen3(cli, "a", ip_address, hostname)
          else
              stdin, stdout, stderr, wait_thr = Open3.popen3('sudo', cli, "a", ip_address, hostname)
          end
          if !wait_thr.value.success?
              error = true
              errorText = stderr.read.strip
          end
        end
    end
  end
  printReadme(error, errorText)
end

#get_cliObject



61
62
63
64
65
66
67
# File 'lib/vagrant-goodhosts/GoodHosts.rb', line 61

def get_cli
    cli = get_OS
    path = File.expand_path(File.dirname(File.dirname(__FILE__))) + '/vagrant-goodhosts/bundle/'
    path = "#{path}#{cli}"
    
    return path
end

#get_OSObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vagrant-goodhosts/GoodHosts.rb', line 45

def get_OS
  return os ||= (
  host_os = RbConfig::CONFIG['host_os']
  case host_os
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      :'cli.exe'
  when /darwin|mac os/
      :'cli_osx'
  when /linux/
      :'cli'
  else
      raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
  end
  )
end

#getHostnames(ips) ⇒ Object

Get a hash of hostnames indexed by ip, e.g. { ‘ip1’: [‘host1’], ‘ip2’: [‘host2’, ‘host3’] }



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vagrant-goodhosts/GoodHosts.rb', line 70

def getHostnames(ips)
  hostnames = Hash.new { |h, k| h[k] = [] }

  case @machine.config.goodhosts.aliases
  when Array
    # simple list of aliases to link to all ips
    ips.each do |ip|
      hostnames[ip] += @machine.config.goodhosts.aliases
    end
  when Hash
    # complex definition of aliases for various ips
    @machine.config.goodhosts.aliases.each do |ip, hosts|
      hostnames[ip] += Array(hosts)
    end
  end

  # handle default hostname(s) if not already specified in the aliases
  Array(@machine.config.vm.hostname).each do |host|
    if hostnames.none? { |k, v| v.include?(host) }
      ips.each do |ip|
        hostnames[ip].unshift host
      end
    end
  end

  return hostnames
end

#getIpsObject



8
9
10
11
12
13
14
15
16
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
# File 'lib/vagrant-goodhosts/GoodHosts.rb', line 8

def getIps
  if Vagrant.has_plugin?("vagrant-hostsupdater")
      @ui.warn "[vagrant-goodhosts] Warning: The vagrant-hostsupdater plugin is installed, hostsupdater always adds the VM name even if the VagrantFile says not to. This shouldn't cause issues, but if it does, uninstall hostsupdater" 
  end
  
  ips = []

      @machine.config.vm.networks.each do |network|
        key, options = network[0], network[1]
        ip = options[:ip] if (key == :private_network || key == :public_network) && options[:goodhosts] != "skip"
        ips.push(ip) if ip
        if options[:goodhosts] == 'skip'
          @ui.info '[vagrant-goodhosts] Skipping adding host entries (config.vm.network goodhosts: "skip" is set)'
      end
      
      @machine.config.vm.provider :hyperv do |v|
          timeout = @machine.provider_config.ip_address_timeout
          @ui.output("[vagrant-goodhosts] Waiting for the guest machine to report its IP address ( this might take some time, have patience )...")
          @ui.detail("Timeout: #{timeout} seconds")

          options = {
              vmm_server_address: @machine.provider_config.vmm_server_address,
              proxy_server_address: @machine.provider_config.proxy_server_address,
              timeout: timeout,
              machine: @machine
          }
          network = @machine.provider.driver.read_guest_ip(options)
          if network["ip"]
              ips.push( network["ip"] ) unless ips.include? network["ip"]
          end
      end
      
      return ips
  end
end

#printReadme(error, errorText) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/vagrant-goodhosts/GoodHosts.rb', line 150

def printReadme(error, errorText)
  if error
      cli = get_cli
      @ui.error "[vagrant-goodhosts] Issue executing goodhosts CLI: #{errorText}"
      @ui.error "[vagrant-goodhosts] Cli path: #{cli}"
      @ui.error "[vagrant-goodhosts] Check the readme at https://github.com/goodhosts/vagrant#passwordless-sudo"
  end

end

#removeHostEntriesObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vagrant-goodhosts/GoodHosts.rb', line 124

def removeHostEntries
  ips = getIps
  hostnames = getHostnames(ips)
  error = false
  errorText = ""
  cli = get_cli
  ips.each do |ip|
    hostnames[ip].each do |hostname|
        ip_address = ip
        if !ip_address.nil?
          @ui.info "[vagrant-goodhosts] - remove entry for: #{ip_address} #{hostname}"
          if cli.include? ".exe"
              stdin, stdout, stderr, wait_thr = Open3.popen3(cli, "r", ip_address, hostname)
          else
              stdin, stdout, stderr, wait_thr = Open3.popen3('sudo', cli, "r", ip_address, hostname)
          end
          if !wait_thr.value.success?
              error = true
              errorText = stderr.read.strip
          end
        end
    end
  end
  printReadme(error, errorText)
end