Tiny helper script to start Livebook connected to production (fly.io) or development

Hey friends, wanted to share a tiny shell script I’ve been using to start Livebook with easy access to either a running production server or a development server. This is based on the fly.io guide and offers nothing really new or novel beyond some small conveniences:

  • Starts Livebook with a default data directory set.
  • Sets the default runtime to be Attached, configured for your development or production server, so that you can immediately get going in that environment without having to set anything up.

Pre-reqs for development server:

  • For Livebook to connect, you need to start it with an sname and cookie:
$ iex --sname my-server --cookie my-server-cookie -S mix phx.server

Pre-reqs for production fly.io server:

Script:

#!/bin/sh
# connect-live.sh

fly_app=my-fly-app
fly_cookie=$ERL_COOKIE
dev_app=my-server
dev_app_cookie=my-server-cookie
data_path=notebooks
home=notebooks

case "$1" in
  "prod")
    ip=$(fly ips private --app $fly_app | cut -f 3 | sed -n 2p)
    node=$fly_app@$ip

    ERL_AFLAGS="-proto_dist inet6_tcp" \
      livebook server \
        --name livebook@127.0.0.1 \
        --default-runtime attached:$node:$fly_cookie \
        --data-path $data_path \
        --home $home
    ;;

  *)
    node=$dev_app@$(hostname)
    livebook server \
      --default-runtime attached:$node:$dev_cookie \
      --data-path $data_path \
      --home $home
    ;;
esac

Make sure to chmod +x connect-live.sh, then getting things going is as easy as:

$ ./connect-live.sh # start with development defaults
$ ./connect-live.sh prod # start with production defaults

As a final note: I do next to no shell scripting and the above may be total crap, but it took me a bit longer than I’d like to get this kind of thing set up in the first place and now it’s very convenient & easy, so I thought I’d share!

5 Likes