Skip to content

Commit

Permalink
initial support for snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
bryangwilliam authored and bastelfreak committed Feb 9, 2019
1 parent ca4d5ea commit aa07f14
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/snippet.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

class { 'nginx':
snippets_dir => '/etc/nginx/snippets',
}

$snippet = @("SNIPPET"/L)
location @custom_451_error {
return 451;
}
| SNIPPET

nginx::resource::snippet { 'test_snippet':
raw_content => $snippet,
}

nginx::resource::server { 'test.local:8080':
ensure => present,
listen_port => 8080,
server_name => ['test.local test'],
include_files => ["${nginx::snippets_dir}/test_snippet.conf"],
try_files => [ 'non-existant', '@custom_451_error'],
}

nginx::resource::server { 'test.local:8081':
ensure => present,
listen_port => 8081,
server_name => ['test.local test'],
include_files => ["${nginx::snippets_dir}/test_snippet.conf"],
try_files => [ 'non-existant', '@custom_451_error'],
}
6 changes: 6 additions & 0 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@
ensure => directory,
}

if $nginx::manage_snippets_dir {
file { $nginx::snippets_dir:
ensure => directory,
}
}

file { $log_dir:
ensure => directory,
mode => $log_mode,
Expand Down
2 changes: 2 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
Enum['on', 'off'] $spdy = 'off',
Enum['on', 'off'] $http2 = 'off',
Enum['on', 'off'] $ssl_stapling = 'off',
Stdlib::Absolutepath $snippets_dir = $nginx::params::snippets_dir,
Boolean $manage_snippets_dir = true,
$types_hash_bucket_size = '512',
$types_hash_max_size = '1024',
Integer $worker_connections = 1024,
Expand Down
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@

### Referenced Variables
$conf_dir = $_module_parameters['conf_dir']
$snippets_dir = "${conf_dir}/snippets"
$log_dir = $_module_parameters['log_dir']
$log_user = $_module_parameters['log_user']
$log_group = $_module_parameters['log_group']
Expand Down
40 changes: 40 additions & 0 deletions manifests/resource/snippet.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# define: nginx::resource::snippet
#
# This definition creates a reusable config snippet that can be included by other resources
#
# Parameters:
# [*ensure*] - Enables or disables the specified snippet (present|absent)
# [*owner*] - Defines owner of the .conf file
# [*group*] - Defines group of the .conf file
# [*mode*] - Defines mode of the .conf file
# [*raw_content*] - Raw content that will be inserted into the snipped as-is
#
define nginx::resource::snippet (
String[1] $raw_content,
Enum['absent', 'present'] $ensure = 'present',
String $owner = $nginx::global_owner,
String $group = $nginx::global_group,
Stdlib::Filemode $mode = $nginx::global_mode,
) {
if ! defined(Class['nginx']) {
fail('You must include the nginx base class before using any defined resources')
}

$name_sanitized = regsubst($name, ' ', '_', 'G')
$config_file = "${nginx::snippets_dir}/${name_sanitized}.conf"

concat { $config_file:
ensure => $ensure,
owner => $owner,
group => $group,
mode => $mode,
notify => Class['nginx::service'],
require => File[$nginx::snippets_dir],
}

concat::fragment { "snippet-${name_sanitized}-header":
target => $config_file,
content => epp("${module_name}/snippet/snippet_header.epp", { 'raw_content' => $raw_content }),
order => '001',
}
}
30 changes: 30 additions & 0 deletions spec/defines/resource_snippet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe 'nginx::resource::snippet' do
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
facts
end
let :title do
'some_snippet'
end

let :pre_condition do
'include nginx'
end

describe 'basic snippet' do
let :params do
{
raw_content: 'this is a test'
}
end

it { is_expected.to contain_concat__fragment('snippet-some_snippet-header').with_target("/etc/nginx/snippets/#{title}.conf").with_content(%r{this is a test}) }
it { is_expected.to contain_concat('/etc/nginx/snippets/some_snippet.conf') }
it { is_expected.to compile.with_all_deps }
end
end
end
end
5 changes: 5 additions & 0 deletions templates/snippet/snippet_header.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%- | String $raw_content,
| -%>
# MANAGED BY PUPPET

<%= $raw_content %>

0 comments on commit aa07f14

Please sign in to comment.