Skip to content

Commit

Permalink
add library to get virtual ips
Browse files Browse the repository at this point in the history
  • Loading branch information
davidredborder committed Jun 12, 2024
1 parent 32fe52b commit 161b5dd
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions resources/libraries/get_virtual_ip_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module Rb_manager
module Helpers
def get_virtual_ip_info(managers)
virtual_ips = {}
ip_regex = /^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$/
has_any_virtual_ip=false

node["redborder"]["manager"]["virtual_ips"].each do |type, services|
virtual_ips[type.to_s] = {}
services.each do |service|
virtual_dg = Chef::DataBagItem.load('rBglobal', "ipvirtual-#{type}-#{service["service"]}") rescue ipvirtual_dg = {}
hash = {}
hash['ip'] = virtual_dg['ip']
hash['loadbalance'] = (virtual_dg['loadbalance'].nil? ? true : virtual_dg['loadbalance'])

hash['service'] = service['service']
if virtual_dg['ip'] =~ ip_regex and !hash['ip'].nil?
hash['enable'] = true
else
hash['enable'] = false
end

if hash['enable']
hash['virtual'] = true
hash['run_anywhere'] = true
has_any_virtual_ip=true if (hash['ip'] and manager_services[service['service']])
hash["iface"]= node['redborder']['management_interface']
if manager_services[service['service']]
all_deps_enabled=true
if !service['deps'].nil?
service['deps'].each do |srv_dep|
all_deps_enabled=false if manager_services[srv_dep].nil? or manager_services[srv_dep]==false
end
end
hash['enable'] = all_deps_enabled
else
hash['enable'] = false
end
else
#The virtual ip is not valid or it has not been specified. We need to set at least one
hash['virtual'] = false
hash['run_anywhere'] = false
nodeservice=nil
managers.each do |m|
hash["iface"] = node['redborder']['management_interface']
run_anywhere_flag=false

if m.name == node.name
run_anywhere_flag=manager_services[service['service']]
elsif !m['redborder'].nil? and !m['redborder']['manager'].nil? and !m['redborder']['manager']['services'].nil? and !m['redborder']['manager']['services']['current'].nil?
run_anywhere_flag=m['redborder']["manager"]["services"]["current"][service['service']]
end

if run_anywhere_flag and !m['redborder']['manager'][hash['iface']].nil?
nodeservice=m
break
end
end

if nodeservice.nil?
managers.each do |m|
hash["iface"] = node['redborder']['management_interface']
run_anywhere_flag=false
run_anywhere_flag=m['redborder']['manager']['services']['overwrite'][service['service']] if !m['redborder']['manager']['services']['overwrite'].nil?
run_anywhere_flag=m['redborder']['manager']['services'][m['redborder']['manager']['mode']][service['service']] if (run_anywhere_flag!=true and !m['redborder']['manager']['services'][m['redborder']['manager']['mode']].nil?)
if run_anywhere_flag and !m["redborder"]["manager"][hash["iface"]].nil?
nodeservice=m
break
end
end
end

if !nodeservice.nil? and !nodeservice['redborder']['manager'][hash['iface']].nil?
hash['virtual'] = false
hash['ip'] = nodeservice['redborder']['manager'][hash['iface']]['ip']
hash['physical_ip'] = hash['ip']
hash['prefixlen'] = nodeservice['redborder']['manager'][hash['iface']]['prefixlen']
hash['run_anywhere'] = true
end
end
virtual_ips[type.to_s][service["service"]] = hash
end
end
return virtual_ips, has_any_virtual_ip

end

def get_virtual_ips_per_ip_info(virtual_ips)
virtual_ips_per_ip = {}
virtual_ips.each do |type, data|
data.each.each do |service, vi|
if vi['ip']
virtual_ips_per_ip[vi['ip']] = [] if virtual_ips_per_ip[vi['ip']].nil?
virtual_ips_per_ip[vi['ip']] << vi
end
end
end
virtual_ips_per_ip
end

end
end

0 comments on commit 161b5dd

Please sign in to comment.