Class: Bosh::Director::DnsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/dns/dns_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dns_domain_name, dns_config, dns_provider, dns_publisher, local_dns_repo, logger) ⇒ DnsManager

Returns a new instance of DnsManager.



22
23
24
25
26
27
28
29
30
31
# File 'lib/bosh/director/dns/dns_manager.rb', line 22

def initialize(dns_domain_name, dns_config, dns_provider, dns_publisher, local_dns_repo, logger)
  @dns_domain_name = dns_domain_name
  @dns_provider = dns_provider
  @dns_publisher = dns_publisher
  @default_server = dns_config['server']
  @flush_command = dns_config['flush_command']
  @ip_address = dns_config['address']
  @local_dns_repo = local_dns_repo
  @logger = logger
end

Instance Attribute Details

#dns_domain_nameObject (readonly)

Returns the value of attribute dns_domain_name.



20
21
22
# File 'lib/bosh/director/dns/dns_manager.rb', line 20

def dns_domain_name
  @dns_domain_name
end

Instance Method Details

#cleanup_dns_recordsObject



149
150
151
152
153
# File 'lib/bosh/director/dns/dns_manager.rb', line 149

def cleanup_dns_records
  if publisher_enabled?
    @dns_publisher.cleanup_blobs
  end
end

#configure_nameserverObject



41
42
43
# File 'lib/bosh/director/dns/dns_manager.rb', line 41

def configure_nameserver
  @dns_provider.create_or_update_nameserver(@ip_address) if dns_enabled?
end

#delete_dns_for_instance(instance_model) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bosh/director/dns/dns_manager.rb', line 86

def delete_dns_for_instance(instance_model)
  return if !dns_enabled?

  current_dns_records = @local_dns_repo.find(instance_model)
  if current_dns_records.empty?
    # for backwards compatibility when old instances
    # did not have records in local repo
    # we cannot migrate them because powerdns can be different database
    # those instance only had index-based dns records (before global-net)
    index_record_pattern = dns_record_name(instance_model.index, instance_model.job, '%', instance_model.deployment.name)
    @dns_provider.delete(index_record_pattern)
    return
  end

  current_dns_records.each do |record_name|
    @logger.info("Removing DNS for: #{record_name}")
    @dns_provider.delete(record_name)
  end

  @local_dns_repo.delete(instance_model)
end

#dns_enabled?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/bosh/director/dns/dns_manager.rb', line 33

def dns_enabled?
  !@dns_provider.nil?
end

#dns_record_name(hostname, job_name, network_name, deployment_name) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/bosh/director/dns/dns_manager.rb', line 159

def dns_record_name(hostname, job_name, network_name, deployment_name)
  network_name = Canonicalizer.canonicalize(network_name) unless network_name == '%'

  [ hostname,
    Canonicalizer.canonicalize(job_name),
    network_name,
    Canonicalizer.canonicalize(deployment_name),
    @dns_domain_name
  ].join('.')
end

#dns_servers(network, dns_spec, add_default_dns = true) ⇒ Object

build a list of dns servers to use



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bosh/director/dns/dns_manager.rb', line 109

def dns_servers(network, dns_spec, add_default_dns = true)
  servers = nil

  if dns_spec
    servers = []
    dns_spec.each do |dns|
      dns = NetAddr::CIDR.create(dns)
      unless dns.size == 1
        raise NetworkInvalidDns,
          "Invalid DNS for network '#{network}': must be a single IP"
      end

      servers << dns.ip
    end
  end

  return servers unless add_default_dns
  add_default_dns_server(servers)
end

#find_dns_record(dns_record_name, ip_address) ⇒ Object



45
46
47
# File 'lib/bosh/director/dns/dns_manager.rb', line 45

def find_dns_record(dns_record_name, ip_address)
  @dns_provider.find_dns_record(dns_record_name, ip_address)
end

#find_dns_record_names_by_instance(instance_model) ⇒ Object



155
156
157
# File 'lib/bosh/director/dns/dns_manager.rb', line 155

def find_dns_record_names_by_instance(instance_model)
  instance_model.nil? ? [] : instance_model.dns_record_names.to_a.compact
end

#flush_dns_cacheObject

Purge cached DNS records



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bosh/director/dns/dns_manager.rb', line 130

def flush_dns_cache
  if @flush_command && !@flush_command.empty?
    stdout, stderr, status = Open3.capture3(@flush_command)
    if status == 0
      @logger.debug("Flushed #{stdout.chomp} records from DNS cache")
    else
      @logger.warn("Failed to flush DNS cache: #{stderr.chomp}")
    end
  end
  publish_dns_records
end

#migrate_legacy_records(instance_model) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bosh/director/dns/dns_manager.rb', line 61

def migrate_legacy_records(instance_model)
  return if @local_dns_repo.find(instance_model).any?
  return unless dns_enabled?

  index_pattern_for_all_networks = dns_record_name(
    instance_model.index,
    instance_model.job,
    '%',
    instance_model.deployment.name
  )
  uuid_pattern_for_all_networks = dns_record_name(
    instance_model.uuid,
    instance_model.job,
    '%',
    instance_model.deployment.name
  )

  legacy_record_names = [index_pattern_for_all_networks, uuid_pattern_for_all_networks]
    .map { |pattern| @dns_provider.find_dns_records_by_pattern(pattern) }
    .flatten
    .map(&:name)

  @local_dns_repo.create_or_update(instance_model, legacy_record_names)
end

#publish_dns_recordsObject



142
143
144
145
146
147
# File 'lib/bosh/director/dns/dns_manager.rb', line 142

def publish_dns_records
  if publisher_enabled?
    dns_records = @dns_publisher.export_dns_records
    @dns_publisher.publish(dns_records)
  end
end

#publisher_enabled?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/bosh/director/dns/dns_manager.rb', line 37

def publisher_enabled?
  !@dns_publisher.nil?
end

#update_dns_record_for_instance(instance_model, dns_names_to_ip) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bosh/director/dns/dns_manager.rb', line 49

def update_dns_record_for_instance(instance_model, dns_names_to_ip)
  current_dns_records = @local_dns_repo.find(instance_model)
  new_dns_records = []
  dns_names_to_ip.each do |record_name, ip_address|
    new_dns_records << record_name
    @logger.info("Updating DNS for: #{record_name} to #{ip_address}")
    @dns_provider.create_or_update_dns_records(record_name, ip_address)
  end
  dns_records = (current_dns_records + new_dns_records).uniq
  @local_dns_repo.create_or_update(instance_model, dns_records)
end