Skip to content

Commit 10a4372

Browse files
committed
Set permissions more sanely on log dir. Add tests.
Only sets permissions on the redis_logfile directory if the directory has just been created. Otherwise, it will touch the log file with ownership of the redis user. I'm probably a bad person for not doing this in separate commits, but this also adds in a test-kitchen test suite.
1 parent 9e52777 commit 10a4372

21 files changed

+242
-43
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
*.swp
2+
.kitchen/
3+
.bundle
4+
.vagrant

.kitchen.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
driver:
3+
name: vagrant
4+
5+
provisioner:
6+
name: ansible_playbook
7+
ansible_verbose: true
8+
ansible_verbosity: 2
9+
require_ruby_for_busser: false
10+
require_chef_for_busser: true
11+
hosts: all
12+
13+
platforms:
14+
- name: ubuntu-14.04
15+
- name: centos-6.7
16+
driver_config:
17+
box: wittman/centos-6.7-ansible
18+
- name: centos-7.2
19+
driver_config:
20+
box: wittman/centos-7.2-ansible
21+
22+
suites:
23+
- name: default
24+
- name: logfile
25+
- name: sentinel

.travis.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ install:
1111

1212
script:
1313
# Syntax check
14-
- "ansible-playbook -i localhost, tests/test_server.yml --syntax-check"
15-
- "ansible-playbook -i localhost, tests/test_sentinel.yml --syntax-check"
14+
- "ansible-playbook -i localhost, test/test_server.yml --syntax-check"
15+
- "ansible-playbook -i localhost, test/test_sentinel.yml --syntax-check"
1616
# Run role
17-
- "ansible-playbook -i localhost, tests/test_server.yml --connection=local --sudo"
17+
- "ansible-playbook -i localhost, test/test_server.yml --connection=local --sudo"
1818
# Idempotency check
1919
- >
20-
ansible-playbook -i localhost, tests/test_server.yml --connection=local --sudo
20+
ansible-playbook -i localhost, test/test_server.yml --connection=local --sudo
2121
| grep -q 'changed=0.*failed=0'
2222
&& (echo 'Idempotency: PASS' && exit 0)
2323
|| (echo 'Idempotency: FAIL' && exit 1)
24-
- "ansible-playbook -i localhost, tests/test_sentinel.yml --connection=local --sudo"
24+
- "ansible-playbook -i localhost, test/test_sentinel.yml --connection=local --sudo"
2525
- >
26-
ansible-playbook -i localhost, tests/test_sentinel.yml --connection=local --sudo
26+
ansible-playbook -i localhost, test/test_sentinel.yml --connection=local --sudo
2727
| grep -q 'changed=0.*failed=0'
2828
&& (echo 'Idempotency: PASS' && exit 0)
2929
|| (echo 'Idempotency: FAIL' && exit 1)

Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "test-kitchen"
4+
gem "kitchen-ansible"
5+
gem "kitchen-vagrant"

Gemfile.lock

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
faraday (0.9.2)
5+
multipart-post (>= 1.2, < 3)
6+
highline (1.7.8)
7+
kitchen-ansible (0.0.36)
8+
librarian-ansible
9+
test-kitchen (~> 1.4)
10+
kitchen-vagrant (0.19.0)
11+
test-kitchen (~> 1.4)
12+
librarian (0.1.2)
13+
highline
14+
thor (~> 0.15)
15+
librarian-ansible (1.0.6)
16+
faraday
17+
librarian (~> 0.1.0)
18+
mixlib-shellout (2.2.5)
19+
multipart-post (2.0.0)
20+
net-scp (1.2.1)
21+
net-ssh (>= 2.6.5)
22+
net-ssh (2.9.2)
23+
safe_yaml (1.0.4)
24+
test-kitchen (1.4.2)
25+
mixlib-shellout (>= 1.2, < 3.0)
26+
net-scp (~> 1.1)
27+
net-ssh (~> 2.7, < 2.10)
28+
safe_yaml (~> 1.0)
29+
thor (~> 0.18)
30+
thor (0.19.1)
31+
32+
PLATFORMS
33+
ruby
34+
35+
DEPENDENCIES
36+
kitchen-ansible
37+
kitchen-vagrant
38+
test-kitchen
39+
40+
BUNDLED WITH
41+
1.11.2

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ redis_syslog_facility: USER
212212
## General configuration
213213
# Daemonize the redis server. Must be a string "yes" or "no".
214214
redis_daemonize: "yes"
215+
# Pidfile. If the directory does not exist, it will be created with the redis user as the owner. The redis user must have rwx permissions on this directory.
215216
redis_pidfile: /var/run/redis/{{ redis_port }}.pid
216217
# Number of databases to allow
217218
redis_databases: 16

