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.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vagrant-powerdns/action.rb', line 79

def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @host = env[:machine].config.vm.hostname.nil? ?
    env[:machine].name.to_s : env[:machine].config.vm.hostname.to_s
  @domain = @host + env[:machine].config.powerdns.default_zone.to_s
  @zone = env[:machine].config.powerdns.default_zone.name

  # Identify who i am
  @myname = Etc.getpwuid[:gecos]
  @myuser = Etc.getlogin.gsub(/\s+/, '')
  @myhost = Socket.gethostname
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  if @machine.config.powerdns.enabled?
    p = PdnsRestApiClient.new(env[:machine].config.powerdns.api_url,
                              env[:machine].config.powerdns.api_key)

    # Get A record
    record = p.zone(@zone)

    # only disable if active
    if not record["records"].find {|v| v["name"] == @domain}["disabled"]
      env[:ui].info "PowerDNS action..."

      # Prepare comment to be appended
      new_comment = {
        content: "#{@myname} (#{@myuser}) disabled this record from #{@myhost}",
        account: @myname,
        name: @domain,
        type: "A"
      }
      comments = record["comments"].delete_if { |v| v["name"] != @domain }
      comments << new_comment

      # Get the old IP
      ip = record["records"].find {|v| v["name"] == @domain}["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