| 1 |
local cmd = import '../cmd/cmd.libsonnet'; |
| 2 |
|
| 3 |
{ |
| 4 |
// A Debian instance mixin that manages Flash and Uwsgi for the purposes of handling HTTP |
| 5 |
// requests. This supports: |
| 6 |
// - Installation of the correct packages |
| 7 |
// - Configuration of uwsgi |
| 8 |
// - Management of a python file that receives the request from uwsgi. |
| 9 |
// - Running the uwsgi daemon. |
| 10 |
DebianUwsgiFlask:: { |
| 11 |
|
| 12 |
local version = self, |
| 13 |
|
| 14 |
StandardRootImage+: { |
| 15 |
aptPackages+: ['python-dev'], |
| 16 |
pipPackages+: ['flask', 'uwsgi'], |
| 17 |
/* |
| 18 |
cmds+: [ |
| 19 |
cmd.LiteralFile { |
| 20 |
to: '/etc/cron.d/emperor', |
| 21 |
content: '@reboot root /usr/local/bin/uwsgi --master --emperor /etc/uwsgi/vassals ' |
| 22 |
+ '--daemonize /var/log/uwsgi/emperor.log --pidfile /var/run/uwsgi.pid ' |
| 23 |
+ '--die-on-term --uid www-data --gid www-data\n', |
| 24 |
filePermissions: '700', |
| 25 |
}, |
| 26 |
], |
| 27 |
*/ |
| 28 |
}, |
| 29 |
|
| 30 |
// Name of the Python module (written in Flask) that will be called. |
| 31 |
module:: 'uwsgi_module', |
| 32 |
|
| 33 |
// Name of the application in Flask. |
| 34 |
application:: 'app', |
| 35 |
|
| 36 |
// The frontend interface to uwsgi, necessary to configure web servers to find it. |
| 37 |
uwsgiSocket:: '/var/www/uwsgi.sock', |
| 38 |
|
| 39 |
uwsgiConf:: { |
| 40 |
chdir: '/var/www', |
| 41 |
base: '/var/www', |
| 42 |
module: version.module, |
| 43 |
pythonpath: '/var/www', |
| 44 |
socket: version.uwsgiSocket, |
| 45 |
'chmod-socket': '644', |
| 46 |
callable: version.application, |
| 47 |
logto: '/var/log/uwsgi/uwsgi.log', |
| 48 |
}, |
| 49 |
|
| 50 |
// Python code to handle requests goes here, as a string. |
| 51 |
uwsgiModuleContent:: null, |
| 52 |
|
| 53 |
httpContentCmds+: if version.uwsgiModuleContent == null then [] else [ |
| 54 |
cmd.LiteralFile { |
| 55 |
content: version.uwsgiModuleContent, |
| 56 |
to: '/var/www/%s.py' % version.module, |
| 57 |
}, |
| 58 |
], |
| 59 |
|
| 60 |
httpHandlerCmds+: [ |
| 61 |
cmd.EnsureDir { dir: '/etc/uwsgi/vassals' }, |
| 62 |
cmd.LiteralFile { |
| 63 |
to: '/etc/uwsgi/vassals/uwsgi.ini', |
| 64 |
content: std.manifestIni({ |
| 65 |
sections: { |
| 66 |
uwsgi: version.uwsgiConf, |
| 67 |
}, |
| 68 |
}), |
| 69 |
}, |
| 70 |
cmd.EnsureDir { dir: '/var/log/uwsgi', owner: 'www-data' }, |
| 71 |
], |
| 72 |
|
| 73 |
bootCmds+: [ |
| 74 |
'/usr/local/bin/uwsgi --master --emperor /etc/uwsgi/vassals ' |
| 75 |
+ '--daemonize /var/log/uwsgi/emperor.log --pidfile /var/run/uwsgi.pid ' |
| 76 |
+ '--die-on-term --uid www-data --gid www-data\n', |
| 77 |
], |
| 78 |
}, |
| 79 |
|
| 80 |
// A mixin on top of an instance that configures Nginx to use Uwsgi. It is expected that both |
| 81 |
// are already installed and configured. |
| 82 |
// TODO: There's a lot of general nginx stuff here that could be factored out, e.g. the keys. |
| 83 |
NginxUwsgiGlue: { |
| 84 |
local version = self, |
| 85 |
|
| 86 |
local base_conf = [ |
| 87 |
'server_name ~^.*$;', |
| 88 |
'charset utf-8;', |
| 89 |
'client_max_body_size 75M;', |
| 90 |
'location / { try_files $uri @yourapplication; }', |
| 91 |
'location @yourapplication {', |
| 92 |
' include uwsgi_params;', |
| 93 |
' uwsgi_pass unix:%s;' % version.uwsgiSocket, |
| 94 |
'}', |
| 95 |
], |
| 96 |
local http_part = [ |
| 97 |
'listen %d;' % version.httpPort, |
| 98 |
], |
| 99 |
local https_part = [ |
| 100 |
'listen %d ssl;' % version.httpsPort, |
| 101 |
'ssl_certificate /etc/ssl/cert.pem;', |
| 102 |
'ssl_certificate_key /etc/ssl/key.pem;', |
| 103 |
], |
| 104 |
local all_parts = base_conf |
| 105 |
+ (if version.httpPort != null then http_part else []) |
| 106 |
+ (if version.httpsPort != null then https_part else []), |
| 107 |
local whole_conf = std.lines(['server {'] + [' ' + line for line in all_parts] + ['}']), |
| 108 |
|
| 109 |
nginxAdditionalCmds+: [ |
| 110 |
cmd.LiteralFile { |
| 111 |
to: '/etc/nginx/conf.d/frontend_nginx.conf', |
| 112 |
content: whole_conf, |
| 113 |
}, |
| 114 |
] + (if version.httpsPort != null then [ |
| 115 |
cmd.LiteralFile { |
| 116 |
to: '/etc/ssl/cert.pem', |
| 117 |
content: version.sslCertificate, |
| 118 |
}, |
| 119 |
cmd.LiteralFile { |
| 120 |
to: '/etc/ssl/key.pem', |
| 121 |
content: version.sslCertificateKey, |
| 122 |
}, |
| 123 |
] else []), |
| 124 |
}, |
| 125 |
} |