Skip to content

Commit

Permalink
update hosts file
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsver committed Sep 26, 2024
1 parent 1effbed commit f1d532e
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions resources/libraries/update_hosts_file.rb
Original file line number Diff line number Diff line change
@@ -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?('#')
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit f1d532e

Please sign in to comment.