Project

General

Profile

Download (4.87 KB) Statistics
| Branch: | Tag: | Revision:
90b83222 Ohad Levy
module Orchestration::DNS
dc457681 Joseph Mitchell Magen
extend ActiveSupport::Concern
e2163020 Ondrej Prazak
include Orchestration::Common
90b83222 Ohad Levy
dc457681 Joseph Mitchell Magen
included do
d6bc6b86 Shimon Shtein
after_validation :dns_conflict_detected?, :unless => :skip_orchestration?
after_validation :queue_dns
before_destroy :queue_dns_destroy
2fba6ad7 Ondrej Prazak
register_rebuild(:rebuild_dns, N_('DNS'))
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
041436c8 Timo Goebel
def dns_ready?
5440d482 Greg Sutcliffe
# host.managed? and managed? should always come first so that orchestration doesn't
# even get tested for such objects
041436c8 Timo Goebel
SETTINGS[:unattended] && (host.nil? || host.managed?) && managed? && hostname.present?
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
041436c8 Timo Goebel
def dns?
dns_ready? && ip_available? && domain.present? && domain.proxy.present?
dc457681 Joseph Mitchell Magen
end
dd42df0a Ohad Levy
041436c8 Timo Goebel
def dns6?
dns_ready? && ip6_available? && domain.present? && domain.proxy.present?
2fba6ad7 Ondrej Prazak
end

041436c8 Timo Goebel
def reverse_dns?
dns_ready? && ip_available? && subnet.present? && subnet.dns?
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
041436c8 Timo Goebel
def reverse_dns6?
dns_ready? && ip6_available? && subnet6.present? && subnet6.dns?
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
041436c8 Timo Goebel
def rebuild_dns
feasible = {}
DnsInterface::RECORD_TYPES.each do |record_type|
feasible[record_type] = dns_feasible?(record_type)
logger.info "DNS record type #{record_type} not supported for #{name}, skipping orchestration rebuild" unless feasible[record_type]
2fba6ad7 Ondrej Prazak
end
041436c8 Timo Goebel
return true unless feasible.any?

results = {}

DnsInterface::RECORD_TYPES.each do |record_type|
del_dns_record_safe(record_type)
2fba6ad7 Ondrej Prazak
begin
088c8f3d Guido Günther
results[record_type] = dns_feasible?(record_type) ? recreate_dns_record(record_type) : true
2fba6ad7 Ondrej Prazak
rescue => e
041436c8 Timo Goebel
Foreman::Logging.exception "Failed to rebuild DNS record for #{name}(#{ip}/#{ip6})", e, :level => :error
return false
2fba6ad7 Ondrej Prazak
end
end
041436c8 Timo Goebel
results.values.all?
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
dc457681 Joseph Mitchell Magen
def queue_dns
c6294926 Lukas Zapletal
return log_orchestration_errors unless (dns? || dns6? || reverse_dns? || reverse_dns6?) && errors.empty?
dc457681 Joseph Mitchell Magen
queue_remove_dns_conflicts if overwrite?
new_record? ? queue_dns_create : queue_dns_update
end
90b83222 Ohad Levy
dc457681 Joseph Mitchell Magen
def queue_dns_create
logger.debug "Scheduling new DNS entries"
041436c8 Timo Goebel
DnsInterface::RECORD_TYPES.each do |record_type|
d7c67746 Michael Moll
if dns_feasible?(record_type)
queue.create(:name => _("Create %{type} for %{host}") % {:host => self, :type => dns_class(record_type).human}, :priority => 10,
:action => [self, :set_dns_record, record_type])
end
041436c8 Timo Goebel
end
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
dc457681 Joseph Mitchell Magen
def queue_dns_update
3442a707 Timo Goebel
return unless pending_dns_record_changes?
DnsInterface::RECORD_TYPES.each do |record_type|
d7c67746 Michael Moll
if old.dns_feasible?(record_type)
queue.create(:name => _("Remove %{type} for %{host}") % {:host => old, :type => dns_class(record_type).human }, :priority => 9,
:action => [old, :del_dns_record, record_type])
end
90b83222 Ohad Levy
end
3442a707 Timo Goebel
queue_dns_create
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
dc457681 Joseph Mitchell Magen
def queue_dns_destroy
return unless errors.empty?
041436c8 Timo Goebel
DnsInterface::RECORD_TYPES.each do |record_type|
d7c67746 Michael Moll
if dns_feasible?(record_type)
queue.create(:name => _("Remove %{type} for %{host}") % {:host => self, :type => dns_class(record_type).human}, :priority => 1,
:action => [self, :del_dns_record, record_type])
end
041436c8 Timo Goebel
end
dc457681 Joseph Mitchell Magen
end
30ae12bf Ohad Levy
dc457681 Joseph Mitchell Magen
def queue_remove_dns_conflicts
return unless errors.empty?
return unless overwrite?
logger.debug "Scheduling DNS conflict removal"
041436c8 Timo Goebel
DnsInterface::RECORD_TYPES.each do |record_type|
d7c67746 Michael Moll
if dns_feasible?(record_type) && dns_record(record_type) && dns_record(record_type).conflicting?
queue.create(:name => _("Remove conflicting %{type} for %{host}") % {:host => self, :type => dns_class(record_type).human}, :priority => 0,
:action => [self, :del_conflicting_dns_record, record_type])
end
041436c8 Timo Goebel
end
dc457681 Joseph Mitchell Magen
end
30ae12bf Ohad Levy
3442a707 Timo Goebel
def pending_dns_record_changes?
b7cc39f8 Dominic Cleal
!attr_equivalent?(old.ip, ip) || !attr_equivalent?(old.ip6, ip6) || !attr_equivalent?(old.hostname, hostname)
3442a707 Timo Goebel
end

dc457681 Joseph Mitchell Magen
def dns_conflict_detected?
041436c8 Timo Goebel
return false if ip.blank? || hostname.blank?
dc457681 Joseph Mitchell Magen
# can't validate anything if dont have an ip-address yet
041436c8 Timo Goebel
return false unless require_ip4_validation? || require_ip6_validation?
dc457681 Joseph Mitchell Magen
# we should only alert on conflicts if overwrite mode is off
return false if overwrite?

status = true
041436c8 Timo Goebel
DnsInterface::RECORD_TYPES.each do |record_type|
if dns_feasible?(record_type) && dns_record(record_type) && dns_record(record_type).conflicting?
conflicts = dns_record(record_type).conflicts
status = failure(_("%{type} %{conflicts} already exists") % {:conflicts => conflicts.to_sentence, :type => dns_class(record_type).human(conflicts.count)}, nil, :conflict)
end
end
68388bc2 Michael Moll
!status # failure method returns 'false'
da5a9c4e Lukas Zapletal
rescue Net::Error => e
if domain.nameservers.empty?
failure(_("Error connecting to system DNS server(s) - check /etc/resolv.conf"), e)
else
448b447e Lukas Zapletal
failure(_("Error connecting to '%{domain}' domain DNS servers: %{servers} - check query_local_nameservers and dns_conflict_timeout settings") % {:domain => domain.try(:name), :servers => domain.nameservers.join(',')}, e)
da5a9c4e Lukas Zapletal
end
dc457681 Joseph Mitchell Magen
end
90b83222 Ohad Levy
end