Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

Commit

Permalink
Support binding to all interfaces
Browse files Browse the repository at this point in the history
`bind` now takes an array (of IP addresses).
To bind to all interfaces, use `bind => []`.

Due to the new parameter validation, if you were previously using a
single string to specify multiple IP addresses, you will need to change
this to an array.

Fixes #257
Fixes fsalum#60
  • Loading branch information
alexjfisher committed Mar 12, 2019
1 parent e42ae30 commit d94f060
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 19 deletions.
14 changes: 12 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
# slaveof => '10.0.1.1 6379',
# }
#
# @example Binding on multiple interfaces
# class { 'redis':
# bind => ['127.0.0.1', '10.0.0.1', '10.1.0.1'],
# }
#
# @example Binding on all interfaces
# class { 'redis':
# bind => [],
# }
#
# @param [String] activerehashing Enable/disable active rehashing.
# @param [String] aof_load_truncated Enable/disable loading truncated AOF file
# @param [String] aof_rewrite_incremental_fsync Enable/disable fsync for AOF file
Expand All @@ -17,7 +27,7 @@
# @param [String] appendonly Enable/disable appendonly mode.
# @param [String] auto_aof_rewrite_min_size Adjust minimum size for auto-aof-rewrite.
# @param [String] auto_aof_rewrite_percentage Adjust percentatge for auto-aof-rewrite.
# @param [String] bind Configure which IP address to listen on.
# @param bind Configure which IP address(es) to listen on. To bind on all interfaces, use an empty array.
# @param [String] config_dir Directory containing the configuration files.
# @param [String] config_dir_mode Adjust mode for directory containing configuration files.
# @param [String] config_file_orig The location and name of a config file that provides the source
Expand Down Expand Up @@ -143,7 +153,7 @@
$appendonly = $::redis::params::appendonly,
$auto_aof_rewrite_min_size = $::redis::params::auto_aof_rewrite_min_size,
$auto_aof_rewrite_percentage = $::redis::params::auto_aof_rewrite_percentage,
$bind = $::redis::params::bind,
Variant[Stdlib::IP::Address, Array[Stdlib::IP::Address]] $bind = $::redis::params::bind,
$output_buffer_limit_slave = $::redis::params::output_buffer_limit_slave,
$output_buffer_limit_pubsub = $::redis::params::output_buffer_limit_pubsub,
$conf_template = $::redis::params::conf_template,
Expand Down
7 changes: 5 additions & 2 deletions manifests/instance.pp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# @param [String] appendonly Enable/disable appendonly mode.
# @param [String] auto_aof_rewrite_min_size Adjust minimum size for auto-aof-rewrite.
# @param [String] auto_aof_rewrite_percentage Adjust percentatge for auto-aof-rewrite.
# @param [String] bind Configure which IP address to listen on.
# @param bind Configure which IP address(es) to listen on. To bind on all interfaces, use an empty array.
# @param [String] config_dir Directory containing the configuration files.
# @param [String] config_dir_mode Adjust mode for directory containing configuration files.
# @param [String] config_file_orig The location and name of a config file that provides the source
Expand Down Expand Up @@ -133,7 +133,7 @@
$appendonly = $::redis::appendonly,
$auto_aof_rewrite_min_size = $::redis::auto_aof_rewrite_min_size,
$auto_aof_rewrite_percentage = $::redis::auto_aof_rewrite_percentage,
$bind = $::redis::bind,
Variant[Stdlib::IP::Address, Array[Stdlib::IP::Address]] $bind = $::redis::bind,
$output_buffer_limit_slave = $::redis::output_buffer_limit_slave,
$output_buffer_limit_pubsub = $::redis::output_buffer_limit_pubsub,
$conf_template = $::redis::conf_template,
Expand Down Expand Up @@ -314,6 +314,8 @@
refreshonly => true,
}

$bind_arr = [$bind].flatten

