Siscia Tech

Programming, tech related blog

2 notes &

Webnoir on Openshift

After one whole day spent trying to figure out how make a webnoir application work in openshift  I decide that I should share what I have learned.

The process is not very hard. I am assuming that you already have a openshift account— if not is not really hard to register, Although I won’t use it in the tutorial, it could be useful have rhc (the openshift client) installed; you will definitely need git.

The easiest way is to start creating the application on openshift— we need to create a Do-It-Yourself (DIY) application.

I call my application examplenoir.

Now we download the code that we will need, using git.

After the creation of our application openshift will give us the URL of the git repository. If you forget to save it, don’t worry, you will get the same URL in the application console.

Now we will just clone the repository in our computer.

It will ask you to add the rsa key to the know keys, just type yes and keep going.

It is now time to build our application, so let’s move in the diy directory and just launch lein, please note that I used the noir script for lein.

Now that your application is ready, we can push it on the cloud (or you can wait the next step)

Visiting now your webpage, you will probably see something like that:

This seems to be meaningless, but it gives us important clues, it say to modify the .openshift/action_hooks/{start, stop} 

Before we do that we need to understand what those file are; they are simply bash script that openshift will execute every time that you start or stop your script, we will use them to run the webserver through lein or to kill every clojure process.

Since we will need lein, before of everything we can “install” it, just make another directory called bin and download inside the lein script and make it executable.

Now we can think about the start script, my looks like that:

#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080
# save as .openshift/action_hooks/start

export HTTP_CLIENT="wget --no-check-certificate -O"
export PORT=$OPENSHIFT_INTERNAL_PORT
export HOST=$OPENSHIFT_INTERNAL_IP
export HOME=$OPENSHIFT_DATA_DIR/home
export LEIN_JVM_OPTS=-Duser.home=$HOME

cd $OPENSHIFT_REPO_DIR/diy/examplenoir

$OPENSHIFT_REPO_DIR/bin/lein deps

$OPENSHIFT_REPO_DIR/bin/lein run >${OPENSHIFT_LOG_DIR}/lein.log 2>&1 &
disown

And this other is the stop script:

#!/bin/bash

# The logic to stop your application should be put in this script.

# save as .openshift/action_hooks/stop

kill `ps -ef | grep clojure | grep -v “grep clojure” | awk ‘{ print $2 }’` >${OPENSHIFT_LOG_DIR}/stop.log 2>&1

exit 0

(Thanks to Philipp Meier)

Now it is time to push all back to Openshift.

It should print out a lot of stuff, (it is lein that is downloading the deps)

Now everything should work just fine, it is time to visit our URL, and…

It is not working.

Ok keep calm, and let’s try to figure out what went wrong.

You can log in your application via ssh. On the Openshift page there is a link “WANT TO LOG IN TO YOUR APPLICATION?” it should expand to show an ssh command; copy it and paste it to your terminal and log in.

Now that we are in we could look for the log of lein. So just move in the right directory >examplenoir(YOUR_NAME_APP)>logs and take a look at the lein.log (cat lein.log)

The problem is that since Openshift is a PaaS you cannot use whatever ip address or port you want— you need to use your allocated address and port. We can just adjust noir/jetty and solve the problem.

It is prettty simple, just modified your server.clj in something like that:

(ns YOUR_NAME_APP.server

  (:require [noir.server :as server]))

(server/load-views “src/YOUR_NAME_APP/views/”)

(defn -main [& m]

  (let [mode (keyword (or (first m) :dev))

        port (Integer. (get (System/getenv) “OPENSHIFT_INTERNAL_PORT” “8080”))

host (get (System/getenv) “OPENSHIFT_INTERNAL_IP”)]

    (server/start port {:mode mode

                        :ns ‘YOUR_NAME_APP

                        :jetty-options {:host host}})))

Now push it again, and wait— it will take a little while…

In the meantime you can log in in the ssh and keep looking at the logs, to see if it is finished.

when you see that means that it is running.

http://examplenoir-siscia.rhcloud.com/

Now just keep coding and make your app ;-)

Here the repository on github

https://github.com/siscia/examplonoir

Filed under DIY PaaS clojure diy clojure openshift noir openshift redhat web web developing webnoir

  1. sisciatech posted this