Posts tagged Lisp

Using Racket Minimal and raco

:: IT, Lisp, Racket

I use Racket Minimal on my smart phone (this describes how to compile the run time for an ARM based system). It’s is a very small installation of Racket (about 36 MB after installation). After installation one only needs to install the packages that are really neded. But this can be a bit tricky because a lot of packages want to install their documentation and other stuff and bring a whole bunch of files on your drive as dependencies.

Some of the packages are divided up into a "-lib", "-doc" (and sometimes "-test") as laid out in the documentation. With these packages it’s easier to only install the implementation.

A small script of mine used only basic modules and relied on rackunit for the tests. On a mobile device the start up time of such a program can be critical. Therefore it is wise to only require the needed packages and to have the source code being compiled to byte code. One could do this with raco setup (which is included in Minimal Racket) but I wanted to have raco make (which is not part of Minimal Racket) available.

The commands of raco are added via a raco-commands variable in packages’ info.rkt file. I looked through the packages of my “full install” and found the package compiler-lib which adds some commands (make, exe, pack, unpack, decompile, test, expand, read, distribute, demodularize) to raco and relies on only a few other packages. As a result the source and binary files need about 3.8 MB on my phone which is okay for me.

To sum up: After a simple raco pkg install compiler-lib I could easily use raco make and raco test to play with my program on my phone.

I played with CHICKEN Scheme, Docker and Alpine Linux

:: IT, Lisp, Scheme, CHICKEN, Docker, Linux

I am looking forward to meet LISP people at the 32c3’s LISP assembly. The last days I played a bit with different Scheme implementations including CHICKEN scheme. The main feature of CHICKEN is that it compiles the Scheme code to C and then creates dynamic libraries and binaries with the C compiler. I thought that combining these binaries with a minimal Docker container could give me a very small deployment. So here are my steps:

Choosing Alpine Linux as a “small” Linux

The smallest Linux image for Docker is undoubtly busybox with a size of about 2.489 MB. But busybox lacks a package manager which makes installing software painful. Therefore I have chosen Alpine Linux which comes with package manager and it’s image’s size is about 5.234 MB. That’s double the size of the busybox image but still quite small compared to the Ubuntu image which is about 266 MB.

Creating a Docker container with CHICKEN

Alpine Linux comes with the musl libc and I thought it would be best to compile all the CHICKEN stuff with that libc. Therefore I created a Docker container with gcc and all the other stuff with this Dockerfile (Github repository):

FROM alpine:3.2

RUN apk update && apk add make gcc musl-dev 
RUN wget -O - http://code.call-cc.org/releases/4.10.0/chicken-4.10.0.tar.gz | tar xz

WORKDIR /chicken-4.10.0

RUN make PLATFORM=linux && make PLATFORM=linux install

RUN rm -fr /chicken-4.10.0 

WORKDIR /

CMD ["csi"]

This image is quite big (about 161.7 MB) and is available for download at the Docker Hub.

Compiling some CHICKEN code

For testing purposes I wanted a minimal web server running in the Alpine Linux image. Therefore I looked through the egg index and found spiffy. I fired up the chicken-alpine container (but I used ash as command instead of the csi Scheme interpreter) and created a small web server that serves some static pages. I wrote a main.scm:

(use spiffy)
(start-server)

and added some static pages to a ./web sub-directory. Then everything had to be compiled and prepared for deployment:

chicken-install spiffy
csc -deploy main.scm
chicken-install -deploy -p $PWD/main spiffy

Deploy in a fresh Alpine Linux image

After the compilation I copied the main and web directories on my host machine using docker cp and created the following Dockerfile:

FROM alpine:3.2

ADD main /main
ADD web main/web
WORKDIR main

CMD /main/main

and let docker build -t krrrcks/spiffy-test . do the job. The size of the resulting image is about 12.37 MB and that’s pretty small. I uploaded that image to the Docker Hub as well.

To serve the pages I did a docker run -d -p 8080:8080 krrrcks/spiffy-test (spiffy listens on port 8080 in the default install) and browsed my static pages.

