From d7bf4493d471d33c55f68c57f9a243c0268b6a3c Mon Sep 17 00:00:00 2001 From: nilsver Date: Fri, 23 Aug 2024 11:02:39 +0100 Subject: [PATCH 1/2] update hosts file --- resources/libraries/get_managers_all.rb | 16 ++++++ resources/libraries/update_hosts_file.rb | 67 ++++++++++++++++++++++++ resources/recipes/prepare_system.rb | 12 +++++ resources/templates/default/hosts.erb | 3 ++ 4 files changed, 98 insertions(+) create mode 100644 resources/libraries/get_managers_all.rb create mode 100644 resources/libraries/update_hosts_file.rb create mode 100644 resources/templates/default/hosts.erb diff --git a/resources/libraries/get_managers_all.rb b/resources/libraries/get_managers_all.rb new file mode 100644 index 0000000..dd813c7 --- /dev/null +++ b/resources/libraries/get_managers_all.rb @@ -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 diff --git a/resources/libraries/update_hosts_file.rb b/resources/libraries/update_hosts_file.rb new file mode 100644 index 0000000..9f4c7e9 --- /dev/null +++ b/resources/libraries/update_hosts_file.rb @@ -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 = sprintf("%-18s%s", ip, services.join(' ')) + hosts_entries << format_entry unless services.empty? + end + + hosts_entries + end + end +end diff --git a/resources/recipes/prepare_system.rb b/resources/recipes/prepare_system.rb index 9336322..e0e23d1 100644 --- a/resources/recipes/prepare_system.rb +++ b/resources/recipes/prepare_system.rb @@ -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 diff --git a/resources/templates/default/hosts.erb b/resources/templates/default/hosts.erb new file mode 100644 index 0000000..5ef8de4 --- /dev/null +++ b/resources/templates/default/hosts.erb @@ -0,0 +1,3 @@ +<% @hosts_entries.each do |host| %> +<%= host %> +<% end %> \ No newline at end of file From cc8b9a6d6934a9f10f0ba972fdb7ecc277f78484 Mon Sep 17 00:00:00 2001 From: nilsver Date: Fri, 23 Aug 2024 12:32:36 +0100 Subject: [PATCH 2/2] update format --- resources/libraries/update_hosts_file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/libraries/update_hosts_file.rb b/resources/libraries/update_hosts_file.rb index 9f4c7e9..0a6eec3 100644 --- a/resources/libraries/update_hosts_file.rb +++ b/resources/libraries/update_hosts_file.rb @@ -57,7 +57,7 @@ def update_hosts_file # Prepare the lines for the hosts file hosts_entries = [] hosts_hash.each do |ip, services| - format_entry = sprintf("%-18s%s", ip, services.join(' ')) + format_entry = format('%-18s%s', ip, services.join(' ')) hosts_entries << format_entry unless services.empty? end