Class: Vagrant::Action::Up

Inherits:
Object show all
Defined in:
lib/vagrant-dnsmasq/actions.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Up

Returns a new instance of Up.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/vagrant-dnsmasq/actions.rb', line 5

def initialize(app, env)
  inc_path = Pathname.new(File.expand_path("../includes", __FILE__))
  require inc_path.join("Domain.class.rb")
  require inc_path.join("Ip.class.rb")
  require inc_path.join("Dnsmasq.class.rb")
  require inc_path.join("Resolver.class.rb")
  require inc_path.join("helper.rb")

  @app = app
  @machine = env[:machine]
  @ip = []
end

Instance Method Details

#call(env) ⇒ Object



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
53
54
55
56
# File 'lib/vagrant-dnsmasq/actions.rb', line 18

def call(env)
  if @machine.config.dnsmasq.enabled?
    
    # overwrite ip with value from config?
    @ip = @machine.config.dnsmasq.ip if @machine.config.dnsmasq.ip.count > 0
    
    
    # ... or try to fetch it from guest system
    if @ip.count === 0
      @machine.communicate.sudo("hostname -I") do |type, data| 
        @ip = data.scan /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/
      end
      @ip.map!{|ip| begin Ip.new(ip) rescue nil end}.compact!
    end

    # is there a ip?
    if @ip.count > 0

      # update dnsmasq.conf
      brew_prefix = `brew --prefix`
      brew_prefix.strip!
      dnsmasq_conf_file = brew_prefix + "/etc/dnsmasq.conf"

      dnsmasq = DnsmasqConf.new(dnsmasq_conf_file)
      @ip.each do |ip|
        dnsmasq.insert(@machine.config.dnsmasq.domain, ip)
      end
      
      # update /etc/resolver
      resolver = Resolver.new('/etc/resolver')
      @ip.each do |ip|
        resolver.insert(@machine.config.dnsmasq.domain, ip)
      end

      env[:ui].info "Added domain #{@machine.config.dnsmasq.domain} for IP #{@ip}"
      @app.call(env)
    end
  end
end