Class: Vagrant::Action::Destroy

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Destroy

Returns a new instance of Destroy.



84
85
86
87
# File 'lib/vagrant-powerdns/action.rb', line 84

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

Instance Method Details

#call(env) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/vagrant-powerdns/action.rb', line 89

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
    p = PdnsRestApiClient.new(env[:machine].config.powerdns.api_url,
                              env[:machine].config.powerdns.api_key)

    zone = @zone.name
    begin
      record = p.zone(zone)["records"].find {|v| v["name"] == @domain}
      # Get comments for this domain
      comments = p.zone(zone)["comments"].select {|v| v["name"] == @domain}
    rescue
      env[:ui].info "PowerDNS: Error while calling PowernDNS API. Skip DNS action..."
      return
    end          # Get A record

    # only disable if active
    if !record.nil? and not record["disabled"]
      env[:ui].info "PowerDNS action..."

      # Prepare comment to be appended
      new_comment = {
        content: "#{@myuser} disabled this record from #{@myhost}",
        account: @myuser,
        name: @domain,
        type: "A"
      }
      comments << new_comment

      # Get the old IP
      ip = record["content"]

      ret = p.disable_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} disabled !"
      else
        env[:ui].detail "=> failed to disab;e record #{@domain} in zone #{zone}. Error was: #{error}"
      end
    end

    @app.call(env)
  end
end