Skip to content

Commit 273d107

Browse files
committed
Merge remote-tracking branch 'github/master'
Conflicts: README.md
2 parents e5050f0 + e038c7b commit 273d107

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2393
-2
lines changed

COPYRIGHT

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Copyright 2014 Goran Sterjov
2+
Copyright 2013 Red Hat, Inc. and/or its affiliates.

LICENSE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Licensed under the Apache License, Version 2.0 (the "License");
2+
you may not use this file except in compliance with the License.
3+
You may obtain a copy of the License at
4+
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,63 @@
1-
## OpenShift Cartridge Development Kit - makes creating carts easy!
1+
## OpenShift Advanced Python Cartridge
22

3-
For the latest on creating cartridges, read the [Cartridge Writer's Guide](https://github.com/openshift/origin-server/blob/master/node/README.writing_cartridges.md)
3+
Inspired by the [Advanced Ruby Cartridge](https://github.com/openshift-cartridges/advanced-ruby-cartridge) this cartridge attempts to add support for the various WSGI-compliant python servers to the OpenShift platform.
4+
It does this by combining a modified python cartridge with the [downloadable Nginx cartridge](https://github.com/gsterjov/openshift-nginx-cartridge) as a reverse proxy.
5+
6+
7+
### Why?
8+
9+
The official python cartridge uses Apache and mod_wsgi to serve your app which isn't asynchronous and presents a problem for websockets. An alternative is to provide an app.py file which allows you to avoid mod_wsgi and use something like gevent, but that elimintates the ability to serve static files through a fast webserver like Apache or Nginx.
10+
11+
12+
### Installation
13+
14+
To install this cartridge use the cartridge reflector when creating an app
15+
16+
rhc create-app myapp http://cartreflect-claytondev.rhcloud.com/reflect?github=gsterjov/openshift-advanced-python-cartridge
17+
18+
19+
### Usage
20+
21+
Using the cartridge isn't very different to the official python cartridge. Instead of providing a WSGI <code>application()</code> function at <code>wsgi/application</code> you instead provide the <code>application()</code> function at <code>app.py</code>. This file will be used directly by all the available servers.
22+
23+
By default **wsgiref** is used so a working environment can be provided immediately. This is easily changed by setting the <code>OPENSHIFT_PYTHON_SERVER</code> environment variable and then restarting or redeploying the app.
24+
25+
rhc env set OPENSHIFT_PYTHON_SERVER=gunicorn
26+
rhc app restart
27+
28+
Be aware, however, that restarting/redeploying after changing servers for the first time might take a fair amount of time. This is because the server packages get compiled and installed on an as needed basis. Gevent and Gunicorn (which is configured to use gevent as its workers), for example, needs to be compiled within the app as OpenShift doesn't provide it as a system level package.
29+
30+
31+
### Supported servers
32+
33+
- wsgiref
34+
- gevent
35+
- gunicorn
36+
37+
38+
### Configuration
39+
40+
There is little to no configuration required as most of the details lay in the interaction between Nginx and the WSGI server package. All that is required is to define the <code>application()</code> function in <code>app.py</code>.
41+
Any configuration for the server package will be exposed via environment variables.
42+
43+
#### Environment Variables
44+
45+
<code>OPENSHIFT_PYTHON_WORKERS</code> - The number of workers to spawn for packages like gunicorn.
46+
Default: <code>number of CPUs * 2 + 1</code>
47+
48+
49+
### Static files
50+
51+
Static files will be served from the <code>public/</code> directory. These files will be served directly by Nginx.
52+
53+
54+
### Web Sockets
55+
56+
Web socket support is enabled in Nginx, however it does little more than passing the requests through with the appropriate upgrade headers. More complex websocket environments will need to go for the customised <code>nginx.conf</code> option.
57+
58+
In the future there might be a nicer way to support websockets as a completely separate server. For example, the application might be served out by gunicorn, but websocket services served out with twisted or tornado. These are purely thoughts at the moment however.
59+
60+
61+
### Custom nginx.conf
62+
63+
Like the standalone Nginx cartridge, its possible to provide your own server configuration to be included in the main <code>nginx.conf</code> file. A sample is provided in the cloned repo as <code>nginx.conf.erb.sample</code>. Simply remove the .sample suffix and commit the changes.<code>nginx.conf.erb</code> will be processed and included in the main configuration every time the server starts.

bin/control

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash -e
2+
3+
source $OPENSHIFT_CARTRIDGE_SDK_BASH
4+
5+
6+
NGINX_DIR=$OPENSHIFT_ADVANCED_PYTHON_DIR/usr/nginx/versions/$NGINX_VERSION
7+
PYTHON_DIR=$OPENSHIFT_ADVANCED_PYTHON_DIR/usr/python/versions/$OPENSHIFT_ADVANCED_PYTHON_VERSION
8+
9+
10+
case $1 in
11+
update-configuration)
12+
source $PYTHON_DIR/lib/update-configuration
13+
update-configuration $PYTHON_VERSION
14+
;;
15+
stop)
16+
$NGINX_DIR/bin/control "$@"
17+
$PYTHON_DIR/bin/control "$@"
18+
;;
19+
restart)
20+
$NGINX_DIR/bin/control "stop"
21+
$PYTHON_DIR/bin/control "stop"
22+
$PYTHON_DIR/bin/control "start"
23+
$NGINX_DIR/bin/control "start"
24+
;;
25+
*)
26+
$PYTHON_DIR/bin/control "$@"
27+
$NGINX_DIR/bin/control "$@"
28+
;;
29+
esac

