Skip to content

Commit

Permalink
Merge pull request #47 from redBorder/bugfix/18716_remove_sync_addres…
Browse files Browse the repository at this point in the history
…s_from_hosts_file

update hosts file to remove sync_ip and add running services
  • Loading branch information
manegron authored Oct 8, 2024
2 parents 630cc1d + 08d7c8e commit 64e7db8
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions resources/libraries/update_hosts_file.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
module RbProxy
module Helpers
def update_hosts_file
managers = get_managers_all()
manager_ip = []
managers.each do |m|
manager_ip << m['ipaddress_sync']
end

# grouped_virtual_ips returns a hash where:
# - The keys are IP addresses from the data bags, or `nil` if an IP is missing.
# - The values are arrays of services associated with each IP address.
# - If an IP is missing from a data bag, the associated services are grouped under the sync_ip key.
grouped_virtual_ips = Hash.new { |hash, key| hash[key] = [] }
databags = Chef::DataBag.load('rBglobal').keys
databags.each do |bag|
next unless bag.start_with?('ipvirtual-external')
virtual_dg = data_bag_item('rBglobal', bag)
ip = virtual_dg['ip']

if ip && !ip.empty?
grouped_virtual_ips[ip] << bag.gsub('ipvirtual-external-', '')
else
grouped_virtual_ips[manager_ip[0]] << bag.gsub('ipvirtual-external-', '')
end
end

# Read hosts file and store in hash
def read_hosts_file
hosts_hash = Hash.new { |hash, key| hash[key] = [] }
File.readlines('/etc/hosts').each do |line|
next if line.strip.empty? || line.start_with?('#')
Expand All @@ -34,23 +9,39 @@ def update_hosts_file
services = values
hosts_hash[ip].concat(services).uniq!
end
hosts_hash
end

# Update hosts_hash based on grouped_virtual_ips
def update_hosts_file
manager_registration_ip = node['redborder']['manager_registration_ip'] if node['redborder'] && node['redborder']['manager_registration_ip']

return unless manager_registration_ip

running_services = node['redborder']['systemdservices'].values.flatten if node['redborder']['systemdservices']
databags = Chef::DataBag.load('rBglobal').keys.grep(/^ipvirtual-external-/).map { |bag| bag.sub('ipvirtual-external-', '') }
hosts_hash = read_hosts_file

# Hash where services (from databag) are grouped by ip
grouped_virtual_ips = Hash.new { |hash, key| hash[key] = [] }
databags.each { |bag_serv| grouped_virtual_ips[manager_registration_ip] << "#{bag_serv}" }
running_services.each { |serv| grouped_virtual_ips['127.0.0.1'] << serv }

# Group services
grouped_virtual_ips.each do |new_ip, new_services|
new_services.each do |new_service|
# Avoids having duplicate services in the list
service_key = new_service.split('.').first

hosts_hash.each do |_ip, services|
services.delete_if { |service| service.split('.').first == service_key }
end

if new_ip
hosts_hash[new_ip] << "#{new_service}.service"
hosts_hash[new_ip] << "#{new_service}.#{node['redborder']['cdomain']}"
else
hosts_hash[manager_ip[0]] << "#{new_service}.service"
hosts_hash[manager_ip[0]] << "#{new_service}.#{node['redborder']['cdomain']}"
# Add running services to localhost
if new_ip == '127.0.0.1' && running_services.include?(new_service)
hosts_hash['127.0.0.1'] << "#{new_service}.service"
next
end
hosts_hash[manager_registration_ip] << "#{new_service}.service"
hosts_hash[manager_registration_ip] << "#{new_service}.#{node['redborder']['cdomain']}"
end
end

Expand All @@ -60,7 +51,6 @@ def update_hosts_file
format_entry = format('%-18s%s', ip, services.join(' '))
hosts_entries << format_entry unless services.empty?
end

hosts_entries
end
end
Expand Down

0 comments on commit 64e7db8

Please sign in to comment.