Deploying and updates
This page explains what cairn deploy does, what happens when a deploy fails or is interrupted, and how to stop, start, pause and resume an app. Restarts and health checks are in Health and restarts. Every cairn.toml key is in cairn.toml.
In short
cairn deploybuilds a new VM while the old one keeps running. Then it stops the old VM, starts the new one, and waits until the new one is ready.- An app runs one VM at a time, so clients see a short gap during each deploy.
- If the new VM never becomes ready, cairn starts the old one again. This is a rollback.
- Every deploy builds a fresh root disk. Data you want to keep belongs on a volume.
Deploy
cairn deploy # reads ./cairn.toml
cairn deploy ./shop # or another project directory
cairn deploy --dry-run # show the plan, with every default filled in; change nothingA deploy goes through these steps:
- Get ready. It finishes any earlier deploy that was interrupted, sets up the app's network, and pulls or builds the image.
- Build the new VM (a root disk and a small config disk). The old VM is still running.
- Stop the old VM and make the new one current.
- Start the new VM. Its ports are published as it connects to the network.
- Wait until it is ready (see below).
- Accept it and delete the old VM.
Changes to the manifest, your .env file or the environment only take effect on a deploy. Restarts and cairn start reuse what the last deploy saved.
When is a VM "ready"?
Before it accepts the new VM, cairn waits until:
- every TCP
internal_portin[[services]]accepts connections, and - every health check you defined passes.
An app with no services and no checks is ready as soon as its program starts.
The wait has a time limit: 60 seconds by default. Change it with --wait-timeout (1 second to 1 hour). --no-checks skips the wait entirely, although health checks still run afterwards.
The new VM serves traffic before it is "ready"
The new VM's ports are reachable as soon as your program listens on them, which is usually before the wait ends. If the new VM then fails its checks and the deploy rolls back, some clients will have been served briefly by the version that was rolled back.
When a deploy fails
| It fails… | What happens |
|---|---|
| Before the old VM is stopped (the image won't pull, the build fails, you press Ctrl+C) | The new VM is deleted. The old one never noticed. |
| After the old VM is stopped (the new one won't boot, isn't ready in time, you press Ctrl+C) | Rollback: the old VM becomes current again and is started. The new one is removed, but its logs are kept until the next successful deploy so you can see what went wrong. |
| After the new VM is accepted | The deploy succeeded. If some cleanup couldn't be done, it's reported and retried by the next deploy. |
Two exceptions:
- If the old VM was already stopped before the deploy, it stays stopped after a rollback.
- For an app with volumes, a rollback may hold the app instead of starting the old version, because the new version may already have changed the data. See Volumes: failed deploys.
Ctrl+C. The first Ctrl+C cancels the deploy cleanly, which deletes the new VM or rolls back as above. A second Ctrl+C exits straight away and leaves the deploy unfinished.
The deploy's result names exactly where the app ended up, for example previous_running. All the values are listed in the command line reference.
If a deploy is interrupted
A deploy can be cut short by a crash, a power cut or a second Ctrl+C. cairn records each step before it takes it, in ~/.cairn/apps/<app>/deploy.json, so it always knows how far the deploy got. cairn status <app> tells you when there's an unfinished deploy.
The next cairn deploy, start, stop or destroy of the app tidies it up first, and says so:
an earlier deploy of web did not finish (it was starting machine 4N8KDQ2W7R); settling it
rolling back: machine YJ449SSML5 is current again| The deploy had got as far as… | Tidying up… |
|---|---|
| building the new VM, or stopping the old one | deletes the new VM, which never ran |
| starting the new VM, or rolling back | rolls back to the old VM (or holds the app, if it has volumes) |
| accepting the new VM | finishes the cleanup, and keeps the new VM |
Tidying up never starts anything
After tidying up, the command does its own job: start starts, deploy deploys, stop stops. So if the deploy was interrupted after it stopped the old VM, the app stays down until you run cairn start <app> or deploy again.
This includes reboots. If the power went out while a new VM was starting, the app doesn't come back at boot. Run cairn start <app> afterwards. If the app is already running, it does nothing.
What other commands do while a deploy is unfinished
cairn status,psandexecwork as usual.execruns in the current VM if it's running.cairn pause,resume, and thecairn volumecommands that change a volume refuse, and name the commands that finish the deploy.- If tidying up can't finish (the new VM won't stop, for example),
deploy.jsonstays, andcairn startandcairn deployrefuse withUnsettled.cairn stop <app>stops the VMs and finishes it. - When only cleanup is left over (a file or a systemd unit that couldn't be removed),
cairn statussays "cleanup left". Nothing is refused, and the next deploy retries it. The exception is the start-at-boot setting: until that's applied, the next deploy refuses. cairn doctorlists every app with an unfinished deploy.
Stop, start and destroy
cairn stop web # shut the VM down; keep its disks, ports and network
cairn stop web --timeout 60s # allow longer for a graceful shutdown
cairn start web # boot the same VM again
cairn destroy web # delete the app and its VMs; keep its volumes and networkHow a stop works
- cairn asks the VM to shut down, and the VM sends your program its stop signal.
- Your program gets 10 seconds (by default) to exit. After that it is killed.
- If the whole VM hasn't exited within the stop timeout, cairn kills it.
The stop signal is the first of these that's set:
kill_signalin yourcairn.toml;- the image's own
StopSignal(for exampleSIGQUITfor nginx,SIGINTfor Postgres); SIGTERM.
kill_signal = "SIGINT"
kill_timeout = "30s" # how long your program gets; 1s to 5mThe stop timeout is 20 seconds, or kill_timeout plus 10 seconds if that's longer. cairn stop --timeout overrides it, up to 310 seconds. SIGKILL, SIGSTOP, SIGCHLD and the job-control signals can't be used as a stop signal.
After a stop
cairn start <app>boots the same VM again, on the same disk. It waits up to 250 seconds for the VM to start (--wait), but doesn't wait for it to become ready.- The app keeps its published ports while it's stopped, so nothing else can take them.
cairn destroy <app>deletes the VM, its disks and records. It keeps the network, and it keeps the volumes unless you add--volumes.
For apps made by cairn run, stop and destroy first sync the agent's work back to your folder. See Run an agent.
Pause and resume
cairn pause web
cairn resume webmachine YJ449SSML5 paused; it keeps its memory and its ports. New connections to a 127.0.0.1-published port are reset after about 3 s while it is paused (cairn-netd's proxy). `cairn resume web` continues it
machine YJ449SSML5 runningPausing freezes the VM in memory. It keeps its memory and ports, but doesn't answer, and new connections are reset after about 3 seconds. resume carries on exactly where it stopped.
- You can only pause a running VM. A stopped VM needs
cairn start, notresume. - A pause is refused while the VM is starting, stopping or between restarts, and while a deploy of the app is running or unfinished.
- A VM always boots unpaused.
Frozen VMs, and pauses made outside cairn
A VM can also end up frozen: its process was stopped with a signal (SIGSTOP), or its systemd unit was frozen with systemctl --user freeze. cairn resume releases those too. It sends SIGCONT, or runs systemctl --user thaw. A VM held by a debugger is left alone.
A pause made directly through Firecracker's API socket is noticed within about 45 seconds. Until then cairn status says running.
Start at boot
When the systemd launcher is used (the default where systemd is available), apps run under your systemd user manager. They start again when the computer boots, but only if linger is on for your user:
loginctl enable-linger $USER
cairn doctor- Apps you stopped stay stopped after a reboot.
- Held apps aren't started.
- With
--launcher direct, or without systemd, apps don't come back after a reboot.
See Install.
Choose how the VM is launched
cairn deploy --launcher systemd # under your systemd user manager (the default when available)
cairn deploy --launcher direct # cairn starts it itself; no start at boot
cairn deploy --launcher auto # keep whatever this app used last timeMemory
[vm] memory is the most memory the VM can use. It isn't reserved up front. Your computer only hands over memory as the VM actually uses it.
By default, memory the VM frees is given back to your computer within a few seconds, through a balloon device. Some memory stays: the VM's file cache, small free blocks, and memory a program has freed but kept for itself. Giving memory back and taking it again costs a little time. If your program frees and re-allocates large buffers all the time, turn it off:
[vm]
memory = "2gb"
balloon = falseThe change takes effect on the next deploy. See cairn.toml for cpus, memory and rootfs_size.
How it works
Each running VM has a small supervisor process. It boots the VM, receives the VM's events (over a virtual socket, vsock), applies the restart policy, and writes a run file that cairn ps and cairn status read.
When systemd is the launcher, systemd restarts the supervisor itself if cairn's own machinery fails. The supervisor tells systemd what happened through its exit code: 0 done, 1 cairn's machinery failed (restart it), 64 the VM can't start (don't restart), 65 gave up after too many crashes (don't restart).
Booting has deadlines: 30 seconds until the VM's first message, then 10 seconds until your program starts.
The deploy's steps are recorded in deploy.json as building, stopping, starting, accepted and rolling-back. See Architecture for the full sequence.