From 2778df07fbdcc65485f580d9a092ec121b1a87d8 Mon Sep 17 00:00:00 2001 From: Marin Vintila Date: Tue, 24 Jun 2014 19:17:08 +0300 Subject: [PATCH 1/5] Add uwsgi support --- manifests/resource/location.pp | 25 ++++++++++++++++++++++--- manifests/resource/vhost.pp | 16 ++++++++++++++++ templates/vhost/locations/uwsgi.erb | 8 ++++++++ templates/vhost/uwsgi_params.erb | 15 +++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 templates/vhost/locations/uwsgi.erb create mode 100644 templates/vhost/uwsgi_params.erb diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index 876cb37cf..b5ccc17f6 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -33,6 +33,8 @@ # [*fastcgi_script*] - optional SCRIPT_FILE parameter # [*fastcgi_split_path*] - Allows settings of fastcgi_split_path_info so # that you can split the script_name and path_info via regex +# [*uwsgi*] - location of uwsgi (host:port) +# [*uwsgi_params*] - optional alternative uwsgi_params file to use # [*ssl*] - Indicates whether to setup SSL bindings for # this location. # [*ssl_only*] - Required if the SSL and normal vHost have the @@ -119,6 +121,8 @@ $fastcgi_params = "${nginx::config::conf_dir}/fastcgi_params", $fastcgi_script = undef, $fastcgi_split_path = undef, + $uwsgi = undef, + $uwsgi_params = "${nginx::config::conf_dir}/uwsgi_params", $ssl = false, $ssl_only = false, $location_alias = undef, @@ -178,6 +182,10 @@ if ($fastcgi_split_path != undef) { validate_string($fastcgi_split_path) } + if ($fastcgi != undef) { + validate_string($fastcgi) + } + validate_string($fastcgi_params) validate_bool($ssl) validate_bool($ssl_only) if ($location_alias != undef) { @@ -249,14 +257,14 @@ if ($vhost == undef) { fail('Cannot create a location reference without attaching to a virtual host') } - if (($www_root == undef) and ($proxy == undef) and ($location_alias == undef) and ($stub_status == undef) and ($fastcgi == undef) and ($location_custom_cfg == undef)) { - fail('Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, stub_status, or location_custom_cfg defined') + if (($www_root == undef) and ($proxy == undef) and ($location_alias == undef) and ($stub_status == undef) and ($fastcgi == undef) and ($uwsgi == undef) and ($location_custom_cfg == undef)) { + fail('Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, or location_custom_cfg defined') } if (($www_root != undef) and ($proxy != undef)) { fail('Cannot define both directory and proxy in a virtual host') } - # Use proxy or fastcgi template if $proxy is defined, otherwise use directory template. + # Use proxy, fastcgi or uwsgi template if $proxy is defined, otherwise use directory template. if ($proxy != undef) { $content_real = template('nginx/vhost/locations/proxy.erb') } elsif ($location_alias != undef) { @@ -265,6 +273,8 @@ $content_real = template('nginx/vhost/locations/stub_status.erb') } elsif ($fastcgi != undef) { $content_real = template('nginx/vhost/locations/fastcgi.erb') + } elsif ($wusgi != undef) { + $content_real = template('nginx/vhost/locations/wusgi.erb') } elsif ($www_root != undef) { $content_real = template('nginx/vhost/locations/directory.erb') } else { @@ -279,6 +289,15 @@ } } + if $uwsgi != undef and !defined(File[$wusgi_params]) { + file { $wusgi_params: + ensure => present, + mode => '0770', + content => template('nginx/vhost/uwsgi_params.erb'), + } + } + + ## Create stubs for vHost File Fragment Pattern if ($ssl_only != true) { $tmpFile=md5("${vhost_sanitized}-${priority}-${location_sanitized}") diff --git a/manifests/resource/vhost.pp b/manifests/resource/vhost.pp index 785ff183e..6344f59e0 100644 --- a/manifests/resource/vhost.pp +++ b/manifests/resource/vhost.pp @@ -165,6 +165,8 @@ $fastcgi = undef, $fastcgi_params = "${nginx::config::conf_dir}/fastcgi_params", $fastcgi_script = undef, + $uwsgi = undef, + $uwsgi_params = "${nginx::config::conf_dir}/uwsgi_params", $index_files = [ 'index.html', 'index.htm', @@ -275,6 +277,10 @@ if ($fastcgi_script != undef) { validate_string($fastcgi_script) } + if ($uwsgi != undef) { + validate_string($uwsgi) + } + validate_string($wusgi_params) validate_array($index_files) if ($autoindex != undef) { validate_string($autoindex) @@ -424,6 +430,8 @@ fastcgi => $fastcgi, fastcgi_params => $fastcgi_params, fastcgi_script => $fastcgi_script, + uwsgi => $uwsgi, + uwsgi_params => $uwsgi_params, try_files => $try_files, www_root => $www_root, autoindex => $autoindex, @@ -466,6 +474,14 @@ } } + if $wusgi != undef and !defined(File[$wusgi_params]) { + file { $uwsgi_params: + ensure => present, + mode => '0770', + content => template('nginx/vhost/wusgi_params.erb'), + } + } + if ($listen_port != $ssl_port) { concat::fragment { "${name_sanitized}-header": ensure => present, diff --git a/templates/vhost/locations/uwsgi.erb b/templates/vhost/locations/uwsgi.erb new file mode 100644 index 000000000..a20cc4685 --- /dev/null +++ b/templates/vhost/locations/uwsgi.erb @@ -0,0 +1,8 @@ +<% if defined? @www_root -%> + root <%= @www_root %>; +<% end -%> +<% if @try_files -%> + try_files<% @try_files.each do |try| -%> <%= try %><% end -%>; +<% end -%> + include <%= @uwsgi_params %>; + uwsgi_pass <%= @uwsgi %>; diff --git a/templates/vhost/uwsgi_params.erb b/templates/vhost/uwsgi_params.erb new file mode 100644 index 000000000..86b9a2de7 --- /dev/null +++ b/templates/vhost/uwsgi_params.erb @@ -0,0 +1,15 @@ +# This file managed by puppet on host <%= @fqdn %> + +uwsgi_param QUERY_STRING $query_string; +uwsgi_param REQUEST_METHOD $request_method; +uwsgi_param CONTENT_TYPE $content_type; +uwsgi_param CONTENT_LENGTH $content_length; +uwsgi_param REQUEST_URI $request_uri; +uwsgi_param PATH_INFO $document_uri; +uwsgi_param DOCUMENT_ROOT $document_root; +uwsgi_param SERVER_PROTOCOL $server_protocol; +uwsgi_param REMOTE_ADDR $remote_addr; +uwsgi_param REMOTE_PORT $remote_port; +uwsgi_param SERVER_ADDR $server_addr; +uwsgi_param SERVER_PORT $server_port; +uwsgi_param SERVER_NAME $server_name; From 2e7e059c06793601e5c56f881d42da15337d98d2 Mon Sep 17 00:00:00 2001 From: Marin Vintila Date: Tue, 24 Jun 2014 20:05:10 +0300 Subject: [PATCH 2/5] Fix typos --- manifests/resource/location.pp | 8 ++++---- manifests/resource/vhost.pp | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index b5ccc17f6..e10c61da6 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -273,8 +273,8 @@ $content_real = template('nginx/vhost/locations/stub_status.erb') } elsif ($fastcgi != undef) { $content_real = template('nginx/vhost/locations/fastcgi.erb') - } elsif ($wusgi != undef) { - $content_real = template('nginx/vhost/locations/wusgi.erb') + } elsif ($uwsgi != undef) { + $content_real = template('nginx/vhost/locations/uwsgi.erb') } elsif ($www_root != undef) { $content_real = template('nginx/vhost/locations/directory.erb') } else { @@ -289,8 +289,8 @@ } } - if $uwsgi != undef and !defined(File[$wusgi_params]) { - file { $wusgi_params: + if $uwsgi != undef and !defined(File[$uwsgi_params]) { + file { $uwsgi_params: ensure => present, mode => '0770', content => template('nginx/vhost/uwsgi_params.erb'), diff --git a/manifests/resource/vhost.pp b/manifests/resource/vhost.pp index 6344f59e0..8a0b8ddb0 100644 --- a/manifests/resource/vhost.pp +++ b/manifests/resource/vhost.pp @@ -280,7 +280,7 @@ if ($uwsgi != undef) { validate_string($uwsgi) } - validate_string($wusgi_params) + validate_string($uwsgi_params) validate_array($index_files) if ($autoindex != undef) { validate_string($autoindex) @@ -474,11 +474,11 @@ } } - if $wusgi != undef and !defined(File[$wusgi_params]) { + if $uwsgi != undef and !defined(File[$uwsgi_params]) { file { $uwsgi_params: ensure => present, mode => '0770', - content => template('nginx/vhost/wusgi_params.erb'), + content => template('nginx/vhost/uwsgi_params.erb'), } } From ca717f2c6cea1ea32f1ae3e39e394dceed322e18 Mon Sep 17 00:00:00 2001 From: Marin Vintila Date: Wed, 13 Aug 2014 17:18:56 +0300 Subject: [PATCH 3/5] Add tests for uwsgi --- spec/defines/resource_location_spec.rb | 68 +++++++++++++++++++++++++- spec/defines/resource_vhost_spec.rb | 10 ++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 019f20890..11355ce22 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -30,6 +30,7 @@ it { should contain_class("nginx::config") } it { should contain_concat__fragment("f25e14942fb58942ee13b1465a4e1719").with_content(/location rspec-test/) } it { should_not contain_file('/etc/nginx/fastcgi_params') } + it { should_not contain_file('/etc/nginx/uwsgi_params') } it { should_not contain_concat__fragment("vhost1-800-rspec-test-ssl") } it { should_not contain_file("/etc/nginx/rspec-test_htpasswd") } end @@ -472,6 +473,64 @@ end end + describe "vhost_location_uwsgi template content" do + let :default_params do + { + :location => 'location', + :uwsgi => 'uwsgi_upstream', + :vhost => 'vhost1' + } + end + + [ + { + :title => 'should set www_root', + :attr => 'www_root', + :value => '/', + :match => ' root /;' + }, + { + :title => 'should set try_file(s)', + :attr => 'try_files', + :value => ['name1','name2'], + :match => ' try_files name1 name2;', + }, + { + :title => 'should set uwsgi_params', + :attr => 'uwsgi_params', + :value => 'value', + :match => /^[ ]+include\s+value;/ + }, + { + :title => 'should set uwsgi_pass', + :attr => 'uwsgi', + :value => 'value', + :match => ' uwsgi_pass value;' + }, + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end + + it { should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) } + it param[:title] do + fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}") + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| should contain_concat__fragment(fragment).with_content(item) } + else + lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + (lines & matches).should == matches + end + + Array(param[:notmatch]).each do |item| + should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item) + end + end + end + end + end + describe "vhost_location_proxy template content" do [ { @@ -603,6 +662,13 @@ it { should contain_file('/etc/nginx/fastcgi_params').with_mode('0770') } end + context 'when uwsgi => "uwsgi_upstream"' do + let :params do { :fastcgi => 'uwsgi_upstream', :vhost => 'vhost1' } end + + it { should contain_file('/etc/nginx/uwsgi_params').with_mode('0770') } + end + + context 'when ssl_only => true' do let :params do { :ssl_only => true, :vhost => 'vhost1', :www_root => '/', } end it { should_not contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-rspec-test")) } @@ -657,7 +723,7 @@ :vhost => 'vhost1', } end - it { expect { should contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, stub_status, or location_custom_cfg defined/) } + it { expect { should contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, or location_custom_cfg defined/) } end context "www_root and proxy are set" do diff --git a/spec/defines/resource_vhost_spec.rb b/spec/defines/resource_vhost_spec.rb index 81568e905..3f379a30c 100644 --- a/spec/defines/resource_vhost_spec.rb +++ b/spec/defines/resource_vhost_spec.rb @@ -40,6 +40,7 @@ it { should contain_concat__fragment("#{title}-footer") } it { should contain_nginx__resource__location("#{title}-default") } it { should_not contain_file("/etc/nginx/fastcgi_params") } + it { should_not contain_file("/etc/nginx/uwsgi_params") } it { should contain_file("#{title}.conf symlink").with({ 'ensure' => 'link', 'path' => "/etc/nginx/sites-enabled/#{title}.conf", @@ -738,6 +739,15 @@ it { should contain_file('/etc/nginx/fastcgi_params').with_mode('0770') } end + context 'when uwsgi => "uwsgi_upstream"' do + let :params do default_params.merge({ + :uwsgi => 'uwsgi_upstream', + }) end + + it { should contain_file('/etc/nginx/uwsgi_params').with_mode('0770') } + end + + context 'when listen_port == ssl_port' do let :params do default_params.merge({ :listen_port => 80, From fc72e506a469dbf3809f40716e482bc0327c407d Mon Sep 17 00:00:00 2001 From: Marin Vintila Date: Fri, 8 May 2015 12:22:31 +0300 Subject: [PATCH 4/5] Fix diff lines leftover from merge --- spec/defines/resource_location_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 8d754d204..1d1cc679c 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -743,9 +743,7 @@ :vhost => 'vhost1', } end - it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, or location_custom_cfg defined/) } - it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, stub_status, internal, or location_custom_cfg defined/) } ->>>>>>> 7c4473857369fdf09aa24a97e3ccc180ef3b4c73 + it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, internal, or location_custom_cfg defined/) } end context "www_root and proxy are set" do From 96cd9887dba6be177668afbdfb374e02f6b03e3a Mon Sep 17 00:00:00 2001 From: Marin Vintila Date: Fri, 8 May 2015 18:00:35 +0300 Subject: [PATCH 5/5] Fix uwsgi related tests --- manifests/resource/location.pp | 6 +++--- spec/defines/resource_location_spec.rb | 27 +++++++++++++------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index 356b8c141..3b5313a72 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -310,8 +310,8 @@ if ($vhost == undef) { fail('Cannot create a location reference without attaching to a virtual host') } - if (($www_root == undef) and ($proxy == undef) and ($location_alias == undef) and ($stub_status == undef) and ($fastcgi == undef) and ($uwsgi == undef) and ($location_custom_cfg == undef)) { - fail('Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, or location_custom_cfg defined') + if (($www_root == undef) and ($proxy == undef) and ($location_alias == undef) and ($stub_status == undef) and ($fastcgi == undef) and ($uwsgi == undef) and ($location_custom_cfg == undef) and ($internal == false)) { + fail('Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, internal, or location_custom_cfg defined') } if (($www_root != undef) and ($proxy != undef)) { fail('Cannot define both directory and proxy in a virtual host') @@ -354,7 +354,7 @@ } } - if $uwsgi != undef and !defined(File[$uwsgi_params]) { + if $ensure == present and $uwsgi != undef and !defined(File[$uwsgi_params]) { file { $uwsgi_params: ensure => present, mode => '0770', diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 1d1cc679c..20a246dc0 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -523,7 +523,7 @@ let :default_params do { :location => 'location', - :uwsgi => 'uwsgi_upstream', + :uwsgi => 'unix:/home/project/uwsgi.socket', :vhost => 'vhost1' } end @@ -533,50 +533,51 @@ :title => 'should set www_root', :attr => 'www_root', :value => '/', - :match => ' root /;' + :match => %r'\s+root\s+/;' }, { :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 uwsgi_params', :attr => 'uwsgi_params', :value => 'value', - :match => /^[ ]+include\s+value;/ + :match => %r'\s+include\s+value;' }, { :title => 'should set uwsgi_pass', :attr => 'uwsgi', :value => 'value', - :match => ' uwsgi_pass value;' + :match => %r'\s+uwsgi_pass\s+value;' }, ].each do |param| context "when #{param[:attr]} is #{param[:value]}" do let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end - it { should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) } + it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) } it param[:title] do fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}") matches = Array(param[:match]) if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| should contain_concat__fragment(fragment).with_content(item) } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } else - lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - (lines & matches).should == matches + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) end Array(param[:notmatch]).each do |item| - should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item) + is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item) end end end end end + describe "vhost_location_proxy template content" do [ { @@ -700,10 +701,10 @@ it { is_expected.to contain_file('/etc/nginx/fastcgi_params').with_mode('0770') } end - context 'when uwsgi => "uwsgi_upstream"' do - let :params do { :fastcgi => 'uwsgi_upstream', :vhost => 'vhost1' } end + context 'when uwsgi => "unix:/home/project/uwsgi.socket"' do + let :params do { :uwsgi => 'uwsgi_upstream', :vhost => 'vhost1' } end - it { should contain_file('/etc/nginx/uwsgi_params').with_mode('0770') } + it { should contain_file('/etc/nginx/uwsgi_params') } end