How to use GET Bucket location on Amazon S3 with Racket

:: IT, Lisp, Racket, AWS, S3

In Racket I want to iterate over my buckets in Amazon S3. They are located in different regions. So how do I get my bucket’s location/region? In the API Reference there is a call GET Bucket location. I use Greg’s AWS library for Racket and this library authenticates its calls with signature version V4. But V4 requires the user to know the region to correctly sign the request. So I need to know the region to ask Amazon S3 for the region where the bucket is located. Otherwise Amazon S3 responds with an error:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
 <Code>AuthorizationHeaderMalformed</Code>
 <Message>The authorization header is malformed; the region 'us-east-1'
is wrong; expecting 'eu-central-1'</Message>
 <Region>eu-central-1</Region>
 <RequestId>XXXX</RequestId>
 <HostId>XXXX>
</Error>

After some search on the net I found a post on Stackoverflow that helped to solve that issue: If I use the URL format (instead of the normally used virtual host format) I could get the location of any bucket. Every region responds with a LocationConstraint answer.

Therefore a code snippet for Racket could be:

(define (get-bucket-location bucket)
  (parameterize
      ([s3-path-requests? #t])
    (define xpr (get/proc (string-append bucket "/?location") read-entity/xexpr))
    (and (list? xpr)
         (= (length xpr) 3)
         (third xpr))))

For example:

> (get-bucket-location "my-bucket-somewhere")
"eu-central-1"

PS: I think official Amazon S3 documentation could be a bit more verbose on the issues with GetBucketLocation and signature V4.

Update: Greg added a bucket-location function to his great library

How to run Racket on the Raspberry Pi 2

:: IT, Lisp, Racket

I got a Raspberry Pi 2 Model B to play with. I used Raspbian image as operating system. I was wondering how difficult it is to get Racket running on the Raspberry Pi. I downloaded the Unix source + built packages tarball from Racket’s homepage because I only wanted to compile the core of Racket. After unpacking the tarball I was suprised that the instructions were quite short:

From this directory (where the `configure' file is), run the following
commands:

  mkdir build
  cd build
  ../configure
  make
  make install

Between make and make install I had to wait for about 40 minutes but then everything was fine and I could even use DrRacket on the Raspberry Pi:

DrRacket on Raspberry Pi

DrRacket on Raspberry Pi

Very nice and easy to get Racket running on ARM.

PS: Because the Raspberry Pi 2 Model B has an ARMv7 processor the binary runs on my Jolla smart phone as well.

Running Racket on AWS Lambda

:: IT, Lisp, Racket, AWS

I started to use AWS for some projects recently. But I only use few of their services. From time to time I look into some of there services and wonder if they are useful for my tasks. I looked into AWS Lambda, "… a compute service that runs your code in response to events and automatically manages the compute resources for you, making it easy to build applications that respond quickly to new information." Nowadays these “lambda functions” could be written in NodeJS or Java. When I was looking for a roadmap of the supported languages I found an interesting blog post by Ruben Fonseca. He explaind how to run Go code on AWS Lambda.

I tried the same with Racket and wrote a short Racket programm test.rkt:

#lang racket/base

(display (format "Hello from Racket, args: ~a~%" (current-command-line-arguments)))

Then I used raco to create a binary test:

raco exe --orig-exe test.rkt

I took the NodeJS wrapper from Ruben’s blog post and put it in a file main.js:

var child_process = require('child_process');

exports.handler = function(event, context) {
  var proc = child_process.spawn('./test', [ JSON.stringify(event) ], { stdio: 'inherit' });

  proc.on('close', function(code) {
    if(code !== 0) {
      return context.done(new Error("Process exited with non-zero status code"));
    }

    context.done(null);
  });
}

Then I put both files in a zip archive, created a new AWS Lambda function, uploaded the zip file and invoked the function:

Invocation of AWS Lambda function

Invocation of AWS Lambda function

Fine!

PS: Only question is: When is AWS Lambda coming to the region eu-central-1, located in Frankfurt?

Upate (2016–03–15): AWS Lambda is now available in the EU (Frankfurt) region!