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 the load balancer proxies requests for
the service's domains to that port.
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. Fix migration 7 and deploy again: that deploy applies 7 through 10. The running release serves traffic while migrations apply, so write each migration so that the running release still works against the new schema.
Migrations are forward-only. There is no automatic rollback for a migration that has already succeeded. If a deploy applies a migration that is wrong, correct the schema 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 with the @migration tx=false build tag. The deploy applies it
like any other migration, so every environment runs the same statements. Production tables keep taking
writes while a concurrent index builds. See elements man migrations.
Moving to a Managed Database
To move a prototype's data to the provider's managed Postgres, create the database as in AWS (opens in a new tab) or DigitalOcean (opens in a new tab). Then copy the data from the app machine, which is the only machine that can connect to both the bundled Postgres and the new database.
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 machine'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) and deploy. Compare a few row counts with
elements db -remote, then turn the maintenance page off with
elements maint off. Keep the data on the machine 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 redirect its requests in a route 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 one after another, and the deploy stops at the first command that fails.
config.jsocdeploy: { preDeploy: [ "npm run lint", "go build ./...", ], }
Use preDeploy for a command that is not the build of one service, such as
a lint run.
Logging In to a Machine
elements deploy -json reports every failure, local and remote, and it is the
only command a deploy needs. The commands below are for a person who has a
question the diagnostics did not answer. They are not a step in a deploy.
After a failed deploy, report the diagnostics to the user and do not run these
commands.
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 build server's.
elements db -remote=production is for everyday use. 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 copies the changed files to a remote machine and produces the release there. - Migrations:
elements man migrations. The frozen-after-deploy rule and transaction batching. - Database:
elements man database. The bundled Postgres cluster and the app and test databases.