Connect To Hatchbox Rails Console
by Josh Branchaud
My bespoke content platform — still ↗ — is a
Rails app running on a Hetzner server managed via
Hatchbox ↗. When I first set this app up, the way I could
connect to the production console was to SSH into the box, navigate to the
current app directory, and then run rails console. I didn't wait long to set
up a single rake task (and mise ↗ task) to handle "prod
console access".
There is a rails production:console task I can run. I also have it wired up
via mise so that I can access it with the mise run menu or
mise run production:console directly. Here is what
lib/tasks/production.rake looks like:
require "shellwords"
module HatchboxRemote
HOST = ENV.fetch("HATCHBOX_HOST", "[email protected]")
APP_DIR = "/home/deploy/#{ENV.fetch("HATCHBOX_APP", "visualmode-dev-rails-app")}"
# Replaces the current process with `bin/rails <args>` run over ssh in the
# deployed app dir with production env loaded. Never returns.
# tty: true forces a remote tty (needed for interactive consoles); leave it
# off for piped/redirected output, since a tty rewrites newlines as CRLF.
def self.exec_rails!(*args, tty: true)
remote = <<~SH.strip
set -e
app_dir=#{Shellwords.escape(APP_DIR)}
cd "$app_dir/current"
env_file=
for f in "$app_dir/.hatchbox.env" "$app_dir/.env"; do
[ -f "$f" ] && { env_file=$f; break; }
done
[ -n "$env_file" ] || { echo "missing $app_dir/.hatchbox.env (or .env)" >&2; exit 1; }
set -a; . "$env_file"; set +a
RAILS_ENV=production exec bin/rails #{Shellwords.join(args)}
SH
exec("ssh", *("-t" if tty), HOST, remote)
end
end
namespace :production do
desc "Open a rails console on the hatchbox production server"
task :console do
HatchboxRemote.exec_rails!("console")
end
end
There is a lot going on here, but I can break it down into four parts.
-
#exec_rails!is defined as an all-purpose method to execute whateverrails ...command you want against the remote (hatch)box with the full app env loaded. -
remoteis a heredoc that gets assigned a series of shell commands which (a)cdinto the current app directory, (b) load the app's env (via.hatchbox.envor.env), and (c) runbin/railswith whatever args have been passed in specifically targeting theproductionrails env. -
Everything in 2 gets injected into an
execwhich runs through SSH with the-tflag (since the console needs an interactive connection) against theHATCHBOX_HOST(where my public SSH key is authorized). Crucially, theexecmethod replaces the rake process with the interactive SSH process. -
The actual rake task gets to lean on all the above complexity by simply passing
consoletoHatchboxRemote.exec_rails!.
It's nice to have all of this separated out into the HatchboxRemote module
because I then get to reuse it for the dbconsole task. This connects me
directly to the remote PostgreSQL DB via psql.
namespace :production do
# ...
desc "Open a psql session on the hatchbox production database"
task :dbconsole do
# -p passes the password from DATABASE_URL so psql doesn't prompt
HatchboxRemote.exec_rails!("dbconsole", "-p")
end
end
As for how all of this translates to mise, the production:console task in
mise.toml ends up looking like this:
[tasks."production:console"]
run = "bin/rails production:console"
description = "Open a Rails console on the hatchbox production server (override with HATCHBOX_HOST, HATCHBOX_APP)"
depends = ["bundle-install"]
raw = true
I don't show the bundle-install task, but as you might imagine that is a
separate mise task that ensures gem dependencies are installed. The raw = true
is what allows for direct stdin/stdout with the interactive SSH process. The
equivalent production:dbconsole looks nearly identical.
This setup (or something close to it) will work with other providers and server setups besides Hatchbox. Any server that is running a Rails app that you can SSH into and access the env should work with this style of rake task.