Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sentinel multi monitor #376

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@
}

'RedHat': {
$ppa_repo = undef
$daemonize = false
$config_owner = 'redis'
$config_group = 'root'
$config_dir_mode = '0755'
$log_dir_mode = '0750'

$sentinel_daemonize = false
$sentinel_init_script = undef
$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_init_script = undef
$sentinel_working_dir = '/tmp'
$sentinel_protected_mode = true

$scl = $redis::globals::scl
if $scl {
Expand Down Expand Up @@ -205,4 +205,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,
}
}
115 changes: 50 additions & 65 deletions manifests/sentinel.pp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# @summary Install redis-sentinel
#
# @param auth_pass
# The password to use to authenticate with the master and slaves.
# @param sentinel_monitor
# Specify the sentinel monitor.
#
# @param monitor_defaults
# Override the monitor defaults
#
# @param config_file
# The location and name of the sentinel config file.
Expand All @@ -22,14 +25,6 @@
# @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 init_script
# Specifiy the init script that will be created for sentinel.
#
Expand All @@ -39,16 +34,6 @@
# @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.
#
Expand All @@ -58,17 +43,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
Expand All @@ -92,53 +69,57 @@
# 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_monitor => {
# '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,
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,
Optional[Stdlib::Absolutepath] $init_script = $redis::params::sentinel_init_script,
String[1] $init_template = 'redis/redis-sentinel.init.erb',
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,
String[1] $init_template = 'redis/redis-sentinel.init.erb',
Redis::LogLevel $log_level = 'notice',
Stdlib::Absolutepath $log_file = $redis::params::sentinel_log_file,
Redis::SentinelMonitor $sentinel_monitor = $redis::params::sentinel_default_monitor,
$monitor_defaults = $redis::params::sentinel_monitor_defaults,
basti-nis marked this conversation as resolved.
Show resolved Hide resolved
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'

Expand All @@ -156,6 +137,10 @@
$sentinel_bind_arr = delete_undef_values([$sentinel_bind].flatten)
$supports_protected_mode = $redis::supports_protected_mode

$_monitor = $sentinel_monitor.map |$monitor,$values| {
$redis_values = $monitor_defaults + { 'monitor_name' => $monitor } + $values
}

file { $config_file_orig:
ensure => file,
owner => $service_user,
Expand Down
13 changes: 10 additions & 3 deletions spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_monitor => {
'mymaster' => {
redis_host => '127.0.0.1',
redis_port => 6379,
quorum => 2,
parallel_sync => 1,
down_after => 30000,
failover_timeout => 10000,
},
},
}
EOS

Expand Down
56 changes: 35 additions & 21 deletions spec/classes/redis_sentinel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,23 @@
describe 'with custom parameters' do
let(:params) do
{
auth_pass: 'password',
sentinel_monitor: {
'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

Expand All @@ -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
Expand All @@ -119,15 +126,22 @@
describe 'with array sentinel bind' do
let(:params) do
{
auth_pass: 'password',
sentinel_monitor: {
'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

Expand Down
23 changes: 12 additions & 11 deletions templates/redis-sentinel.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
12 changes: 12 additions & 0 deletions types/sentinelmonitor.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Redis::SentinelMonitor = Hash[String,Struct[{
basti-nis marked this conversation as resolved.
Show resolved Hide resolved
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],
}]]