bin/install

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash -eu
2+
3+
source $OPENSHIFT_CARTRIDGE_SDK_BASH
4+
5+
case "$1" in
6+
-v|--version)
7+
version="$2"
8+
esac
9+
10+
11+
echo "$version" > env/OPENSHIFT_ADVANCED_PYTHON_VERSION
12+
echo "1.4" > env/NGINX_VERSION
13+
14+
15+
for dir in logs run; do
16+
mkdir -p $dir
17+
done
18+
19+
# Call the version specific install script
20+
exec $OPENSHIFT_ADVANCED_PYTHON_DIR/usr/python/versions/$version/bin/install $version

bin/setup

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash -eu
2+
3+
source $OPENSHIFT_CARTRIDGE_SDK_BASH
4+
5+
case "$1" in
6+
-v|--version)
7+
version="$2"
8+
esac
9+
10+
# Update environment
11+
source $OPENSHIFT_ADVANCED_PYTHON_DIR/usr/python/versions/$version/lib/update-configuration
12+
update-configuration
13+
14+
# Call the version specific setup script
15+
exec $OPENSHIFT_ADVANCED_PYTHON_DIR/usr/python/versions/$version/bin/setup $version

bin/upgrade

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash -eu
2+
3+
python_version="$1"
4+
5+
upgrade_script="$OPENSHIFT_ADVANCED_PYTHON_DIR/usr/python/versions/$python_version/bin/upgrade"
6+
7+
8+
if [ -e "$upgrade_script" ]
9+
then
10+
exec "$upgrade_script" "$@"
11+
fi
12+
13+
exit 0

conf/gunicorn.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import multiprocessing
3+
4+
cart_dir = os.environ["OPENSHIFT_ADVANCED_PYTHON_DIR"]
5+
tmp_dir = os.environ["OPENSHIFT_TMP_DIR"]
6+
7+
8+
if os.environ.has_key("OPENSHIFT_PYTHON_WORKERS"):
9+
workers = os.environ["OPENSHIFT_PYTHON_WORKERS"]
10+
else:
11+
workers = multiprocessing.cpu_count() * 2 + 1
12+
13+
14+
worker_class = "gevent"
15+
daemon = True
16+
bind = "unix:{0}run/appserver.sock".format(cart_dir)
17+
pidfile = "{0}run/appserver.pid".format(cart_dir)
18+
19+
accesslog = "{0}logs/appserver.access.log".format(cart_dir)
20+
errorlog = "{0}logs/appserver.error.log".format(cart_dir)
21+
22+
worker_tmp_dir = "{0}".format(tmp_dir)
23+
tmp_upload_dir = "{0}".format(tmp_dir)

conf/mime.types

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
types {
3+
text/html html htm shtml;
4+
text/css css;
5+
text/xml xml;
6+
image/gif gif;
7+
image/jpeg jpeg jpg;
8+
application/x-javascript js;
9+
application/atom+xml atom;
10+
application/rss+xml rss;
11+
12+
text/mathml mml;
13+
text/plain txt;
14+
text/vnd.sun.j2me.app-descriptor jad;
15+
text/vnd.wap.wml wml;
16+
text/x-component htc;
17+
18+
image/png png;
19+
image/tiff tif tiff;
20+
image/vnd.wap.wbmp wbmp;
21+
image/x-icon ico;
22+
image/x-jng jng;
23+
image/x-ms-bmp bmp;
24+
image/svg+xml svg svgz;
25+
image/webp webp;
26+
27+
application/java-archive jar war ear;
28+
application/mac-binhex40 hqx;
29+
application/msword doc;
30+
application/pdf pdf;
31+
application/postscript ps eps ai;
32+
application/rtf rtf;
33+
application/vnd.ms-excel xls;
34+
application/vnd.ms-powerpoint ppt;
35+
application/vnd.wap.wmlc wmlc;
36+
application/vnd.google-earth.kml+xml kml;
37+
application/vnd.google-earth.kmz kmz;
38+
application/x-7z-compressed 7z;
39+
application/x-cocoa cco;
40+
application/x-java-archive-diff jardiff;
41+
application/x-java-jnlp-file jnlp;
42+
application/x-makeself run;
43+
application/x-perl pl pm;
44+
application/x-pilot prc pdb;
45+
application/x-rar-compressed rar;
46+
application/x-redhat-package-manager rpm;
47+
application/x-sea sea;
48+
application/x-shockwave-flash swf;
49+
application/x-stuffit sit;
50+
application/x-tcl tcl tk;
51+
application/x-x509-ca-cert der pem crt;
52+
application/x-xpinstall xpi;
53+
application/xhtml+xml xhtml;
54+
application/zip zip;
55+
56+
application/octet-stream bin exe dll;
57+
application/octet-stream deb;
58+
application/octet-stream dmg;
59+
application/octet-stream eot;
60+
application/octet-stream iso img;
61+
application/octet-stream msi msp msm;
62+
63+
audio/midi mid midi kar;
64+
audio/mpeg mp3;
65+
audio/ogg ogg;
66+
audio/x-m4a m4a;
67+
audio/x-realaudio ra;
68+
69+
video/3gpp 3gpp 3gp;
70+
video/mp4 mp4;
71+
video/mpeg mpeg mpg;
72+
video/quicktime mov;
73+
video/webm webm;
74+
video/x-flv flv;
75+
video/x-m4v m4v;
76+
video/x-mng mng;
77+
video/x-ms-asf asx asf;
78+
video/x-ms-wmv wmv;
79+
video/x-msvideo avi;
80+
}

