Skip to content

Commit

Permalink
Merge pull request #41 from redBorder/feature/18393_update_hosts_file
Browse files Browse the repository at this point in the history
Feature/18393 update hosts file
  • Loading branch information
JPeraltaNic authored Sep 2, 2024
2 parents 2d14d08 + 982e2b0 commit acd32dc
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions resources/libraries/get_managers_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module RbProxy
module Helpers
def get_managers_all
managers = []
managers_keys = Chef::Node.list.keys.sort
managers_keys.each do |m_key|
m = Chef::Node.load(m_key)
roles = m[:roles] || []
if roles.include?('manager')
managers << m
end
end
managers
end
end
end
67 changes: 67 additions & 0 deletions resources/libraries/update_hosts_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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
hosts_hash = Hash.new { |hash, key| hash[key] = [] }
File.readlines('/etc/hosts').each do |line|
next if line.strip.empty? || line.start_with?('#')
values = line.split(/\s+/)
ip = values.shift
services = values
hosts_hash[ip].concat(services).uniq!
end

# Update hosts_hash based on grouped_virtual_ips
grouped_virtual_ips.each do |new_ip, new_services|
new_services.each do |new_service|
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']}"
end
end
end

# Prepare the lines for the hosts file
hosts_entries = []
hosts_hash.each do |ip, services|
format_entry = format('%-18s%s', ip, services.join(' '))
hosts_entries << format_entry unless services.empty?
end

hosts_entries
end
end
end
12 changes: 12 additions & 0 deletions resources/recipes/prepare_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,15 @@
sysmem_total = (node['memory']['total'].to_i * 0.90).to_i
# node attributes related with memory are changed inside the function to have simplicity using recursivity
memory_services(sysmem_total)

hosts_entries = update_hosts_file()

template '/etc/hosts' do
source 'hosts.erb'
cookbook 'rb-proxy'
owner 'root'
group 'root'
mode '644'
retries 2
variables(hosts_entries: hosts_entries)
end
3 changes: 3 additions & 0 deletions resources/templates/default/hosts.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @hosts_entries.each do |host| %>
<%= host %>
<% end %>

0 comments on commit acd32dc

Please sign in to comment.