Skip to content

Commit

Permalink
Add an initscript for sentinel on apt-based systems
Browse files Browse the repository at this point in the history
Until an initscript is available for redis-sentinel in its package[1]
we need to roll our own. This change adds that by creating a templated
initscript with the sentinel config file and service owner and group
as template variables that get filled in.

Testing this revealed some bugs and misfeatures that are also addressed
in this change:

* apt systems want the redis and redis-sentinel processes to daemonize
  (systemd-based) rpm does not. This was not reflected in the params
  defaults, nor reflected in the sentinel configuration template. Now
  it is.

* the package_name for redis-sentinel was not being passed in as a
  parameter, instead it was just using the params.pp default for
  redis server. When [1] is resolve, it will likely mean a different
  package for redis-sentinel so we need there to be a separate parameter
  for the sentinel package that gets installed. For now the name of
  the existing package is used.

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=775414
  • Loading branch information
cdent authored and arioch committed Jan 19, 2015
1 parent 6d0aafd commit adeb407
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 7 deletions.
10 changes: 9 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
$auto_aof_rewrite_percentage = 100
$bind = '127.0.0.1'
$conf_template = 'redis/redis.conf.erb'
$daemonize = true
$databases = 16
$dbfilename = 'dump.rdb'
$extra_config_file = undef
Expand Down Expand Up @@ -47,6 +46,7 @@
$sentinel_quorum = 2
$sentinel_service_name = 'redis-sentinel'
$sentinel_working_dir = '/tmp'
$sentinel_init_template = 'redis/redis-sentinel.init.erb'
$set_max_intset_entries = 512
$slowlog_log_slower_than = 10000
$slowlog_max_len = 1024
Expand Down Expand Up @@ -74,10 +74,14 @@
$config_file_mode = '0644'
$config_group = 'root'
$config_owner = 'root'
$daemonize = true
$package_ensure = 'present'
$package_name = 'redis-server'
$sentinel_config_file = '/etc/redis/redis-sentinel.conf'
$sentinel_config_file_orig = '/etc/redis/redis-sentinel.conf.puppet'
$sentinel_init_script = '/etc/init.d/redis-sentinel'
$sentinel_package_name = 'redis-server'
$sentinel_package_ensure = 'present'
$service_enable = true
$service_ensure = 'running'
$service_group = 'redis'
Expand All @@ -95,10 +99,14 @@
$config_file_mode = '0644'
$config_group = 'root'
$config_owner = 'root'
$daemonize = false
$package_ensure = 'present'
$package_name = 'redis'
$sentinel_config_file = '/etc/redis-sentinel.conf'
$sentinel_config_file_orig = '/etc/redis-sentinel.conf.puppet'
$sentinel_init_script = undef
$sentinel_package_name = 'redis'
$sentinel_package_ensure = 'present'
$service_enable = true
$service_ensure = 'running'
$service_group = 'redis'
Expand Down
39 changes: 35 additions & 4 deletions manifests/sentinel.pp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# = Class: redis::sentinel
#
# This class install redis-sentinel
# This class installs redis-sentinel
#
# == Parameters:
#
Expand Down Expand Up @@ -42,6 +42,11 @@
#
# Default: 180000
#
# [*init_script*]
# Specifiy the init script that will be created for sentinel.
#
# Default: undef on rpm, /etc/init.d/redis-sentinel on apt.
#
# [*log_file*]
# Specify where to write log entries.
#
Expand All @@ -63,6 +68,16 @@
#
# Default: 6379
#
# [*package_name*]
# The name of the package that installs sentinel.
#
# Default: 'redis-server' on apt, 'redis' on rpm
#
# [*package_ensure*]
# Do we ensure this package.
#
# Default: 'present'
#
# [*parallel_sync*]
# How many slaves can be reconfigured at the same time to use a
# new master after a failover.
Expand Down Expand Up @@ -120,10 +135,14 @@
$conf_template = $::redis::params::sentinel_conf_template,
$down_after = $::redis::params::sentinel_down_after,
$failover_timeout = $::redis::params::sentinel_failover_timeout,
$init_script = $::redis::params::sentinel_init_script,
$init_template = $::redis::params::sentinel_init_template,
$log_file = $::redis::params::log_file,
$master_name = $::redis::params::sentinel_master_name,
$redis_host = $::redis::params::bind,
$redis_port = $::redis::params::port,
$package_name = $::redis::params::sentinel_package_name,
$package_ensure = $::redis::params::sentinel_package_ensure,
$parallel_sync = $::redis::params::sentinel_parallel_sync,
$quorum = $::redis::params::sentinel_quorum,
$sentinel_port = $::redis::params::sentinel_port,
Expand All @@ -134,8 +153,8 @@
) inherits redis::params {


ensure_resource('package', $::redis::params::package_name, {
'ensure' => $::redis::params::package_ensure
ensure_resource('package', $package_name, {
'ensure' => $package_ensure
})

file {
Expand All @@ -145,7 +164,7 @@
group => $service_group,
mode => $config_file_mode,
content => template($conf_template),
require => Package[$::redis::params::package_name];
require => Package[$package_name];
}

exec {
Expand All @@ -156,6 +175,18 @@
refreshonly => true;
}

if $init_script {
file {
$init_script:
ensure => present,
owner => 'root',
group => 'root',
mode => '0755',
content => template($init_template),
require => Package[$package_name];
}
}

service { $service_name:
ensure => $::redis::params::service_ensure,
enable => $::redis::params::service_enable,
Expand Down
4 changes: 2 additions & 2 deletions spec/classes/redis_sentinel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$expected_noparams_content = <<EOF
port 26379
dir /tmp
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
Expand All @@ -15,7 +15,7 @@
$expected_params_content = <<EOF
port 26379
dir /tmp
daemonize yes
sentinel monitor cow 127.0.0.1 6379 2
sentinel down-after-milliseconds cow 6000
sentinel parallel-syncs cow 1
Expand Down
1 change: 1 addition & 0 deletions templates/redis-sentinel.conf.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
port <%= @sentinel_port %>
dir <%= @working_dir %>
<% if @daemonize -%>daemonize yes<% else -%>daemonize no<% end -%>

sentinel monitor <%= @master_name %> <%= @redis_host %> <%= @redis_port %> <%= @quorum %>
sentinel down-after-milliseconds <%= @master_name %> <%= @down_after %>
Expand Down
89 changes: 89 additions & 0 deletions templates/redis-sentinel.init.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-sentinel
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# Description: redis-server - Persistent key-value db
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/redis-sentinel
DAEMON_ARGS=<%= @config_file %>
NAME=redis-sentinel
DESC=redis-senitnel

RUNDIR=/var/run/redis
PIDFILE=$RUNDIR/redis-sentinel.pid

test -x $DAEMON || exit 0

if [ -r /etc/default/$NAME ]
then
. /etc/default/$NAME
fi

. /lib/lsb/init-functions

set -e

case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chown <%= @service_user %>:<%= @service_group %> $RUNDIR $PIDFILE
chmod 755 $RUNDIR

if [ -n "$ULIMIT" ]
then
ulimit -n $ULIMIT
fi

if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid <%= @service_user %>:<%= @service_group %> --exec $DAEMON -- $DAEMON_ARGS
then
echo "$NAME."
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
sleep 1
;;

restart|force-reload)
${0} stop
${0} start
;;

status)
echo -n "$DESC is "
if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
then
echo "running"
else
echo "not running"
exit 1
fi
;;

*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac

exit 0

0 comments on commit adeb407

Please sign in to comment.