Skip to content

Commit a30001d

Browse files
Merge pull request #269 from netmanagers/master
Check nginx config before deploying & various passenger fixes
2 parents 63d32a4 + eedfc56 commit a30001d

File tree

16 files changed

+286
-12
lines changed

16 files changed

+286
-12
lines changed

.yamllint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ignore: |
1414
.cache/
1515
.git/
1616
node_modules/
17-
test/**/states/**/*.sls
17+
test/salt/**/*.sls
1818
.kitchen/
1919
2020
yaml-files:

nginx/config.sls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ nginx_config:
3131
- context:
3232
config: {{ nginx.server.config|json(sort_keys=False) }}
3333
{% endif %}
34+
{% if nginx.check_config_before_apply %}
35+
- check_cmd: /usr/sbin/nginx -t -c
36+
{% endif %}

nginx/map.jinja

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'Debian': {
1010
'package': 'nginx',
1111
'passenger_package': 'passenger',
12-
'passenger_config_file': '/etc/nginx/conf.d/passenger.conf',
12+
'passenger_config_file': '/etc/nginx/conf.d/mod-http-passenger.conf',
1313
'service': 'nginx',
1414
'webuser': 'www-data',
1515
'conf_file': '/etc/nginx/nginx.conf',
@@ -112,6 +112,7 @@
112112
'install_from_ppa': False,
113113
'install_from_repo': False,
114114
'install_from_phusionpassenger': False,
115+
'check_config_before_apply': False,
115116
'ppa_version': 'stable',
116117
'source_version': '1.10.0',
117118
'source_hash': '8ed647c3dd65bc4ced03b0e0f6bf9e633eff6b01bac772bcf97077d58bc2be4d',

nginx/passenger.sls

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ passenger_install:
2525
- pkg: nginx_install
2626
- require_in:
2727
- service: nginx_service
28+
- file: nginx_config
2829
2930
/etc/nginx/passenger.conf:
3031
file.absent:
@@ -46,6 +47,7 @@ passenger_config:
4647
- service: nginx_service
4748
- require_in:
4849
- service: nginx_service
50+
- file: nginx_config
4951
- require:
5052
- file: /etc/nginx/passenger.conf
5153
- pkg: passenger_install

nginx/pkg.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ nginx_phusionpassenger_yum_repo:
163163
- baseurl: 'https://oss-binaries.phusionpassenger.com/yum/passenger/el/$releasever/$basearch'
164164
- repo_gpgcheck: 1
165165
- gpgcheck: 0
166-
- gpgkey: 'https://packagecloud.io/gpg.key'
166+
- gpgkey: 'https://oss-binaries.phusionpassenger.com/yum/definitions/RPM-GPG-KEY.asc'
167167
- enabled: True
168168
- sslverify: 1
169169
- sslcacert: /etc/pki/tls/certs/ca-bundle.crt

nginx/snippets.sls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ nginx_snippet_{{ snippet }}:
2828
- context:
2929
config: {{ config|json() }}
3030
nginx: {{ _nginx|json() }}
31+
- require:
32+
- file: nginx_snippets_dir
33+
- require_in:
34+
- file: nginx_config
35+
- service: nginx_service
3136
{% endfor %}

pillar.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ nginx:
2828
source_version: '1.10.0'
2929
source_hash: ''
3030

31+
# Check the configuration before applying:
32+
# To prevent applying a configuration that might break nginx, set this
33+
# parameter to true so the configuration is checked BEFORE applying. If
34+
# the check fails, the state will fail and it won't be deployed.
35+
# CAVEAT: As the configuration file is created in a temp dir, it can't
36+
# have relative references or it will fail to check. You'll need to
37+
# specify full paths where required (ie, `include`, `load_module`,
38+
# `snippets`, etc.0
39+
# Defaults to false
40+
check_config_before_apply: false
41+
3142
# These are usually set by grains in map.jinja
3243
# Typically you can comment these out.
3344
lookup:

test/integration/default/controls/config.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# frozen_string_literal: true
2+
13
# Set defaults, use debian as base
24

35
server_available = '/etc/nginx/sites-available'
4-
server_enabled = '/etc/nginx/sites-enabled'
6+
server_enabled = '/etc/nginx/sites-enabled'
57

68
# Override by platform family
79
case platform[:family]
8-
when 'redhat','fedora'
10+
when 'redhat', 'fedora'
911
server_available = '/etc/nginx/conf.d'
1012
server_enabled = '/etc/nginx/conf.d'
1113
when 'suse'
@@ -22,9 +24,13 @@
2224
it { should be_owned_by 'root' }
2325
it { should be_grouped_into 'root' }
2426
its('mode') { should cmp '0644' }
25-
its('content') { should include %Q[ log_format main '$remote_addr - $remote_user [$time_local] $status '
27+
its('content') do
28+
# rubocop:disable Metrics/LineLength
29+
should include %( log_format main '$remote_addr - $remote_user [$time_local] $status '
2630
'"$request" $body_bytes_sent "$http_referer" '
27-
'"$http_user_agent" "$http_x_forwarded_for"';] }
31+
'"$http_user_agent" "$http_x_forwarded_for"';)
32+
# rubocop:enable Metrics/LineLength
33+
end
2834
end
2935

3036
# snippets configuration
@@ -40,12 +46,11 @@
4046

4147
# sites configuration
4248
[server_available, server_enabled].each do |dir|
43-
44-
describe file ("#{dir}/default") do
45-
it { should_not exist }
49+
describe file "#{dir}/default" do
50+
it { should_not exist }
4651
end
4752

48-
describe file ("#{dir}/mysite") do
53+
describe file "#{dir}/mysite" do
4954
it { should be_file }
5055
it { should be_owned_by 'root' }
5156
it { should be_grouped_into 'root' }
@@ -57,6 +62,5 @@
5762
its('content') { should include 'try_files $uri $uri/ =404;' }
5863
its('content') { should include 'include snippets/letsencrypt.conf;' }
5964
end
60-
6165
end
6266
end

test/integration/default/controls/install.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
control 'Nginx package' do
24
title 'should be installed'
35

test/integration/default/controls/service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
control 'Nginx service' do
24
title 'should be running and enabled'
35

test/integration/passenger/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# InSpec Profile: `passenger`
2+
3+
This shows the implementation of the `passenger` InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md).
4+
5+
## Verify a profile
6+
7+
InSpec ships with built-in features to verify a profile structure.
8+
9+
```bash
10+
$ inspec check passenger
11+
Summary
12+
-------
13+
Location: passenger
14+
Profile: profile
15+
Controls: 4
16+
Timestamp: 2019-06-24T23:09:01+00:00
17+
Valid: true
18+
19+
Errors
20+
------
21+
22+
Warnings
23+
--------
24+
```
25+
26+
## Execute a profile
27+
28+
To run all **supported** controls on a local machine use `inspec exec /path/to/profile`.
29+
30+
```bash
31+
$ inspec exec passenger
32+
..
33+
34+
Finished in 0.0025 seconds (files took 0.12449 seconds to load)
35+
8 examples, 0 failures
36+
```
37+
38+
## Execute a specific control from a profile
39+
40+
To run one control from the profile use `inspec exec /path/to/profile --controls name`.
41+
42+
```bash
43+
$ inspec exec passenger --controls package
44+
.
45+
46+
Finished in 0.0025 seconds (files took 0.12449 seconds to load)
47+
1 examples, 0 failures
48+
```
49+
50+
See an [example control here](https://github.com/inspec/inspec/blob/master/examples/profile/controls/example.rb).
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
# Set defaults, use debian as base
4+
5+
# Override by OS Family
6+
case platform[:family]
7+
when 'redhat', 'centos', 'fedora'
8+
server_available = '/etc/nginx/conf.d'
9+
server_enabled = '/etc/nginx/conf.d'
10+
passenger_mod = '/usr/lib64/nginx/modules/ngx_http_passenger_module.so'
11+
passenger_root = '/usr/share/ruby/vendor_ruby/phusion_passenger/locations.ini'
12+
passenger_config_file = '/etc/nginx/conf.d/passenger.conf'
13+
should_not_exist_file = '/etc/nginx/conf.d/mod-http-passenger.conf'
14+
when 'debian', 'ubuntu'
15+
server_available = '/etc/nginx/sites-available'
16+
server_enabled = '/etc/nginx/sites-enabled'
17+
passenger_mod = '/usr/lib/nginx/modules/ngx_http_passenger_module.so'
18+
passenger_root = '/usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini'
19+
passenger_config_file = '/etc/nginx/conf.d/mod-http-passenger.conf'
20+
should_not_exist_file = '/etc/nginx/conf.d/passenger.conf'
21+
end
22+
23+
control 'Passenger configuration' do
24+
title 'should match desired lines'
25+
26+
# main configuration
27+
describe file('/etc/nginx/nginx.conf') do
28+
its('content') { should include "load_module #{passenger_mod}" }
29+
end
30+
31+
describe file(passenger_config_file) do
32+
it { should be_file }
33+
it { should be_owned_by 'root' }
34+
it { should be_grouped_into 'root' }
35+
its('mode') { should cmp '0644' }
36+
its('content') { should include "passenger_root #{passenger_root};" }
37+
its('content') { should include 'passenger_ruby /usr/bin/ruby;' }
38+
end
39+
40+
describe file(should_not_exist_file) do
41+
it { should_not exist }
42+
end
43+
44+
# sites configuration
45+
[server_available, server_enabled].each do |dir|
46+
describe file "#{dir}/default" do
47+
it { should_not exist }
48+
end
49+
50+
describe file "#{dir}/mysite" do
51+
it { should be_file }
52+
it { should be_owned_by 'root' }
53+
it { should be_grouped_into 'root' }
54+
its('mode') { should cmp '0644' }
55+
its('content') { should include 'passenger_enabled on;' }
56+
end
57+
end
58+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
control 'Nginx package' do
4+
title 'should be installed'
5+
6+
describe package('nginx') do
7+
it { should be_installed }
8+
end
9+
end
10+
11+
control 'Passenger packages' do
12+
title 'should be installed'
13+
14+
# Override by OS Family
15+
passenger_mod_pkg = case platform[:family]
16+
when 'redhat', 'centos', 'fedora'
17+
'nginx-mod-http-passenger'
18+
when 'debian', 'ubuntu'
19+
'libnginx-mod-http-passenger'
20+
end
21+
22+
describe package('passenger') do
23+
it { should be_installed }
24+
end
25+
describe package(passenger_mod_pkg) do
26+
it { should be_installed }
27+
end
28+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
control 'Nginx service' do
4+
title 'should be running and enabled'
5+
6+
describe service('nginx') do
7+
it { should be_enabled }
8+
it { should be_running }
9+
end
10+
end
11+
12+
control 'Passenger module' do
13+
title 'should be running and enabled'
14+
15+
describe 'Passenger engine' do
16+
it 'passenger-config should say configuration "looks good"' do
17+
expect(command(
18+
'/usr/bin/passenger-config validate-install --auto'
19+
).stdout).to match(/looks good/)
20+
end
21+
22+
it 'passenger-memory-stats should return Passenger stats' do
23+
expect(command('/usr/sbin/passenger-memory-stats').stdout).to match(
24+
%r{nginx: master process /usr/sbin/nginx.*Passenger watchdog.*Passenger core.*}m
25+
)
26+
end
27+
end
28+
end

test/integration/passenger/inspec.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=yaml
3+
---
4+
name: default
5+
title: nginx formula
6+
maintainer: SaltStack Formulas
7+
license: Apache-2.0
8+
summary: Verify that the nginx formula is setup and configured correctly
9+
supports:
10+
- platform-name: debian
11+
- platform-name: ubuntu
12+
- platform-name: centos

0 commit comments

Comments
 (0)