-
-
Save saga/4078557 to your computer and use it in GitHub Desktop.
aaa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://openshift.redhat.com/community/blogs/enabling-python-27-on-a-paas-with-the-openshift-diy-app-type | |
https://openshift.redhat.com/community/blogs/a-paas-that-runs-anything-http-getting-started-with-diy-applications-on-openshift | |
Enabling Python 2.7 on a PaaS with the OpenShift DIY App Type | |
Thursday, May 17, 2012 | |
by Evan Hazlett | |
Developer - Hacker | |
Preface | |
I have been using OpenShift for a few months now and have to say that I still love it. I've sampled most of the other PaaS solutions and feel most comfortable with OpenShift. One of the features I enjoy most in the service is the "do-it-yourself" application. With this, you can run just about anything in a "minimal-hassle", scalable environment. | |
I created a quickstart a few weeks ago that had Django running on Python 2.7 in "DIY" mode. This will use some of that, but be a more "step-by-step" approach for newcomers. This example will use the Flask micro-framework as well as the uWSGI application container for a true, production ready environment. | |
Diving In | |
The first thing you will need to do is get an OpenShift account (it's free). | |
Client Tools | |
Install the OpenShift client tools: | |
gem install rhc | |
** See here if you have trouble getting the tools installed. | |
Application Setup | |
Once you have the toolchain setup, we will create the application environment in OpenShift. For this example, we will name our application "py27". | |
rhc-create-app -a py27 -t diy-0.1 | |
You will see something similar to: | |
Creating application: py27 in ehazlett | |
Now your new domain name is being propagated worldwide (this might take a minute)... | |
Confirming application 'py27' is available: Success! | |
py27 published: http://py27-ehazlett.rhcloud.com/ | |
git url: ssh://[email protected]/~/git/py27.git/ | |
Disclaimer: This is an experimental cartridge that provides a way to try unsupported languages, frameworks, and middleware on Openshift. | |
You can view the bootstrapped application at http://py27-[namepsace].rhcloud.com | |
Python 2.7 | |
Now that the application environment is created, we will build and install the latest Python 2.7.x release. To do that, we will first need to get the application SSH credentials. | |
Run the following to show your application config (enter your account password when prompted): | |
rhc app show -a py27 | |
You should see something similar to this: | |
Application Info | |
================ | |
py27 | |
Framework: diy-0.1 | |
Creation: 2012-05-15T22:54:09-04:00 | |
UUID: 1qaz2wsx3edc4rfv | |
Git URL: ssh://[email protected]/~/git/py27.git/ | |
Public URL: http://py27-ehazlett.rhcloud.com/ | |
Embedded: | |
None | |
Log into your OpenShift application using the SSH credentials from above (in the Git URL line): | |
ssh 1qaz2wsx3edc4rfv@py27-[username].rhcloud.com | |
We will build everything in the OpenShift application tmp directory. | |
Navigate into the "tmp" directory: | |
cd $OPENSHIFT_TMP_DIR | |
Download the latest Python 2.7.x release: | |
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 | |
Extract: | |
tar jxf Python-2.7.3.tar.bz2 | |
Configure (to put the custom Python into the OpenShift runtime dir): | |
cd Python-2.7.3 | |
./configure --prefix=$OPENSHIFT_DATA_DIR | |
Make and install: | |
make install | |
You can now check that Python was successfully installed: | |
$OPENSHIFT_DATA_DIR/bin/python -V | |
You should get | |
Python 2.7.3 | |
Supporting Tools | |
We now have Python installed, but we can't do much. Part of the common Python toolchain is PIP for Python package management. | |
Setuptools | |
Change into the OpenShift "tmp" directory: | |
cd $OPENSHIFT_TMP_DIR | |
Download and install setuptools: | |
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz | |
tar zxf setuptools-0.6c11.tar.gz | |
cd setuptools-0.6c11 | |
Install: | |
$OPENSHIFT_DATA_DIR/bin/python setup.py install | |
PIP | |
Change into the OpenShift "tmp" directory: | |
cd $OPENSHIFT_TMP_DIR | |
Download and install setuptools: | |
wget http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz | |
tar zxf pip-1.1.tar.gz | |
cd pip-1.1 | |
Install: | |
$OPENSHIFT_DATA_DIR/bin/python setup.py install | |
Application | |
Okay, so now we have all of the parts needed to run the app. We now need to create the Flask application and configure OpenShift so that it knows how to start and stop it. | |
First we will create the Flask application. Logout of your OpenShift environment and navigate into the Git repository that was created for your app. | |
Create a file named application.py in the repo, for example - in the diy folder: | |
├── README | |
├── diy | |
│ └── index.html | |
│ └── testrubyserver.rb | |
│ └── application.py | |
└── misc | |
BTW, the index.html and testrubyserver.rb are not longer necessary and can be removed anytime. | |
Edit application.py with the following: | |
from flask import Flask | |
import platform | |
application = Flask(__name__) | |
@application.route("/") | |
def index(): | |
return 'Hello from Flask' | |
@application.route("/info") | |
def info(): | |
return platform.python_version() | |
Create a requirements.txt in the root application directory with the following: | |
uWSGI==1.2.3 | |
Flask==0.8 | |
Directory structure should look like: | |
├── README | |
├── diy | |
│ ├── application.py | |
│ ├── index.html | |
│ └── testrubyserver.rb | |
├── misc | |
└── requirements.txt | |
Hooks | |
We will now create the hooks that are needed for deployment as well as for starting and stopping the application. In your application Git repository, there is a hidden directory called ".openshift" that contains stubs for the action hooks. We will be editing these. | |
Edit .openshift/action_hooks/build to have the following: | |
$OPENSHIFT_DATA_DIR/bin/pip install --use-mirrors -r $OPENSHIFT_REPO_DIR/requirements.txt | |
Edit .openshift/action_hooks/start to have the following: | |
$OPENSHIFT_DATA_DIR/bin/uwsgi -s $OPENSHIFT_INTERNAL_IP:$OPENSHIFT_INTERNAL_PORT --socket-protocol http --pp $OPENSHIFT_REPO_DIR/diy --module application -d $OPENSHIFT_DIY_LOG_DIR/app.log --pidfile $OPENSHIFT_TMP_DIR/uwsgi.pid | |
Edit .openshift/action_hooks/stop to the following: | |
kill `cat $OPENSHIFT_TMP_DIR/uwsgi.pid` | |
Deploy | |
Commit all of the changes to the repository: | |
git add . | |
git commit -am "initial commit" | |
Deploy to OpenShift: | |
git push | |
You should now be able to browse and see your application running: | |
curl http://py27-[namespace].rhcloud.com | |
Should return: | |
Hello from Flask | |
And to verify that your application is using Python 2.7: | |
curl http://py27-[namespace].rhcloud.com/info | |
Should return: | |
2.7.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A PaaS that runs anything HTTP: Getting Started with DIY Applications on OpenShift | |
Thursday, March 22, 2012 | |
by Mark Atwood | |
Open Source Advocate & Developer Evangelist for the Red Hat OpenShift PaaS | |
In addition to supporting Perl, Ruby, PHP, Python, and Java EE6, the OpenShift environment supports the "Do it Yourself" or "DIY" application type. Using this application type, you can run just about any program that speaks HTTP. | |
How this works is remarkably straightforward. The OpenShift execution environment is a carefully secured Red Hat Enterprise Linux 6.2 on x64 systems. Thus, OpenShift can run any binary that will run on RHEL 6.2 x64. | |
The way that OpenShift DIY interfaces your application to the outside world is that creates a HTTP proxy specified by the environment variables OPENSHIFT_INTERNAL_IP and OPENSHIFT_INTERNAL_PORT. All your application has to do is bind and listen on that address and port. HTTP requests will come into the OpenShift environment, which will proxy those requests to your application. Your application will reply with HTTP responses, and the OpenShift environment will relay those responses back to your users. | |
Your application will be executed by the .openshift/action_hooks/start script, and will be stopped by the .openshift/action_hooks/stop script. | |
To learn more about how it works, follow along with as we construct a simple C application, and then run it as an OpenShift DIY. We will use the libmicrohttpd library so we dont have to write our own HTTP server. | |
Run the following commands at a shell prompt on your own Linux or MacOS machine. | |
svn checkout https: //gnunet.org/svn/libmicrohttpd/src-libmicrohttpd | |
cd src-libmicrohttpd | |
autoreconf -fi | |
mkdir ${HOME}/lib-libmicrohttpd | |
./configure --prefix=${HOME}/lib-libmicrohttpd make && make install | |
cd ${HOME} | |
Next, we download the binhello.c sample program. | |
curl -NOL https ://raw.github.com/openshift/openshift-diy-binhello-demo/master/binhello.c | |
This was originally the "hellobrowser.c" from the libmicrohttpd examples, but has been modified to run in OpenShift, by looking for the OPENSHIFT_INTERNAL_IP and OPENSHIFT_INTERNAL_PORT environment variables, and then binding to and listening on that address and port. | |
Next, we compile the program. We will statically link the libmicrohttpd.a object instead of using the .so shared object libraries. We could instead include the shared object library files into our application, and then play with LD_PRELOAD, but that is more than we want to do for this demo. | |
gcc -o binhello binhello.c ${HOME}/lib-libmicrohttpd/lib/libmicrohttpd.a -L ${HOME}/lib-libmicrohttpd/include -pthread | |
If we want to skip that step, and avoid playing with the GCC compiler, we can instead download a precompiled binary: | |
curl -NOL https ://raw.github.com/openshift/openshift-diy-binhello-demo/master/binhello | |
chmod +x ./binhello | |
Next, we create the OpenShift application container. We will name this application jamie, after one of the kings of Do It Yourself: Jamie Hyneman. | |
rhc app create -a jamie -t diy-0.1 | |
Next, we place our binary into the local git repo we just created. | |
mkdir jamie/bin | |
mv binhello jamie/bin | |
cd jamie | |
git add bin/binhello | |
Next, we tell OpenShift how to run our program. We go into the .openshift/action-hooks directory | |
cd .openshift/action_hooks | |
and replace the start script with the following text: | |
#!/bin/bash | |
cd $OPENSHIFT_REPO_DIR/bin | |
nohup ./binhello >;${OPENSHIFT_DIY_LOG_DIR}/binhello.log 2>&1 & | |
We are redirecting the UNIX standard output and standard error to a file in the OpenShift logging directory, and using nohup to assure that the program properly runs in the background. | |
Next, we replace the stop script with the following text: | |
#!/bin/bash | |
kill <code>ps -ef | grep binhello | grep -v grep | awk '{ print $2 }'</code> > /dev/null 2>&1 | |
exit 0 | |
We make sure those scripts are still executable. | |
chmod +x start stop | |
Now, we go back to the top directory of our application, commit it to git, and push it up to the OpenShift servers. | |
cd ../.. | |
git commit -a -m "install the binhello binary and start stop scripts" | |
git push | |
We can then browse to the application. In my case, it will be http://jamie-matwood.rhcloud.com/. We should see a very simple Hello, World! | |
And that is all there is to it! | |
Using the DIY application type, you can do all sorts of crazy things. Do you have a favorite scripting language that OpenShift does not yet support? Do you have a framework written in Smalltalk? Do you have a HTTP server written in Fortran? Do you have a control server for a model train? With DIY, you can run it on OpenShift. | |
We would like to extend a challenge: If you do something useful, amazing, or crazy with the DIY application container, please contact us at [email protected], or tell us on the OpenShift Forums or come to IRC at freenode.net #openshift. We want to hear from you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa