Set up Redis using the latest version of redisio

This replaces the deprecated redis cookbook. Compiles the latest version
of Redis, currently 7.0.11

Refs #488
This commit is contained in:
Greg Karékinian
2023-06-19 16:02:58 +02:00
parent b164d7a444
commit 6116729907
119 changed files with 2568 additions and 7343 deletions

View File

@@ -1,9 +1,4 @@
action :run do
configure
new_resource.updated_by_last_action(true)
end
def configure
base_piddir = new_resource.base_piddir
if !new_resource.version
@@ -72,318 +67,311 @@ def configure
current['maxclients']
end
recipe_eval do
server_name = current['name'] || current['port']
piddir = "#{base_piddir}/#{server_name}"
aof_file = current['appendfilename'] || "#{current['datadir']}/appendonly-#{server_name}.aof"
rdb_file = current['dbfilename'] || "#{current['datadir']}/dump-#{server_name}.rdb"
server_name = current['name'] || current['port']
piddir = "#{base_piddir}/#{server_name}"
aof_file = current['appendfilename'] || "#{current['datadir']}/appendonly-#{server_name}.aof"
rdb_file = current['dbfilename'] || "#{current['datadir']}/dump-#{server_name}.rdb"
# Create the owner of the redis data directory
user current['user'] do
comment 'Redis service account'
manage_home true
home current['homedir']
shell current['shell']
system current['systemuser']
uid current['uid'] unless current['uid'].nil?
end
# Create the owner of the redis data directory
user current['user'] do
comment 'Redis service account'
manage_home true
home current['homedir']
shell current['shell']
system current['systemuser']
uid current['uid'] unless current['uid'].nil?
end
# Create the redis configuration directory
directory current['configdir'] do
owner 'root'
group platform_family?('freebsd') ? 'wheel' : 'root'
mode '0755'
recursive true
action :create
end
# Create the instance data directory
directory current['datadir'] do
owner current['user']
group current['group']
mode '0775'
recursive true
action :create
end
# Create the pid file directory
directory piddir do
# Create the redis configuration directory
directory current['configdir'] do
owner 'root'
group platform_family?('freebsd') ? 'wheel' : 'redis'
mode '0775'
recursive true
action :create
end
# Create the instance data directory
directory current['datadir'] do
owner current['user']
group current['group']
mode '0775'
recursive true
action :create
end
# Create the pid file directory
directory piddir do
owner current['user']
group current['group']
mode '0755'
recursive true
action :create
end
# Create the log directory if syslog is not being used
if log_directory
directory log_directory do
owner current['user']
group current['group']
mode '0755'
recursive true
action :create
end
# Create the log directory if syslog is not being used
end
# Configure SELinux if it is enabled
extend Chef::Util::Selinux
if selinux_enabled?
selinux_install 'install'
selinux_fcontext "#{current['configdir']}(/.*)?" do
secontext 'redis_conf_t'
end
selinux_fcontext "#{current['datadir']}(/.*)?" do
secontext 'redis_var_lib_t'
end
selinux_fcontext "#{piddir}(/.*)?" do
secontext 'redis_var_run_t'
end
if log_directory
directory log_directory do
owner current['user']
group current['group']
mode '0755'
recursive true
action :create
selinux_fcontext "#{log_directory}(/.*)?" do
secontext 'redis_log_t'
end
end
# Configure SELinux if it is enabled
extend Chef::Util::Selinux
if selinux_enabled?
selinux_policy_install 'install'
selinux_policy_fcontext "#{current['configdir']}(/.*)?" do
secontext 'redis_conf_t'
end
selinux_policy_fcontext "#{current['datadir']}(/.*)?" do
secontext 'redis_var_lib_t'
end
selinux_policy_fcontext "#{piddir}(/.*)?" do
secontext 'redis_var_run_t'
end
if log_directory
selinux_policy_fcontext "#{log_directory}(/.*)?" do
secontext 'redis_log_t'
end
end
end
# Create the log file if syslog is not being used
if log_file
file current['logfile'] do
owner current['user']
group current['group']
mode '0644'
backup false
action :touch
# in version 2.8 or higher the empty string is used instead of stdout
only_if { !log_file.empty? && log_file != 'stdout' }
end
end
# Set proper permissions on the AOF or RDB files
file aof_file do
end
# Create the log file if syslog is not being used
if log_file
file current['logfile'] do
owner current['user']
group current['group']
mode '0644'
only_if { current['backuptype'] == 'aof' || current['backuptype'] == 'both' }
only_if { ::File.exist?(aof_file) }
end
file rdb_file do
owner current['user']
group current['group']
mode '0644'
only_if { current['backuptype'] == 'rdb' || current['backuptype'] == 'both' }
only_if { ::File.exist?(rdb_file) }
end
# Setup the redis users descriptor limits
# Pending response on https://github.com/brianbianco/redisio/commit/4ee9aad3b53029cc3b6c6cf741f5126755e712cd#diff-8ae42a59a6f4e8dc5b4e6dd2d6a34eab
# TODO: ulimit cookbook v0.1.2 doesn't work with freeBSD
if current['ulimit'] && !platform_family?('freebsd')
user_ulimit current['user'] do
filehandle_limit descriptors
end
end
computed_save = current['save']
if current['save'] && current['save'].respond_to?(:each_line)
computed_save = current['save'].each_line
Chef::Log.warn("#{server_name}: given a save argument as a string, instead of an array.")
Chef::Log.warn("#{server_name}: This will be deprecated in future versions of the redisio cookbook.")
end
# Load password for use with requirepass from data bag if needed
if current['data_bag_name'] && current['data_bag_item'] && current['data_bag_key']
bag = data_bag_item(current['data_bag_name'], current['data_bag_item'])
current['requirepass'] = bag[current['data_bag_key']]
current['masterauth'] = bag[current['data_bag_key']]
end
# Lay down the configuration files for the current instance
template "#{current['configdir']}/#{server_name}.conf" do
source node['redisio']['redis_config']['template_source']
cookbook node['redisio']['redis_config']['template_cookbook']
owner current['user']
group current['group']
mode current['permissions']
backup false
action :create
# in version 2.8 or higher the empty string is used instead of stdout
only_if { !log_file.empty? && log_file != 'stdout' }
end
end
# Set proper permissions on the AOF or RDB files
file aof_file do
owner current['user']
group current['group']
mode '0644'
only_if { current['backuptype'] == 'aof' || current['backuptype'] == 'both' }
only_if { ::File.exist?(aof_file) }
end
file rdb_file do
owner current['user']
group current['group']
mode '0644'
only_if { current['backuptype'] == 'rdb' || current['backuptype'] == 'both' }
only_if { ::File.exist?(rdb_file) }
end
# Setup the redis users descriptor limits
# Pending response on https://github.com/brianbianco/redisio/commit/4ee9aad3b53029cc3b6c6cf741f5126755e712cd#diff-8ae42a59a6f4e8dc5b4e6dd2d6a34eab
# TODO: ulimit cookbook v0.1.2 doesn't work with freeBSD
if current['ulimit'] && !platform_family?('freebsd')
user_ulimit current['user'] do
filehandle_limit descriptors
end
end
computed_save = current['save']
if current['save'] && current['save'].respond_to?(:each_line)
computed_save = current['save'].each_line
Chef::Log.warn("#{server_name}: given a save argument as a string, instead of an array.")
Chef::Log.warn("#{server_name}: This will be deprecated in future versions of the redisio cookbook.")
end
# Load password for use with requirepass from data bag if needed
if current['data_bag_name'] && current['data_bag_item'] && current['data_bag_key']
bag = data_bag_item(current['data_bag_name'], current['data_bag_item'])
current['requirepass'] = bag[current['data_bag_key']]
current['masterauth'] = bag[current['data_bag_key']]
end
# Lay down the configuration files for the current instance
template "#{current['configdir']}/#{server_name}.conf" do
source node['redisio']['redis_config']['template_source']
cookbook node['redisio']['redis_config']['template_cookbook']
owner current['user']
group current['group']
mode current['permissions']
action :create
variables(
version: version_hash,
piddir: piddir,
name: server_name,
job_control: node['redisio']['job_control'],
port: current['port'],
tcpbacklog: current['tcpbacklog'],
address: current['address'],
databases: current['databases'],
backuptype: current['backuptype'],
datadir: current['datadir'],
unixsocket: current['unixsocket'],
unixsocketperm: current['unixsocketperm'],
timeout: current['timeout'],
keepalive: current['keepalive'],
loglevel: current['loglevel'],
logfile: current['logfile'],
syslogenabled: current['syslogenabled'],
syslogfacility: current['syslogfacility'],
save: computed_save,
stopwritesonbgsaveerror: current['stopwritesonbgsaveerror'],
rdbcompression: current['rdbcompression'],
rdbchecksum: current['rdbchecksum'],
dbfilename: current['dbfilename'],
slaveof: current['slaveof'],
protected_mode: current['protected_mode'],
masterauth: current['masterauth'],
slaveservestaledata: current['slaveservestaledata'],
slavereadonly: current['slavereadonly'],
replpingslaveperiod: current['replpingslaveperiod'],
repltimeout: current['repltimeout'],
repldisabletcpnodelay: current['repldisabletcpnodelay'],
replbacklogsize: current['replbacklogsize'],
replbacklogttl: current['replbacklogttl'],
slavepriority: current['slavepriority'],
requirepass: current['requirepass'],
rename_commands: current['rename_commands'],
maxclients: current['maxclients'],
maxmemory: maxmemory,
maxmemorypolicy: current['maxmemorypolicy'],
maxmemorysamples: current['maxmemorysamples'],
appendfilename: current['appendfilename'],
appendfsync: current['appendfsync'],
noappendfsynconrewrite: current['noappendfsynconrewrite'],
aofrewritepercentage: current['aofrewritepercentage'],
aofrewriteminsize: current['aofrewriteminsize'],
aofloadtruncated: current['aofloadtruncated'],
luatimelimit: current['luatimelimit'],
slowloglogslowerthan: current['slowloglogslowerthan'],
slowlogmaxlen: current['slowlogmaxlen'],
notifykeyspaceevents: current['notifykeyspaceevents'],
hashmaxziplistentries: current['hashmaxziplistentries'],
hashmaxziplistvalue: current['hashmaxziplistvalue'],
listmaxziplistentries: current['listmaxziplistentries'],
listmaxziplistvalue: current['listmaxziplistvalue'],
setmaxintsetentries: current['setmaxintsetentries'],
zsetmaxziplistentries: current['zsetmaxziplistentries'],
zsetmaxziplistvalue: current['zsetmaxziplistvalue'],
hllsparsemaxbytes: current['hllsparsemaxbytes'],
activerehasing: current['activerehasing'],
clientoutputbufferlimit: current['clientoutputbufferlimit'],
hz: current['hz'],
aofrewriteincrementalfsync: current['aofrewriteincrementalfsync'],
clusterenabled: current['clusterenabled'],
clusterconfigfile: current['clusterconfigfile'],
clusternodetimeout: current['clusternodetimeout'],
includes: current['includes'],
minslavestowrite: current['minslavestowrite'],
minslavesmaxlag: current['minslavesmaxlag'],
repldisklesssync: current['repldisklesssync'],
repldisklesssyncdelay: current['repldisklesssyncdelay']
)
not_if { ::File.exist?("#{current['configdir']}/#{server_name}.conf.breadcrumb") }
end
file "#{current['configdir']}/#{server_name}.conf.breadcrumb" do
content 'This file prevents the chef cookbook from overwritting the redis config more than once'
action :create_if_missing
only_if { current['breadcrumb'] == true }
end
# Setup init.d file
bin_path = if node['redisio']['install_dir']
::File.join(node['redisio']['install_dir'], 'bin')
else
node['redisio']['bin_path']
end
case node['redisio']['job_control']
when 'initd'
template "/etc/init.d/redis#{server_name}" do
source 'redis.init.erb'
cookbook 'redisio'
owner 'root'
group 'root'
mode '0755'
variables(
version: version_hash,
piddir: piddir,
name: server_name,
job_control: node['redisio']['job_control'],
port: current['port'],
tcpbacklog: current['tcpbacklog'],
address: current['address'],
databases: current['databases'],
backuptype: current['backuptype'],
datadir: current['datadir'],
unixsocket: current['unixsocket'],
unixsocketperm: current['unixsocketperm'],
timeout: current['timeout'],
keepalive: current['keepalive'],
loglevel: current['loglevel'],
logfile: current['logfile'],
syslogenabled: current['syslogenabled'],
syslogfacility: current['syslogfacility'],
save: computed_save,
stopwritesonbgsaveerror: current['stopwritesonbgsaveerror'],
rdbcompression: current['rdbcompression'],
rdbchecksum: current['rdbchecksum'],
dbfilename: current['dbfilename'],
slaveof: current['slaveof'],
protected_mode: current['protected_mode'],
masterauth: current['masterauth'],
slaveservestaledata: current['slaveservestaledata'],
slavereadonly: current['slavereadonly'],
replpingslaveperiod: current['replpingslaveperiod'],
repltimeout: current['repltimeout'],
repldisabletcpnodelay: current['repldisabletcpnodelay'],
replbacklogsize: current['replbacklogsize'],
replbacklogttl: current['replbacklogttl'],
slavepriority: current['slavepriority'],
requirepass: current['requirepass'],
rename_commands: current['rename_commands'],
maxclients: current['maxclients'],
maxmemory: maxmemory,
maxmemorypolicy: current['maxmemorypolicy'],
maxmemorysamples: current['maxmemorysamples'],
appendfilename: current['appendfilename'],
appendfsync: current['appendfsync'],
noappendfsynconrewrite: current['noappendfsynconrewrite'],
aofrewritepercentage: current['aofrewritepercentage'],
aofrewriteminsize: current['aofrewriteminsize'],
aofloadtruncated: current['aofloadtruncated'],
luatimelimit: current['luatimelimit'],
slowloglogslowerthan: current['slowloglogslowerthan'],
slowlogmaxlen: current['slowlogmaxlen'],
notifykeyspaceevents: current['notifykeyspaceevents'],
hashmaxziplistentries: current['hashmaxziplistentries'],
hashmaxziplistvalue: current['hashmaxziplistvalue'],
listmaxziplistentries: current['listmaxziplistentries'],
listmaxziplistvalue: current['listmaxziplistvalue'],
setmaxintsetentries: current['setmaxintsetentries'],
zsetmaxziplistentries: current['zsetmaxziplistentries'],
zsetmaxziplistvalue: current['zsetmaxziplistvalue'],
hllsparsemaxbytes: current['hllsparsemaxbytes'],
activerehasing: current['activerehasing'],
clientoutputbufferlimit: current['clientoutputbufferlimit'],
hz: current['hz'],
aofrewriteincrementalfsync: current['aofrewriteincrementalfsync'],
clusterenabled: current['clusterenabled'],
clusterconfigfile: current['clusterconfigfile'],
clusternodetimeout: current['clusternodetimeout'],
includes: current['includes'],
minslavestowrite: current['minslavestowrite'],
minslavesmaxlag: current['minslavesmaxlag'],
repldisklesssync: current['repldisklesssync'],
repldisklesssyncdelay: current['repldisklesssyncdelay']
name: server_name,
bin_path: bin_path,
port: current['port'],
address: current['address'],
user: current['user'],
configdir: current['configdir'],
piddir: piddir,
requirepass: current['requirepass'],
shutdown_save: current['shutdown_save'],
platform: node['platform'],
unixsocket: current['unixsocket'],
ulimit: descriptors,
required_start: node['redisio']['init.d']['required_start'].join(' '),
required_stop: node['redisio']['init.d']['required_stop'].join(' ')
)
not_if { ::File.exist?("#{current['configdir']}/#{server_name}.conf.breadcrumb") }
end
when 'upstart'
template "/etc/init/redis#{server_name}.conf" do
source 'redis.upstart.conf.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0644'
variables(
name: server_name,
bin_path: bin_path,
port: current['port'],
user: current['user'],
group: current['group'],
configdir: current['configdir'],
piddir: piddir
)
end
when 'rcinit'
template "/usr/local/etc/rc.d/redis#{server_name}" do
source 'redis.rcinit.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0755'
variables(
name: server_name,
bin_path: bin_path,
user: current['user'],
configdir: current['configdir'],
piddir: piddir
)
end
when 'systemd'
service_name = "redis@#{server_name}"
reload_name = "#{service_name} systemd reload"
file "/etc/tmpfiles.d/#{service_name}.conf" do
content "d #{piddir} 0755 #{current['user']} #{current['group']}\n"
owner 'root'
group 'root'
mode '0644'
end
file "#{current['configdir']}/#{server_name}.conf.breadcrumb" do
content 'This file prevents the chef cookbook from overwritting the redis config more than once'
action :create_if_missing
only_if { current['breadcrumb'] == true }
execute reload_name do
command 'systemctl daemon-reload'
action :nothing
end
# Setup init.d file
bin_path = if node['redisio']['install_dir']
::File.join(node['redisio']['install_dir'], 'bin')
else
node['redisio']['bin_path']
end
case node['redisio']['job_control']
when 'initd'
template "/etc/init.d/redis#{server_name}" do
source 'redis.init.erb'
cookbook 'redisio'
owner 'root'
group 'root'
mode '0755'
variables(
name: server_name,
bin_path: bin_path,
port: current['port'],
address: current['address'],
user: current['user'],
configdir: current['configdir'],
piddir: piddir,
requirepass: current['requirepass'],
shutdown_save: current['shutdown_save'],
platform: node['platform'],
unixsocket: current['unixsocket'],
ulimit: descriptors,
required_start: node['redisio']['init.d']['required_start'].join(' '),
required_stop: node['redisio']['init.d']['required_stop'].join(' ')
)
end
when 'upstart'
template "/etc/init/redis#{server_name}.conf" do
source 'redis.upstart.conf.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0644'
variables(
name: server_name,
bin_path: bin_path,
port: current['port'],
user: current['user'],
group: current['group'],
configdir: current['configdir'],
piddir: piddir
)
end
when 'rcinit'
template "/usr/local/etc/rc.d/redis#{server_name}" do
source 'redis.rcinit.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0755'
variables(
name: server_name,
bin_path: bin_path,
user: current['user'],
configdir: current['configdir'],
piddir: piddir
)
end
when 'systemd'
service_name = "redis@#{server_name}"
reload_name = "#{service_name} systemd reload"
file "/etc/tmpfiles.d/#{service_name}.conf" do
content "d #{piddir} 0755 #{current['user']} #{current['group']}\n"
owner 'root'
group 'root'
mode '0644'
end
execute reload_name do
command 'systemctl daemon-reload'
action :nothing
end
template "/lib/systemd/system/#{service_name}.service" do
source 'redis@.service.erb'
cookbook 'redisio'
owner 'root'
group 'root'
mode '0644'
variables(
bin_path: bin_path,
user: current['user'],
group: current['group'],
limit_nofile: descriptors
)
notifies :run, "execute[#{reload_name}]", :immediately
end
template "/lib/systemd/system/#{service_name}.service" do
source 'redis@.service.erb'
cookbook 'redisio'
owner 'root'
group 'root'
mode '0644'
variables(
bin_path: bin_path,
user: current['user'],
group: current['group'],
limit_nofile: descriptors
)
notifies :run, "execute[#{reload_name}]", :immediately
end
end
end
# servers each loop
end
def load_current_resource
@current_resource = Chef::Resource.resource_for_node(:redisio_configure, node).new(new_resource.name)
@current_resource
end

