Persistent Geoserver on Openshift

This post is going to be a technical one. If you’re not interested in servers and cloud platforms and stuff like that, please enjoy this picture of alpacas with haircuts instead:

Shaved=Yes, Yak=No

Update 7/2013: I don’t use Geoserver on Openshift anymore – I’m now running static tiles generated with TileMill and hosted on Amazon S3. I thought the whole JBoss/Geoserver combo was too taxing on the little application gears, but I think I know what was going on now – if you’re on the free tier your applications will be hibernated if they’re not accessed for 12 hours. And waking up that whole stack is takes a bit of time. So if you want to dork around with GeoServer every now and then, proceed. If you need something that will always be up and running, you’ll need to spend a little money.

My Colorado Gravel Roads site uses Geoserver to create the map tiles from the road data I assembled. OpenShift provides a simple and easy way to get Geoserver running. In a nutshell, all you have to do is create a JBoss application, add a patched Geoserver WAR to your application’s local git repository, sync it, and you’re done.

There is one catch – OpenShift applications do not persist. Geoserver stores all of its data in a folder unpacked from the WAR, so any changes made to that data are lost if the server is restarted. OpenShift does have its own persistent user data directory, so what we need to do is move GeoServer’s data directory into it. OpenShift stores the user data directory in the environmental variable $OPENSHIFT_DATA_DIR, so we just need to communicate that location to GeoServer.

There are several ways to set the GeoServer data directory, as described here. Setting a context variable in web.xml seems like a natural way to do things, but the environmental variable isn’t available there. OpenShift has a convenient solution – you can run special pre-deploy scripts that can set things up before the application is launched. So you simply run a script that sets the Geoserver data directory environmental variable to point to a directory in the user data directory.

So let’s get going – you’ll need to sign in to your OpenShift account, create a JBoss AS7.1 app (for this example I’ll call it ‘geoserver99’), and create your local git repository. You’ll need to run Geoserver 2.1.3 (Geoserver 2.14 doesn’t start for some reason) and it will need to be patched to run in JBoss – the easiest thing is to grab the already modified ROOT.war from Jaku’s blog.

First a little bit of prep work. From the local application directory:

rm -rf pom.xml src/main

Move the ROOT.war file into the deployments directory:

mv path/to/your/ROOT.war deployments/

Sync up with git:

git add .
git commit -m "init"
git push

After git finishes, you should be able to browse to your app and see the GeoServer login page. It might take a minute to start up the application, so if you get a blank page just wait a bit.

Now lets ssh into OpenShift so we can move the data directory. You can find the ssh command from the application’s page in the OpenShift console – it will be something like:

ssh 22f911b5470843ef975af0a1eda37f0d@geoserver99-yourcloud.rhcloud.com

Lets make a directory to use for GeoServer:

mkdir ${OPENSHIFT_DATA_DIR}gs-data

Unpack the war, copy the data directory, clean up, and leave:

cd geoserver99/repo/deployments/
unzip ROOT.war
cp -r data/* ${OPENSHIFT_DATA_DIR}gs-data/
rm -rf data index.html META-INF/ WEB-INF/
exit

Back in your local application directory, edit .openshift/action_hooks/pre_start_jbossas-7 and add the following line:

export GEOSERVER_DATA_DIR=${OPENSHIFT_DATA_DIR}gs-data

Save that file and do the git dance again:

git add .
git commit -m "move data dir"
git push

Once the server restarts, you can test to see if Geoserver is persistent by logging in, changing something, and restarting the server with the rhc command:

rhc app restart -a geoserver99

If your changes are still there after it restarts, you’re good to go!

1 thought on “Persistent Geoserver on Openshift

Leave a comment