From 24c43546187749c7201577eaf9a9a7fa38e42d7d Mon Sep 17 00:00:00 2001 From: Vadim Lebedev Date: Tue, 27 Sep 2016 00:43:58 +0300 Subject: [PATCH] Add option http_cfg_prepend (#870) Add http_cfg_prepend option --- manifests/config.pp | 7 +++++++ manifests/init.pp | 2 ++ spec/classes/config_spec.rb | 28 ++++++++++++++++++++++++++++ templates/conf.d/nginx.conf.erb | 8 ++++++++ 4 files changed, 45 insertions(+) diff --git a/manifests/config.pp b/manifests/config.pp index da9e29b7f..7514ab4aa 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -64,6 +64,7 @@ $gzip_proxied = 'off', $gzip_types = undef, $gzip_vary = 'off', + $http_cfg_prepend = false, $http_cfg_append = false, $http_tcp_nodelay = 'on', $http_tcp_nopush = 'off', @@ -176,6 +177,12 @@ } validate_string($proxy_buffers) validate_string($proxy_buffer_size) + if ($http_cfg_prepend != false) { + if !(is_hash($http_cfg_prepend) or is_array($http_cfg_prepend)) { + fail('$http_cfg_prepend must be either a hash or array') + } + } + if ($http_cfg_append != false) { if !(is_hash($http_cfg_append) or is_array($http_cfg_append)) { fail('$http_cfg_append must be either a hash or array') diff --git a/manifests/init.pp b/manifests/init.pp index 2656b62de..c14eead5a 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -39,6 +39,7 @@ $fastcgi_cache_path = undef, $fastcgi_cache_use_stale = undef, $gzip = undef, + $http_cfg_prepend = undef, $http_cfg_append = undef, $http_tcp_nodelay = undef, $http_tcp_nopush = undef, @@ -245,6 +246,7 @@ fastcgi_cache_use_stale => $fastcgi_cache_use_stale, gzip => $gzip, http_access_log => $http_access_log, + http_cfg_prepend => $http_cfg_prepend, http_cfg_append => $http_cfg_append, http_tcp_nodelay => $http_tcp_nodelay, http_tcp_nopush => $http_tcp_nopush, diff --git a/spec/classes/config_spec.rb b/spec/classes/config_spec.rb index 92dee1671..35e37eef2 100644 --- a/spec/classes/config_spec.rb +++ b/spec/classes/config_spec.rb @@ -383,6 +383,34 @@ value: false, notmatch: %r{fastcgi_cache_use_stale} }, + { + title: 'should contain ordered appended directives from hash', + attr: 'http_cfg_prepend', + value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from list of hashes', + attr: 'http_cfg_prepend', + value: [['allow', 'test value 1'], ['allow', 'test value 2']], + match: [ + ' allow test value 1;', + ' allow test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from array values', + attr: 'http_cfg_prepend', + value: { 'test1' => ['test value 1', 'test value 2', 'test value 3'] }, + match: [ + ' test1 test value 1;', + ' test1 test value 2;' + ] + }, { title: 'should contain ordered appended directives from hash', attr: 'http_cfg_append', diff --git a/templates/conf.d/nginx.conf.erb b/templates/conf.d/nginx.conf.erb index ff4b2a8ce..baa72012e 100644 --- a/templates/conf.d/nginx.conf.erb +++ b/templates/conf.d/nginx.conf.erb @@ -36,6 +36,14 @@ events { } http { +<% if @http_cfg_prepend -%> + <%- field_width = @http_cfg_prepend.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%> + <%- @http_cfg_prepend.sort_by{|k,v| k}.each do |key,value| -%> + <%- Array(value).each do |asubvalue| -%> + <%= sprintf("%-*s", field_width, key) %> <%= asubvalue %>; + <%- end -%> + <%- end -%> +<% end -%> include <%= @conf_dir %>/mime.types; default_type application/octet-stream; <% if @log_format -%>