Skip to content

Commit

Permalink
Add CentOS 7 support with systemd configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Pellittieri authored and martinb3 committed Jun 24, 2015
1 parent 601e095 commit 7146921
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ platforms:
run_list:
- recipe[yum-epel::default]
- recipe[yum-remi::default]
- name: centos-7.1
run_list:
- recipe[yum-epel::default]
- name: fedora-20

suites:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Tested on:
* Ubuntu 12.04
* Debian 6.0.8
* Fedora 20
* Centos 6.4
* Centos 6.6
* Centos 7.1

Usage
=====
Expand Down
8 changes: 6 additions & 2 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@
# Custom installation directory
default['redisio']['install_dir'] = nil

# Job control related options (initd or upstart)
default['redisio']['job_control'] = 'initd'
# Job control related options (initd, upstart, or systemd)
if node['platform_family'] == 'rhel' && Gem::Version.new(node['platform_version']) > Gem::Version.new('7.0.0')
default['redisio']['job_control'] = 'systemd'
else
default['redisio']['job_control'] = 'initd'
end

# Init.d script related options
default['redisio']['init.d']['required_start'] = []
Expand Down
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
license 'Apache 2.0'
description 'Installs/Configures redis'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '2.3.1'
version '2.4.0'
%w[ debian ubuntu centos redhat fedora scientific suse amazon].each do |os|
supports os
end
Expand Down
18 changes: 15 additions & 3 deletions recipes/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,37 @@
base_piddir redis['base_piddir']
end

template '/usr/lib/systemd/system/[email protected]' do
source '[email protected]'
variables({ :bin_path => node['redisio']['bin_path'] })
only_if { node['redisio']['job_control'] == 'systemd' }
end

# Create a service resource for each redis instance, named for the port it runs on.
redis_instances.each do |current_server|
server_name = current_server['name'] || current_server['port']
job_control = node['redisio']['job_control']

if job_control == 'initd'
case node['redisio']['job_control']
when 'initd'
service "redis#{server_name}" do
# don't supply start/stop/restart commands, Chef::Provider::Service::*
# do a fine job on it's own, and support systemd correctly
supports :start => true, :stop => true, :restart => false, :status => true
end
elsif job_control == 'upstart'
when 'upstart'
service "redis#{server_name}" do
provider Chef::Provider::Service::Upstart
provider Chef::Provider::Service::Upstart
start_command "start redis#{server_name}"
stop_command "stop redis#{server_name}"
restart_command "restart redis#{server_name}"
supports :start => true, :stop => true, :restart => true, :status => false
end
when 'systemd'
service "redis@#{server_name}" do
provider Chef::Provider::Service::Systemd
supports :start => true, :stop => true, :restart => true, :status => true
end
else
Chef::Log.error("Unknown job control type, no service resource created!")
end
Expand Down
22 changes: 20 additions & 2 deletions recipes/enable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,28 @@

redis = node['redisio']

execute 'reload-systemd' do
command '/usr/bin/systemctl daemon-reload'
only_if { node['redisio']['job_control'] == 'systemd'}
action :nothing
end

redis['servers'].each do |current_server|
server_name = current_server["name"] || current_server["port"]
resource = resources("service[redis#{server_name}]")
resource_name = if node['redisio']['job_control'] == 'systemd'
"service[redis@#{server_name}]"
else
"service[redis#{server_name}]"
end
resource = resources(resource_name)
resource.action Array(resource.action)
resource.action << :start
resource.action << :enable
if node['redisio']['job_control'] != 'systemd'
resource.action << :enable
else
link "/etc/systemd/system/multi-user.target.wants/redis@#{server_name}.service" do
to '/usr/lib/systemd/system/[email protected]'
notifies :run, 'execute[reload-systemd]', :immediately
end
end
end
17 changes: 14 additions & 3 deletions recipes/sentinel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,36 @@
base_piddir redis['base_piddir']
end

template '/usr/lib/systemd/system/[email protected]' do
source '[email protected]'
variables({ :bin_path => node['redisio']['bin_path'] })
only_if { node['redisio']['job_control'] == 'systemd' }
end

# Create a service resource for each sentinel instance, named for the port it runs on.
sentinel_instances.each do |current_sentinel|
sentinel_name = current_sentinel['name']
job_control = node['redisio']['job_control']

if job_control == 'initd'
case node['redisio']['job_control']
when 'initd'
service "redis_sentinel_#{sentinel_name}" do
# don't supply start/stop/restart commands, Chef::Provider::Service::*
# do a fine job on it's own, and support systemd correctly
supports :start => true, :stop => true, :restart => true, :status => false
end
elsif job_control == 'upstart'
when 'upstart'
service "redis_sentinel_#{sentinel_name}" do
provider Chef::Provider::Service::Upstart
start_command "start redis_sentinel_#{sentinel_name}"
stop_command "stop redis_sentinel_#{sentinel_name}"
restart_command "restart redis_sentinel_#{sentinel_name}"
supports :start => true, :stop => true, :restart => true, :status => false
end
when 'systemd'
service "redis-sentinel@#{sentinel_name}" do
provider Chef::Provider::Service::Systemd
supports :start => true, :stop => true, :restart => true, :status => true
end
else
Chef::Log.error("Unknown job control type, no service resource created!")
end
Expand Down
22 changes: 20 additions & 2 deletions recipes/sentinel_enable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,28 @@
sentinel_instances = [{'sentinel_port' => '26379', 'name' => 'mycluster', 'master_ip' => '127.0.0.1', 'master_port' => '6379'}]
end