env/PYTHON_EGG_CACHE.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= ENV['OPENSHIFT_ADVANCED_PYTHON_DIR'] %>virtenv/.python-eggs/

lib/util

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
# Utility functions for use in the cartridge scripts.
3+
4+
function parse_args {
5+
while :
6+
do
7+
case $1 in
8+
-h | --help | -\?)
9+
echo "usage: $0 [--version[=]<value>]"
10+
exit 0
11+
;;
12+
-v | --version)
13+
version=$2 # You might want to check if you really got VERSION
14+
shift 2
15+
;;
16+
--version=*)
17+
version=${1#*=} # Delete everything up till "="
18+
shift
19+
;;
20+
--) # End of all options
21+
shift
22+
break
23+
;;
24+
-*)
25+
echo "WARN: Unknown option... Exiting: $1" >&2
26+
exit 1
27+
;;
28+
*) # no more options. Stop while loop
29+
break
30+
;;
31+
esac
32+
done
33+
}

metadata/managed_files.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
processed_templates:
2+
- 'conf/*.erb'
3+
dependency_dirs:
4+
- virtenv
5+
build_dependency_dirs:
6+
- ~/.distlib

metadata/manifest.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Name: advanced-python
2+
Cartridge-Short-Name: ADVANCED_PYTHON
3+
Display-Name: Advanced Python 2.7
4+
Description: 'An advanced python cartridge utilising an Nginx reverse proxy and a choice of modern WSGI servers such as gevent or gunicorn'
5+
Version: '2.7'
6+
Versions:
7+
- '2.7'
8+
- '3.3'
9+
License: The Python License, version 2.7
10+
License-Url: http://docs.python.org/3/license.html
11+
Vendor: python.org
12+
Cartridge-Version: 0.0.1
13+
Cartridge-Vendor: gsterjov
14+
Categories:
15+
- service
16+
- python
17+
- web_framework
18+
Website: http://www.python.org
19+
Help-Topics:
20+
Developer Center: https://www.openshift.com/developers
21+
Provides:
22+
- python-2.7
23+
- python
24+
- python(version) = 2.7
25+
Publishes:
26+
Subscribes:
27+
set-env:
28+
Type: ENV:*
29+
Required: false
30+
set-doc-url:
31+
Type: STRING:urlpath
32+
Required: false
33+
Scaling:
34+
Min: 1
35+
Max: -1
36+
Endpoints:
37+
- Private-IP-Name: IP
38+
Private-Port-Name: PORT
39+
Private-Port: 8080
40+
Public-Port-Name: PROXY_PORT
41+
Protocols:
42+
- http
43+
- ws
44+
Options:
45+
primary: true
46+
Mappings:
47+
- Frontend: ''
48+
Backend: ''
49+
Options:
50+
websocket: true
51+
- Frontend: /health
52+
Backend: ''
53+
Options:
54+
health: true
55+
Version-Overrides:
56+
'3.3':
57+
Display-Name: Advanced Python 3.3
58+
License: The Python License, version 3.3
59+
Provides:
60+
- python-3.3
61+
- python
62+
- python(version) = 3.3
63+
Categories:
64+
- service
65+
- python
66+
- web_framework
67+
Install-Build-Required: false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
For information about action hooks supported by OpenShift, consult the documentation:
2+
3+
http://openshift.github.io/documentation/oo_user_guide.html#the-openshift-directory

0 commit comments

Comments
 (0)