Class: VagrantPlugins::CommandDns::Action::GetIP

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-command-dns/action/get_ip.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ GetIP

Returns a new instance of GetIP.



8
9
10
11
# File 'lib/vagrant-command-dns/action/get_ip.rb', line 8

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_command_dns::action::get_ip')
end

Instance Method Details

#call(env) ⇒ Object



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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-command-dns/action/get_ip.rb', line 13

def call(env)
  if !env[:machine].config.dns.__skip
    if env[:machine].state.id != :running
      raise Errors::MachineState, state: 'running'
    end

    ip = nil
    unable = true

    case env[:machine].provider_name

      when :virtualbox
        env[:machine].config.vm.networks.each do |network|
          key, options = network[0], network[1]
          if key == :private_network or key == :public_network
            unable = false
            if options[:dns] != 'skip'
              if options[:ip] # static ip specified
                ip = IPAddr.new(options[:ip])
              else
                case key
                  when :private_network
                    cmd = "vagrant ssh -c \"ip -4 addr show \\$(ip -4 route | tail -n1 | awk '{print \\$3}') | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'\" 2>/dev/null"
                  when :public_network
                    cmd = "vagrant ssh -c \"ip -4 addr show \\$(ip -4 route | head -n2 | tail -n1 | awk '{print \\$5}') | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'\" 2>/dev/null"
                end
                begin
                  ip = IPAddr.new(`#{cmd}`.chomp())
                rescue IPAddr::InvalidAddressError
                  raise Errors::InvalidAddress
                end
              end
            else
              env[:ui].info(I18n.t('vagrant_command_dns.config.network_skip'))
            end
          end
        end
        if unable
          raise Errors::NoNetwork
        end

      when :aws
        cmd = "vagrant ssh -c 'curl https://api.ipify.org'"
        begin
          ip = IPAddr.new(`#{cmd}`.chomp())
        rescue IPAddr::InvalidAddressError
          raise Errors::InvalidAddress
        end

      else
        raise Errors::UnsupportedProvider

    end

    unless ip.nil?
      host_names = [env[:machine].config.vm.hostname]
      env[:machine].config.dns.aliases.each do |a|
        host_names.push(a)
      end

      record_map = {}
      host_names.each do |h|
        record_map[h] = ip.to_string
      end

      env[:record_map] = record_map
    end

  else
    host_names = [env[:machine].config.vm.hostname]
    env[:machine].config.dns.aliases.each do |a|
      host_names.push(a)
    end

    record_map = {}
    host_names.each do |h|
      record_map[h] = ip
    end

      env[:record_map] = record_map
  end

  @app.call(env)
end