vagrant-landrush / landrush

A Vagrant plugin that provides a simple DNS server for Vagrant guests
MIT License
668 stars 80 forks source link

get_network_name fails on windows 10 #314

Closed johnrizzo1 closed 5 years ago

johnrizzo1 commented 7 years ago

When I bring up the hosts in vagrant I get an error

[CNYWIT43] C:\projects\DevOps\devenv [feature/get_hyperv_working +5 ~3 -0 !]> vagrant up default                                    
Bringing machine 'default' up with 'hyperv' provider...                                                                             
[[[ lines removed for brevity ]]]                                                      
==> default: Setting hostname...                                                                                                    
==> default: Mounting SMB shared folders...                                                                                         
    default: C:/projects/DevOps/devenv => /vagrant                                                                                  
[landrush] Using eth0 (10.0.75.6)                                                                                                   
==> default: [landrush] hostname salt.dev.local does not match the configured TLD: vagrant.test                                     
==> default: [landrush] You will not be able to access salt.dev.local from the host                                                 
==> default: [landrush] adding machine entry: salt.dev.local => 10.0.75.6                                                           
[landrush] Using eth0 (10.0.75.6)                                                                                                   
C:/Users/jrizzo/.vagrant.d/gems/gems/landrush-1.2.0/lib/landrush/cap/host/windows/configure_visibility_on_host.rb:133:in `get_networ
k_name': undefined method `captures' for nil:NilClass (NoMethodError)                                                               
        from C:/Users/jrizzo/.vagrant.d/gems/gems/landrush-1.2.0/lib/landrush/cap/host/windows/configure_visibility_on_host.rb:58:in
 `update_network_adapter'                                                                                                           
        from C:/Users/jrizzo/.vagrant.d/gems/gems/landrush-1.2.0/lib/landrush/cap/host/windows/configure_visibility_on_host.rb:15:in
 `configure_visibility_on_host'                                                                                                     
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/lib/vagrant/capability_host.rb:111:in `call'                     
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/lib/vagrant/capability_host.rb:111:in `capability'               
        from C:/Users/jrizzo/.vagrant.d/gems/gems/landrush-1.2.0/lib/landrush/action/setup.rb:42:in `post_boot_setup'               
        from C:/Users/jrizzo/.vagrant.d/gems/gems/landrush-1.2.0/lib/landrush/action/setup.rb:18:in `call'                          
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/lib/vagrant/action/warden.rb:34:in `call'                        
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/plugins/providers/hyperv/action/start_instance.rb:12:in `call'   
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/lib/vagrant/action/warden.rb:34:in `call'                        
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/plugins/providers/hyperv/action/net_set_mac.rb:15:in `call'      
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/lib/vagrant/action/warden.rb:34:in `call'                        
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/plugins/providers/hyperv/action/net_set_vlan.rb:15:in `call'     
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.7/lib/vagrant/action/warden.rb:34:in `call'                        
        from C:/Users/jrizzo/.vagrant.d/gems/gems/vagrant-hostmanager-1.8.5/lib/vagrant-hostmanager/action/update_all.rb:24:in `call
'                                                                                                                                   

As you can see from the following debug session that the first line is blank.

[15] pry(main)> network_details[0].split(/\n/)
=> ["",
 "Configuration for interface \"vEthernet (DockerNAT)\"",
 "    DHCP enabled:                         No",
 "    IP Address:                           10.0.75.1",
 "    Subnet Prefix:                        10.0.75.0/24 (mask 255.255.255.0)",
 "    InterfaceMetric:                      15",
 "    Statically Configured DNS Servers:    None",
 "    Register with which suffix:           Primary only",
 "    Statically Configured WINS Servers:   None"]
[16] pry(main)> network_details[0].split(/\n/)[1]
=> "Configuration for interface \"vEthernet (DockerNAT)\""

I updated the get_network_name method to filter empty lines.

      def get_network_name(ip)
        cmd_out = `netsh interface ip show config`
        network_details = cmd_out.split(/\n\n/).reject(&:empty?).select do |settings|
          begin
            lines = settings.split(/\n/).reject(&:empty?)
            subnet = lines[3]
            next false unless subnet =~ /Subnet Prefix/

            mask = IPAddr.new(subnet.match(%r{.* (\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}/\d{1,3}).*}).captures[0])
            address = IPAddr.new(ip)

            mask.include?(address)
          rescue
            false
          end
        end
        return nil if network_details[0].nil? || network_details[0].empty?
        network_details[0].split(/\n/)[1].match(/Configuration for interface "(.*)"/).captures[0].strip
      end
      end
hferentschik commented 5 years ago

Merged via pull request #335