Deploying Haskell

tl;dr  use heroku-buildpack-stack for Heroku deployment.

You just built a cool toy haskell app and now you want to show to the world that Haskell rocks..!!!
Welcome to the hard part :P .. or at least it used to be :)

I had just written my first web server in Haskell and i was looking for deploying it.
I had a few apps on Heroku and it was my first choice as well.
I easily found HaskellOnHeroku on the web and i knew that golden sun is not far..

HaskellOnHeroku uses build-pack hoh-buildpack.

Alas, it did not supported `ghc-7.10.3` at that time. :( 

It was also not using `stack` (which is ok).

HaskellOnHeroku's buildpack is based on tool halcyon and it had no support for Max OSx >= 10.11
so i could not hack on it on my own system.

But nevertheless, i used it and pushed to heroku but then came :

remote: -----> Restoring sandbox directory
remote: -----> Locating sandbox directories
remote:    *** ERROR: Cannot build sandbox directory
remote:    *** ERROR: Failed to deploy app
remote:    *** ERROR: Deploying buildpack only
remote:
remote:        First, set up private storage:
remote:        $ heroku config:set HALCYON_AWS_ACCESS_KEY_ID=...
remote:        $ heroku config:set HALCYON_AWS_SECRET_ACCESS_KEY=...
remote:        $ heroku config:set HALCYON_S3_BUCKET=...
remote:
remote:        To continue, build the app on a one-off PX dyno:
remote:        $ heroku run -s PX build
remote:


I need AWS S3 buckets to store the .stack-work caches for faster recompilation. :(
I did not had that. :( poor guy.

Well, i did what others do in such a situation. I started looking for alternatives.
Ok, DigitalOcean ( DO ) is cheap; lets create one droplet.

DigitalOcean's documentation is very good and everything was a breeze.
I took 512 MB Ram machine and ran my favourite command `git clone <my-app>`.

Installing stack was a breeze. Compiling project was not.
Setting up a machine for running a binary is hard work. I was getting
package-not-found errors one after another. I would like to shout out for good
work done by `stack-team` that errors were very readable. Many times hints to
solutions were also great.

Missing packages started with : `gcc, libcurl4-openssl-dev, libstdc++-5-dev, g++`
Note that in your case this list can be small or big as it depends upon which packages
you are using.


But then came next blow :
`Process exited with code: ExitFailure (-9) (THIS MAY INDICATE OUT OF MEMORY)`

Oh.. my 512 MB Ram.

well, at this point i was getting disappointed when i realized that DO gives us SSDs. :)
StackOverflow : user316146 at StackOverflow  has answered this :

```
dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
swapon /swapfile 
```

And i ran the `stack build` again.

Package-not-found hit me one more time with `libz-dev`.
Not to mention that this whole process (end to end) took a lot of time.

And meanwhile i started reading more about deployment with haskell and found :
reddit-thread-on-heroku-buildpack-using-stack

People were talking about docker and halcyon.
But down below, something is there.. heroku-buildpack-stack
Some noble soul `mfine` has created this for me.. yes for me.. :)
tears flowing down my cheeks.. thank you mfine

 I again started using this buildpack, added Procfile for heroku and scaled web dyno.

 While DO was still building, i pushed to heroku.

Both nearly finished at same time, and now i had two servers running.. i got more
than i could want.. such is life.

I finally went with heroku.. but here is the command history of DO if anyone wants
to have a look. And ofcourse you can see haskell app at : https://sanskell.herokuapp.com/


Note that DO was also good, but i did not wanted to build my project from scratch if something goes wrong.

   16  git clone https://github.com/ashishnegi/sanskell.git
   17  cd sanskell/
   18  stack build
   19  apt install haskell-stack
   20  ls
   21  stack setup
   22  apt install gcc
   23  stack setup
   24  stack path
   25  ls
   26  stack build
   27  curl

       ....
   33  apt-get install libcurl4-openssl-dev
   34  stack build
       ...

   39  apt install libstdc++-4.8-dev
   40  stack build
   41  ls /usr/include/c++/4.8/
   42  apt search stdc++  | grep dev
       ...

   47  apt install libstdc++-5-dev
   48  stack build

       ...
   53  stack build
   54  apt install g++
   55  stack build
   56  /root/sanskell/.stack-work/logs/regex-tdfa-1.2.2.log
   57  cat /root/sanskell/.stack-work/logs/regex-tdfa-1.2.2.log
   58  stack build
   59  ls
   60  dd if=/dev/zero of=/swapfile bs=1M count=1024
   61  mkswap /swapfile
   62  swapon /swapfile
   63  stack build

       ...

   70  apt install libz-dev
   71  stack build
   72  AppEnv=prod
   73  $AppEnv
   74  AppEnv=prod PORT=8034 /root/sanskell/.stack-work/install/i386-linux/lts-6.12/7.10.3/bin/sanskell-exe
   75  exit
   76  history

Comments