Skip to content

Commit 9bc4bc5

Browse files
committed
Add support for uwsgi applications
1 parent 4d08a29 commit 9bc4bc5

6 files changed

Lines changed: 161 additions & 9 deletions

File tree

README.md

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ A puppet module for installing and managing uwsgi
88
This module installs and configures [uWSGI](http://uwsgi-docs.readthedocs.org)
99
in [Emperor mode](http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html).
1010

11-
It does not, currently, manage uwsgi applications (PRs welcome).
11+
It can also create and manage uwsgi applications that run under the emperor,
12+
which are best defined in hiera.
1213

1314
Just about every option is configurable, so it should work on most distributions
1415
by putting together a hiera file.
@@ -30,6 +31,9 @@ parameters.
3031
Package state.
3132
Default: 'installed'
3233

34+
If 'absent' or 'purged', then remove the `service_file` and `config_file`
35+
also
36+
3337
* `package_provider`
3438
The provider to use to install the package.
3539
Default: 'pip'
@@ -97,8 +101,6 @@ parameters.
97101

98102
#### Using Hiera
99103

100-
Hiera can be (should be!) used to change any of the default uwsgi parameters.
101-
102104
Sets up some custom options within the emperor config file:
103105

104106
```yaml
@@ -129,3 +131,85 @@ uwsgi::service_enable: false
129131
uwsgi::install_pip: false
130132
uwsgi::install_python_dev: false
131133
```
134+
135+
## Defined Types
136+
137+
### uwsgi::app
138+
139+
Responsible for creating uwsgi applications that run under the uwsgi emperor.
140+
You shouldn't need to use this type directly, as the `uwsgi` class will
141+
automatically create all applications defined in hiera under the uwsgi::app
142+
key. See the hiera section below for examples.
143+
144+
#### Parameters
145+
146+
* `ensure`
147+
Ensure the config file exists. Default: 'present'
148+
149+
* `template`
150+
The template used to construct the config file.
151+
Default: 'uwsgi/uwsgi_app.ini.erb'
152+
153+
* `uid`
154+
The user to run the application as. Required.
155+
May be the user name, not just the id.
156+
157+
* `gid`
158+
The group to run the application as. Required.
159+
May be the group name, not just the id.
160+
161+
* `application_options`
162+
Extra options to set in the application config file
163+
164+
#### Using Hiera
165+
166+
Configure a django application:
167+
168+
```yaml
169+
---
170+
uwsgi::app:
171+
django:
172+
ensure: 'present'
173+
uid: 'django'
174+
gid: 'django'
175+
application_options:
176+
chdir: '/path/to/project'
177+
module: 'mysite.wsgi:application'
178+
socket: '/tmp/uwsgi_django.sock'
179+
master: 'True'
180+
vaccum: 'True'
181+
max-requests: '5000'
182+
buffer-size: '32768'
183+
processes: 4
184+
threads: 8
185+
```
186+
187+
Configure multiple applications (all yaml files are aggregated using
188+
`hiera_hash`):
189+
190+
```yaml
191+
# common.yaml
192+
---
193+
uwsgi::app:
194+
django_1:
195+
ensure: 'present'
196+
uid: 'django'
197+
gid: 'django'
198+
application_options:
199+
chdir: '/path/to/project'
200+
module: 'mysite.wsgi:application'
201+
socket: '/tmp/uwsgi_django.sock'
202+
203+
204+
# role_app_server.yaml
205+
---
206+
uwsgi::app:
207+
django_2:
208+
ensure: 'present'
209+
uid: 'django'
210+
gid: 'django'
211+
application_options:
212+
chdir: '/path/to/project2'
213+
module: 'mysite.wsgi:application'
214+
socket: '/tmp/uwsgi_django2.sock'
215+
```

manifests/app.pp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# == Define: uwsgi::app
2+
#
3+
# Responsible for creating uwsgi applications. You shouldn't need to use this
4+
# type directly, as the main `uwsgi` class uses this type internally.
5+
#
6+
# === Parameters
7+
#
8+
# [*ensure*]
9+
# Ensure the config file exists. Default: 'present'
10+
#
11+
# [*template*]
12+
# The template used to construct the config file.
13+
# Default: 'uwsgi/uwsgi_app.ini.erb'
14+
#
15+
# [*uid*]
16+
# The user to run the application as. Required.
17+
# May be the user name, not just the id.
18+
#
19+
# [*gid*]
20+
# The group to run the application as. Required.
21+
# May be the group name, not just the id.
22+
#
23+
# [*application_options*]
24+
# Extra options to set in the application config file
25+
#
26+
# === Authors
27+
# - Josh Smeaton <josh.smeaton@gmail.com>
28+
#
29+
define uwsgi::app (
30+
$ensure = 'present',
31+
$template = 'uwsgi/uwsgi_app.ini.erb',
32+
$application_options = undef,
33+
$uid,
34+
$gid
35+
) {
36+
37+
file { "${::uwsgi::app_directory}/${title}.ini":
38+
ensure => $ensure,
39+
owner => $uid,
40+
group => $gid,
41+
mode => '0644',
42+
content => template($template),
43+
require => Package[$::uwsgi::package_name],
44+
notify => Service[$::uwsgi::service_name]
45+
}
46+
}

manifests/init.pp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# [*package_ensure*]
1414
# Package state. Default: 'installed'
1515
#
16+
# If 'absent' or 'purged', then remove the `service_file` and `config_file`
17+
# also
18+
#
1619
# [*package_provider*]
1720
# The provider to use to install the package. Default: 'pip'
1821
#
@@ -108,8 +111,15 @@
108111
provider => $package_provider
109112
}
110113

