Class: DNSOMatic::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsomatic/updater.rb

Overview

This is the primary interface used to perform updates of IP information to the DNS-o-Matic service.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Updater

Initialize and updater object with a set of config options. config is a hash that must contain the following keys:

  • hostname: a string representing the hostname to be updated

  • mx: a string representing a valid MX for hostname

  • backmx: a string representing a secondary MX for hostname

  • username: the username with rights to update hostname

  • password: the corresponding password

  • webipfetchurl: a url that returns the IP of the system requesting the it

  • offline: determines whether ‘offline’ mode should be used.



15
16
17
18
19
20
# File 'lib/dnsomatic/updater.rb', line 15

def initialize(config)
  @config = config
  #use a cache in case other host stanzas have already looked up
  #our ip using the same remote agent.
  @lookup = DNSOMatic::IPLookup.instance
end

Instance Method Details

#to_sObject

This is used to simply return the url that would be sent to DNS-o-Matic. Using this is an easy way to build a list of update URL’s that could be directed elsewhere (a log, or wget, etc).



59
60
61
# File 'lib/dnsomatic/updater.rb', line 59

def to_s
  upd_url
end

#update(force = false) ⇒ Object

Build and send the url that is passed to the DNS-o-Matic system to request an update for the hostname provided in the configuration. By default, the we’ll honour the changed? status of the IPStatus object returned to us, but if force is set to true, we’ll send the update request anyway.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dnsomatic/updater.rb', line 27

def update(force = false)
  url = upd_url()

  DNSOMatic::Logger.log("Updating with URL: #{url}")

  if !@ipstatus.changed? and !force
    Logger::log("No change in IP detected for #{@config['hostname']}.  Not updating.")
  else
    Logger::alert("Updating IP for #{@config['hostname']} to #{@ipstatus.ip}.")
    update = DNSOMatic::http_fetch(url)

    if !update.match(/^good\s+#{@ipstatus.ip}$/)
      msg = "Error updating host definition for #{@config['hostname']}\n"
      msg += "Results:\n#{update}\n"
      msg += "Error codes at: https://www.dnsomatic.com/wiki/api"
      raise(DNSOMatic::Error, msg)
    end
  end

  true
end

#update!Object

A simple wrapper around update that sets force to true. The forceful nature of the update is logged if verbosity is enabled.



51
52
53
54
# File 'lib/dnsomatic/updater.rb', line 51

def update!
  Logger::log("Forcing update at user request.")
  update(true)
end