Skip to content

Commit

Permalink
Merge pull request voxpupuli#398 from syscollective/master
Browse files Browse the repository at this point in the history
Add uwsgi support
  • Loading branch information
jfryman committed May 8, 2015
2 parents 328fad1 + 96cd988 commit 4aee3dd
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 3 deletions.
24 changes: 22 additions & 2 deletions manifests/resource/location.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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
Expand Down Expand Up @@ -145,6 +147,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,
Expand Down Expand Up @@ -217,6 +221,10 @@
if ($fastcgi_split_path != undef) {
validate_string($fastcgi_split_path)
}
if ($uwsgi != undef) {
validate_string($uwsgi)
}
validate_string($uwsgi_params)

validate_bool($internal)

Expand Down Expand Up @@ -302,13 +310,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) and ($internal == false)) {
fail('Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, stub_status, internal, 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')
}

# Use proxy, fastcgi or uwsgi template if $proxy is defined, otherwise use directory template.
# fastcgi_script is deprecated
if ($fastcgi_script != undef) {
warning('The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!')
Expand All @@ -329,6 +338,8 @@
$content_real = template('nginx/vhost/locations/stub_status.erb')
} elsif ($fastcgi != undef) {
$content_real = template('nginx/vhost/locations/fastcgi.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 {
Expand All @@ -343,6 +354,15 @@
}
}

if $ensure == present and $uwsgi != undef and !defined(File[$uwsgi_params]) {
file { $uwsgi_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}")
Expand Down
16 changes: 16 additions & 0 deletions manifests/resource/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,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',
Expand Down Expand Up @@ -332,6 +334,10 @@
if ($fastcgi_script != undef) {
validate_string($fastcgi_script)
}
if ($uwsgi != undef) {
validate_string($uwsgi)
}
validate_string($uwsgi_params)
validate_array($index_files)
if ($autoindex != undef) {
validate_string($autoindex)
Expand Down Expand Up @@ -525,6 +531,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,
Expand Down Expand Up @@ -569,6 +577,14 @@
}
}

if $uwsgi != undef and !defined(File[$uwsgi_params]) {
file { $uwsgi_params:
ensure => present,
mode => '0770',
content => template('nginx/vhost/uwsgi_params.erb'),
}
}

if ($listen_port != $ssl_port) {
concat::fragment { "${name_sanitized}-header":
target => $config_file,
Expand Down
68 changes: 67 additions & 1 deletion spec/defines/resource_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,65 @@
end
end

describe "vhost_location_uwsgi template content" do
let :default_params do
{
:location => 'location',
:uwsgi => 'unix:/home/project/uwsgi.socket',
:vhost => 'vhost1'
}
end

[
{
:title => 'should set www_root',
:attr => 'www_root',
:value => '/',
:match => %r'\s+root\s+/;'
},
{
:title => 'should set try_file(s)',
:attr => 'try_files',
:value => ['name1','name2'],
:match => %r'\s+try_files\s+name1 name2;',
},
{
:title => 'should set uwsgi_params',
:attr => 'uwsgi_params',
:value => 'value',
:match => %r'\s+include\s+value;'
},
{
:title => 'should set uwsgi_pass',
:attr => 'uwsgi',
:value => '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 { 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| is_expected.to contain_concat__fragment(fragment).with_content(item) }
else
lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
expect(lines & matches).to eq(matches)
end

Array(param[:notmatch]).each do |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
[
{
Expand Down Expand Up @@ -642,6 +701,13 @@
it { is_expected.to contain_file('/etc/nginx/fastcgi_params').with_mode('0770') }
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') }
end


context 'when ssl_only => true' do
let :params do { :ssl_only => true, :vhost => 'vhost1', :www_root => '/', } end
it { is_expected.not_to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-rspec-test")) }
Expand Down Expand Up @@ -678,7 +744,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, stub_status, internal, 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, uwsgi, stub_status, internal, or location_custom_cfg defined/) }
end

context "www_root and proxy are set" do
Expand Down
9 changes: 9 additions & 0 deletions spec/defines/resource_vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,15 @@
it { is_expected.to 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,
Expand Down
8 changes: 8 additions & 0 deletions templates/vhost/locations/uwsgi.erb
Original file line number Diff line number Diff line change
@@ -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 %>;
15 changes: 15 additions & 0 deletions templates/vhost/uwsgi_params.erb
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 4aee3dd

Please sign in to comment.