zachallaun

zachallaun

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!

Most Liked

zachallaun

zachallaun

Script updated for Livebook 0.8.1 using environment variables instead of flags:

#!/bin/sh

# Usage:
#
# Development:  ./connect-live.sh
# Production:   ./connect-live.sh prod

fly_app="my-app"
fly_cookie=$ERL_COOKIE
dev_app="my-app"
dev_cookie="dev-cookie"

export LIVEBOOK_HOME="notebooks"
export LIVEBOOK_DATA_PATH="notebooks"

case "$1" in
  "prod")
    echo "[PROD] Starting Livebook..."
    fly_ip=$(fly ips private --app $fly_app | cut -f 3 | sed -n 2p)
    fly_node="$fly_app@$fly_ip"
    export LIVEBOOK_DEFAULT_RUNTIME="attached:$fly_node:$fly_cookie"
    export LIVEBOOK_NODE="livebook@127.0.0.1"
    export LIVEBOOK_DISTRIBUTION="name"
    export ERL_AFLAGS="-proto_dist inet6_tcp"
    ;;
  *)
    echo "[DEV] Starting Livebook..."
    dev_node="$dev_app@$(hostname)"
    export LIVEBOOK_DEFAULT_RUNTIME="attached:$dev_node:$dev_cookie"
    ;;
esac

livebook server
zachallaun

zachallaun

Nice tip on the password manager! I personally use direnv, so project secrets are kept in an .envrc that’s gitignored.

Some context first: my primary project is a site for our business of which I’m the sole developer, so I’m able to play some things a little more fast-and-loose than I might otherwise. :slight_smile:

Two primary use-cases for me:

  1. Debugging. When something’s reported, it’s really convenient to just be able to poke around and see what’s up, what state things are in, etc. instead of having to rely on what Zach of yesterday thought to log.

  2. Performing one-off or infrequent tasks that I haven’t gotten around to making a UI for. Basically using Livebook as a bespoke admin interface. There are some things that are really easy in code, e.g. nested data structures, that are kind of a pain in the ass otherwise, e.g. nested forms. This lets me play around with new features that have a business-logic implementation but don’t yet have a nailed down UI.

Overall I’ve found it extremely productive and pretty indispensable as a solo dev! As mentioned, if I were working on a larger team and hadn’t written literally all of it myself, I’d exercise more caution on making sure that I don’t break something.

That said, I do have a scratch notebook that I use for all this that has this nice warning at the top:

Where Next?

Popular in Guides/Tuts Top

sergio
Wrote this guide on how to integrate DropzoneJS with Phoenix Liveview. Hope it helps someone out! https://sergiotapia.com/dropzonejs-dir...
New
kuon
I have a page with a large state that is loaded from DB, let’s call that “data”. I have a root live view that load “data” on mount and r...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
pinksynth
Whenever tests have to interact with an Ecto.Repo, sometimes it’s necessary to test a few different branches or paths that data can take....
New
danschultzer
I wrote this blog post based on our experiences setting up continuous delivery for our first production umbrella Phoenix app. Coming from...
New
Eiji
Hey, today I give amnesia library a try and found a few problems. I would like describe how to setup it properly and solve problems which...
New
dmitriid
This is not a question, but a post for anyone who may need it in the future. Sometimes some browsers may mangle the filename if it’s non-...
New
zookzook
If you want to use the MongoDB in your next Killer-App-Project, but you did not dare ask because otherwise many would advise you to use P...
New
nietaki
Just a quick heads up: There seems to be a bug in Erlang/OTP 21.3, which can cause some errors when making http requests. If you’re using...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement