Operations
elements man deploy/operationsServices, migrations on production, redirects, packages, the preDeploy hook, and logging in to a machine by hand.
Services
Custom backend services deploy alongside the main app. Each service runs as a systemd unit, listens on its own port, and routes through the load balancer based on its declared domains.
config.jsocdeploy: { services: [ { name: "transcode-worker", build: "go build -o services/transcode-worker/bin/transcode-worker ./cmd/transcode", bin: "services/transcode-worker/bin/transcode-worker", port: 8082, domains: ["transcode.example.com"], }, ], }
build runs locally before any SSH. The output binary at bin is then synced
to each machine. If build fails, the deploy aborts before any machine is
touched.
Migrations on Production
Pending migrations apply in filename order, each in its own Postgres transaction together with the record that it applied. If migration 7 of 10 fails, 7 rolls back, 1 through 6 stay applied, and the deploy stops before the release swap, so the running release keeps serving. Ship the fix and the next deploy applies 7 through 10. The running release serves traffic while migrations apply, so write each migration to keep it working against the new schema.
Migrations are forward-only. There is no automatic rollback for a migration that has already succeeded. If a release ships a bad migration, fix it with a new migration.
The new release starts serving traffic once the app database schema is ready, however long the migrations take.
One deploy migrates a database at a time: Elements holds a Postgres advisory lock on the database for the whole run.
CREATE INDEX CONCURRENTLY, REINDEX CONCURRENTLY, and VACUUM go in a
migration tagged @migration tx=false, which applies in the deploy like any
other, so every environment stays in step. Production tables keep taking
writes while a concurrent index builds. See elements man migrations.
Moving to a Managed Database
A prototype's data moves to the provider's managed Postgres in one sitting: create the database as in AWS (opens in a new tab) or DigitalOcean (opens in a new tab), then from the app machine, which is the one place that reaches both.
Terminalelements maint on # the page goes up on every machine elements ssh # a shell in /elements/app on production0
On the machine, dump with the box's own pg_dump so versions match, and
restore into the new server. The app database is the one named in
config.jsoc; Elements makes the test database on the new server itself at
the next build.
Terminalpg_dump -h 127.0.0.1 -p 5433 -U postgres -Fc my_app > /tmp/my_app.dump createdb -h <endpoint> -p 5432 -U elements my_app pg_restore -h <endpoint> -p 5432 -U elements -d my_app --no-owner /tmp/my_app.dump
Back on the laptop, point production.env at the new server as in
Setup (opens in a new tab), deploy, compare a few row counts with elements db -remote,
and take the page down. The data on the box stays until the counts match.
Terminalelements deploy -json elements db -remote -c "select count(*) from users" elements maint off
Redirects
The www pair of every server.domain value is automatic: the load balancer
301s www.example.com to example.com (or example.com to www.example.com
when the value is the www form), keeping the path and query. There is no
config for it.
A redirect from one domain to another is a route. Give the old host a key in
server.domain and send it on from a handler:
config.jsocserver: { domain: { default: env!("DOMAIN"), "old.example.com": env!("DOMAIN_OLD"), }, },
index.tsapp.route({ domain: "old.example.com", path: "/*", handler: (req, res) => { res.redirect(`https://example.com${req.url}`, 301); }, });
Packages
OS packages are installed on every deploy machine during provisioning.
config.jsocdeploy: { packages: ["ffmpeg", "imagemagick"], }
Elements diffs the package list against the per-machine state, so
already-installed packages are not reinstalled. Per-environment or per-machine
packages entries extend this list.
preDeploy
Local commands run before any service builds and before any SSH. They run sequentially and fail fast.
config.jsocdeploy: { preDeploy: [ "npm run lint", "go build ./...", ], }
Use for cross-cutting prep that doesn't belong to a single service.
Logging In to a Machine
elements deploy -json reports every failure, local and remote, and it is the
only thing a deploy needs. The commands below are for a human operator holding a
question the diagnostics did not answer. They are not a step in a deploy, and
reaching for one after a failed deploy trades a report the user can act on for a
tour of a machine.
Terminalelements ssh -remote=production # shell in /elements/app on the first production machine elements ssh -remote=production#production2 # specific machine elements build -remote=production -json # query build state on the machine
elements ssh opens its shell in the app, /elements/app. Every Elements
service on the machine logs to the systemd journal. elements logs -remote
reads it from your machine (see elements man logs), and on the machine
journalctl -u elementsd shows the project server's.
elements db -remote=production is not in that category. It is the normal way
to run SQL against a deployed database, including the statements above.
Related
- Build:
elements man build. The deploy is a build that ships the release to a remote machine. - Migrations:
elements man migrations. The frozen-after-deploy rule and transaction batching. - Database:
elements man database. The bundled Postgres cluster and the