execute 'reload-systemd' do
command '/usr/bin/systemctl daemon-reload'
only_if { node['redisio']['job_control'] == 'systemd'}
action :nothing
end

sentinel_instances.each do |current_sentinel|
sentinel_name = current_sentinel['name']
resource = resources("service[redis_sentinel_#{sentinel_name}]")
resource_name = if node['redisio']['job_control'] == 'systemd'
"service[redis-sentinel@#{sentinel_name}]"
else
"service[redis_sentinel_#{sentinel_name}]"
end
resource = resources(resource_name)
resource.action Array(resource.action)
resource.action << :start
resource.action << :enable
if node['redisio']['job_control'] != 'systemd'
resource.action << :enable
else
link "/etc/systemd/system/multi-user.target.wants/redis@#{sentinel_name}.service" do
to '/usr/lib/systemd/system/[email protected]'
notifies :run, 'execute[reload-systemd]', :immediately
end
end
end
11 changes: 11 additions & 0 deletions templates/default/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=<%= @bin_path %>/redis-server /etc/redis/sentinel_%i.conf --sentinel --daemonize no
User=redis
Group=redis

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion templates/default/redis.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
<% unless @job_control == 'upstart' %>
<% if @job_control == 'initd' %>
daemonize yes
<% end %>

Expand Down
12 changes: 12 additions & 0 deletions templates/default/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=<%= @bin_path %>/redis-server /etc/redis/%i.conf --daemonize no
ExecStop=/usr/bin/redis-shutdown
User=redis
Group=redis

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion templates/default/sentinel.conf.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Example sentinel.conf

# redisio Cookbook additions
<% unless @job_control == 'upstart' %>
<% if @job_control == 'initd' %>
daemonize yes
<% end %>
pidfile <%= @piddir %>/sentinel_<%=@name%>.pid
Expand Down
7 changes: 6 additions & 1 deletion test/integration/helpers/serverspec/redisio_examples.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
shared_examples_for 'redis on port' do |redis_port, args|
it 'enables the redis service' do
expect(service "redis#{redis_port}").to be_enabled
service_name = if os[:family] == 'redhat' and os[:release][0] == '7'
"redis@#{redis_port}"
else
"redis#{redis_port}"
end
expect(service service_name).to be_enabled
end

context 'starts the redis service' do
Expand Down
9 changes: 7 additions & 2 deletions test/integration/helpers/serverspec/sentinel_examples.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
shared_examples_for 'sentinel on port' do |redis_port, redis_instance_name, args|
shared_examples_for 'sentinel on port' do |redis_port, redis_cluster_name, args|
it 'enables the redis-sentinel service' do
name = redis_instance_name || 'redis_sentinel_mycluster'
redis_cluster_name ||= 'mycluster'
name = if os[:family] == 'redhat' and os[:release][0] == '7'
"redis-sentinel@#{redis_cluster_name}"
else
"redis_sentinel_#{redis_cluster_name}"
end
expect(service name).to be_enabled
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe 'Redis-Sentinel' do
it_behaves_like 'sentinel on port', 26379, 'redis_sentinel_cluster'
it_behaves_like 'sentinel on port', 26379, 'cluster'
end

describe file('/etc/redis/sentinel_cluster.conf') do
Expand All @@ -19,15 +19,17 @@
end
end

describe file('/etc/init.d/redis_sentinel_cluster') do
[
%r{SENTINELNAME=sentinel_cluster},
%r{EXEC="su -s /bin/sh -c '/usr/local/bin/redis-server /etc/redis/\$\{SENTINELNAME\}.conf --sentinel' redis"},
%r{PIDFILE=/var/run/redis/sentinel_cluster/\$\{SENTINELNAME\}.pid},
%r{mkdir -p /var/run/redis/sentinel_cluster},
%r{chown redis /var/run/redis/sentinel_cluster}
].each do |pattern|
its(:content) { should match(pattern) }
unless os[:family] == 'redhat' and os[:release][0] == '7'
describe file('/etc/init.d/redis_sentinel_cluster') do
[
%r{SENTINELNAME=sentinel_cluster},
%r{EXEC="(su -s /bin/sh)|(runuser redis) -c \\?["']/usr/local/bin/redis-server /etc/redis/\$\{SENTINELNAME\}.conf --sentinel\\?["']( redis)?"},
%r{PIDFILE=/var/run/redis/sentinel_cluster/\$\{SENTINELNAME\}.pid},
%r{mkdir -p /var/run/redis/sentinel_cluster},
%r{chown redis /var/run/redis/sentinel_cluster}
].each do |pattern|
its(:content) { should match(pattern) }
end
end
end

Expand Down

0 comments on commit 7146921

Please sign in to comment.