Fly series: Custom scripts

by Paulo Gonzalez

2025-01-09 | elixir bash productivity ci automation

Fly.io has become the go-to platform for apps I build due to their involvement with open source and support of the Elixir ecossystem. In this series, I will be sharing a couple of lessons learned when working with Fly. The experience has been positive and I'll continue to use and suggest Fly until further notice.

For this post, I'll focus on utility scripts that have saved me some mental load when moving across projects. Yes, they could be made shorter. Yes, lots of different solutions, but, this is where we are at the moment, and that's alright! All scripts are from Pista's point of view in the examples below, direct copies from the repo.

detect_os.sh - For all Fly related scripts, I have a script that derives the OS that is running it and chooses which command to run. Rather than going through the rabbithole as to WHY the alias doesn't get set, I use the script and call it a day:

#!/bin/bash

detect_os() {
  if [[ "$OSTYPE" == "darwin"* ]]; then
    # MacOS
    echo "fly"
  else
    # Linux or other Unix-like OS
    echo "$HOME/.fly/bin/flyctl"
  fi
}

prod_deploy.sh - Let's deploy to prod:

#!/bin/bash

source "$(dirname "$0")/detect_os.sh"

FLY_COMMAND=$(detect_os)

$FLY_COMMAND deploy

prod_logs.sh - Let's tail logs for Pista:

#!/bin/bash

source "$(dirname "$0")/detect_os.sh"

FLY_COMMAND=$(detect_os)

$FLY_COMMAND logs -a pista

prod_console.sh - Let's get console access to a running Pista vm:

#!/bin/bash

source "$(dirname "$0")/detect_os.sh"

FLY_COMMAND=$(detect_os)

$FLY_COMMAND ssh console --pty -C "/app/bin/pista remote"

prod_psql.sh - Let's get access to Pista's database (Postgres):

#!/bin/bash

source "$(dirname "$0")/detect_os.sh"

FLY_COMMAND=$(detect_os)

$FLY_COMMAND postgres connect -a pistadb -d pista

ci.sh - Here is a non-Fly related one, but helpful nonetheless. Here is an example of a small Continuous Integration (CI) script:

#!/bin/bash

set -e

mix format --force --check-formatted \
  && mix compile --force --warnings-as-errors \
  && mix test

Remember to make the scripts executable chmod +x script_name.sh and have fun!

Thanks for reading!