Running the bepasty pastebin on Fly.io

For many years I was running a bepasty-server on a small Hetzner VPS. There is nothing wrong doing this, but updating the server and upgrading the virtualenv every year for a new Python version, feels like a chore that could be avoided. So I moved the bepasty-server to Fly.io.

The ingredients for this are a bepasty.conf, a fly.toml, a Procfile and a requirements.txt. I will go into detail on them in this post.

First the bepasty.conf. When using this you should clearly change the domain, the secret and probably the passwords in there:

STORAGE_FILESYSTEM_DIRECTORY = '/data'
SITENAME = 'your-domain.example.com'   # i.e. your fly.dev domain
SECRET_KEY = 'somelongrandomstringforthecookies'
SESSION_COOKIE_SECURE = True
DEFAULT_PERMISSIONS = 'read'
PERMISSIONS = {
    'passwordforupload': 'create,read,delete',
    'passwordforadmin': 'admin,list,create,read,delete',
}

I first used the fly.io app domain to setup up everything and replaced it later with my own domain. Bepasty uses passwords to increase the allowance higher than the default (read-only).

Next the fly.toml.

app = 'mfa-paste'
primary_region = 'ams'

[build]
  builtin = "python"

[build.settings]
  pythonbase = "3-slim"

[[mounts]]
  source = "bepasty_data"
  destination = "/data"
  processes = ["app"]

[http_service]
  internal_port = 8000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[[vm]]
  size = 'shared-cpu-1x'
  memory = '256mb'

Of course your app should be named differently. I used the Python builder, that is starting a Procfile as default. The default Python here is a bit old, so I set it to always use the newest stable Python 3.x with the current slim Debian (this is the name of a Python Docker hub tag).

The volume needs to be created before a deployment will work with for example: flyctl volumes create bepasty_data --size 1. The last years showed me that 1GB is enough for me.

The app is configured to auto-stop when not used. A stopped app is started on request fast enough to not be an issue here. And this saves not only costs, but no cpu/memory is blocked for no usage.

I set the memory to 256MB for the fly instance. This is a lot less than the Hetzner VPS had, but seems to be enough. To test this I uploaded a 235mb file to my new pastebin and the memory usage didn't increase at all.

Next the Procfile:

web: BEPASTY_CONFIG=/app/bepasty.conf gunicorn bepasty.wsgi --name bepasty --workers 2 -b 0.0.0.0:8000 -k gevent

I use gunicorn to run bepasty here. Mostly because I know it, but there are a lot of alternatives, i.e. uvicorn, uwsgi or hypercorn.

And finally the requirements.txt:

bepasty[magic]
gevent
gunicorn

Obviously bepasty with the magic file recognition feature. And gunicorn with gevent to run the bepasty server.

For the future I hope running fly deploy every other month should be enough to get the newest bepasty and Python combination.