Extending OpenShift

Build with Downloadable Cartridges

Using a downloadable cartridge, you can easily bring your own frameworks and technologies to OpenShift, reducing the need to set up a DIY cart. Read our guides on creating and using your own cartridges.

You can use your own Languages and Data Stores by talking advantage of OpenShift's powerful Do-It-Yourself (DIY) feature if the built-in Perl, Ruby, PHP, Python, and Java EE6 support doesn't suit you. OpenShift's DIY allows you run just about any program that speaks HTTP. OpenShift can run any binary that will run on RHEL 6.2 x64 because the OpenShift execution environment is a carefully secured Red Hat Enterprise Linux 6.2 running on x64 systems.

How it Works

OpenShift DIY interfaces your application to the outside world by creating an HTTP proxy specified by the environment variables OPENSHIFT_DIY_IP and OPENSHIFT_DIY_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 is executed by the .openshift/action_hooks/start script, and stopped by the .openshift/action_hooks/stop script.

Sample Tutorial

To learn how it works, follow along as we construct a simple C application, and then run it as an OpenShift DIY. We will use the libmicrohttpd library so we don't have to write our own HTTP server.

To build our application:
  1. Run the following commands from a shell prompt on your 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}
  2. 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_DIY_IP and OPENSHIFT_DIY_PORT environment variables, and then binding to and listening on that address and port.
  3. 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 -I${HOME}/lib-libmicrohttpd/include -pthread -lrt
    Or you can skip this step and follow the next step instead.
  4. If we want to skip the previous step, and avoid playing with the GCC compiler, download a precompiled binary instead:
    curl -NOL https://raw.github.com/openshift/openshift-diy-binhello-demo/master/binhello
    chmod +x ./binhello
  5. 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 jamie diy-0.1 
  6. Place our binary into the local git repo we just created.
    mkdir jamie/bin
    mv binhello jamie/bin
    cd jamie
    git add bin/binhello
  7. Tell OpenShift how to run our program. Navigate 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.
  8. Replace the stop script with the following text:
    #!/bin/bash
    kill `ps -ef | grep binhello | grep -v grep | awk '{ print $2 }'` > /dev/null 2>&1
    exit 0
  9. Ensure those scripts are still executable.
    chmod +x start stop
  10. Navigate 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
  11. Browse to the application. In this 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 unusual 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.

Your Challenge

We would like to extend a challenge: If you do something useful, amazing, or crazy with the DIY application container, please contact us at openshift@redhat.com, or tell us on the OpenShift Forums or come to IRC at freenode.net #openshift. We want to hear from you!