From fb880576a63f220e0fdc463a7c40732eadfc36ac Mon Sep 17 00:00:00 2001 From: "Carl P. Corliss" Date: Fri, 12 Sep 2014 13:42:51 -0400 Subject: [PATCH] cleanup resultant whitespace and key/value alignment in config files - fix tests related to whitespace changes --- spec/classes/config_spec.rb | 24 ++++- spec/defines/resource_geo_spec.rb | 6 +- spec/defines/resource_location_spec.rb | 42 ++++---- spec/defines/resource_vhost_spec.rb | 122 ++++++++++++------------ templates/conf.d/geo.erb | 13 ++- templates/conf.d/map.erb | 4 +- templates/conf.d/nginx.conf.erb | 37 +++---- templates/vhost/location_footer.erb | 1 + templates/vhost/location_header.erb | 4 +- templates/vhost/locations/alias.erb | 2 +- templates/vhost/locations/directory.erb | 31 +++--- templates/vhost/locations/fastcgi.erb | 28 +++--- templates/vhost/locations/proxy.erb | 48 ++++++---- templates/vhost/vhost_header.erb | 33 ++++--- templates/vhost/vhost_ssl_header.erb | 57 ++++++----- 15 files changed, 254 insertions(+), 198 deletions(-) diff --git a/spec/classes/config_spec.rb b/spec/classes/config_spec.rb index a97565d..34ba4a6 100644 --- a/spec/classes/config_spec.rb +++ b/spec/classes/config_spec.rb @@ -195,13 +195,13 @@ describe 'nginx::config' do :title => 'should set proxy_cache_path', :attr => 'proxy_cache_path', :value => '/path/to/proxy.cache', - :match => ' proxy_cache_path /path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;', + :match => %r'\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;', }, { :title => 'should not set proxy_cache_path', :attr => 'proxy_cache_path', :value => false, - :notmatch => / proxy_cache_path \/path\/to\/proxy\.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;/, + :notmatch => %r'\s+proxy_cache_path\s+/path/to/proxy\.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;', }, { :title => 'should contain ordered appended directives from hash', @@ -252,7 +252,15 @@ describe 'nginx::config' do it { is_expected.to contain_file("/etc/nginx/nginx.conf").with_mode('0644') } it param[:title] do - verify_contents(subject, "/etc/nginx/nginx.conf", Array(param[:match])) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_file('/etc/nginx/nginx.conf').with_content(item) } + else + lines = subject.resource('file', '/etc/nginx/nginx.conf').send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + Array(param[:notmatch]).each do |item| is_expected.to contain_file("/etc/nginx/nginx.conf").without_content(item) end @@ -308,7 +316,15 @@ describe 'nginx::config' do it { is_expected.to contain_file("/etc/nginx/conf.d/proxy.conf").with_mode('0644') } it param[:title] do - verify_contents(subject, "/etc/nginx/conf.d/proxy.conf", Array(param[:match])) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_file('/etc/nginx/conf.d/proxy.conf').with_content(item) } + else + lines = subject.resource('file', '/etc/nginx/conf.d/proxy.conf').send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + Array(param[:notmatch]).each do |item| is_expected.to contain_file("/etc/nginx/conf.d/proxy.conf").without_content(item) end diff --git a/spec/defines/resource_geo_spec.rb b/spec/defines/resource_geo_spec.rb index 69ba9ab..cceff06 100644 --- a/spec/defines/resource_geo_spec.rb +++ b/spec/defines/resource_geo_spec.rb @@ -75,8 +75,8 @@ describe 'nginx::resource::geo' do '10.0.0.0/8' => 'intra', }, :match => [ - ' 10.0.0.0/8 intra;', - ' 172.16.0.0/12 intra;', + ' 10.0.0.0/8 intra;', + ' 172.16.0.0/12 intra;', ' 192.168.0.0/16 intra;', ], }, @@ -99,7 +99,7 @@ describe 'nginx::resource::geo' do :title => 'should set delete', :attr => 'delete', :value => '192.168.0.0/16', - :match => ' delete 192.168.0.0/16;' + :match => ' delete 192.168.0.0/16;' }, ].each do |param| context "when #{param[:attr]} is #{param[:value]}" do diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 9a893f9..9fc3989 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -274,7 +274,7 @@ describe 'nginx::resource::location' do { :location => 'location', :www_root => '/var/www/root', - :vhost => 'vhost1' + :vhost => 'vhost1', } end @@ -283,7 +283,7 @@ describe 'nginx::resource::location' do :title => 'should set www_root', :attr => 'www_root', :value => '/', - :match => ' root /;' + :match => ' root /;' }, { :title => 'should set try_file(s)', @@ -295,7 +295,7 @@ describe 'nginx::resource::location' do :title => 'should set index_file(s)', :attr => 'index_files', :value => ['name1','name2'], - :match => ' index name1 name2;', + :match => ' index name1 name2;', }, { :title => 'should contain rewrite rules', @@ -428,31 +428,31 @@ describe 'nginx::resource::location' do :title => 'should set www_root', :attr => 'www_root', :value => '/', - :match => ' root /;' + :match => %r'\s+root\s+/;' }, { :title => 'should set fastcgi_split_path', :attr => 'fastcgi_split_path', :value => 'value', - :match => ' fastcgi_split_path_info value;' + :match => %r'\s+fastcgi_split_path_info\s+value;' }, { :title => 'should set try_file(s)', :attr => 'try_files', :value => ['name1','name2'], - :match => ' try_files name1 name2;', + :match => %r'\s+try_files\s+name1 name2;', }, { :title => 'should set fastcgi_params', :attr => 'fastcgi_params', :value => 'value', - :match => /^[ ]+include\s+value;/ + :match => %r'\s+include\s+value;' }, { :title => 'should set fastcgi_pass', :attr => 'fastcgi', :value => 'value', - :match => ' fastcgi_pass value;' + :match => %r'\s+fastcgi_pass\s+value;' }, ].each do |param| context "when #{param[:attr]} is #{param[:value]}" do @@ -502,10 +502,6 @@ describe 'nginx::resource::location' do with_content(%r|fastcgi_param\s+CUSTOM_PARAM\s+value;|). with_content(%r|fastcgi_param\s+CUSTOM_PARAM2\s+value2;|) end - it "should add comment # Enable custom fastcgi_params" do - should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")). - with_content(%r|# Enable custom fastcgi_params\s+|) - end end context "when fastcgi_param is not set" do @@ -528,7 +524,7 @@ describe 'nginx::resource::location' do :title => 'should set proxy_cache', :attr => 'proxy_cache', :value => 'value', - :match => /^[ ]+proxy_cache\s+value;/, + :match => /^\s+proxy_cache\s+value;/, }, { :title => 'should not set proxy_cache_valid', @@ -540,7 +536,7 @@ describe 'nginx::resource::location' do :title => 'should set proxy_cache_valid', :attr => 'proxy_cache_valid', :value => 'value', - :match => /^[ ]+proxy_cache_valid\s+value;/, + :match => /^\s+proxy_cache_valid\s+value;/, }, { :title => 'should not set proxy_cache', @@ -552,46 +548,46 @@ describe 'nginx::resource::location' do :title => 'should set proxy_pass', :attr => 'proxy', :value => 'value', - :match => /^[ ]+proxy_pass\s+value;/, + :match => /^\s+proxy_pass\s+value;/, }, { :title => 'should set proxy_read_timeout', :attr => 'proxy_read_timeout', :value => 'value', - :match => ' proxy_read_timeout value;', + :match => %r'\s+proxy_read_timeout\s+value;', }, { :title => 'should set proxy_connect_timeout', :attr => 'proxy_connect_timeout', :value => 'value', - :match => ' proxy_connect_timeout value;', + :match => %r'\s+proxy_connect_timeout\s+value;', }, { :title => 'should set proxy_read_timeout', :attr => 'proxy_read_timeout', :value => 'value', - :match => ' proxy_read_timeout value;', + :match => %r'\s+proxy_read_timeout\s+value;', }, { :title => 'should set proxy headers', :attr => 'proxy_set_header', :value => [ 'X-TestHeader1 value1', 'X-TestHeader2 value2' ], :match => [ - /^[ ]+proxy_set_header\s+X-TestHeader1 value1;/, - /^[ ]+proxy_set_header\s+X-TestHeader2 value2;/, + /^\s+proxy_set_header\s+X-TestHeader1 value1;/, + /^\s+proxy_set_header\s+X-TestHeader2 value2;/, ] }, { :title => 'should set proxy_method', :attr => 'proxy_method', :value => 'value', - :match => ' proxy_method value;', + :match => %r'\s+proxy_method\s+value;', }, { :title => 'should set proxy_set_body', :attr => 'proxy_set_body', :value => 'value', - :match => ' proxy_set_body value;', + :match => %r'\s+proxy_set_body\s+value;', }, { :title => 'should contain rewrite rules', @@ -646,7 +642,7 @@ describe 'nginx::resource::location' do :proxy_cache_valid => '10m', } end - it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).with_content(/proxy_cache_valid 10m;/) } + it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).with_content(/proxy_cache_valid\s+10m;/) } end end diff --git a/spec/defines/resource_vhost_spec.rb b/spec/defines/resource_vhost_spec.rb index c463309..6cb8d4c 100644 --- a/spec/defines/resource_vhost_spec.rb +++ b/spec/defines/resource_vhost_spec.rb @@ -35,8 +35,8 @@ describe 'nginx::resource::vhost' do 'group' => 'root', 'mode' => '0644', })} - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{access_log[ ]+/var/log/nginx/www\.rspec\.example\.com\.access\.log}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{error_log[ ]+/var/log/nginx/www\.rspec\.example\.com\.error\.log}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{access_log\s+/var/log/nginx/www\.rspec\.example\.com\.access\.log}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{error_log\s+/var/log/nginx/www\.rspec\.example\.com\.error\.log}) } it { is_expected.to contain_concat__fragment("#{title}-footer") } it { is_expected.to contain_nginx__resource__location("#{title}-default") } it { is_expected.not_to contain_file("/etc/nginx/fastcgi_params") } @@ -55,116 +55,117 @@ describe 'nginx::resource::vhost' do :value => false, :notmatch => %r| ^ - \s+listen\s+\*:443\s+ssl;\n + \s+listen\s+\*:80;\n \s+server_name\s+www\.rspec\.example\.com;\n - \s+return\s+301\s+https://rspec\.example\.com\$uri; + \s+return\s+301\s+http://rspec\.example\.com\$uri; |x, }, { :title => 'should contain www to non-www rewrite', :attr => 'rewrite_www_to_non_www', :value => true, - :match => [ - ' listen *:80;', - ' server_name www.rspec.example.com;', - ' return 301 http://rspec.example.com$uri;', - ], + :match => %r| + ^ + \s+listen\s+\*:80;\n + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+http://rspec\.example\.com\$uri; + |x, }, { :title => 'should set the IPv4 listen IP', :attr => 'listen_ip', :value => '127.0.0.1', - :match => ' listen 127.0.0.1:80;', + :match => %r'\s+listen\s+127.0.0.1:80;', }, { :title => 'should set the IPv4 listen port', :attr => 'listen_port', :value => 45, - :match => ' listen *:45;', + :match => %r'\s+listen\s+\*:45;', }, { :title => 'should set the IPv4 listen options', :attr => 'listen_options', :value => 'spdy default', - :match => ' listen *:80 spdy default;', + :match => %r'\s+listen\s+\*:80 spdy default;', }, { :title => 'should enable IPv6', :attr => 'ipv6_enable', :value => true, - :match => ' listen [::]:80 default ipv6only=on;', + :match => %r'\s+listen\s+\[::\]:80 default ipv6only=on;', }, { :title => 'should not enable IPv6', :attr => 'ipv6_enable', :value => false, - :notmatch => / listen \[::\]:80 default ipv6only=on;/, + :notmatch => %r'\slisten \[::\]:80 default ipv6only=on;', }, { :title => 'should set the IPv6 listen IP', :attr => 'ipv6_listen_ip', :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - :match => ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80 default ipv6only=on;', + :match => %r'\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:80 default ipv6only=on;', }, { :title => 'should set the IPv6 listen port', :attr => 'ipv6_listen_port', :value => 45, - :match => ' listen [::]:45 default ipv6only=on;', + :match => %r'\s+listen\s+\[::\]:45 default ipv6only=on;', }, { :title => 'should set the IPv6 listen options', :attr => 'ipv6_listen_options', :value => 'spdy', - :match => ' listen [::]:80 spdy;', + :match => %r'\s+listen\s+\[::\]:80 spdy;', }, { :title => 'should set servername(s)', :attr => 'server_name', :value => ['www.foo.com','foo.com'], - :match => ' server_name www.foo.com foo.com;', + :match => %r'\s+server_name\s+www.foo.com foo.com;', }, { :title => 'should rewrite www servername to non-www', :attr => 'rewrite_www_to_non_www', :value => true, - :match => ' server_name rspec.example.com;', + :match => %r'\s+server_name\s+rspec.example.com;', }, { :title => 'should not rewrite www servername to non-www', :attr => 'rewrite_www_to_non_www', :value => false, - :match => ' server_name www.rspec.example.com;', + :match => %r'\s+server_name\s+www.rspec.example.com;', }, { :title => 'should set auth_basic', :attr => 'auth_basic', :value => 'value', - :match => ' auth_basic "value";', + :match => %r'\s+auth_basic\s+"value";', }, { :title => 'should set auth_basic_user_file', :attr => 'auth_basic_user_file', :value => 'value', - :match => ' auth_basic_user_file value;', + :match => %r'\s+auth_basic_user_file\s+value;', }, { :title => 'should set the client_body_timeout', :attr => 'client_body_timeout', :value => 'value', - :match => /^[ ]+client_body_timeout\s+value;/ + :match => /^\s+client_body_timeout\s+value;/ }, { :title => 'should set the client_header_timeout', :attr => 'client_header_timeout', :value => 'value', - :match => /^[ ]+client_header_timeout\s+value;/ + :match => /^\s+client_header_timeout\s+value;/ }, { :title => 'should set the gzip_types', :attr => 'gzip_types', :value => 'value', - :match => /^[ ]+gzip_types\s+value;/ + :match => /^\s+gzip_types\s+value;/ }, { :title => 'should contain raw_prepend directives', @@ -222,8 +223,8 @@ describe 'nginx::resource::vhost' do :attr => 'rewrite_to_https', :value => false, :notmatch => [ - /if \(\$ssl_protocol = ""\) \{/, - / return 301 https:\/\/\$host\$request_uri;/, + %r'if \(\$ssl_protocol = ""\) \{', + %r'\s+return 301 https://\$host\$request_uri;', ], }, { @@ -278,8 +279,8 @@ describe 'nginx::resource::vhost' do :attr => 'include_files', :value => [ '/file1', '/file2' ], :match => [ - %r'^[ ]+include\s+/file1;', - %r'^[ ]+include\s+/file2;', + %r'^\s+include\s+/file1;', + %r'^\s+include\s+/file2;', ], }, { @@ -342,47 +343,48 @@ describe 'nginx::resource::vhost' do :title => 'should contain www to non-www rewrite', :attr => 'rewrite_www_to_non_www', :value => true, - :match => [ - ' listen *:443 ssl;', - ' server_name www.rspec.example.com;', - ' return 301 https://rspec.example.com$uri;', - ], + :match => %r| + ^ + \s+listen\s+\*:443\s+ssl;\n + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+https://rspec\.example\.com\$uri; + |x, }, { :title => 'should set the IPv4 listen IP', :attr => 'listen_ip', :value => '127.0.0.1', - :match => ' listen 127.0.0.1:443 ssl;', + :match => %r'\s+listen\s+127.0.0.1:443 ssl;', }, { :title => 'should set the IPv4 SSL listen port', :attr => 'ssl_port', :value => 45, - :match => ' listen *:45 ssl;', + :match => %r'\s+listen\s+\*:45 ssl;', }, { :title => 'should set SPDY', :attr => 'spdy', :value => 'on', - :match => ' listen *:443 ssl spdy;', + :match => %r'\s+listen\s+\*:443 ssl spdy;', }, { :title => 'should not set SPDY', :attr => 'spdy', :value => 'off', - :match => ' listen *:443 ssl;', + :match => %r'\s+listen\s+\*:443 ssl;', }, { :title => 'should set the IPv4 listen options', :attr => 'listen_options', :value => 'default', - :match => ' listen *:443 ssl default;', + :match => %r'\s+listen\s+\*:443 ssl default;', }, { :title => 'should enable IPv6', :attr => 'ipv6_enable', :value => true, - :match => ' listen [::]:443 ssl default ipv6only=on;', + :match => %r'\s+listen\s+\[::\]:443 ssl default ipv6only=on;', }, { :title => 'should disable IPv6', @@ -394,85 +396,85 @@ describe 'nginx::resource::vhost' do :title => 'should set the IPv6 listen IP', :attr => 'ipv6_listen_ip', :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - :match => ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:443 ssl default ipv6only=on;', + :match => %r'\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:443 ssl default ipv6only=on;', }, { :title => 'should set the IPv6 listen port', :attr => 'ssl_port', :value => 45, - :match => ' listen [::]:45 ssl default ipv6only=on;', + :match => %r'\s+listen\s+\[::\]:45 ssl default ipv6only=on;', }, { :title => 'should set the IPv6 listen options', :attr => 'ipv6_listen_options', :value => 'spdy default', - :match => ' listen [::]:443 ssl spdy default;', + :match => %r'\s+listen\s+\[::\]:443 ssl spdy default;', }, { :title => 'should set servername(s)', :attr => 'server_name', :value => ['www.foo.com','foo.com'], - :match => ' server_name www.foo.com foo.com;', + :match => %r'\s+server_name\s+www.foo.com foo.com;', }, { :title => 'should rewrite www servername to non-www', :attr => 'rewrite_www_to_non_www', :value => true, - :match => ' server_name rspec.example.com;', + :match => %r'\s+server_name\s+rspec.example.com;', }, { :title => 'should not rewrite www servername to non-www', :attr => 'rewrite_www_to_non_www', :value => false, - :match => ' server_name www.rspec.example.com;', + :match => %r'\s+server_name\s+www.rspec.example.com;', }, { :title => 'should set the SSL cache', :attr => 'ssl_cache', :value => 'shared:SSL:1m', - :match => ' ssl_session_cache shared:SSL:1m;', + :match => %r'\s+ssl_session_cache\s+shared:SSL:1m;', }, { :title => 'should set the SSL protocols', :attr => 'ssl_protocols', :value => 'SSLv3', - :match => ' ssl_protocols SSLv3;', + :match => %r'\s+ssl_protocols\s+SSLv3;', }, { :title => 'should set the SSL ciphers', :attr => 'ssl_ciphers', :value => 'HIGH', - :match => ' ssl_ciphers HIGH;', + :match => %r'\s+ssl_ciphers\s+HIGH;', }, { :title => 'should set auth_basic', :attr => 'auth_basic', :value => 'value', - :match => ' auth_basic "value";', + :match => %r'\s+auth_basic\s+"value";', }, { :title => 'should set auth_basic_user_file', :attr => 'auth_basic_user_file', :value => 'value', - :match => ' auth_basic_user_file "value";', + :match => %r'\s+auth_basic_user_file\s+"value";', }, { :title => 'should set the client_body_timeout', :attr => 'client_body_timeout', :value => 'value', - :match => /^[ ]+client_body_timeout\s+value;/ + :match => /^\s+client_body_timeout\s+value;/ }, { :title => 'should set the client_header_timeout', :attr => 'client_header_timeout', :value => 'value', - :match => /^[ ]+client_header_timeout\s+value;/ + :match => /^\s+client_header_timeout\s+value;/ }, { :title => 'should set the gzip_types', :attr => 'gzip_types', :value => 'value', - :match => /^[ ]+gzip_types\s+value;/ + :match => /^\s+gzip_types\s+value;/ }, { :title => 'should set access_log', @@ -574,8 +576,8 @@ describe 'nginx::resource::vhost' do :attr => 'include_files', :value => [ '/file1', '/file2' ], :match => [ - %r'^[ ]+include\s+/file1;', - %r'^[ ]+include\s+/file2;', + %r'^\s+include\s+/file1;', + %r'^\s+include\s+/file2;', ], }, { @@ -651,7 +653,7 @@ describe 'nginx::resource::vhost' do end it "should set the server_name of the rewrite server stanza to the first server_name with 'www.' stripped" do - is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(/^[ ]+server_name\s+foo.com;/) + is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(/^\s+server_name\s+foo.com;/) end end @@ -666,7 +668,7 @@ describe 'nginx::resource::vhost' do end it "should set the server_name of the rewrite server stanza to the first server_name with 'www.' stripped" do - is_expected.to contain_concat__fragment("#{title}-header").with_content(/^[ ]+server_name\s+foo.com;/) + is_expected.to contain_concat__fragment("#{title}-header").with_content(/^\s+server_name\s+foo.com;/) end end @@ -780,8 +782,8 @@ describe 'nginx::resource::vhost' do }) end it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log[ ]+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log[ ]+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) } it { is_expected.to contain_concat__fragment("#{title}-ssl-footer") } it { is_expected.to contain_file("/etc/nginx/#{title}.crt") } it { is_expected.to contain_file("/etc/nginx/#{title}.key") } diff --git a/templates/conf.d/geo.erb b/templates/conf.d/geo.erb index 0b6da7d..677d28c 100644 --- a/templates/conf.d/geo.erb +++ b/templates/conf.d/geo.erb @@ -11,19 +11,22 @@ geo <%= @address ? "#{@address} " : '' %>$<%= @name %> { default <%= @default %>; <% end -%> <% if @delete -%> - delete <%= @delete %>; + delete <%= @delete %>; <% end -%> <% if @proxies -%> + + <%- if @proxy_recursive -%> + proxy_recursive; + <%- end -%> <%- [@proxies].flatten.each do |proxy| -%> proxy <%= proxy %>; <%- end -%> <% end -%> -<% if @proxy_recursive && @proxies -%> - proxy_recursive; -<% end -%> <% if @networks -%> + + <%- field_width = @networks.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%> <%- @networks.sort_by{|k,v| IPAddr.new(k.split('-').first).to_i }.each do |key,value| -%> - <%= key %> <%= value %>; + <%= sprintf("%-*s", field_width, key) %> <%= value %>; <%- end -%> <% end -%> } diff --git a/templates/conf.d/map.erb b/templates/conf.d/map.erb index 67fd5c5..b5d6b2e 100644 --- a/templates/conf.d/map.erb +++ b/templates/conf.d/map.erb @@ -5,9 +5,11 @@ map <%= @string %> $<%= @name %> { <% if @default -%> default <%= @default %>; <% end -%> + <% if @mappings -%> + <%- field_width = @mappings.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%> <%- @mappings.sort_by{|k,v| k}.each do |key,value| -%> - <%= key %> <%= value %>; + <%= sprintf("%-*s", field_width, key) %> <%= value %>; <%- end -%> <% end -%> } diff --git a/templates/conf.d/nginx.conf.erb b/templates/conf.d/nginx.conf.erb index 9c6d8b1..0fc5307 100644 --- a/templates/conf.d/nginx.conf.erb +++ b/templates/conf.d/nginx.conf.erb @@ -2,21 +2,23 @@ user <%= @daemon_user %>; <% end -%> worker_processes <%= @worker_processes %>; -<% if @worker_rlimit_nofile %>worker_rlimit_nofile <%= @worker_rlimit_nofile %>;<% end -%> +<% if @worker_rlimit_nofile -%> +worker_rlimit_nofile <%= @worker_rlimit_nofile %>; +<% end -%> -error_log <%= @nginx_error_log %>; <% if @pid -%> pid <%= @pid %>; <% end -%> +error_log <%= @nginx_error_log %>; events { worker_connections <%= @worker_connections -%>; - <% if @multi_accept == 'on' -%> + <%- if @multi_accept == 'on' -%> multi_accept on; - <% end -%> - <% if @events_use -%> + <%- end -%> + <%- if @events_use -%> use <%= @events_use %>; - <% end -%> + <%- end -%> } http { @@ -27,9 +29,9 @@ http { <% if @sendfile == 'on' -%> sendfile on; -<% if @http_tcp_nopush == 'on' -%> + <%- if @http_tcp_nopush == 'on' -%> tcp_nopush on; -<% end -%> + <%- end -%> <% end -%> server_tokens <%= @server_tokens %>; @@ -46,30 +48,31 @@ http { <% if @gzip == 'on' -%> gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; -<% end -%> +<% end -%> <% if @proxy_cache_path -%> - proxy_cache_path <%= @proxy_cache_path %> levels=<%= @proxy_cache_levels %> keys_zone=<%= @proxy_cache_keys_zone %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive %>; -<% end -%> + proxy_cache_path <%= @proxy_cache_path %> levels=<%= @proxy_cache_levels %> keys_zone=<%= @proxy_cache_keys_zone %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive %>; +<% end -%> <% if @fastcgi_cache_path -%> fastcgi_cache_path <%= @fastcgi_cache_path %> levels=<%= @fastcgi_cache_levels %> keys_zone=<%= @fastcgi_cache_keys_zone %> max_size=<%= @fastcgi_cache_max_size %> inactive=<%= @fastcgi_cache_inactive %>; <% end -%> <% if @fastcgi_cache_key -%> - fastcgi_cache_key <%= @fastcgi_cache_key %>; + fastcgi_cache_key <%= @fastcgi_cache_key %>; <% end -%> <% if @fastcgi_cache_use_stale -%> - fastcgi_cache_use_stale <%= @fastcgi_cache_use_stale %>; + fastcgi_cache_use_stale <%= @fastcgi_cache_use_stale %>; <% end -%> +<% if @http_cfg_append -%> -<% if @http_cfg_append -%><% @http_cfg_append.sort_by{|k,v| k}.each do |key,value| -%> - <%= key %> <%= value %>; -<% end -%> + <%- field_width = @http_cfg_append.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%> + <%- @http_cfg_append.sort_by{|k,v| k}.each do |key,value| -%> + <%= sprintf("%-*s", field_width, key) %> <%= value %>; + <%- end -%> <% end -%> include <%= @conf_dir %>/conf.d/*.conf; include <%= @conf_dir %>/sites-enabled/*; - } <% if @mail -%> mail { diff --git a/templates/vhost/location_footer.erb b/templates/vhost/location_footer.erb index 6ea0d19..3dc8db6 100644 --- a/templates/vhost/location_footer.erb +++ b/templates/vhost/location_footer.erb @@ -34,3 +34,4 @@ <%- end -%> <% end -%> } + diff --git a/templates/vhost/location_header.erb b/templates/vhost/location_header.erb index e94d411..59b3edf 100644 --- a/templates/vhost/location_header.erb +++ b/templates/vhost/location_header.erb @@ -48,8 +48,8 @@ <%- end -%> <%- end -%> <% end -%> -<% if @raw_prepend && Array(@raw_prepend).size > 0 %> +<% if @raw_prepend && Array(@raw_prepend).size > 0 -%> <%- Array(@raw_prepend).each do |line| -%> <%= line %> <%- end -%> -<% end %> +<% end -%> \ No newline at end of file diff --git a/templates/vhost/locations/alias.erb b/templates/vhost/locations/alias.erb index 4189541..a588efd 100644 --- a/templates/vhost/locations/alias.erb +++ b/templates/vhost/locations/alias.erb @@ -1,4 +1,4 @@ - alias <%= @location_alias %>; + alias <%= @location_alias %>; <% if defined? @autoindex -%> autoindex <%= @autoindex %>; <% end -%> diff --git a/templates/vhost/locations/directory.erb b/templates/vhost/locations/directory.erb index e06c748..5415a40 100644 --- a/templates/vhost/locations/directory.erb +++ b/templates/vhost/locations/directory.erb @@ -1,21 +1,28 @@ <% if defined? @www_root -%> - root <%= @www_root %>; + root <%= @www_root %>; +<% end -%> +<% if defined? @autoindex -%> + + autoindex <%= @autoindex %>; +<% end -%> +<% if @index_files.count > 0 -%> + index <% Array(@index_files).each do |i| %> <%= i %><% end %>; <% end -%> <% if @try_files -%> try_files<% @try_files.each do |try| -%> <%= try %><% end -%>; <% end -%> -<% if defined? @autoindex -%> - autoindex <%= @autoindex %>; -<% end -%> -<% if @index_files.count > 0 -%> - index <% Array(@index_files).each do |i| %> <%= i %><% end %>; -<% end -%> -<% @rewrite_rules.each do |rewrite_rule| -%> +<%- unless @rewrite_rules.nil? || @rewrite_rules.empty? -%> + + <%- @rewrite_rules.each do |rewrite_rule| -%> rewrite <%= rewrite_rule %>; + <%- end -%> <% end -%> -<% if defined? @auth_basic -%> +<% if instance_variables.any? { |iv| iv.to_s.include? 'auth_basic' } -%> + + <%- if @auth_basic -%> auth_basic "<%= @auth_basic %>"; -<% end -%> -<% if defined? @auth_basic_user_file -%> + <%- end -%> + <%- if defined? @auth_basic_user_file -%> auth_basic_user_file <%= @auth_basic_user_file %>; -<% end -%> + <%- end -%> +<% end -%> \ No newline at end of file diff --git a/templates/vhost/locations/fastcgi.erb b/templates/vhost/locations/fastcgi.erb index 1035d27..2dacdf0 100644 --- a/templates/vhost/locations/fastcgi.erb +++ b/templates/vhost/locations/fastcgi.erb @@ -1,20 +1,22 @@ <% if defined? @www_root -%> - root <%= @www_root %>; + root <%= @www_root %>; <% end -%> + include <%= @fastcgi_params %>; +<% if @try_files -%> + try_files <% @try_files.each do |try| -%> <%= try %><% end -%>; +<% end -%> + + fastcgi_pass <%= @fastcgi %>; <% if @fastcgi_split_path -%> fastcgi_split_path_info <%= @fastcgi_split_path %>; <% end -%> -<% if @try_files -%> - try_files<% @try_files.each do |try| -%> <%= try %><% end -%>; +<% if defined? @fastcgi_script -%> + <%-# this setting can be overridden by setting it in the fastcgi_param hash too %> + <%- @fastcgi_param = { 'SCRIPT_FILENAME' => @fastcgi_script }.merge(@fastcgi_param || {}) -%> <% end -%> - include <%= @fastcgi_params %>; - fastcgi_pass <%= @fastcgi %>; -<% if defined? @fastcgi_script %> - fastcgi_param SCRIPT_FILENAME <%= @fastcgi_script %>; +<% if defined? @fastcgi_param -%> + <%- field_width = @fastcgi_param.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%> + <%- @fastcgi_param.each do |key, val| -%> + fastcgi_param <%= sprintf("%-*s", field_width, key) %> <%= val %>; + <%- end -%> <% end -%> -<% if defined? @fastcgi_param %> - # Enable custom fastcgi_params - <% @fastcgi_param.each_pair do |key, val| -%> - fastcgi_param <%= key %> <%= val %>; - <% end -%> -<% end %> diff --git a/templates/vhost/locations/proxy.erb b/templates/vhost/locations/proxy.erb index 097f6cc..be9ae08 100644 --- a/templates/vhost/locations/proxy.erb +++ b/templates/vhost/locations/proxy.erb @@ -1,28 +1,38 @@ -<% if @proxy_cache -%> - proxy_cache <%= @proxy_cache %>; -<% end -%> -<% if @proxy_cache_valid -%> - proxy_cache_valid <%= @proxy_cache_valid %>; -<% end -%> - proxy_pass <%= @proxy %>; - proxy_read_timeout <%= @proxy_read_timeout %>; - proxy_connect_timeout <%= @proxy_connect_timeout %>; - proxy_redirect <%= @proxy_redirect %>; -<% @proxy_set_header.each do |header| -%> - proxy_set_header <%= header %>; -<% end -%> + proxy_pass <%= @proxy %>; + proxy_read_timeout <%= @proxy_read_timeout %>; + proxy_connect_timeout <%= @proxy_connect_timeout %>; + proxy_redirect <%= @proxy_redirect %>; <% if @proxy_method -%> - proxy_method <%= @proxy_method %>; + proxy_method <%= @proxy_method %>; <% end -%> <% if @proxy_set_body -%> - proxy_set_body <%= @proxy_set_body %>; + proxy_set_body <%= @proxy_set_body %>; <% end -%> -<% @rewrite_rules.each do |rewrite_rule| -%> +<% unless @proxy_set_header.nil? -%> + + <%- @proxy_set_header.each do |header| -%> + proxy_set_header <%= header %>; + <%- end -%> + +<% end -%> +<% if @proxy_cache -%> + proxy_cache <%= @proxy_cache %>; +<% end -%> +<% if @proxy_cache_valid -%> + proxy_cache_valid <%= @proxy_cache_valid %>; +<% end -%> +<%- unless @rewrite_rules.nil? || @rewrite_rules.empty? -%> + + <%- @rewrite_rules.each do |rewrite_rule| -%> rewrite <%= rewrite_rule %>; + <%- end -%> <% end -%> -<% if defined? @auth_basic -%> +<% if instance_variables.any? { |iv| iv.to_s.include? 'auth_basic' } -%> + + <%- if @auth_basic -%> auth_basic "<%= @auth_basic %>"; -<% end -%> -<% if defined? @auth_basic_user_file -%> + <%- end -%> + <%- if defined? @auth_basic_user_file -%> auth_basic_user_file <%= @auth_basic_user_file %>; + <%- end -%> <% end -%> diff --git a/templates/vhost/vhost_header.erb b/templates/vhost/vhost_header.erb index 307b032..eef9f66 100644 --- a/templates/vhost/vhost_header.erb +++ b/templates/vhost/vhost_header.erb @@ -8,25 +8,31 @@ server { <% end -%> server { listen <%= @listen_ip %>:<%= @listen_port %><% if @listen_options %> <%= @listen_options %><% end %>; -<% # check to see if ipv6 support exists in the kernel before applying %> -<% if @ipv6_enable && (defined? @ipaddress6) %> +<%# check to see if ipv6 support exists in the kernel before applying -%> +<% if @ipv6_enable && (defined? @ipaddress6) -%> listen [<%= @ipv6_listen_ip %>]:<%= @ipv6_listen_port %> <% if @ipv6_listen_options %><%= @ipv6_listen_options %><% end %>; -<% end %> +<% end -%> server_name <%= @rewrite_www_to_non_www ? @server_name[0].gsub(/^www\./, '') : @server_name.join(" ") %>; -<% if defined? @auth_basic -%> +<%- if instance_variables.any? { |iv| iv.to_s.include? 'auth_basic' } -%> + <% if defined? @auth_basic -%> auth_basic "<%= @auth_basic %>"; -<% end -%> -<% if defined? @auth_basic_user_file -%> + <%- end -%> + <%- if defined? @auth_basic_user_file -%> auth_basic_user_file <%= @auth_basic_user_file %>; + <%- end -%> <% end -%> -<% if defined? @client_body_timeout -%> +<% if instance_variables.any? { |iv| iv.to_s.include? 'client_' } -%> + + <%- if defined? @client_body_timeout -%> client_body_timeout <%= @client_body_timeout %>; -<% end -%> -<% if defined? @client_header_timeout -%> + <%- end -%> + <%- if defined? @client_header_timeout -%> client_header_timeout <%= @client_header_timeout %>; -<% end -%> -<% if defined? @client_max_body_size -%> + <%- end -%> + <%- if defined? @client_max_body_size -%> client_max_body_size <%= @client_max_body_size %>; + <%- end -%> + <% end -%> <% if defined? @gzip_types -%> gzip_types <%= @gzip_types %>; @@ -60,7 +66,7 @@ server { passenger_set_cgi_param <%= key %> <%= @passenger_cgi_param[key] %>; <%- end -%> <% end -%> -<% if @resolver.count > 0 -%> +<% if Array(@resolver).count > 0 -%> resolver <% Array(@resolver).each do |r| %> <%= r %><% end %>; <% end -%> <% @proxy_set_header.each do |header| -%> @@ -87,5 +93,4 @@ server { <% end -%> access_log <%= @access_log_real %>; - error_log <%= @error_log_real %>; - + error_log <%= @error_log_real %>; \ No newline at end of file diff --git a/templates/vhost/vhost_ssl_header.erb b/templates/vhost/vhost_ssl_header.erb index c44161f..b3c10c5 100644 --- a/templates/vhost/vhost_ssl_header.erb +++ b/templates/vhost/vhost_ssl_header.erb @@ -5,12 +5,12 @@ server { return 301 https://<%= @server_name[0].gsub(/^www\./, '') %>$uri; } -<% end %> +<% end -%> server { listen <%= @listen_ip %>:<%= @ssl_port %> <% if @ssl_listen_option %>ssl<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_options %> <%= @listen_options %><% end %>; - <%- if @ipv6_enable && (defined? @ipaddress6) %> + <%- if @ipv6_enable && (defined? @ipaddress6) -%> listen [<%= @ipv6_listen_ip %>]:<%= @ssl_port %> ssl<% if @spdy == 'on' %> spdy<% end %><% if @ipv6_listen_options %> <%= @ipv6_listen_options %><% end %>; - <%- end %> + <%- end -%> server_name <%= @rewrite_www_to_non_www ? @server_name[0].gsub(/^www\./, '') : @server_name.join(" ") %>; ssl on; @@ -25,44 +25,53 @@ server { ssl_protocols <%= @ssl_protocols %>; ssl_ciphers <%= @ssl_ciphers %>; ssl_prefer_server_ciphers on; -<% if @ssl_stapling -%> +<%- if instance_variables.any? { |iv| iv.to_s.include? 'ssl_' } -%> + <%- if @ssl_stapling -%> ssl_stapling on; -<% end -%> -<% if defined? @ssl_stapling_file -%> + <%- end -%> + <%- if defined? @ssl_stapling_file -%> ssl_stapling_file <%= scope.lookupvar('nginx::config::conf_dir') %>/<%= @name.gsub(' ', '_') %>.ocsp.resp; -<% end -%> -<% if defined? @ssl_stapling_responder -%> + <%- end -%> + <%- if defined? @ssl_stapling_responder -%> ssl_stapling_responder <%= @ssl_stapling_responder %>; -<% end -%> -<% if @ssl_stapling_verify -%> + <%- end -%> + <%- if @ssl_stapling_verify -%> ssl_stapling_verify on; -<% end -%> -<% if defined? @ssl_trusted_cert -%> + <%- end -%> + <%- if defined? @ssl_trusted_cert -%> ssl_trusted_certificate <%= scope.lookupvar('nginx::config::conf_dir') %>/<%= @name.gsub(' ', '_') %>.trusted.crt; + <%- end -%> + <% end -%> -<% if @resolver.count > 0 -%> +<% if Array(@resolver).count > 0 -%> resolver <% Array(@resolver).each do |r| %> <%= r %><% end %>; <% end -%> -<% if defined? @auth_basic -%> +<%- if instance_variables.any? { |iv| iv.to_s.include? 'auth_basic' } -%> + <% if defined? @auth_basic -%> auth_basic "<%= @auth_basic %>"; -<% end -%> -<% if defined? @auth_basic_user_file -%> + <% end -%> + <% if defined? @auth_basic_user_file -%> auth_basic_user_file "<%= @auth_basic_user_file %>"; -<% end -%> -<% if defined? @client_body_timeout -%> + <% end -%> +<%- end -%> +<%- if instance_variables.any? { |iv| iv.to_s.include? 'client_' } -%> + + <%- if defined? @client_body_timeout -%> client_body_timeout <%= @client_body_timeout %>; -<% end -%> -<% if defined? @client_header_timeout -%> + <%- end -%> + <%- if defined? @client_header_timeout -%> client_header_timeout <%= @client_header_timeout %>; -<% end -%> -<% if defined? @client_max_body_size -%> + <%- end -%> + <%- if defined? @client_max_body_size -%> client_max_body_size <%= @client_max_body_size %>; + <%- end -%> + <% end -%> <% if defined? @gzip_types -%> gzip_types <%= @gzip_types %>; <% end -%> <% if @index_files.count > 0 -%> - index <% Array(@index_files).each do |i| %> <%= i %><% end %>; + index <% Array(@index_files).each do |i| %> <%= i %><% end %>; <% end -%> access_log <%= @ssl_access_log_real %>; @@ -100,7 +109,7 @@ server { <% end -%> <% Array(@raw_prepend).each do |line| -%> <%= line %> -<% end %> +<% end -%> <% if @root -%> root <%= @root %>; <% end -%>