A PaaS that runs anything HTTP: Getting Started with DIY Applications on OpenShift

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_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 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_DIY_IP and OPENSHIFT_DIY_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:

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 jamie 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:

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:

kill `ps -ef | grep binhello | grep -v grep | awk '{ print $2 }'` > /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 openshift@redhat.com, or tell us on the OpenShift Forums or come to IRC at freenode.net #openshift. We want to hear from you!

Comments are closed.