Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Fixes BZ #1176423: create reservation for VIP ip addresses in DHCP (Do not merge) #402

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion app/models/staypuft/vip_nic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,46 @@ class VipNic < Nic::Managed
has_one :deployment_vip_nic, :dependent => :destroy, :class_name => 'Staypuft::DeploymentVipNic'
has_one :deployment, :class_name => 'Staypuft::Deployment', :through => :deployment_vip_nic

before_save :reserve_ip
before_save :reserve_ip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick... either make them line up, or don't change the before_save.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ew :) I agree

before_destroy :release_ip

# VIP nic is associated with the deployment, not the host
def require_host?
false
end

def reserve_ip
# if changing subnets from dhcp network, clear old reservation
if self.subnet_id_changed? && self.subnet_id_was
old_subnet = Subnet.find(self.subnet_id_was)
if old_subnet && (old_subnet.ipam == Subnet::IPAM_MODES[:dhcp]) && old_subnet.dhcp?
begin
old_subnet.dhcp_proxy.delete(old_subnet.network, self.mac)
rescue ProxyAPI::ProxyException => ex
Rails.logger.error "Error removing DHCP address reservation for VIP nic #{identifier}_#{mac.delete(':')}, mac #{mac}: #{ex}"
end
end
end
if self.subnet.present? && self.subnet.ipam? && (!self.ip || self.subnet_id_changed?)
self.ip = self.subnet.unused_ip
if (subnet.ipam == Subnet::IPAM_MODES[:dhcp]) && subnet.dhcp?
begin
subnet.dhcp_proxy.set(subnet.network, {:mac => self.mac, :ip => self.ip, :hostname => "#{identifier}_#{mac.delete(':')}"})
rescue ProxyAPI::ProxyException => ex
Rails.logger.error "Error reserving DHCP address for VIP nic #{identifier}_#{mac.delete(':')}, mac #{mac}: #{ex}"
end
end
end
end

def release_ip
# if changing subnets from dhcp network, clear old reservation
if subnet.present? && (subnet.ipam == Subnet::IPAM_MODES[:dhcp]) && subnet.dhcp?
begin
subnet.dhcp_proxy.delete(subnet.network, self.mac)
rescue ProxyAPI::ProxyException => ex
Rails.logger.error "Error removing DHCP address reservation for VIP nic #{identifier}_#{mac.delete(':')}, mac #{mac}: #{ex}"
end
end
end
end
Expand Down