114+
# remove config files if package is purged
115+
$file_ensure = $package_ensure ? {
116+
'absent' => 'absent',
117+
'purged' => 'absent',
118+
default => 'present'
119+
}
120+
111121
file { $config_file:
112-
ensure => 'present',
122+
ensure => $file_ensure,
113123
owner => 'root',
114124
group => 'root',
115125
mode => '0644',
@@ -118,7 +128,7 @@
118128
}
119129

120130
file { $service_file:
121-
ensure => 'present',
131+
ensure => $file_ensure,
122132
owner => 'root',
123133
group => 'root',
124134
mode => '0644',
@@ -127,6 +137,14 @@
127137
require => Package[$package_name]
128138
}
129139

140+
file { $app_directory:
141+
ensure => 'directory',
142+
owner => 'root',
143+
group => 'root',
144+
mode => '0644',
145+
require => Package[$package_name]
146+
}
147+
130148
service { $service_name:
131149
ensure => $service_ensure,
132150
enable => $service_enable,
@@ -143,4 +161,8 @@
143161
File[$service_file]
144162
]
145163
}
164+
165+
# finally, configure any applications necessary
166+
$applications = hiera_hash('uwsgi::app', {})
167+
create_resources('uwsgi::app', $applications)
146168
}

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "engage-uwsgi",
3-
"version": "0.1.1",
3+
"version": "0.9.0",
44
"author": "jarshwah <josh.smeaton@gmail.com>",
55
"summary": "Install and configure uwsgi in Emperor mode",
66
"license": "MIT",

templates/uwsgi.ini.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[uwsgi]
77
socket = /run/uwsgi/uwsgi.socket
88
pidfile = /run/uwsgi/uwsgi.pid
9-
emperor = <%= @app_config_dir %>
9+
emperor = <%= @app_directory %>
1010
emperor-tyrant = true
1111
master = true
1212
autoload = true

templates/uwsgi_app.ini.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
uid = <%= @uid %>
88
gid = <%= @gid %>
99
<%
10-
if @config
11-
@config.sort.each do |key, value|
10+
if @application_options
11+
@application_options.sort.each do |key, value|
1212
-%>
1313
<%= key %> = <%= value%>
1414
<%

0 commit comments

Comments
 (0)