defaults/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
redis_version: 2.8.9
44
redis_install_dir: /opt/redis
55
redis_user: redis
6+
redis_group: "{{ redis_user }}"
67
redis_dir: /var/lib/redis/{{ redis_port }}
78
redis_tarball: false
89
# The open file limit for Redis/Sentinel

tasks/install.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
when: redis_tarball
3737

3838
- name: extract redis tarball
39-
shell: tar xf /usr/local/src/redis-{{ redis_version }}.tar.gz -C /usr/local/src
40-
creates=/usr/local/src/redis-{{ redis_version }}
39+
unarchive: src=/usr/local/src/redis-{{ redis_version }}.tar.gz dest=/usr/local/src
40+
creates=/usr/local/src/redis-{{ redis_version }}/Makefile
4141

4242
- name: compile redis
4343
command: make -j5

tasks/sentinel.yml

+30-17
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,43 @@
2222
service: name=sentinel_{{ redis_sentinel_port }} enabled=yes
2323
when: redis_as_service
2424

25-
- name: check if sentinel log file exists
26-
stat: path={{ redis_sentinel_logfile }}
27-
register: sentinel_logfile_stat
25+
# Check then create log dir to prevent aggressively overwriting permissions
26+
- name: check if sentinel log directory exists
27+
stat: path={{ redis_sentinel_logfile|dirname }}
28+
register: sentinel_logdir
29+
changed_when: false
30+
when: redis_sentinel_logfile != '""'
2831

29-
- name: ensure sentinel pidfile directory exists and has correct owner
30-
file: path={{ redis_sentinel_pidfile|dirname }}
32+
- name: create sentinel log directory if it does not exist
33+
file: state=directory
34+
path={{ redis_sentinel_logfile|dirname }}
3135
owner={{ redis_user }}
32-
state=directory
33-
recurse=yes
36+
group={{ redis_group }}
37+
when:
38+
- redis_sentinel_logfile != '""'
39+
- not sentinel_logdir.stat.exists
3440

35-
- name: ensure sentinel logfile directory exists and has correct owner
36-
file: path={{ redis_sentinel_logfile|dirname }}
41+
- name: touch the sentinel log file
42+
file: state=touch
43+
path={{ redis_sentinel_logfile }}
3744
owner={{ redis_user }}
38-
state=directory
39-
recurse=yes
45+
group={{ redis_group }}
4046
when: redis_sentinel_logfile != '""'
4147

42-
- name: ensure that sentinel log file exists and is writable by redis
43-
file: path={{ redis_sentinel_logfile }}
48+
- name: check if sentinel pid directory exists
49+
stat: path={{ redis_sentinel_pidfile|dirname }}
50+
register: sentinel_piddir
51+
changed_when: false
52+
when: redis_sentinel_pidfile != '""'
53+
54+
- name: create sentinel pid directory if it does not exist
55+
file: state=directory
56+
path={{ redis_sentinel_pidfile|dirname }}
4457
owner={{ redis_user }}
45-
group={{ redis_user }}
46-
mode=0600
47-
state=touch
48-
when: sentinel_logfile_stat.stat.exists == False and redis_sentinel_logfile != '""'
58+
group={{ redis_group }}
59+
when:
60+
- redis_sentinel_pidfile != '""'
61+
- not sentinel_piddir.stat.exists
4962

5063
- name: create sentinel config file
5164
template: src=redis_sentinel.conf.j2

tasks/server.yml

+31-18
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,43 @@
2121
service: name=redis_{{ redis_port }} enabled=yes
2222
when: redis_as_service
2323

24-
- name: check if log file exists
25-
stat: path={{ redis_logfile }}
26-
register: logfile_stat
27-
28-
- name: ensure pidfile directory exists and has correct owner
29-
file: path={{ redis_pidfile|dirname }}
24+
# Check then create log dir to prevent aggressively overwriting permissions
25+
- name: check if log directory exists
26+
stat: path={{ redis_logfile|dirname }}
27+
register: logdir
28+
changed_when: false
29+
when: redis_logfile != '""'
30+
31+
- name: create log directory if it does not exist
32+
file: state=directory
33+
path={{ redis_logfile|dirname }}
3034
owner={{ redis_user }}
31-
state=directory
32-
recurse=yes
35+
group={{ redis_group }}
36+
when:
37+
- redis_logfile != '""'
38+
- not logdir.stat.exists
3339

