Class: VagrantPlugins::SoftLayer::Action::UpdateDNS

Inherits:
Object
  • Object
show all
Includes:
Util::Network, Util::Warden
Defined in:
lib/vagrant-softlayer/action/update_dns.rb

Overview

Look for the DNS zone relative to the configured domain and, on return path, perform an API call to add or remove the A resource record for the host.

Instance Method Summary collapse

Methods included from Util::Warden

#sl_warden

Methods included from Util::Network

#hostname, #ip_address, #ip_address_id, #ip_address_record, #ssh_keys

Constructor Details

#initialize(app, env) ⇒ UpdateDNS

Returns a new instance of UpdateDNS.



11
12
13
14
# File 'lib/vagrant-softlayer/action/update_dns.rb', line 11

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_softlayer::action::update_dns")
end

Instance Method Details

#add_recordObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-softlayer/action/update_dns.rb', line 24

def add_record
  template = {
    "data"     => ip_address(@env),
    "domainId" => @dns_zone["id"],
    "host"     => hostname(@env),
    "ttl"      => 86400,
    "type"     => "a"
  }
  @env[:ui].info I18n.t("vagrant_softlayer.vm.creating_dns_record")
  @logger.debug("Creating DNS A record for #{template['host']}.#{@dns_zone[:name]} (IP address #{template['data']}).")
  resource = sl_warden { @resource.createObject(template) }
  self.dns_id = resource["id"]
end

#call(env) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/vagrant-softlayer/action/update_dns.rb', line 16

def call(env)
  @env = env

  update_dns

  @app.call(@env)
end

#delete_recordObject



38
39
40
41
42
43
# File 'lib/vagrant-softlayer/action/update_dns.rb', line 38

def delete_record
  @env[:ui].info I18n.t("vagrant_softlayer.vm.deleting_dns_record")
  @logger.debug("Deleting stored DNS A record (ID #{self.dns_id}).")
  warn_msg = lambda { @env[:ui].warn I18n.t("vagrant_softlayer.errors.dns_record_not_found") }
  sl_warden(warn_msg) { @resource.object_with_id(self.dns_id).deleteObject }
end

#dns_idObject



45
46
47
48
49
50
# File 'lib/vagrant-softlayer/action/update_dns.rb', line 45

def dns_id
  id = nil
  id_file = @env[:machine].data_dir.join("dns_id")
  id = id_file.read.chomp.to_i if id_file.file?
  return id
end

#dns_id=(value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vagrant-softlayer/action/update_dns.rb', line 52

def dns_id=(value)
  @logger.info("New machine DNS ID: #{value.inspect}")

  # The file that will store the id if we have one. This allows the
  # ID to persist across Vagrant runs.
  id_file = @env[:machine].data_dir.join("dns_id")

  if value
    # Write the "id" file with the id given.
    id_file.open("w+") do |f|
      f.write(value)
    end
  end
end

#update_dnsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagrant-softlayer/action/update_dns.rb', line 67

def update_dns
  unless @env[:machine].provider_config.manage_dns
    @logger.debug("Not managing DNS. Going ahead.")
    return
  end

  # Lookup the DNS zone
  zone   = ::SoftLayer::Service.new("SoftLayer_Dns_Domain", @env[:sl_credentials])
  domain = @env[:machine].provider_config.domain

  @logger.debug("Looking for #{domain} zone into the SoftLayer zone list.")
  @dns_zone = sl_warden { zone.getByDomainName(domain).first }
  raise Errors::SLDNSZoneNotFound, :zone => domain unless @dns_zone
  @logger.debug("Found DNS zone: #{@dns_zone.inspect}")

  # Add or remove the resource record
  @resource = ::SoftLayer::Service.new("SoftLayer_Dns_Domain_ResourceRecord", @env[:sl_credentials])
  case @env[:machine_action]
  when :up
    add_record unless self.dns_id
  when :destroy
    delete_record
  end
end