Sha256: d803b05ce977887e9990c5e4bdcf083a6e65bd04182ac1c779f362321c90bd23

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

module Vagrant
  module LXC
    class Driver
      module FetchIpFromDsnmasq
        def assigned_ip
          @logger.debug 'Loading ip from dnsmasq leases'
          ip = nil
          # TODO: Use Vagrant::Util::Retryable
          10.times do
            if dnsmasq_leases =~ /#{Regexp.escape mac_address}\s+([0-9.]+)\s+/
              ip = $1.to_s
              break
            else
              @logger.debug 'Ip could not be parsed from dnsmasq leases file'
              sleep 2
            end
          end
          # TODO: Raise an user friendly error
          raise 'Unable to identify container IP!' unless ip
          ip
        end

        def mac_address
          @mac_address ||= base_path.join('config').read.match(/^lxc\.network\.hwaddr\s+=\s+(.+)$/)[1]
        end

        LEASES_PATHS = %w(
          /var/lib/misc/dnsmasq.leases
          /var/lib/dnsmasq/dnsmasq.leases
          /var/db/dnsmasq.leases
        )

        def dnsmasq_leases
          LEASES_PATHS.map do |path|
            File.read(path) if File.exists?(path)
          end.join("\n")
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vagrant-lxc-0.4.0 lib/vagrant-lxc/driver/fetch_ip_from_dnsmasq.rb
vagrant-lxc-0.3.4 lib/vagrant-lxc/driver/fetch_ip_from_dnsmasq.rb
vagrant-lxc-0.3.3 lib/vagrant-lxc/driver/fetch_ip_from_dnsmasq.rb
vagrant-lxc-0.3.2 lib/vagrant-lxc/driver/fetch_ip_from_dnsmasq.rb
vagrant-lxc-0.3.1 lib/vagrant-lxc/driver/fetch_ip_from_dnsmasq.rb
vagrant-lxc-0.3.0 lib/vagrant-lxc/driver/fetch_ip_from_dnsmasq.rb