Skip to content

Commit

Permalink
Merge pull request #43 from redBorder/development
Browse files Browse the repository at this point in the history
Release 0.5.0
  • Loading branch information
manegron authored Sep 2, 2024
2 parents b271475 + 794a4a6 commit 2a15362
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
cookbook-rb-proxy CHANGELOG
===============

## 0.5.0

- nilsver
- [cc8b9a6] update format
- [d7bf449] update hosts file
- Luis Blanco
- [cb38a36] disable service ale by default
- [bbaff20] action based on if there is any ale sensor

## 0.4.0

- Miguel Alvarez
Expand Down
2 changes: 1 addition & 1 deletion resources/attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
default['redborder']['services']['pmacct'] = true
default['redborder']['services']['rsyslog'] = true
default['redborder']['services']['redborder-nmsp'] = true
default['redborder']['services']['redborder-ale'] = true
default['redborder']['services']['redborder-ale'] = false
default['redborder']['services']['n2klocd'] = true
default['redborder']['services']['radiusd'] = false
default['redborder']['services']['k2http'] = true
Expand Down
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
2 changes: 1 addition & 1 deletion resources/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
maintainer_email 'git@redborder.com'
license 'AGPL-3.0'
description 'Installs/Configures redborder proxy'
version '0.4.0'
version '0.5.0'

depends 'rb-common'
depends 'rb-selinux'
Expand Down
5 changes: 3 additions & 2 deletions resources/recipes/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@

# TODO: replace node['redborder']['services'] in action with 'proxy_services'..
rbale_config 'Configure redborder-ale' do
ale_nodes node.run_state['sensors_info_all']['ale-sensor']
if node['redborder']['services']['redborder-ale']
ale_sensors = node.run_state['sensors_info_all']['ale-sensor']
if !ale_sensors.empty?
ale_nodes ale_sensors
action [:add]
else
action [:remove]
Expand Down
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 2a15362

Please sign in to comment.