burrito-elixir / burrito

Wrap your application in a BEAM Burrito!
MIT License
908 stars 34 forks source link
beam distribution elixir packaging-tool

Burrito 🌯

Hex version badge

Cross-Platform Elixir Deployments

What Is It?

Background

Burrito is our answer to the problem of distributing Elixir CLI applications across varied environments, where we cannot guarantee that the Erlang runtime is installed, and where we lack the permissions to install it ourselves. In particular, we have CLI tooling that must be deployed on-premise, by consultants, into customer environments that may be running MacOS, Linux, or Windows.

Furthermore, these tools depend on NIFs that we need to cross-compile for any of the environments that we support, from one common build server, running in our CI environment.

We were heavily inspired by Bakeware, which lays a lot of the ground work for our approach. Ultimately we implemented and expanded upon many of Bakeware's ideas using Zig.

Feature Overview

Supported Versions:

We provide pre-compiled Erlang/OTP distributions starting from OTP-25.3 onwards for MacOS, Linux, and Windows targets. If you require an older version, please refer to the section about providing custom Erlang/OTP builds.

Technical Component Overview

Burrito is composed of a few different components:

      Burrito Produced Binary
┌────────────────────────────────┐
│                                │
│       Zig Wrapper Binary       │ <---- Compiled from `wrapper.zig`
│                                │
├────────────────────────────────┤
│        Payload Archive         │
│ ┌────────────────────────────┐ │
│ │                            │ │
│ │    ERTS Native Binaries    │ <------ If cross-compiling, this is downloaded from a build server
│ │                            │ │
│ └────────────────────────────┘ │
│                                │ <---- This bottom payload portion is generated by `archiver.zig`
│ ┌────────────────────────────┐ │
│ │                            │ │
│ │   Application BEAM Code    │ │
│ │                            │ │
│ └────────────────────────────┘ │
│                                │
└────────────────────────────────┘

End To End Overview

  1. You build a Burrito wrapped binary of your application and send it to an end-user
  2. The end-user launches your binary like any other native application on their system
  3. In the background (first-run only) the payload is extracted into a well defined location on the system. (AppData, Application Support, etc.)
  4. The wrapper executes the Erlang runtime in the background, and transparently launches your application within the same process
  5. Subsequent runs of the same version of that application will use the previously extracted payload

Quick Start

Disclaimer

Burrito was built with our specific use case in mind, and while we've found success with deploying applications packaged using Burrito to a number of production environments, the approach we're taking is still experimental.

That being said, we're excited by our early use of the tooling, and are eager to accept community contributions that improve the reliability of Burrito, or that add support for additional platforms.

Preparation and Requirements

NOTE: Due to current limitations of Zig, some platforms are less suited as build machines than others: we've found the most success building from Linux and MacOS. The matrix below outlines which build targets are currently supported by each host.

Target Host Host Host Host
Windows x64 Linux MacOS (x86_64) MacOS (Apple Silicon)
Windows x64
Linux
MacOS (x86_64)
MacOS (Apple Silicon)

We support targeting Windows (x86_64) from MacOS and Linux, we do not officially support building ON Windows, it's recommended you use WSL if your development machine is Windows.


You must have the following installed and in your PATH:


Mix Project Setup

  1. Add burrito to your list of dependencies:

    defp deps do
      [{:burrito, "~> 1.0"}]
    end
  2. Create a releases function in your mix.exs, add and configure the following for your project:

    def releases do
      [
        example_cli_app: [
          steps: [:assemble, &Burrito.wrap/1],
          burrito: [
            targets: [
              macos: [os: :darwin, cpu: :x86_64],
              linux: [os: :linux, cpu: :x86_64],
              windows: [os: :windows, cpu: :x86_64]
            ]
          ]
        ]
      ]
    end

(See the Mix Release Config Options for additional options)

  1. Add the releases function into your project function:

    def project do
    [
      # ... other project configuration
      releases: releases()
    ]
    end
  2. To build a release for all the targets defined in your mix.exs file: MIX_ENV=prod mix release

  3. You can also build a single target by setting the BURRITO_TARGET environment variable to the alias for that target (e.g. Setting BURRITO_TARGET=macos builds only the macos target defined above.)

