Functional Programming in the Cloud: How to Run Haskell on OpenShift - Archived

Haskell logo run on Openshift

One of the benefits of Platform as a Service (PaaS) is that it makes it trivial to try out alternative technology stacks. The OpenShift PaaS is a polyglot platform at the heart of a thriving open-source community, the contributions of which make it easy to experiment with applications written in a host of different programming languages. This includes the purely functional Haskell language.

Although it is not one of the Red Hat-supported languages for OpenShift, Haskell web applications run on the platform with the aid of the community-created Haskell cartridge project. This is great news for functional programming (FP) enthusiasts such as myself and those who want to learn more about the paradigm; Haskell is a popular choice for learning FP principles. In this blog post, I will discuss how to create a Haskell web application on OpenShift.

Prerequisites

If you do not have an OpenShift account yet, sign up for OpenShift Online for free. You’ll receive three gears (containers) in which to run applications. At the time of writing, each of these free gears come with 512MB of RAM and 1GB of disk space.

To help you communicate with the OpenShift platform, you should install the RHC client tools on your machine. There are instructions on how to do that for a variety of operating systems at the OpenShift Dev Center. Once the RHC tools are installed, run the command rhc setup on the command line to configure RHC ready for use.

Creating a Haskell App

Create Haskell applications with RHC in the same way as any other application, using the following syntax:

rhc app create {appname} {cartridge} -s

Cartridge here can be a short name such as python-3.3 or jbossas-7, as seen when you execute the command rhc cartridge list, or it can be a URL linking to the manifest file for an external cartridge. In our case, it will be the latter. The -s option specifies that we want the application to be auto-scalable.

The Haskell community cartridge project in fact encompasses several OpenShift cartridges for Haskell. These are listed on the Haskell Wiki. The simplest provides only Haskell’s network package, whereas the others come with particular frameworks preinstalled. For this blog post, I am going to use the cartridge for Scotty, which is a simple web framework inspired by Ruby’s Sinatra project. Using the Scotty manifest URL found on the Haskell Wiki, you can create a scalable Haskell Scotty app with the following command:

rhc app create scottyapp http://www.accursoft.com/cartridges/scotty.yml -s

Once this command has finished executing, the app will be available at http://scottyapp-{yourdomain}.rhcloud.com (you can use rhc app show to check the URL).

Adding Code

As part of the app creation process, the source code of the app will have been cloned to a directory called scottyapp. By default, inside is a server.cabal file, Main.hs file, and .openshift directory for configuration. Cabal is a Haskell build system; if you are not yet familiar with it you may want to read How to Write a Haskell Program.

The OpenShift Haskell community cartridges expect a Cabal file that defines an executable called server, which takes as command line arguments the IP address and port on which to listen. Other than conforming to this requirement and using these arguments or OpenShift environment variables to refer to any environment specifics, Haskell web app development on OpenShift does not differ much from local Haskell web dev.

You can test your code changes locally (you may want to create a Cabal sandbox) and add and commit them to the local repository using standard Git commands (git add and git commit). To deploy your changes to OpenShift, use the command git push.

The template repository code that comes with the various Haskell cartridges is the same for each one, so there will be nothing Scotty-specific in the scottyapp directory just yet. You can address this by using the following command to pull in some basic Scotty code.

cd scottyapp
git pull -s recursive -X theirs git://github.com/codemiller/openshift-scotty.git

We could also have added this template code as part of the rhc app create command by using the option --from-code=http://github.com/codemiller/openshift-scotty.git.

Once the new code is merged into the Git repository, push the changes to OpenShift in the standard way:

git push

This command will take a few moments to complete while the project’s dependencies are downloaded, built and installed. Once it has finished, when you go to the app URL you should see the greeting Hello World!.

Marker Files

It may take quite some time to build new code. Out of the box your web app will be unavailable during that process. You can fix this by configuring OpenShift to hot deploy your app. To do this, add the hot_deploy marker file.

cd scottyapp
touch .openshift/markers/hot_deploy
git add .
git commit -m "Added hot deployment marker"
git push

Windows users, replace the touch command with your preferred process for creating an empty file at that location in the repository, with the name hot_deploy.

When you push new code, you will see from the output that the server no longer restarts. To change this behavior, remove the marker file.

Other Haskell OpenShift cartridge marker files you may be interested in include force_clean_build and cabal_update.

Memory Considerations

Given building Haskell programs can be quite memory intensive, you may find the free resources available on OpenShift Online are insufficient when you start to create Haskell applications with lots of dependencies (you might see ExitFailure 9 if the build runs out of memory). OpenShift Online’s paid plans can provide more resources. As OpenShift is open source, you could also opt to run your own OpenShift Origin instance to facilitate experiments with memory-intensive Haskell builds.

Another issue you may encounter is OpenShift timing out when you try to create beefy Haskell apps in one command using rhc app create with the --from-code option. This command both creates the Haskell application and copies and builds the referenced source code. If the build takes a very long time to complete, the OpenShift broker may produce a timeout error and the app creation will be unsuccessful. In this case, it is better to separate the commands. Firstly, create your OpenShift app with the required cartridges, then add the template code with a Git command such as the one we used earlier, git pull -s recursive -X theirs git://github.com/codemiller/openshift-scotty.git.

Summary

OpenShift is a polyglot platform used to run programs written in many languages, including Haskell. A collection of open-source Haskell cartridges has been created by the community to facilitate this, including a cartridge for the lightweight Scotty web framework. Using this resource, it only takes one terminal command to get a Haskell web app running in the cloud.

To learn more about Haskell, have a look at the excellent free online book Learn You A Haskell for Great Good! or try a free online Haskell course. To delve further into Scotty, check out the Scotty starter app code available on GitHub and the tutorial here. Happy coding!

Next Steps

Categories
OpenShift Origin
Tags
, , , , , ,
Comments are closed.