View File

@@ -1,9 +1,4 @@
action :run do
configure
new_resource.updated_by_last_action(true)
end
def configure
base_piddir = new_resource.base_piddir
current_version = if new_resource.version.nil?
@@ -23,200 +18,199 @@ def configure
# Merge the configuration defaults with the provided array of configurations provided
current = current_defaults_hash.merge(current_instance_hash)
recipe_eval do
sentinel_name = current['name'] || current['port']
sentinel_name = "sentinel_#{sentinel_name}"
piddir = "#{base_piddir}/#{sentinel_name}"
sentinel_name = current['name'] || current['port']
sentinel_name = "sentinel_#{sentinel_name}"
piddir = "#{base_piddir}/#{sentinel_name}"
# Create the owner of the redis data directory
user current['user'] do
comment 'Redis service account'
manage_home true
home current['homedir']
shell current['shell']
system current['systemuser']
uid current['uid'] unless current['uid'].nil?
end
# Create the owner of the redis data directory
user current['user'] do
comment 'Redis service account'
manage_home true
home current['homedir']
shell current['shell']
system current['systemuser']
uid current['uid'] unless current['uid'].nil?
end
# Create the redis configuration directory
directory current['configdir'] do
owner 'root'
group platform_family?('freebsd') ? 'wheel' : 'root'
mode '0755'
recursive true
action :create
end
# Create the pid file directory
directory piddir do
# Create the redis configuration directory
directory current['configdir'] do
owner 'root'
group platform_family?('freebsd') ? 'wheel' : 'redis'
mode '0775'
recursive true
action :create
end
# Create the pid file directory
directory piddir do
owner current['user']
group current['group']
mode '0755'
recursive true
action :create
end
unless current['logfile'].nil?
# Create the log directory if syslog is not being used
directory ::File.dirname(current['logfile']) do
owner current['user']
group current['group']
mode '0755'
recursive true
action :create
only_if { current['syslogenabled'] != 'yes' && current['logfile'] && current['logfile'] != 'stdout' }
end
unless current['logfile'].nil?
# Create the log directory if syslog is not being used
directory ::File.dirname(current['logfile']) do
owner current['user']
group current['group']
mode '0755'
recursive true
action :create
only_if { current['syslogenabled'] != 'yes' && current['logfile'] && current['logfile'] != 'stdout' }
end
# Create the log file is syslog is not being used
file current['logfile'] do
owner current['user']
group current['group']
mode '0644'
backup false
action :touch
only_if { current['logfile'] && current['logfile'] != 'stdout' }
end
end
# <%=@name%> <%=@masterip%> <%=@masterport%> <%= @quorum_count %>
# <%= "sentinel auth-pass #{@name} #{@authpass}" unless @authpass.nil? %>
# sentinel down-after-milliseconds <%=@name%> <%=@downaftermil%>
# sentinel parallel-syncs <%=@name%> <%=@parallelsyncs%>
# sentinel failover-timeout <%=@name%> <%=@failovertimeout%>
# convert from old format (preserve compat)
if !current['masters'] && current['master_ip']
Chef::Log.warn('You are using a deprecated sentinel format. This will be removed in future versions.')
# use old key names if newer key names aren't present (e.g. 'foo' || :foo)
masters = [
{
master_name: current['master_name'] || current[:mastername],
master_ip: current['master_ip'] || current[:masterip],
master_port: current['master_port'] || current[:masterport],
quorum_count: current['quorum_count'] || current[:quorum_count],
auth_pass: current['auth-pass'] || current[:authpass],
down_after_milliseconds: current['down-after-milliseconds'] || current[:downaftermil],
parallel_syncs: current['parallel-syncs'] || current[:parallelsyncs],
failover_timeout: current['failover-timeout'] || current[:failovertimeout],
},
]
else
masters = [current['masters']].flatten
end
# Load password for use with requirepass from data bag if needed
if current['data_bag_name'] && current['data_bag_item'] && current['data_bag_key']
bag = data_bag_item(current['data_bag_name'], current['data_bag_item'])
masters.each do |master|
master['auth_pass'] = bag[current['data_bag_key']]
end
end
# merge in default values to each sentinel hash
masters_with_defaults = []
masters.each do |current_sentinel_master|
default_sentinel_master = new_resource.sentinel_defaults.to_hash
sentinel_master = default_sentinel_master.merge(current_sentinel_master || {})
masters_with_defaults << sentinel_master
end
# Don't render a template if we're missing these from any sentinel,
# as these are the minimal settings required to be passed in
masters_with_defaults.each do |sentinel_instance|
%w(master_ip master_port quorum_count).each do |param|
raise "Missing required sentinel parameter #{param} for #{sentinel_instance}" unless sentinel_instance[param]
end
end
# Lay down the configuration files for the current instance
template "#{current['configdir']}/#{sentinel_name}.conf" do
source 'sentinel.conf.erb'
cookbook 'redisio'
# Create the log file is syslog is not being used
file current['logfile'] do
owner current['user']
group current['group']
mode '0644'
action :create
variables(
name: current['name'],
piddir: piddir,
version: version_hash,
job_control: node['redisio']['job_control'],
sentinel_bind: current['sentinel_bind'],
sentinel_port: current['sentinel_port'],
loglevel: current['loglevel'],
logfile: current['logfile'],
syslogenabled: current['syslogenabled'],
syslogfacility: current['syslogfacility'],
masters: masters_with_defaults,
announce_ip: current['announce-ip'],
announce_port: current['announce-port'],
notification_script: current['notification-script'],
client_reconfig_script: current['client-reconfig-script']
)
not_if { ::File.exist?("#{current['configdir']}/#{sentinel_name}.conf.breadcrumb") }
backup false
action :touch
only_if { current['logfile'] && current['logfile'] != 'stdout' }
end
end
file "#{current['configdir']}/#{sentinel_name}.conf.breadcrumb" do
content 'This file prevents the chef cookbook from overwritting the sentinel config more than once'
action :create_if_missing
end
# <%=@name%> <%=@masterip%> <%=@masterport%> <%= @quorum_count %>
# <%= "sentinel auth-pass #{@name} #{@authpass}" unless @authpass.nil? %>
# sentinel down-after-milliseconds <%=@name%> <%=@downaftermil%>
# sentinel parallel-syncs <%=@name%> <%=@parallelsyncs%>
# sentinel failover-timeout <%=@name%> <%=@failovertimeout%>
# Setup init.d file
bin_path = if node['redisio']['install_dir']
::File.join(node['redisio']['install_dir'], 'bin')
else
node['redisio']['bin_path']
end
template "/etc/init.d/redis_#{sentinel_name}" do
source 'sentinel.init.erb'
cookbook 'redisio'
owner 'root'
group 'root'
mode '0755'
variables(
name: sentinel_name,
bin_path: bin_path,
user: current['user'],
configdir: current['configdir'],
piddir: piddir,
platform: node['platform']
)
only_if { node['redisio']['job_control'] == 'initd' }
end
# convert from old format (preserve compat)
if !current['masters'] && current['master_ip']
Chef::Log.warn('You are using a deprecated sentinel format. This will be removed in future versions.')
template "/etc/init/redis_#{sentinel_name}.conf" do
source 'sentinel.upstart.conf.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0644'
variables(
name: sentinel_name,
bin_path: bin_path,
user: current['user'],
group: current['group'],
configdir: current['configdir'],
piddir: piddir
)
only_if { node['redisio']['job_control'] == 'upstart' }
# use old key names if newer key names aren't present (e.g. 'foo' || :foo)
masters = [
{
master_name: current['master_name'] || current[:mastername],
master_ip: current['master_ip'] || current[:masterip],
master_port: current['master_port'] || current[:masterport],
quorum_count: current['quorum_count'] || current[:quorum_count],
auth_pass: current['auth-pass'] || current[:authpass],
down_after_milliseconds: current['down-after-milliseconds'] || current[:downaftermil],
parallel_syncs: current['parallel-syncs'] || current[:parallelsyncs],
failover_timeout: current['failover-timeout'] || current[:failovertimeout],
},
]
else
masters = [current['masters']].flatten
end
# Load password for use with requirepass from data bag if needed
if current['data_bag_name'] && current['data_bag_item'] && current['data_bag_key']
bag = data_bag_item(current['data_bag_name'], current['data_bag_item'])
masters.each do |master|
master['auth_pass'] = bag[current['data_bag_key']]
end
# TODO: fix for freebsd
template "/usr/local/etc/rc.d/redis_#{sentinel_name}" do
source 'sentinel.rcinit.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0755'
variables(
name: sentinel_name,
bin_path: bin_path,
user: current['user'],
configdir: current['configdir'],
piddir: piddir
)
only_if { node['redisio']['job_control'] == 'rcinit' }
end
# merge in default values to each sentinel hash
masters_with_defaults = []
masters.each do |current_sentinel_master|
default_sentinel_master = new_resource.sentinel_defaults.to_hash
sentinel_master = default_sentinel_master.merge(current_sentinel_master || {})
masters_with_defaults << sentinel_master
end
# Don't render a template if we're missing these from any sentinel,
# as these are the minimal settings required to be passed in
masters_with_defaults.each do |sentinel_instance|
%w(master_ip master_port quorum_count).each do |param|
raise "Missing required sentinel parameter #{param} for #{sentinel_instance}" unless sentinel_instance[param]
end
end
# Lay down the configuration files for the current instance
template "#{current['configdir']}/#{sentinel_name}.conf" do
source 'sentinel.conf.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0644'
action :create
variables(
name: current['name'],
piddir: piddir,
version: version_hash,
job_control: node['redisio']['job_control'],
sentinel_bind: current['sentinel_bind'],
sentinel_port: current['sentinel_port'],
loglevel: current['loglevel'],
logfile: current['logfile'],
syslogenabled: current['syslogenabled'],
syslogfacility: current['syslogfacility'],
masters: masters_with_defaults,
announce_ip: current['announce-ip'],
announce_port: current['announce-port'],
notification_script: current['notification-script'],
client_reconfig_script: current['client-reconfig-script'],
protected_mode: current['protected_mode']
)
not_if { ::File.exist?("#{current['configdir']}/#{sentinel_name}.conf.breadcrumb") }
end
file "#{current['configdir']}/#{sentinel_name}.conf.breadcrumb" do
content 'This file prevents the chef cookbook from overwritting the sentinel config more than once'
action :create_if_missing
end
# Setup init.d file
bin_path = if node['redisio']['install_dir']
::File.join(node['redisio']['install_dir'], 'bin')
else
node['redisio']['bin_path']
end
template "/etc/init.d/redis_#{sentinel_name}" do
source 'sentinel.init.erb'
cookbook 'redisio'
owner 'root'
group 'root'
mode '0755'
variables(
name: sentinel_name,
bin_path: bin_path,
user: current['user'],
configdir: current['configdir'],
piddir: piddir,
platform: node['platform']
)
only_if { node['redisio']['job_control'] == 'initd' }
end
template "/etc/init/redis_#{sentinel_name}.conf" do
source 'sentinel.upstart.conf.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0644'
variables(
name: sentinel_name,
bin_path: bin_path,
user: current['user'],
group: current['group'],
configdir: current['configdir'],
piddir: piddir
)
only_if { node['redisio']['job_control'] == 'upstart' }
end
# TODO: fix for freebsd
template "/usr/local/etc/rc.d/redis_#{sentinel_name}" do
source 'sentinel.rcinit.erb'
cookbook 'redisio'
owner current['user']
group current['group']
mode '0755'
variables(
name: sentinel_name,
bin_path: bin_path,
user: current['user'],
configdir: current['configdir'],
piddir: piddir
)
only_if { node['redisio']['job_control'] == 'rcinit' }
end
end
# servers each loop
end
@@ -247,9 +241,3 @@ def version
end
nil
end
def load_current_resource
@current_resource = Chef::Resource.resource_for_node(:redisio_sentinel, node).new(new_resource.name)
@current_resource.version(version)
@current_resource
end