Class: Bosh::Director::EnabledDnsManager

Inherits:
DnsManager 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, local_dns_repo, logger) ⇒ EnabledDnsManager

Returns a new instance of EnabledDnsManager.



71
72
73
74
75
76
77
78
79
# File 'lib/bosh/director/dns/dns_manager.rb', line 71

def initialize(dns_domain_name, dns_config, dns_provider, local_dns_repo, logger)
  @dns_domain_name = dns_domain_name
  @dns_provider = dns_provider
  @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.



69
70
71
# File 'lib/bosh/director/dns/dns_manager.rb', line 69

def dns_domain_name
  @dns_domain_name
end

Instance Method Details

#configure_nameserverObject



85
86
87
# File 'lib/bosh/director/dns/dns_manager.rb', line 85

def configure_nameserver
  @dns_provider.create_or_update_nameserver(@ip_address)
end

#delete_dns_for_instance(instance_model) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bosh/director/dns/dns_manager.rb', line 133

def delete_dns_for_instance(instance_model)
  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)


81
82
83
# File 'lib/bosh/director/dns/dns_manager.rb', line 81

def dns_enabled?
  true
end

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



186
187
188
189
190
191
192
193
194
195
# File 'lib/bosh/director/dns/dns_manager.rb', line 186

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/bosh/director/dns/dns_manager.rb', line 154

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



89
90
91
# File 'lib/bosh/director/dns/dns_manager.rb', line 89

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



93
94
95
# File 'lib/bosh/director/dns/dns_manager.rb', line 93

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



175
176
177
178
179
180
181
182
183
184
# File 'lib/bosh/director/dns/dns_manager.rb', line 175

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
end

#migrate_legacy_records(instance_model) ⇒ Object



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

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

  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

#update_dns_record_for_instance(instance_model, dns_names_to_ip) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bosh/director/dns/dns_manager.rb', line 97

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