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