Running a Clojure Pedestal application on Raspberry Pi model B revision 2

Jul 15, 2019

Compiling a fat JAR (uberjar) from the Pedestal application

Given that we already have a working application with an endpoint (/temperature) we now can deploy it and run it. Thinking on where this app should be run, I noticed that I have sitting around a Raspberry Pi Model B Rev 2. For those who might ask what processing power this little device has, this is fitted with 512MB RAM and an ARM CPU running at 700MHz. Not a very powerful device, but good enough for our little Clojure app.

If you have a Raspberry Pi sitting around and you want to find out what version it is, just type in the command line:

pi@raspberrypi:~ $ cat /proc/device-tree/model

And you should see something similar on your screen:

Raspberry Pi Model B Rev 2pi@raspberrypi:~ $

On the development machine, just go to the root folder of your Pedestal application and run:

➜  pedestal-api git:(master) lein uberjar
Compiling pedestal-api.server
INFO  org.eclipse.jetty.util.log  - Logging initialized @20621ms to org.eclipse.jetty.util.log.Slf4jLog
Created /Users/x/Projects/clojure/pedestal-api/target/pedestal-api-0.0.1-SNAPSHOT.jar
Created /Users/x/Projects/clojure/pedestal-api/target/pedestal-api-0.0.1-SNAPSHOT-standalone.jar

And the output should be a JAR file in target/pedestal-api-0.0.1-SNAPSHOT.jar. This file contains everything that is needed for the app to run on a system capable of running Java 8 applications.

Install Java 8 on Raspberry Pi Model B Rev 2

In this case, the CPU architecture restricts our options in which JRE (Java Runtime Environment) to install. I struggled a little bit until I managed to find a version that works on Raspbian Buster Lite (my OS on Raspberry Pi).

Let’s start clean and remove any traces of the OpenJDK:

sudo apt-get purge openjdk-8-jre-headles

Now let’s install Java 8:

sudo apt-get install openjdk-8-jre-headless
sudo apt-get install openjdk-8-jre

In the end the output should show Java 8 installed

java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1+rpi1-b01)
OpenJDK Client VM (build 25.212-b01, mixed mode)

SSH into the Raspberry Pi

Until we will setup continuous deployments onto our little server (Raspberry Pi) we will do it by hand. We just ssh into the box and copy the uber JAR.

Enable SSH onto Raspberry Pi

My distro of Raspbian comes with SSH disabled by default. This is a headless distro and all you need to do to enable SSH at boot time is to place a file named ssh onto the boot partition of the SD card. No file content is required and no extension. Raspbian will pickup the file at boot time and enable SSH.

More information can be found into official documentation.

Copy the uber JAR via SSH

To copy the uber JAR we will be using scp (scp – secure copy (remote file copy program)).

scp target/pedestal-api-0.0.1-SNAPSHOT-standalone.jar pi@192.168.1.151:/home/pi/clojure-jars

Arguments passed to the scp command:

  • File to copy: target/pedestal-api-0.0.1-SNAPSHOT-standalone.jar
  • Destination: username@host:location

Start the Pedestal app on the Raspberry Pi

To start the Clojure app we need to SSH into the server and run the JAR as any Java archive.

➜  pedestal-api git:(master) ✗ ssh pi@192.168.1.151
pi@raspberrypi:~ $ java -jar clojure-jars/pedestal-api-0.0.1-SNAPSHOT-standalone.jar

It might seem that the command is stuck, but be patient. We need to reminder that this is a less powerful device and it needs some time to start. After the server starts, you should see something like this into your terminal:

pi@raspberrypi:~ $ java -jar clojure-jars/pedestal-api-0.0.1-SNAPSHOT-standalone.jar
INFO  org.eclipse.jetty.util.log  - Logging initialized @93062ms to org.eclipse.jetty.util.log.Slf4jLog

Creating your server...
INFO  org.eclipse.jetty.server.Server  - jetty-9.4.z-SNAPSHOT; built: 2019-04-29T20:42:08.989Z; git: e1bc35120a6617ee3df052294e433f3a25ce7097; jvm 1.8.0_212-8u212-b01-1+rpi1-b01
INFO  o.e.j.server.handler.ContextHandler  - Started o.e.j.s.ServletContextHandler@40e9cd{/,null,AVAILABLE}
INFO  o.e.jetty.server.AbstractConnector  - Started ServerConnector@ce284d{HTTP/1.1,[http/1.1, h2c]}{localhost:8080}
INFO  org.eclipse.jetty.server.Server  - Started @97551ms

This means that your server is started and is ready to accept connections.

Test the app via curl

At this stage, the application is not accessible outside of the Raspberry Pi box, so a way to test it is to use curl (curl - transfer a URL).

pi@raspberrypi:~ $ curl localhost:8080/temperature
{"celsius":0,"fahrenheit":32}

What we achieved so far

  • We packaged a Pedestal web service into an uber JAR
  • We installed Java8 on Raspberry Pi Model B Rev 2
  • We enabled SSH onto the Raspberry Pi and we connected to it
  • We copied the uber JAR onto the server via scp
  • We started the web service and tested via curl

Tags: programmingclojureraspberrypipedestal

Archives

  1. December 2024
  2. Keeping Software Simple to speed up Software Development
  3. October 2024
  4. The Kanban Café - A Story of Flow
  5. A Story on Accidental Complexity in Software Development
  6. February 2024
  7. Maximizing Software Development Productivity: The Power of Flow and Minimizing Interruptions
  8. December 2023
  9. Clean Code in Java: Writing Code that Speaks
  10. Clean Code in Java: A concise guide
  11. Understanding Value Objects in Java: A Brief Guide
  12. August 2023
  13. Must Have on Message Payload
  14. Centralised Management System For Message Schemas
  15. Consuming RabbitMQ Messages with Clojure: A Step-by-Step Tutorial with Tests
  16. January 2023
  17. Running a Spring Boot service with kubernetes
  18. December 2022
  19. Hosting a PWA with Jekyll and Github pages
  20. November 2022
  21. Global Day of Code Retreat
  22. Facilitating a mini Code Retreat
  23. October 2022
  24. The Curse of Optional
  25. September 2022
  26. Testing Spring Boot Microservices - Presentation
  27. March 2022
  28. TDD Workshop
  29. February 2022
  30. Value Objects in Java
  31. Efficient Java
  32. January 2022
  33. Spring Boot testing - Focus on your changes
  34. Product users - Personas
  35. December 2021
  36. Write code fit for testing
  37. November 2020
  38. Running a Spring Boot app with kubernetes
  39. September 2019
  40. Setup GPG on Mac and sign git repositories
  41. July 2019
  42. Running a Clojure Pedestal application on Raspberry Pi model B revision 2
  43. Clojure from zero to hero (part 3) - First endpoint
  44. Clojure from zero to hero (part 2) - A bit of syntax
  45. June 2019
  46. Clojure from zero to hero (1) - explaining project.clj
  47. Clojure from zero to hero (0) - creating a Pedestal app
  48. November 2017
  49. Introduction to Docker
  50. April 2015
  51. Git micro commits
  52. July 2014
  53. Google Glass Development - setup tools, environment and turn on debugging on Glass
  54. June 2013
  55. How To: Get the rendered HTML of a webpage with Python
  56. Set union of two lists in Python