Class: Vagrant::Action::Up

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-powerdns/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Up

Returns a new instance of Up.



5
6
7
8
# File 'lib/vagrant-powerdns/action.rb', line 5

def initialize(app, env)
  @app = app
  @machine = env[:machine]
end

Instance Method Details

#call(env) ⇒ Object



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
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
# File 'lib/vagrant-powerdns/action.rb', line 10

def call(env)
  if @machine.config.powerdns.enabled?
    @zone = env[:machine].config.powerdns.default_zone
    @host = env[:machine].config.vm.hostname.nil? ?
      env[:machine].name.to_s : env[:machine].config.vm.hostname.to_s
    @domain = @host.include?(@zone.name)? @host : @host + @zone.dotted

    # Identify who i am
    @myuser = Etc.getlogin.gsub(/\s+/, '')
    @myhost = Socket.gethostname

    # assume default gateway address
    @machine.communicate.sudo "ip route get to 8.8.8.8 | head -n 1" do |type,data|
      stdout = data.chomp if type == :stdout
      if !stdout.empty?
        re = /src ([0-9\.]+)/
        ip = stdout.match(re)[1]
        zone = @zone.name

        p = PdnsRestApiClient.new(env[:machine].config.powerdns.api_url,
                                  env[:machine].config.powerdns.api_key)
        begin
          # Only update if IP changed or inactive
          record_not_found = p.zone(zone)["records"].select {|v| v["type"] == "A" and v["name"] == @domain and v["content"] == ip}.empty?
          record_disabled = p.zone(zone)["records"].select { |v| v["type"] == "A" and v["name"] == @domain and v["disable"]}.empty?
        rescue
          env[:ui].info "PowerDNS: Error while calling PowernDNS API. Skip DNS action..."
          return
        end

        if record_not_found or record_disabled
          env[:ui].info "PowerDNS action..."
          # Append new comment
          new_comment = {
            content: "#{@myuser} added this record from #{@myhost}",
            account: @myuser,
            name: @domain,
            type: "A"
          }
          comments = p.zone(zone)["comments"].delete_if { |v| v["name"] != @domain  }
          comments << new_comment

          ret = p.modify_domain(domain: @domain, ip: ip, zone_id: zone,
                                comments: comments)

          # Check return
          error = nil
          if ret.is_a?(String)
            error = ret
          else
            if ret.is_a?(Hash)
              error = ret.values[0] if ret.keys[0] == "error"
            else
              raise "Unknown esponse from PowerDNS API"
            end
          end

          # Display ui
          if error.nil?
              env[:ui].detail "=> record #{@domain}(#{ip}) in zone #{zone} added !"
          else
            env[:ui].detail "=> failed to add record #{@domain}(#{ip}) in zone #{zone}. Error was: #{error}"
          end
        end
      end
    end

    @app.call(env)
  end
end