Skip to content

Commit 6ec5bf2

Browse files
Merge pull request #278 from netmanagers/master
feat(servers_config): add require statement to manage dependencies
2 parents 46faf4e + 6478143 commit 6ec5bf2

File tree

10 files changed

+65
-6
lines changed

10 files changed

+65
-6
lines changed

kitchen.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,13 @@ verifier:
373373
suites:
374374
- name: default
375375
provisioner:
376+
dependencies:
377+
- name: test_dep
378+
path: test/salt/default/states
376379
state_top:
377380
base:
378381
'*':
382+
- test_dep.create_dependency_file
379383
- nginx._mapdata
380384
- nginx
381385
pillars:

nginx/config.sls

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,3 @@ 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/servers_config.sls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ nginx_server_available_dir:
123123
}}
124124
- makedirs: True
125125
- template: jinja
126+
{%- if 'requires' in settings %}
127+
- require:
128+
{%- for k, v in settings.requires.items() %}
129+
- {{ k }}: {{ v }}
130+
{%- endfor %}
131+
{%- endif %}
126132
{% if 'source_path' not in settings.config %}
127133
- context:
128134
config: {{ settings.config|json(sort_keys=False) }}

nginx/service.sls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ nginx_service:
4242
{% else %}
4343
- pkg: nginx_install
4444
{% endif %}
45+
{% if nginx.check_config_before_apply %}
46+
- only_if: /usr/sbin/nginx -t
47+
{% endif %}

pillar.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ nginx:
202202
# and None indicates no action
203203
enabled: true
204204

205+
# This let's you add dependencies on other resources being applied for a
206+
# particular vhost
207+
# A common case is when you use this formula together with letsencrypt's,
208+
# validating through nginx: you need nginx running (to validate the vhost) but
209+
# can't have the ssl vhost up until the certificate is created (because it
210+
# won't exist and will make nginx fail to load the configuration)
211+
#
212+
# An example, when using LE to create the cert for 'some.host.domain':
213+
# requires:
214+
# cmd: create-initial-cert-some.host.domain
215+
requires: {}
216+
205217
# Remove the site config file shipped by nginx
206218
# (i.e. '/etc/nginx/sites-available/default' by default)
207219
# It also remove the symlink (if it is exists).

test/integration/default/controls/config.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,16 @@
7171
its('content') { should include 'try_files $uri $uri/ =404;' }
7272
its('content') { should include 'include snippets/letsencrypt.conf;' }
7373
end
74+
describe file "#{dir}/mysite_with_require" do
75+
it { should be_file }
76+
it { should be_owned_by file_owner }
77+
it { should be_grouped_into file_group }
78+
its('mode') { should cmp '0644' }
79+
its('content') { should include 'server_name with-deps;' }
80+
its('content') { should include 'listen 80;' }
81+
its('content') { should include 'index index.html index.htm;' }
82+
its('content') { should include 'location ~ .htm {' }
83+
its('content') { should include 'try_files $uri $uri/ =404;' }
84+
end
7485
end
7586
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
control 'Dependency test file' do
4+
title 'should exist'
5+
6+
describe file('/tmp/created_to_test_dependencies') do
7+
it { should be_file }
8+
end
9+
end

test/salt/default/pillar/nginx.sls

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ nginx:
3737
- location ~ .htm:
3838
- try_files: '$uri $uri/ =404'
3939
- include: 'snippets/letsencrypt.conf'
40+
mysite_with_require:
41+
enabled: true
42+
config:
43+
- server:
44+
- server_name: with-deps
45+
- listen:
46+
- '80'
47+
- index: 'index.html index.htm'
48+
- location ~ .htm:
49+
- try_files: '$uri $uri/ =404'
50+
requires:
51+
file: created_to_test_dependencies
52+
4053
dh_param:
4154
'mydhparam2.pem':
4255
keysize: 2048
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## this state creates a file that is used to test vhosts dependencies
2+
# (see https://github.com/saltstack-formulas/nginx-formula/pull/278)
3+
4+
created_to_test_dependencies:
5+
file.managed:
6+
- name: /tmp/created_to_test_dependencies

test/salt/passenger/pillar/nginx.sls

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ nginx:
2626
- location ^~ /.well-known/acme-challenge/:
2727
- proxy_pass: http://localhost:9999
2828
server:
29-
3029
config:
3130
# This is required to get the passenger module loaded
3231
# In Debian it can be done with this
@@ -64,5 +63,4 @@ nginx:
6463
- index: 'index.html index.htm'
6564
- location ~ .htm:
6665
- try_files: '$uri $uri/ =404'
67-
# - include: '/etc/nginx/snippets/letsencrypt.conf'
68-
- include: 'snippets/letsencrypt.conf'
66+
- include: '/etc/nginx/snippets/letsencrypt.conf'

0 commit comments

Comments
 (0)