#
# Cookbook:: irrigate-redis
# Recipe:: default
#
# Copyright:: 2017, The Orchard, All Rights Reserved.

raise 'Unsupported RHEL version (< 7.4) detected' if node['platform_family'].include?('rhel') && node['platform_version'].to_f < 7.4

tuned_profile 'nothp_profile' do
  action [:create, :enable]
end

package 'epel-release' if node['redisio']['package_install'] == true

include_recipe 'redisio::install'
include_recipe 'redisio::configure'
include_recipe 'redisio::disable_os_default'
include_recipe 'redisio::enable'

# Make sure the firewall is installed and enabled
firewall 'default' do
  log_level :high
  action :install
end

firewall_rule 'established' do
  stateful %w(related established)
  protocol :none
  position 2
  command :allow
end

firewall_rule 'ssh' do
  port 22
  protocol :tcp
  position 3
  command :allow
end

node['redisio']['servers'].each do |current_server|
  redis_source_ip_addresses = node['redis']['source_ip_addresses']
  redis_source_ip_addresses.each_with_index do |source_ip, index|
    firewall_rule "redis-#{current_server['port']}-#{index}" do
      port current_server['port']
      protocol :tcp
      position 4 + index
      source source_ip
      command :allow
    end
  end
end
