Skip to content

Commit

Permalink
Update update hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
manegron committed Oct 8, 2024
1 parent b4a2579 commit 6ff442c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 55 deletions.
28 changes: 9 additions & 19 deletions resources/libraries/update_hosts_file.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
module RbIps
module Helpers
def load_rb_init_conf
@rb_init_conf ||= YAML.load_file('/etc/redborder/rb_init_conf.yml')
end

def get_setup_ip
conf = load_rb_init_conf
if node['roles'].include?('ipscp-sensor')
conf['cloud_address']
else
conf['webui_host']
end
end

def get_external_databag_services
Chef::DataBag.load('rBglobal').keys.grep(/^ipvirtual-external-/).map { |bag| bag.sub('ipvirtual-external-', '') }
end
Expand All @@ -30,7 +17,10 @@ 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
hosts_hash = read_hosts_file
Expand All @@ -44,7 +34,7 @@ def update_hosts_file
if ip && !ip.empty?
grouped_virtual_ips[ip] << bag.gsub('ipvirtual-external-', '')
else
grouped_virtual_ips[setup_ip] << bag.gsub('ipvirtual-external-', '')
grouped_virtual_ips[manager_registration_ip] << bag.gsub('ipvirtual-external-', '')
end
end

Expand All @@ -68,12 +58,12 @@ def update_hosts_file
end

# If there is a virtual ip and ips is manager mode
if new_ip && !node['roles'].include?('ipscp-sensor')
if new_ip && !node['roles'].include?('ipscp-sensor')
hosts_hash[new_ip] << "#{new_service}.service"
hosts_hash[new_ip] << "#{new_service}.#{node['redborder']['cdomain']}"
else # Add services with setup_ip
hosts_hash[setup_ip] << "#{new_service}.service"
hosts_hash[setup_ip] << "#{new_service}.#{node['redborder']['cdomain']}"
else # Add services with manager_registration_ip
hosts_hash[manager_registration_ip] << "#{new_service}.service"
hosts_hash[manager_registration_ip] << "#{new_service}.#{node['redborder']['cdomain']}"
end
end
end
Expand Down
72 changes: 36 additions & 36 deletions resources/recipes/system_health.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@
end

# Add manager node ip addr to /etc/hosts
ruby_block 'update_hosts_file_if_needed' do
block do
def managerToIp(str)
ipv4_regex = /\A(\d{1,3}\.){3}\d{1,3}\z/
ipv6_regex = /\A(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}\z/
dns_regex = /\A[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\z/

return str if str =~ ipv4_regex || str =~ ipv6_regex

if str =~ dns_regex
ip = `dig +short #{str}`.strip
return ip unless ip.empty?
end
end

unless node['redborder']['cloud']
# Read webui_host from the rb_init_conf.yml file
webui_host_command = "grep '^webui_host:' /etc/redborder/rb_init_conf.yml | awk '{print $2}'"
webui_host = managerToIp `#{webui_host_command}`.strip

# Search for a node matching the webui_host IP address
matching_node_name = search(:node, "ipaddress:#{webui_host}").first&.name

# Update /etc/hosts if a matching node is found
if matching_node_name
node_name_with_suffix = "#{matching_node_name}.node"
hosts_file = '/etc/hosts'

unless ::File.readlines(hosts_file).grep(/#{Regexp.escape(node_name_with_suffix)}/).any?
::File.open(hosts_file, 'a') { |file| file.puts "#{webui_host} #{node_name_with_suffix}" }
end
end
end
end
action :run
end
# ruby_block 'update_hosts_file_if_needed' do
# block do
# def managerToIp(str)
# ipv4_regex = /\A(\d{1,3}\.){3}\d{1,3}\z/
# ipv6_regex = /\A(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}\z/
# dns_regex = /\A[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\z/
#
# return str if str =~ ipv4_regex || str =~ ipv6_regex
#
# if str =~ dns_regex
# ip = `dig +short #{str}`.strip
# return ip unless ip.empty?
# end
# end
#
# unless node['redborder']['cloud']
# # Read webui_host from the rb_init_conf.yml file
# webui_host_command = "grep '^webui_host:' /etc/redborder/rb_init_conf.yml | awk '{print $2}'"
# webui_host = managerToIp `#{webui_host_command}`.strip
#
# # Search for a node matching the webui_host IP address
# matching_node_name = search(:node, "ipaddress:#{webui_host}").first&.name
#
# # Update /etc/hosts if a matching node is found
# if matching_node_name
# node_name_with_suffix = "#{matching_node_name}.node"
# hosts_file = '/etc/hosts'
#
# unless ::File.readlines(hosts_file).grep(/#{Regexp.escape(node_name_with_suffix)}/).any?
# ::File.open(hosts_file, 'a') { |file| file.puts "#{webui_host} #{node_name_with_suffix}" }
# end
# end
# end
# end
# action :run
# end

# Check barnyard2 health
ruby_block 'check_barnyard2_health' do
Expand Down

0 comments on commit 6ff442c

Please sign in to comment.