# ScalWS Console: configuration workflow

URL: https://scalws.com/docs/console/config-workflow/
Source: `docs/console-config-workflow.md` in the ScalWS repository

The console never edits the active configuration in place. Every change is a transaction
over the configuration file the controller reads (`controller.config` in
`controller.yaml`), and the controller turns the file into a new generation.

```text
Current ─clone─▶ Draft ─edit (raw or structured)─▶ Validate ─▶ Diff + impact ─▶ Confirm
   ─▶ Apply: validating → preparing → applying → health_check → complete
                                                            └──▶ rolled_back / failed
```

## Drafts

* A draft is a copy of the current file (or of a history entry) with the file's SHA-256 at
  that moment (its base). Drafts live in the console's memory and end when it restarts.
* Settings pages: every field of scalws.yaml and controller.yaml, generated from the
  configuration's JSON Schema and grouped as Server, Listeners & TLS, Tuning, Security,
  Cache, Logging & metrics, Sites, PHP / FastCGI, Proxy & applications, Advanced and
  Adaptive Runtime (controller.yaml). Each input shows the active value; an absent key
  shows the effective default; Reset removes a key, Undo returns to the active value; a
  search box filters one section, "search all" every section. Edits are saved to the draft
  half a second after typing stops.
* Raw editor: the YAML text of the same draft, comments kept. Saving from the forms
  rewrites the file canonically (comments and inline `{…}` style are not kept), which is
  why review lists changed settings first and folds the text diff.
* Secrets (password/secret/token keys, every value under `env:`) appear as
  `__REDACTED_n__`. The originals stay in the draft on the server; a placeholder left in
  place keeps its value.

## Validation

1. `scalws-config`: parsing and the same semantic validation `scalwsd` and the controller
   use (errors with their path, warnings).
   A draft's `include` and `certificates_file` may only name files below the
   configuration directory (also after symbolic links); those files are checked like
   site files (owner root or the main file's owner, not writable by group or others), and a
   file that does not parse is reported by name and position only, never its content.
   The same holds for the other files the checks read: a route's `basic_auth.users_file`,
   certificate and key files, `client_ca`, `ca_bundle`, `tls_ca_file` and `ca_file`,
   `client_ca_file`, `client_cert_file` and `client_key_file`, the `key_file` of a pool's
   `hash` and of a route's `sticky`, a respond runtime's `body_file`,
   `server.networks.<name>.file` and `server.geoip.database` must be absolute paths below
   the configuration directory, unless the configuration file on the server already names
   that file for the same setting (or, for the GeoIP database, it is
   `/var/lib/scalws/geoip/country.mmdb`, where `scalwsctl geoip update` writes it).
2. `scalwsd --check` with the draft (the controller's `webd_binary`): certificates load,
   profiles and runtimes are prepared, nothing is bound or started.
3. Impact: listener additions, moves and removals, HTTP/3 on/off (UDP socket and QUIC
   demux), TLS changes, applications added/changed/removed, server sections changed. The
   controller remains the judge: a change that needs a restart is rejected by its reload
   and the current generation stays.
4. Changed settings (path, before, after; secret values shown as "(secret)"), the text
   diff against the current file (redacted), and whether the file is still the draft's
   base.

## Apply

| State | What happens | On failure |
|---|---|---|
| validating | the file's hash must equal the draft's base (else `conflict`); the draft must change something and validate | nothing written |
| preparing | controller status read; a rollout in progress refuses the apply; the current file is recorded in history | nothing written |
| applying | new file written atomically (temporary file in the same directory, original owner and mode, fsync, rename, directory fsync); `POST /reload` | reload refused (bad listener, port in use, restart needed): previous file restored, generation unchanged → `failed` |
| health_check | until the new generation has as many active instances as desired, nothing starting or draining and no instance of an older generation active (timeout `--health-timeout-secs`, default 120 s); a controller rollback of that generation or failed instances end it early | previous file restored, `POST /reload`, wait for that generation → `rolled_back` |
| complete | new history entry with the generation | |

`POST /reload` answers when the controller has finished the roll, and the console waits up
to 200 s for it (the controller's own bound is 180 s). The outcomes:

| Controller answer | Meaning | The console |
|---|---|---|
| `200` | the new generation serves alone, old instances are gone | goes on to `health_check`; the new file stays |
| `409` | the generation was rolled back or replaced by a later reload | restores the previous file; the new configuration is **not** in place |
| `504` | the generation has not taken over within 180 s; the roll goes on | reports the reload as still in progress and keeps the new file |
| no answer within 200 s | unknown | as for `504` |
| refused (bad listener, port in use, restart needed) | no generation was made | restores the previous file |

Only `200` means the configuration is in place everywhere. That matters for a `read_only`
fence ([replicas guide](https://scalws.com/docs/guide/replicas/#fencing-with-read_only)): after `409`, `504` or a
timeout the fence is **not** established and writes may still reach the upstreams.
`SIGHUP` is not an acknowledged reload at all.

One apply runs at a time. Each apply carries an idempotency key: repeating the request
(browser retry) returns the same operation instead of applying twice. A browser that loses
the connection can find the operation under Configuration → Operations; the operation
continues on the server.

## History and rollback

Every configuration the file had is kept in `/var/lib/scalws-console/history/<seq>.yaml`
with an index (time, actor, kind, hash, generation): `baseline` (found at console start),
`apply`, `rollback` (automatic restore), `external` (the file changed outside the
console). Restoring an entry is an apply of that text: a new generation, the same checks;
history is never rewritten. Automatic rollbacks by the console and by the controller are
shown on the Overview.

## Concurrency between operators

* Optimistic: an apply whose base is not the current file is refused (`conflict`); the
  review page offers a new draft from the current file.
* Drafts belong to their creator (administrators may act on any).
* Changes made outside the console (an editor on the host, configuration management) are
  detected by hash and recorded as `external` history entries.

## controller.yaml

Drafts of controller.yaml follow the same flow with two differences: validation runs
`scalws-controller check-config` (CPUs per instance, min/max, thresholds), and apply
restarts the controller with the operator-configured `--controller-restart-command`
(instances keep serving and are adopted), then waits until the controller answers with its
generation settled. If it does not settle in time, the previous file is restored and the
controller restarted again. Without a restart command the validated file is written and
takes effect at the next restart (said so before and after applying).

## Runtime actions (no file change)

Desired instance count (within `instances.min..max`), autoscaling mode and cache purge go
straight to the controller API, are confirmed in the UI with their impact, and are
audited. `instances.*`, CPUs and autoscaling thresholds are edited under Configuration →
Adaptive Runtime (controller.yaml, above).
