Skip to main content

State Providers

Octo's state decides who can run your infrastructure. Kept on a laptop, only the person with the laptop can. Kept in a repository, anybody with the repository can, as long as only one of them runs at a time, and nothing stops two machines from trying.

A state provider is where the state lives. It holds Octo's record, Terraform's state for every module, and the lock that keeps two runs from writing at once. settings.stateProvider picks one, and three ship with Octo:

  • LocalStateProvider keeps everything in a directory on one machine. It is the right choice for one person, and the one a new project starts with.
  • RemoteStateProvider keeps everything in a backend every machine reaches, which is what a team needs.
  • TestStateProvider keeps everything in memory, for tests.

Local state​

octo.yaml
settings:
stateProvider:
type: LocalStateProvider
statePath: .octo

The files are plain JSON and Terraform state, readable and diffable, and a new project commits them. The lock is a file beside them, local-state.lock, which keeps two runs on the same machine apart. Two machines each have their own copy of that file, so they are not kept apart at all; that is the point at which a team moves to remote state.

Octo records every Terraform output it maps to a resource. A module that returned a credential as part of a resource's response would put it in resources-actual.json in plain text, and Terraform's own state has always held every attribute a provider returned. Keep a repository holding state readable only by the people who run Octo.

Terraform's state​

Terraform keeps its own record of what exists, and by default leaves it in the folder it ran in, where only that machine can see it. Lose it and Terraform plans to create everything again.

So Octo keeps it in the state provider too. Before any Terraform command, Octo writes each module's state from the provider into its folder, and afterwards, whether the command succeeded or failed, it writes the folder's state back. A long apply is also saved every ten seconds, so a runner killed halfway through loses at most the last few seconds of record.

A project that has been running Terraform with a local backend can move to Octo's state as it is: the first command finds the state in the folder, and saves it to the provider.

Remote state​

octo.yaml
env:
- key: STATE_SESSION_ID
kind: string
value: ""

settings:
stateProvider:
type: RemoteStateProvider
organizationId: shop-organization # your organization
appId: shop # the app in Octo
sessionId: "${env.STATE_SESSION_ID}"
statePath: .octo

sessionId is the personal access token every request is made with. It is a credential, so declare it under env and let the shell supply it as OCTO_ENV_STATE_SESSION_ID. Two keys are optional: lockContext, what somebody blocked by your lock is told about who holds it, which defaults to octo on <hostname>; and checkInIntervalInSeconds, how often between 5 and 60 seconds an apply saves Terraform's state, which defaults to 10.

statePath is a working copy, and the backend is the copy that counts, so add .octo/ to your .gitignore. Each command works on the directory exactly as it would with local state, and the provider keeps the backend in step:

  1. generate replaces the directory with the newest state a finished run left in the backend, and plans against that.
  2. lock opens a record for the run. If another run finished after your generate, it refuses with App state moved since it was last refreshed!, and you run generate again. On this provider, generate always comes before lock.
  3. Every write during the run reaches the backend before the command moves on, and Terraform's state follows on every check-in.
  4. A commit that succeeds, or an unlock, finishes the record, and that is the state the next generate reads.

The backend never plans anybody else's run against an unfinished record. A run that died leaves its record unfinished and its lock held, and any machine can finish it, as the account that took the lock or as an administrator of your organization. Recovering a Failed Run has the steps.

To move a project from local state onto this provider, change settings.stateProvider and run the lifecycle as usual. While the backend holds nothing for the app, the first lock uploads what the directory holds.

The lock​

Neither provider's lock expires. A lock that expired mid-apply would let a second apply start while the first might still be running, and from outside nobody can tell whether a dead run applied nothing, everything, or half. So a run that died is finished through its own lock, never released by somebody else. The message from a refused lock says how long the lock has been held, which is usually enough to tell a slow run from a dead one.

Releasing the lock seals the change: a copy of the state, with the change's journal, goes into .octo/archives/. The newest archive is what an app building on this one reads.

Going back​

No provider gives you a way back to an earlier state. Octo writes each file whole, every time, and has no command that restores an old version; going back comes from wherever the files live, such as the history of the repository holding them. Restoring old state does not undo infrastructure either. It makes Terraform forget what exists, and the next apply tries to build it again.