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

update hosts file to remove sync_ip and add running services #47

Merged
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
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
Loading