34-
- name: ensure logfile directory exists and has correct owner
35-
file: path={{ redis_logfile|dirname }}
40+
- name: touch the log file
41+
file: state=touch
42+
path={{ redis_logfile }}
3643
owner={{ redis_user }}
37-
state=directory
38-
recurse=yes
44+
group={{ redis_group }}
3945
when: redis_logfile != '""'
4046

41-
- name: ensure that log file exists and is writable by redis
42-
file: path={{ redis_logfile }}
47+
- name: check if pid directory exists
48+
stat: path={{ redis_pidfile|dirname }}
49+
register: piddir
50+
changed_when: false
51+
when: redis_pidfile != '""'
52+
53+
- name: create pid directory if it does not exist
54+
file: state=directory
55+
path={{ redis_pidfile|dirname }}
4356
owner={{ redis_user }}
44-
group={{ redis_user }}
45-
mode=0600
46-
state=touch
47-
when: logfile_stat.stat.exists == False and redis_logfile != '""'
57+
group={{ redis_group }}
58+
when:
59+
- redis_pidfile != '""'
60+
- not piddir.stat.exists
4861

4962
- name: create redis config file
5063
template: src=redis.conf.j2 dest=/etc/redis/{{ redis_port }}.conf

test/integration/default/default.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- hosts: localhost
3+
roles:
4+
- ansible-redis
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe 'Redis' do
4+
describe service('redis_6379') do
5+
it { should be_enabled }
6+
it { should be_running }
7+
end
8+
9+
describe port(6379) do
10+
it { should be_listening.on('0.0.0.0').with('tcp') }
11+
end
12+
13+
describe file('/etc/redis/6379.conf') do
14+
it { should be_file }
15+
it { should be_owned_by 'redis' }
16+
its(:content) { should match /port 6379/ }
17+
end
18+
19+
describe file('/var/run/redis/6379.pid') do
20+
it { should be_file }
21+
it { should be_owned_by 'redis' }
22+
its(:size) { should_be > 0 }
23+
end
24+
25+
describe file('/proc/sys/vm/overcommit_memory') do
26+
it { should be_file }
27+
it { should contain '1' }
28+
end
29+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'serverspec'
2+
set :backend, :exec

test/integration/logfile/default.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- hosts: localhost
3+
roles:
4+
- role: ansible-redis
5+
redis_logfile: "/var/log/redis.log"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe 'Redis' do
4+
describe service('redis_6379') do
5+
it { should be_enabled }
6+
it { should be_running }
7+
end
8+
9+
describe port(6379) do
10+
it { should be_listening.with('tcp') }
11+
end
12+
13+
describe file('/var/log/redis.log') do
14+
it { should be_file }
15+
it { should be_owned_by 'redis' }
16+
its(:size) { should > 0 }
17+
end
18+
19+
describe file('/var/log') do
20+
it { should be_directory }
21+
it { should_not be_owned_by('redis') }
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'serverspec'
2+
set :backend, :exec

test/integration/sentinel/default.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- hosts: localhost
3+
roles:
4+
- role: ansible-redis
5+
redis_sentinel: true
6+
redis_sentinel_logfile: "/var/log/redis_sentinel.log"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'spec_helper'
2+
3+
describe 'Redis' do
4+
describe service('sentinel_26379') do
5+
it { should be_enabled }
6+
it { should be_running }
7+
end
8+
9+
describe port(26379) do
10+
it { should be_listening.on('0.0.0.0').with('tcp') }
11+
end
12+
13+
describe file('/etc/redis/sentinel_26379.conf') do
14+
it { should be_file }
15+
it { should be_owned_by 'redis' }
16+
its(:content) { should match /port 26379/ }
17+
end
18+
19+
describe file('/var/run/redis/sentinel_26379.pid') do
20+
it { should be_file }
21+
it { should be_owned_by 'redis' }
22+
its(:size) { should > 0 }
23+
end
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'serverspec'
2+
set :backend, :exec
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)