Add new Redis cookbook
This commit is contained in:
389
cookbooks/redisio/providers/configure.rb
Normal file
389
cookbooks/redisio/providers/configure.rb
Normal file
@@ -0,0 +1,389 @@
|
||||
action :run do
|
||||
configure
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
def configure
|
||||
base_piddir = new_resource.base_piddir
|
||||
|
||||
if !new_resource.version
|
||||
redis_output = Mixlib::ShellOut.new("#{node['redisio']['bin_path']}/redis-server -v")
|
||||
redis_output.run_command
|
||||
redis_output.error!
|
||||
current_version = redis_output.stdout.gsub(/.*v=((\d+\.){2}\d+).*/, '\1').chomp
|
||||
else
|
||||
current_version = new_resource.version
|
||||
end
|
||||
version_hash = RedisioHelper.version_to_hash(current_version)
|
||||
|
||||
# Setup a configuration file and init script for each configuration provided
|
||||
new_resource.servers.each do |current_instance|
|
||||
# Retrieve the default settings hash and the current server setups settings hash.
|
||||
current_instance_hash = current_instance.to_hash
|
||||
current_defaults_hash = new_resource.default_settings.to_hash
|
||||
|
||||
# Merge the configuration defaults with the provided array of configurations provided
|
||||
current = current_defaults_hash.merge(current_instance_hash)
|
||||
|
||||
# Merge in the default maxmemory
|
||||
node_memory_kb = node['memory']['total']
|
||||
# On BSD platforms Ohai reports total memory as a Fixnum
|
||||
|
||||
node_memory_kb = node_memory_kb.sub('kB', '').to_i if node_memory_kb.is_a?(String)
|
||||
|
||||
# Here we determine what the logfile is. It has these possible states
|
||||
#
|
||||
# Redis 2.6 and lower can be
|
||||
# stdout
|
||||
# A path
|
||||
# nil
|
||||
# Redis 2.8 and higher can be
|
||||
# empty string, which means stdout)
|
||||
# A path
|
||||
# nil
|
||||
|
||||
if current['logfile'].nil?
|
||||
log_file = nil
|
||||
log_directory = nil
|
||||
elsif current['logfile'] == 'stdout' || current['logfile'].empty?
|
||||
log_directory = nil
|
||||
log_file = current['logfile']
|
||||
else
|
||||
log_directory = ::File.dirname(current['logfile'])
|
||||
log_file = ::File.basename(current['logfile'])
|
||||
if current['syslogenabled'] == 'yes'
|
||||
Chef::Log.warn("log file is set to #{current['logfile']} but syslogenabled is also set to 'yes'")
|
||||
end
|
||||
end
|
||||
|
||||
maxmemory = current['maxmemory'].to_s
|
||||
if !maxmemory.empty? && maxmemory.include?('%')
|
||||
# Just assume this is sensible like "95%" or "95 %"
|
||||
percent_factor = current['maxmemory'].to_f / 100.0
|
||||
# Ohai reports memory in KB as it looks in /proc/meminfo
|
||||
maxmemory = (node_memory_kb * 1024 * percent_factor / new_resource.servers.length).round.to_s
|
||||
end
|
||||
|
||||
descriptors = if current['ulimit'] == 0
|
||||
current['maxclients'] + 32
|
||||
elsif current['ulimit'] > current['maxclients']
|
||||
current['ulimit']
|
||||
else
|
||||
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"
|
||||
|
||||
# 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
|
||||
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
|
||||
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
|
||||
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(
|
||||
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
|
||||
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
|
||||
93
cookbooks/redisio/providers/install.rb
Normal file
93
cookbooks/redisio/providers/install.rb
Normal file
@@ -0,0 +1,93 @@
|
||||
action :run do
|
||||
# Package install
|
||||
if node['redisio']['package_install']
|
||||
package_resource = package 'redisio_package_name' do
|
||||
package_name node['redisio']['package_name']
|
||||
version node['redisio']['version']
|
||||
action :nothing
|
||||
end
|
||||
|
||||
package_resource.run_action(:install)
|
||||
new_resource.updated_by_last_action(true) if package_resource.updated_by_last_action?
|
||||
|
||||
# freeBSD does not support from source since ports does not support versioning (without a lot of hassle)
|
||||
elsif platform_family?('freebsd')
|
||||
raise 'Source install not supported for freebsd'
|
||||
# Tarball install
|
||||
else
|
||||
@tarball = "#{new_resource.base_name}#{new_resource.version}.#{new_resource.artifact_type}"
|
||||
|
||||
unless current_resource.version == new_resource.version || (redis_exists? && new_resource.safe_install)
|
||||
Chef::Log.info("Installing Redis #{new_resource.version} from source")
|
||||
download
|
||||
unpack
|
||||
build
|
||||
install
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def download
|
||||
Chef::Log.info("Downloading redis tarball from #{new_resource.download_url}")
|
||||
remote_file "#{new_resource.download_dir}/#{@tarball}" do
|
||||
source new_resource.download_url
|
||||
end
|
||||
end
|
||||
|
||||
def unpack
|
||||
install_dir = "#{new_resource.base_name}#{new_resource.version}"
|
||||
case new_resource.artifact_type
|
||||
when 'tar.gz', '.tgz'
|
||||
execute %(cd #{new_resource.download_dir} ; mkdir -p '#{install_dir}' ; tar zxf '#{@tarball}' --strip-components=1 -C '#{install_dir}' --no-same-owner)
|
||||
else
|
||||
raise Chef::Exceptions::UnsupportedAction, "Current package type #{new_resource.artifact_type} is unsupported"
|
||||
end
|
||||
end
|
||||
|
||||
def build
|
||||
execute "cd #{new_resource.download_dir}/#{new_resource.base_name}#{new_resource.version} && make clean && make"
|
||||
end
|
||||
|
||||
def install
|
||||
install_prefix = if new_resource.install_dir
|
||||
"PREFIX=#{new_resource.install_dir}"
|
||||
else
|
||||
''
|
||||
end
|
||||
execute "cd #{new_resource.download_dir}/#{new_resource.base_name}#{new_resource.version} && make #{install_prefix} install"
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
def redis_exists?
|
||||
bin_path = if node['redisio']['install_dir']
|
||||
::File.join(node['redisio']['install_dir'], 'bin')
|
||||
else
|
||||
node['redisio']['bin_path']
|
||||
end
|
||||
redis_server = ::File.join(bin_path, 'redis-server')
|
||||
::File.exist?(redis_server)
|
||||
end
|
||||
|
||||
def version
|
||||
if redis_exists?
|
||||
bin_path = if node['redisio']['install_dir']
|
||||
::File.join(node['redisio']['install_dir'], 'bin')
|
||||
else
|
||||
node['redisio']['bin_path']
|
||||
end
|
||||
redis_server = ::File.join(bin_path, 'redis-server')
|
||||
redis_version = Mixlib::ShellOut.new("#{redis_server} -v")
|
||||
redis_version.run_command
|
||||
version = redis_version.stdout[/version (\d*.\d*.\d*)/, 1] || redis_version.stdout[/v=(\d*.\d*.\d*)/, 1]
|
||||
Chef::Log.info("The Redis server version is: #{version}")
|
||||
return version.delete("\n")
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def load_current_resource
|
||||
@current_resource = Chef::Resource.resource_for_node(:redisio_install, node).new(new_resource.name)
|
||||
@current_resource.version(version)
|
||||
@current_resource
|
||||
end
|
||||
256
cookbooks/redisio/providers/sentinel.rb
Normal file
256
cookbooks/redisio/providers/sentinel.rb
Normal file
@@ -0,0 +1,256 @@
|
||||
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?
|
||||
version
|
||||
else
|
||||
new_resource.version
|
||||
end
|
||||
|
||||
version_hash = RedisioHelper.version_to_hash(current_version)
|
||||
|
||||
# Setup a configuration file and init script for each configuration provided
|
||||
new_resource.sentinels.each do |current_instance|
|
||||
# Retrieve the default settings hash and the current server setups settings hash.
|
||||
current_instance_hash = current_instance.to_hash
|
||||
current_defaults_hash = new_resource.sentinel_defaults.to_hash
|
||||
|
||||
# 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}"
|
||||
|
||||
# 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
|
||||
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
|
||||
|
||||
# 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'
|
||||
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
|
||||
end
|
||||
# servers each loop
|
||||
end
|
||||
|
||||
def redis_exists?
|
||||
bin_path = if node['redisio']['install_dir']
|
||||
::File.join(node['redisio']['install_dir'], 'bin')
|
||||
else
|
||||
node['redisio']['bin_path']
|
||||
end
|
||||
redis_server = ::File.join(bin_path, 'redis-server')
|
||||
::File.exist?(redis_server)
|
||||
end
|
||||
|
||||
def version
|
||||
if redis_exists?
|
||||
bin_path = if node['redisio']['install_dir']
|
||||
::File.join(node['redisio']['install_dir'], 'bin')
|
||||
else
|
||||
node['redisio']['bin_path']
|
||||
end
|
||||
redis_server = ::File.join(bin_path, 'redis-server')
|
||||
redis_version = Mixlib::ShellOut.new("#{redis_server} -v")
|
||||
redis_version.run_command
|
||||
version = redis_version.stdout[/version (\d*.\d*.\d*)/, 1] || redis_version.stdout[/v=(\d*.\d*.\d*)/, 1]
|
||||
Chef::Log.info("The Redis server version is: #{version}")
|
||||
return version.delete("\n")
|
||||
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
|
||||
Reference in New Issue
Block a user