From f1d532eb929f8eb350450aa43e93a3a897f2d734 Mon Sep 17 00:00:00 2001 From: nilsver Date: Thu, 26 Sep 2024 15:41:06 +0100 Subject: [PATCH 1/5] update hosts file --- resources/libraries/update_hosts_file.rb | 67 +++++++++++------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/resources/libraries/update_hosts_file.rb b/resources/libraries/update_hosts_file.rb index 0a6eec3..7a85f5e 100644 --- a/resources/libraries/update_hosts_file.rb +++ b/resources/libraries/update_hosts_file.rb @@ -1,31 +1,16 @@ 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'] + def get_setup_ip + rb_init_conf = YAML.load_file('/etc/redborder/rb_init_conf.yml') + setup_ip = rb_init_conf['cloud_address'] + setup_ip + end - 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 + def get_external_databag_services + Chef::DataBag.load('rBglobal').keys.grep(/^ipvirtual-external-/).map { |bag| bag.sub('ipvirtual-external-', '') } + 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?('#') @@ -34,23 +19,34 @@ def update_hosts_file services = values hosts_hash[ip].concat(services).uniq! end + hosts_hash + end + + def update_hosts_file + setup_ip = get_setup_ip + running_services = node['redborder']['systemdservices'].values.flatten if node['redborder']['systemdservices'] + databags = get_external_databag_services + 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[setup_ip] << "#{bag_serv}" } + running_services.each { |serv| grouped_virtual_ips['127.0.0.1'] << "#{serv}" } - # Update hosts_hash based on grouped_virtual_ips + # Group services grouped_virtual_ips.each do |new_ip, new_services| new_services.each do |new_service| + # Remove suffix and get services service_key = new_service.split('.').first + hosts_hash.each { |_ip, services| services.delete_if { |service| service.split('.').first == service_key } } - 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 running_services.include?(new_service) + hosts_hash['127.0.0.1'] << "#{new_service}.service" + next end + hosts_hash[setup_ip] << "#{new_service}.service" + hosts_hash[setup_ip] << "#{new_service}.#{node['redborder']['cdomain']}" end end @@ -60,7 +56,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 From 7a174c54c9d446108d93c923e2136d09bea6ec53 Mon Sep 17 00:00:00 2001 From: nilsver Date: Thu, 26 Sep 2024 15:50:04 +0100 Subject: [PATCH 2/5] fix lint --- resources/libraries/update_hosts_file.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/resources/libraries/update_hosts_file.rb b/resources/libraries/update_hosts_file.rb index 7a85f5e..cd0ee18 100644 --- a/resources/libraries/update_hosts_file.rb +++ b/resources/libraries/update_hosts_file.rb @@ -2,8 +2,7 @@ module RbProxy module Helpers def get_setup_ip rb_init_conf = YAML.load_file('/etc/redborder/rb_init_conf.yml') - setup_ip = rb_init_conf['cloud_address'] - setup_ip + rb_init_conf['cloud_address'] end def get_external_databag_services @@ -34,7 +33,7 @@ def update_hosts_file running_services.each { |serv| grouped_virtual_ips['127.0.0.1'] << "#{serv}" } # Group services - grouped_virtual_ips.each do |new_ip, new_services| + grouped_virtual_ips.each do |_new_ip, new_services| new_services.each do |new_service| # Remove suffix and get services service_key = new_service.split('.').first From 20f43484aa7559c9eb460eb181b0fb72be63cd2c Mon Sep 17 00:00:00 2001 From: nilsver Date: Mon, 7 Oct 2024 14:00:23 +0100 Subject: [PATCH 3/5] fix duplication bug --- resources/libraries/update_hosts_file.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/resources/libraries/update_hosts_file.rb b/resources/libraries/update_hosts_file.rb index cd0ee18..1dc8e20 100644 --- a/resources/libraries/update_hosts_file.rb +++ b/resources/libraries/update_hosts_file.rb @@ -30,17 +30,20 @@ def update_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[setup_ip] << "#{bag_serv}" } - running_services.each { |serv| grouped_virtual_ips['127.0.0.1'] << "#{serv}" } + running_services.each { |serv| grouped_virtual_ips['127.0.0.1'] << serv } # Group services - grouped_virtual_ips.each do |_new_ip, new_services| + grouped_virtual_ips.each do |new_ip, new_services| new_services.each do |new_service| - # Remove suffix and get services + # Avoids having duplicate services in the list service_key = new_service.split('.').first - hosts_hash.each { |_ip, services| services.delete_if { |service| service.split('.').first == service_key } } + hosts_hash.each do |_ip, services| + services_before = services.dup + services.delete_if { |service| service.split('.').first == service_key } + end # Add running services to localhost - if running_services.include?(new_service) + if new_ip == '127.0.0.1' && running_services.include?(new_service) hosts_hash['127.0.0.1'] << "#{new_service}.service" next end From cf63684cde317c6139f26564d3a99f41eb11fd78 Mon Sep 17 00:00:00 2001 From: nilsver Date: Mon, 7 Oct 2024 14:02:20 +0100 Subject: [PATCH 4/5] fix lint --- resources/libraries/update_hosts_file.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/libraries/update_hosts_file.rb b/resources/libraries/update_hosts_file.rb index 1dc8e20..a3cf058 100644 --- a/resources/libraries/update_hosts_file.rb +++ b/resources/libraries/update_hosts_file.rb @@ -38,7 +38,6 @@ def update_hosts_file # Avoids having duplicate services in the list service_key = new_service.split('.').first hosts_hash.each do |_ip, services| - services_before = services.dup services.delete_if { |service| service.split('.').first == service_key } end From 08d7c8ec40beb56f9525b4412573f4c2cfe23b1f Mon Sep 17 00:00:00 2001 From: manegron Date: Tue, 8 Oct 2024 22:31:28 +0100 Subject: [PATCH 5/5] Simplify code --- resources/libraries/update_hosts_file.rb | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/resources/libraries/update_hosts_file.rb b/resources/libraries/update_hosts_file.rb index a3cf058..e35bae3 100644 --- a/resources/libraries/update_hosts_file.rb +++ b/resources/libraries/update_hosts_file.rb @@ -1,14 +1,5 @@ module RbProxy module Helpers - def get_setup_ip - rb_init_conf = YAML.load_file('/etc/redborder/rb_init_conf.yml') - rb_init_conf['cloud_address'] - end - - def get_external_databag_services - Chef::DataBag.load('rBglobal').keys.grep(/^ipvirtual-external-/).map { |bag| bag.sub('ipvirtual-external-', '') } - end - def read_hosts_file hosts_hash = Hash.new { |hash, key| hash[key] = [] } File.readlines('/etc/hosts').each do |line| @@ -22,14 +13,17 @@ def read_hosts_file end def update_hosts_file - setup_ip = get_setup_ip + 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 = get_external_databag_services + 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[setup_ip] << "#{bag_serv}" } + 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 @@ -46,8 +40,8 @@ def update_hosts_file hosts_hash['127.0.0.1'] << "#{new_service}.service" next end - hosts_hash[setup_ip] << "#{new_service}.service" - hosts_hash[setup_ip] << "#{new_service}.#{node['redborder']['cdomain']}" + hosts_hash[manager_registration_ip] << "#{new_service}.service" + hosts_hash[manager_registration_ip] << "#{new_service}.#{node['redborder']['cdomain']}" end end