NOTE: In order to speed up iteration times during development, if the Mix environment is not set to prod, the binary will always extract its payload, even if that version of the application has already been unpacked on the target machine.

Mix Release Config Options

Build-Time Environment Variables

Application Entry Point

For Burrito to work properly you must define a :mod in your project's Mix config:

def application do
  [
    mod: {MyEntryModule, []}
  ]
end

This module must implement the callbacks defined by the Application module, as stated in the Mix documentation:

defmodule MyEntryModule do
  def start(_, _) do
   # Returning `{:ok, pid}` will prevent the application from halting.
   # Use System.halt(exit_code) to terminate the VM when required
  end
end

If you wish you retrieve the argv passed to your program by Burrito use this snippet:

args = Burrito.Util.Args.argv() # this returns a list of strings

Maintenance Commands

Binaries built by Burrito include a built-in set of commands for performing maintenance operations against the included application:

Advanced Build Configuration

Build Steps and Phases

Burrito runs the mix release task in three "Phases". Each of these phases contains a number of "Steps", and a context struct containing the current state of the build, which is passed between each step.

The three phases of the Burrito build pipeline are:

Build Targets and Qualifiers

A Burrito build target is a keyword list that contains an operating system, a CPU architecture, and extra build options (called Qualifiers).

Here's a definition for a build target configured for Linux x86-64 that adds extra CFLAGS to the NIF recompile step:

targets: [
  linux: [os: :linux, cpu: :x86_64, nif_cflags: "-DSOME_DEFINE"]
]

Build qualifiers are a simple way to pass specific flags into the Burrito build pipeline. Here is a list of the supported qualifiers:

Tip: You can use these qualifiers as a way to pass per-target information into your custom build steps.

Using Custom ERTS Builds

The Burrito project provides precompiled builds of Erlang for the following platforms:

[os: :darwin, cpu: :x86_64],
[os: :darwin, cpu: :aarch64],
[os: :linux, cpu: :x86_64],
[os: :linux, cpu: :aarch64],
[os: :windows, cpu: :x86_64]

If you require a custom build of ERTS, you're able to override the precompiled binaries on a per target basis by setting custom_erts to the path of your ERTS build:

targets: [
  linux_arm: [
    os: :linux,
    cpu: :aarch64,
    custom_erts: "/path/to/my_custom_erts.tar.gz"
  ]
]

The custom_erts value should be a path to a local .tar.gz of a release from the Erlang source tree. The structure inside the archive should mirror:

. (TAR Root)
└─ otp-A.B.C-OS-ARCH
  ├─ erts-X.Y.Z/
  ├─ releases/
  ├─ lib/
  ├─ misc/
  ├─ usr/
  └─ Install

You can easily build an archive like this by doing the following commands inside the (official Erlang source code)[https://github.com/erlang/otp]:

# configure and build Erlang as you require...
# ...

export RELEASE_ROOT=$(pwd)/release/otp-A.B.C-OS-ARCH
make release
cd release
tar czf my_custom_erts.tar.gz otp-A.B.C-OS-ARCH

Known Limitations and Issues

Phx Tips

In order to run a Phoenix app with Burrito, you may need to edit your runtime.exs file to ensure it will always start up the server:

# This is the default line in runtime.exs, you can remove the if statement wrapping this config line, or check for other conditions:
if System.get_env("PHX_SERVER") do
  config :phx_app, PhxAppWeb.Endpoint, server: true
end

If you do not want to edit runtime.exs just ensure the environment variable PHX_SERVER is set to 1 when you launch your burrito wrapped binary. Otherwise it will simply exit without starting your server application.

Additionally, you should take care to ensure you compile your assets BEFORE you wrap your application with Burrito.

Runtime Requirements

Minimizing the runtime dependencies of the package binaries is an explicit design goal, and the requirements for each platform are as follows:

Windows

Contributing

Welcome!

We are happy to review and accept pull requests to improve Burrito, and ask that you follow the established code formatting present in the repo!

Everything in this repo is licensed under The MIT License, see LICENSE for the full license text.