diff --git a/manifests/params.pp b/manifests/params.pp index 6d85c870..59ef8796 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -53,16 +53,16 @@ } 'RedHat': { - $ppa_repo = undef - $daemonize = false - $config_owner = 'redis' - $config_group = 'root' - $config_dir_mode = '0755' - $log_dir_mode = '0750' - - $sentinel_daemonize = false - $sentinel_working_dir = '/tmp' - $sentinel_protected_mode = true + $ppa_repo = undef + $daemonize = false + $config_owner = 'redis' + $config_group = 'root' + $config_dir_mode = '0755' + $log_dir_mode = '0750' + + $sentinel_daemonize = false + $sentinel_working_dir = '/tmp' + $sentinel_protected_mode = true $scl = $redis::globals::scl if $scl { @@ -203,4 +203,16 @@ fail "Operating system ${facts['os']['name']} is not supported yet." } } + + $sentinel_monitor_defaults = { + redis_host => '127.0.0.1', + redis_port => 6379, + quorum => 2, + down_after => 30000, + parallel_sync => 1, + failover_timeout => 180000, + } + $sentinel_default_monitor = { + 'mymaster' => $sentinel_monitor_defaults, + } } diff --git a/manifests/sentinel.pp b/manifests/sentinel.pp index e29e364f..c9148d36 100644 --- a/manifests/sentinel.pp +++ b/manifests/sentinel.pp @@ -1,7 +1,10 @@ # @summary Install redis-sentinel # -# @param auth_pass -# The password to use to authenticate with the master and slaves. +# @param sentinel_monitors +# Specify the sentinel monitor. +# +# @param monitor_defaults +# Override the monitor defaults # # @param config_file # The location and name of the sentinel config file. @@ -22,30 +25,12 @@ # @param daemonize # Have Redis sentinel run as a daemon. # -# @param down_after -# Number of milliseconds the master (or any attached slave or sentinel) -# should be unreachable (as in, not acceptable reply to PING, continuously, -# for the specified period) in order to consider it in S_DOWN state. -# -# @param failover_timeout -# Specify the failover timeout in milliseconds. -# # @param log_file # Specify where to write log entries. # # @param log_level # Specify how much we should log. # -# @param master_name -# Specify the name of the master redis server. -# The valid charset is A-z 0-9 and the three characters ".-_". -# -# @param redis_host -# Specify the bound host of the master redis server. -# -# @param redis_port -# Specify the port of the master redis server. -# # @param protected_mode # Whether protected mode is enabled or not. Only applicable when no bind is set. # @@ -55,17 +40,9 @@ # @param package_ensure # Do we ensure this package. # -# @param parallel_sync -# How many slaves can be reconfigured at the same time to use a -# new master after a failover. -# # @param pid_file # If sentinel is daemonized it will write its pid at this location. # -# @param quorum -# Number of sentinels that must agree that a master is down to -# signal sdown state. -# # @param sentinel_bind # Allow optional sentinel server ip binding. Can help overcome # issues arising from protect-mode added Redis 3.2 @@ -89,51 +66,55 @@ # The directory into which sentinel will change to avoid mount # conflicts. # -# @param notification_script -# Path to the notification script -# -# @param client_reconfig_script -# Path to the client-reconfig script -# # @example Basic inclusion # include redis::sentinel # # @example Configuring options # class {'redis::sentinel': -# down_after => 80000, # log_file => '/var/log/redis/sentinel.log', -# } +# sentinel_monitors => { +# 'session' => { +# redis_host => $redis_master_ip, +# redis_port => 6381, +# quorum => 2, +# parallel_sync => 1, +# down_after => 5000, +# failover_timeout => 12000, +# auth_pass => $redis_auth, +# }, +# 'cache' => { +# redis_host => $redis_master_ip, +# redis_port => 6380, +# quorum => 2, +# parallel_sync => 1, +# down_after => 5000, +# failover_timeout => 12000, +# auth_pass => $redis_auth, +# } +# } # class redis::sentinel ( - Optional[String[1]] $auth_pass = undef, - Stdlib::Absolutepath $config_file = $redis::params::sentinel_config_file, - Stdlib::Absolutepath $config_file_orig = $redis::params::sentinel_config_file_orig, - Stdlib::Filemode $config_file_mode = '0644', - String[1] $conf_template = 'redis/redis-sentinel.conf.erb', - Boolean $daemonize = $redis::params::sentinel_daemonize, - Boolean $protected_mode = $redis::params::sentinel_protected_mode, - Integer[1] $down_after = 30000, - Integer[1] $failover_timeout = 180000, - Redis::LogLevel $log_level = 'notice', - Stdlib::Absolutepath $log_file = $redis::params::sentinel_log_file, - String[1] $master_name = 'mymaster', - Stdlib::Host $redis_host = '127.0.0.1', - Stdlib::Port $redis_port = 6379, - String[1] $package_name = $redis::params::sentinel_package_name, - String[1] $package_ensure = 'present', - Integer[0] $parallel_sync = 1, - Stdlib::Absolutepath $pid_file = $redis::params::sentinel_pid_file, - Integer[1] $quorum = 2, + Stdlib::Absolutepath $config_file = $redis::params::sentinel_config_file, + Stdlib::Absolutepath $config_file_orig = $redis::params::sentinel_config_file_orig, + Stdlib::Filemode $config_file_mode = '0644', + String[1] $conf_template = 'redis/redis-sentinel.conf.erb', + Boolean $daemonize = $redis::params::sentinel_daemonize, + Boolean $protected_mode = $redis::params::sentinel_protected_mode, + Redis::LogLevel $log_level = 'notice', + Stdlib::Absolutepath $log_file = $redis::params::sentinel_log_file, + Redis::SentinelMonitors $sentinel_monitors = $redis::params::sentinel_default_monitor, + Redis::SentinelMonitorDefaults $monitor_defaults = $redis::params::sentinel_monitor_defaults, + String[1] $package_name = $redis::params::sentinel_package_name, + String[1] $package_ensure = 'present', + Stdlib::Absolutepath $pid_file = $redis::params::sentinel_pid_file, + Stdlib::Port $sentinel_port = 26379, + String[1] $service_group = 'redis', + String[1] $service_name = $redis::params::sentinel_service_name, + Stdlib::Ensure::Service $service_ensure = 'running', + Boolean $service_enable = true, + String[1] $service_user = 'redis', + Stdlib::Absolutepath $working_dir = $redis::params::sentinel_working_dir, Variant[Undef, Stdlib::IP::Address, Array[Stdlib::IP::Address]] $sentinel_bind = undef, - Stdlib::Port $sentinel_port = 26379, - String[1] $service_group = 'redis', - String[1] $service_name = $redis::params::sentinel_service_name, - Stdlib::Ensure::Service $service_ensure = 'running', - Boolean $service_enable = true, - String[1] $service_user = 'redis', - Stdlib::Absolutepath $working_dir = $redis::params::sentinel_working_dir, - Optional[Stdlib::Absolutepath] $notification_script = undef, - Optional[Stdlib::Absolutepath] $client_reconfig_script = undef, ) inherits redis::params { require 'redis' @@ -143,6 +124,10 @@ $sentinel_bind_arr = delete_undef_values([$sentinel_bind].flatten) $supports_protected_mode = $redis::supports_protected_mode + $_monitor = $sentinel_monitors.map |$monitor,$values| { + $redis_values = $monitor_defaults + { 'monitor_name' => $monitor } + $values + } + file { $config_file_orig: ensure => file, owner => $service_user, diff --git a/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb b/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb index 277242f5..37a273f8 100644 --- a/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb +++ b/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb @@ -11,9 +11,16 @@ it 'runs successfully' do pp = <<-EOS class { 'redis::sentinel': - master_name => 'mymaster', - redis_host => '127.0.0.1', - failover_timeout => 10000, + sentinel_monitors => { + 'mymaster' => { + redis_host => '127.0.0.1', + redis_port => 6379, + quorum => 2, + parallel_sync => 1, + down_after => 30000, + failover_timeout => 10000, + }, + }, } EOS diff --git a/spec/classes/redis_sentinel_spec.rb b/spec/classes/redis_sentinel_spec.rb index 9d277c8f..b531e73a 100644 --- a/spec/classes/redis_sentinel_spec.rb +++ b/spec/classes/redis_sentinel_spec.rb @@ -77,16 +77,23 @@ describe 'with custom parameters' do let(:params) do { - auth_pass: 'password', + sentinel_monitors: { + 'mymaster' => { + 'redis_host' => '127.0.0.1', + 'redis_port' => 6379, + 'quorum' => 2, + 'parallel_sync' => 1, + 'auth_pass' => 'password', + 'down_after' => 6000, + 'failover_timeout' => 28_000, + 'notification_script' => '/path/to/bar.sh', + 'client_reconfig_script' => '/path/to/foo.sh', + }, + }, sentinel_bind: '192.0.2.10', protected_mode: false, - master_name: 'cow', - down_after: 6000, working_dir: '/tmp/redis', - log_file: '/tmp/barn-sentinel.log', - failover_timeout: 28_000, - notification_script: '/path/to/bar.sh', - client_reconfig_script: '/path/to/foo.sh' + log_file: '/tmp/barn-sentinel.log' } end @@ -98,13 +105,13 @@ daemonize #{facts[:osfamily] == 'RedHat' ? 'no' : 'yes'} pidfile #{pidfile} #{protected_mode ? "protected-mode no\n" : ''} -sentinel monitor cow 127.0.0.1 6379 2 -sentinel down-after-milliseconds cow 6000 -sentinel parallel-syncs cow 1 -sentinel failover-timeout cow 28000 -sentinel auth-pass cow password -sentinel notification-script cow /path/to/bar.sh -sentinel client-reconfig-script cow /path/to/foo.sh +sentinel monitor mymaster 127.0.0.1 6379 2 +sentinel down-after-milliseconds mymaster 6000 +sentinel parallel-syncs mymaster 1 +sentinel failover-timeout mymaster 28000 +sentinel auth-pass mymaster password +sentinel notification-script mymaster /path/to/bar.sh +sentinel client-reconfig-script mymaster /path/to/foo.sh loglevel notice logfile /tmp/barn-sentinel.log @@ -119,15 +126,22 @@ describe 'with array sentinel bind' do let(:params) do { - auth_pass: 'password', + sentinel_monitors: { + 'cow' => { + 'redis_host' => '127.0.0.1', + 'redis_port' => 6379, + 'quorum' => 2, + 'parallel_sync' => 1, + 'auth_pass' => 'password', + 'down_after' => 6000, + 'failover_timeout' => 28_000, + 'notification_script' => '/path/to/bar.sh', + 'client_reconfig_script' => '/path/to/foo.sh', + }, + }, sentinel_bind: ['192.0.2.10', '192.168.1.1'], - master_name: 'cow', - down_after: 6000, working_dir: '/tmp/redis', - log_file: '/tmp/barn-sentinel.log', - failover_timeout: 28_000, - notification_script: '/path/to/bar.sh', - client_reconfig_script: '/path/to/foo.sh' + log_file: '/tmp/barn-sentinel.log' } end diff --git a/templates/redis-sentinel.conf.erb b/templates/redis-sentinel.conf.erb index 632fa302..33d80d91 100644 --- a/templates/redis-sentinel.conf.erb +++ b/templates/redis-sentinel.conf.erb @@ -9,19 +9,20 @@ pidfile <%= @pid_file %> protected-mode <%= @protected_mode ? 'yes' : 'no' %> <% end -%> -sentinel monitor <%= @master_name %> <%= @redis_host %> <%= @redis_port %> <%= @quorum %> -sentinel down-after-milliseconds <%= @master_name %> <%= @down_after %> -sentinel parallel-syncs <%= @master_name %> <%= @parallel_sync %> -sentinel failover-timeout <%= @master_name %> <%= @failover_timeout %> -<% if @auth_pass -%> -sentinel auth-pass <%= @master_name %> <%= @auth_pass %> +<% @_monitor.each do |values| -%> +sentinel monitor <%= values['monitor_name'] %> <%= values['redis_host'] %> <%= values['redis_port'] %> <%= values['quorum'] %> +sentinel down-after-milliseconds <%= values['monitor_name'] %> <%= values['down_after'] %> +sentinel parallel-syncs <%= values['monitor_name'] %> <%= values['parallel_sync'] %> +sentinel failover-timeout <%= values['monitor_name'] %> <%= values['failover_timeout'] %> +<% if values['auth_pass'] -%> +sentinel auth-pass <%= values['monitor_name'] %> <%= values['auth_pass'] %> <% end -%> -<% if @notification_script -%> -sentinel notification-script <%= @master_name %> <%= @notification_script %> +<% if values['notification_script'] -%> +sentinel notification-script <%= values['monitor_name'] %> <%= values['notification_script'] %> <% end -%> -<% if @client_reconfig_script -%> -sentinel client-reconfig-script <%= @master_name %> <%= @client_reconfig_script %> +<% if values['client_reconfig_script'] -%> +sentinel client-reconfig-script <%= values['monitor_name'] %> <%= values['client_reconfig_script'] %> +<% end %> <% end -%> - loglevel <%= @log_level %> logfile <%= @log_file %> diff --git a/types/sentinelmonitordefaults.pp b/types/sentinelmonitordefaults.pp new file mode 100644 index 00000000..53be2904 --- /dev/null +++ b/types/sentinelmonitordefaults.pp @@ -0,0 +1,12 @@ +type Redis::SentinelMonitorDefaults = Struct[{ + redis_host => Stdlib::Host, + redis_port => Stdlib::Port, + quorum => Integer[1], + down_after => Integer[1], + parallel_sync => Integer[0], + failover_timeout => Integer[1], + monitor_name => Optional[String], + auth_pass => Optional[String], + notification_script => Optional[Stdlib::Absolutepath], + client_reconfig_script => Optional[Stdlib::Absolutepath], +}] diff --git a/types/sentinelmonitors.pp b/types/sentinelmonitors.pp new file mode 100644 index 00000000..03417ea8 --- /dev/null +++ b/types/sentinelmonitors.pp @@ -0,0 +1,12 @@ +type Redis::SentinelMonitors = Hash[String,Struct[{ + redis_host => Stdlib::Host, + redis_port => Stdlib::Port, + quorum => Integer[1], + down_after => Integer[1], + parallel_sync => Integer[0], + failover_timeout => Integer[1], + monitor_name => Optional[String], + auth_pass => Optional[String], + notification_script => Optional[Stdlib::Absolutepath], + client_reconfig_script => Optional[Stdlib::Absolutepath], +}]]