Getting started
On this page
This guide takes an application from nothing to a first hot deploy, then to a two-person
rule. It assumes you ship with mix release (or run mix run in development) and have
Elixir 1.17+ (on 1.17, add {:jason, "~> 1.4"} to your deps: the JSON module arrived in 1.18).
If you would rather watch it work first, run the examples:
elixir examples/pulse/demo.exs moves a three-node cluster through eleven new versions and back
and restarts it, while a checker proves nothing broke (about 4 minutes warm; longer the first
time: it builds Rust and fetches dependencies). For one change followed from code to deploy to
rollback, read A change, step by step.
How the pieces fit
private key (yours, never on a server)
│ signs
your machine ───▶ patch (only the beams that changed) ───▶ the node
mix hotcode.* holds PUBLIC keys only,
verifies, then loads
Two roles, two sets of secrets:
- Signers hold private keys. They decide what runs.
- The node holds public keys and a policy (
allowed_prefixes,audience). It decides whether it will accept a signed patch at all. It cannot create one.
1. Add the dependency
Hotcode is not on Hex yet. Until it is, use a path or git dependency, for example
{:hotcode, path: "../hotcode"} or {:hotcode, github: "filipecabaco/hotcode"}, in place of
{:hotcode, "~> 0.1"} below; mix igniter.install hotcode applies once it is published.
With Igniter: one command adds the dependency and writes the
node's configuration (audience, allowed_prefixes, store_dir, threshold) to
config/runtime.exs, steps 1 and 3 below. allowed_prefixes gets your module prefix, MyAppWeb
for a Phoenix application (lib/my_app_web.ex or a :phoenix dependency), and every other
top-level namespace defined under lib/; the installer prints the list. Check it:
mix igniter.install hotcode --audience production
It adds Credo and the Hotcode rules too (--no-credo to skip), and it does
not choose a signing key for you: a node with none refuses every deploy, so go on to step 2.
By hand:
# mix.exs
defp deps do
[
{:hotcode, "~> 0.1"},
# only needed if you want the HTTP deploy endpoint
{:plug, "~> 1.20"},
{:bandit, "~> 1.12"}
]
end
Without :plug and :bandit you can still deploy over Erlang distribution (see
Transports), but the HTTP endpoint is the recommended transport.
2. Create a signing key
Every person who may deploy makes their own key, on their own machine:
$ mix hotcode.gen_key alice
wrote alice.key (private, keep it secret) and alice.pub
fingerprint: 4a7cd66871f7376c
config :hotcode, trusted_keys: [%{name: "alice", public: "QwsIC7hunlG7OYwKtkI7..."}]
alice.key is created with mode 0600 and the task refuses to overwrite an existing key, or an
existing alice.pub unless you pass --force.
Keep it off servers. The line starting config contains only the public half, and is
what you give to the node.
3. Configure the node
# config/runtime.exs
config :hotcode,
trusted_keys: [%{name: "alice", public: "QwsIC7hunlG7OYwKtkI7..."}],
audience: "prod",
# every top-level namespace you ship: a Phoenix app needs both, "MyApp" does not match "MyAppWeb"
allowed_prefixes: ["MyApp", "MyAppWeb"],
store_dir: "/var/lib/my_app/hotcode",
# the server listens on loopback unless you give it an address, and plain HTTP is refused off
# loopback: bind the interface the deploy tool reaches, with a certificate
server: [
port: 4747,
ip: {0, 0, 0, 0},
tls: [certfile: "/etc/ssl/app.pem", keyfile: "/etc/ssl/app.key"]
]
| key | what it does |
|---|---|
trusted_keys |
the public keys that may sign. Required. |
audience |
the environment's name; every signed patch must carry it, so a patch signed for staging is useless against production. Required. |
allowed_prefixes |
which modules a patch may touch. "MyApp" matches MyApp and MyApp.*, not MyAppWeb: list every top-level namespace you ship (MyAppWeb for Phoenix controllers, LiveViews and channels). A changed module that no prefix matches is left out of the patch (not shipped). OTP, the Elixir standard library and Hotcode are always refused. Required. |
store_dir |
where applied patches, the replay counter and the audit log live. Without it a restart loses patches. Recommended. |
server |
the HTTP endpoint. Plain HTTP is refused off loopback, so use tls: for anything reachable. |
Until the three required keys are set, every deploy is refused, so a freshly installed node accepts code from no one.
The full list is in the Operations guide.
4. Ship one normal release
Hotcode upgrades a node that is already running. The first version still ships the way you ship today. Make sure the release you build is the one your later deploys compare against.
If you use mix release, nothing special is needed: releases load every module at boot,
which is what the node reports when it is asked what it runs.
5. Deploy a change
Change some code, then, from the project directory, built with the same Elixir and OTP
as the running release (normally MIX_ENV=prod):
$ mix hotcode.deploy https://host:4747 --key alice.key --dry-run
patch 8f2c1d0: 2 changed, 0 new, 0 removed (8185 bytes)
~ Elixir.MyApp.Session
~ Elixir.MyApp.Session.State
1 process(es) will be suspended and migrated
--dry-run asks the node what it would do, including anything risky (see
Troubleshooting for what the findings mean), and changes nothing.
Remove it to deploy:
$ mix hotcode.deploy https://host:4747 --key alice.key
patch 8f2c1d0: 2 changed, 0 new, 0 removed (8185 bytes)
deployed: ... "migrated" => 1, "ms" => 8, "ok" => true
What the deploy did:
- the task compiled your project and asked the node what it runs;
- it built the smallest patch (only beams whose code differs) and signed it;
- the node verified the signature, audience, expiry, replay counter, the code the patch was built against and the module policy;
- it suspended the processes of changed modules, loaded the new code atomically, migrated their state and resumed them. Open connections were never touched;
- it persisted the patch (so a restart re-applies it) and wrote an audit entry.
To undo it, with the same authority a deploy needs:
$ mix hotcode.rollback https://host:4747 --key alice.key
6. Require two people
Set threshold: 2 in the node config and add the second key to trusted_keys. Now one
signature is not enough, so split the flow so each person reviews and signs for themselves:
alice$ mix hotcode.build https://host:4747 --key alice.key --out patch.hcp
alice$ mix hotcode.sign patch.hcp --key alice.key
# hand patch.hcp to bob however you like: it is a file
bob$ mix hotcode.sign patch.hcp --key bob.key # shows what he is signing, asks first
bob$ mix hotcode.push patch.hcp https://host:4747 --key bob.key
mix hotcode.sign prints the audience, version, expiry, whether it overrides safety
checks, which applications it may restart and every module it replaces or removes, then
asks. It also refuses to sign an envelope whose statement does not match the code inside
it, so a first signer cannot trick a second one. The patch expires after 15 minutes
(--ttl-minutes on build), so review it promptly.
Transports
| target | how | notes |
|---|---|---|
https://host:4747 |
signed HTTP requests | Recommended. Needs --key (to sign requests) |
my_app@10.0.0.5 |
Erlang distribution | Needs --cookie. The cookie is full trust: anyone with it can already run anything on the node, so only use this inside a network you control |
Either way, whether code is accepted is decided by the signatures inside the patch, never by the transport.
The HTTP endpoint starts when the Bandit module can be loaded and trust is configured.
Hotcode does not start :bandit or :plug itself: keep them ordinary dependencies of your
application (a release that lists bandit: :load loads the module without starting its
application).
Reaching the node
The server listens on loopback (127.0.0.1) unless server: [ip: ...] says otherwise, and
serves plain HTTP only on a loopback address (127.x.x.x or ::1). Anything else needs tls:.
Ways to reach it from the machine that deploys:
- SSH tunnel. Keep the default loopback listener, no certificate needed:
ssh -N -L 4747:127.0.0.1:4747 deploy@host, then deploy tohttp://127.0.0.1:4747. fly proxy.fly proxy 4747:4747 -a my-appforwards a local port to one Machine's private address (--selectpicks which Machine), so the server must listen there (ip: {0, 0, 0, 0, 0, 0, 0, 0}on Fly's IPv6 network) and, being off loopback, needstls:. The client checks the certificate's name: issue it forlocalhost, deploy tohttps://localhost:4747and trust it with--cacert. One proxy per Machine.- A tunnel sidecar (Cloudflare Tunnel, Tailscale Funnel, ngrok) running on the same host:
point it at
http://127.0.0.1:4747, so the node keeps a loopback listener and TLS ends at the tunnel; deploy to the tunnel'shttps://hostname. Put the hostname behind the tunnel's access control as well if it offers one that a command-line client can pass (Cloudflare Access service tokens need headers thehotcodeclient does not send: use an IP allow-list, or Tailscale, instead). - A reverse proxy (nginx, Caddy, a load balancer). On the same host, forward to
http://127.0.0.1:4747; on another host, forward HTTPS to the node's TLS listener (plain HTTP to a non-loopback address is refused unlessinsecure_http: true). Forward the path unchanged (the client signs/deploy,/plan, ...; a path prefix on the proxy must be stripped before the node sees it), pass thex-hotcode-*headers through, allow request bodies up to 64 MB, and route to one node: each node issues its own challenge, so a load balancer that sends the challenge and the signed request to two different nodes gets a401. Deploy to each node by its own address. - Tailscale or a VPN: bind the tailnet address with
tls:. See Deploying from CI.
IPv6. The server listens on an IPv6 address given as an 8-tuple ({0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0}), without an :inet6 option: checked on OTP 27, 28 and 29 (not on
26). The hotcode client and mix hotcode.* tasks try IPv6 first, then IPv4 (their own :httpc
profile with ipfamily: :inet6fb4), so an IPv6 literal such as http://[::1]:4747, or a name
that resolves only to IPv6, works as well as an IPv4 address.
Releases and development nodes
- Release (
mix release). Loads all modules at boot. Build deploys in the same environment as the release (same OTP and Elixir; ideally the same path, see Troubleshooting). - Development (
mix run). Modules load lazily. Hotcode handles this (it reports the on-disk hash of modules that are not yet loaded), but you must deploy from a different build directory than the node runs from. If you deploy from the node's own_build, the code on disk no longer matches what is loaded, and the deploy is refused because rollback could not be guaranteed. Theexamples/pulseproject shows the right layout.
Where next
- How it works: what happens inside a deploy, and what happens when something goes wrong in the middle of one.
- State migration: structs,
Hotcode.Versioned,code_change/3. - Testing your hot deploys: deploy the next version under your processes in a test, before a node sees it.
- Security: the threat model and a deployment checklist.
- Operations: configuration, audit log, kill switch, clusters.