if $package_ensure =~ /^([0-9]+:)?[0-9]+\.[0-9]/ {
if ':' in $package_ensure {
$_redis_version_real = split($package_ensure, ':')
Expand All @@ -328,6 +330,7 @@
if ($redis_version_real and $conf_template == 'redis/redis.conf.erb') {
case $redis_version_real {
/^2.4./: {
if $bind_arr.length > 1 { fail('Redis 2.4 doesn\'t support binding to multiple IPs') }
File[$redis_file_name_orig] { content => template('redis/redis.conf.2.4.10.erb') }
}
/^2.8./: {
Expand Down
3 changes: 2 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$appendonly = false
$auto_aof_rewrite_min_size = '64mb'
$auto_aof_rewrite_percentage = 100
$bind = '127.0.0.1'
$bind = ['127.0.0.1']
$output_buffer_limit_slave = '256mb 64mb 60'
$output_buffer_limit_pubsub = '32mb 8mb 60'
$conf_template = 'redis/redis.conf.erb'
Expand Down Expand Up @@ -61,6 +61,7 @@
$sentinel_parallel_sync = 1
$sentinel_port = 26379
$sentinel_quorum = 2
$sentinel_redis_host = '127.0.0.1'
$sentinel_service_name = 'redis-sentinel'
$sentinel_working_dir = '/tmp'
$sentinel_init_template = 'redis/redis-sentinel.init.erb'
Expand Down
2 changes: 1 addition & 1 deletion manifests/sentinel.pp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
$log_level = $::redis::params::log_level,
$log_file = $::redis::params::log_file,
$master_name = $::redis::params::sentinel_master_name,
$redis_host = $::redis::params::bind,
Stdlib::Host $redis_host = $::redis::params::sentinel_redis_host,
$redis_port = $::redis::params::port,
$package_name = $::redis::params::sentinel_package_name,
$package_ensure = $::redis::params::sentinel_package_ensure,
Expand Down
39 changes: 33 additions & 6 deletions spec/classes/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,41 @@
it { is_expected.to contain_file(config_file_orig).with_content(%r{auto-aof-rewrite-percentage.*_VALUE_}) }
end

describe 'with parameter bind' do
let(:params) do
{
bind: '_VALUE_'
}
describe 'parameter bind' do
context 'by default' do
it 'binds to localhost' do
is_expected.to contain_file(config_file_orig).with_content(%r{bind 127\.0\.0\.1$})
end
end
context 'with a single IP address' do
let(:params) { { bind: '10.0.0.1' } }

it { is_expected.to contain_file(config_file_orig).with_content(%r{bind 10\.0\.0\.1$}) }
end
context 'with array of IP addresses' do
let(:params) do
{
bind: ['127.0.0.1', '::1']
}
end

it { is_expected.to contain_file(config_file_orig).with_content(%r{bind.*_VALUE_}) }
it { is_expected.to contain_file(config_file_orig).with_content(%r{bind 127\.0\.0\.1 ::1}) }
end
context 'with empty array' do
let(:params) { { bind: [] } }

it { is_expected.not_to contain_file(config_file_orig).with_content(%r{^bind}) }
end
context 'with multiple IP addresses on redis 2.4' do
let(:params) do
{
package_ensure: '2.4.10',
bind: ['127.0.0.1', '::1']
}
end

it { is_expected.to compile.and_raise_error(%r{Redis 2\.4 doesn't support binding to multiple IPs}) }
end
end

describe 'with parameter output_buffer_limit_slave' do
Expand Down
4 changes: 3 additions & 1 deletion templates/redis.conf.2.4.10.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ pidfile <%= @pid_file %>
# If port 0 is specified Redis will not listen on a TCP socket.
port <%= @port %>

<% unless @bind_arr.empty? -%>
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
bind <%= @bind %>
bind <%= @bind_arr.join(' ') %>
<% end -%>

# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
Expand Down
4 changes: 3 additions & 1 deletion templates/redis.conf.2.8.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ port <%= @port %>
tcp-backlog <%= @tcp_backlog %>
<% end -%>

<% unless @bind_arr.empty? -%>
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
Expand All @@ -72,7 +73,8 @@ tcp-backlog <%= @tcp_backlog %>
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
bind <%= @bind %>
bind <%= @bind_arr.join(' ') %>
<% end -%>

# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
Expand Down
11 changes: 7 additions & 4 deletions templates/redis.conf.3.2.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ port <%= @port %>
# in order to get the desired effect.
tcp-backlog <%= @tcp_backlog %>

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
bind <%= @bind %>
<% unless @bind_arr.empty? -%>
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
bind <%= @bind_arr.join(' ') %>
<% end -%>

# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
Expand Down
4 changes: 3 additions & 1 deletion templates/redis.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ port <%= @port %>
# in order to get the desired effect.
tcp-backlog <%= @tcp_backlog %>

<% unless @bind_arr.empty? -%>
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
bind <%= @bind %>
bind <%= @bind_arr.join(' ') %>
<% end -%>

# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
Expand Down

0 comments on commit d94f060

Please sign in to comment.