Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #27975 - DHCP cleanup task #7073

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions lib/tasks/orchestration.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# TRANSLATORS: do not translate
desc <<-END_DESC
Orchestration maintainance tasks.

WARNING: Always backup server data (e.g. DHCP leases file, DNS journal files)
prior running these tasks. They do nothing by default, unless perform=1 is
specified.

Examples:
rake orchestration:dhcp:add_missing subnet_name=NAME
Preview missing DHCP records on a DHCP Smart Proxy.

rake orchestration:dhcp:add_missing subnet_name=NAME perform=1
Do create missing DHCP records on a DHCP Smart Proxy.

rake orchestration:dhcp:remove_offending subnet_name=NAME
Preview offending DHCP records on a DHCP Smart Proxy.

rake orchestration:dhcp:remove_offending subnet_name=NAME perform=1
Do remove offending DHCP records on a DHCP Smart Proxy.
END_DESC

def fetch_subnet(subnet_name)
subnet = Subnet.unscoped.find_by_name(subnet_name)
raise("Subnet '#{subnet_name}' not found") unless subnet
raise("Subnet '#{subnet_name}' has no DHCP proxy associated") unless subnet.dhcp_proxy
subnet
end

def fetch_dhcp_records(subnet)
subnet.dhcp_proxy.subnet(subnet.network)["reservations"].map do |rec|
Net::DHCP::Record.new(rec.merge(proxy: subnet.dhcp_proxy, network: subnet.network))
end
end

namespace :orchestration do
namespace :dhcp do
task :add_missing => :environment do
User.as_anonymous_admin do
dry_run = (ENV['perform'] != '1')
subnet = fetch_subnet(ENV['subnet_name'])
# Create missing records on DHCP proxy
dhcp_records = fetch_dhcp_records(subnet)
subnet.hosts.where(type: "Host::Managed").each do |host|
host.provision_interface.dhcp_records.each do |host_record|
Rails.logger.debug "Checking host #{host} (#{host.mac})"
if dhcp_records.include?(host_record)
Rails.logger.info "Host #{host} is up-to-date"
else
Rails.logger.warn "Host #{host} needs config rebuild"
host.recreate_config unless dry_run
end
end
end
end
end

task :remove_offending => :environment do
ekohl marked this conversation as resolved.
Show resolved Hide resolved
User.as_anonymous_admin do
dry_run = (ENV['perform'] != '1')
subnet = fetch_subnet(ENV['subnet_name'])
# Delete offending records from DHCP proxy
dhcp_records = fetch_dhcp_records(subnet)
dhcp_records.each do |dhcp_record|
Rails.logger.debug "Checking DHCP record #{dhcp_record}"
if subnet.id == Nic::Managed.unscoped.find_by_mac(dhcp_record.mac).try(:subnet_id)
Rails.logger.info "DHCP record #{dhcp_record} is up-to-date"
else
Rails.logger.warn "DHCP record #{dhcp_record} not found in DB, deleting"
subnet.dhcp_proxy.delete(subnet.network, dhcp_record.mac) unless dry_run
end
end
end
end
end
end